org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.22a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.22a"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. ;; FIXME: Needs a separate group...
  138. (defcustom org-completion-fallback-command 'hippie-expand
  139. "The expansion command called by \\[org-complete] in normal context.
  140. Normal means, no org-mode-specific context."
  141. :group 'org
  142. :type 'function)
  143. (defgroup org-startup nil
  144. "Options concerning startup of Org-mode."
  145. :tag "Org Startup"
  146. :group 'org)
  147. (defcustom org-startup-folded t
  148. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  149. This can also be configured on a per-file basis by adding one of
  150. the following lines anywhere in the buffer:
  151. #+STARTUP: fold
  152. #+STARTUP: nofold
  153. #+STARTUP: content"
  154. :group 'org-startup
  155. :type '(choice
  156. (const :tag "nofold: show all" nil)
  157. (const :tag "fold: overview" t)
  158. (const :tag "content: all headlines" content)))
  159. (defcustom org-startup-truncated t
  160. "Non-nil means, entering Org-mode will set `truncate-lines'.
  161. This is useful since some lines containing links can be very long and
  162. uninteresting. Also tables look terrible when wrapped."
  163. :group 'org-startup
  164. :type 'boolean)
  165. (defcustom org-startup-align-all-tables nil
  166. "Non-nil means, align all tables when visiting a file.
  167. This is useful when the column width in tables is forced with <N> cookies
  168. in table fields. Such tables will look correct only after the first re-align.
  169. This can also be configured on a per-file basis by adding one of
  170. the following lines anywhere in the buffer:
  171. #+STARTUP: align
  172. #+STARTUP: noalign"
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-insert-mode-line-in-empty-file nil
  176. "Non-nil means insert the first line setting Org-mode in empty files.
  177. When the function `org-mode' is called interactively in an empty file, this
  178. normally means that the file name does not automatically trigger Org-mode.
  179. To ensure that the file will always be in Org-mode in the future, a
  180. line enforcing Org-mode will be inserted into the buffer, if this option
  181. has been set."
  182. :group 'org-startup
  183. :type 'boolean)
  184. (defcustom org-replace-disputed-keys nil
  185. "Non-nil means use alternative key bindings for some keys.
  186. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  187. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  188. If you want to use Org-mode together with one of these other modes,
  189. or more generally if you would like to move some Org-mode commands to
  190. other keys, set this variable and configure the keys with the variable
  191. `org-disputed-keys'.
  192. This option is only relevant at load-time of Org-mode, and must be set
  193. *before* org.el is loaded. Changing it requires a restart of Emacs to
  194. become effective."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (if (fboundp 'defvaralias)
  198. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  199. (defcustom org-disputed-keys
  200. '(([(shift up)] . [(meta p)])
  201. ([(shift down)] . [(meta n)])
  202. ([(shift left)] . [(meta -)])
  203. ([(shift right)] . [(meta +)])
  204. ([(control shift right)] . [(meta shift +)])
  205. ([(control shift left)] . [(meta shift -)]))
  206. "Keys for which Org-mode and other modes compete.
  207. This is an alist, cars are the default keys, second element specifies
  208. the alternative to use when `org-replace-disputed-keys' is t.
  209. Keys can be specified in any syntax supported by `define-key'.
  210. The value of this option takes effect only at Org-mode's startup,
  211. therefore you'll have to restart Emacs to apply it after changing."
  212. :group 'org-startup
  213. :type 'alist)
  214. (defun org-key (key)
  215. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  216. Or return the original if not disputed."
  217. (if org-replace-disputed-keys
  218. (let* ((nkey (key-description key))
  219. (x (org-find-if (lambda (x)
  220. (equal (key-description (car x)) nkey))
  221. org-disputed-keys)))
  222. (if x (cdr x) key))
  223. key))
  224. (defun org-find-if (predicate seq)
  225. (catch 'exit
  226. (while seq
  227. (if (funcall predicate (car seq))
  228. (throw 'exit (car seq))
  229. (pop seq)))))
  230. (defun org-defkey (keymap key def)
  231. "Define a key, possibly translated, as returned by `org-key'."
  232. (define-key keymap (org-key key) def))
  233. (defcustom org-ellipsis nil
  234. "The ellipsis to use in the Org-mode outline.
  235. When nil, just use the standard three dots. When a string, use that instead,
  236. When a face, use the standart 3 dots, but with the specified face.
  237. The change affects only Org-mode (which will then use its own display table).
  238. Changing this requires executing `M-x org-mode' in a buffer to become
  239. effective."
  240. :group 'org-startup
  241. :type '(choice (const :tag "Default" nil)
  242. (face :tag "Face" :value org-warning)
  243. (string :tag "String" :value "...#")))
  244. (defvar org-display-table nil
  245. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  246. (defgroup org-keywords nil
  247. "Keywords in Org-mode."
  248. :tag "Org Keywords"
  249. :group 'org)
  250. (defcustom org-deadline-string "DEADLINE:"
  251. "String to mark deadline entries.
  252. A deadline is this string, followed by a time stamp. Should be a word,
  253. terminated by a colon. You can insert a schedule keyword and
  254. a timestamp with \\[org-deadline].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-scheduled-string "SCHEDULED:"
  259. "String to mark scheduled TODO entries.
  260. A schedule is this string, followed by a time stamp. Should be a word,
  261. terminated by a colon. You can insert a schedule keyword and
  262. a timestamp with \\[org-schedule].
  263. Changes become only effective after restarting Emacs."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defcustom org-closed-string "CLOSED:"
  267. "String used as the prefix for timestamps logging closing a TODO entry."
  268. :group 'org-keywords
  269. :type 'string)
  270. (defcustom org-clock-string "CLOCK:"
  271. "String used as prefix for timestamps clocking work hours on an item."
  272. :group 'org-keywords
  273. :type 'string)
  274. (defcustom org-comment-string "COMMENT"
  275. "Entries starting with this keyword will never be exported.
  276. An entry can be toggled between COMMENT and normal with
  277. \\[org-toggle-comment].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-quote-string "QUOTE"
  282. "Entries starting with this keyword will be exported in fixed-width font.
  283. Quoting applies only to the text in the entry following the headline, and does
  284. not extend beyond the next headline, even if that is lower level.
  285. An entry can be toggled between QUOTE and normal with
  286. \\[org-toggle-fixed-width-section]."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defconst org-repeat-re
  290. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  291. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  292. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  293. "Regular expression for specifying repeated events.
  294. After a match, group 1 contains the repeat expression.")
  295. (defgroup org-structure nil
  296. "Options concerning the general structure of Org-mode files."
  297. :tag "Org Structure"
  298. :group 'org)
  299. (defgroup org-reveal-location nil
  300. "Options about how to make context of a location visible."
  301. :tag "Org Reveal Location"
  302. :group 'org-structure)
  303. (defconst org-context-choice
  304. '(choice
  305. (const :tag "Always" t)
  306. (const :tag "Never" nil)
  307. (repeat :greedy t :tag "Individual contexts"
  308. (cons
  309. (choice :tag "Context"
  310. (const agenda)
  311. (const org-goto)
  312. (const occur-tree)
  313. (const tags-tree)
  314. (const link-search)
  315. (const mark-goto)
  316. (const bookmark-jump)
  317. (const isearch)
  318. (const default))
  319. (boolean))))
  320. "Contexts for the reveal options.")
  321. (defcustom org-show-hierarchy-above '((default . t))
  322. "Non-nil means, show full hierarchy when revealing a location.
  323. Org-mode often shows locations in an org-mode file which might have
  324. been invisible before. When this is set, the hierarchy of headings
  325. above the exposed location is shown.
  326. Turning this off for example for sparse trees makes them very compact.
  327. Instead of t, this can also be an alist specifying this option for different
  328. contexts. Valid contexts are
  329. agenda when exposing an entry from the agenda
  330. org-goto when using the command `org-goto' on key C-c C-j
  331. occur-tree when using the command `org-occur' on key C-c /
  332. tags-tree when constructing a sparse tree based on tags matches
  333. link-search when exposing search matches associated with a link
  334. mark-goto when exposing the jump goal of a mark
  335. bookmark-jump when exposing a bookmark location
  336. isearch when exiting from an incremental search
  337. default default for all contexts not set explicitly"
  338. :group 'org-reveal-location
  339. :type org-context-choice)
  340. (defcustom org-show-following-heading '((default . nil))
  341. "Non-nil means, show following heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the heading following the
  344. match is shown.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type org-context-choice)
  352. (defcustom org-show-siblings '((default . nil) (isearch t))
  353. "Non-nil means, show all sibling heading when revealing a location.
  354. Org-mode often shows locations in an org-mode file which might have
  355. been invisible before. When this is set, the sibling of the current entry
  356. heading are all made visible. If `org-show-hierarchy-above' is t,
  357. the same happens on each level of the hierarchy above the current entry.
  358. By default this is on for the isearch context, off for all other contexts.
  359. Turning this off for example for sparse trees makes them very compact,
  360. but makes it harder to edit the location of the match. In such a case,
  361. use the command \\[org-reveal] to show more context.
  362. Instead of t, this can also be an alist specifying this option for different
  363. contexts. See `org-show-hierarchy-above' for valid contexts."
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-entry-below '((default . nil))
  367. "Non-nil means, show the entry below a headline when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the text below the headline that is
  370. exposed is also shown.
  371. By default this is off for all contexts.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defgroup org-cycle nil
  377. "Options concerning visibility cycling in Org-mode."
  378. :tag "Org Cycle"
  379. :group 'org-structure)
  380. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  381. "Names of drawers. Drawers are not opened by cycling on the headline above.
  382. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  383. this:
  384. :DRAWERNAME:
  385. .....
  386. :END:
  387. The drawer \"PROPERTIES\" is special for capturing properties through
  388. the property API.
  389. Drawers can be defined on the per-file basis with a line like:
  390. #+DRAWERS: HIDDEN STATE PROPERTIES"
  391. :group 'org-structure
  392. :type '(repeat (string :tag "Drawer Name")))
  393. (defcustom org-cycle-global-at-bob nil
  394. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  395. This makes it possible to do global cycling without having to use S-TAB or
  396. C-u TAB. For this special case to work, the first line of the buffer
  397. must not be a headline - it may be empty ot some other text. When used in
  398. this way, `org-cycle-hook' is disables temporarily, to make sure the
  399. cursor stays at the beginning of the buffer.
  400. When this option is nil, don't do anything special at the beginning
  401. of the buffer."
  402. :group 'org-cycle
  403. :type 'boolean)
  404. (defcustom org-cycle-emulate-tab t
  405. "Where should `org-cycle' emulate TAB.
  406. nil Never
  407. white Only in completely white lines
  408. whitestart Only at the beginning of lines, before the first non-white char
  409. t Everywhere except in headlines
  410. exc-hl-bol Everywhere except at the start of a headline
  411. If TAB is used in a place where it does not emulate TAB, the current subtree
  412. visibility is cycled."
  413. :group 'org-cycle
  414. :type '(choice (const :tag "Never" nil)
  415. (const :tag "Only in completely white lines" white)
  416. (const :tag "Before first char in a line" whitestart)
  417. (const :tag "Everywhere except in headlines" t)
  418. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  419. ))
  420. (defcustom org-cycle-separator-lines 2
  421. "Number of empty lines needed to keep an empty line between collapsed trees.
  422. If you leave an empty line between the end of a subtree and the following
  423. headline, this empty line is hidden when the subtree is folded.
  424. Org-mode will leave (exactly) one empty line visible if the number of
  425. empty lines is equal or larger to the number given in this variable.
  426. So the default 2 means, at least 2 empty lines after the end of a subtree
  427. are needed to produce free space between a collapsed subtree and the
  428. following headline.
  429. Special case: when 0, never leave empty lines in collapsed view."
  430. :group 'org-cycle
  431. :type 'integer)
  432. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  433. org-cycle-hide-drawers
  434. org-cycle-show-empty-lines
  435. org-optimize-window-after-visibility-change)
  436. "Hook that is run after `org-cycle' has changed the buffer visibility.
  437. The function(s) in this hook must accept a single argument which indicates
  438. the new state that was set by the most recent `org-cycle' command. The
  439. argument is a symbol. After a global state change, it can have the values
  440. `overview', `content', or `all'. After a local state change, it can have
  441. the values `folded', `children', or `subtree'."
  442. :group 'org-cycle
  443. :type 'hook)
  444. (defgroup org-edit-structure nil
  445. "Options concerning structure editing in Org-mode."
  446. :tag "Org Edit Structure"
  447. :group 'org-structure)
  448. (defcustom org-special-ctrl-a/e nil
  449. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  450. When t, `C-a' will bring back the cursor to the beginning of the
  451. headline text, i.e. after the stars and after a possible TODO keyword.
  452. In an item, this will be the position after the bullet.
  453. When the cursor is already at that position, another `C-a' will bring
  454. it to the beginning of the line.
  455. `C-e' will jump to the end of the headline, ignoring the presence of tags
  456. in the headline. A second `C-e' will then jump to the true end of the
  457. line, after any tags.
  458. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  459. and only a directly following, identical keypress will bring the cursor
  460. to the special positions."
  461. :group 'org-edit-structure
  462. :type '(choice
  463. (const :tag "off" nil)
  464. (const :tag "after bullet first" t)
  465. (const :tag "border first" reversed)))
  466. (if (fboundp 'defvaralias)
  467. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  468. (defcustom org-special-ctrl-k nil
  469. "Non-nil means `C-k' will behave specially in headlines.
  470. When nil, `C-k' will call the default `kill-line' command.
  471. When t, the following will happen while the cursor is in the headline:
  472. - When the cursor is at the beginning of a headline, kill the entire
  473. line and possible the folded subtree below the line.
  474. - When in the middle of the headline text, kill the headline up to the tags.
  475. - When after the headline text, kill the tags."
  476. :group 'org-edit-structure
  477. :type 'boolean)
  478. (defcustom org-odd-levels-only nil
  479. "Non-nil means, skip even levels and only use odd levels for the outline.
  480. This has the effect that two stars are being added/taken away in
  481. promotion/demotion commands. It also influences how levels are
  482. handled by the exporters.
  483. Changing it requires restart of `font-lock-mode' to become effective
  484. for fontification also in regions already fontified.
  485. You may also set this on a per-file basis by adding one of the following
  486. lines to the buffer:
  487. #+STARTUP: odd
  488. #+STARTUP: oddeven"
  489. :group 'org-edit-structure
  490. :group 'org-font-lock
  491. :type 'boolean)
  492. (defcustom org-adapt-indentation t
  493. "Non-nil means, adapt indentation when promoting and demoting.
  494. When this is set and the *entire* text in an entry is indented, the
  495. indentation is increased by one space in a demotion command, and
  496. decreased by one in a promotion command. If any line in the entry
  497. body starts at column 0, indentation is not changed at all."
  498. :group 'org-edit-structure
  499. :type 'boolean)
  500. (defcustom org-blank-before-new-entry '((heading . nil)
  501. (plain-list-item . nil))
  502. "Should `org-insert-heading' leave a blank line before new heading/item?
  503. The value is an alist, with `heading' and `plain-list-item' as car,
  504. and a boolean flag as cdr."
  505. :group 'org-edit-structure
  506. :type '(list
  507. (cons (const heading) (boolean))
  508. (cons (const plain-list-item) (boolean))))
  509. (defcustom org-insert-heading-hook nil
  510. "Hook being run after inserting a new heading."
  511. :group 'org-edit-structure
  512. :type 'hook)
  513. (defcustom org-enable-fixed-width-editor t
  514. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  515. This currently only means, they are never auto-wrapped.
  516. When nil, such lines will be treated like ordinary lines.
  517. See also the QUOTE keyword."
  518. :group 'org-edit-structure
  519. :type 'boolean)
  520. (defcustom org-goto-auto-isearch t
  521. "Non-nil means, typing characters in org-goto starts incremental search."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defgroup org-sparse-trees nil
  525. "Options concerning sparse trees in Org-mode."
  526. :tag "Org Sparse Trees"
  527. :group 'org-structure)
  528. (defcustom org-highlight-sparse-tree-matches t
  529. "Non-nil means, highlight all matches that define a sparse tree.
  530. The highlights will automatically disappear the next time the buffer is
  531. changed by an edit command."
  532. :group 'org-sparse-trees
  533. :type 'boolean)
  534. (defcustom org-remove-highlights-with-change t
  535. "Non-nil means, any change to the buffer will remove temporary highlights.
  536. Such highlights are created by `org-occur' and `org-clock-display'.
  537. When nil, `C-c C-c needs to be used to get rid of the highlights.
  538. The highlights created by `org-preview-latex-fragment' always need
  539. `C-c C-c' to be removed."
  540. :group 'org-sparse-trees
  541. :group 'org-time
  542. :type 'boolean)
  543. (defcustom org-occur-hook '(org-first-headline-recenter)
  544. "Hook that is run after `org-occur' has constructed a sparse tree.
  545. This can be used to recenter the window to show as much of the structure
  546. as possible."
  547. :group 'org-sparse-trees
  548. :type 'hook)
  549. (defgroup org-plain-lists nil
  550. "Options concerning plain lists in Org-mode."
  551. :tag "Org Plain lists"
  552. :group 'org-structure)
  553. (defcustom org-cycle-include-plain-lists nil
  554. "Non-nil means, include plain lists into visibility cycling.
  555. This means that during cycling, plain list items will *temporarily* be
  556. interpreted as outline headlines with a level given by 1000+i where i is the
  557. indentation of the bullet. In all other operations, plain list items are
  558. not seen as headlines. For example, you cannot assign a TODO keyword to
  559. such an item."
  560. :group 'org-plain-lists
  561. :type 'boolean)
  562. (defcustom org-plain-list-ordered-item-terminator t
  563. "The character that makes a line with leading number an ordered list item.
  564. Valid values are ?. and ?\). To get both terminators, use t. While
  565. ?. may look nicer, it creates the danger that a line with leading
  566. number may be incorrectly interpreted as an item. ?\) therefore is
  567. the safe choice."
  568. :group 'org-plain-lists
  569. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  570. (const :tag "paren like in \"2)\"" ?\))
  571. (const :tab "both" t)))
  572. (defcustom org-auto-renumber-ordered-lists t
  573. "Non-nil means, automatically renumber ordered plain lists.
  574. Renumbering happens when the sequence have been changed with
  575. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  576. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defcustom org-provide-checkbox-statistics t
  580. "Non-nil means, update checkbox statistics after insert and toggle.
  581. When this is set, checkbox statistics is updated each time you either insert
  582. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  583. with \\[org-ctrl-c-ctrl-c\\]."
  584. :group 'org-plain-lists
  585. :type 'boolean)
  586. (defgroup org-archive nil
  587. "Options concerning archiving in Org-mode."
  588. :tag "Org Archive"
  589. :group 'org-structure)
  590. (defcustom org-archive-tag "ARCHIVE"
  591. "The tag that marks a subtree as archived.
  592. An archived subtree does not open during visibility cycling, and does
  593. not contribute to the agenda listings.
  594. After changing this, font-lock must be restarted in the relevant buffers to
  595. get the proper fontification."
  596. :group 'org-archive
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-agenda-skip-archived-trees t
  600. "Non-nil means, the agenda will skip any items located in archived trees.
  601. An archived tree is a tree marked with the tag ARCHIVE."
  602. :group 'org-archive
  603. :group 'org-agenda-skip
  604. :type 'boolean)
  605. (defcustom org-cycle-open-archived-trees nil
  606. "Non-nil means, `org-cycle' will open archived trees.
  607. An archived tree is a tree marked with the tag ARCHIVE.
  608. When nil, archived trees will stay folded. You can still open them with
  609. normal outline commands like `show-all', but not with the cycling commands."
  610. :group 'org-archive
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-sparse-tree-open-archived-trees nil
  614. "Non-nil means sparse tree construction shows matches in archived trees.
  615. When nil, matches in these trees are highlighted, but the trees are kept in
  616. collapsed state."
  617. :group 'org-archive
  618. :group 'org-sparse-trees
  619. :type 'boolean)
  620. (defcustom org-archive-location "%s_archive::"
  621. "The location where subtrees should be archived.
  622. This string consists of two parts, separated by a double-colon.
  623. The first part is a file name - when omitted, archiving happens in the same
  624. file. %s will be replaced by the current file name (without directory part).
  625. Archiving to a different file is useful to keep archived entries from
  626. contributing to the Org-mode Agenda.
  627. The part after the double colon is a headline. The archived entries will be
  628. filed under that headline. When omitted, the subtrees are simply filed away
  629. at the end of the file, as top-level entries.
  630. Here are a few examples:
  631. \"%s_archive::\"
  632. If the current file is Projects.org, archive in file
  633. Projects.org_archive, as top-level trees. This is the default.
  634. \"::* Archived Tasks\"
  635. Archive in the current file, under the top-level headline
  636. \"* Archived Tasks\".
  637. \"~/org/archive.org::\"
  638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  639. \"basement::** Finished Tasks\"
  640. Archive in file ./basement (relative path), as level 3 trees
  641. below the level 2 heading \"** Finished Tasks\".
  642. You may set this option on a per-file basis by adding to the buffer a
  643. line like
  644. #+ARCHIVE: basement::** Finished Tasks"
  645. :group 'org-archive
  646. :type 'string)
  647. (defcustom org-archive-mark-done t
  648. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  649. This can be a string to set the keyword to use. When t, Org-mode will
  650. use the first keyword in its list that means done."
  651. :group 'org-archive
  652. :type '(choice
  653. (const :tag "No" nil)
  654. (const :tag "Yes" t)
  655. (string :tag "Use this keyword")))
  656. (defcustom org-archive-stamp-time t
  657. "Non-nil means, add a time stamp to entries moved to an archive file.
  658. This variable is obsolete and has no effect anymore, instead add ot remove
  659. `time' from the variablle `org-archive-save-context-info'."
  660. :group 'org-archive
  661. :type 'boolean)
  662. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  663. "Parts of context info that should be stored as properties when archiving.
  664. When a subtree is moved to an archive file, it looses information given by
  665. context, like inherited tags, the category, and possibly also the TODO
  666. state (depending on the variable `org-archive-mark-done').
  667. This variable can be a list of any of the following symbols:
  668. time The time of archiving.
  669. file The file where the entry originates.
  670. itags The local tags, in the headline of the subtree.
  671. ltags The tags the subtree inherits from further up the hierarchy.
  672. todo The pre-archive TODO state.
  673. category The category, taken from file name or #+CATEGORY lines.
  674. olpath The outline path to the item. These are all headlines above
  675. the current item, separated by /, like a file path.
  676. For each symbol present in the list, a property will be created in
  677. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  678. information."
  679. :group 'org-archive
  680. :type '(set :greedy t
  681. (const :tag "Time" time)
  682. (const :tag "File" file)
  683. (const :tag "Category" category)
  684. (const :tag "TODO state" todo)
  685. (const :tag "TODO state" priority)
  686. (const :tag "Inherited tags" itags)
  687. (const :tag "Outline path" olpath)
  688. (const :tag "Local tags" ltags)))
  689. (defgroup org-imenu-and-speedbar nil
  690. "Options concerning imenu and speedbar in Org-mode."
  691. :tag "Org Imenu and Speedbar"
  692. :group 'org-structure)
  693. (defcustom org-imenu-depth 2
  694. "The maximum level for Imenu access to Org-mode headlines.
  695. This also applied for speedbar access."
  696. :group 'org-imenu-and-speedbar
  697. :type 'number)
  698. (defgroup org-table nil
  699. "Options concerning tables in Org-mode."
  700. :tag "Org Table"
  701. :group 'org)
  702. (defcustom org-enable-table-editor 'optimized
  703. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  704. When nil, such lines will be treated like ordinary lines.
  705. When equal to the symbol `optimized', the table editor will be optimized to
  706. do the following:
  707. - Automatic overwrite mode in front of whitespace in table fields.
  708. This makes the structure of the table stay in tact as long as the edited
  709. field does not exceed the column width.
  710. - Minimize the number of realigns. Normally, the table is aligned each time
  711. TAB or RET are pressed to move to another field. With optimization this
  712. happens only if changes to a field might have changed the column width.
  713. Optimization requires replacing the functions `self-insert-command',
  714. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  715. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  716. very good at guessing when a re-align will be necessary, but you can always
  717. force one with \\[org-ctrl-c-ctrl-c].
  718. If you would like to use the optimized version in Org-mode, but the
  719. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  720. This variable can be used to turn on and off the table editor during a session,
  721. but in order to toggle optimization, a restart is required.
  722. See also the variable `org-table-auto-blank-field'."
  723. :group 'org-table
  724. :type '(choice
  725. (const :tag "off" nil)
  726. (const :tag "on" t)
  727. (const :tag "on, optimized" optimized)))
  728. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  729. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  730. In the optimized version, the table editor takes over all simple keys that
  731. normally just insert a character. In tables, the characters are inserted
  732. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  733. for empty fields). Outside tables, the correct binding of the keys is
  734. restored.
  735. The default for this option is t if the optimized version is also used in
  736. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  737. this variable requires a restart of Emacs to become effective."
  738. :group 'org-table
  739. :type 'boolean)
  740. (defcustom orgtbl-radio-table-templates
  741. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  742. % END RECEIVE ORGTBL %n
  743. \\begin{comment}
  744. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  745. | | |
  746. \\end{comment}\n")
  747. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  748. @c END RECEIVE ORGTBL %n
  749. @ignore
  750. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  751. | | |
  752. @end ignore\n")
  753. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  754. <!-- END RECEIVE ORGTBL %n -->
  755. <!--
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. -->\n"))
  759. "Templates for radio tables in different major modes.
  760. All occurrences of %n in a template will be replaced with the name of the
  761. table, obtained by prompting the user."
  762. :group 'org-table
  763. :type '(repeat
  764. (list (symbol :tag "Major mode")
  765. (string :tag "Format"))))
  766. (defgroup org-table-settings nil
  767. "Settings for tables in Org-mode."
  768. :tag "Org Table Settings"
  769. :group 'org-table)
  770. (defcustom org-table-default-size "5x2"
  771. "The default size for newly created tables, Columns x Rows."
  772. :group 'org-table-settings
  773. :type 'string)
  774. (defcustom org-table-number-regexp
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  776. "Regular expression for recognizing numbers in table columns.
  777. If a table column contains mostly numbers, it will be aligned to the
  778. right. If not, it will be aligned to the left.
  779. The default value of this option is a regular expression which allows
  780. anything which looks remotely like a number as used in scientific
  781. context. For example, all of the following will be considered a
  782. number:
  783. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  784. Other options offered by the customize interface are more restrictive."
  785. :group 'org-table-settings
  786. :type '(choice
  787. (const :tag "Positive Integers"
  788. "^[0-9]+$")
  789. (const :tag "Integers"
  790. "^[-+]?[0-9]+$")
  791. (const :tag "Floating Point Numbers"
  792. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  793. (const :tag "Floating Point Number or Integer"
  794. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  795. (const :tag "Exponential, Floating point, Integer"
  796. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  797. (const :tag "Very General Number-Like, including hex"
  798. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  799. (string :tag "Regexp:")))
  800. (defcustom org-table-number-fraction 0.5
  801. "Fraction of numbers in a column required to make the column align right.
  802. In a column all non-white fields are considered. If at least this
  803. fraction of fields is matched by `org-table-number-fraction',
  804. alignment to the right border applies."
  805. :group 'org-table-settings
  806. :type 'number)
  807. (defgroup org-table-editing nil
  808. "Behavior of tables during editing in Org-mode."
  809. :tag "Org Table Editing"
  810. :group 'org-table)
  811. (defcustom org-table-automatic-realign t
  812. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  813. When nil, aligning is only done with \\[org-table-align], or after column
  814. removal/insertion."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-auto-blank-field t
  818. "Non-nil means, automatically blank table field when starting to type into it.
  819. This only happens when typing immediately after a field motion
  820. command (TAB, S-TAB or RET).
  821. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  822. :group 'org-table-editing
  823. :type 'boolean)
  824. (defcustom org-table-tab-jumps-over-hlines t
  825. "Non-nil means, tab in the last column of a table with jump over a hline.
  826. If a horizontal separator line is following the current line,
  827. `org-table-next-field' can either create a new row before that line, or jump
  828. over the line. When this option is nil, a new line will be created before
  829. this line."
  830. :group 'org-table-editing
  831. :type 'boolean)
  832. (defcustom org-table-tab-recognizes-table.el t
  833. "Non-nil means, TAB will automatically notice a table.el table.
  834. When it sees such a table, it moves point into it and - if necessary -
  835. calls `table-recognize-table'."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defgroup org-table-calculation nil
  839. "Options concerning tables in Org-mode."
  840. :tag "Org Table Calculation"
  841. :group 'org-table)
  842. (defcustom org-table-use-standard-references t
  843. "Should org-mode work with table refrences like B3 instead of @3$2?
  844. Possible values are:
  845. nil never use them
  846. from accept as input, do not present for editing
  847. t: accept as input and present for editing"
  848. :group 'org-table-calculation
  849. :type '(choice
  850. (const :tag "Never, don't even check unser input for them" nil)
  851. (const :tag "Always, both as user input, and when editing" t)
  852. (const :tag "Convert user input, don't offer during editing" 'from)))
  853. (defcustom org-table-copy-increment t
  854. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  855. :group 'org-table-calculation
  856. :type 'boolean)
  857. (defcustom org-calc-default-modes
  858. '(calc-internal-prec 12
  859. calc-float-format (float 5)
  860. calc-angle-mode deg
  861. calc-prefer-frac nil
  862. calc-symbolic-mode nil
  863. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  864. calc-display-working-message t
  865. )
  866. "List with Calc mode settings for use in calc-eval for table formulas.
  867. The list must contain alternating symbols (Calc modes variables and values).
  868. Don't remove any of the default settings, just change the values. Org-mode
  869. relies on the variables to be present in the list."
  870. :group 'org-table-calculation
  871. :type 'plist)
  872. (defcustom org-table-formula-evaluate-inline t
  873. "Non-nil means, TAB and RET evaluate a formula in current table field.
  874. If the current field starts with an equal sign, it is assumed to be a formula
  875. which should be evaluated as described in the manual and in the documentation
  876. string of the command `org-table-eval-formula'. This feature requires the
  877. Emacs calc package.
  878. When this variable is nil, formula calculation is only available through
  879. the command \\[org-table-eval-formula]."
  880. :group 'org-table-calculation
  881. :type 'boolean)
  882. (defcustom org-table-formula-use-constants t
  883. "Non-nil means, interpret constants in formulas in tables.
  884. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  885. by the value given in `org-table-formula-constants', or by a value obtained
  886. from the `constants.el' package."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defcustom org-table-formula-constants nil
  890. "Alist with constant names and values, for use in table formulas.
  891. The car of each element is a name of a constant, without the `$' before it.
  892. The cdr is the value as a string. For example, if you'd like to use the
  893. speed of light in a formula, you would configure
  894. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  895. and then use it in an equation like `$1*$c'.
  896. Constants can also be defined on a per-file basis using a line like
  897. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  898. :group 'org-table-calculation
  899. :type '(repeat
  900. (cons (string :tag "name")
  901. (string :tag "value"))))
  902. (defvar org-table-formula-constants-local nil
  903. "Local version of `org-table-formula-constants'.")
  904. (make-variable-buffer-local 'org-table-formula-constants-local)
  905. (defcustom org-table-allow-automatic-line-recalculation t
  906. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  907. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  908. :group 'org-table-calculation
  909. :type 'boolean)
  910. (defgroup org-link nil
  911. "Options concerning links in Org-mode."
  912. :tag "Org Link"
  913. :group 'org)
  914. (defvar org-link-abbrev-alist-local nil
  915. "Buffer-local version of `org-link-abbrev-alist', which see.
  916. The value of this is taken from the #+LINK lines.")
  917. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  918. (defcustom org-link-abbrev-alist nil
  919. "Alist of link abbreviations.
  920. The car of each element is a string, to be replaced at the start of a link.
  921. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  922. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  923. [[linkkey:tag][description]]
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there.
  926. REPLACE may also be a function that will be called with the tag as the
  927. only argument to create the link, which should be returned as a string.
  928. See the manual for examples."
  929. :group 'org-link
  930. :type 'alist)
  931. (defcustom org-descriptive-links t
  932. "Non-nil means, hide link part and only show description of bracket links.
  933. Bracket links are like [[link][descritpion]]. This variable sets the initial
  934. state in new org-mode buffers. The setting can then be toggled on a
  935. per-buffer basis from the Org->Hyperlinks menu."
  936. :group 'org-link
  937. :type 'boolean)
  938. (defcustom org-link-file-path-type 'adaptive
  939. "How the path name in file links should be stored.
  940. Valid values are:
  941. relative Relative to the current directory, i.e. the directory of the file
  942. into which the link is being inserted.
  943. absolute Absolute path, if possible with ~ for home directory.
  944. noabbrev Absolute path, no abbreviation of home directory.
  945. adaptive Use relative path for files in the current directory and sub-
  946. directories of it. For other files, use an absolute path."
  947. :group 'org-link
  948. :type '(choice
  949. (const relative)
  950. (const absolute)
  951. (const noabbrev)
  952. (const adaptive)))
  953. (defcustom org-activate-links '(bracket angle plain radio tag date)
  954. "Types of links that should be activated in Org-mode files.
  955. This is a list of symbols, each leading to the activation of a certain link
  956. type. In principle, it does not hurt to turn on most link types - there may
  957. be a small gain when turning off unused link types. The types are:
  958. bracket The recommended [[link][description]] or [[link]] links with hiding.
  959. angular Links in angular brackes that may contain whitespace like
  960. <bbdb:Carsten Dominik>.
  961. plain Plain links in normal text, no whitespace, like http://google.com.
  962. radio Text that is matched by a radio target, see manual for details.
  963. tag Tag settings in a headline (link to tag search).
  964. date Time stamps (link to calendar).
  965. Changing this variable requires a restart of Emacs to become effective."
  966. :group 'org-link
  967. :type '(set (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Tags" target)
  973. (const :tag "Timestamps" date)))
  974. (defgroup org-link-store nil
  975. "Options concerning storing links in Org-mode"
  976. :tag "Org Store Link"
  977. :group 'org-link)
  978. (defcustom org-email-link-description-format "Email %c: %.30s"
  979. "Format of the description part of a link to an email or usenet message.
  980. The following %-excapes will be replaced by corresponding information:
  981. %F full \"From\" field
  982. %f name, taken from \"From\" field, address if no name
  983. %T full \"To\" field
  984. %t first name in \"To\" field, address if no name
  985. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  986. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  987. %s subject
  988. %m message-id.
  989. You may use normal field width specification between the % and the letter.
  990. This is for example useful to limit the length of the subject.
  991. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  992. :group 'org-link-store
  993. :type 'string)
  994. (defcustom org-from-is-user-regexp
  995. (let (r1 r2)
  996. (when (and user-mail-address (not (string= user-mail-address "")))
  997. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  998. (when (and user-full-name (not (string= user-full-name "")))
  999. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1000. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1001. "Regexp mached against the \"From:\" header of an email or usenet message.
  1002. It should match if the message is from the user him/herself."
  1003. :group 'org-link-store
  1004. :type 'regexp)
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defcustom org-usenet-links-prefer-google nil
  1026. "Non-nil means, `org-store-link' will create web links to Google groups.
  1027. When nil, Gnus will be used for such links.
  1028. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1029. negates this setting for the duration of the command."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode"
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-tab-follows-link nil
  1037. "Non-nil means, on links TAB will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-return-follows-link nil
  1042. "Non-nil means, on links RET will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mouse-1-follows-link t
  1047. "Non-nil means, mouse-1 on a link will follow the link.
  1048. A longer mouse click will still set point. Does not wortk on XEmacs.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mark-ring-length 4
  1053. "Number of different positions to be recorded in the ring
  1054. Changing this requires a restart of Emacs to work correctly."
  1055. :group 'org-link-follow
  1056. :type 'interger)
  1057. (defcustom org-link-frame-setup
  1058. '((vm . vm-visit-folder-other-frame)
  1059. (gnus . gnus-other-frame)
  1060. (file . find-file-other-window))
  1061. "Setup the frame configuration for following links.
  1062. When following a link with Emacs, it may often be useful to display
  1063. this link in another window or frame. This variable can be used to
  1064. set this up for the different types of links.
  1065. For VM, use any of
  1066. `vm-visit-folder'
  1067. `vm-visit-folder-other-frame'
  1068. For Gnus, use any of
  1069. `gnus'
  1070. `gnus-other-frame'
  1071. For FILE, use any of
  1072. `find-file'
  1073. `find-file-other-window'
  1074. `find-file-other-frame'
  1075. For the calendar, use the variable `calendar-setup'.
  1076. For BBDB, it is currently only possible to display the matches in
  1077. another window."
  1078. :group 'org-link-follow
  1079. :type '(list
  1080. (cons (const vm)
  1081. (choice
  1082. (const vm-visit-folder)
  1083. (const vm-visit-folder-other-window)
  1084. (const vm-visit-folder-other-frame)))
  1085. (cons (const gnus)
  1086. (choice
  1087. (const gnus)
  1088. (const gnus-other-frame)))
  1089. (cons (const file)
  1090. (choice
  1091. (const find-file)
  1092. (const find-file-other-window)
  1093. (const find-file-other-frame)))))
  1094. (defcustom org-display-internal-link-with-indirect-buffer nil
  1095. "Non-nil means, use indirect buffer to display infile links.
  1096. Activating internal links (from one location in a file to another location
  1097. in the same file) normally just jumps to the location. When the link is
  1098. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1099. another window. When this option is set, the other window actually displays
  1100. an indirect buffer clone of the current buffer, to avoid any visibility
  1101. changes to the current buffer."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-open-non-existing-files nil
  1105. "Non-nil means, `org-open-file' will open non-existing files.
  1106. When nil, an error will be generated."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1110. "Function and arguments to call for following mailto links.
  1111. This is a list with the first element being a lisp function, and the
  1112. remaining elements being arguments to the function. In string arguments,
  1113. %a will be replaced by the address, and %s will be replaced by the subject
  1114. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1118. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1119. (const :tag "message-mail" (message-mail "%a" "%s"))
  1120. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1121. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1122. "Non-nil means, ask for confirmation before executing shell links.
  1123. Shell links can be dangerous: just think about a link
  1124. [[shell:rm -rf ~/*][Google Search]]
  1125. This link would show up in your Org-mode document as \"Google Search\",
  1126. but really it would remove your entire home directory.
  1127. Therefore we advise against setting this variable to nil.
  1128. Just change it to `y-or-n-p' of you want to confirm with a
  1129. single keystroke rather than having to type \"yes\"."
  1130. :group 'org-link-follow
  1131. :type '(choice
  1132. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1133. (const :tag "with y-or-n (faster)" y-or-n-p)
  1134. (const :tag "no confirmation (dangerous)" nil)))
  1135. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1137. Elisp links can be dangerous: just think about a link
  1138. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' of you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defconst org-file-apps-defaults-gnu
  1150. '((remote . emacs)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. ("ps" . "gv %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps" . "gv %s")
  1160. ("eps.gz" . "gv %s")
  1161. ("dvi" . "xdvi %s")
  1162. ("fig" . "xfig %s"))
  1163. "Default file applications on a MacOS X system.
  1164. The system \"open\" is known as a default, but we use X11 applications
  1165. for some files for which the OS does not have a good default.
  1166. See `org-file-apps'.")
  1167. (defconst org-file-apps-defaults-windowsnt
  1168. (list
  1169. '(remote . emacs)
  1170. (cons t
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. ("txt" . emacs)
  1181. ("tex" . emacs)
  1182. ("ltx" . emacs)
  1183. ("org" . emacs)
  1184. ("el" . emacs)
  1185. ("bib" . emacs)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"ext\" A string identifying an extension
  1194. `directory' Matches a directory
  1195. `remote' Matches a remote file, accessible through tramp or efs.
  1196. Remote files most likely should be visited through Emacs
  1197. because external applications cannot handle such paths.
  1198. t Default for all remaining files
  1199. Possible values for the command are:
  1200. `emacs' The file will be visited by the current Emacs process.
  1201. `default' Use the default application for this file type.
  1202. string A command to be executed by a shell; %s will be replaced
  1203. by the path to the file.
  1204. sexp A Lisp form which will be evaluated. The file path will
  1205. be available in the Lisp variable `file'.
  1206. For more examples, see the system specific constants
  1207. `org-file-apps-defaults-macosx'
  1208. `org-file-apps-defaults-windowsnt'
  1209. `org-file-apps-defaults-gnu'."
  1210. :group 'org-link-follow
  1211. :type '(repeat
  1212. (cons (choice :value ""
  1213. (string :tag "Extension")
  1214. (const :tag "Default for unrecognized files" t)
  1215. (const :tag "Remote file" remote)
  1216. (const :tag "Links to a directory" directory))
  1217. (choice :value ""
  1218. (const :tag "Visit with Emacs" emacs)
  1219. (const :tag "Use system default" default)
  1220. (string :tag "Command")
  1221. (sexp :tag "Lisp form")))))
  1222. (defcustom org-mhe-search-all-folders nil
  1223. "Non-nil means, that the search for the mh-message will be extended to
  1224. all folders if the message cannot be found in the folder given in the link.
  1225. Searching all folders is very efficient with one of the search engines
  1226. supported by MH-E, but will be slow with pick."
  1227. :group 'org-link-follow
  1228. :type 'boolean)
  1229. (defgroup org-remember nil
  1230. "Options concerning interaction with remember.el."
  1231. :tag "Org Remember"
  1232. :group 'org)
  1233. (defcustom org-directory "~/org"
  1234. "Directory with org files.
  1235. This directory will be used as default to prompt for org files.
  1236. Used by the hooks for remember.el."
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file "~/.notes"
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-remember
  1246. :type '(choice
  1247. (const :tag "Default from remember-data-file" nil)
  1248. file))
  1249. (defcustom org-remember-store-without-prompt t
  1250. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1251. In this case, you need `C-u C-c C-c' to get the prompts for
  1252. note file and headline.
  1253. When this variable is nil, `C-c C-c' give you the prompts, and
  1254. `C-u C-c C-c' trigger the fasttrack."
  1255. :group 'org-remember
  1256. :type 'boolean)
  1257. (defcustom org-remember-interactive-interface 'refile
  1258. "The interface to be used for interactive filing of remember notes.
  1259. This is only used when the interactive mode for selecting a filing
  1260. location is used (see the variable `org-remember-store-without-prompt').
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion.
  1267. refile Use the refile interface, and offer headlines,
  1268. possibly from different buffers."
  1269. :group 'org-remember
  1270. :type '(choice
  1271. (const :tag "Refile" refile)
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-interface 'outline
  1275. "The default interface to be used for `org-goto'.
  1276. Allowed vaues are:
  1277. outline The interface shows an outline of the relevant file
  1278. and the correct heading is found by moving through
  1279. the outline or by searching with incremental search.
  1280. outline-path-completion Headlines in the current buffer are offered via
  1281. completion."
  1282. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1283. :type '(choice
  1284. (const :tag "Outline" outline)
  1285. (const :tag "Outline-path-completion" outline-path-completion)))
  1286. (defcustom org-remember-default-headline ""
  1287. "The headline that should be the default location in the notes file.
  1288. When filing remember notes, the cursor will start at that position.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type 'string)
  1293. (defcustom org-remember-templates nil
  1294. "Templates for the creation of remember buffers.
  1295. When nil, just let remember make the buffer.
  1296. When not nil, this is a list of 5-element lists. In each entry, the first
  1297. element is the name of the template, which should be a single short word.
  1298. The second element is a character, a unique key to select this template.
  1299. The third element is the template. The fourth element is optional and can
  1300. specify a destination file for remember items created with this template.
  1301. The default file is given by `org-default-notes-file'. An optional fifth
  1302. element can specify the headline in that file that should be offered
  1303. first when the user is asked to file the entry. The default headline is
  1304. given in the variable `org-remember-default-headline'.
  1305. The template specifies the structure of the remember buffer. It should have
  1306. a first line starting with a star, to act as the org-mode headline.
  1307. Furthermore, the following %-escapes will be replaced with content:
  1308. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1309. A default value and a completion table ca be specified like this:
  1310. %^{prompt|default|completion2|completion3|...}
  1311. %t time stamp, date only
  1312. %T time stamp with date and time
  1313. %u, %U like the above, but inactive time stamps
  1314. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1315. You may define a prompt like %^{Please specify birthday}t
  1316. %n user name (taken from `user-full-name')
  1317. %a annotation, normally the link created with org-store-link
  1318. %i initial content, the region active. If %i is indented,
  1319. the entire inserted text will be indented as well.
  1320. %c content of the clipboard, or current kill ring head
  1321. %^g prompt for tags, with completion on tags in target file
  1322. %^G prompt for tags, with completion all tags in all agenda files
  1323. %:keyword specific information for certain link types, see below
  1324. %[pathname] insert the contents of the file given by `pathname'
  1325. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1326. %! Store this note immediately after filling the template
  1327. %? After completing the template, position cursor here.
  1328. Apart from these general escapes, you can access information specific to the
  1329. link type that is created. For example, calling `remember' in emails or gnus
  1330. will record the author and the subject of the message, which you can access
  1331. with %:author and %:subject, respectively. Here is a complete list of what
  1332. is recorded for each link type.
  1333. Link type | Available information
  1334. -------------------+------------------------------------------------------
  1335. bbdb | %:type %:name %:company
  1336. vm, wl, mh, rmail | %:type %:subject %:message-id
  1337. | %:from %:fromname %:fromaddress
  1338. | %:to %:toname %:toaddress
  1339. | %:fromto (either \"to NAME\" or \"from NAME\")
  1340. gnus | %:group, for messages also all email fields
  1341. w3, w3m | %:type %:url
  1342. info | %:type %:file %:node
  1343. calendar | %:type %:date"
  1344. :group 'org-remember
  1345. :get (lambda (var) ; Make sure all entries have 5 elements
  1346. (mapcar (lambda (x)
  1347. (if (not (stringp (car x))) (setq x (cons "" x)))
  1348. (cond ((= (length x) 4) (append x '("")))
  1349. ((= (length x) 3) (append x '("" "")))
  1350. (t x)))
  1351. (default-value var)))
  1352. :type '(repeat
  1353. :tag "enabled"
  1354. (list :value ("" ?a "\n" nil nil)
  1355. (string :tag "Name")
  1356. (character :tag "Selection Key")
  1357. (string :tag "Template")
  1358. (choice
  1359. (file :tag "Destination file")
  1360. (const :tag "Prompt for file" nil))
  1361. (choice
  1362. (string :tag "Destination headline")
  1363. (const :tag "Selection interface for heading")))))
  1364. (defcustom org-reverse-note-order nil
  1365. "Non-nil means, store new notes at the beginning of a file or entry.
  1366. When nil, new notes will be filed to the end of a file or entry.
  1367. This can also be a list with cons cells of regular expressions that
  1368. are matched against file names, and values."
  1369. :group 'org-remember
  1370. :type '(choice
  1371. (const :tag "Reverse always" t)
  1372. (const :tag "Reverse never" nil)
  1373. (repeat :tag "By file name regexp"
  1374. (cons regexp boolean))))
  1375. (defcustom org-refile-targets nil
  1376. "Targets for refiling entries with \\[org-refile].
  1377. This is list of cons cells. Each cell contains:
  1378. - a specification of the files to be considered, either a list of files,
  1379. or a symbol whose function or value fields will be used to retrieve
  1380. a file name or a list of file names. Nil means, refile to a different
  1381. heading in the current buffer.
  1382. - A specification of how to find candidate refile targets. This may be
  1383. any of
  1384. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1385. This tag has to be present in all target headlines, inheritance will
  1386. not be considered.
  1387. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1388. todo keyword.
  1389. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1390. headlines that are refiling targets.
  1391. - a cons cell (:level . N). Any headline of level N is considered a target.
  1392. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1393. ;; FIXME: what if there are a var and func with same name???
  1394. :group 'org-remember
  1395. :type '(repeat
  1396. (cons
  1397. (choice :value org-agenda-files
  1398. (const :tag "All agenda files" org-agenda-files)
  1399. (const :tag "Current buffer" nil)
  1400. (function) (variable) (file))
  1401. (choice :tag "Identify target headline by"
  1402. (cons :tag "Specific tag" (const :tag) (string))
  1403. (cons :tag "TODO keyword" (const :todo) (string))
  1404. (cons :tag "Regular expression" (const :regexp) (regexp))
  1405. (cons :tag "Level number" (const :level) (integer))
  1406. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1407. (defcustom org-refile-use-outline-path nil
  1408. "Non-nil means, provide refile targets as paths.
  1409. So a level 3 headline will be available as level1/level2/level3.
  1410. When the value is `file', also include the file name (without directory)
  1411. into the path. When `full-file-path', include the full file path."
  1412. :group 'org-remember
  1413. :type '(choice
  1414. (const :tag "Not" nil)
  1415. (const :tag "Yes" t)
  1416. (const :tag "Start with file name" file)
  1417. (const :tag "Start with full file path" full-file-path)))
  1418. (defgroup org-todo nil
  1419. "Options concerning TODO items in Org-mode."
  1420. :tag "Org TODO"
  1421. :group 'org)
  1422. (defgroup org-progress nil
  1423. "Options concerning Progress logging in Org-mode."
  1424. :tag "Org Progress"
  1425. :group 'org-time)
  1426. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1427. "List of TODO entry keyword sequences and their interpretation.
  1428. \\<org-mode-map>This is a list of sequences.
  1429. Each sequence starts with a symbol, either `sequence' or `type',
  1430. indicating if the keywords should be interpreted as a sequence of
  1431. action steps, or as different types of TODO items. The first
  1432. keywords are states requiring action - these states will select a headline
  1433. for inclusion into the global TODO list Org-mode produces. If one of
  1434. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1435. signify that no further action is necessary. If \"|\" is not found,
  1436. the last keyword is treated as the only DONE state of the sequence.
  1437. The command \\[org-todo] cycles an entry through these states, and one
  1438. additional state where no keyword is present. For details about this
  1439. cycling, see the manual.
  1440. TODO keywords and interpretation can also be set on a per-file basis with
  1441. the special #+SEQ_TODO and #+TYP_TODO lines.
  1442. Each keyword can optionally specify a character for fast state selection
  1443. \(in combination with the variable `org-use-fast-todo-selection')
  1444. and specifiers for state change logging, using the same syntax
  1445. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1446. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1447. indicates to record a time stamp each time this state is selected.
  1448. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1449. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1450. entering and when leaving this state.
  1451. For backward compatibility, this variable may also be just a list
  1452. of keywords - in this case the interptetation (sequence or type) will be
  1453. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1454. :group 'org-todo
  1455. :group 'org-keywords
  1456. :type '(choice
  1457. (repeat :tag "Old syntax, just keywords"
  1458. (string :tag "Keyword"))
  1459. (repeat :tag "New syntax"
  1460. (cons
  1461. (choice
  1462. :tag "Interpretation"
  1463. (const :tag "Sequence (cycling hits every state)" sequence)
  1464. (const :tag "Type (cycling directly to DONE)" type))
  1465. (repeat
  1466. (string :tag "Keyword"))))))
  1467. (defvar org-todo-keywords-1 nil
  1468. "All TODO and DONE keywords active in a buffer.")
  1469. (make-variable-buffer-local 'org-todo-keywords-1)
  1470. (defvar org-todo-keywords-for-agenda nil)
  1471. (defvar org-done-keywords-for-agenda nil)
  1472. (defvar org-not-done-keywords nil)
  1473. (make-variable-buffer-local 'org-not-done-keywords)
  1474. (defvar org-done-keywords nil)
  1475. (make-variable-buffer-local 'org-done-keywords)
  1476. (defvar org-todo-heads nil)
  1477. (make-variable-buffer-local 'org-todo-heads)
  1478. (defvar org-todo-sets nil)
  1479. (make-variable-buffer-local 'org-todo-sets)
  1480. (defvar org-todo-log-states nil)
  1481. (make-variable-buffer-local 'org-todo-log-states)
  1482. (defvar org-todo-kwd-alist nil)
  1483. (make-variable-buffer-local 'org-todo-kwd-alist)
  1484. (defvar org-todo-key-alist nil)
  1485. (make-variable-buffer-local 'org-todo-key-alist)
  1486. (defvar org-todo-key-trigger nil)
  1487. (make-variable-buffer-local 'org-todo-key-trigger)
  1488. (defcustom org-todo-interpretation 'sequence
  1489. "Controls how TODO keywords are interpreted.
  1490. This variable is in principle obsolete and is only used for
  1491. backward compatibility, if the interpretation of todo keywords is
  1492. not given already in `org-todo-keywords'. See that variable for
  1493. more information."
  1494. :group 'org-todo
  1495. :group 'org-keywords
  1496. :type '(choice (const sequence)
  1497. (const type)))
  1498. (defcustom org-use-fast-todo-selection 'prefix
  1499. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1500. This variable describes if and under what circumstances the cycling
  1501. mechanism for TODO keywords will be replaced by a single-key, direct
  1502. selection scheme.
  1503. When nil, fast selection is never used.
  1504. When the symbol `prefix', it will be used when `org-todo' is called with
  1505. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1506. in an agenda buffer.
  1507. When t, fast selection is used by default. In this case, the prefix
  1508. argument forces cycling instead.
  1509. In all cases, the special interface is only used if access keys have actually
  1510. been assigned by the user, i.e. if keywords in the configuration are followed
  1511. by a letter in parenthesis, like TODO(t)."
  1512. :group 'org-todo
  1513. :type '(choice
  1514. (const :tag "Never" nil)
  1515. (const :tag "By default" t)
  1516. (const :tag "Only with C-u C-c C-t" prefix)))
  1517. (defcustom org-after-todo-state-change-hook nil
  1518. "Hook which is run after the state of a TODO item was changed.
  1519. The new state (a string with a TODO keyword, or nil) is available in the
  1520. Lisp variable `state'."
  1521. :group 'org-todo
  1522. :type 'hook)
  1523. (defcustom org-log-done nil
  1524. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1525. When equal to the list (done), also prompt for a closing note.
  1526. This can also be configured on a per-file basis by adding one of
  1527. the following lines anywhere in the buffer:
  1528. #+STARTUP: logdone
  1529. #+STARTUP: lognotedone
  1530. #+STARTUP: nologdone"
  1531. :group 'org-todo
  1532. :group 'org-progress
  1533. :type '(choice
  1534. (const :tag "No logging" nil)
  1535. (const :tag "Record CLOSED timestamp" time)
  1536. (const :tag "Record CLOSED timestamp with closing note." note)))
  1537. ;; Normalize old uses of org-log-done.
  1538. (cond
  1539. ((eq org-log-done t) (setq org-log-done 'time))
  1540. ((and (listp org-log-done) (memq 'done org-log-done))
  1541. (setq org-log-done 'note)))
  1542. ;; FIXME: document
  1543. (defcustom org-log-note-clock-out nil
  1544. "Non-nil means, recored a note when clocking out of an item.
  1545. This can also be configured on a per-file basis by adding one of
  1546. the following lines anywhere in the buffer:
  1547. #+STARTUP: lognoteclock-out
  1548. #+STARTUP: nolognoteclock-out"
  1549. :group 'org-todo
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-done-with-time t
  1553. "Non-nil means, the CLOSED time stamp will contain date and time.
  1554. When nil, only the date will be recorded."
  1555. :group 'org-progress
  1556. :type 'boolean)
  1557. (defcustom org-log-note-headings
  1558. '((done . "CLOSING NOTE %t")
  1559. (state . "State %-12s %t")
  1560. (clock-out . ""))
  1561. "Headings for notes added when clocking out or closing TODO items.
  1562. The value is an alist, with the car being a symbol indicating the note
  1563. context, and the cdr is the heading to be used. The heading may also be the
  1564. empty string.
  1565. %t in the heading will be replaced by a time stamp.
  1566. %s will be replaced by the new TODO state, in double quotes.
  1567. %u will be replaced by the user name.
  1568. %U will be replaced by the full user name."
  1569. :group 'org-todo
  1570. :group 'org-progress
  1571. :type '(list :greedy t
  1572. (cons (const :tag "Heading when closing an item" done) string)
  1573. (cons (const :tag
  1574. "Heading when changing todo state (todo sequence only)"
  1575. state) string)
  1576. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1577. (defcustom org-log-states-order-reversed t
  1578. "Non-nil means, the latest state change note will be directly after heading.
  1579. When nil, the notes will be orderer according to time."
  1580. :group 'org-todo
  1581. :group 'org-progress
  1582. :type 'boolean)
  1583. (defcustom org-log-repeat 'time
  1584. "Non-nil means, record moving through the DONE state when triggering repeat.
  1585. An auto-repeating tasks is immediately switched back to TODO when marked
  1586. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1587. the TODO keyword definition, or recording a cloing note by setting
  1588. `org-log-done', there will be no record of the task moving trhough DONE.
  1589. This variable forces taking a note anyway. Possible values are:
  1590. nil Don't force a record
  1591. time Record a time stamp
  1592. note Record a note
  1593. This option can also be set with on a per-file-basis with
  1594. #+STARTUP: logrepeat
  1595. #+STARTUP: lognoterepeat
  1596. #+STARTUP: nologrepeat
  1597. You can have local logging settings for a subtree by setting the LOGGING
  1598. property to one or more of these keywords."
  1599. :group 'org-todo
  1600. :group 'org-progress
  1601. :type '(choice
  1602. (const :tag "Don't force a record" nil)
  1603. (const :tag "Force recording the DONE state" time)
  1604. (const :tag "Force recording a note with the DONE state" note)))
  1605. (defcustom org-clock-into-drawer 2
  1606. "Should clocking info be wrapped into a drawer?
  1607. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1608. If necessary, the drawer will be created.
  1609. When nil, the drawer will not be created, but used when present.
  1610. When an integer and the number of clocking entries in an item
  1611. reaches or exceeds this number, a drawer will be created."
  1612. :group 'org-todo
  1613. :group 'org-progress
  1614. :type '(choice
  1615. (const :tag "Always" t)
  1616. (const :tag "Only when drawer exists" nil)
  1617. (integer :tag "When at least N clock entries")))
  1618. (defcustom org-clock-out-when-done t
  1619. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1620. Nil means, clock will keep running until stopped explicitly with
  1621. `C-c C-x C-o', or until the clock is started in a different item."
  1622. :group 'org-progress
  1623. :type 'boolean)
  1624. (defcustom org-clock-in-switch-to-state nil
  1625. "Set task to a special todo state while clocking it.
  1626. The value should be the state to which the entry should be switched."
  1627. :group 'org-progress
  1628. :group 'org-todo
  1629. :type '(choice
  1630. (const :tag "Don't force a state" nil)
  1631. (string :tag "State")))
  1632. (defgroup org-priorities nil
  1633. "Priorities in Org-mode."
  1634. :tag "Org Priorities"
  1635. :group 'org-todo)
  1636. (defcustom org-highest-priority ?A
  1637. "The highest priority of TODO items. A character like ?A, ?B etc.
  1638. Must have a smaller ASCII number than `org-lowest-priority'."
  1639. :group 'org-priorities
  1640. :type 'character)
  1641. (defcustom org-lowest-priority ?C
  1642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1643. Must have a larger ASCII number than `org-highest-priority'."
  1644. :group 'org-priorities
  1645. :type 'character)
  1646. (defcustom org-default-priority ?B
  1647. "The default priority of TODO items.
  1648. This is the priority an item get if no explicit priority is given."
  1649. :group 'org-priorities
  1650. :type 'character)
  1651. (defcustom org-priority-start-cycle-with-default t
  1652. "Non-nil means, start with default priority when starting to cycle.
  1653. When this is nil, the first step in the cycle will be (depending on the
  1654. command used) one higher or lower that the default priority."
  1655. :group 'org-priorities
  1656. :type 'boolean)
  1657. (defgroup org-time nil
  1658. "Options concerning time stamps and deadlines in Org-mode."
  1659. :tag "Org Time"
  1660. :group 'org)
  1661. (defcustom org-insert-labeled-timestamps-at-point nil
  1662. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1663. When nil, these labeled time stamps are forces into the second line of an
  1664. entry, just after the headline. When scheduling from the global TODO list,
  1665. the time stamp will always be forced into the second line."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1669. "Formats for `format-time-string' which are used for time stamps.
  1670. It is not recommended to change this constant.")
  1671. (defcustom org-time-stamp-rounding-minutes 0
  1672. "Number of minutes to round time stamps to upon insertion.
  1673. When zero, insert the time unmodified. Useful rounding numbers
  1674. should be factors of 60, so for example 5, 10, 15.
  1675. When this is not zero, you can still force an exact time-stamp by using
  1676. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1677. :group 'org-time
  1678. :type 'integer)
  1679. (defcustom org-display-custom-times nil
  1680. "Non-nil means, overlay custom formats over all time stamps.
  1681. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1682. To turn this on on a per-file basis, insert anywhere in the file:
  1683. #+STARTUP: customtime"
  1684. :group 'org-time
  1685. :set 'set-default
  1686. :type 'sexp)
  1687. (make-variable-buffer-local 'org-display-custom-times)
  1688. (defcustom org-time-stamp-custom-formats
  1689. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1690. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1691. These are overlayed over the default ISO format if the variable
  1692. `org-display-custom-times' is set. Time like %H:%M should be at the
  1693. end of the second format."
  1694. :group 'org-time
  1695. :type 'sexp)
  1696. (defun org-time-stamp-format (&optional long inactive)
  1697. "Get the right format for a time string."
  1698. (let ((f (if long (cdr org-time-stamp-formats)
  1699. (car org-time-stamp-formats))))
  1700. (if inactive
  1701. (concat "[" (substring f 1 -1) "]")
  1702. f)))
  1703. (defcustom org-read-date-prefer-future t
  1704. "Non-nil means, assume future for incomplete date input from user.
  1705. This affects the following situations:
  1706. 1. The user gives a day, but no month.
  1707. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1708. read this as the third of *next* month. However, if you enter \"17\",
  1709. it will be considered as *this* month.
  1710. 2. The user gives a month but not a year.
  1711. For example, if it is april and you enter \"feb 2\", this will be read
  1712. as feb 2, *next* year. \"May 5\", however, will be this year.
  1713. When this option is nil, the current month and year will always be used
  1714. as defaults."
  1715. :group 'org-time
  1716. :type 'boolean)
  1717. (defcustom org-read-date-display-live t
  1718. "Non-nil means, display current interpretation of date prompt live.
  1719. This display will be in an overlay, in the minibuffer."
  1720. :group 'org-time
  1721. :type 'boolean)
  1722. (defcustom org-read-date-popup-calendar t
  1723. "Non-nil means, pop up a calendar when prompting for a date.
  1724. In the calendar, the date can be selected with mouse-1. However, the
  1725. minibuffer will also be active, and you can simply enter the date as well.
  1726. When nil, only the minibuffer will be available."
  1727. :group 'org-time
  1728. :type 'boolean)
  1729. (if (fboundp 'defvaralias)
  1730. (defvaralias 'org-popup-calendar-for-date-prompt
  1731. 'org-read-date-popup-calendar))
  1732. (defcustom org-extend-today-until 0
  1733. "The hour when your day really ends.
  1734. This has influence for the following applications:
  1735. - When switching the agenda to \"today\". It it is still earlier than
  1736. the time given here, the day recognized as TODAY is actually yesterday.
  1737. - When a date is read from the user and it is still before the time given
  1738. here, the current date and time will be assumed to be yesterday, 23:59.
  1739. FIXME:
  1740. IMPORTANT: This is still a very experimental feature, it may disappear
  1741. again or it may be extended to mean more things."
  1742. :group 'org-time
  1743. :type 'number)
  1744. (defcustom org-edit-timestamp-down-means-later nil
  1745. "Non-nil means, S-down will increase the time in a time stamp.
  1746. When nil, S-up will increase."
  1747. :group 'org-time
  1748. :type 'boolean)
  1749. (defcustom org-calendar-follow-timestamp-change t
  1750. "Non-nil means, make the calendar window follow timestamp changes.
  1751. When a timestamp is modified and the calendar window is visible, it will be
  1752. moved to the new date."
  1753. :group 'org-time
  1754. :type 'boolean)
  1755. (defcustom org-clock-heading-function nil
  1756. "When non-nil, should be a function to create `org-clock-heading'.
  1757. This is the string shown in the mode line when a clock is running.
  1758. The function is called with point at the beginning of the headline."
  1759. :group 'org-time ; FIXME: Should we have a separate group????
  1760. :type 'function)
  1761. (defgroup org-tags nil
  1762. "Options concerning tags in Org-mode."
  1763. :tag "Org Tags"
  1764. :group 'org)
  1765. (defcustom org-tag-alist nil
  1766. "List of tags allowed in Org-mode files.
  1767. When this list is nil, Org-mode will base TAG input on what is already in the
  1768. buffer.
  1769. The value of this variable is an alist, the car of each entry must be a
  1770. keyword as a string, the cdr may be a character that is used to select
  1771. that tag through the fast-tag-selection interface.
  1772. See the manual for details."
  1773. :group 'org-tags
  1774. :type '(repeat
  1775. (choice
  1776. (cons (string :tag "Tag name")
  1777. (character :tag "Access char"))
  1778. (const :tag "Start radio group" (:startgroup))
  1779. (const :tag "End radio group" (:endgroup)))))
  1780. (defcustom org-use-fast-tag-selection 'auto
  1781. "Non-nil means, use fast tag selection scheme.
  1782. This is a special interface to select and deselect tags with single keys.
  1783. When nil, fast selection is never used.
  1784. When the symbol `auto', fast selection is used if and only if selection
  1785. characters for tags have been configured, either through the variable
  1786. `org-tag-alist' or through a #+TAGS line in the buffer.
  1787. When t, fast selection is always used and selection keys are assigned
  1788. automatically if necessary."
  1789. :group 'org-tags
  1790. :type '(choice
  1791. (const :tag "Always" t)
  1792. (const :tag "Never" nil)
  1793. (const :tag "When selection characters are configured" 'auto)))
  1794. (defcustom org-fast-tag-selection-single-key nil
  1795. "Non-nil means, fast tag selection exits after first change.
  1796. When nil, you have to press RET to exit it.
  1797. During fast tag selection, you can toggle this flag with `C-c'.
  1798. This variable can also have the value `expert'. In this case, the window
  1799. displaying the tags menu is not even shown, until you press C-c again."
  1800. :group 'org-tags
  1801. :type '(choice
  1802. (const :tag "No" nil)
  1803. (const :tag "Yes" t)
  1804. (const :tag "Expert" expert)))
  1805. (defvar org-fast-tag-selection-include-todo nil
  1806. "Non-nil means, fast tags selection interface will also offer TODO states.
  1807. This is an undocumented feature, you should not rely on it.")
  1808. (defcustom org-tags-column -80
  1809. "The column to which tags should be indented in a headline.
  1810. If this number is positive, it specifies the column. If it is negative,
  1811. it means that the tags should be flushright to that column. For example,
  1812. -80 works well for a normal 80 character screen."
  1813. :group 'org-tags
  1814. :type 'integer)
  1815. (defcustom org-auto-align-tags t
  1816. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1817. These operations change the length of a headline and therefore shift
  1818. the tags around. With this options turned on, after each such operation
  1819. the tags are again aligned to `org-tags-column'."
  1820. :group 'org-tags
  1821. :type 'boolean)
  1822. (defcustom org-use-tag-inheritance t
  1823. "Non-nil means, tags in levels apply also for sublevels.
  1824. When nil, only the tags directly given in a specific line apply there.
  1825. If you turn off this option, you very likely want to turn on the
  1826. companion option `org-tags-match-list-sublevels'."
  1827. :group 'org-tags
  1828. :type 'boolean)
  1829. (defcustom org-tags-match-list-sublevels nil
  1830. "Non-nil means list also sublevels of headlines matching tag search.
  1831. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1832. the sublevels of a headline matching a tag search often also match
  1833. the same search. Listing all of them can create very long lists.
  1834. Setting this variable to nil causes subtrees of a match to be skipped.
  1835. This option is off by default, because inheritance in on. If you turn
  1836. inheritance off, you very likely want to turn this option on.
  1837. As a special case, if the tag search is restricted to TODO items, the
  1838. value of this variable is ignored and sublevels are always checked, to
  1839. make sure all corresponding TODO items find their way into the list."
  1840. :group 'org-tags
  1841. :type 'boolean)
  1842. (defvar org-tags-history nil
  1843. "History of minibuffer reads for tags.")
  1844. (defvar org-last-tags-completion-table nil
  1845. "The last used completion table for tags.")
  1846. (defvar org-after-tags-change-hook nil
  1847. "Hook that is run after the tags in a line have changed.")
  1848. (defgroup org-properties nil
  1849. "Options concerning properties in Org-mode."
  1850. :tag "Org Properties"
  1851. :group 'org)
  1852. (defcustom org-property-format "%-10s %s"
  1853. "How property key/value pairs should be formatted by `indent-line'.
  1854. When `indent-line' hits a property definition, it will format the line
  1855. according to this format, mainly to make sure that the values are
  1856. lined-up with respect to each other."
  1857. :group 'org-properties
  1858. :type 'string)
  1859. (defcustom org-use-property-inheritance nil
  1860. "Non-nil means, properties apply also for sublevels.
  1861. This setting is only relevant during property searches, not when querying
  1862. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1863. you need to call `org-entry-get' with the inheritance flag.
  1864. Turning this on can cause significant overhead when doing a search, so
  1865. this is turned off by default.
  1866. When nil, only the properties directly given in the current entry count.
  1867. The value may also be a list of properties that shouldhave inheritance.
  1868. However, note that some special properties use inheritance under special
  1869. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1870. and the properties ending in \"_ALL\" when they are used as descriptor
  1871. for valid values of a property."
  1872. :group 'org-properties
  1873. :type '(choice
  1874. (const :tag "Not" nil)
  1875. (const :tag "Always" nil)
  1876. (repeat :tag "Specific properties" (string :tag "Property"))))
  1877. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1878. "The default column format, if no other format has been defined.
  1879. This variable can be set on the per-file basis by inserting a line
  1880. #+COLUMNS: %25ITEM ....."
  1881. :group 'org-properties
  1882. :type 'string)
  1883. (defcustom org-global-properties nil
  1884. "List of property/value pairs that can be inherited by any entry.
  1885. You can set buffer-local values for this by adding lines like
  1886. #+PROPERTY: NAME VALUE"
  1887. :group 'org-properties
  1888. :type '(repeat
  1889. (cons (string :tag "Property")
  1890. (string :tag "Value"))))
  1891. (defvar org-local-properties nil
  1892. "List of property/value pairs that can be inherited by any entry.
  1893. Valid for the current buffer.
  1894. This variable is populated from #+PROPERTY lines.")
  1895. (defgroup org-agenda nil
  1896. "Options concerning agenda views in Org-mode."
  1897. :tag "Org Agenda"
  1898. :group 'org)
  1899. (defvar org-category nil
  1900. "Variable used by org files to set a category for agenda display.
  1901. Such files should use a file variable to set it, for example
  1902. # -*- mode: org; org-category: \"ELisp\"
  1903. or contain a special line
  1904. #+CATEGORY: ELisp
  1905. If the file does not specify a category, then file's base name
  1906. is used instead.")
  1907. (make-variable-buffer-local 'org-category)
  1908. (defcustom org-agenda-files nil
  1909. "The files to be used for agenda display.
  1910. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1911. \\[org-remove-file]. You can also use customize to edit the list.
  1912. If an entry is a directory, all files in that directory that are matched by
  1913. `org-agenda-file-regexp' will be part of the file list.
  1914. If the value of the variable is not a list but a single file name, then
  1915. the list of agenda files is actually stored and maintained in that file, one
  1916. agenda file per line."
  1917. :group 'org-agenda
  1918. :type '(choice
  1919. (repeat :tag "List of files and directories" file)
  1920. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1921. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1922. "Regular expression to match files for `org-agenda-files'.
  1923. If any element in the list in that variable contains a directory instead
  1924. of a normal file, all files in that directory that are matched by this
  1925. regular expression will be included."
  1926. :group 'org-agenda
  1927. :type 'regexp)
  1928. (defcustom org-agenda-skip-unavailable-files nil
  1929. "t means to just skip non-reachable files in `org-agenda-files'.
  1930. Nil means to remove them, after a query, from the list."
  1931. :group 'org-agenda
  1932. :type 'boolean)
  1933. (defcustom org-agenda-multi-occur-extra-files nil
  1934. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1935. The files in `org-agenda-files' are always searched."
  1936. :group 'org-agenda
  1937. :type '(repeat file))
  1938. (defcustom org-agenda-confirm-kill 1
  1939. "When set, remote killing from the agenda buffer needs confirmation.
  1940. When t, a confirmation is always needed. When a number N, confirmation is
  1941. only needed when the text to be killed contains more than N non-white lines."
  1942. :group 'org-agenda
  1943. :type '(choice
  1944. (const :tag "Never" nil)
  1945. (const :tag "Always" t)
  1946. (number :tag "When more than N lines")))
  1947. (defcustom org-calendar-to-agenda-key [?c]
  1948. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1949. The command `org-calendar-goto-agenda' will be bound to this key. The
  1950. default is the character `c' because then `c' can be used to switch back and
  1951. forth between agenda and calendar."
  1952. :group 'org-agenda
  1953. :type 'sexp)
  1954. (defcustom org-agenda-compact-blocks nil
  1955. "Non-nil means, make the block agenda more compact.
  1956. This is done by leaving out unnecessary lines."
  1957. :group 'org-agenda
  1958. :type nil)
  1959. (defgroup org-agenda-export nil
  1960. "Options concerning exporting agenda views in Org-mode."
  1961. :tag "Org Agenda Export"
  1962. :group 'org-agenda)
  1963. (defcustom org-agenda-with-colors t
  1964. "Non-nil means, use colors in agenda views."
  1965. :group 'org-agenda-export
  1966. :type 'boolean)
  1967. (defcustom org-agenda-exporter-settings nil
  1968. "Alist of variable/value pairs that should be active during agenda export.
  1969. This is a good place to set uptions for ps-print and for htmlize."
  1970. :group 'org-agenda-export
  1971. :type '(repeat
  1972. (list
  1973. (variable)
  1974. (sexp :tag "Value"))))
  1975. (defcustom org-agenda-export-html-style ""
  1976. "The style specification for exported HTML Agenda files.
  1977. If this variable contains a string, it will replace the default <style>
  1978. section as produced by `htmlize'.
  1979. Since there are different ways of setting style information, this variable
  1980. needs to contain the full HTML structure to provide a style, including the
  1981. surrounding HTML tags. The style specifications should include definitions
  1982. the fonts used by the agenda, here is an example:
  1983. <style type=\"text/css\">
  1984. p { font-weight: normal; color: gray; }
  1985. .org-agenda-structure {
  1986. font-size: 110%;
  1987. color: #003399;
  1988. font-weight: 600;
  1989. }
  1990. .org-todo {
  1991. color: #cc6666;Week-agenda:
  1992. font-weight: bold;
  1993. }
  1994. .org-done {
  1995. color: #339933;
  1996. }
  1997. .title { text-align: center; }
  1998. .todo, .deadline { color: red; }
  1999. .done { color: green; }
  2000. </style>
  2001. or, if you want to keep the style in a file,
  2002. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2003. As the value of this option simply gets inserted into the HTML <head> header,
  2004. you can \"misuse\" it to also add other text to the header. However,
  2005. <style>...</style> is required, if not present the variable will be ignored."
  2006. :group 'org-agenda-export
  2007. :group 'org-export-html
  2008. :type 'string)
  2009. (defgroup org-agenda-custom-commands nil
  2010. "Options concerning agenda views in Org-mode."
  2011. :tag "Org Agenda Custom Commands"
  2012. :group 'org-agenda)
  2013. (defcustom org-agenda-custom-commands nil
  2014. "Custom commands for the agenda.
  2015. These commands will be offered on the splash screen displayed by the
  2016. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2017. (key desc type match options files)
  2018. key The key (one or more characters as a string) to be associated
  2019. with the command.
  2020. desc A description of the commend, when omitted or nil, a default
  2021. description is built using MATCH.
  2022. type The command type, any of the following symbols:
  2023. todo Entries with a specific TODO keyword, in all agenda files.
  2024. tags Tags match in all agenda files.
  2025. tags-todo Tags match in all agenda files, TODO entries only.
  2026. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2027. tags-tree Sparse tree with all tags matches in *current* file.
  2028. occur-tree Occur sparse tree for *current* file.
  2029. ... A user-defined function.
  2030. match What to search for:
  2031. - a single keyword for TODO keyword searches
  2032. - a tags match expression for tags searches
  2033. - a regular expression for occur searches
  2034. options A list of option settings, similar to that in a let form, so like
  2035. this: ((opt1 val1) (opt2 val2) ...)
  2036. files A list of files file to write the produced agenda buffer to
  2037. with the command `org-store-agenda-views'.
  2038. If a file name ends in \".html\", an HTML version of the buffer
  2039. is written out. If it ends in \".ps\", a postscript version is
  2040. produced. Otherwide, only the plain text is written to the file.
  2041. You can also define a set of commands, to create a composite agenda buffer.
  2042. In this case, an entry looks like this:
  2043. (key desc (cmd1 cmd2 ...) general-options file)
  2044. where
  2045. desc A description string to be displayed in the dispatcher menu.
  2046. cmd An agenda command, similar to the above. However, tree commands
  2047. are no allowed, but instead you can get agenda and global todo list.
  2048. So valid commands for a set are:
  2049. (agenda)
  2050. (alltodo)
  2051. (stuck)
  2052. (todo \"match\" options files)
  2053. (tags \"match\" options files)
  2054. (tags-todo \"match\" options files)
  2055. Each command can carry a list of options, and another set of options can be
  2056. given for the whole set of commands. Individual command options take
  2057. precedence over the general options.
  2058. When using several characters as key to a command, the first characters
  2059. are prefix commands. For the dispatcher to display useful information, you
  2060. should provide a description for the prefix, like
  2061. (setq org-agenda-custom-commands
  2062. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2063. (\"hl\" tags \"+HOME+Lisa\")
  2064. (\"hp\" tags \"+HOME+Peter\")
  2065. (\"hk\" tags \"+HOME+Kim\")))"
  2066. :group 'org-agenda-custom-commands
  2067. :type '(repeat
  2068. (choice :value ("a" "" tags "" nil)
  2069. (list :tag "Single command"
  2070. (string :tag "Access Key(s) ")
  2071. (option (string :tag "Description"))
  2072. (choice
  2073. (const :tag "Agenda" agenda)
  2074. (const :tag "TODO list" alltodo)
  2075. (const :tag "Stuck projects" stuck)
  2076. (const :tag "Tags search (all agenda files)" tags)
  2077. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2078. (const :tag "TODO keyword search (all agenda files)" todo)
  2079. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2080. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2081. (const :tag "Occur tree (current buffer)" occur-tree)
  2082. (sexp :tag "Other, user-defined function"))
  2083. (string :tag "Match")
  2084. (repeat :tag "Local options"
  2085. (list (variable :tag "Option") (sexp :tag "Value")))
  2086. (option (repeat :tag "Export" (file :tag "Export to"))))
  2087. (list :tag "Command series, all agenda files"
  2088. (string :tag "Access Key(s)")
  2089. (string :tag "Description ")
  2090. (repeat
  2091. (choice
  2092. (const :tag "Agenda" (agenda))
  2093. (const :tag "TODO list" (alltodo))
  2094. (const :tag "Stuck projects" (stuck))
  2095. (list :tag "Tags search"
  2096. (const :format "" tags)
  2097. (string :tag "Match")
  2098. (repeat :tag "Local options"
  2099. (list (variable :tag "Option")
  2100. (sexp :tag "Value"))))
  2101. (list :tag "Tags search, TODO entries only"
  2102. (const :format "" tags-todo)
  2103. (string :tag "Match")
  2104. (repeat :tag "Local options"
  2105. (list (variable :tag "Option")
  2106. (sexp :tag "Value"))))
  2107. (list :tag "TODO keyword search"
  2108. (const :format "" todo)
  2109. (string :tag "Match")
  2110. (repeat :tag "Local options"
  2111. (list (variable :tag "Option")
  2112. (sexp :tag "Value"))))
  2113. (list :tag "Other, user-defined function"
  2114. (symbol :tag "function")
  2115. (string :tag "Match")
  2116. (repeat :tag "Local options"
  2117. (list (variable :tag "Option")
  2118. (sexp :tag "Value"))))))
  2119. (repeat :tag "General options"
  2120. (list (variable :tag "Option")
  2121. (sexp :tag "Value")))
  2122. (option (repeat :tag "Export" (file :tag "Export to"))))
  2123. (cons :tag "Prefix key documentation"
  2124. (string :tag "Access Key(s)")
  2125. (string :tag "Description ")))))
  2126. (defcustom org-stuck-projects
  2127. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2128. "How to identify stuck projects.
  2129. This is a list of four items:
  2130. 1. A tags/todo matcher string that is used to identify a project.
  2131. The entire tree below a headline matched by this is considered one project.
  2132. 2. A list of TODO keywords identifying non-stuck projects.
  2133. If the project subtree contains any headline with one of these todo
  2134. keywords, the project is considered to be not stuck. If you specify
  2135. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2136. 3. A list of tags identifying non-stuck projects.
  2137. If the project subtree contains any headline with one of these tags,
  2138. the project is considered to be not stuck. If you specify \"*\" as
  2139. a tag, any tag will mark the project unstuck.
  2140. 4. An arbitrary regular expression matching non-stuck projects.
  2141. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2142. or `C-c a #' to produce the list."
  2143. :group 'org-agenda-custom-commands
  2144. :type '(list
  2145. (string :tag "Tags/TODO match to identify a project")
  2146. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2147. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2148. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2149. (defgroup org-agenda-skip nil
  2150. "Options concerning skipping parts of agenda files."
  2151. :tag "Org Agenda Skip"
  2152. :group 'org-agenda)
  2153. (defcustom org-agenda-todo-list-sublevels t
  2154. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2155. When nil, the sublevels of a TODO entry are not checked, resulting in
  2156. potentially much shorter TODO lists."
  2157. :group 'org-agenda-skip
  2158. :group 'org-todo
  2159. :type 'boolean)
  2160. (defcustom org-agenda-todo-ignore-with-date nil
  2161. "Non-nil means, don't show entries with a date in the global todo list.
  2162. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2163. but don't want them to show up in the TODO list.
  2164. When this is set, it also covers deadlines and scheduled items, the settings
  2165. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2166. will be ignored."
  2167. :group 'org-agenda-skip
  2168. :group 'org-todo
  2169. :type 'boolean)
  2170. (defcustom org-agenda-todo-ignore-scheduled nil
  2171. "Non-nil means, don't show scheduled entries in the global todo list.
  2172. The idea behind this is that by scheduling it, you have already taken care
  2173. of this item.
  2174. See also `org-agenda-todo-ignore-with-date'."
  2175. :group 'org-agenda-skip
  2176. :group 'org-todo
  2177. :type 'boolean)
  2178. (defcustom org-agenda-todo-ignore-deadlines nil
  2179. "Non-nil means, don't show near deadline entries in the global todo list.
  2180. Near means closer than `org-deadline-warning-days' days.
  2181. The idea behind this is that such items will appear in the agenda anyway.
  2182. See also `org-agenda-todo-ignore-with-date'."
  2183. :group 'org-agenda-skip
  2184. :group 'org-todo
  2185. :type 'boolean)
  2186. (defcustom org-agenda-skip-scheduled-if-done nil
  2187. "Non-nil means don't show scheduled items in agenda when they are done.
  2188. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2189. it applies only to the actual date of the scheduling. Warnings about
  2190. an item with a past scheduling dates are always turned off when the item
  2191. is DONE."
  2192. :group 'org-agenda-skip
  2193. :type 'boolean)
  2194. (defcustom org-agenda-skip-deadline-if-done nil
  2195. "Non-nil means don't show deadines when the corresponding item is done.
  2196. When nil, the deadline is still shown and should give you a happy feeling.
  2197. This is relevant for the daily/weekly agenda. And it applied only to the
  2198. actualy date of the deadline. Warnings about approching and past-due
  2199. deadlines are always turned off when the item is DONE."
  2200. :group 'org-agenda-skip
  2201. :type 'boolean)
  2202. (defcustom org-agenda-skip-timestamp-if-done nil
  2203. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2204. :group 'org-agenda-skip
  2205. :type 'boolean)
  2206. (defcustom org-timeline-show-empty-dates 3
  2207. "Non-nil means, `org-timeline' also shows dates without an entry.
  2208. When nil, only the days which actually have entries are shown.
  2209. When t, all days between the first and the last date are shown.
  2210. When an integer, show also empty dates, but if there is a gap of more than
  2211. N days, just insert a special line indicating the size of the gap."
  2212. :group 'org-agenda-skip
  2213. :type '(choice
  2214. (const :tag "None" nil)
  2215. (const :tag "All" t)
  2216. (number :tag "at most")))
  2217. (defgroup org-agenda-startup nil
  2218. "Options concerning initial settings in the Agenda in Org Mode."
  2219. :tag "Org Agenda Startup"
  2220. :group 'org-agenda)
  2221. (defcustom org-finalize-agenda-hook nil
  2222. "Hook run just before displaying an agenda buffer."
  2223. :group 'org-agenda-startup
  2224. :type 'hook)
  2225. (defcustom org-agenda-mouse-1-follows-link nil
  2226. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2227. A longer mouse click will still set point. Does not wortk on XEmacs.
  2228. Needs to be set before org.el is loaded."
  2229. :group 'org-agenda-startup
  2230. :type 'boolean)
  2231. (defcustom org-agenda-start-with-follow-mode nil
  2232. "The initial value of follow-mode in a newly created agenda window."
  2233. :group 'org-agenda-startup
  2234. :type 'boolean)
  2235. (defgroup org-agenda-windows nil
  2236. "Options concerning the windows used by the Agenda in Org Mode."
  2237. :tag "Org Agenda Windows"
  2238. :group 'org-agenda)
  2239. (defcustom org-agenda-window-setup 'reorganize-frame
  2240. "How the agenda buffer should be displayed.
  2241. Possible values for this option are:
  2242. current-window Show agenda in the current window, keeping all other windows.
  2243. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2244. other-window Use `switch-to-buffer-other-window' to display agenda.
  2245. reorganize-frame Show only two windows on the current frame, the current
  2246. window and the agenda.
  2247. See also the variable `org-agenda-restore-windows-after-quit'."
  2248. :group 'org-agenda-windows
  2249. :type '(choice
  2250. (const current-window)
  2251. (const other-frame)
  2252. (const other-window)
  2253. (const reorganize-frame)))
  2254. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2255. "The min and max height of the agenda window as a fraction of frame height.
  2256. The value of the variable is a cons cell with two numbers between 0 and 1.
  2257. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2258. :group 'org-agenda-windows
  2259. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2260. (defcustom org-agenda-restore-windows-after-quit nil
  2261. "Non-nil means, restore window configuration open exiting agenda.
  2262. Before the window configuration is changed for displaying the agenda,
  2263. the current status is recorded. When the agenda is exited with
  2264. `q' or `x' and this option is set, the old state is restored. If
  2265. `org-agenda-window-setup' is `other-frame', the value of this
  2266. option will be ignored.."
  2267. :group 'org-agenda-windows
  2268. :type 'boolean)
  2269. (defcustom org-indirect-buffer-display 'other-window
  2270. "How should indirect tree buffers be displayed?
  2271. This applies to indirect buffers created with the commands
  2272. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2273. Valid values are:
  2274. current-window Display in the current window
  2275. other-window Just display in another window.
  2276. dedicated-frame Create one new frame, and re-use it each time.
  2277. new-frame Make a new frame each time. Note that in this case
  2278. previously-made indirect buffers are kept, and you need to
  2279. kill these buffers yourself."
  2280. :group 'org-structure
  2281. :group 'org-agenda-windows
  2282. :type '(choice
  2283. (const :tag "In current window" current-window)
  2284. (const :tag "In current frame, other window" other-window)
  2285. (const :tag "Each time a new frame" new-frame)
  2286. (const :tag "One dedicated frame" dedicated-frame)))
  2287. (defgroup org-agenda-daily/weekly nil
  2288. "Options concerning the daily/weekly agenda."
  2289. :tag "Org Agenda Daily/Weekly"
  2290. :group 'org-agenda)
  2291. (defcustom org-agenda-ndays 7
  2292. "Number of days to include in overview display.
  2293. Should be 1 or 7."
  2294. :group 'org-agenda-daily/weekly
  2295. :type 'number)
  2296. (defcustom org-agenda-start-on-weekday 1
  2297. "Non-nil means, start the overview always on the specified weekday.
  2298. 0 denotes Sunday, 1 denotes Monday etc.
  2299. When nil, always start on the current day."
  2300. :group 'org-agenda-daily/weekly
  2301. :type '(choice (const :tag "Today" nil)
  2302. (number :tag "Weekday No.")))
  2303. (defcustom org-agenda-show-all-dates t
  2304. "Non-nil means, `org-agenda' shows every day in the selected range.
  2305. When nil, only the days which actually have entries are shown."
  2306. :group 'org-agenda-daily/weekly
  2307. :type 'boolean)
  2308. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2309. "Format string for displaying dates in the agenda.
  2310. Used by the daily/weekly agenda and by the timeline. This should be
  2311. a format string understood by `format-time-string', or a function returning
  2312. the formatted date as a string. The function must take a single argument,
  2313. a calendar-style date list like (month day year)."
  2314. :group 'org-agenda-daily/weekly
  2315. :type '(choice
  2316. (string :tag "Format string")
  2317. (function :tag "Function")))
  2318. (defun org-agenda-format-date-aligned (date)
  2319. "Format a date string for display in the daily/weekly agenda, or timeline.
  2320. This function makes sure that dates are aligned for easy reading."
  2321. (format "%-9s %2d %s %4d"
  2322. (calendar-day-name date)
  2323. (extract-calendar-day date)
  2324. (calendar-month-name (extract-calendar-month date))
  2325. (extract-calendar-year date)))
  2326. (defcustom org-agenda-include-diary nil
  2327. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2328. :group 'org-agenda-daily/weekly
  2329. :type 'boolean)
  2330. (defcustom org-agenda-include-all-todo nil
  2331. "Set means weekly/daily agenda will always contain all TODO entries.
  2332. The TODO entries will be listed at the top of the agenda, before
  2333. the entries for specific days."
  2334. :group 'org-agenda-daily/weekly
  2335. :type 'boolean)
  2336. (defcustom org-agenda-repeating-timestamp-show-all t
  2337. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2338. When nil, only one occurence is shown, either today or the
  2339. nearest into the future."
  2340. :group 'org-agenda-daily/weekly
  2341. :type 'boolean)
  2342. (defcustom org-deadline-warning-days 14
  2343. "No. of days before expiration during which a deadline becomes active.
  2344. This variable governs the display in sparse trees and in the agenda.
  2345. When negative, it means use this number (the absolute value of it)
  2346. even if a deadline has a different individual lead time specified."
  2347. :group 'org-time
  2348. :group 'org-agenda-daily/weekly
  2349. :type 'number)
  2350. (defcustom org-scheduled-past-days 10000
  2351. "No. of days to continue listing scheduled items that are not marked DONE.
  2352. When an item is scheduled on a date, it shows up in the agenda on this
  2353. day and will be listed until it is marked done for the number of days
  2354. given here."
  2355. :group 'org-agenda-daily/weekly
  2356. :type 'number)
  2357. (defgroup org-agenda-time-grid nil
  2358. "Options concerning the time grid in the Org-mode Agenda."
  2359. :tag "Org Agenda Time Grid"
  2360. :group 'org-agenda)
  2361. (defcustom org-agenda-use-time-grid t
  2362. "Non-nil means, show a time grid in the agenda schedule.
  2363. A time grid is a set of lines for specific times (like every two hours between
  2364. 8:00 and 20:00). The items scheduled for a day at specific times are
  2365. sorted in between these lines.
  2366. For details about when the grid will be shown, and what it will look like, see
  2367. the variable `org-agenda-time-grid'."
  2368. :group 'org-agenda-time-grid
  2369. :type 'boolean)
  2370. (defcustom org-agenda-time-grid
  2371. '((daily today require-timed)
  2372. "----------------"
  2373. (800 1000 1200 1400 1600 1800 2000))
  2374. "The settings for time grid for agenda display.
  2375. This is a list of three items. The first item is again a list. It contains
  2376. symbols specifying conditions when the grid should be displayed:
  2377. daily if the agenda shows a single day
  2378. weekly if the agenda shows an entire week
  2379. today show grid on current date, independent of daily/weekly display
  2380. require-timed show grid only if at least one item has a time specification
  2381. The second item is a string which will be places behing the grid time.
  2382. The third item is a list of integers, indicating the times that should have
  2383. a grid line."
  2384. :group 'org-agenda-time-grid
  2385. :type
  2386. '(list
  2387. (set :greedy t :tag "Grid Display Options"
  2388. (const :tag "Show grid in single day agenda display" daily)
  2389. (const :tag "Show grid in weekly agenda display" weekly)
  2390. (const :tag "Always show grid for today" today)
  2391. (const :tag "Show grid only if any timed entries are present"
  2392. require-timed)
  2393. (const :tag "Skip grid times already present in an entry"
  2394. remove-match))
  2395. (string :tag "Grid String")
  2396. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2397. (defgroup org-agenda-sorting nil
  2398. "Options concerning sorting in the Org-mode Agenda."
  2399. :tag "Org Agenda Sorting"
  2400. :group 'org-agenda)
  2401. (defconst org-sorting-choice
  2402. '(choice
  2403. (const time-up) (const time-down)
  2404. (const category-keep) (const category-up) (const category-down)
  2405. (const tag-down) (const tag-up)
  2406. (const priority-up) (const priority-down))
  2407. "Sorting choices.")
  2408. (defcustom org-agenda-sorting-strategy
  2409. '((agenda time-up category-keep priority-down)
  2410. (todo category-keep priority-down)
  2411. (tags category-keep priority-down))
  2412. "Sorting structure for the agenda items of a single day.
  2413. This is a list of symbols which will be used in sequence to determine
  2414. if an entry should be listed before another entry. The following
  2415. symbols are recognized:
  2416. time-up Put entries with time-of-day indications first, early first
  2417. time-down Put entries with time-of-day indications first, late first
  2418. category-keep Keep the default order of categories, corresponding to the
  2419. sequence in `org-agenda-files'.
  2420. category-up Sort alphabetically by category, A-Z.
  2421. category-down Sort alphabetically by category, Z-A.
  2422. tag-up Sort alphabetically by last tag, A-Z.
  2423. tag-down Sort alphabetically by last tag, Z-A.
  2424. priority-up Sort numerically by priority, high priority last.
  2425. priority-down Sort numerically by priority, high priority first.
  2426. The different possibilities will be tried in sequence, and testing stops
  2427. if one comparison returns a \"not-equal\". For example, the default
  2428. '(time-up category-keep priority-down)
  2429. means: Pull out all entries having a specified time of day and sort them,
  2430. in order to make a time schedule for the current day the first thing in the
  2431. agenda listing for the day. Of the entries without a time indication, keep
  2432. the grouped in categories, don't sort the categories, but keep them in
  2433. the sequence given in `org-agenda-files'. Within each category sort by
  2434. priority.
  2435. Leaving out `category-keep' would mean that items will be sorted across
  2436. categories by priority.
  2437. Instead of a single list, this can also be a set of list for specific
  2438. contents, with a context symbol in the car of the list, any of
  2439. `agenda', `todo', `tags' for the corresponding agenda views."
  2440. :group 'org-agenda-sorting
  2441. :type `(choice
  2442. (repeat :tag "General" ,org-sorting-choice)
  2443. (list :tag "Individually"
  2444. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2445. (repeat ,org-sorting-choice))
  2446. (cons (const :tag "Strategy for TODO lists" todo)
  2447. (repeat ,org-sorting-choice))
  2448. (cons (const :tag "Strategy for Tags matches" tags)
  2449. (repeat ,org-sorting-choice)))))
  2450. (defcustom org-sort-agenda-notime-is-late t
  2451. "Non-nil means, items without time are considered late.
  2452. This is only relevant for sorting. When t, items which have no explicit
  2453. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2454. do have a time. When nil, the default time is before 0:00. You can use this
  2455. option to decide if the schedule for today should come before or after timeless
  2456. agenda entries."
  2457. :group 'org-agenda-sorting
  2458. :type 'boolean)
  2459. (defgroup org-agenda-line-format nil
  2460. "Options concerning the entry prefix in the Org-mode agenda display."
  2461. :tag "Org Agenda Line Format"
  2462. :group 'org-agenda)
  2463. (defcustom org-agenda-prefix-format
  2464. '((agenda . " %-12:c%?-12t% s")
  2465. (timeline . " % s")
  2466. (todo . " %-12:c")
  2467. (tags . " %-12:c"))
  2468. "Format specifications for the prefix of items in the agenda views.
  2469. An alist with four entries, for the different agenda types. The keys to the
  2470. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2471. are format strings.
  2472. This format works similar to a printf format, with the following meaning:
  2473. %c the category of the item, \"Diary\" for entries from the diary, or
  2474. as given by the CATEGORY keyword or derived from the file name.
  2475. %T the *last* tag of the item. Last because inherited tags come
  2476. first in the list.
  2477. %t the time-of-day specification if one applies to the entry, in the
  2478. format HH:MM
  2479. %s Scheduling/Deadline information, a short string
  2480. All specifiers work basically like the standard `%s' of printf, but may
  2481. contain two additional characters: A question mark just after the `%' and
  2482. a whitespace/punctuation character just before the final letter.
  2483. If the first character after `%' is a question mark, the entire field
  2484. will only be included if the corresponding value applies to the
  2485. current entry. This is useful for fields which should have fixed
  2486. width when present, but zero width when absent. For example,
  2487. \"%?-12t\" will result in a 12 character time field if a time of the
  2488. day is specified, but will completely disappear in entries which do
  2489. not contain a time.
  2490. If there is punctuation or whitespace character just before the final
  2491. format letter, this character will be appended to the field value if
  2492. the value is not empty. For example, the format \"%-12:c\" leads to
  2493. \"Diary: \" if the category is \"Diary\". If the category were be
  2494. empty, no additional colon would be interted.
  2495. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2496. - Indent the line with two space characters
  2497. - Give the category in a 12 chars wide field, padded with whitespace on
  2498. the right (because of `-'). Append a colon if there is a category
  2499. (because of `:').
  2500. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2501. time, don't put in an empty field, just skip it (because of '?').
  2502. - Finally, put the scheduling information and append a whitespace.
  2503. As another example, if you don't want the time-of-day of entries in
  2504. the prefix, you could use:
  2505. (setq org-agenda-prefix-format \" %-11:c% s\")
  2506. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2507. `org-agenda-remove-tags'."
  2508. :type '(choice
  2509. (string :tag "General format")
  2510. (list :greedy t :tag "View dependent"
  2511. (cons (const agenda) (string :tag "Format"))
  2512. (cons (const timeline) (string :tag "Format"))
  2513. (cons (const todo) (string :tag "Format"))
  2514. (cons (const tags) (string :tag "Format"))))
  2515. :group 'org-agenda-line-format)
  2516. (defvar org-prefix-format-compiled nil
  2517. "The compiled version of the most recently used prefix format.
  2518. See the variable `org-agenda-prefix-format'.")
  2519. (defcustom org-agenda-todo-keyword-format "%-1s"
  2520. "Format for the TODO keyword in agenda lines.
  2521. Set this to something like \"%-12s\" if you want all TODO keywords
  2522. to occupy a fixed space in the agenda display."
  2523. :group 'org-agenda-line-format
  2524. :type 'string)
  2525. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2526. "Text preceeding scheduled items in the agenda view.
  2527. This is a list with two strings. The first applies when the item is
  2528. scheduled on the current day. The second applies when it has been scheduled
  2529. previously, it may contain a %d to capture how many days ago the item was
  2530. scheduled."
  2531. :group 'org-agenda-line-format
  2532. :type '(list
  2533. (string :tag "Scheduled today ")
  2534. (string :tag "Scheduled previously")))
  2535. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2536. "Text preceeding deadline items in the agenda view.
  2537. This is a list with two strings. The first applies when the item has its
  2538. deadline on the current day. The second applies when it is in the past or
  2539. in the future, it may contain %d to capture how many days away the deadline
  2540. is (was)."
  2541. :group 'org-agenda-line-format
  2542. :type '(list
  2543. (string :tag "Deadline today ")
  2544. (string :tag "Deadline relative")))
  2545. (defcustom org-agenda-remove-times-when-in-prefix t
  2546. "Non-nil means, remove duplicate time specifications in agenda items.
  2547. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2548. time-of-day specification in a headline or diary entry is extracted and
  2549. placed into the prefix. If this option is non-nil, the original specification
  2550. \(a timestamp or -range, or just a plain time(range) specification like
  2551. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2552. cluttered.
  2553. The option can be t or nil. It may also be the symbol `beg', indicating
  2554. that the time should only be removed what it is located at the beginning of
  2555. the headline/diary entry."
  2556. :group 'org-agenda-line-format
  2557. :type '(choice
  2558. (const :tag "Always" t)
  2559. (const :tag "Never" nil)
  2560. (const :tag "When at beginning of entry" beg)))
  2561. (defcustom org-agenda-default-appointment-duration nil
  2562. "Default duration for appointments that only have a starting time.
  2563. When nil, no duration is specified in such cases.
  2564. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2565. :group 'org-agenda-line-format
  2566. :type '(choice
  2567. (integer :tag "Minutes")
  2568. (const :tag "No default duration")))
  2569. (defcustom org-agenda-remove-tags nil
  2570. "Non-nil means, remove the tags from the headline copy in the agenda.
  2571. When this is the symbol `prefix', only remove tags when
  2572. `org-agenda-prefix-format' contains a `%T' specifier."
  2573. :group 'org-agenda-line-format
  2574. :type '(choice
  2575. (const :tag "Always" t)
  2576. (const :tag "Never" nil)
  2577. (const :tag "When prefix format contains %T" prefix)))
  2578. (if (fboundp 'defvaralias)
  2579. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2580. 'org-agenda-remove-tags))
  2581. (defcustom org-agenda-tags-column -80
  2582. "Shift tags in agenda items to this column.
  2583. If this number is positive, it specifies the column. If it is negative,
  2584. it means that the tags should be flushright to that column. For example,
  2585. -80 works well for a normal 80 character screen."
  2586. :group 'org-agenda-line-format
  2587. :type 'integer)
  2588. (if (fboundp 'defvaralias)
  2589. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2590. (defcustom org-agenda-fontify-priorities t
  2591. "Non-nil means, highlight low and high priorities in agenda.
  2592. When t, the highest priority entries are bold, lowest priority italic.
  2593. This may also be an association list of priority faces. The face may be
  2594. a names face, or a list like `(:background \"Red\")'."
  2595. :group 'org-agenda-line-format
  2596. :type '(choice
  2597. (const :tag "Never" nil)
  2598. (const :tag "Defaults" t)
  2599. (repeat :tag "Specify"
  2600. (list (character :tag "Priority" :value ?A)
  2601. (sexp :tag "face")))))
  2602. (defgroup org-latex nil
  2603. "Options for embedding LaTeX code into Org-mode"
  2604. :tag "Org LaTeX"
  2605. :group 'org)
  2606. (defcustom org-format-latex-options
  2607. '(:foreground default :background default :scale 1.0
  2608. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2609. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2610. "Options for creating images from LaTeX fragments.
  2611. This is a property list with the following properties:
  2612. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2613. `default' means use the forground of the default face.
  2614. :background the background color, or \"Transparent\".
  2615. `default' means use the background of the default face.
  2616. :scale a scaling factor for the size of the images
  2617. :html-foreground, :html-background, :html-scale
  2618. The same numbers for HTML export.
  2619. :matchers a list indicating which matchers should be used to
  2620. find LaTeX fragments. Valid members of this list are:
  2621. \"begin\" find environments
  2622. \"$\" find math expressions surrounded by $...$
  2623. \"$$\" find math expressions surrounded by $$....$$
  2624. \"\\(\" find math expressions surrounded by \\(...\\)
  2625. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2626. :group 'org-latex
  2627. :type 'plist)
  2628. (defcustom org-format-latex-header "\\documentclass{article}
  2629. \\usepackage{fullpage} % do not remove
  2630. \\usepackage{amssymb}
  2631. \\usepackage[usenames]{color}
  2632. \\usepackage{amsmath}
  2633. \\usepackage{latexsym}
  2634. \\usepackage[mathscr]{eucal}
  2635. \\pagestyle{empty} % do not remove"
  2636. "The document header used for processing LaTeX fragments."
  2637. :group 'org-latex
  2638. :type 'string)
  2639. (defgroup org-export nil
  2640. "Options for exporting org-listings."
  2641. :tag "Org Export"
  2642. :group 'org)
  2643. (defgroup org-export-general nil
  2644. "General options for exporting Org-mode files."
  2645. :tag "Org Export General"
  2646. :group 'org-export)
  2647. ;; FIXME
  2648. (defvar org-export-publishing-directory nil)
  2649. (defcustom org-export-with-special-strings t
  2650. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2651. When this option is turned on, these strings will be exported as:
  2652. Org HTML LaTeX
  2653. -----+----------+--------
  2654. \\- &shy; \\-
  2655. -- &ndash; --
  2656. --- &mdash; ---
  2657. ... &hellip; \ldots
  2658. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2659. :group 'org-export-translation
  2660. :type 'boolean)
  2661. (defcustom org-export-language-setup
  2662. '(("en" "Author" "Date" "Table of Contents")
  2663. ("cs" "Autor" "Datum" "Obsah")
  2664. ("da" "Ophavsmand" "Dato" "Indhold")
  2665. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2666. ("es" "Autor" "Fecha" "\xcdndice")
  2667. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2668. ("it" "Autore" "Data" "Indice")
  2669. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2670. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2671. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2672. "Terms used in export text, translated to different languages.
  2673. Use the variable `org-export-default-language' to set the language,
  2674. or use the +OPTION lines for a per-file setting."
  2675. :group 'org-export-general
  2676. :type '(repeat
  2677. (list
  2678. (string :tag "HTML language tag")
  2679. (string :tag "Author")
  2680. (string :tag "Date")
  2681. (string :tag "Table of Contents"))))
  2682. (defcustom org-export-default-language "en"
  2683. "The default language of HTML export, as a string.
  2684. This should have an association in `org-export-language-setup'."
  2685. :group 'org-export-general
  2686. :type 'string)
  2687. (defcustom org-export-skip-text-before-1st-heading t
  2688. "Non-nil means, skip all text before the first headline when exporting.
  2689. When nil, that text is exported as well."
  2690. :group 'org-export-general
  2691. :type 'boolean)
  2692. (defcustom org-export-headline-levels 3
  2693. "The last level which is still exported as a headline.
  2694. Inferior levels will produce itemize lists when exported.
  2695. Note that a numeric prefix argument to an exporter function overrides
  2696. this setting.
  2697. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2698. :group 'org-export-general
  2699. :type 'number)
  2700. (defcustom org-export-with-section-numbers t
  2701. "Non-nil means, add section numbers to headlines when exporting.
  2702. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2703. :group 'org-export-general
  2704. :type 'boolean)
  2705. (defcustom org-export-with-toc t
  2706. "Non-nil means, create a table of contents in exported files.
  2707. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2708. When an integer, include levels up to N in the toc, this may then be
  2709. different from `org-export-headline-levels', but it will not be allowed
  2710. to be larger than the number of headline levels.
  2711. When nil, no table of contents is made.
  2712. Headlines which contain any TODO items will be marked with \"(*)\" in
  2713. ASCII export, and with red color in HTML output, if the option
  2714. `org-export-mark-todo-in-toc' is set.
  2715. In HTML output, the TOC will be clickable.
  2716. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2717. or \"toc:3\"."
  2718. :group 'org-export-general
  2719. :type '(choice
  2720. (const :tag "No Table of Contents" nil)
  2721. (const :tag "Full Table of Contents" t)
  2722. (integer :tag "TOC to level")))
  2723. (defcustom org-export-mark-todo-in-toc nil
  2724. "Non-nil means, mark TOC lines that contain any open TODO items."
  2725. :group 'org-export-general
  2726. :type 'boolean)
  2727. (defcustom org-export-preserve-breaks nil
  2728. "Non-nil means, preserve all line breaks when exporting.
  2729. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2730. export, line breaks will always be preserved, regardless of this variable.
  2731. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2732. :group 'org-export-general
  2733. :type 'boolean)
  2734. (defcustom org-export-with-archived-trees 'headline
  2735. "Whether subtrees with the ARCHIVE tag should be exported.
  2736. This can have three different values
  2737. nil Do not export, pretend this tree is not present
  2738. t Do export the entire tree
  2739. headline Only export the headline, but skip the tree below it."
  2740. :group 'org-export-general
  2741. :group 'org-archive
  2742. :type '(choice
  2743. (const :tag "not at all" nil)
  2744. (const :tag "headline only" 'headline)
  2745. (const :tag "entirely" t)))
  2746. (defcustom org-export-author-info t
  2747. "Non-nil means, insert author name and email into the exported file.
  2748. This option can also be set with the +OPTIONS line,
  2749. e.g. \"author-info:nil\"."
  2750. :group 'org-export-general
  2751. :type 'boolean)
  2752. (defcustom org-export-time-stamp-file t
  2753. "Non-nil means, insert a time stamp into the exported file.
  2754. The time stamp shows when the file was created.
  2755. This option can also be set with the +OPTIONS line,
  2756. e.g. \"timestamp:nil\"."
  2757. :group 'org-export-general
  2758. :type 'boolean)
  2759. (defcustom org-export-with-timestamps t
  2760. "If nil, do not export time stamps and associated keywords."
  2761. :group 'org-export-general
  2762. :type 'boolean)
  2763. (defcustom org-export-remove-timestamps-from-toc t
  2764. "If nil, remove timestamps from the table of contents entries."
  2765. :group 'org-export-general
  2766. :type 'boolean)
  2767. (defcustom org-export-with-tags 'not-in-toc
  2768. "If nil, do not export tags, just remove them from headlines.
  2769. If this is the symbol `not-in-toc', tags will be removed from table of
  2770. contents entries, but still be shown in the headlines of the document.
  2771. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2772. :group 'org-export-general
  2773. :type '(choice
  2774. (const :tag "Off" nil)
  2775. (const :tag "Not in TOC" not-in-toc)
  2776. (const :tag "On" t)))
  2777. (defcustom org-export-with-drawers nil
  2778. "Non-nil means, export with drawers like the property drawer.
  2779. When t, all drawers are exported. This may also be a list of
  2780. drawer names to export."
  2781. :group 'org-export-general
  2782. :type '(choice
  2783. (const :tag "All drawers" t)
  2784. (const :tag "None" nil)
  2785. (repeat :tag "Selected drawers"
  2786. (string :tag "Drawer name"))))
  2787. (defgroup org-export-translation nil
  2788. "Options for translating special ascii sequences for the export backends."
  2789. :tag "Org Export Translation"
  2790. :group 'org-export)
  2791. (defcustom org-export-with-emphasize t
  2792. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2793. If the export target supports emphasizing text, the word will be
  2794. typeset in bold, italic, or underlined, respectively. Works only for
  2795. single words, but you can say: I *really* *mean* *this*.
  2796. Not all export backends support this.
  2797. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2798. :group 'org-export-translation
  2799. :type 'boolean)
  2800. (defcustom org-export-with-footnotes t
  2801. "If nil, export [1] as a footnote marker.
  2802. Lines starting with [1] will be formatted as footnotes.
  2803. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2804. :group 'org-export-translation
  2805. :type 'boolean)
  2806. (defcustom org-export-with-sub-superscripts t
  2807. "Non-nil means, interpret \"_\" and \"^\" for export.
  2808. When this option is turned on, you can use TeX-like syntax for sub- and
  2809. superscripts. Several characters after \"_\" or \"^\" will be
  2810. considered as a single item - so grouping with {} is normally not
  2811. needed. For example, the following things will be parsed as single
  2812. sub- or superscripts.
  2813. 10^24 or 10^tau several digits will be considered 1 item.
  2814. 10^-12 or 10^-tau a leading sign with digits or a word
  2815. x^2-y^3 will be read as x^2 - y^3, because items are
  2816. terminated by almost any nonword/nondigit char.
  2817. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2818. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2819. sub/superscript. If you set this variable to the symbol `{}',
  2820. the braces are *required* in order to trigger interpretations as
  2821. sub/superscript. This can be helpful in documents that need \"_\"
  2822. frequently in plain text.
  2823. Not all export backends support this, but HTML does.
  2824. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2825. :group 'org-export-translation
  2826. :type '(choice
  2827. (const :tag "Always interpret" t)
  2828. (const :tag "Only with braces" {})
  2829. (const :tag "Never interpret" nil)))
  2830. (defcustom org-export-with-special-strings t
  2831. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2832. When this option is turned on, these strings will be exported as:
  2833. \\- : &shy;
  2834. -- : &ndash;
  2835. --- : &mdash;
  2836. Not all export backends support this, but HTML does.
  2837. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2838. :group 'org-export-translation
  2839. :type 'boolean)
  2840. (defcustom org-export-with-TeX-macros t
  2841. "Non-nil means, interpret simple TeX-like macros when exporting.
  2842. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2843. No only real TeX macros will work here, but the standard HTML entities
  2844. for math can be used as macro names as well. For a list of supported
  2845. names in HTML export, see the constant `org-html-entities'.
  2846. Not all export backends support this.
  2847. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2848. :group 'org-export-translation
  2849. :group 'org-export-latex
  2850. :type 'boolean)
  2851. (defcustom org-export-with-LaTeX-fragments nil
  2852. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2853. When set, the exporter will find LaTeX environments if the \\begin line is
  2854. the first non-white thing on a line. It will also find the math delimiters
  2855. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2856. display math.
  2857. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2858. :group 'org-export-translation
  2859. :group 'org-export-latex
  2860. :type 'boolean)
  2861. (defcustom org-export-with-fixed-width t
  2862. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2863. This can be used to have pre-formatted text, fragments of code etc. For
  2864. example:
  2865. : ;; Some Lisp examples
  2866. : (while (defc cnt)
  2867. : (ding))
  2868. will be looking just like this in also HTML. See also the QUOTE keyword.
  2869. Not all export backends support this.
  2870. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2871. :group 'org-export-translation
  2872. :type 'boolean)
  2873. (defcustom org-match-sexp-depth 3
  2874. "Number of stacked braces for sub/superscript matching.
  2875. This has to be set before loading org.el to be effective."
  2876. :group 'org-export-translation
  2877. :type 'integer)
  2878. (defgroup org-export-tables nil
  2879. "Options for exporting tables in Org-mode."
  2880. :tag "Org Export Tables"
  2881. :group 'org-export)
  2882. (defcustom org-export-with-tables t
  2883. "If non-nil, lines starting with \"|\" define a table.
  2884. For example:
  2885. | Name | Address | Birthday |
  2886. |-------------+----------+-----------|
  2887. | Arthur Dent | England | 29.2.2100 |
  2888. Not all export backends support this.
  2889. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2890. :group 'org-export-tables
  2891. :type 'boolean)
  2892. (defcustom org-export-highlight-first-table-line t
  2893. "Non-nil means, highlight the first table line.
  2894. In HTML export, this means use <th> instead of <td>.
  2895. In tables created with table.el, this applies to the first table line.
  2896. In Org-mode tables, all lines before the first horizontal separator
  2897. line will be formatted with <th> tags."
  2898. :group 'org-export-tables
  2899. :type 'boolean)
  2900. (defcustom org-export-table-remove-special-lines t
  2901. "Remove special lines and marking characters in calculating tables.
  2902. This removes the special marking character column from tables that are set
  2903. up for spreadsheet calculations. It also removes the entire lines
  2904. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2905. the values of constants may be useful to have."
  2906. :group 'org-export-tables
  2907. :type 'boolean)
  2908. (defcustom org-export-prefer-native-exporter-for-tables nil
  2909. "Non-nil means, always export tables created with table.el natively.
  2910. Natively means, use the HTML code generator in table.el.
  2911. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2912. the table does not use row- or column-spanning). This has the
  2913. advantage, that the automatic HTML conversions for math symbols and
  2914. sub/superscripts can be applied. Org-mode's HTML generator is also
  2915. much faster."
  2916. :group 'org-export-tables
  2917. :type 'boolean)
  2918. (defgroup org-export-ascii nil
  2919. "Options specific for ASCII export of Org-mode files."
  2920. :tag "Org Export ASCII"
  2921. :group 'org-export)
  2922. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2923. "Characters for underlining headings in ASCII export.
  2924. In the given sequence, these characters will be used for level 1, 2, ..."
  2925. :group 'org-export-ascii
  2926. :type '(repeat character))
  2927. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2928. "Bullet characters for headlines converted to lists in ASCII export.
  2929. The first character is used for the first lest level generated in this
  2930. way, and so on. If there are more levels than characters given here,
  2931. the list will be repeated.
  2932. Note that plain lists will keep the same bullets as the have in the
  2933. Org-mode file."
  2934. :group 'org-export-ascii
  2935. :type '(repeat character))
  2936. (defgroup org-export-xml nil
  2937. "Options specific for XML export of Org-mode files."
  2938. :tag "Org Export XML"
  2939. :group 'org-export)
  2940. (defgroup org-export-html nil
  2941. "Options specific for HTML export of Org-mode files."
  2942. :tag "Org Export HTML"
  2943. :group 'org-export)
  2944. (defcustom org-export-html-coding-system nil
  2945. ""
  2946. :group 'org-export-html
  2947. :type 'coding-system)
  2948. (defcustom org-export-html-extension "html"
  2949. "The extension for exported HTML files."
  2950. :group 'org-export-html
  2951. :type 'string)
  2952. (defcustom org-export-html-style
  2953. "<style type=\"text/css\">
  2954. html {
  2955. font-family: Times, serif;
  2956. font-size: 12pt;
  2957. }
  2958. .title { text-align: center; }
  2959. .todo { color: red; }
  2960. .done { color: green; }
  2961. .timestamp { color: grey }
  2962. .timestamp-kwd { color: CadetBlue }
  2963. .tag { background-color:lightblue; font-weight:normal }
  2964. .target { background-color: lavender; }
  2965. pre {
  2966. border: 1pt solid #AEBDCC;
  2967. background-color: #F3F5F7;
  2968. padding: 5pt;
  2969. font-family: courier, monospace;
  2970. }
  2971. table { border-collapse: collapse; }
  2972. td, th {
  2973. vertical-align: top;
  2974. <!--border: 1pt solid #ADB9CC;-->
  2975. }
  2976. </style>"
  2977. "The default style specification for exported HTML files.
  2978. Since there are different ways of setting style information, this variable
  2979. needs to contain the full HTML structure to provide a style, including the
  2980. surrounding HTML tags. The style specifications should include definitions
  2981. for new classes todo, done, title, and deadline. For example, legal values
  2982. would be:
  2983. <style type=\"text/css\">
  2984. p { font-weight: normal; color: gray; }
  2985. h1 { color: black; }
  2986. .title { text-align: center; }
  2987. .todo, .deadline { color: red; }
  2988. .done { color: green; }
  2989. </style>
  2990. or, if you want to keep the style in a file,
  2991. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2992. As the value of this option simply gets inserted into the HTML <head> header,
  2993. you can \"misuse\" it to add arbitrary text to the header."
  2994. :group 'org-export-html
  2995. :type 'string)
  2996. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2997. "Format for typesetting the document title in HTML export."
  2998. :group 'org-export-html
  2999. :type 'string)
  3000. (defcustom org-export-html-toplevel-hlevel 2
  3001. "The <H> level for level 1 headings in HTML export."
  3002. :group 'org-export-html
  3003. :type 'string)
  3004. (defcustom org-export-html-link-org-files-as-html t
  3005. "Non-nil means, make file links to `file.org' point to `file.html'.
  3006. When org-mode is exporting an org-mode file to HTML, links to
  3007. non-html files are directly put into a href tag in HTML.
  3008. However, links to other Org-mode files (recognized by the
  3009. extension `.org.) should become links to the corresponding html
  3010. file, assuming that the linked org-mode file will also be
  3011. converted to HTML.
  3012. When nil, the links still point to the plain `.org' file."
  3013. :group 'org-export-html
  3014. :type 'boolean)
  3015. (defcustom org-export-html-inline-images 'maybe
  3016. "Non-nil means, inline images into exported HTML pages.
  3017. This is done using an <img> tag. When nil, an anchor with href is used to
  3018. link to the image. If this option is `maybe', then images in links with
  3019. an empty description will be inlined, while images with a description will
  3020. be linked only."
  3021. :group 'org-export-html
  3022. :type '(choice (const :tag "Never" nil)
  3023. (const :tag "Always" t)
  3024. (const :tag "When there is no description" maybe)))
  3025. ;; FIXME: rename
  3026. (defcustom org-export-html-expand t
  3027. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3028. When nil, these tags will be exported as plain text and therefore
  3029. not be interpreted by a browser.
  3030. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3031. :group 'org-export-html
  3032. :type 'boolean)
  3033. (defcustom org-export-html-table-tag
  3034. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3035. "The HTML tag that is used to start a table.
  3036. This must be a <table> tag, but you may change the options like
  3037. borders and spacing."
  3038. :group 'org-export-html
  3039. :type 'string)
  3040. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3041. "The opening tag for table header fields.
  3042. This is customizable so that alignment options can be specified."
  3043. :group 'org-export-tables
  3044. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3045. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3046. "The opening tag for table data fields.
  3047. This is customizable so that alignment options can be specified."
  3048. :group 'org-export-tables
  3049. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3050. (defcustom org-export-html-with-timestamp nil
  3051. "If non-nil, write `org-export-html-html-helper-timestamp'
  3052. into the exported HTML text. Otherwise, the buffer will just be saved
  3053. to a file."
  3054. :group 'org-export-html
  3055. :type 'boolean)
  3056. (defcustom org-export-html-html-helper-timestamp
  3057. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3058. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3059. :group 'org-export-html
  3060. :type 'string)
  3061. (defgroup org-export-icalendar nil
  3062. "Options specific for iCalendar export of Org-mode files."
  3063. :tag "Org Export iCalendar"
  3064. :group 'org-export)
  3065. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3066. "The file name for the iCalendar file covering all agenda files.
  3067. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3068. The file name should be absolute, the file will be overwritten without warning."
  3069. :group 'org-export-icalendar
  3070. :type 'file)
  3071. (defcustom org-icalendar-include-todo nil
  3072. "Non-nil means, export to iCalendar files should also cover TODO items."
  3073. :group 'org-export-icalendar
  3074. :type '(choice
  3075. (const :tag "None" nil)
  3076. (const :tag "Unfinished" t)
  3077. (const :tag "All" all)))
  3078. (defcustom org-icalendar-include-sexps t
  3079. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3080. These are entries like in the diary, but directly in an Org-mode file."
  3081. :group 'org-export-icalendar
  3082. :type 'boolean)
  3083. (defcustom org-icalendar-include-body 100
  3084. "Amount of text below headline to be included in iCalendar export.
  3085. This is a number of characters that should maximally be included.
  3086. Properties, scheduling and clocking lines will always be removed.
  3087. The text will be inserted into the DESCRIPTION field."
  3088. :group 'org-export-icalendar
  3089. :type '(choice
  3090. (const :tag "Nothing" nil)
  3091. (const :tag "Everything" t)
  3092. (integer :tag "Max characters")))
  3093. (defcustom org-icalendar-combined-name "OrgMode"
  3094. "Calendar name for the combined iCalendar representing all agenda files."
  3095. :group 'org-export-icalendar
  3096. :type 'string)
  3097. (defgroup org-font-lock nil
  3098. "Font-lock settings for highlighting in Org-mode."
  3099. :tag "Org Font Lock"
  3100. :group 'org)
  3101. (defcustom org-level-color-stars-only nil
  3102. "Non-nil means fontify only the stars in each headline.
  3103. When nil, the entire headline is fontified.
  3104. Changing it requires restart of `font-lock-mode' to become effective
  3105. also in regions already fontified."
  3106. :group 'org-font-lock
  3107. :type 'boolean)
  3108. (defcustom org-hide-leading-stars nil
  3109. "Non-nil means, hide the first N-1 stars in a headline.
  3110. This works by using the face `org-hide' for these stars. This
  3111. face is white for a light background, and black for a dark
  3112. background. You may have to customize the face `org-hide' to
  3113. make this work.
  3114. Changing it requires restart of `font-lock-mode' to become effective
  3115. also in regions already fontified.
  3116. You may also set this on a per-file basis by adding one of the following
  3117. lines to the buffer:
  3118. #+STARTUP: hidestars
  3119. #+STARTUP: showstars"
  3120. :group 'org-font-lock
  3121. :type 'boolean)
  3122. (defcustom org-fontify-done-headline nil
  3123. "Non-nil means, change the face of a headline if it is marked DONE.
  3124. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3125. When this is non-nil, the headline after the keyword is set to the
  3126. `org-headline-done' as an additional indication."
  3127. :group 'org-font-lock
  3128. :type 'boolean)
  3129. (defcustom org-fontify-emphasized-text t
  3130. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3131. Changing this variable requires a restart of Emacs to take effect."
  3132. :group 'org-font-lock
  3133. :type 'boolean)
  3134. (defcustom org-highlight-latex-fragments-and-specials nil
  3135. "Non-nil means, fontify what is treated specially by the exporters."
  3136. :group 'org-font-lock
  3137. :type 'boolean)
  3138. (defcustom org-hide-emphasis-markers nil
  3139. "Non-nil mean font-lock should hide the emphasis marker characters."
  3140. :group 'org-font-lock
  3141. :type 'boolean)
  3142. (defvar org-emph-re nil
  3143. "Regular expression for matching emphasis.")
  3144. (defvar org-verbatim-re nil
  3145. "Regular expression for matching verbatim text.")
  3146. (defvar org-emphasis-regexp-components) ; defined just below
  3147. (defvar org-emphasis-alist) ; defined just below
  3148. (defun org-set-emph-re (var val)
  3149. "Set variable and compute the emphasis regular expression."
  3150. (set var val)
  3151. (when (and (boundp 'org-emphasis-alist)
  3152. (boundp 'org-emphasis-regexp-components)
  3153. org-emphasis-alist org-emphasis-regexp-components)
  3154. (let* ((e org-emphasis-regexp-components)
  3155. (pre (car e))
  3156. (post (nth 1 e))
  3157. (border (nth 2 e))
  3158. (body (nth 3 e))
  3159. (nl (nth 4 e))
  3160. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3161. (body1 (concat body "*?"))
  3162. (markers (mapconcat 'car org-emphasis-alist ""))
  3163. (vmarkers (mapconcat
  3164. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3165. org-emphasis-alist "")))
  3166. ;; make sure special characters appear at the right position in the class
  3167. (if (string-match "\\^" markers)
  3168. (setq markers (concat (replace-match "" t t markers) "^")))
  3169. (if (string-match "-" markers)
  3170. (setq markers (concat (replace-match "" t t markers) "-")))
  3171. (if (string-match "\\^" vmarkers)
  3172. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3173. (if (string-match "-" vmarkers)
  3174. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3175. (if (> nl 0)
  3176. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3177. (int-to-string nl) "\\}")))
  3178. ;; Make the regexp
  3179. (setq org-emph-re
  3180. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3181. "\\("
  3182. "\\([" markers "]\\)"
  3183. "\\("
  3184. "[^" border "]\\|"
  3185. "[^" border (if (and nil stacked) markers) "]"
  3186. body1
  3187. "[^" border (if (and nil stacked) markers) "]"
  3188. "\\)"
  3189. "\\3\\)"
  3190. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3191. (setq org-verbatim-re
  3192. (concat "\\([" pre "]\\|^\\)"
  3193. "\\("
  3194. "\\([" vmarkers "]\\)"
  3195. "\\("
  3196. "[^" border "]\\|"
  3197. "[^" border "]"
  3198. body1
  3199. "[^" border "]"
  3200. "\\)"
  3201. "\\3\\)"
  3202. "\\([" post "]\\|$\\)")))))
  3203. (defcustom org-emphasis-regexp-components
  3204. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3205. "Components used to build the regular expression for emphasis.
  3206. This is a list with 6 entries. Terminology: In an emphasis string
  3207. like \" *strong word* \", we call the initial space PREMATCH, the final
  3208. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3209. and \"trong wor\" is the body. The different components in this variable
  3210. specify what is allowed/forbidden in each part:
  3211. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3212. post Chars allowed as postmatch. End of line will be allowed too.
  3213. border The chars *forbidden* as border characters.
  3214. body-regexp A regexp like \".\" to match a body character. Don't use
  3215. non-shy groups here, and don't allow newline here.
  3216. newline The maximum number of newlines allowed in an emphasis exp.
  3217. Use customize to modify this, or restart Emacs after changing it."
  3218. :group 'org-font-lock
  3219. :set 'org-set-emph-re
  3220. :type '(list
  3221. (sexp :tag "Allowed chars in pre ")
  3222. (sexp :tag "Allowed chars in post ")
  3223. (sexp :tag "Forbidden chars in border ")
  3224. (sexp :tag "Regexp for body ")
  3225. (integer :tag "number of newlines allowed")
  3226. (option (boolean :tag "Stacking (DISABLED) "))))
  3227. (defcustom org-emphasis-alist
  3228. '(("*" bold "<b>" "</b>")
  3229. ("/" italic "<i>" "</i>")
  3230. ("_" underline "<u>" "</u>")
  3231. ("=" org-code "<code>" "</code>" verbatim)
  3232. ("~" org-verbatim "" "" verbatim)
  3233. ("+" (:strike-through t) "<del>" "</del>")
  3234. )
  3235. "Special syntax for emphasized text.
  3236. Text starting and ending with a special character will be emphasized, for
  3237. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3238. characters, the face to be used by font-lock for highlighting in Org-mode
  3239. Emacs buffers, and the HTML tags to be used for this.
  3240. Use customize to modify this, or restart Emacs after changing it."
  3241. :group 'org-font-lock
  3242. :set 'org-set-emph-re
  3243. :type '(repeat
  3244. (list
  3245. (string :tag "Marker character")
  3246. (choice
  3247. (face :tag "Font-lock-face")
  3248. (plist :tag "Face property list"))
  3249. (string :tag "HTML start tag")
  3250. (string :tag "HTML end tag")
  3251. (option (const verbatim)))))
  3252. ;;; The faces
  3253. (defgroup org-faces nil
  3254. "Faces in Org-mode."
  3255. :tag "Org Faces"
  3256. :group 'org-font-lock)
  3257. (defun org-compatible-face (inherits specs)
  3258. "Make a compatible face specification.
  3259. If INHERITS is an existing face and if the Emacs version supports it,
  3260. just inherit the face. If not, use SPECS to define the face.
  3261. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3262. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3263. to the top of the list. The `min-colors' attribute will be removed from
  3264. any other entries, and any resulting duplicates will be removed entirely."
  3265. (cond
  3266. ((and inherits (facep inherits)
  3267. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3268. ;; In Emacs 23, we use inheritance where possible.
  3269. ;; We only do this in Emacs 23, because only there the outline
  3270. ;; faces have been changed to the original org-mode-level-faces.
  3271. (list (list t :inherit inherits)))
  3272. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3273. ;; These do not understand the `min-colors' attribute.
  3274. (let (r e a)
  3275. (while (setq e (pop specs))
  3276. (cond
  3277. ((memq (car e) '(t default)) (push e r))
  3278. ((setq a (member '(min-colors 8) (car e)))
  3279. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3280. (cdr e)))))
  3281. ((setq a (assq 'min-colors (car e)))
  3282. (setq e (cons (delq a (car e)) (cdr e)))
  3283. (or (assoc (car e) r) (push e r)))
  3284. (t (or (assoc (car e) r) (push e r)))))
  3285. (nreverse r)))
  3286. (t specs)))
  3287. (put 'org-compatible-face 'lisp-indent-function 1)
  3288. (defface org-hide
  3289. '((((background light)) (:foreground "white"))
  3290. (((background dark)) (:foreground "black")))
  3291. "Face used to hide leading stars in headlines.
  3292. The forground color of this face should be equal to the background
  3293. color of the frame."
  3294. :group 'org-faces)
  3295. (defface org-level-1 ;; font-lock-function-name-face
  3296. (org-compatible-face 'outline-1
  3297. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3298. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3299. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3300. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3301. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3302. (t (:bold t))))
  3303. "Face used for level 1 headlines."
  3304. :group 'org-faces)
  3305. (defface org-level-2 ;; font-lock-variable-name-face
  3306. (org-compatible-face 'outline-2
  3307. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3308. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3309. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3310. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3311. (t (:bold t))))
  3312. "Face used for level 2 headlines."
  3313. :group 'org-faces)
  3314. (defface org-level-3 ;; font-lock-keyword-face
  3315. (org-compatible-face 'outline-3
  3316. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3317. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3318. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3319. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3320. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3321. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3322. (t (:bold t))))
  3323. "Face used for level 3 headlines."
  3324. :group 'org-faces)
  3325. (defface org-level-4 ;; font-lock-comment-face
  3326. (org-compatible-face 'outline-4
  3327. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3328. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3329. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3330. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3331. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3332. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3333. (t (:bold t))))
  3334. "Face used for level 4 headlines."
  3335. :group 'org-faces)
  3336. (defface org-level-5 ;; font-lock-type-face
  3337. (org-compatible-face 'outline-5
  3338. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3339. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3340. (((class color) (min-colors 8)) (:foreground "green"))))
  3341. "Face used for level 5 headlines."
  3342. :group 'org-faces)
  3343. (defface org-level-6 ;; font-lock-constant-face
  3344. (org-compatible-face 'outline-6
  3345. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3346. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3347. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3348. "Face used for level 6 headlines."
  3349. :group 'org-faces)
  3350. (defface org-level-7 ;; font-lock-builtin-face
  3351. (org-compatible-face 'outline-7
  3352. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3353. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3354. (((class color) (min-colors 8)) (:foreground "blue"))))
  3355. "Face used for level 7 headlines."
  3356. :group 'org-faces)
  3357. (defface org-level-8 ;; font-lock-string-face
  3358. (org-compatible-face 'outline-8
  3359. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3360. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3361. (((class color) (min-colors 8)) (:foreground "green"))))
  3362. "Face used for level 8 headlines."
  3363. :group 'org-faces)
  3364. (defface org-special-keyword ;; font-lock-string-face
  3365. (org-compatible-face nil
  3366. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3367. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3368. (t (:italic t))))
  3369. "Face used for special keywords."
  3370. :group 'org-faces)
  3371. (defface org-drawer ;; font-lock-function-name-face
  3372. (org-compatible-face nil
  3373. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3374. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3375. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3376. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3377. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3378. (t (:bold t))))
  3379. "Face used for drawers."
  3380. :group 'org-faces)
  3381. (defface org-property-value nil
  3382. "Face used for the value of a property."
  3383. :group 'org-faces)
  3384. (defface org-column
  3385. (org-compatible-face nil
  3386. '((((class color) (min-colors 16) (background light))
  3387. (:background "grey90"))
  3388. (((class color) (min-colors 16) (background dark))
  3389. (:background "grey30"))
  3390. (((class color) (min-colors 8))
  3391. (:background "cyan" :foreground "black"))
  3392. (t (:inverse-video t))))
  3393. "Face for column display of entry properties."
  3394. :group 'org-faces)
  3395. (when (fboundp 'set-face-attribute)
  3396. ;; Make sure that a fixed-width face is used when we have a column table.
  3397. (set-face-attribute 'org-column nil
  3398. :height (face-attribute 'default :height)
  3399. :family (face-attribute 'default :family)))
  3400. (defface org-warning
  3401. (org-compatible-face 'font-lock-warning-face
  3402. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3403. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3404. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3405. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3406. (t (:bold t))))
  3407. "Face for deadlines and TODO keywords."
  3408. :group 'org-faces)
  3409. (defface org-archived ; similar to shadow
  3410. (org-compatible-face 'shadow
  3411. '((((class color grayscale) (min-colors 88) (background light))
  3412. (:foreground "grey50"))
  3413. (((class color grayscale) (min-colors 88) (background dark))
  3414. (:foreground "grey70"))
  3415. (((class color) (min-colors 8) (background light))
  3416. (:foreground "green"))
  3417. (((class color) (min-colors 8) (background dark))
  3418. (:foreground "yellow"))))
  3419. "Face for headline with the ARCHIVE tag."
  3420. :group 'org-faces)
  3421. (defface org-link
  3422. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3423. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3424. (t (:underline t)))
  3425. "Face for links."
  3426. :group 'org-faces)
  3427. (defface org-ellipsis
  3428. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3429. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3430. (t (:strike-through t)))
  3431. "Face for the ellipsis in folded text."
  3432. :group 'org-faces)
  3433. (defface org-target
  3434. '((((class color) (background light)) (:underline t))
  3435. (((class color) (background dark)) (:underline t))
  3436. (t (:underline t)))
  3437. "Face for links."
  3438. :group 'org-faces)
  3439. (defface org-date
  3440. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3441. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3442. (t (:underline t)))
  3443. "Face for links."
  3444. :group 'org-faces)
  3445. (defface org-sexp-date
  3446. '((((class color) (background light)) (:foreground "Purple"))
  3447. (((class color) (background dark)) (:foreground "Cyan"))
  3448. (t (:underline t)))
  3449. "Face for links."
  3450. :group 'org-faces)
  3451. (defface org-tag
  3452. '((t (:bold t)))
  3453. "Face for tags."
  3454. :group 'org-faces)
  3455. (defface org-todo ; font-lock-warning-face
  3456. (org-compatible-face nil
  3457. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3458. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3459. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3460. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3461. (t (:inverse-video t :bold t))))
  3462. "Face for TODO keywords."
  3463. :group 'org-faces)
  3464. (defface org-done ;; font-lock-type-face
  3465. (org-compatible-face nil
  3466. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3467. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3468. (((class color) (min-colors 8)) (:foreground "green"))
  3469. (t (:bold t))))
  3470. "Face used for todo keywords that indicate DONE items."
  3471. :group 'org-faces)
  3472. (defface org-headline-done ;; font-lock-string-face
  3473. (org-compatible-face nil
  3474. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3475. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3476. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3477. "Face used to indicate that a headline is DONE.
  3478. This face is only used if `org-fontify-done-headline' is set. If applies
  3479. to the part of the headline after the DONE keyword."
  3480. :group 'org-faces)
  3481. (defcustom org-todo-keyword-faces nil
  3482. "Faces for specific TODO keywords.
  3483. This is a list of cons cells, with TODO keywords in the car
  3484. and faces in the cdr. The face can be a symbol, or a property
  3485. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3486. :group 'org-faces
  3487. :group 'org-todo
  3488. :type '(repeat
  3489. (cons
  3490. (string :tag "keyword")
  3491. (sexp :tag "face"))))
  3492. (defface org-table ;; font-lock-function-name-face
  3493. (org-compatible-face nil
  3494. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3495. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3496. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3497. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3498. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3499. (((class color) (min-colors 8) (background dark)))))
  3500. "Face used for tables."
  3501. :group 'org-faces)
  3502. (defface org-formula
  3503. (org-compatible-face nil
  3504. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3505. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3506. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3507. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3508. (t (:bold t :italic t))))
  3509. "Face for formulas."
  3510. :group 'org-faces)
  3511. (defface org-code
  3512. (org-compatible-face nil
  3513. '((((class color grayscale) (min-colors 88) (background light))
  3514. (:foreground "grey50"))
  3515. (((class color grayscale) (min-colors 88) (background dark))
  3516. (:foreground "grey70"))
  3517. (((class color) (min-colors 8) (background light))
  3518. (:foreground "green"))
  3519. (((class color) (min-colors 8) (background dark))
  3520. (:foreground "yellow"))))
  3521. "Face for fixed-with text like code snippets."
  3522. :group 'org-faces
  3523. :version "22.1")
  3524. (defface org-verbatim
  3525. (org-compatible-face nil
  3526. '((((class color grayscale) (min-colors 88) (background light))
  3527. (:foreground "grey50" :underline t))
  3528. (((class color grayscale) (min-colors 88) (background dark))
  3529. (:foreground "grey70" :underline t))
  3530. (((class color) (min-colors 8) (background light))
  3531. (:foreground "green" :underline t))
  3532. (((class color) (min-colors 8) (background dark))
  3533. (:foreground "yellow" :underline t))))
  3534. "Face for fixed-with text like code snippets."
  3535. :group 'org-faces
  3536. :version "22.1")
  3537. (defface org-agenda-structure ;; font-lock-function-name-face
  3538. (org-compatible-face nil
  3539. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3540. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3541. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3542. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3543. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3544. (t (:bold t))))
  3545. "Face used in agenda for captions and dates."
  3546. :group 'org-faces)
  3547. (defface org-scheduled-today
  3548. (org-compatible-face nil
  3549. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3550. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3551. (((class color) (min-colors 8)) (:foreground "green"))
  3552. (t (:bold t :italic t))))
  3553. "Face for items scheduled for a certain day."
  3554. :group 'org-faces)
  3555. (defface org-scheduled-previously
  3556. (org-compatible-face nil
  3557. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3558. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3559. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3560. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3561. (t (:bold t))))
  3562. "Face for items scheduled previously, and not yet done."
  3563. :group 'org-faces)
  3564. (defface org-upcoming-deadline
  3565. (org-compatible-face nil
  3566. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3567. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3568. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3569. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3570. (t (:bold t))))
  3571. "Face for items scheduled previously, and not yet done."
  3572. :group 'org-faces)
  3573. (defcustom org-agenda-deadline-faces
  3574. '((1.0 . org-warning)
  3575. (0.5 . org-upcoming-deadline)
  3576. (0.0 . default))
  3577. "Faces for showing deadlines in the agenda.
  3578. This is a list of cons cells. The cdr of each cell is a face to be used,
  3579. and it can also just be like '(:foreground \"yellow\").
  3580. Each car is a fraction of the head-warning time that must have passed for
  3581. this the face in the cdr to be used for display. The numbers must be
  3582. given in descending order. The head-warning time is normally taken
  3583. from `org-deadline-warning-days', but can also be specified in the deadline
  3584. timestamp itself, like this:
  3585. DEADLINE: <2007-08-13 Mon -8d>
  3586. You may use d for days, w for weeks, m for months and y for years. Months
  3587. and years will only be treated in an approximate fashion (30.4 days for a
  3588. month and 365.24 days for a year)."
  3589. :group 'org-faces
  3590. :group 'org-agenda-daily/weekly
  3591. :type '(repeat
  3592. (cons
  3593. (number :tag "Fraction of head-warning time passed")
  3594. (sexp :tag "Face"))))
  3595. ;; FIXME: this is not a good face yet.
  3596. (defface org-agenda-restriction-lock
  3597. (org-compatible-face nil
  3598. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3599. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3600. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3601. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3602. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3603. (t (:inverse-video t))))
  3604. "Face for showing the agenda restriction lock."
  3605. :group 'org-faces)
  3606. (defface org-time-grid ;; font-lock-variable-name-face
  3607. (org-compatible-face nil
  3608. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3609. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3610. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3611. "Face used for time grids."
  3612. :group 'org-faces)
  3613. (defconst org-level-faces
  3614. '(org-level-1 org-level-2 org-level-3 org-level-4
  3615. org-level-5 org-level-6 org-level-7 org-level-8
  3616. ))
  3617. (defcustom org-n-level-faces (length org-level-faces)
  3618. "The number different faces to be used for headlines.
  3619. Org-mode defines 8 different headline faces, so this can be at most 8.
  3620. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3621. :type 'number
  3622. :group 'org-faces)
  3623. ;;; Functions and variables from ther packages
  3624. ;; Declared here to avoid compiler warnings
  3625. (eval-and-compile
  3626. (unless (fboundp 'declare-function)
  3627. (defmacro declare-function (fn file &optional arglist fileonly))))
  3628. ;; XEmacs only
  3629. (defvar outline-mode-menu-heading)
  3630. (defvar outline-mode-menu-show)
  3631. (defvar outline-mode-menu-hide)
  3632. (defvar zmacs-regions) ; XEmacs regions
  3633. ;; Emacs only
  3634. (defvar mark-active)
  3635. ;; Various packages
  3636. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3637. (declare-function add-to-diary-list "diary-lib"
  3638. (date string specifier &optional marker globcolor literal))
  3639. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3640. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3641. (declare-function Info-goto-node "info" (nodename &optional fork))
  3642. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3643. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3644. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3645. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3646. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3647. (declare-function bbdb-record-name "ext:bbdb" (record))
  3648. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3649. (declare-function bibtex-generate-autokey "bibtex" ())
  3650. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3651. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3652. (defvar calc-embedded-close-formula)
  3653. (defvar calc-embedded-open-formula)
  3654. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3655. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3656. (declare-function calendar-check-holidays "holidays" (date))
  3657. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3658. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3659. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3660. (declare-function calendar-forward-day "cal-move" (arg))
  3661. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3662. (declare-function calendar-goto-date "cal-move" (date))
  3663. (declare-function calendar-goto-today "cal-move" ())
  3664. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3665. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3666. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3667. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3668. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3669. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3670. (defvar calendar-mode-map)
  3671. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3672. (declare-function cdlatex-tab "ext:cdlatex" ())
  3673. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3674. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3675. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3676. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3677. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3678. (defvar font-lock-unfontify-region-function)
  3679. (declare-function gnus-article-show-summary "gnus-art" ())
  3680. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3681. (defvar gnus-other-frame-object)
  3682. (defvar gnus-group-name)
  3683. (defvar gnus-article-current)
  3684. (defvar Info-current-file)
  3685. (defvar Info-current-node)
  3686. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3687. (declare-function mh-find-path "mh-utils" ())
  3688. (declare-function mh-get-header-field "mh-utils" (field))
  3689. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3690. (declare-function mh-header-display "mh-show" ())
  3691. (declare-function mh-index-previous-folder "mh-search" ())
  3692. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3693. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3694. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3695. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3696. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3697. (declare-function mh-show-header-display "mh-show" t t)
  3698. (declare-function mh-show-msg "mh-show" (msg))
  3699. (declare-function mh-show-show "mh-show" t t)
  3700. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3701. (defvar mh-progs)
  3702. (defvar mh-current-folder)
  3703. (defvar mh-show-folder-buffer)
  3704. (defvar mh-index-folder)
  3705. (defvar mh-searcher)
  3706. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3707. (declare-function parse-time-string "parse-time" (string))
  3708. (declare-function remember "remember" (&optional initial))
  3709. (declare-function remember-buffer-desc "remember" ())
  3710. (declare-function remember-finalize "remember" ())
  3711. (defvar remember-save-after-remembering)
  3712. (defvar remember-data-file)
  3713. (defvar remember-register)
  3714. (defvar remember-buffer)
  3715. (defvar remember-handler-functions)
  3716. (defvar remember-annotation-functions)
  3717. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3718. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3719. (declare-function rmail-what-message "rmail" ())
  3720. (defvar rmail-current-message)
  3721. (defvar texmathp-why)
  3722. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3723. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3724. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3725. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3726. (declare-function vm-isearch-update "ext:vm-search" ())
  3727. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3728. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3729. (declare-function vm-su-subject "ext:vm-summary" (m))
  3730. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3731. (defvar vm-message-pointer)
  3732. (defvar vm-folder-directory)
  3733. (defvar w3m-current-url)
  3734. (defvar w3m-current-title)
  3735. ;; backward compatibility to old version of wl
  3736. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3737. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3738. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3739. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3740. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3741. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3742. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3743. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3744. (defvar wl-summary-buffer-elmo-folder)
  3745. (defvar wl-summary-buffer-folder-name)
  3746. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3747. (defvar org-latex-regexps)
  3748. (defvar constants-unit-system)
  3749. ;;; Variables for pre-computed regular expressions, all buffer local
  3750. (defvar org-drawer-regexp nil
  3751. "Matches first line of a hidden block.")
  3752. (make-variable-buffer-local 'org-drawer-regexp)
  3753. (defvar org-todo-regexp nil
  3754. "Matches any of the TODO state keywords.")
  3755. (make-variable-buffer-local 'org-todo-regexp)
  3756. (defvar org-not-done-regexp nil
  3757. "Matches any of the TODO state keywords except the last one.")
  3758. (make-variable-buffer-local 'org-not-done-regexp)
  3759. (defvar org-todo-line-regexp nil
  3760. "Matches a headline and puts TODO state into group 2 if present.")
  3761. (make-variable-buffer-local 'org-todo-line-regexp)
  3762. (defvar org-complex-heading-regexp nil
  3763. "Matches a headline and puts everything into groups:
  3764. group 1: the stars
  3765. group 2: The todo keyword, maybe
  3766. group 3: Priority cookie
  3767. group 4: True headline
  3768. group 5: Tags")
  3769. (make-variable-buffer-local 'org-complex-heading-regexp)
  3770. (defvar org-todo-line-tags-regexp nil
  3771. "Matches a headline and puts TODO state into group 2 if present.
  3772. Also put tags into group 4 if tags are present.")
  3773. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3774. (defvar org-nl-done-regexp nil
  3775. "Matches newline followed by a headline with the DONE keyword.")
  3776. (make-variable-buffer-local 'org-nl-done-regexp)
  3777. (defvar org-looking-at-done-regexp nil
  3778. "Matches the DONE keyword a point.")
  3779. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3780. (defvar org-ds-keyword-length 12
  3781. "Maximum length of the Deadline and SCHEDULED keywords.")
  3782. (make-variable-buffer-local 'org-ds-keyword-length)
  3783. (defvar org-deadline-regexp nil
  3784. "Matches the DEADLINE keyword.")
  3785. (make-variable-buffer-local 'org-deadline-regexp)
  3786. (defvar org-deadline-time-regexp nil
  3787. "Matches the DEADLINE keyword together with a time stamp.")
  3788. (make-variable-buffer-local 'org-deadline-time-regexp)
  3789. (defvar org-deadline-line-regexp nil
  3790. "Matches the DEADLINE keyword and the rest of the line.")
  3791. (make-variable-buffer-local 'org-deadline-line-regexp)
  3792. (defvar org-scheduled-regexp nil
  3793. "Matches the SCHEDULED keyword.")
  3794. (make-variable-buffer-local 'org-scheduled-regexp)
  3795. (defvar org-scheduled-time-regexp nil
  3796. "Matches the SCHEDULED keyword together with a time stamp.")
  3797. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3798. (defvar org-closed-time-regexp nil
  3799. "Matches the CLOSED keyword together with a time stamp.")
  3800. (make-variable-buffer-local 'org-closed-time-regexp)
  3801. (defvar org-keyword-time-regexp nil
  3802. "Matches any of the 4 keywords, together with the time stamp.")
  3803. (make-variable-buffer-local 'org-keyword-time-regexp)
  3804. (defvar org-keyword-time-not-clock-regexp nil
  3805. "Matches any of the 3 keywords, together with the time stamp.")
  3806. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3807. (defvar org-maybe-keyword-time-regexp nil
  3808. "Matches a timestamp, possibly preceeded by a keyword.")
  3809. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3810. (defvar org-planning-or-clock-line-re nil
  3811. "Matches a line with planning or clock info.")
  3812. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3813. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3814. rear-nonsticky t mouse-map t fontified t)
  3815. "Properties to remove when a string without properties is wanted.")
  3816. (defsubst org-match-string-no-properties (num &optional string)
  3817. (if (featurep 'xemacs)
  3818. (let ((s (match-string num string)))
  3819. (remove-text-properties 0 (length s) org-rm-props s)
  3820. s)
  3821. (match-string-no-properties num string)))
  3822. (defsubst org-no-properties (s)
  3823. (if (fboundp 'set-text-properties)
  3824. (set-text-properties 0 (length s) nil s)
  3825. (remove-text-properties 0 (length s) org-rm-props s))
  3826. s)
  3827. (defsubst org-get-alist-option (option key)
  3828. (cond ((eq key t) t)
  3829. ((eq option t) t)
  3830. ((assoc key option) (cdr (assoc key option)))
  3831. (t (cdr (assq 'default option)))))
  3832. (defsubst org-inhibit-invisibility ()
  3833. "Modified `buffer-invisibility-spec' for Emacs 21.
  3834. Some ops with invisible text do not work correctly on Emacs 21. For these
  3835. we turn off invisibility temporarily. Use this in a `let' form."
  3836. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3837. (defsubst org-set-local (var value)
  3838. "Make VAR local in current buffer and set it to VALUE."
  3839. (set (make-variable-buffer-local var) value))
  3840. (defsubst org-mode-p ()
  3841. "Check if the current buffer is in Org-mode."
  3842. (eq major-mode 'org-mode))
  3843. (defsubst org-last (list)
  3844. "Return the last element of LIST."
  3845. (car (last list)))
  3846. (defun org-let (list &rest body)
  3847. (eval (cons 'let (cons list body))))
  3848. (put 'org-let 'lisp-indent-function 1)
  3849. (defun org-let2 (list1 list2 &rest body)
  3850. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3851. (put 'org-let2 'lisp-indent-function 2)
  3852. (defconst org-startup-options
  3853. '(("fold" org-startup-folded t)
  3854. ("overview" org-startup-folded t)
  3855. ("nofold" org-startup-folded nil)
  3856. ("showall" org-startup-folded nil)
  3857. ("content" org-startup-folded content)
  3858. ("hidestars" org-hide-leading-stars t)
  3859. ("showstars" org-hide-leading-stars nil)
  3860. ("odd" org-odd-levels-only t)
  3861. ("oddeven" org-odd-levels-only nil)
  3862. ("align" org-startup-align-all-tables t)
  3863. ("noalign" org-startup-align-all-tables nil)
  3864. ("customtime" org-display-custom-times t)
  3865. ("logdone" org-log-done time)
  3866. ("lognotedone" org-log-done note)
  3867. ("nologdone" org-log-done nil)
  3868. ("lognoteclock-out" org-log-note-clock-out t)
  3869. ("nolognoteclock-out" org-log-note-clock-out nil)
  3870. ("logrepeat" org-log-repeat state)
  3871. ("lognoterepeat" org-log-repeat note)
  3872. ("nologrepeat" org-log-repeat nil)
  3873. ("constcgs" constants-unit-system cgs)
  3874. ("constSI" constants-unit-system SI))
  3875. "Variable associated with STARTUP options for org-mode.
  3876. Each element is a list of three items: The startup options as written
  3877. in the #+STARTUP line, the corresponding variable, and the value to
  3878. set this variable to if the option is found. An optional forth element PUSH
  3879. means to push this value onto the list in the variable.")
  3880. (defun org-set-regexps-and-options ()
  3881. "Precompute regular expressions for current buffer."
  3882. (when (org-mode-p)
  3883. (org-set-local 'org-todo-kwd-alist nil)
  3884. (org-set-local 'org-todo-key-alist nil)
  3885. (org-set-local 'org-todo-key-trigger nil)
  3886. (org-set-local 'org-todo-keywords-1 nil)
  3887. (org-set-local 'org-done-keywords nil)
  3888. (org-set-local 'org-todo-heads nil)
  3889. (org-set-local 'org-todo-sets nil)
  3890. (org-set-local 'org-todo-log-states nil)
  3891. (let ((re (org-make-options-regexp
  3892. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3893. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3894. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3895. (splitre "[ \t]+")
  3896. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3897. tail sep kws1 prio props drawers)
  3898. (save-excursion
  3899. (save-restriction
  3900. (widen)
  3901. (goto-char (point-min))
  3902. (while (re-search-forward re nil t)
  3903. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3904. (cond
  3905. ((equal key "CATEGORY")
  3906. (if (string-match "[ \t]+$" value)
  3907. (setq value (replace-match "" t t value)))
  3908. (setq cat value))
  3909. ((member key '("SEQ_TODO" "TODO"))
  3910. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3911. ((equal key "TYP_TODO")
  3912. (push (cons 'type (org-split-string value splitre)) kwds))
  3913. ((equal key "TAGS")
  3914. (setq tags (append tags (org-split-string value splitre))))
  3915. ((equal key "COLUMNS")
  3916. (org-set-local 'org-columns-default-format value))
  3917. ((equal key "LINK")
  3918. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3919. (push (cons (match-string 1 value)
  3920. (org-trim (match-string 2 value)))
  3921. links)))
  3922. ((equal key "PRIORITIES")
  3923. (setq prio (org-split-string value " +")))
  3924. ((equal key "PROPERTY")
  3925. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3926. (push (cons (match-string 1 value) (match-string 2 value))
  3927. props)))
  3928. ((equal key "DRAWERS")
  3929. (setq drawers (org-split-string value splitre)))
  3930. ((equal key "CONSTANTS")
  3931. (setq const (append const (org-split-string value splitre))))
  3932. ((equal key "STARTUP")
  3933. (let ((opts (org-split-string value splitre))
  3934. l var val)
  3935. (while (setq l (pop opts))
  3936. (when (setq l (assoc l org-startup-options))
  3937. (setq var (nth 1 l) val (nth 2 l))
  3938. (if (not (nth 3 l))
  3939. (set (make-local-variable var) val)
  3940. (if (not (listp (symbol-value var)))
  3941. (set (make-local-variable var) nil))
  3942. (set (make-local-variable var) (symbol-value var))
  3943. (add-to-list var val))))))
  3944. ((equal key "ARCHIVE")
  3945. (string-match " *$" value)
  3946. (setq arch (replace-match "" t t value))
  3947. (remove-text-properties 0 (length arch)
  3948. '(face t fontified t) arch)))
  3949. )))
  3950. (when cat
  3951. (org-set-local 'org-category (intern cat))
  3952. (push (cons "CATEGORY" cat) props))
  3953. (when prio
  3954. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3955. (setq prio (mapcar 'string-to-char prio))
  3956. (org-set-local 'org-highest-priority (nth 0 prio))
  3957. (org-set-local 'org-lowest-priority (nth 1 prio))
  3958. (org-set-local 'org-default-priority (nth 2 prio)))
  3959. (and props (org-set-local 'org-local-properties (nreverse props)))
  3960. (and drawers (org-set-local 'org-drawers drawers))
  3961. (and arch (org-set-local 'org-archive-location arch))
  3962. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3963. ;; Process the TODO keywords
  3964. (unless kwds
  3965. ;; Use the global values as if they had been given locally.
  3966. (setq kwds (default-value 'org-todo-keywords))
  3967. (if (stringp (car kwds))
  3968. (setq kwds (list (cons org-todo-interpretation
  3969. (default-value 'org-todo-keywords)))))
  3970. (setq kwds (reverse kwds)))
  3971. (setq kwds (nreverse kwds))
  3972. (let (inter kws kw)
  3973. (while (setq kws (pop kwds))
  3974. (setq inter (pop kws) sep (member "|" kws)
  3975. kws0 (delete "|" (copy-sequence kws))
  3976. kwsa nil
  3977. kws1 (mapcar
  3978. (lambda (x)
  3979. ;; 1 2
  3980. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3981. (progn
  3982. (setq kw (match-string 1 x)
  3983. key (and (match-end 2) (match-string 2 x))
  3984. log (org-extract-log-state-settings x))
  3985. (push (cons kw (and key (string-to-char key))) kwsa)
  3986. (and log (push log org-todo-log-states))
  3987. kw)
  3988. (error "Invalid TODO keyword %s" x)))
  3989. kws0)
  3990. kwsa (if kwsa (append '((:startgroup))
  3991. (nreverse kwsa)
  3992. '((:endgroup))))
  3993. hw (car kws1)
  3994. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3995. tail (list inter hw (car dws) (org-last dws)))
  3996. (add-to-list 'org-todo-heads hw 'append)
  3997. (push kws1 org-todo-sets)
  3998. (setq org-done-keywords (append org-done-keywords dws nil))
  3999. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4000. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4001. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4002. (setq org-todo-sets (nreverse org-todo-sets)
  4003. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4004. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4005. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4006. ;; Process the constants
  4007. (when const
  4008. (let (e cst)
  4009. (while (setq e (pop const))
  4010. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4011. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4012. (setq org-table-formula-constants-local cst)))
  4013. ;; Process the tags.
  4014. (when tags
  4015. (let (e tgs)
  4016. (while (setq e (pop tags))
  4017. (cond
  4018. ((equal e "{") (push '(:startgroup) tgs))
  4019. ((equal e "}") (push '(:endgroup) tgs))
  4020. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4021. (push (cons (match-string 1 e)
  4022. (string-to-char (match-string 2 e)))
  4023. tgs))
  4024. (t (push (list e) tgs))))
  4025. (org-set-local 'org-tag-alist nil)
  4026. (while (setq e (pop tgs))
  4027. (or (and (stringp (car e))
  4028. (assoc (car e) org-tag-alist))
  4029. (push e org-tag-alist))))))
  4030. ;; Compute the regular expressions and other local variables
  4031. (if (not org-done-keywords)
  4032. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4033. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4034. (length org-scheduled-string)))
  4035. org-drawer-regexp
  4036. (concat "^[ \t]*:\\("
  4037. (mapconcat 'regexp-quote org-drawers "\\|")
  4038. "\\):[ \t]*$")
  4039. org-not-done-keywords
  4040. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4041. org-todo-regexp
  4042. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4043. "\\|") "\\)\\>")
  4044. org-not-done-regexp
  4045. (concat "\\<\\("
  4046. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4047. "\\)\\>")
  4048. org-todo-line-regexp
  4049. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4050. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4051. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4052. org-complex-heading-regexp
  4053. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4054. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4055. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4056. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4057. org-nl-done-regexp
  4058. (concat "\n\\*+[ \t]+"
  4059. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4060. "\\)" "\\>")
  4061. org-todo-line-tags-regexp
  4062. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4063. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4064. (org-re
  4065. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4066. org-looking-at-done-regexp
  4067. (concat "^" "\\(?:"
  4068. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4069. "\\>")
  4070. org-deadline-regexp (concat "\\<" org-deadline-string)
  4071. org-deadline-time-regexp
  4072. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4073. org-deadline-line-regexp
  4074. (concat "\\<\\(" org-deadline-string "\\).*")
  4075. org-scheduled-regexp
  4076. (concat "\\<" org-scheduled-string)
  4077. org-scheduled-time-regexp
  4078. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4079. org-closed-time-regexp
  4080. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4081. org-keyword-time-regexp
  4082. (concat "\\<\\(" org-scheduled-string
  4083. "\\|" org-deadline-string
  4084. "\\|" org-closed-string
  4085. "\\|" org-clock-string "\\)"
  4086. " *[[<]\\([^]>]+\\)[]>]")
  4087. org-keyword-time-not-clock-regexp
  4088. (concat "\\<\\(" org-scheduled-string
  4089. "\\|" org-deadline-string
  4090. "\\|" org-closed-string
  4091. "\\)"
  4092. " *[[<]\\([^]>]+\\)[]>]")
  4093. org-maybe-keyword-time-regexp
  4094. (concat "\\(\\<\\(" org-scheduled-string
  4095. "\\|" org-deadline-string
  4096. "\\|" org-closed-string
  4097. "\\|" org-clock-string "\\)\\)?"
  4098. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4099. org-planning-or-clock-line-re
  4100. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4101. "\\|" org-deadline-string
  4102. "\\|" org-closed-string "\\|" org-clock-string
  4103. "\\)\\>\\)")
  4104. )
  4105. (org-compute-latex-and-specials-regexp)
  4106. (org-set-font-lock-defaults)))
  4107. (defun org-extract-log-state-settings (x)
  4108. "Extract the log state setting from a TODO keyword string.
  4109. This will extract info from a string like \"WAIT(w@/!)\"."
  4110. (let (kw key log1 log2)
  4111. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4112. (setq kw (match-string 1 x)
  4113. key (and (match-end 2) (match-string 2 x))
  4114. log1 (and (match-end 3) (match-string 3 x))
  4115. log2 (and (match-end 4) (match-string 4 x)))
  4116. (and (or log1 log2)
  4117. (list kw
  4118. (and log1 (if (equal log1 "!") 'time 'note))
  4119. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4120. (defun org-remove-keyword-keys (list)
  4121. (mapcar (lambda (x)
  4122. (if (string-match "(..?)$" x)
  4123. (substring x 0 (match-beginning 0))
  4124. x))
  4125. list))
  4126. ;; FIXME: this could be done much better, using second characters etc.
  4127. (defun org-assign-fast-keys (alist)
  4128. "Assign fast keys to a keyword-key alist.
  4129. Respect keys that are already there."
  4130. (let (new e k c c1 c2 (char ?a))
  4131. (while (setq e (pop alist))
  4132. (cond
  4133. ((equal e '(:startgroup)) (push e new))
  4134. ((equal e '(:endgroup)) (push e new))
  4135. (t
  4136. (setq k (car e) c2 nil)
  4137. (if (cdr e)
  4138. (setq c (cdr e))
  4139. ;; automatically assign a character.
  4140. (setq c1 (string-to-char
  4141. (downcase (substring
  4142. k (if (= (string-to-char k) ?@) 1 0)))))
  4143. (if (or (rassoc c1 new) (rassoc c1 alist))
  4144. (while (or (rassoc char new) (rassoc char alist))
  4145. (setq char (1+ char)))
  4146. (setq c2 c1))
  4147. (setq c (or c2 char)))
  4148. (push (cons k c) new))))
  4149. (nreverse new)))
  4150. ;;; Some variables ujsed in various places
  4151. (defvar org-window-configuration nil
  4152. "Used in various places to store a window configuration.")
  4153. (defvar org-finish-function nil
  4154. "Function to be called when `C-c C-c' is used.
  4155. This is for getting out of special buffers like remember.")
  4156. ;; FIXME: Occasionally check by commenting these, to make sure
  4157. ;; no other functions uses these, forgetting to let-bind them.
  4158. (defvar entry)
  4159. (defvar state)
  4160. (defvar last-state)
  4161. (defvar date)
  4162. (defvar description)
  4163. ;; Defined somewhere in this file, but used before definition.
  4164. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4165. (defvar org-agenda-buffer-name)
  4166. (defvar org-agenda-undo-list)
  4167. (defvar org-agenda-pending-undo-list)
  4168. (defvar org-agenda-overriding-header)
  4169. (defvar orgtbl-mode)
  4170. (defvar org-html-entities)
  4171. (defvar org-struct-menu)
  4172. (defvar org-org-menu)
  4173. (defvar org-tbl-menu)
  4174. (defvar org-agenda-keymap)
  4175. ;;;; Emacs/XEmacs compatibility
  4176. ;; Overlay compatibility functions
  4177. (defun org-make-overlay (beg end &optional buffer)
  4178. (if (featurep 'xemacs)
  4179. (make-extent beg end buffer)
  4180. (make-overlay beg end buffer)))
  4181. (defun org-delete-overlay (ovl)
  4182. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4183. (defun org-detach-overlay (ovl)
  4184. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4185. (defun org-move-overlay (ovl beg end &optional buffer)
  4186. (if (featurep 'xemacs)
  4187. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4188. (move-overlay ovl beg end buffer)))
  4189. (defun org-overlay-put (ovl prop value)
  4190. (if (featurep 'xemacs)
  4191. (set-extent-property ovl prop value)
  4192. (overlay-put ovl prop value)))
  4193. (defun org-overlay-display (ovl text &optional face evap)
  4194. "Make overlay OVL display TEXT with face FACE."
  4195. (if (featurep 'xemacs)
  4196. (let ((gl (make-glyph text)))
  4197. (and face (set-glyph-face gl face))
  4198. (set-extent-property ovl 'invisible t)
  4199. (set-extent-property ovl 'end-glyph gl))
  4200. (overlay-put ovl 'display text)
  4201. (if face (overlay-put ovl 'face face))
  4202. (if evap (overlay-put ovl 'evaporate t))))
  4203. (defun org-overlay-before-string (ovl text &optional face evap)
  4204. "Make overlay OVL display TEXT with face FACE."
  4205. (if (featurep 'xemacs)
  4206. (let ((gl (make-glyph text)))
  4207. (and face (set-glyph-face gl face))
  4208. (set-extent-property ovl 'begin-glyph gl))
  4209. (if face (org-add-props text nil 'face face))
  4210. (overlay-put ovl 'before-string text)
  4211. (if evap (overlay-put ovl 'evaporate t))))
  4212. (defun org-overlay-get (ovl prop)
  4213. (if (featurep 'xemacs)
  4214. (extent-property ovl prop)
  4215. (overlay-get ovl prop)))
  4216. (defun org-overlays-at (pos)
  4217. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4218. (defun org-overlays-in (&optional start end)
  4219. (if (featurep 'xemacs)
  4220. (extent-list nil start end)
  4221. (overlays-in start end)))
  4222. (defun org-overlay-start (o)
  4223. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4224. (defun org-overlay-end (o)
  4225. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4226. (defun org-find-overlays (prop &optional pos delete)
  4227. "Find all overlays specifying PROP at POS or point.
  4228. If DELETE is non-nil, delete all those overlays."
  4229. (let ((overlays (org-overlays-at (or pos (point))))
  4230. ov found)
  4231. (while (setq ov (pop overlays))
  4232. (if (org-overlay-get ov prop)
  4233. (if delete (org-delete-overlay ov) (push ov found))))
  4234. found))
  4235. ;; Region compatibility
  4236. (defun org-add-hook (hook function &optional append local)
  4237. "Add-hook, compatible with both Emacsen."
  4238. (if (and local (featurep 'xemacs))
  4239. (add-local-hook hook function append)
  4240. (add-hook hook function append local)))
  4241. (defvar org-ignore-region nil
  4242. "To temporarily disable the active region.")
  4243. (defun org-region-active-p ()
  4244. "Is `transient-mark-mode' on and the region active?
  4245. Works on both Emacs and XEmacs."
  4246. (if org-ignore-region
  4247. nil
  4248. (if (featurep 'xemacs)
  4249. (and zmacs-regions (region-active-p))
  4250. (if (fboundp 'use-region-p)
  4251. (use-region-p)
  4252. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4253. ;; Invisibility compatibility
  4254. (defun org-add-to-invisibility-spec (arg)
  4255. "Add elements to `buffer-invisibility-spec'.
  4256. See documentation for `buffer-invisibility-spec' for the kind of elements
  4257. that can be added."
  4258. (cond
  4259. ((fboundp 'add-to-invisibility-spec)
  4260. (add-to-invisibility-spec arg))
  4261. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4262. (setq buffer-invisibility-spec (list arg)))
  4263. (t
  4264. (setq buffer-invisibility-spec
  4265. (cons arg buffer-invisibility-spec)))))
  4266. (defun org-remove-from-invisibility-spec (arg)
  4267. "Remove elements from `buffer-invisibility-spec'."
  4268. (if (fboundp 'remove-from-invisibility-spec)
  4269. (remove-from-invisibility-spec arg)
  4270. (if (consp buffer-invisibility-spec)
  4271. (setq buffer-invisibility-spec
  4272. (delete arg buffer-invisibility-spec)))))
  4273. (defun org-in-invisibility-spec-p (arg)
  4274. "Is ARG a member of `buffer-invisibility-spec'?"
  4275. (if (consp buffer-invisibility-spec)
  4276. (member arg buffer-invisibility-spec)
  4277. nil))
  4278. ;;;; Define the Org-mode
  4279. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4280. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  4281. ;; We use a before-change function to check if a table might need
  4282. ;; an update.
  4283. (defvar org-table-may-need-update t
  4284. "Indicates that a table might need an update.
  4285. This variable is set by `org-before-change-function'.
  4286. `org-table-align' sets it back to nil.")
  4287. (defvar org-mode-map)
  4288. (defvar org-mode-hook nil)
  4289. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4290. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4291. (defvar org-table-buffer-is-an nil)
  4292. (defconst org-outline-regexp "\\*+ ")
  4293. ;;;###autoload
  4294. (define-derived-mode org-mode outline-mode "Org"
  4295. "Outline-based notes management and organizer, alias
  4296. \"Carsten's outline-mode for keeping track of everything.\"
  4297. Org-mode develops organizational tasks around a NOTES file which
  4298. contains information about projects as plain text. Org-mode is
  4299. implemented on top of outline-mode, which is ideal to keep the content
  4300. of large files well structured. It supports ToDo items, deadlines and
  4301. time stamps, which magically appear in the diary listing of the Emacs
  4302. calendar. Tables are easily created with a built-in table editor.
  4303. Plain text URL-like links connect to websites, emails (VM), Usenet
  4304. messages (Gnus), BBDB entries, and any files related to the project.
  4305. For printing and sharing of notes, an Org-mode file (or a part of it)
  4306. can be exported as a structured ASCII or HTML file.
  4307. The following commands are available:
  4308. \\{org-mode-map}"
  4309. ;; Get rid of Outline menus, they are not needed
  4310. ;; Need to do this here because define-derived-mode sets up
  4311. ;; the keymap so late. Still, it is a waste to call this each time
  4312. ;; we switch another buffer into org-mode.
  4313. (if (featurep 'xemacs)
  4314. (when (boundp 'outline-mode-menu-heading)
  4315. ;; Assume this is Greg's port, it used easymenu
  4316. (easy-menu-remove outline-mode-menu-heading)
  4317. (easy-menu-remove outline-mode-menu-show)
  4318. (easy-menu-remove outline-mode-menu-hide))
  4319. (define-key org-mode-map [menu-bar headings] 'undefined)
  4320. (define-key org-mode-map [menu-bar hide] 'undefined)
  4321. (define-key org-mode-map [menu-bar show] 'undefined))
  4322. (easy-menu-add org-org-menu)
  4323. (easy-menu-add org-tbl-menu)
  4324. (org-install-agenda-files-menu)
  4325. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4326. (org-add-to-invisibility-spec '(org-cwidth))
  4327. (when (featurep 'xemacs)
  4328. (org-set-local 'line-move-ignore-invisible t))
  4329. (org-set-local 'outline-regexp org-outline-regexp)
  4330. (org-set-local 'outline-level 'org-outline-level)
  4331. (when (and org-ellipsis
  4332. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4333. (fboundp 'make-glyph-code))
  4334. (unless org-display-table
  4335. (setq org-display-table (make-display-table)))
  4336. (set-display-table-slot
  4337. org-display-table 4
  4338. (vconcat (mapcar
  4339. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4340. org-ellipsis)))
  4341. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4342. (setq buffer-display-table org-display-table))
  4343. (org-set-regexps-and-options)
  4344. ;; Calc embedded
  4345. (org-set-local 'calc-embedded-open-mode "# ")
  4346. (modify-syntax-entry ?# "<")
  4347. (modify-syntax-entry ?@ "w")
  4348. (if org-startup-truncated (setq truncate-lines t))
  4349. (org-set-local 'font-lock-unfontify-region-function
  4350. 'org-unfontify-region)
  4351. ;; Activate before-change-function
  4352. (org-set-local 'org-table-may-need-update t)
  4353. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4354. 'local)
  4355. ;; Check for running clock before killing a buffer
  4356. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4357. ;; Paragraphs and auto-filling
  4358. (org-set-autofill-regexps)
  4359. (setq indent-line-function 'org-indent-line-function)
  4360. (org-update-radio-target-regexp)
  4361. ;; Comment characters
  4362. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4363. (org-set-local 'comment-padding " ")
  4364. ;; Align options lines
  4365. (org-set-local
  4366. 'align-mode-rules-list
  4367. '((org-in-buffer-settings
  4368. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4369. (modes . '(org-mode)))))
  4370. ;; Imenu
  4371. (org-set-local 'imenu-create-index-function
  4372. 'org-imenu-get-tree)
  4373. ;; Make isearch reveal context
  4374. (if (or (featurep 'xemacs)
  4375. (not (boundp 'outline-isearch-open-invisible-function)))
  4376. ;; Emacs 21 and XEmacs make use of the hook
  4377. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4378. ;; Emacs 22 deals with this through a special variable
  4379. (org-set-local 'outline-isearch-open-invisible-function
  4380. (lambda (&rest ignore) (org-show-context 'isearch))))
  4381. ;; If empty file that did not turn on org-mode automatically, make it to.
  4382. (if (and org-insert-mode-line-in-empty-file
  4383. (interactive-p)
  4384. (= (point-min) (point-max)))
  4385. (insert "# -*- mode: org -*-\n\n"))
  4386. (unless org-inhibit-startup
  4387. (when org-startup-align-all-tables
  4388. (let ((bmp (buffer-modified-p)))
  4389. (org-table-map-tables 'org-table-align)
  4390. (set-buffer-modified-p bmp)))
  4391. (org-cycle-hide-drawers 'all)
  4392. (cond
  4393. ((eq org-startup-folded t)
  4394. (org-cycle '(4)))
  4395. ((eq org-startup-folded 'content)
  4396. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4397. (org-cycle '(4)) (org-cycle '(4)))))))
  4398. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4399. (defsubst org-call-with-arg (command arg)
  4400. "Call COMMAND interactively, but pretend prefix are was ARG."
  4401. (let ((current-prefix-arg arg)) (call-interactively command)))
  4402. (defsubst org-current-line (&optional pos)
  4403. (save-excursion
  4404. (and pos (goto-char pos))
  4405. ;; works also in narrowed buffer, because we start at 1, not point-min
  4406. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4407. (defun org-current-time ()
  4408. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4409. (if (> org-time-stamp-rounding-minutes 0)
  4410. (let ((r org-time-stamp-rounding-minutes)
  4411. (time (decode-time)))
  4412. (apply 'encode-time
  4413. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4414. (nthcdr 2 time))))
  4415. (current-time)))
  4416. (defun org-add-props (string plist &rest props)
  4417. "Add text properties to entire string, from beginning to end.
  4418. PLIST may be a list of properties, PROPS are individual properties and values
  4419. that will be added to PLIST. Returns the string that was modified."
  4420. (add-text-properties
  4421. 0 (length string) (if props (append plist props) plist) string)
  4422. string)
  4423. (put 'org-add-props 'lisp-indent-function 2)
  4424. ;;;; Font-Lock stuff, including the activators
  4425. (defvar org-mouse-map (make-sparse-keymap))
  4426. (org-defkey org-mouse-map
  4427. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4428. (org-defkey org-mouse-map
  4429. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4430. (when org-mouse-1-follows-link
  4431. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4432. (when org-tab-follows-link
  4433. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4434. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4435. (when org-return-follows-link
  4436. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4437. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4438. (require 'font-lock)
  4439. (defconst org-non-link-chars "]\t\n\r<>")
  4440. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4441. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4442. (defvar org-link-re-with-space nil
  4443. "Matches a link with spaces, optional angular brackets around it.")
  4444. (defvar org-link-re-with-space2 nil
  4445. "Matches a link with spaces, optional angular brackets around it.")
  4446. (defvar org-angle-link-re nil
  4447. "Matches link with angular brackets, spaces are allowed.")
  4448. (defvar org-plain-link-re nil
  4449. "Matches plain link, without spaces.")
  4450. (defvar org-bracket-link-regexp nil
  4451. "Matches a link in double brackets.")
  4452. (defvar org-bracket-link-analytic-regexp nil
  4453. "Regular expression used to analyze links.
  4454. Here is what the match groups contain after a match:
  4455. 1: http:
  4456. 2: http
  4457. 3: path
  4458. 4: [desc]
  4459. 5: desc")
  4460. (defvar org-any-link-re nil
  4461. "Regular expression matching any link.")
  4462. (defun org-make-link-regexps ()
  4463. "Update the link regular expressions.
  4464. This should be called after the variable `org-link-types' has changed."
  4465. (setq org-link-re-with-space
  4466. (concat
  4467. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4468. "\\([^" org-non-link-chars " ]"
  4469. "[^" org-non-link-chars "]*"
  4470. "[^" org-non-link-chars " ]\\)>?")
  4471. org-link-re-with-space2
  4472. (concat
  4473. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4474. "\\([^" org-non-link-chars " ]"
  4475. "[^]\t\n\r]*"
  4476. "[^" org-non-link-chars " ]\\)>?")
  4477. org-angle-link-re
  4478. (concat
  4479. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4480. "\\([^" org-non-link-chars " ]"
  4481. "[^" org-non-link-chars "]*"
  4482. "\\)>")
  4483. org-plain-link-re
  4484. (concat
  4485. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4486. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4487. org-bracket-link-regexp
  4488. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4489. org-bracket-link-analytic-regexp
  4490. (concat
  4491. "\\[\\["
  4492. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4493. "\\([^]]+\\)"
  4494. "\\]"
  4495. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4496. "\\]")
  4497. org-any-link-re
  4498. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4499. org-angle-link-re "\\)\\|\\("
  4500. org-plain-link-re "\\)")))
  4501. (org-make-link-regexps)
  4502. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4503. "Regular expression for fast time stamp matching.")
  4504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4505. "Regular expression for fast time stamp matching.")
  4506. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4507. "Regular expression matching time strings for analysis.
  4508. This one does not require the space after the date.")
  4509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4510. "Regular expression matching time strings for analysis.")
  4511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4512. "Regular expression matching time stamps, with groups.")
  4513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4514. "Regular expression matching time stamps (also [..]), with groups.")
  4515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4516. "Regular expression matching a time stamp range.")
  4517. (defconst org-tr-regexp-both
  4518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4519. "Regular expression matching a time stamp range.")
  4520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4521. org-ts-regexp "\\)?")
  4522. "Regular expression matching a time stamp or time stamp range.")
  4523. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4524. org-ts-regexp-both "\\)?")
  4525. "Regular expression matching a time stamp or time stamp range.
  4526. The time stamps may be either active or inactive.")
  4527. (defvar org-emph-face nil)
  4528. (defun org-do-emphasis-faces (limit)
  4529. "Run through the buffer and add overlays to links."
  4530. (let (rtn)
  4531. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4532. (if (not (= (char-after (match-beginning 3))
  4533. (char-after (match-beginning 4))))
  4534. (progn
  4535. (setq rtn t)
  4536. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4537. 'face
  4538. (nth 1 (assoc (match-string 3)
  4539. org-emphasis-alist)))
  4540. (add-text-properties (match-beginning 2) (match-end 2)
  4541. '(font-lock-multiline t))
  4542. (when org-hide-emphasis-markers
  4543. (add-text-properties (match-end 4) (match-beginning 5)
  4544. '(invisible org-link))
  4545. (add-text-properties (match-beginning 3) (match-end 3)
  4546. '(invisible org-link)))))
  4547. (backward-char 1))
  4548. rtn))
  4549. (defun org-emphasize (&optional char)
  4550. "Insert or change an emphasis, i.e. a font like bold or italic.
  4551. If there is an active region, change that region to a new emphasis.
  4552. If there is no region, just insert the marker characters and position
  4553. the cursor between them.
  4554. CHAR should be either the marker character, or the first character of the
  4555. HTML tag associated with that emphasis. If CHAR is a space, the means
  4556. to remove the emphasis of the selected region.
  4557. If char is not given (for example in an interactive call) it
  4558. will be prompted for."
  4559. (interactive)
  4560. (let ((eal org-emphasis-alist) e det
  4561. (erc org-emphasis-regexp-components)
  4562. (prompt "")
  4563. (string "") beg end move tag c s)
  4564. (if (org-region-active-p)
  4565. (setq beg (region-beginning) end (region-end)
  4566. string (buffer-substring beg end))
  4567. (setq move t))
  4568. (while (setq e (pop eal))
  4569. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4570. c (aref tag 0))
  4571. (push (cons c (string-to-char (car e))) det)
  4572. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4573. (substring tag 1)))))
  4574. (unless char
  4575. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4576. (setq char (read-char-exclusive)))
  4577. (setq char (or (cdr (assoc char det)) char))
  4578. (if (equal char ?\ )
  4579. (setq s "" move nil)
  4580. (unless (assoc (char-to-string char) org-emphasis-alist)
  4581. (error "No such emphasis marker: \"%c\"" char))
  4582. (setq s (char-to-string char)))
  4583. (while (and (> (length string) 1)
  4584. (equal (substring string 0 1) (substring string -1))
  4585. (assoc (substring string 0 1) org-emphasis-alist))
  4586. (setq string (substring string 1 -1)))
  4587. (setq string (concat s string s))
  4588. (if beg (delete-region beg end))
  4589. (unless (or (bolp)
  4590. (string-match (concat "[" (nth 0 erc) "\n]")
  4591. (char-to-string (char-before (point)))))
  4592. (insert " "))
  4593. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4594. (char-to-string (char-after (point))))
  4595. (insert " ") (backward-char 1))
  4596. (insert string)
  4597. (and move (backward-char 1))))
  4598. (defconst org-nonsticky-props
  4599. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4600. (defun org-activate-plain-links (limit)
  4601. "Run through the buffer and add overlays to links."
  4602. (catch 'exit
  4603. (let (f)
  4604. (while (re-search-forward org-plain-link-re limit t)
  4605. (setq f (get-text-property (match-beginning 0) 'face))
  4606. (if (or (eq f 'org-tag)
  4607. (and (listp f) (memq 'org-tag f)))
  4608. nil
  4609. (add-text-properties (match-beginning 0) (match-end 0)
  4610. (list 'mouse-face 'highlight
  4611. 'rear-nonsticky org-nonsticky-props
  4612. 'keymap org-mouse-map
  4613. ))
  4614. (throw 'exit t))))))
  4615. (defun org-activate-code (limit)
  4616. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4617. (unless (get-text-property (match-beginning 1) 'face)
  4618. (remove-text-properties (match-beginning 0) (match-end 0)
  4619. '(display t invisible t intangible t))
  4620. t)))
  4621. (defun org-activate-angle-links (limit)
  4622. "Run through the buffer and add overlays to links."
  4623. (if (re-search-forward org-angle-link-re limit t)
  4624. (progn
  4625. (add-text-properties (match-beginning 0) (match-end 0)
  4626. (list 'mouse-face 'highlight
  4627. 'rear-nonsticky org-nonsticky-props
  4628. 'keymap org-mouse-map
  4629. ))
  4630. t)))
  4631. (defmacro org-maybe-intangible (props)
  4632. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4633. In emacs 21, invisible text is not avoided by the command loop, so the
  4634. intangible property is needed to make sure point skips this text.
  4635. In Emacs 22, this is not necessary. The intangible text property has
  4636. led to problems with flyspell. These problems are fixed in flyspell.el,
  4637. but we still avoid setting the property in Emacs 22 and later.
  4638. We use a macro so that the test can happen at compilation time."
  4639. (if (< emacs-major-version 22)
  4640. `(append '(intangible t) ,props)
  4641. props))
  4642. (defun org-activate-bracket-links (limit)
  4643. "Run through the buffer and add overlays to bracketed links."
  4644. (if (re-search-forward org-bracket-link-regexp limit t)
  4645. (let* ((help (concat "LINK: "
  4646. (org-match-string-no-properties 1)))
  4647. ;; FIXME: above we should remove the escapes.
  4648. ;; but that requires another match, protecting match data,
  4649. ;; a lot of overhead for font-lock.
  4650. (ip (org-maybe-intangible
  4651. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4652. 'keymap org-mouse-map 'mouse-face 'highlight
  4653. 'font-lock-multiline t 'help-echo help)))
  4654. (vp (list 'rear-nonsticky org-nonsticky-props
  4655. 'keymap org-mouse-map 'mouse-face 'highlight
  4656. ' font-lock-multiline t 'help-echo help)))
  4657. ;; We need to remove the invisible property here. Table narrowing
  4658. ;; may have made some of this invisible.
  4659. (remove-text-properties (match-beginning 0) (match-end 0)
  4660. '(invisible nil))
  4661. (if (match-end 3)
  4662. (progn
  4663. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4664. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4665. (add-text-properties (match-end 3) (match-end 0) ip))
  4666. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4667. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4668. (add-text-properties (match-end 1) (match-end 0) ip))
  4669. t)))
  4670. (defun org-activate-dates (limit)
  4671. "Run through the buffer and add overlays to dates."
  4672. (if (re-search-forward org-tsr-regexp-both limit t)
  4673. (progn
  4674. (add-text-properties (match-beginning 0) (match-end 0)
  4675. (list 'mouse-face 'highlight
  4676. 'rear-nonsticky org-nonsticky-props
  4677. 'keymap org-mouse-map))
  4678. (when org-display-custom-times
  4679. (if (match-end 3)
  4680. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4681. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4682. t)))
  4683. (defvar org-target-link-regexp nil
  4684. "Regular expression matching radio targets in plain text.")
  4685. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4686. "Regular expression matching a link target.")
  4687. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4688. "Regular expression matching a radio target.")
  4689. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4690. "Regular expression matching any target.")
  4691. (defun org-activate-target-links (limit)
  4692. "Run through the buffer and add overlays to target matches."
  4693. (when org-target-link-regexp
  4694. (let ((case-fold-search t))
  4695. (if (re-search-forward org-target-link-regexp limit t)
  4696. (progn
  4697. (add-text-properties (match-beginning 0) (match-end 0)
  4698. (list 'mouse-face 'highlight
  4699. 'rear-nonsticky org-nonsticky-props
  4700. 'keymap org-mouse-map
  4701. 'help-echo "Radio target link"
  4702. 'org-linked-text t))
  4703. t)))))
  4704. (defun org-update-radio-target-regexp ()
  4705. "Find all radio targets in this file and update the regular expression."
  4706. (interactive)
  4707. (when (memq 'radio org-activate-links)
  4708. (setq org-target-link-regexp
  4709. (org-make-target-link-regexp (org-all-targets 'radio)))
  4710. (org-restart-font-lock)))
  4711. (defun org-hide-wide-columns (limit)
  4712. (let (s e)
  4713. (setq s (text-property-any (point) (or limit (point-max))
  4714. 'org-cwidth t))
  4715. (when s
  4716. (setq e (next-single-property-change s 'org-cwidth))
  4717. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4718. (goto-char e)
  4719. t)))
  4720. (defvar org-latex-and-specials-regexp nil
  4721. "Regular expression for highlighting export special stuff.")
  4722. (defvar org-match-substring-regexp)
  4723. (defvar org-match-substring-with-braces-regexp)
  4724. (defvar org-export-html-special-string-regexps)
  4725. (defun org-compute-latex-and-specials-regexp ()
  4726. "Compute regular expression for stuff treated specially by exporters."
  4727. (if (not org-highlight-latex-fragments-and-specials)
  4728. (org-set-local 'org-latex-and-specials-regexp nil)
  4729. (let*
  4730. ((matchers (plist-get org-format-latex-options :matchers))
  4731. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4732. org-latex-regexps)))
  4733. (options (org-combine-plists (org-default-export-plist)
  4734. (org-infile-export-plist)))
  4735. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4736. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4737. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4738. (org-export-html-expand (plist-get options :expand-quoted-html))
  4739. (org-export-with-special-strings (plist-get options :special-strings))
  4740. (re-sub
  4741. (cond
  4742. ((equal org-export-with-sub-superscripts '{})
  4743. (list org-match-substring-with-braces-regexp))
  4744. (org-export-with-sub-superscripts
  4745. (list org-match-substring-regexp))
  4746. (t nil)))
  4747. (re-latex
  4748. (if org-export-with-LaTeX-fragments
  4749. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4750. (re-macros
  4751. (if org-export-with-TeX-macros
  4752. (list (concat "\\\\"
  4753. (regexp-opt
  4754. (append (mapcar 'car org-html-entities)
  4755. (if (boundp 'org-latex-entities)
  4756. org-latex-entities nil))
  4757. 'words))) ; FIXME
  4758. ))
  4759. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4760. (re-special (if org-export-with-special-strings
  4761. (mapcar (lambda (x) (car x))
  4762. org-export-html-special-string-regexps)))
  4763. (re-rest
  4764. (delq nil
  4765. (list
  4766. (if org-export-html-expand "@<[^>\n]+>")
  4767. ))))
  4768. (org-set-local
  4769. 'org-latex-and-specials-regexp
  4770. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4771. re-rest) "\\|")))))
  4772. (defface org-latex-and-export-specials
  4773. (let ((font (cond ((assq :inherit custom-face-attributes)
  4774. '(:inherit underline))
  4775. (t '(:underline t)))))
  4776. `((((class grayscale) (background light))
  4777. (:foreground "DimGray" ,@font))
  4778. (((class grayscale) (background dark))
  4779. (:foreground "LightGray" ,@font))
  4780. (((class color) (background light))
  4781. (:foreground "SaddleBrown"))
  4782. (((class color) (background dark))
  4783. (:foreground "burlywood"))
  4784. (t (,@font))))
  4785. "Face used to highlight math latex and other special exporter stuff."
  4786. :group 'org-faces)
  4787. (defun org-do-latex-and-special-faces (limit)
  4788. "Run through the buffer and add overlays to links."
  4789. (when org-latex-and-specials-regexp
  4790. (let (rtn d)
  4791. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4792. limit t))
  4793. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4794. 'face))
  4795. '(org-code org-verbatim underline)))
  4796. (progn
  4797. (setq rtn t
  4798. d (cond ((member (char-after (1+ (match-beginning 0)))
  4799. '(?_ ?^)) 1)
  4800. (t 0)))
  4801. (font-lock-prepend-text-property
  4802. (+ d (match-beginning 0)) (match-end 0)
  4803. 'face 'org-latex-and-export-specials)
  4804. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4805. '(font-lock-multiline t)))))
  4806. rtn)))
  4807. (defun org-restart-font-lock ()
  4808. "Restart font-lock-mode, to force refontification."
  4809. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4810. (font-lock-mode -1)
  4811. (font-lock-mode 1)))
  4812. (defun org-all-targets (&optional radio)
  4813. "Return a list of all targets in this file.
  4814. With optional argument RADIO, only find radio targets."
  4815. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4816. rtn)
  4817. (save-excursion
  4818. (goto-char (point-min))
  4819. (while (re-search-forward re nil t)
  4820. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4821. rtn)))
  4822. (defun org-make-target-link-regexp (targets)
  4823. "Make regular expression matching all strings in TARGETS.
  4824. The regular expression finds the targets also if there is a line break
  4825. between words."
  4826. (and targets
  4827. (concat
  4828. "\\<\\("
  4829. (mapconcat
  4830. (lambda (x)
  4831. (while (string-match " +" x)
  4832. (setq x (replace-match "\\s-+" t t x)))
  4833. x)
  4834. targets
  4835. "\\|")
  4836. "\\)\\>")))
  4837. (defun org-activate-tags (limit)
  4838. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4839. (progn
  4840. (add-text-properties (match-beginning 1) (match-end 1)
  4841. (list 'mouse-face 'highlight
  4842. 'rear-nonsticky org-nonsticky-props
  4843. 'keymap org-mouse-map))
  4844. t)))
  4845. (defun org-outline-level ()
  4846. (save-excursion
  4847. (looking-at outline-regexp)
  4848. (if (match-beginning 1)
  4849. (+ (org-get-string-indentation (match-string 1)) 1000)
  4850. (1- (- (match-end 0) (match-beginning 0))))))
  4851. (defvar org-font-lock-keywords nil)
  4852. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4853. "Regular expression matching a property line.")
  4854. (defun org-set-font-lock-defaults ()
  4855. (let* ((em org-fontify-emphasized-text)
  4856. (lk org-activate-links)
  4857. (org-font-lock-extra-keywords
  4858. (list
  4859. ;; Headlines
  4860. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4861. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4862. ;; Table lines
  4863. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4864. (1 'org-table t))
  4865. ;; Table internals
  4866. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4867. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4868. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4869. ;; Drawers
  4870. (list org-drawer-regexp '(0 'org-special-keyword t))
  4871. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4872. ;; Properties
  4873. (list org-property-re
  4874. '(1 'org-special-keyword t)
  4875. '(3 'org-property-value t))
  4876. (if org-format-transports-properties-p
  4877. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4878. ;; Links
  4879. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4880. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4881. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4882. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4883. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4884. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4885. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4886. '(org-hide-wide-columns (0 nil append))
  4887. ;; TODO lines
  4888. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4889. '(1 (org-get-todo-face 1) t))
  4890. ;; DONE
  4891. (if org-fontify-done-headline
  4892. (list (concat "^[*]+ +\\<\\("
  4893. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4894. "\\)\\(.*\\)")
  4895. '(2 'org-headline-done t))
  4896. nil)
  4897. ;; Priorities
  4898. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4899. ;; Special keywords
  4900. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4901. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4902. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4903. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4904. ;; Emphasis
  4905. (if em
  4906. (if (featurep 'xemacs)
  4907. '(org-do-emphasis-faces (0 nil append))
  4908. '(org-do-emphasis-faces)))
  4909. ;; Checkboxes
  4910. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4911. 2 'bold prepend)
  4912. (if org-provide-checkbox-statistics
  4913. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4914. (0 (org-get-checkbox-statistics-face) t)))
  4915. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4916. '(1 'org-archived prepend))
  4917. ;; Specials
  4918. '(org-do-latex-and-special-faces)
  4919. ;; Code
  4920. '(org-activate-code (1 'org-code t))
  4921. ;; COMMENT
  4922. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4923. "\\|" org-quote-string "\\)\\>")
  4924. '(1 'org-special-keyword t))
  4925. '("^#.*" (0 'font-lock-comment-face t))
  4926. )))
  4927. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4928. ;; Now set the full font-lock-keywords
  4929. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4930. (org-set-local 'font-lock-defaults
  4931. '(org-font-lock-keywords t nil nil backward-paragraph))
  4932. (kill-local-variable 'font-lock-keywords) nil))
  4933. (defvar org-m nil)
  4934. (defvar org-l nil)
  4935. (defvar org-f nil)
  4936. (defun org-get-level-face (n)
  4937. "Get the right face for match N in font-lock matching of healdines."
  4938. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4939. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4940. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4941. (cond
  4942. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4943. ((eq n 2) org-f)
  4944. (t (if org-level-color-stars-only nil org-f))))
  4945. (defun org-get-todo-face (kwd)
  4946. "Get the right face for a TODO keyword KWD.
  4947. If KWD is a number, get the corresponding match group."
  4948. (if (numberp kwd) (setq kwd (match-string kwd)))
  4949. (or (cdr (assoc kwd org-todo-keyword-faces))
  4950. (and (member kwd org-done-keywords) 'org-done)
  4951. 'org-todo))
  4952. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4953. "Remove fontification and activation overlays from links."
  4954. (font-lock-default-unfontify-region beg end)
  4955. (let* ((buffer-undo-list t)
  4956. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4957. (inhibit-modification-hooks t)
  4958. deactivate-mark buffer-file-name buffer-file-truename)
  4959. (remove-text-properties beg end
  4960. '(mouse-face t keymap t org-linked-text t
  4961. invisible t intangible t))))
  4962. ;;;; Visibility cycling, including org-goto and indirect buffer
  4963. ;;; Cycling
  4964. (defvar org-cycle-global-status nil)
  4965. (make-variable-buffer-local 'org-cycle-global-status)
  4966. (defvar org-cycle-subtree-status nil)
  4967. (make-variable-buffer-local 'org-cycle-subtree-status)
  4968. ;;;###autoload
  4969. (defun org-cycle (&optional arg)
  4970. "Visibility cycling for Org-mode.
  4971. - When this function is called with a prefix argument, rotate the entire
  4972. buffer through 3 states (global cycling)
  4973. 1. OVERVIEW: Show only top-level headlines.
  4974. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4975. 3. SHOW ALL: Show everything.
  4976. - When point is at the beginning of a headline, rotate the subtree started
  4977. by this line through 3 different states (local cycling)
  4978. 1. FOLDED: Only the main headline is shown.
  4979. 2. CHILDREN: The main headline and the direct children are shown.
  4980. From this state, you can move to one of the children
  4981. and zoom in further.
  4982. 3. SUBTREE: Show the entire subtree, including body text.
  4983. - When there is a numeric prefix, go up to a heading with level ARG, do
  4984. a `show-subtree' and return to the previous cursor position. If ARG
  4985. is negative, go up that many levels.
  4986. - When point is not at the beginning of a headline, execute
  4987. `indent-relative', like TAB normally does. See the option
  4988. `org-cycle-emulate-tab' for details.
  4989. - Special case: if point is at the beginning of the buffer and there is
  4990. no headline in line 1, this function will act as if called with prefix arg.
  4991. But only if also the variable `org-cycle-global-at-bob' is t."
  4992. (interactive "P")
  4993. (let* ((outline-regexp
  4994. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4995. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4996. outline-regexp))
  4997. (bob-special (and org-cycle-global-at-bob (bobp)
  4998. (not (looking-at outline-regexp))))
  4999. (org-cycle-hook
  5000. (if bob-special
  5001. (delq 'org-optimize-window-after-visibility-change
  5002. (copy-sequence org-cycle-hook))
  5003. org-cycle-hook))
  5004. (pos (point)))
  5005. (if (or bob-special (equal arg '(4)))
  5006. ;; special case: use global cycling
  5007. (setq arg t))
  5008. (cond
  5009. ((org-at-table-p 'any)
  5010. ;; Enter the table or move to the next field in the table
  5011. (or (org-table-recognize-table.el)
  5012. (progn
  5013. (if arg (org-table-edit-field t)
  5014. (org-table-justify-field-maybe)
  5015. (call-interactively 'org-table-next-field)))))
  5016. ((eq arg t) ;; Global cycling
  5017. (cond
  5018. ((and (eq last-command this-command)
  5019. (eq org-cycle-global-status 'overview))
  5020. ;; We just created the overview - now do table of contents
  5021. ;; This can be slow in very large buffers, so indicate action
  5022. (message "CONTENTS...")
  5023. (org-content)
  5024. (message "CONTENTS...done")
  5025. (setq org-cycle-global-status 'contents)
  5026. (run-hook-with-args 'org-cycle-hook 'contents))
  5027. ((and (eq last-command this-command)
  5028. (eq org-cycle-global-status 'contents))
  5029. ;; We just showed the table of contents - now show everything
  5030. (show-all)
  5031. (message "SHOW ALL")
  5032. (setq org-cycle-global-status 'all)
  5033. (run-hook-with-args 'org-cycle-hook 'all))
  5034. (t
  5035. ;; Default action: go to overview
  5036. (org-overview)
  5037. (message "OVERVIEW")
  5038. (setq org-cycle-global-status 'overview)
  5039. (run-hook-with-args 'org-cycle-hook 'overview))))
  5040. ((and org-drawers org-drawer-regexp
  5041. (save-excursion
  5042. (beginning-of-line 1)
  5043. (looking-at org-drawer-regexp)))
  5044. ;; Toggle block visibility
  5045. (org-flag-drawer
  5046. (not (get-char-property (match-end 0) 'invisible))))
  5047. ((integerp arg)
  5048. ;; Show-subtree, ARG levels up from here.
  5049. (save-excursion
  5050. (org-back-to-heading)
  5051. (outline-up-heading (if (< arg 0) (- arg)
  5052. (- (funcall outline-level) arg)))
  5053. (org-show-subtree)))
  5054. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5055. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5056. ;; At a heading: rotate between three different views
  5057. (org-back-to-heading)
  5058. (let ((goal-column 0) eoh eol eos)
  5059. ;; First, some boundaries
  5060. (save-excursion
  5061. (org-back-to-heading)
  5062. (save-excursion
  5063. (beginning-of-line 2)
  5064. (while (and (not (eobp)) ;; this is like `next-line'
  5065. (get-char-property (1- (point)) 'invisible))
  5066. (beginning-of-line 2)) (setq eol (point)))
  5067. (outline-end-of-heading) (setq eoh (point))
  5068. (org-end-of-subtree t)
  5069. (unless (eobp)
  5070. (skip-chars-forward " \t\n")
  5071. (beginning-of-line 1) ; in case this is an item
  5072. )
  5073. (setq eos (1- (point))))
  5074. ;; Find out what to do next and set `this-command'
  5075. (cond
  5076. ((= eos eoh)
  5077. ;; Nothing is hidden behind this heading
  5078. (message "EMPTY ENTRY")
  5079. (setq org-cycle-subtree-status nil)
  5080. (save-excursion
  5081. (goto-char eos)
  5082. (outline-next-heading)
  5083. (if (org-invisible-p) (org-flag-heading nil))))
  5084. ((or (>= eol eos)
  5085. (not (string-match "\\S-" (buffer-substring eol eos))))
  5086. ;; Entire subtree is hidden in one line: open it
  5087. (org-show-entry)
  5088. (show-children)
  5089. (message "CHILDREN")
  5090. (save-excursion
  5091. (goto-char eos)
  5092. (outline-next-heading)
  5093. (if (org-invisible-p) (org-flag-heading nil)))
  5094. (setq org-cycle-subtree-status 'children)
  5095. (run-hook-with-args 'org-cycle-hook 'children))
  5096. ((and (eq last-command this-command)
  5097. (eq org-cycle-subtree-status 'children))
  5098. ;; We just showed the children, now show everything.
  5099. (org-show-subtree)
  5100. (message "SUBTREE")
  5101. (setq org-cycle-subtree-status 'subtree)
  5102. (run-hook-with-args 'org-cycle-hook 'subtree))
  5103. (t
  5104. ;; Default action: hide the subtree.
  5105. (hide-subtree)
  5106. (message "FOLDED")
  5107. (setq org-cycle-subtree-status 'folded)
  5108. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5109. ;; TAB emulation
  5110. (buffer-read-only (org-back-to-heading))
  5111. ((org-try-cdlatex-tab))
  5112. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5113. (or (not (bolp))
  5114. (not (looking-at outline-regexp))))
  5115. (call-interactively (global-key-binding "\t")))
  5116. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5117. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5118. (or (and (eq org-cycle-emulate-tab 'white)
  5119. (= (match-end 0) (point-at-eol)))
  5120. (and (eq org-cycle-emulate-tab 'whitestart)
  5121. (>= (match-end 0) pos))))
  5122. t
  5123. (eq org-cycle-emulate-tab t))
  5124. ; (if (and (looking-at "[ \n\r\t]")
  5125. ; (string-match "^[ \t]*$" (buffer-substring
  5126. ; (point-at-bol) (point))))
  5127. ; (progn
  5128. ; (beginning-of-line 1)
  5129. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5130. (call-interactively (global-key-binding "\t")))
  5131. (t (save-excursion
  5132. (org-back-to-heading)
  5133. (org-cycle))))))
  5134. ;;;###autoload
  5135. (defun org-global-cycle (&optional arg)
  5136. "Cycle the global visibility. For details see `org-cycle'."
  5137. (interactive "P")
  5138. (let ((org-cycle-include-plain-lists
  5139. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5140. (if (integerp arg)
  5141. (progn
  5142. (show-all)
  5143. (hide-sublevels arg)
  5144. (setq org-cycle-global-status 'contents))
  5145. (org-cycle '(4)))))
  5146. (defun org-overview ()
  5147. "Switch to overview mode, shoing only top-level headlines.
  5148. Really, this shows all headlines with level equal or greater than the level
  5149. of the first headline in the buffer. This is important, because if the
  5150. first headline is not level one, then (hide-sublevels 1) gives confusing
  5151. results."
  5152. (interactive)
  5153. (let ((level (save-excursion
  5154. (goto-char (point-min))
  5155. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5156. (progn
  5157. (goto-char (match-beginning 0))
  5158. (funcall outline-level))))))
  5159. (and level (hide-sublevels level))))
  5160. (defun org-content (&optional arg)
  5161. "Show all headlines in the buffer, like a table of contents.
  5162. With numerical argument N, show content up to level N."
  5163. (interactive "P")
  5164. (save-excursion
  5165. ;; Visit all headings and show their offspring
  5166. (and (integerp arg) (org-overview))
  5167. (goto-char (point-max))
  5168. (catch 'exit
  5169. (while (and (progn (condition-case nil
  5170. (outline-previous-visible-heading 1)
  5171. (error (goto-char (point-min))))
  5172. t)
  5173. (looking-at outline-regexp))
  5174. (if (integerp arg)
  5175. (show-children (1- arg))
  5176. (show-branches))
  5177. (if (bobp) (throw 'exit nil))))))
  5178. (defun org-optimize-window-after-visibility-change (state)
  5179. "Adjust the window after a change in outline visibility.
  5180. This function is the default value of the hook `org-cycle-hook'."
  5181. (when (get-buffer-window (current-buffer))
  5182. (cond
  5183. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5184. ; ((eq state 'overview) (org-beginning-of-line))
  5185. ((eq state 'content) nil)
  5186. ((eq state 'all) nil)
  5187. ((eq state 'folded) nil)
  5188. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5189. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5190. (defun org-compact-display-after-subtree-move ()
  5191. (let (beg end)
  5192. (save-excursion
  5193. (if (org-up-heading-safe)
  5194. (progn
  5195. (hide-subtree)
  5196. (show-entry)
  5197. (show-children)
  5198. (org-cycle-show-empty-lines 'children)
  5199. (org-cycle-hide-drawers 'children))
  5200. (org-overview)))))
  5201. (defun org-cycle-show-empty-lines (state)
  5202. "Show empty lines above all visible headlines.
  5203. The region to be covered depends on STATE when called through
  5204. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5205. entire buffer covered. Note that an empty line is only shown if there
  5206. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5207. (when (> org-cycle-separator-lines 0)
  5208. (save-excursion
  5209. (let* ((n org-cycle-separator-lines)
  5210. (re (cond
  5211. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5212. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5213. (t (let ((ns (number-to-string (- n 2))))
  5214. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5215. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5216. beg end)
  5217. (cond
  5218. ((memq state '(overview contents t))
  5219. (setq beg (point-min) end (point-max)))
  5220. ((memq state '(children folded))
  5221. (setq beg (point) end (progn (org-end-of-subtree t t)
  5222. (beginning-of-line 2)
  5223. (point)))))
  5224. (when beg
  5225. (goto-char beg)
  5226. (while (re-search-forward re end t)
  5227. (if (not (get-char-property (match-end 1) 'invisible))
  5228. (outline-flag-region
  5229. (match-beginning 1) (match-end 1) nil)))))))
  5230. ;; Never hide empty lines at the end of the file.
  5231. (save-excursion
  5232. (goto-char (point-max))
  5233. (outline-previous-heading)
  5234. (outline-end-of-heading)
  5235. (if (and (looking-at "[ \t\n]+")
  5236. (= (match-end 0) (point-max)))
  5237. (outline-flag-region (point) (match-end 0) nil))))
  5238. (defun org-subtree-end-visible-p ()
  5239. "Is the end of the current subtree visible?"
  5240. (pos-visible-in-window-p
  5241. (save-excursion (org-end-of-subtree t) (point))))
  5242. (defun org-first-headline-recenter (&optional N)
  5243. "Move cursor to the first headline and recenter the headline.
  5244. Optional argument N means, put the headline into the Nth line of the window."
  5245. (goto-char (point-min))
  5246. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5247. (beginning-of-line)
  5248. (recenter (prefix-numeric-value N))))
  5249. ;;; Org-goto
  5250. (defvar org-goto-window-configuration nil)
  5251. (defvar org-goto-marker nil)
  5252. (defvar org-goto-map
  5253. (let ((map (make-sparse-keymap)))
  5254. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5255. (while (setq cmd (pop cmds))
  5256. (substitute-key-definition cmd cmd map global-map)))
  5257. (suppress-keymap map)
  5258. (org-defkey map "\C-m" 'org-goto-ret)
  5259. (org-defkey map [(return)] 'org-goto-ret)
  5260. (org-defkey map [(left)] 'org-goto-left)
  5261. (org-defkey map [(right)] 'org-goto-right)
  5262. (org-defkey map [(control ?g)] 'org-goto-quit)
  5263. (org-defkey map "\C-i" 'org-cycle)
  5264. (org-defkey map [(tab)] 'org-cycle)
  5265. (org-defkey map [(down)] 'outline-next-visible-heading)
  5266. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5267. (if org-goto-auto-isearch
  5268. (if (fboundp 'define-key-after)
  5269. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5270. nil)
  5271. (org-defkey map "q" 'org-goto-quit)
  5272. (org-defkey map "n" 'outline-next-visible-heading)
  5273. (org-defkey map "p" 'outline-previous-visible-heading)
  5274. (org-defkey map "f" 'outline-forward-same-level)
  5275. (org-defkey map "b" 'outline-backward-same-level)
  5276. (org-defkey map "u" 'outline-up-heading))
  5277. (org-defkey map "/" 'org-occur)
  5278. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5279. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5280. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5281. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5282. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5283. map))
  5284. (defconst org-goto-help
  5285. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5286. RET=jump to location [Q]uit and return to previous location
  5287. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5288. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5289. (defun org-goto (&optional alternative-interface)
  5290. "Look up a different location in the current file, keeping current visibility.
  5291. When you want look-up or go to a different location in a document, the
  5292. fastest way is often to fold the entire buffer and then dive into the tree.
  5293. This method has the disadvantage, that the previous location will be folded,
  5294. which may not be what you want.
  5295. This command works around this by showing a copy of the current buffer
  5296. in an indirect buffer, in overview mode. You can dive into the tree in
  5297. that copy, use org-occur and incremental search to find a location.
  5298. When pressing RET or `Q', the command returns to the original buffer in
  5299. which the visibility is still unchanged. After RET is will also jump to
  5300. the location selected in the indirect buffer and expose the
  5301. the headline hierarchy above."
  5302. (interactive "P")
  5303. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5304. (org-refile-use-outline-path t)
  5305. (interface
  5306. (if (not alternative-interface)
  5307. org-goto-interface
  5308. (if (eq org-goto-interface 'outline)
  5309. 'outline-path-completion
  5310. 'outline)))
  5311. (org-goto-start-pos (point))
  5312. (selected-point
  5313. (if (eq interface 'outline)
  5314. (car (org-get-location (current-buffer) org-goto-help))
  5315. (nth 3 (org-refile-get-location "Goto: ")))))
  5316. (if selected-point
  5317. (progn
  5318. (org-mark-ring-push org-goto-start-pos)
  5319. (goto-char selected-point)
  5320. (if (or (org-invisible-p) (org-invisible-p2))
  5321. (org-show-context 'org-goto)))
  5322. (message "Quit"))))
  5323. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5324. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5325. (defvar org-goto-local-auto-isearch-map) ; defined below
  5326. (defun org-get-location (buf help)
  5327. "Let the user select a location in the Org-mode buffer BUF.
  5328. This function uses a recursive edit. It returns the selected position
  5329. or nil."
  5330. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5331. (isearch-hide-immediately nil)
  5332. (isearch-search-fun-function
  5333. (lambda () 'org-goto-local-search-forward-headings))
  5334. (org-goto-selected-point org-goto-exit-command))
  5335. (save-excursion
  5336. (save-window-excursion
  5337. (delete-other-windows)
  5338. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5339. (switch-to-buffer
  5340. (condition-case nil
  5341. (make-indirect-buffer (current-buffer) "*org-goto*")
  5342. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5343. (with-output-to-temp-buffer "*Help*"
  5344. (princ help))
  5345. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5346. (setq buffer-read-only nil)
  5347. (let ((org-startup-truncated t)
  5348. (org-startup-folded nil)
  5349. (org-startup-align-all-tables nil))
  5350. (org-mode)
  5351. (org-overview))
  5352. (setq buffer-read-only t)
  5353. (if (and (boundp 'org-goto-start-pos)
  5354. (integer-or-marker-p org-goto-start-pos))
  5355. (let ((org-show-hierarchy-above t)
  5356. (org-show-siblings t)
  5357. (org-show-following-heading t))
  5358. (goto-char org-goto-start-pos)
  5359. (and (org-invisible-p) (org-show-context)))
  5360. (goto-char (point-min)))
  5361. (org-beginning-of-line)
  5362. (message "Select location and press RET")
  5363. (use-local-map org-goto-map)
  5364. (recursive-edit)
  5365. ))
  5366. (kill-buffer "*org-goto*")
  5367. (cons org-goto-selected-point org-goto-exit-command)))
  5368. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5369. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5370. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5371. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5372. (defun org-goto-local-search-forward-headings (string bound noerror)
  5373. "Search and make sure that anu matches are in headlines."
  5374. (catch 'return
  5375. (while (search-forward string bound noerror)
  5376. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5377. (and (member :headline context)
  5378. (not (member :tags context))))
  5379. (throw 'return (point))))))
  5380. (defun org-goto-local-auto-isearch ()
  5381. "Start isearch."
  5382. (interactive)
  5383. (goto-char (point-min))
  5384. (let ((keys (this-command-keys)))
  5385. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5386. (isearch-mode t)
  5387. (isearch-process-search-char (string-to-char keys)))))
  5388. (defun org-goto-ret (&optional arg)
  5389. "Finish `org-goto' by going to the new location."
  5390. (interactive "P")
  5391. (setq org-goto-selected-point (point)
  5392. org-goto-exit-command 'return)
  5393. (throw 'exit nil))
  5394. (defun org-goto-left ()
  5395. "Finish `org-goto' by going to the new location."
  5396. (interactive)
  5397. (if (org-on-heading-p)
  5398. (progn
  5399. (beginning-of-line 1)
  5400. (setq org-goto-selected-point (point)
  5401. org-goto-exit-command 'left)
  5402. (throw 'exit nil))
  5403. (error "Not on a heading")))
  5404. (defun org-goto-right ()
  5405. "Finish `org-goto' by going to the new location."
  5406. (interactive)
  5407. (if (org-on-heading-p)
  5408. (progn
  5409. (setq org-goto-selected-point (point)
  5410. org-goto-exit-command 'right)
  5411. (throw 'exit nil))
  5412. (error "Not on a heading")))
  5413. (defun org-goto-quit ()
  5414. "Finish `org-goto' without cursor motion."
  5415. (interactive)
  5416. (setq org-goto-selected-point nil)
  5417. (setq org-goto-exit-command 'quit)
  5418. (throw 'exit nil))
  5419. ;;; Indirect buffer display of subtrees
  5420. (defvar org-indirect-dedicated-frame nil
  5421. "This is the frame being used for indirect tree display.")
  5422. (defvar org-last-indirect-buffer nil)
  5423. (defun org-tree-to-indirect-buffer (&optional arg)
  5424. "Create indirect buffer and narrow it to current subtree.
  5425. With numerical prefix ARG, go up to this level and then take that tree.
  5426. If ARG is negative, go up that many levels.
  5427. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5428. indirect buffer previously made with this command, to avoid proliferation of
  5429. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5430. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5431. is kept so that you can work with several indirect buffers at the same time.
  5432. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5433. requests that a new frame be made for the new buffer, so that the dedicated
  5434. frame is not changed."
  5435. (interactive "P")
  5436. (let ((cbuf (current-buffer))
  5437. (cwin (selected-window))
  5438. (pos (point))
  5439. beg end level heading ibuf)
  5440. (save-excursion
  5441. (org-back-to-heading t)
  5442. (when (numberp arg)
  5443. (setq level (org-outline-level))
  5444. (if (< arg 0) (setq arg (+ level arg)))
  5445. (while (> (setq level (org-outline-level)) arg)
  5446. (outline-up-heading 1 t)))
  5447. (setq beg (point)
  5448. heading (org-get-heading))
  5449. (org-end-of-subtree t) (setq end (point)))
  5450. (if (and (buffer-live-p org-last-indirect-buffer)
  5451. (not (eq org-indirect-buffer-display 'new-frame))
  5452. (not arg))
  5453. (kill-buffer org-last-indirect-buffer))
  5454. (setq ibuf (org-get-indirect-buffer cbuf)
  5455. org-last-indirect-buffer ibuf)
  5456. (cond
  5457. ((or (eq org-indirect-buffer-display 'new-frame)
  5458. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5459. (select-frame (make-frame))
  5460. (delete-other-windows)
  5461. (switch-to-buffer ibuf)
  5462. (org-set-frame-title heading))
  5463. ((eq org-indirect-buffer-display 'dedicated-frame)
  5464. (raise-frame
  5465. (select-frame (or (and org-indirect-dedicated-frame
  5466. (frame-live-p org-indirect-dedicated-frame)
  5467. org-indirect-dedicated-frame)
  5468. (setq org-indirect-dedicated-frame (make-frame)))))
  5469. (delete-other-windows)
  5470. (switch-to-buffer ibuf)
  5471. (org-set-frame-title (concat "Indirect: " heading)))
  5472. ((eq org-indirect-buffer-display 'current-window)
  5473. (switch-to-buffer ibuf))
  5474. ((eq org-indirect-buffer-display 'other-window)
  5475. (pop-to-buffer ibuf))
  5476. (t (error "Invalid value.")))
  5477. (if (featurep 'xemacs)
  5478. (save-excursion (org-mode) (turn-on-font-lock)))
  5479. (narrow-to-region beg end)
  5480. (show-all)
  5481. (goto-char pos)
  5482. (and (window-live-p cwin) (select-window cwin))))
  5483. (defun org-get-indirect-buffer (&optional buffer)
  5484. (setq buffer (or buffer (current-buffer)))
  5485. (let ((n 1) (base (buffer-name buffer)) bname)
  5486. (while (buffer-live-p
  5487. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5488. (setq n (1+ n)))
  5489. (condition-case nil
  5490. (make-indirect-buffer buffer bname 'clone)
  5491. (error (make-indirect-buffer buffer bname)))))
  5492. (defun org-set-frame-title (title)
  5493. "Set the title of the current frame to the string TITLE."
  5494. ;; FIXME: how to name a single frame in XEmacs???
  5495. (unless (featurep 'xemacs)
  5496. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5497. ;;;; Structure editing
  5498. ;;; Inserting headlines
  5499. (defun org-insert-heading (&optional force-heading)
  5500. "Insert a new heading or item with same depth at point.
  5501. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5502. If point is at the beginning of a headline, insert a sibling before the
  5503. current headline. If point is not at the beginning, do not split the line,
  5504. but create the new hedline after the current line."
  5505. (interactive "P")
  5506. (if (= (buffer-size) 0)
  5507. (insert "\n* ")
  5508. (when (or force-heading (not (org-insert-item)))
  5509. (let* ((head (save-excursion
  5510. (condition-case nil
  5511. (progn
  5512. (org-back-to-heading)
  5513. (match-string 0))
  5514. (error "*"))))
  5515. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5516. pos)
  5517. (cond
  5518. ((and (org-on-heading-p) (bolp)
  5519. (or (bobp)
  5520. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5521. ;; insert before the current line
  5522. (open-line (if blank 2 1)))
  5523. ((and (bolp)
  5524. (or (bobp)
  5525. (save-excursion
  5526. (backward-char 1) (not (org-invisible-p)))))
  5527. ;; insert right here
  5528. nil)
  5529. (t
  5530. ;; in the middle of the line
  5531. (org-show-entry)
  5532. (end-of-line 1)
  5533. (newline (if blank 2 1))))
  5534. (insert head) (just-one-space)
  5535. (setq pos (point))
  5536. (end-of-line 1)
  5537. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5538. (run-hooks 'org-insert-heading-hook)))))
  5539. (defun org-insert-heading-after-current ()
  5540. "Insert a new heading with same level as current, after current subtree."
  5541. (interactive)
  5542. (org-back-to-heading)
  5543. (org-insert-heading)
  5544. (org-move-subtree-down)
  5545. (end-of-line 1))
  5546. (defun org-insert-todo-heading (arg)
  5547. "Insert a new heading with the same level and TODO state as current heading.
  5548. If the heading has no TODO state, or if the state is DONE, use the first
  5549. state (TODO by default). Also with prefix arg, force first state."
  5550. (interactive "P")
  5551. (when (not (org-insert-item 'checkbox))
  5552. (org-insert-heading)
  5553. (save-excursion
  5554. (org-back-to-heading)
  5555. (outline-previous-heading)
  5556. (looking-at org-todo-line-regexp))
  5557. (if (or arg
  5558. (not (match-beginning 2))
  5559. (member (match-string 2) org-done-keywords))
  5560. (insert (car org-todo-keywords-1) " ")
  5561. (insert (match-string 2) " "))))
  5562. (defun org-insert-subheading (arg)
  5563. "Insert a new subheading and demote it.
  5564. Works for outline headings and for plain lists alike."
  5565. (interactive "P")
  5566. (org-insert-heading arg)
  5567. (cond
  5568. ((org-on-heading-p) (org-do-demote))
  5569. ((org-at-item-p) (org-indent-item 1))))
  5570. (defun org-insert-todo-subheading (arg)
  5571. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5572. Works for outline headings and for plain lists alike."
  5573. (interactive "P")
  5574. (org-insert-todo-heading arg)
  5575. (cond
  5576. ((org-on-heading-p) (org-do-demote))
  5577. ((org-at-item-p) (org-indent-item 1))))
  5578. ;;; Promotion and Demotion
  5579. (defun org-promote-subtree ()
  5580. "Promote the entire subtree.
  5581. See also `org-promote'."
  5582. (interactive)
  5583. (save-excursion
  5584. (org-map-tree 'org-promote))
  5585. (org-fix-position-after-promote))
  5586. (defun org-demote-subtree ()
  5587. "Demote the entire subtree. See `org-demote'.
  5588. See also `org-promote'."
  5589. (interactive)
  5590. (save-excursion
  5591. (org-map-tree 'org-demote))
  5592. (org-fix-position-after-promote))
  5593. (defun org-do-promote ()
  5594. "Promote the current heading higher up the tree.
  5595. If the region is active in `transient-mark-mode', promote all headings
  5596. in the region."
  5597. (interactive)
  5598. (save-excursion
  5599. (if (org-region-active-p)
  5600. (org-map-region 'org-promote (region-beginning) (region-end))
  5601. (org-promote)))
  5602. (org-fix-position-after-promote))
  5603. (defun org-do-demote ()
  5604. "Demote the current heading lower down the tree.
  5605. If the region is active in `transient-mark-mode', demote all headings
  5606. in the region."
  5607. (interactive)
  5608. (save-excursion
  5609. (if (org-region-active-p)
  5610. (org-map-region 'org-demote (region-beginning) (region-end))
  5611. (org-demote)))
  5612. (org-fix-position-after-promote))
  5613. (defun org-fix-position-after-promote ()
  5614. "Make sure that after pro/demotion cursor position is right."
  5615. (let ((pos (point)))
  5616. (when (save-excursion
  5617. (beginning-of-line 1)
  5618. (looking-at org-todo-line-regexp)
  5619. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5620. (cond ((eobp) (insert " "))
  5621. ((eolp) (insert " "))
  5622. ((equal (char-after) ?\ ) (forward-char 1))))))
  5623. (defun org-reduced-level (l)
  5624. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5625. (defun org-get-legal-level (level &optional change)
  5626. "Rectify a level change under the influence of `org-odd-levels-only'
  5627. LEVEL is a current level, CHANGE is by how much the level should be
  5628. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5629. even level numbers will become the next higher odd number."
  5630. (if org-odd-levels-only
  5631. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5632. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5633. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5634. (max 1 (+ level change))))
  5635. (defun org-promote ()
  5636. "Promote the current heading higher up the tree.
  5637. If the region is active in `transient-mark-mode', promote all headings
  5638. in the region."
  5639. (org-back-to-heading t)
  5640. (let* ((level (save-match-data (funcall outline-level)))
  5641. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5642. (diff (abs (- level (length up-head) -1))))
  5643. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5644. (replace-match up-head nil t)
  5645. ;; Fixup tag positioning
  5646. (and org-auto-align-tags (org-set-tags nil t))
  5647. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5648. (defun org-demote ()
  5649. "Demote the current heading lower down the tree.
  5650. If the region is active in `transient-mark-mode', demote all headings
  5651. in the region."
  5652. (org-back-to-heading t)
  5653. (let* ((level (save-match-data (funcall outline-level)))
  5654. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5655. (diff (abs (- level (length down-head) -1))))
  5656. (replace-match down-head nil t)
  5657. ;; Fixup tag positioning
  5658. (and org-auto-align-tags (org-set-tags nil t))
  5659. (if org-adapt-indentation (org-fixup-indentation diff))))
  5660. (defun org-map-tree (fun)
  5661. "Call FUN for every heading underneath the current one."
  5662. (org-back-to-heading)
  5663. (let ((level (funcall outline-level)))
  5664. (save-excursion
  5665. (funcall fun)
  5666. (while (and (progn
  5667. (outline-next-heading)
  5668. (> (funcall outline-level) level))
  5669. (not (eobp)))
  5670. (funcall fun)))))
  5671. (defun org-map-region (fun beg end)
  5672. "Call FUN for every heading between BEG and END."
  5673. (let ((org-ignore-region t))
  5674. (save-excursion
  5675. (setq end (copy-marker end))
  5676. (goto-char beg)
  5677. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5678. (< (point) end))
  5679. (funcall fun))
  5680. (while (and (progn
  5681. (outline-next-heading)
  5682. (< (point) end))
  5683. (not (eobp)))
  5684. (funcall fun)))))
  5685. (defun org-fixup-indentation (diff)
  5686. "Change the indentation in the current entry by DIFF
  5687. However, if any line in the current entry has no indentation, or if it
  5688. would end up with no indentation after the change, nothing at all is done."
  5689. (save-excursion
  5690. (let ((end (save-excursion (outline-next-heading)
  5691. (point-marker)))
  5692. (prohibit (if (> diff 0)
  5693. "^\\S-"
  5694. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5695. col)
  5696. (unless (save-excursion (end-of-line 1)
  5697. (re-search-forward prohibit end t))
  5698. (while (and (< (point) end)
  5699. (re-search-forward "^[ \t]+" end t))
  5700. (goto-char (match-end 0))
  5701. (setq col (current-column))
  5702. (if (< diff 0) (replace-match ""))
  5703. (indent-to (+ diff col))))
  5704. (move-marker end nil))))
  5705. (defun org-convert-to-odd-levels ()
  5706. "Convert an org-mode file with all levels allowed to one with odd levels.
  5707. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5708. level 5 etc."
  5709. (interactive)
  5710. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5711. (let ((org-odd-levels-only nil) n)
  5712. (save-excursion
  5713. (goto-char (point-min))
  5714. (while (re-search-forward "^\\*\\*+ " nil t)
  5715. (setq n (- (length (match-string 0)) 2))
  5716. (while (>= (setq n (1- n)) 0)
  5717. (org-demote))
  5718. (end-of-line 1))))))
  5719. (defun org-convert-to-oddeven-levels ()
  5720. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5721. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5722. section with an even level, conversion would destroy the structure of the file. An error
  5723. is signaled in this case."
  5724. (interactive)
  5725. (goto-char (point-min))
  5726. ;; First check if there are no even levels
  5727. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5728. (org-show-context t)
  5729. (error "Not all levels are odd in this file. Conversion not possible."))
  5730. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5731. (let ((org-odd-levels-only nil) n)
  5732. (save-excursion
  5733. (goto-char (point-min))
  5734. (while (re-search-forward "^\\*\\*+ " nil t)
  5735. (setq n (/ (1- (length (match-string 0))) 2))
  5736. (while (>= (setq n (1- n)) 0)
  5737. (org-promote))
  5738. (end-of-line 1))))))
  5739. (defun org-tr-level (n)
  5740. "Make N odd if required."
  5741. (if org-odd-levels-only (1+ (/ n 2)) n))
  5742. ;;; Vertical tree motion, cutting and pasting of subtrees
  5743. (defun org-move-subtree-up (&optional arg)
  5744. "Move the current subtree up past ARG headlines of the same level."
  5745. (interactive "p")
  5746. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5747. (defun org-move-subtree-down (&optional arg)
  5748. "Move the current subtree down past ARG headlines of the same level."
  5749. (interactive "p")
  5750. (setq arg (prefix-numeric-value arg))
  5751. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5752. 'outline-get-last-sibling))
  5753. (ins-point (make-marker))
  5754. (cnt (abs arg))
  5755. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5756. ;; Select the tree
  5757. (org-back-to-heading)
  5758. (setq beg0 (point))
  5759. (save-excursion
  5760. (setq ne-beg (org-back-over-empty-lines))
  5761. (setq beg (point)))
  5762. (save-match-data
  5763. (save-excursion (outline-end-of-heading)
  5764. (setq folded (org-invisible-p)))
  5765. (outline-end-of-subtree))
  5766. (outline-next-heading)
  5767. (setq ne-end (org-back-over-empty-lines))
  5768. (setq end (point))
  5769. (goto-char beg0)
  5770. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5771. ;; include less whitespace
  5772. (save-excursion
  5773. (goto-char beg)
  5774. (forward-line (- ne-beg ne-end))
  5775. (setq beg (point))))
  5776. ;; Find insertion point, with error handling
  5777. (while (> cnt 0)
  5778. (or (and (funcall movfunc) (looking-at outline-regexp))
  5779. (progn (goto-char beg0)
  5780. (error "Cannot move past superior level or buffer limit")))
  5781. (setq cnt (1- cnt)))
  5782. (if (> arg 0)
  5783. ;; Moving forward - still need to move over subtree
  5784. (progn (org-end-of-subtree t t)
  5785. (save-excursion
  5786. (org-back-over-empty-lines)
  5787. (or (bolp) (newline)))))
  5788. (setq ne-ins (org-back-over-empty-lines))
  5789. (move-marker ins-point (point))
  5790. (setq txt (buffer-substring beg end))
  5791. (delete-region beg end)
  5792. (outline-flag-region (1- beg) beg nil)
  5793. (outline-flag-region (1- (point)) (point) nil)
  5794. (insert txt)
  5795. (or (bolp) (insert "\n"))
  5796. (setq ins-end (point))
  5797. (goto-char ins-point)
  5798. (org-skip-whitespace)
  5799. (when (and (< arg 0)
  5800. (org-first-sibling-p)
  5801. (> ne-ins ne-beg))
  5802. ;; Move whitespace back to beginning
  5803. (save-excursion
  5804. (goto-char ins-end)
  5805. (let ((kill-whole-line t))
  5806. (kill-line (- ne-ins ne-beg)) (point)))
  5807. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5808. (move-marker ins-point nil)
  5809. (org-compact-display-after-subtree-move)
  5810. (unless folded
  5811. (org-show-entry)
  5812. (show-children)
  5813. (org-cycle-hide-drawers 'children))))
  5814. (defvar org-subtree-clip ""
  5815. "Clipboard for cut and paste of subtrees.
  5816. This is actually only a copy of the kill, because we use the normal kill
  5817. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5818. (defvar org-subtree-clip-folded nil
  5819. "Was the last copied subtree folded?
  5820. This is used to fold the tree back after pasting.")
  5821. (defun org-cut-subtree (&optional n)
  5822. "Cut the current subtree into the clipboard.
  5823. With prefix arg N, cut this many sequential subtrees.
  5824. This is a short-hand for marking the subtree and then cutting it."
  5825. (interactive "p")
  5826. (org-copy-subtree n 'cut))
  5827. (defun org-copy-subtree (&optional n cut)
  5828. "Cut the current subtree into the clipboard.
  5829. With prefix arg N, cut this many sequential subtrees.
  5830. This is a short-hand for marking the subtree and then copying it.
  5831. If CUT is non-nil, actually cut the subtree."
  5832. (interactive "p")
  5833. (let (beg end folded (beg0 (point)))
  5834. (if (interactive-p)
  5835. (org-back-to-heading nil) ; take what looks like a subtree
  5836. (org-back-to-heading t)) ; take what is really there
  5837. (org-back-over-empty-lines)
  5838. (setq beg (point))
  5839. (skip-chars-forward " \t\r\n")
  5840. (save-match-data
  5841. (save-excursion (outline-end-of-heading)
  5842. (setq folded (org-invisible-p)))
  5843. (condition-case nil
  5844. (outline-forward-same-level (1- n))
  5845. (error nil))
  5846. (org-end-of-subtree t t))
  5847. (org-back-over-empty-lines)
  5848. (setq end (point))
  5849. (goto-char beg0)
  5850. (when (> end beg)
  5851. (setq org-subtree-clip-folded folded)
  5852. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5853. (setq org-subtree-clip (current-kill 0))
  5854. (message "%s: Subtree(s) with %d characters"
  5855. (if cut "Cut" "Copied")
  5856. (length org-subtree-clip)))))
  5857. (defun org-paste-subtree (&optional level tree)
  5858. "Paste the clipboard as a subtree, with modification of headline level.
  5859. The entire subtree is promoted or demoted in order to match a new headline
  5860. level. By default, the new level is derived from the visible headings
  5861. before and after the insertion point, and taken to be the inferior headline
  5862. level of the two. So if the previous visible heading is level 3 and the
  5863. next is level 4 (or vice versa), level 4 will be used for insertion.
  5864. This makes sure that the subtree remains an independent subtree and does
  5865. not swallow low level entries.
  5866. You can also force a different level, either by using a numeric prefix
  5867. argument, or by inserting the heading marker by hand. For example, if the
  5868. cursor is after \"*****\", then the tree will be shifted to level 5.
  5869. If you want to insert the tree as is, just use \\[yank].
  5870. If optional TREE is given, use this text instead of the kill ring."
  5871. (interactive "P")
  5872. (unless (org-kill-is-subtree-p tree)
  5873. (error "%s"
  5874. (substitute-command-keys
  5875. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5876. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5877. (^re (concat "^\\(" outline-regexp "\\)"))
  5878. (re (concat "\\(" outline-regexp "\\)"))
  5879. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5880. (old-level (if (string-match ^re txt)
  5881. (- (match-end 0) (match-beginning 0) 1)
  5882. -1))
  5883. (force-level (cond (level (prefix-numeric-value level))
  5884. ((string-match
  5885. ^re_ (buffer-substring (point-at-bol) (point)))
  5886. (- (match-end 1) (match-beginning 1)))
  5887. (t nil)))
  5888. (previous-level (save-excursion
  5889. (condition-case nil
  5890. (progn
  5891. (outline-previous-visible-heading 1)
  5892. (if (looking-at re)
  5893. (- (match-end 0) (match-beginning 0) 1)
  5894. 1))
  5895. (error 1))))
  5896. (next-level (save-excursion
  5897. (condition-case nil
  5898. (progn
  5899. (or (looking-at outline-regexp)
  5900. (outline-next-visible-heading 1))
  5901. (if (looking-at re)
  5902. (- (match-end 0) (match-beginning 0) 1)
  5903. 1))
  5904. (error 1))))
  5905. (new-level (or force-level (max previous-level next-level)))
  5906. (shift (if (or (= old-level -1)
  5907. (= new-level -1)
  5908. (= old-level new-level))
  5909. 0
  5910. (- new-level old-level)))
  5911. (delta (if (> shift 0) -1 1))
  5912. (func (if (> shift 0) 'org-demote 'org-promote))
  5913. (org-odd-levels-only nil)
  5914. beg end)
  5915. ;; Remove the forced level indicator
  5916. (if force-level
  5917. (delete-region (point-at-bol) (point)))
  5918. ;; Paste
  5919. (beginning-of-line 1)
  5920. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5921. (setq beg (point))
  5922. (insert-before-markers txt) ;; FIXME: correct fix????
  5923. (unless (string-match "\n\\'" txt) (insert "\n"))
  5924. (setq end (point))
  5925. (goto-char beg)
  5926. (skip-chars-forward " \t\n\r")
  5927. (setq beg (point))
  5928. ;; Shift if necessary
  5929. (unless (= shift 0)
  5930. (save-restriction
  5931. (narrow-to-region beg end)
  5932. (while (not (= shift 0))
  5933. (org-map-region func (point-min) (point-max))
  5934. (setq shift (+ delta shift)))
  5935. (goto-char (point-min))))
  5936. (when (interactive-p)
  5937. (message "Clipboard pasted as level %d subtree" new-level))
  5938. (if (and kill-ring
  5939. (eq org-subtree-clip (current-kill 0))
  5940. org-subtree-clip-folded)
  5941. ;; The tree was folded before it was killed/copied
  5942. (hide-subtree))))
  5943. (defun org-kill-is-subtree-p (&optional txt)
  5944. "Check if the current kill is an outline subtree, or a set of trees.
  5945. Returns nil if kill does not start with a headline, or if the first
  5946. headline level is not the largest headline level in the tree.
  5947. So this will actually accept several entries of equal levels as well,
  5948. which is OK for `org-paste-subtree'.
  5949. If optional TXT is given, check this string instead of the current kill."
  5950. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5951. (start-level (and kill
  5952. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5953. org-outline-regexp "\\)")
  5954. kill)
  5955. (- (match-end 2) (match-beginning 2) 1)))
  5956. (re (concat "^" org-outline-regexp))
  5957. (start (1+ (match-beginning 2))))
  5958. (if (not start-level)
  5959. (progn
  5960. nil) ;; does not even start with a heading
  5961. (catch 'exit
  5962. (while (setq start (string-match re kill (1+ start)))
  5963. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5964. (throw 'exit nil)))
  5965. t))))
  5966. (defun org-narrow-to-subtree ()
  5967. "Narrow buffer to the current subtree."
  5968. (interactive)
  5969. (save-excursion
  5970. (save-match-data
  5971. (narrow-to-region
  5972. (progn (org-back-to-heading) (point))
  5973. (progn (org-end-of-subtree t t) (point))))))
  5974. ;;; Outline Sorting
  5975. (defun org-sort (with-case)
  5976. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5977. Optional argument WITH-CASE means sort case-sensitively."
  5978. (interactive "P")
  5979. (if (org-at-table-p)
  5980. (org-call-with-arg 'org-table-sort-lines with-case)
  5981. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5982. (defvar org-priority-regexp) ; defined later in the file
  5983. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5984. "Sort entries on a certain level of an outline tree.
  5985. If there is an active region, the entries in the region are sorted.
  5986. Else, if the cursor is before the first entry, sort the top-level items.
  5987. Else, the children of the entry at point are sorted.
  5988. Sorting can be alphabetically, numerically, and by date/time as given by
  5989. the first time stamp in the entry. The command prompts for the sorting
  5990. type unless it has been given to the function through the SORTING-TYPE
  5991. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5992. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5993. called with point at the beginning of the record. It must return either
  5994. a string or a number that should serve as the sorting key for that record.
  5995. Comparing entries ignores case by default. However, with an optional argument
  5996. WITH-CASE, the sorting considers case as well."
  5997. (interactive "P")
  5998. (let ((case-func (if with-case 'identity 'downcase))
  5999. start beg end stars re re2
  6000. txt what tmp plain-list-p)
  6001. ;; Find beginning and end of region to sort
  6002. (cond
  6003. ((org-region-active-p)
  6004. ;; we will sort the region
  6005. (setq end (region-end)
  6006. what "region")
  6007. (goto-char (region-beginning))
  6008. (if (not (org-on-heading-p)) (outline-next-heading))
  6009. (setq start (point)))
  6010. ((org-at-item-p)
  6011. ;; we will sort this plain list
  6012. (org-beginning-of-item-list) (setq start (point))
  6013. (org-end-of-item-list) (setq end (point))
  6014. (goto-char start)
  6015. (setq plain-list-p t
  6016. what "plain list"))
  6017. ((or (org-on-heading-p)
  6018. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6019. ;; we will sort the children of the current headline
  6020. (org-back-to-heading)
  6021. (setq start (point)
  6022. end (progn (org-end-of-subtree t t)
  6023. (org-back-over-empty-lines)
  6024. (point))
  6025. what "children")
  6026. (goto-char start)
  6027. (show-subtree)
  6028. (outline-next-heading))
  6029. (t
  6030. ;; we will sort the top-level entries in this file
  6031. (goto-char (point-min))
  6032. (or (org-on-heading-p) (outline-next-heading))
  6033. (setq start (point) end (point-max) what "top-level")
  6034. (goto-char start)
  6035. (show-all)))
  6036. (setq beg (point))
  6037. (if (>= beg end) (error "Nothing to sort"))
  6038. (unless plain-list-p
  6039. (looking-at "\\(\\*+\\)")
  6040. (setq stars (match-string 1)
  6041. re (concat "^" (regexp-quote stars) " +")
  6042. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6043. txt (buffer-substring beg end))
  6044. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6045. (if (and (not (equal stars "*")) (string-match re2 txt))
  6046. (error "Region to sort contains a level above the first entry")))
  6047. (unless sorting-type
  6048. (message
  6049. (if plain-list-p
  6050. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6051. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6052. what)
  6053. (setq sorting-type (read-char-exclusive))
  6054. (and (= (downcase sorting-type) ?f)
  6055. (setq getkey-func
  6056. (completing-read "Sort using function: "
  6057. obarray 'fboundp t nil nil))
  6058. (setq getkey-func (intern getkey-func)))
  6059. (and (= (downcase sorting-type) ?r)
  6060. (setq property
  6061. (completing-read "Property: "
  6062. (mapcar 'list (org-buffer-property-keys t))
  6063. nil t))))
  6064. (message "Sorting entries...")
  6065. (save-restriction
  6066. (narrow-to-region start end)
  6067. (let ((dcst (downcase sorting-type))
  6068. (now (current-time)))
  6069. (sort-subr
  6070. (/= dcst sorting-type)
  6071. ;; This function moves to the beginning character of the "record" to
  6072. ;; be sorted.
  6073. (if plain-list-p
  6074. (lambda nil
  6075. (if (org-at-item-p) t (goto-char (point-max))))
  6076. (lambda nil
  6077. (if (re-search-forward re nil t)
  6078. (goto-char (match-beginning 0))
  6079. (goto-char (point-max)))))
  6080. ;; This function moves to the last character of the "record" being
  6081. ;; sorted.
  6082. (if plain-list-p
  6083. 'org-end-of-item
  6084. (lambda nil
  6085. (save-match-data
  6086. (condition-case nil
  6087. (outline-forward-same-level 1)
  6088. (error
  6089. (goto-char (point-max)))))))
  6090. ;; This function returns the value that gets sorted against.
  6091. (if plain-list-p
  6092. (lambda nil
  6093. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6094. (cond
  6095. ((= dcst ?n)
  6096. (string-to-number (buffer-substring (match-end 0)
  6097. (point-at-eol))))
  6098. ((= dcst ?a)
  6099. (buffer-substring (match-end 0) (point-at-eol)))
  6100. ((= dcst ?t)
  6101. (if (re-search-forward org-ts-regexp
  6102. (point-at-eol) t)
  6103. (org-time-string-to-time (match-string 0))
  6104. now))
  6105. ((= dcst ?f)
  6106. (if getkey-func
  6107. (progn
  6108. (setq tmp (funcall getkey-func))
  6109. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6110. tmp)
  6111. (error "Invalid key function `%s'" getkey-func)))
  6112. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6113. (lambda nil
  6114. (cond
  6115. ((= dcst ?n)
  6116. (if (looking-at outline-regexp)
  6117. (string-to-number (buffer-substring (match-end 0)
  6118. (point-at-eol)))
  6119. nil))
  6120. ((= dcst ?a)
  6121. (funcall case-func (buffer-substring (point-at-bol)
  6122. (point-at-eol))))
  6123. ((= dcst ?t)
  6124. (if (re-search-forward org-ts-regexp
  6125. (save-excursion
  6126. (forward-line 2)
  6127. (point)) t)
  6128. (org-time-string-to-time (match-string 0))
  6129. now))
  6130. ((= dcst ?p)
  6131. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6132. (string-to-char (match-string 2))
  6133. org-default-priority))
  6134. ((= dcst ?r)
  6135. (or (org-entry-get nil property) ""))
  6136. ((= dcst ?f)
  6137. (if getkey-func
  6138. (progn
  6139. (setq tmp (funcall getkey-func))
  6140. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6141. tmp)
  6142. (error "Invalid key function `%s'" getkey-func)))
  6143. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6144. nil
  6145. (cond
  6146. ((= dcst ?a) 'string<)
  6147. ((= dcst ?t) 'time-less-p)
  6148. (t nil)))))
  6149. (message "Sorting entries...done")))
  6150. (defun org-do-sort (table what &optional with-case sorting-type)
  6151. "Sort TABLE of WHAT according to SORTING-TYPE.
  6152. The user will be prompted for the SORTING-TYPE if the call to this
  6153. function does not specify it. WHAT is only for the prompt, to indicate
  6154. what is being sorted. The sorting key will be extracted from
  6155. the car of the elements of the table.
  6156. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6157. (unless sorting-type
  6158. (message
  6159. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6160. what)
  6161. (setq sorting-type (read-char-exclusive)))
  6162. (let ((dcst (downcase sorting-type))
  6163. extractfun comparefun)
  6164. ;; Define the appropriate functions
  6165. (cond
  6166. ((= dcst ?n)
  6167. (setq extractfun 'string-to-number
  6168. comparefun (if (= dcst sorting-type) '< '>)))
  6169. ((= dcst ?a)
  6170. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6171. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6172. comparefun (if (= dcst sorting-type)
  6173. 'string<
  6174. (lambda (a b) (and (not (string< a b))
  6175. (not (string= a b)))))))
  6176. ((= dcst ?t)
  6177. (setq extractfun
  6178. (lambda (x)
  6179. (if (string-match org-ts-regexp x)
  6180. (time-to-seconds
  6181. (org-time-string-to-time (match-string 0 x)))
  6182. 0))
  6183. comparefun (if (= dcst sorting-type) '< '>)))
  6184. (t (error "Invalid sorting type `%c'" sorting-type)))
  6185. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6186. table)
  6187. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6188. ;;;; Plain list items, including checkboxes
  6189. ;;; Plain list items
  6190. (defun org-at-item-p ()
  6191. "Is point in a line starting a hand-formatted item?"
  6192. (let ((llt org-plain-list-ordered-item-terminator))
  6193. (save-excursion
  6194. (goto-char (point-at-bol))
  6195. (looking-at
  6196. (cond
  6197. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6198. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6199. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6200. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6201. (defun org-in-item-p ()
  6202. "It the cursor inside a plain list item.
  6203. Does not have to be the first line."
  6204. (save-excursion
  6205. (condition-case nil
  6206. (progn
  6207. (org-beginning-of-item)
  6208. (org-at-item-p)
  6209. t)
  6210. (error nil))))
  6211. (defun org-insert-item (&optional checkbox)
  6212. "Insert a new item at the current level.
  6213. Return t when things worked, nil when we are not in an item."
  6214. (when (save-excursion
  6215. (condition-case nil
  6216. (progn
  6217. (org-beginning-of-item)
  6218. (org-at-item-p)
  6219. (if (org-invisible-p) (error "Invisible item"))
  6220. t)
  6221. (error nil)))
  6222. (let* ((bul (match-string 0))
  6223. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6224. (match-end 0)))
  6225. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6226. pos)
  6227. (cond
  6228. ((and (org-at-item-p) (<= (point) eow))
  6229. ;; before the bullet
  6230. (beginning-of-line 1)
  6231. (open-line (if blank 2 1)))
  6232. ((<= (point) eow)
  6233. (beginning-of-line 1))
  6234. (t (newline (if blank 2 1))))
  6235. (insert bul (if checkbox "[ ]" ""))
  6236. (just-one-space)
  6237. (setq pos (point))
  6238. (end-of-line 1)
  6239. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6240. (org-maybe-renumber-ordered-list)
  6241. (and checkbox (org-update-checkbox-count-maybe))
  6242. t))
  6243. ;;; Checkboxes
  6244. (defun org-at-item-checkbox-p ()
  6245. "Is point at a line starting a plain-list item with a checklet?"
  6246. (and (org-at-item-p)
  6247. (save-excursion
  6248. (goto-char (match-end 0))
  6249. (skip-chars-forward " \t")
  6250. (looking-at "\\[[- X]\\]"))))
  6251. (defun org-toggle-checkbox (&optional arg)
  6252. "Toggle the checkbox in the current line."
  6253. (interactive "P")
  6254. (catch 'exit
  6255. (let (beg end status (firstnew 'unknown))
  6256. (cond
  6257. ((org-region-active-p)
  6258. (setq beg (region-beginning) end (region-end)))
  6259. ((org-on-heading-p)
  6260. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6261. ((org-at-item-checkbox-p)
  6262. (let ((pos (point)))
  6263. (replace-match
  6264. (cond (arg "[-]")
  6265. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6266. (t "[ ]"))
  6267. t t)
  6268. (goto-char pos))
  6269. (throw 'exit t))
  6270. (t (error "Not at a checkbox or heading, and no active region")))
  6271. (save-excursion
  6272. (goto-char beg)
  6273. (while (< (point) end)
  6274. (when (org-at-item-checkbox-p)
  6275. (setq status (equal (match-string 0) "[X]"))
  6276. (when (eq firstnew 'unknown)
  6277. (setq firstnew (not status)))
  6278. (replace-match
  6279. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6280. (beginning-of-line 2)))))
  6281. (org-update-checkbox-count-maybe))
  6282. (defun org-update-checkbox-count-maybe ()
  6283. "Update checkbox statistics unless turned off by user."
  6284. (when org-provide-checkbox-statistics
  6285. (org-update-checkbox-count)))
  6286. (defun org-update-checkbox-count (&optional all)
  6287. "Update the checkbox statistics in the current section.
  6288. This will find all statistic cookies like [57%] and [6/12] and update them
  6289. with the current numbers. With optional prefix argument ALL, do this for
  6290. the whole buffer."
  6291. (interactive "P")
  6292. (save-excursion
  6293. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6294. (beg (condition-case nil
  6295. (progn (outline-back-to-heading) (point))
  6296. (error (point-min))))
  6297. (end (move-marker (make-marker)
  6298. (progn (outline-next-heading) (point))))
  6299. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6300. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6301. (re-find (concat re "\\|" re-box))
  6302. beg-cookie end-cookie is-percent c-on c-off lim
  6303. eline curr-ind next-ind continue-from startsearch
  6304. (cstat 0)
  6305. )
  6306. (when all
  6307. (goto-char (point-min))
  6308. (outline-next-heading)
  6309. (setq beg (point) end (point-max)))
  6310. (goto-char end)
  6311. ;; find each statistic cookie
  6312. (while (re-search-backward re-find beg t)
  6313. (setq beg-cookie (match-beginning 1)
  6314. end-cookie (match-end 1)
  6315. cstat (+ cstat (if end-cookie 1 0))
  6316. startsearch (point-at-eol)
  6317. continue-from (point-at-bol)
  6318. is-percent (match-beginning 2)
  6319. lim (cond
  6320. ((org-on-heading-p) (outline-next-heading) (point))
  6321. ((org-at-item-p) (org-end-of-item) (point))
  6322. (t nil))
  6323. c-on 0
  6324. c-off 0)
  6325. (when lim
  6326. ;; find first checkbox for this cookie and gather
  6327. ;; statistics from all that are at this indentation level
  6328. (goto-char startsearch)
  6329. (if (re-search-forward re-box lim t)
  6330. (progn
  6331. (org-beginning-of-item)
  6332. (setq curr-ind (org-get-indentation))
  6333. (setq next-ind curr-ind)
  6334. (while (= curr-ind next-ind)
  6335. (save-excursion (end-of-line) (setq eline (point)))
  6336. (if (re-search-forward re-box eline t)
  6337. (if (member (match-string 2) '("[ ]" "[-]"))
  6338. (setq c-off (1+ c-off))
  6339. (setq c-on (1+ c-on))
  6340. )
  6341. )
  6342. (org-end-of-item)
  6343. (setq next-ind (org-get-indentation))
  6344. )))
  6345. (goto-char continue-from)
  6346. ;; update cookie
  6347. (when end-cookie
  6348. (delete-region beg-cookie end-cookie)
  6349. (goto-char beg-cookie)
  6350. (insert
  6351. (if is-percent
  6352. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6353. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6354. ;; update items checkbox if it has one
  6355. (when (org-at-item-p)
  6356. (org-beginning-of-item)
  6357. (when (and (> (+ c-on c-off) 0)
  6358. (re-search-forward re-box (point-at-eol) t))
  6359. (setq beg-cookie (match-beginning 2)
  6360. end-cookie (match-end 2))
  6361. (delete-region beg-cookie end-cookie)
  6362. (goto-char beg-cookie)
  6363. (cond ((= c-off 0) (insert "[X]"))
  6364. ((= c-on 0) (insert "[ ]"))
  6365. (t (insert "[-]")))
  6366. )))
  6367. (goto-char continue-from))
  6368. (when (interactive-p)
  6369. (message "Checkbox satistics updated %s (%d places)"
  6370. (if all "in entire file" "in current outline entry") cstat)))))
  6371. (defun org-get-checkbox-statistics-face ()
  6372. "Select the face for checkbox statistics.
  6373. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6374. it will be `org-todo'."
  6375. (if (match-end 1)
  6376. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6377. (if (and (> (match-end 2) (match-beginning 2))
  6378. (equal (match-string 2) (match-string 3)))
  6379. 'org-done
  6380. 'org-todo)))
  6381. (defun org-get-indentation (&optional line)
  6382. "Get the indentation of the current line, interpreting tabs.
  6383. When LINE is given, assume it represents a line and compute its indentation."
  6384. (if line
  6385. (if (string-match "^ *" (org-remove-tabs line))
  6386. (match-end 0))
  6387. (save-excursion
  6388. (beginning-of-line 1)
  6389. (skip-chars-forward " \t")
  6390. (current-column))))
  6391. (defun org-remove-tabs (s &optional width)
  6392. "Replace tabulators in S with spaces.
  6393. Assumes that s is a single line, starting in column 0."
  6394. (setq width (or width tab-width))
  6395. (while (string-match "\t" s)
  6396. (setq s (replace-match
  6397. (make-string
  6398. (- (* width (/ (+ (match-beginning 0) width) width))
  6399. (match-beginning 0)) ?\ )
  6400. t t s)))
  6401. s)
  6402. (defun org-fix-indentation (line ind)
  6403. "Fix indentation in LINE.
  6404. IND is a cons cell with target and minimum indentation.
  6405. If the current indenation in LINE is smaller than the minimum,
  6406. leave it alone. If it is larger than ind, set it to the target."
  6407. (let* ((l (org-remove-tabs line))
  6408. (i (org-get-indentation l))
  6409. (i1 (car ind)) (i2 (cdr ind)))
  6410. (if (>= i i2) (setq l (substring line i2)))
  6411. (if (> i1 0)
  6412. (concat (make-string i1 ?\ ) l)
  6413. l)))
  6414. (defcustom org-empty-line-terminates-plain-lists nil
  6415. "Non-nil means, an empty line ends all plain list levels.
  6416. When nil, empty lines are part of the preceeding item."
  6417. :group 'org-plain-lists
  6418. :type 'boolean)
  6419. (defun org-beginning-of-item ()
  6420. "Go to the beginning of the current hand-formatted item.
  6421. If the cursor is not in an item, throw an error."
  6422. (interactive)
  6423. (let ((pos (point))
  6424. (limit (save-excursion
  6425. (condition-case nil
  6426. (progn
  6427. (org-back-to-heading)
  6428. (beginning-of-line 2) (point))
  6429. (error (point-min)))))
  6430. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6431. ind ind1)
  6432. (if (org-at-item-p)
  6433. (beginning-of-line 1)
  6434. (beginning-of-line 1)
  6435. (skip-chars-forward " \t")
  6436. (setq ind (current-column))
  6437. (if (catch 'exit
  6438. (while t
  6439. (beginning-of-line 0)
  6440. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6441. (if (looking-at "[ \t]*$")
  6442. (setq ind1 ind-empty)
  6443. (skip-chars-forward " \t")
  6444. (setq ind1 (current-column)))
  6445. (if (< ind1 ind)
  6446. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6447. nil
  6448. (goto-char pos)
  6449. (error "Not in an item")))))
  6450. (defun org-end-of-item ()
  6451. "Go to the end of the current hand-formatted item.
  6452. If the cursor is not in an item, throw an error."
  6453. (interactive)
  6454. (let* ((pos (point))
  6455. ind1
  6456. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6457. (limit (save-excursion (outline-next-heading) (point)))
  6458. (ind (save-excursion
  6459. (org-beginning-of-item)
  6460. (skip-chars-forward " \t")
  6461. (current-column)))
  6462. (end (catch 'exit
  6463. (while t
  6464. (beginning-of-line 2)
  6465. (if (eobp) (throw 'exit (point)))
  6466. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6467. (if (looking-at "[ \t]*$")
  6468. (setq ind1 ind-empty)
  6469. (skip-chars-forward " \t")
  6470. (setq ind1 (current-column)))
  6471. (if (<= ind1 ind)
  6472. (throw 'exit (point-at-bol)))))))
  6473. (if end
  6474. (goto-char end)
  6475. (goto-char pos)
  6476. (error "Not in an item"))))
  6477. (defun org-next-item ()
  6478. "Move to the beginning of the next item in the current plain list.
  6479. Error if not at a plain list, or if this is the last item in the list."
  6480. (interactive)
  6481. (let (ind ind1 (pos (point)))
  6482. (org-beginning-of-item)
  6483. (setq ind (org-get-indentation))
  6484. (org-end-of-item)
  6485. (setq ind1 (org-get-indentation))
  6486. (unless (and (org-at-item-p) (= ind ind1))
  6487. (goto-char pos)
  6488. (error "On last item"))))
  6489. (defun org-previous-item ()
  6490. "Move to the beginning of the previous item in the current plain list.
  6491. Error if not at a plain list, or if this is the first item in the list."
  6492. (interactive)
  6493. (let (beg ind ind1 (pos (point)))
  6494. (org-beginning-of-item)
  6495. (setq beg (point))
  6496. (setq ind (org-get-indentation))
  6497. (goto-char beg)
  6498. (catch 'exit
  6499. (while t
  6500. (beginning-of-line 0)
  6501. (if (looking-at "[ \t]*$")
  6502. nil
  6503. (if (<= (setq ind1 (org-get-indentation)) ind)
  6504. (throw 'exit t)))))
  6505. (condition-case nil
  6506. (if (or (not (org-at-item-p))
  6507. (< ind1 (1- ind)))
  6508. (error "")
  6509. (org-beginning-of-item))
  6510. (error (goto-char pos)
  6511. (error "On first item")))))
  6512. (defun org-first-list-item-p ()
  6513. "Is this heading the item in a plain list?"
  6514. (unless (org-at-item-p)
  6515. (error "Not at a plain list item"))
  6516. (org-beginning-of-item)
  6517. (= (point) (save-excursion (org-beginning-of-item-list))))
  6518. (defun org-move-item-down ()
  6519. "Move the plain list item at point down, i.e. swap with following item.
  6520. Subitems (items with larger indentation) are considered part of the item,
  6521. so this really moves item trees."
  6522. (interactive)
  6523. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6524. (org-beginning-of-item)
  6525. (setq beg0 (point))
  6526. (save-excursion
  6527. (setq ne-beg (org-back-over-empty-lines))
  6528. (setq beg (point)))
  6529. (goto-char beg0)
  6530. (setq ind (org-get-indentation))
  6531. (org-end-of-item)
  6532. (setq end0 (point))
  6533. (setq ind1 (org-get-indentation))
  6534. (setq ne-end (org-back-over-empty-lines))
  6535. (setq end (point))
  6536. (goto-char beg0)
  6537. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6538. ;; include less whitespace
  6539. (save-excursion
  6540. (goto-char beg)
  6541. (forward-line (- ne-beg ne-end))
  6542. (setq beg (point))))
  6543. (goto-char end0)
  6544. (if (and (org-at-item-p) (= ind ind1))
  6545. (progn
  6546. (org-end-of-item)
  6547. (org-back-over-empty-lines)
  6548. (setq txt (buffer-substring beg end))
  6549. (save-excursion
  6550. (delete-region beg end))
  6551. (setq pos (point))
  6552. (insert txt)
  6553. (goto-char pos) (org-skip-whitespace)
  6554. (org-maybe-renumber-ordered-list))
  6555. (goto-char pos)
  6556. (error "Cannot move this item further down"))))
  6557. (defun org-move-item-up (arg)
  6558. "Move the plain list item at point up, i.e. swap with previous item.
  6559. Subitems (items with larger indentation) are considered part of the item,
  6560. so this really moves item trees."
  6561. (interactive "p")
  6562. (let (beg beg0 end ind ind1 (pos (point)) txt
  6563. ne-beg ne-ins ins-end)
  6564. (org-beginning-of-item)
  6565. (setq beg0 (point))
  6566. (setq ind (org-get-indentation))
  6567. (save-excursion
  6568. (setq ne-beg (org-back-over-empty-lines))
  6569. (setq beg (point)))
  6570. (goto-char beg0)
  6571. (org-end-of-item)
  6572. (setq end (point))
  6573. (goto-char beg0)
  6574. (catch 'exit
  6575. (while t
  6576. (beginning-of-line 0)
  6577. (if (looking-at "[ \t]*$")
  6578. (if org-empty-line-terminates-plain-lists
  6579. (progn
  6580. (goto-char pos)
  6581. (error "Cannot move this item further up"))
  6582. nil)
  6583. (if (<= (setq ind1 (org-get-indentation)) ind)
  6584. (throw 'exit t)))))
  6585. (condition-case nil
  6586. (org-beginning-of-item)
  6587. (error (goto-char beg)
  6588. (error "Cannot move this item further up")))
  6589. (setq ind1 (org-get-indentation))
  6590. (if (and (org-at-item-p) (= ind ind1))
  6591. (progn
  6592. (setq ne-ins (org-back-over-empty-lines))
  6593. (setq txt (buffer-substring beg end))
  6594. (save-excursion
  6595. (delete-region beg end))
  6596. (setq pos (point))
  6597. (insert txt)
  6598. (setq ins-end (point))
  6599. (goto-char pos) (org-skip-whitespace)
  6600. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6601. ;; Move whitespace back to beginning
  6602. (save-excursion
  6603. (goto-char ins-end)
  6604. (let ((kill-whole-line t))
  6605. (kill-line (- ne-ins ne-beg)) (point)))
  6606. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6607. (org-maybe-renumber-ordered-list))
  6608. (goto-char pos)
  6609. (error "Cannot move this item further up"))))
  6610. (defun org-maybe-renumber-ordered-list ()
  6611. "Renumber the ordered list at point if setup allows it.
  6612. This tests the user option `org-auto-renumber-ordered-lists' before
  6613. doing the renumbering."
  6614. (interactive)
  6615. (when (and org-auto-renumber-ordered-lists
  6616. (org-at-item-p))
  6617. (if (match-beginning 3)
  6618. (org-renumber-ordered-list 1)
  6619. (org-fix-bullet-type))))
  6620. (defun org-maybe-renumber-ordered-list-safe ()
  6621. (condition-case nil
  6622. (save-excursion
  6623. (org-maybe-renumber-ordered-list))
  6624. (error nil)))
  6625. (defun org-cycle-list-bullet (&optional which)
  6626. "Cycle through the different itemize/enumerate bullets.
  6627. This cycle the entire list level through the sequence:
  6628. `-' -> `+' -> `*' -> `1.' -> `1)'
  6629. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6630. 0 meand `-', 1 means `+' etc."
  6631. (interactive "P")
  6632. (org-preserve-lc
  6633. (org-beginning-of-item-list)
  6634. (org-at-item-p)
  6635. (beginning-of-line 1)
  6636. (let ((current (match-string 0))
  6637. (prevp (eq which 'previous))
  6638. new)
  6639. (setq new (cond
  6640. ((and (numberp which)
  6641. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6642. ((string-match "-" current) (if prevp "1)" "+"))
  6643. ((string-match "\\+" current)
  6644. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6645. ((string-match "\\*" current) (if prevp "+" "1."))
  6646. ((string-match "\\." current) (if prevp "*" "1)"))
  6647. ((string-match ")" current) (if prevp "1." "-"))
  6648. (t (error "This should not happen"))))
  6649. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6650. (org-fix-bullet-type)
  6651. (org-maybe-renumber-ordered-list))))
  6652. (defun org-get-string-indentation (s)
  6653. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6654. (let ((n -1) (i 0) (w tab-width) c)
  6655. (catch 'exit
  6656. (while (< (setq n (1+ n)) (length s))
  6657. (setq c (aref s n))
  6658. (cond ((= c ?\ ) (setq i (1+ i)))
  6659. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6660. (t (throw 'exit t)))))
  6661. i))
  6662. (defun org-renumber-ordered-list (arg)
  6663. "Renumber an ordered plain list.
  6664. Cursor needs to be in the first line of an item, the line that starts
  6665. with something like \"1.\" or \"2)\"."
  6666. (interactive "p")
  6667. (unless (and (org-at-item-p)
  6668. (match-beginning 3))
  6669. (error "This is not an ordered list"))
  6670. (let ((line (org-current-line))
  6671. (col (current-column))
  6672. (ind (org-get-string-indentation
  6673. (buffer-substring (point-at-bol) (match-beginning 3))))
  6674. ;; (term (substring (match-string 3) -1))
  6675. ind1 (n (1- arg))
  6676. fmt)
  6677. ;; find where this list begins
  6678. (org-beginning-of-item-list)
  6679. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6680. (setq fmt (concat "%d" (match-string 1)))
  6681. (beginning-of-line 0)
  6682. ;; walk forward and replace these numbers
  6683. (catch 'exit
  6684. (while t
  6685. (catch 'next
  6686. (beginning-of-line 2)
  6687. (if (eobp) (throw 'exit nil))
  6688. (if (looking-at "[ \t]*$") (throw 'next nil))
  6689. (skip-chars-forward " \t") (setq ind1 (current-column))
  6690. (if (> ind1 ind) (throw 'next t))
  6691. (if (< ind1 ind) (throw 'exit t))
  6692. (if (not (org-at-item-p)) (throw 'exit nil))
  6693. (delete-region (match-beginning 2) (match-end 2))
  6694. (goto-char (match-beginning 2))
  6695. (insert (format fmt (setq n (1+ n)))))))
  6696. (goto-line line)
  6697. (move-to-column col)))
  6698. (defun org-fix-bullet-type ()
  6699. "Make sure all items in this list have the same bullet as the firsst item."
  6700. (interactive)
  6701. (unless (org-at-item-p) (error "This is not a list"))
  6702. (let ((line (org-current-line))
  6703. (col (current-column))
  6704. (ind (current-indentation))
  6705. ind1 bullet)
  6706. ;; find where this list begins
  6707. (org-beginning-of-item-list)
  6708. (beginning-of-line 1)
  6709. ;; find out what the bullet type is
  6710. (looking-at "[ \t]*\\(\\S-+\\)")
  6711. (setq bullet (match-string 1))
  6712. ;; walk forward and replace these numbers
  6713. (beginning-of-line 0)
  6714. (catch 'exit
  6715. (while t
  6716. (catch 'next
  6717. (beginning-of-line 2)
  6718. (if (eobp) (throw 'exit nil))
  6719. (if (looking-at "[ \t]*$") (throw 'next nil))
  6720. (skip-chars-forward " \t") (setq ind1 (current-column))
  6721. (if (> ind1 ind) (throw 'next t))
  6722. (if (< ind1 ind) (throw 'exit t))
  6723. (if (not (org-at-item-p)) (throw 'exit nil))
  6724. (skip-chars-forward " \t")
  6725. (looking-at "\\S-+")
  6726. (replace-match bullet))))
  6727. (goto-line line)
  6728. (move-to-column col)
  6729. (if (string-match "[0-9]" bullet)
  6730. (org-renumber-ordered-list 1))))
  6731. (defun org-beginning-of-item-list ()
  6732. "Go to the beginning of the current item list.
  6733. I.e. to the first item in this list."
  6734. (interactive)
  6735. (org-beginning-of-item)
  6736. (let ((pos (point-at-bol))
  6737. (ind (org-get-indentation))
  6738. ind1)
  6739. ;; find where this list begins
  6740. (catch 'exit
  6741. (while t
  6742. (catch 'next
  6743. (beginning-of-line 0)
  6744. (if (looking-at "[ \t]*$")
  6745. (throw (if (bobp) 'exit 'next) t))
  6746. (skip-chars-forward " \t") (setq ind1 (current-column))
  6747. (if (or (< ind1 ind)
  6748. (and (= ind1 ind)
  6749. (not (org-at-item-p)))
  6750. (bobp))
  6751. (throw 'exit t)
  6752. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6753. (goto-char pos)))
  6754. (defun org-end-of-item-list ()
  6755. "Go to the end of the current item list.
  6756. I.e. to the text after the last item."
  6757. (interactive)
  6758. (org-beginning-of-item)
  6759. (let ((pos (point-at-bol))
  6760. (ind (org-get-indentation))
  6761. ind1)
  6762. ;; find where this list begins
  6763. (catch 'exit
  6764. (while t
  6765. (catch 'next
  6766. (beginning-of-line 2)
  6767. (if (looking-at "[ \t]*$")
  6768. (throw (if (eobp) 'exit 'next) t))
  6769. (skip-chars-forward " \t") (setq ind1 (current-column))
  6770. (if (or (< ind1 ind)
  6771. (and (= ind1 ind)
  6772. (not (org-at-item-p)))
  6773. (eobp))
  6774. (progn
  6775. (setq pos (point-at-bol))
  6776. (throw 'exit t))))))
  6777. (goto-char pos)))
  6778. (defvar org-last-indent-begin-marker (make-marker))
  6779. (defvar org-last-indent-end-marker (make-marker))
  6780. (defun org-outdent-item (arg)
  6781. "Outdent a local list item."
  6782. (interactive "p")
  6783. (org-indent-item (- arg)))
  6784. (defun org-indent-item (arg)
  6785. "Indent a local list item."
  6786. (interactive "p")
  6787. (unless (org-at-item-p)
  6788. (error "Not on an item"))
  6789. (save-excursion
  6790. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6791. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6792. (setq beg org-last-indent-begin-marker
  6793. end org-last-indent-end-marker)
  6794. (org-beginning-of-item)
  6795. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6796. (org-end-of-item)
  6797. (setq end (move-marker org-last-indent-end-marker (point))))
  6798. (goto-char beg)
  6799. (setq tmp (org-item-indent-positions)
  6800. ind (car tmp)
  6801. ind-down (nth 2 tmp)
  6802. ind-up (nth 1 tmp)
  6803. delta (if (> arg 0)
  6804. (if ind-down (- ind-down ind) 2)
  6805. (if ind-up (- ind-up ind) -2)))
  6806. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6807. (while (< (point) end)
  6808. (beginning-of-line 1)
  6809. (skip-chars-forward " \t") (setq ind1 (current-column))
  6810. (delete-region (point-at-bol) (point))
  6811. (or (eolp) (indent-to-column (+ ind1 delta)))
  6812. (beginning-of-line 2))))
  6813. (org-fix-bullet-type)
  6814. (org-maybe-renumber-ordered-list-safe)
  6815. (save-excursion
  6816. (beginning-of-line 0)
  6817. (condition-case nil (org-beginning-of-item) (error nil))
  6818. (org-maybe-renumber-ordered-list-safe)))
  6819. (defun org-item-indent-positions ()
  6820. "Return indentation for plain list items.
  6821. This returns a list with three values: The current indentation, the
  6822. parent indentation and the indentation a child should habe.
  6823. Assumes cursor in item line."
  6824. (let* ((bolpos (point-at-bol))
  6825. (ind (org-get-indentation))
  6826. ind-down ind-up pos)
  6827. (save-excursion
  6828. (org-beginning-of-item-list)
  6829. (skip-chars-backward "\n\r \t")
  6830. (when (org-in-item-p)
  6831. (org-beginning-of-item)
  6832. (setq ind-up (org-get-indentation))))
  6833. (setq pos (point))
  6834. (save-excursion
  6835. (cond
  6836. ((and (condition-case nil (progn (org-previous-item) t)
  6837. (error nil))
  6838. (or (forward-char 1) t)
  6839. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6840. (setq ind-down (org-get-indentation)))
  6841. ((and (goto-char pos)
  6842. (org-at-item-p))
  6843. (goto-char (match-end 0))
  6844. (skip-chars-forward " \t")
  6845. (setq ind-down (current-column)))))
  6846. (list ind ind-up ind-down)))
  6847. ;;; The orgstruct minor mode
  6848. ;; Define a minor mode which can be used in other modes in order to
  6849. ;; integrate the org-mode structure editing commands.
  6850. ;; This is really a hack, because the org-mode structure commands use
  6851. ;; keys which normally belong to the major mode. Here is how it
  6852. ;; works: The minor mode defines all the keys necessary to operate the
  6853. ;; structure commands, but wraps the commands into a function which
  6854. ;; tests if the cursor is currently at a headline or a plain list
  6855. ;; item. If that is the case, the structure command is used,
  6856. ;; temporarily setting many Org-mode variables like regular
  6857. ;; expressions for filling etc. However, when any of those keys is
  6858. ;; used at a different location, function uses `key-binding' to look
  6859. ;; up if the key has an associated command in another currently active
  6860. ;; keymap (minor modes, major mode, global), and executes that
  6861. ;; command. There might be problems if any of the keys is otherwise
  6862. ;; used as a prefix key.
  6863. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6864. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6865. ;; addresses this by checking explicitly for both bindings.
  6866. (defvar orgstruct-mode-map (make-sparse-keymap)
  6867. "Keymap for the minor `orgstruct-mode'.")
  6868. (defvar org-local-vars nil
  6869. "List of local variables, for use by `orgstruct-mode'")
  6870. ;;;###autoload
  6871. (define-minor-mode orgstruct-mode
  6872. "Toggle the minor more `orgstruct-mode'.
  6873. This mode is for using Org-mode structure commands in other modes.
  6874. The following key behave as if Org-mode was active, if the cursor
  6875. is on a headline, or on a plain list item (both in the definition
  6876. of Org-mode).
  6877. M-up Move entry/item up
  6878. M-down Move entry/item down
  6879. M-left Promote
  6880. M-right Demote
  6881. M-S-up Move entry/item up
  6882. M-S-down Move entry/item down
  6883. M-S-left Promote subtree
  6884. M-S-right Demote subtree
  6885. M-q Fill paragraph and items like in Org-mode
  6886. C-c ^ Sort entries
  6887. C-c - Cycle list bullet
  6888. TAB Cycle item visibility
  6889. M-RET Insert new heading/item
  6890. S-M-RET Insert new TODO heading / Chekbox item
  6891. C-c C-c Set tags / toggle checkbox"
  6892. nil " OrgStruct" nil
  6893. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6894. ;;;###autoload
  6895. (defun turn-on-orgstruct ()
  6896. "Unconditionally turn on `orgstruct-mode'."
  6897. (orgstruct-mode 1))
  6898. ;;;###autoload
  6899. (defun turn-on-orgstruct++ ()
  6900. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6901. In addition to setting orgstruct-mode, this also exports all indentation and
  6902. autofilling variables from org-mode into the buffer. Note that turning
  6903. off orgstruct-mode will *not* remove these additional settings."
  6904. (orgstruct-mode 1)
  6905. (let (var val)
  6906. (mapc
  6907. (lambda (x)
  6908. (when (string-match
  6909. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6910. (symbol-name (car x)))
  6911. (setq var (car x) val (nth 1 x))
  6912. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6913. org-local-vars)))
  6914. (defun orgstruct-error ()
  6915. "Error when there is no default binding for a structure key."
  6916. (interactive)
  6917. (error "This key has no function outside structure elements"))
  6918. (defun orgstruct-setup ()
  6919. "Setup orgstruct keymaps."
  6920. (let ((nfunc 0)
  6921. (bindings
  6922. (list
  6923. '([(meta up)] org-metaup)
  6924. '([(meta down)] org-metadown)
  6925. '([(meta left)] org-metaleft)
  6926. '([(meta right)] org-metaright)
  6927. '([(meta shift up)] org-shiftmetaup)
  6928. '([(meta shift down)] org-shiftmetadown)
  6929. '([(meta shift left)] org-shiftmetaleft)
  6930. '([(meta shift right)] org-shiftmetaright)
  6931. '([(shift up)] org-shiftup)
  6932. '([(shift down)] org-shiftdown)
  6933. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6934. '("\M-q" fill-paragraph)
  6935. '("\C-c^" org-sort)
  6936. '("\C-c-" org-cycle-list-bullet)))
  6937. elt key fun cmd)
  6938. (while (setq elt (pop bindings))
  6939. (setq nfunc (1+ nfunc))
  6940. (setq key (org-key (car elt))
  6941. fun (nth 1 elt)
  6942. cmd (orgstruct-make-binding fun nfunc key))
  6943. (org-defkey orgstruct-mode-map key cmd))
  6944. ;; Special treatment needed for TAB and RET
  6945. (org-defkey orgstruct-mode-map [(tab)]
  6946. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6947. (org-defkey orgstruct-mode-map "\C-i"
  6948. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6949. (org-defkey orgstruct-mode-map "\M-\C-m"
  6950. (orgstruct-make-binding 'org-insert-heading 105
  6951. "\M-\C-m" [(meta return)]))
  6952. (org-defkey orgstruct-mode-map [(meta return)]
  6953. (orgstruct-make-binding 'org-insert-heading 106
  6954. [(meta return)] "\M-\C-m"))
  6955. (org-defkey orgstruct-mode-map [(shift meta return)]
  6956. (orgstruct-make-binding 'org-insert-todo-heading 107
  6957. [(meta return)] "\M-\C-m"))
  6958. (unless org-local-vars
  6959. (setq org-local-vars (org-get-local-variables)))
  6960. t))
  6961. (defun orgstruct-make-binding (fun n &rest keys)
  6962. "Create a function for binding in the structure minor mode.
  6963. FUN is the command to call inside a table. N is used to create a unique
  6964. command name. KEYS are keys that should be checked in for a command
  6965. to execute outside of tables."
  6966. (eval
  6967. (list 'defun
  6968. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6969. '(arg)
  6970. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6971. "Outside of structure, run the binding of `"
  6972. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6973. "'.")
  6974. '(interactive "p")
  6975. (list 'if
  6976. '(org-context-p 'headline 'item)
  6977. (list 'org-run-like-in-org-mode (list 'quote fun))
  6978. (list 'let '(orgstruct-mode)
  6979. (list 'call-interactively
  6980. (append '(or)
  6981. (mapcar (lambda (k)
  6982. (list 'key-binding k))
  6983. keys)
  6984. '('orgstruct-error))))))))
  6985. (defun org-context-p (&rest contexts)
  6986. "Check if local context is and of CONTEXTS.
  6987. Possible values in the list of contexts are `table', `headline', and `item'."
  6988. (let ((pos (point)))
  6989. (goto-char (point-at-bol))
  6990. (prog1 (or (and (memq 'table contexts)
  6991. (looking-at "[ \t]*|"))
  6992. (and (memq 'headline contexts)
  6993. (looking-at "\\*+"))
  6994. (and (memq 'item contexts)
  6995. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6996. (goto-char pos))))
  6997. (defun org-get-local-variables ()
  6998. "Return a list of all local variables in an org-mode buffer."
  6999. (let (varlist)
  7000. (with-current-buffer (get-buffer-create "*Org tmp*")
  7001. (erase-buffer)
  7002. (org-mode)
  7003. (setq varlist (buffer-local-variables)))
  7004. (kill-buffer "*Org tmp*")
  7005. (delq nil
  7006. (mapcar
  7007. (lambda (x)
  7008. (setq x
  7009. (if (symbolp x)
  7010. (list x)
  7011. (list (car x) (list 'quote (cdr x)))))
  7012. (if (string-match
  7013. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7014. (symbol-name (car x)))
  7015. x nil))
  7016. varlist))))
  7017. ;;;###autoload
  7018. (defun org-run-like-in-org-mode (cmd)
  7019. (unless org-local-vars
  7020. (setq org-local-vars (org-get-local-variables)))
  7021. (eval (list 'let org-local-vars
  7022. (list 'call-interactively (list 'quote cmd)))))
  7023. ;;;; Archiving
  7024. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7025. (defun org-archive-subtree (&optional find-done)
  7026. "Move the current subtree to the archive.
  7027. The archive can be a certain top-level heading in the current file, or in
  7028. a different file. The tree will be moved to that location, the subtree
  7029. heading be marked DONE, and the current time will be added.
  7030. When called with prefix argument FIND-DONE, find whole trees without any
  7031. open TODO items and archive them (after getting confirmation from the user).
  7032. If the cursor is not at a headline when this comand is called, try all level
  7033. 1 trees. If the cursor is on a headline, only try the direct children of
  7034. this heading."
  7035. (interactive "P")
  7036. (if find-done
  7037. (org-archive-all-done)
  7038. ;; Save all relevant TODO keyword-relatex variables
  7039. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7040. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7041. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7042. (tr-org-done-keywords org-done-keywords)
  7043. (tr-org-todo-regexp org-todo-regexp)
  7044. (tr-org-todo-line-regexp org-todo-line-regexp)
  7045. (tr-org-odd-levels-only org-odd-levels-only)
  7046. (this-buffer (current-buffer))
  7047. (org-archive-location org-archive-location)
  7048. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7049. ;; start of variables that will be used for saving context
  7050. ;; The compiler complains about them - keep them anyway!
  7051. (file (abbreviate-file-name (buffer-file-name)))
  7052. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7053. (time (format-time-string
  7054. (substring (cdr org-time-stamp-formats) 1 -1)
  7055. (current-time)))
  7056. afile heading buffer level newfile-p
  7057. category todo priority
  7058. ;; start of variables that will be used for savind context
  7059. ltags itags prop)
  7060. ;; Try to find a local archive location
  7061. (save-excursion
  7062. (save-restriction
  7063. (widen)
  7064. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7065. (if (and prop (string-match "\\S-" prop))
  7066. (setq org-archive-location prop)
  7067. (if (or (re-search-backward re nil t)
  7068. (re-search-forward re nil t))
  7069. (setq org-archive-location (match-string 1))))))
  7070. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7071. (progn
  7072. (setq afile (format (match-string 1 org-archive-location)
  7073. (file-name-nondirectory buffer-file-name))
  7074. heading (match-string 2 org-archive-location)))
  7075. (error "Invalid `org-archive-location'"))
  7076. (if (> (length afile) 0)
  7077. (setq newfile-p (not (file-exists-p afile))
  7078. buffer (find-file-noselect afile))
  7079. (setq buffer (current-buffer)))
  7080. (unless buffer
  7081. (error "Cannot access file \"%s\"" afile))
  7082. (if (and (> (length heading) 0)
  7083. (string-match "^\\*+" heading))
  7084. (setq level (match-end 0))
  7085. (setq heading nil level 0))
  7086. (save-excursion
  7087. (org-back-to-heading t)
  7088. ;; Get context information that will be lost by moving the tree
  7089. (org-refresh-category-properties)
  7090. (setq category (org-get-category)
  7091. todo (and (looking-at org-todo-line-regexp)
  7092. (match-string 2))
  7093. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7094. ltags (org-get-tags)
  7095. itags (org-delete-all ltags (org-get-tags-at)))
  7096. (setq ltags (mapconcat 'identity ltags " ")
  7097. itags (mapconcat 'identity itags " "))
  7098. ;; We first only copy, in case something goes wrong
  7099. ;; we need to protect this-command, to avoid kill-region sets it,
  7100. ;; which would lead to duplication of subtrees
  7101. (let (this-command) (org-copy-subtree))
  7102. (set-buffer buffer)
  7103. ;; Enforce org-mode for the archive buffer
  7104. (if (not (org-mode-p))
  7105. ;; Force the mode for future visits.
  7106. (let ((org-insert-mode-line-in-empty-file t)
  7107. (org-inhibit-startup t))
  7108. (call-interactively 'org-mode)))
  7109. (when newfile-p
  7110. (goto-char (point-max))
  7111. (insert (format "\nArchived entries from file %s\n\n"
  7112. (buffer-file-name this-buffer))))
  7113. ;; Force the TODO keywords of the original buffer
  7114. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7115. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7116. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7117. (org-done-keywords tr-org-done-keywords)
  7118. (org-todo-regexp tr-org-todo-regexp)
  7119. (org-todo-line-regexp tr-org-todo-line-regexp)
  7120. (org-odd-levels-only
  7121. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7122. org-odd-levels-only
  7123. tr-org-odd-levels-only)))
  7124. (goto-char (point-min))
  7125. (if heading
  7126. (progn
  7127. (if (re-search-forward
  7128. (concat "^" (regexp-quote heading)
  7129. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7130. nil t)
  7131. (goto-char (match-end 0))
  7132. ;; Heading not found, just insert it at the end
  7133. (goto-char (point-max))
  7134. (or (bolp) (insert "\n"))
  7135. (insert "\n" heading "\n")
  7136. (end-of-line 0))
  7137. ;; Make the subtree visible
  7138. (show-subtree)
  7139. (org-end-of-subtree t)
  7140. (skip-chars-backward " \t\r\n")
  7141. (and (looking-at "[ \t\r\n]*")
  7142. (replace-match "\n\n")))
  7143. ;; No specific heading, just go to end of file.
  7144. (goto-char (point-max)) (insert "\n"))
  7145. ;; Paste
  7146. (org-paste-subtree (org-get-legal-level level 1))
  7147. ;; Mark the entry as done
  7148. (when (and org-archive-mark-done
  7149. (looking-at org-todo-line-regexp)
  7150. (or (not (match-end 2))
  7151. (not (member (match-string 2) org-done-keywords))))
  7152. (let (org-log-done org-todo-log-states)
  7153. (org-todo
  7154. (car (or (member org-archive-mark-done org-done-keywords)
  7155. org-done-keywords)))))
  7156. ;; Add the context info
  7157. (when org-archive-save-context-info
  7158. (let ((l org-archive-save-context-info) e n v)
  7159. (while (setq e (pop l))
  7160. (when (and (setq v (symbol-value e))
  7161. (stringp v) (string-match "\\S-" v))
  7162. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7163. (org-entry-put (point) n v)))))
  7164. ;; Save the buffer, if it is not the same buffer.
  7165. (if (not (eq this-buffer buffer)) (save-buffer))))
  7166. ;; Here we are back in the original buffer. Everything seems to have
  7167. ;; worked. So now cut the tree and finish up.
  7168. (let (this-command) (org-cut-subtree))
  7169. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7170. (message "Subtree archived %s"
  7171. (if (eq this-buffer buffer)
  7172. (concat "under heading: " heading)
  7173. (concat "in file: " (abbreviate-file-name afile)))))))
  7174. (defun org-refresh-category-properties ()
  7175. "Refresh category text properties in teh buffer."
  7176. (let ((def-cat (cond
  7177. ((null org-category)
  7178. (if buffer-file-name
  7179. (file-name-sans-extension
  7180. (file-name-nondirectory buffer-file-name))
  7181. "???"))
  7182. ((symbolp org-category) (symbol-name org-category))
  7183. (t org-category)))
  7184. beg end cat pos optionp)
  7185. (org-unmodified
  7186. (save-excursion
  7187. (save-restriction
  7188. (widen)
  7189. (goto-char (point-min))
  7190. (put-text-property (point) (point-max) 'org-category def-cat)
  7191. (while (re-search-forward
  7192. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7193. (setq pos (match-end 0)
  7194. optionp (equal (char-after (match-beginning 0)) ?#)
  7195. cat (org-trim (match-string 2)))
  7196. (if optionp
  7197. (setq beg (point-at-bol) end (point-max))
  7198. (org-back-to-heading t)
  7199. (setq beg (point) end (org-end-of-subtree t t)))
  7200. (put-text-property beg end 'org-category cat)
  7201. (goto-char pos)))))))
  7202. (defun org-archive-all-done (&optional tag)
  7203. "Archive sublevels of the current tree without open TODO items.
  7204. If the cursor is not on a headline, try all level 1 trees. If
  7205. it is on a headline, try all direct children.
  7206. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7207. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7208. (rea (concat ".*:" org-archive-tag ":"))
  7209. (begm (make-marker))
  7210. (endm (make-marker))
  7211. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7212. "Move subtree to archive (no open TODO items)? "))
  7213. beg end (cntarch 0))
  7214. (if (org-on-heading-p)
  7215. (progn
  7216. (setq re1 (concat "^" (regexp-quote
  7217. (make-string
  7218. (1+ (- (match-end 0) (match-beginning 0) 1))
  7219. ?*))
  7220. " "))
  7221. (move-marker begm (point))
  7222. (move-marker endm (org-end-of-subtree t)))
  7223. (setq re1 "^* ")
  7224. (move-marker begm (point-min))
  7225. (move-marker endm (point-max)))
  7226. (save-excursion
  7227. (goto-char begm)
  7228. (while (re-search-forward re1 endm t)
  7229. (setq beg (match-beginning 0)
  7230. end (save-excursion (org-end-of-subtree t) (point)))
  7231. (goto-char beg)
  7232. (if (re-search-forward re end t)
  7233. (goto-char end)
  7234. (goto-char beg)
  7235. (if (and (or (not tag) (not (looking-at rea)))
  7236. (y-or-n-p question))
  7237. (progn
  7238. (if tag
  7239. (org-toggle-tag org-archive-tag 'on)
  7240. (org-archive-subtree))
  7241. (setq cntarch (1+ cntarch)))
  7242. (goto-char end)))))
  7243. (message "%d trees archived" cntarch)))
  7244. (defun org-cycle-hide-drawers (state)
  7245. "Re-hide all drawers after a visibility state change."
  7246. (when (and (org-mode-p)
  7247. (not (memq state '(overview folded))))
  7248. (save-excursion
  7249. (let* ((globalp (memq state '(contents all)))
  7250. (beg (if globalp (point-min) (point)))
  7251. (end (if globalp (point-max) (org-end-of-subtree t))))
  7252. (goto-char beg)
  7253. (while (re-search-forward org-drawer-regexp end t)
  7254. (org-flag-drawer t))))))
  7255. (defun org-flag-drawer (flag)
  7256. (save-excursion
  7257. (beginning-of-line 1)
  7258. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7259. (let ((b (match-end 0))
  7260. (outline-regexp org-outline-regexp))
  7261. (if (re-search-forward
  7262. "^[ \t]*:END:"
  7263. (save-excursion (outline-next-heading) (point)) t)
  7264. (outline-flag-region b (point-at-eol) flag)
  7265. (error ":END: line missing"))))))
  7266. (defun org-cycle-hide-archived-subtrees (state)
  7267. "Re-hide all archived subtrees after a visibility state change."
  7268. (when (and (not org-cycle-open-archived-trees)
  7269. (not (memq state '(overview folded))))
  7270. (save-excursion
  7271. (let* ((globalp (memq state '(contents all)))
  7272. (beg (if globalp (point-min) (point)))
  7273. (end (if globalp (point-max) (org-end-of-subtree t))))
  7274. (org-hide-archived-subtrees beg end)
  7275. (goto-char beg)
  7276. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7277. (message "%s" (substitute-command-keys
  7278. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7279. (defun org-force-cycle-archived ()
  7280. "Cycle subtree even if it is archived."
  7281. (interactive)
  7282. (setq this-command 'org-cycle)
  7283. (let ((org-cycle-open-archived-trees t))
  7284. (call-interactively 'org-cycle)))
  7285. (defun org-hide-archived-subtrees (beg end)
  7286. "Re-hide all archived subtrees after a visibility state change."
  7287. (save-excursion
  7288. (let* ((re (concat ":" org-archive-tag ":")))
  7289. (goto-char beg)
  7290. (while (re-search-forward re end t)
  7291. (and (org-on-heading-p) (hide-subtree))
  7292. (org-end-of-subtree t)))))
  7293. (defun org-toggle-tag (tag &optional onoff)
  7294. "Toggle the tag TAG for the current line.
  7295. If ONOFF is `on' or `off', don't toggle but set to this state."
  7296. (unless (org-on-heading-p t) (error "Not on headling"))
  7297. (let (res current)
  7298. (save-excursion
  7299. (beginning-of-line)
  7300. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7301. (point-at-eol) t)
  7302. (progn
  7303. (setq current (match-string 1))
  7304. (replace-match ""))
  7305. (setq current ""))
  7306. (setq current (nreverse (org-split-string current ":")))
  7307. (cond
  7308. ((eq onoff 'on)
  7309. (setq res t)
  7310. (or (member tag current) (push tag current)))
  7311. ((eq onoff 'off)
  7312. (or (not (member tag current)) (setq current (delete tag current))))
  7313. (t (if (member tag current)
  7314. (setq current (delete tag current))
  7315. (setq res t)
  7316. (push tag current))))
  7317. (end-of-line 1)
  7318. (if current
  7319. (progn
  7320. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7321. (org-set-tags nil t))
  7322. (delete-horizontal-space))
  7323. (run-hooks 'org-after-tags-change-hook))
  7324. res))
  7325. (defun org-toggle-archive-tag (&optional arg)
  7326. "Toggle the archive tag for the current headline.
  7327. With prefix ARG, check all children of current headline and offer tagging
  7328. the children that do not contain any open TODO items."
  7329. (interactive "P")
  7330. (if arg
  7331. (org-archive-all-done 'tag)
  7332. (let (set)
  7333. (save-excursion
  7334. (org-back-to-heading t)
  7335. (setq set (org-toggle-tag org-archive-tag))
  7336. (when set (hide-subtree)))
  7337. (and set (beginning-of-line 1))
  7338. (message "Subtree %s" (if set "archived" "unarchived")))))
  7339. ;;;; Tables
  7340. ;;; The table editor
  7341. ;; Watch out: Here we are talking about two different kind of tables.
  7342. ;; Most of the code is for the tables created with the Org-mode table editor.
  7343. ;; Sometimes, we talk about tables created and edited with the table.el
  7344. ;; Emacs package. We call the former org-type tables, and the latter
  7345. ;; table.el-type tables.
  7346. (defun org-before-change-function (beg end)
  7347. "Every change indicates that a table might need an update."
  7348. (setq org-table-may-need-update t))
  7349. (defconst org-table-line-regexp "^[ \t]*|"
  7350. "Detects an org-type table line.")
  7351. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7352. "Detects an org-type table line.")
  7353. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7354. "Detects a table line marked for automatic recalculation.")
  7355. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7356. "Detects a table line marked for automatic recalculation.")
  7357. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7358. "Detects a table line marked for automatic recalculation.")
  7359. (defconst org-table-hline-regexp "^[ \t]*|-"
  7360. "Detects an org-type table hline.")
  7361. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7362. "Detects a table-type table hline.")
  7363. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7364. "Detects an org-type or table-type table.")
  7365. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7366. "Searching from within a table (any type) this finds the first line
  7367. outside the table.")
  7368. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7369. "Searching from within a table (any type) this finds the first line
  7370. outside the table.")
  7371. (defvar org-table-last-highlighted-reference nil)
  7372. (defvar org-table-formula-history nil)
  7373. (defvar org-table-column-names nil
  7374. "Alist with column names, derived from the `!' line.")
  7375. (defvar org-table-column-name-regexp nil
  7376. "Regular expression matching the current column names.")
  7377. (defvar org-table-local-parameters nil
  7378. "Alist with parameter names, derived from the `$' line.")
  7379. (defvar org-table-named-field-locations nil
  7380. "Alist with locations of named fields.")
  7381. (defvar org-table-current-line-types nil
  7382. "Table row types, non-nil only for the duration of a comand.")
  7383. (defvar org-table-current-begin-line nil
  7384. "Table begin line, non-nil only for the duration of a comand.")
  7385. (defvar org-table-current-begin-pos nil
  7386. "Table begin position, non-nil only for the duration of a comand.")
  7387. (defvar org-table-dlines nil
  7388. "Vector of data line line numbers in the current table.")
  7389. (defvar org-table-hlines nil
  7390. "Vector of hline line numbers in the current table.")
  7391. (defconst org-table-range-regexp
  7392. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7393. ;; 1 2 3 4 5
  7394. "Regular expression for matching ranges in formulas.")
  7395. (defconst org-table-range-regexp2
  7396. (concat
  7397. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7398. "\\.\\."
  7399. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7400. "Match a range for reference display.")
  7401. (defconst org-table-translate-regexp
  7402. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7403. "Match a reference that needs translation, for reference display.")
  7404. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7405. (defun org-table-create-with-table.el ()
  7406. "Use the table.el package to insert a new table.
  7407. If there is already a table at point, convert between Org-mode tables
  7408. and table.el tables."
  7409. (interactive)
  7410. (require 'table)
  7411. (cond
  7412. ((org-at-table.el-p)
  7413. (if (y-or-n-p "Convert table to Org-mode table? ")
  7414. (org-table-convert)))
  7415. ((org-at-table-p)
  7416. (if (y-or-n-p "Convert table to table.el table? ")
  7417. (org-table-convert)))
  7418. (t (call-interactively 'table-insert))))
  7419. (defun org-table-create-or-convert-from-region (arg)
  7420. "Convert region to table, or create an empty table.
  7421. If there is an active region, convert it to a table, using the function
  7422. `org-table-convert-region'. See the documentation of that function
  7423. to learn how the prefix argument is interpreted to determine the field
  7424. separator.
  7425. If there is no such region, create an empty table with `org-table-create'."
  7426. (interactive "P")
  7427. (if (org-region-active-p)
  7428. (org-table-convert-region (region-beginning) (region-end) arg)
  7429. (org-table-create arg)))
  7430. (defun org-table-create (&optional size)
  7431. "Query for a size and insert a table skeleton.
  7432. SIZE is a string Columns x Rows like for example \"3x2\"."
  7433. (interactive "P")
  7434. (unless size
  7435. (setq size (read-string
  7436. (concat "Table size Columns x Rows [e.g. "
  7437. org-table-default-size "]: ")
  7438. "" nil org-table-default-size)))
  7439. (let* ((pos (point))
  7440. (indent (make-string (current-column) ?\ ))
  7441. (split (org-split-string size " *x *"))
  7442. (rows (string-to-number (nth 1 split)))
  7443. (columns (string-to-number (car split)))
  7444. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7445. "\n")))
  7446. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7447. (point-at-bol) (point)))
  7448. (beginning-of-line 1)
  7449. (newline))
  7450. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7451. (dotimes (i rows) (insert line))
  7452. (goto-char pos)
  7453. (if (> rows 1)
  7454. ;; Insert a hline after the first row.
  7455. (progn
  7456. (end-of-line 1)
  7457. (insert "\n|-")
  7458. (goto-char pos)))
  7459. (org-table-align)))
  7460. (defun org-table-convert-region (beg0 end0 &optional separator)
  7461. "Convert region to a table.
  7462. The region goes from BEG0 to END0, but these borders will be moved
  7463. slightly, to make sure a beginning of line in the first line is included.
  7464. SEPARATOR specifies the field separator in the lines. It can have the
  7465. following values:
  7466. '(4) Use the comma as a field separator
  7467. '(16) Use a TAB as field separator
  7468. integer When a number, use that many spaces as field separator
  7469. nil When nil, the command tries to be smart and figure out the
  7470. separator in the following way:
  7471. - when each line contains a TAB, assume TAB-separated material
  7472. - when each line contains a comme, assume CSV material
  7473. - else, assume one or more SPACE charcters as separator."
  7474. (interactive "rP")
  7475. (let* ((beg (min beg0 end0))
  7476. (end (max beg0 end0))
  7477. re)
  7478. (goto-char beg)
  7479. (beginning-of-line 1)
  7480. (setq beg (move-marker (make-marker) (point)))
  7481. (goto-char end)
  7482. (if (bolp) (backward-char 1) (end-of-line 1))
  7483. (setq end (move-marker (make-marker) (point)))
  7484. ;; Get the right field separator
  7485. (unless separator
  7486. (goto-char beg)
  7487. (setq separator
  7488. (cond
  7489. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7490. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7491. (t 1))))
  7492. (setq re (cond
  7493. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7494. ((equal separator '(16)) "^\\|\t")
  7495. ((integerp separator)
  7496. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7497. (t (error "This should not happen"))))
  7498. (goto-char beg)
  7499. (while (re-search-forward re end t)
  7500. (replace-match "| " t t))
  7501. (goto-char beg)
  7502. (insert " ")
  7503. (org-table-align)))
  7504. (defun org-table-import (file arg)
  7505. "Import FILE as a table.
  7506. The file is assumed to be tab-separated. Such files can be produced by most
  7507. spreadsheet and database applications. If no tabs (at least one per line)
  7508. are found, lines will be split on whitespace into fields."
  7509. (interactive "f\nP")
  7510. (or (bolp) (newline))
  7511. (let ((beg (point))
  7512. (pm (point-max)))
  7513. (insert-file-contents file)
  7514. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7515. (defun org-table-export ()
  7516. "Export table as a tab-separated file.
  7517. Such a file can be imported into a spreadsheet program like Excel."
  7518. (interactive)
  7519. (let* ((beg (org-table-begin))
  7520. (end (org-table-end))
  7521. (table (buffer-substring beg end))
  7522. (file (read-file-name "Export table to: "))
  7523. buf)
  7524. (unless (or (not (file-exists-p file))
  7525. (y-or-n-p (format "Overwrite file %s? " file)))
  7526. (error "Abort"))
  7527. (with-current-buffer (find-file-noselect file)
  7528. (setq buf (current-buffer))
  7529. (erase-buffer)
  7530. (fundamental-mode)
  7531. (insert table)
  7532. (goto-char (point-min))
  7533. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7534. (replace-match "" t t)
  7535. (end-of-line 1))
  7536. (goto-char (point-min))
  7537. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7538. (replace-match "" t t)
  7539. (goto-char (min (1+ (point)) (point-max))))
  7540. (goto-char (point-min))
  7541. (while (re-search-forward "^-[-+]*$" nil t)
  7542. (replace-match "")
  7543. (if (looking-at "\n")
  7544. (delete-char 1)))
  7545. (goto-char (point-min))
  7546. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7547. (replace-match "\t" t t))
  7548. (save-buffer))
  7549. (kill-buffer buf)))
  7550. (defvar org-table-aligned-begin-marker (make-marker)
  7551. "Marker at the beginning of the table last aligned.
  7552. Used to check if cursor still is in that table, to minimize realignment.")
  7553. (defvar org-table-aligned-end-marker (make-marker)
  7554. "Marker at the end of the table last aligned.
  7555. Used to check if cursor still is in that table, to minimize realignment.")
  7556. (defvar org-table-last-alignment nil
  7557. "List of flags for flushright alignment, from the last re-alignment.
  7558. This is being used to correctly align a single field after TAB or RET.")
  7559. (defvar org-table-last-column-widths nil
  7560. "List of max width of fields in each column.
  7561. This is being used to correctly align a single field after TAB or RET.")
  7562. (defvar org-table-overlay-coordinates nil
  7563. "Overlay coordinates after each align of a table.")
  7564. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7565. (defvar org-last-recalc-line nil)
  7566. (defconst org-narrow-column-arrow "=>"
  7567. "Used as display property in narrowed table columns.")
  7568. (defun org-table-align ()
  7569. "Align the table at point by aligning all vertical bars."
  7570. (interactive)
  7571. (let* (
  7572. ;; Limits of table
  7573. (beg (org-table-begin))
  7574. (end (org-table-end))
  7575. ;; Current cursor position
  7576. (linepos (org-current-line))
  7577. (colpos (org-table-current-column))
  7578. (winstart (window-start))
  7579. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7580. lines (new "") lengths l typenums ty fields maxfields i
  7581. column
  7582. (indent "") cnt frac
  7583. rfmt hfmt
  7584. (spaces '(1 . 1))
  7585. (sp1 (car spaces))
  7586. (sp2 (cdr spaces))
  7587. (rfmt1 (concat
  7588. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7589. (hfmt1 (concat
  7590. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7591. emptystrings links dates emph narrow fmax f1 len c e)
  7592. (untabify beg end)
  7593. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7594. ;; Check if we have links or dates
  7595. (goto-char beg)
  7596. (setq links (re-search-forward org-bracket-link-regexp end t))
  7597. (goto-char beg)
  7598. (setq emph (and org-hide-emphasis-markers
  7599. (re-search-forward org-emph-re end t)))
  7600. (goto-char beg)
  7601. (setq dates (and org-display-custom-times
  7602. (re-search-forward org-ts-regexp-both end t)))
  7603. ;; Make sure the link properties are right
  7604. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7605. ;; Make sure the date properties are right
  7606. (when dates (goto-char beg) (while (org-activate-dates end)))
  7607. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7608. ;; Check if we are narrowing any columns
  7609. (goto-char beg)
  7610. (setq narrow (and org-format-transports-properties-p
  7611. (re-search-forward "<[0-9]+>" end t)))
  7612. ;; Get the rows
  7613. (setq lines (org-split-string
  7614. (buffer-substring beg end) "\n"))
  7615. ;; Store the indentation of the first line
  7616. (if (string-match "^ *" (car lines))
  7617. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7618. ;; Mark the hlines by setting the corresponding element to nil
  7619. ;; At the same time, we remove trailing space.
  7620. (setq lines (mapcar (lambda (l)
  7621. (if (string-match "^ *|-" l)
  7622. nil
  7623. (if (string-match "[ \t]+$" l)
  7624. (substring l 0 (match-beginning 0))
  7625. l)))
  7626. lines))
  7627. ;; Get the data fields by splitting the lines.
  7628. (setq fields (mapcar
  7629. (lambda (l)
  7630. (org-split-string l " *| *"))
  7631. (delq nil (copy-sequence lines))))
  7632. ;; How many fields in the longest line?
  7633. (condition-case nil
  7634. (setq maxfields (apply 'max (mapcar 'length fields)))
  7635. (error
  7636. (kill-region beg end)
  7637. (org-table-create org-table-default-size)
  7638. (error "Empty table - created default table")))
  7639. ;; A list of empty strings to fill any short rows on output
  7640. (setq emptystrings (make-list maxfields ""))
  7641. ;; Check for special formatting.
  7642. (setq i -1)
  7643. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7644. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7645. ;; Check if there is an explicit width specified
  7646. (when narrow
  7647. (setq c column fmax nil)
  7648. (while c
  7649. (setq e (pop c))
  7650. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7651. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7652. ;; Find fields that are wider than fmax, and shorten them
  7653. (when fmax
  7654. (loop for xx in column do
  7655. (when (and (stringp xx)
  7656. (> (org-string-width xx) fmax))
  7657. (org-add-props xx nil
  7658. 'help-echo
  7659. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7660. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7661. (unless (> f1 1)
  7662. (error "Cannot narrow field starting with wide link \"%s\""
  7663. (match-string 0 xx)))
  7664. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7665. (add-text-properties (- f1 2) f1
  7666. (list 'display org-narrow-column-arrow)
  7667. xx)))))
  7668. ;; Get the maximum width for each column
  7669. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7670. ;; Get the fraction of numbers, to decide about alignment of the column
  7671. (setq cnt 0 frac 0.0)
  7672. (loop for x in column do
  7673. (if (equal x "")
  7674. nil
  7675. (setq frac ( / (+ (* frac cnt)
  7676. (if (string-match org-table-number-regexp x) 1 0))
  7677. (setq cnt (1+ cnt))))))
  7678. (push (>= frac org-table-number-fraction) typenums))
  7679. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7680. ;; Store the alignment of this table, for later editing of single fields
  7681. (setq org-table-last-alignment typenums
  7682. org-table-last-column-widths lengths)
  7683. ;; With invisible characters, `format' does not get the field width right
  7684. ;; So we need to make these fields wide by hand.
  7685. (when (or links emph)
  7686. (loop for i from 0 upto (1- maxfields) do
  7687. (setq len (nth i lengths))
  7688. (loop for j from 0 upto (1- (length fields)) do
  7689. (setq c (nthcdr i (car (nthcdr j fields))))
  7690. (if (and (stringp (car c))
  7691. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7692. ; (string-match org-bracket-link-regexp (car c))
  7693. (< (org-string-width (car c)) len))
  7694. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7695. ;; Compute the formats needed for output of the table
  7696. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7697. (while (setq l (pop lengths))
  7698. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7699. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7700. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7701. (setq rfmt (concat rfmt "\n")
  7702. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7703. (setq new (mapconcat
  7704. (lambda (l)
  7705. (if l (apply 'format rfmt
  7706. (append (pop fields) emptystrings))
  7707. hfmt))
  7708. lines ""))
  7709. ;; Replace the old one
  7710. (delete-region beg end)
  7711. (move-marker end nil)
  7712. (move-marker org-table-aligned-begin-marker (point))
  7713. (insert new)
  7714. (move-marker org-table-aligned-end-marker (point))
  7715. (when (and orgtbl-mode (not (org-mode-p)))
  7716. (goto-char org-table-aligned-begin-marker)
  7717. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7718. ;; Try to move to the old location
  7719. (goto-line winstartline)
  7720. (setq winstart (point-at-bol))
  7721. (goto-line linepos)
  7722. (set-window-start (selected-window) winstart 'noforce)
  7723. (org-table-goto-column colpos)
  7724. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7725. (setq org-table-may-need-update nil)
  7726. ))
  7727. (defun org-string-width (s)
  7728. "Compute width of string, ignoring invisible characters.
  7729. This ignores character with invisibility property `org-link', and also
  7730. characters with property `org-cwidth', because these will become invisible
  7731. upon the next fontification round."
  7732. (let (b l)
  7733. (when (or (eq t buffer-invisibility-spec)
  7734. (assq 'org-link buffer-invisibility-spec))
  7735. (while (setq b (text-property-any 0 (length s)
  7736. 'invisible 'org-link s))
  7737. (setq s (concat (substring s 0 b)
  7738. (substring s (or (next-single-property-change
  7739. b 'invisible s) (length s)))))))
  7740. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7741. (setq s (concat (substring s 0 b)
  7742. (substring s (or (next-single-property-change
  7743. b 'org-cwidth s) (length s))))))
  7744. (setq l (string-width s) b -1)
  7745. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7746. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7747. l))
  7748. (defun org-table-begin (&optional table-type)
  7749. "Find the beginning of the table and return its position.
  7750. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7751. (save-excursion
  7752. (if (not (re-search-backward
  7753. (if table-type org-table-any-border-regexp
  7754. org-table-border-regexp)
  7755. nil t))
  7756. (progn (goto-char (point-min)) (point))
  7757. (goto-char (match-beginning 0))
  7758. (beginning-of-line 2)
  7759. (point))))
  7760. (defun org-table-end (&optional table-type)
  7761. "Find the end of the table and return its position.
  7762. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7763. (save-excursion
  7764. (if (not (re-search-forward
  7765. (if table-type org-table-any-border-regexp
  7766. org-table-border-regexp)
  7767. nil t))
  7768. (goto-char (point-max))
  7769. (goto-char (match-beginning 0)))
  7770. (point-marker)))
  7771. (defun org-table-justify-field-maybe (&optional new)
  7772. "Justify the current field, text to left, number to right.
  7773. Optional argument NEW may specify text to replace the current field content."
  7774. (cond
  7775. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7776. ((org-at-table-hline-p))
  7777. ((and (not new)
  7778. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7779. (current-buffer)))
  7780. (< (point) org-table-aligned-begin-marker)
  7781. (>= (point) org-table-aligned-end-marker)))
  7782. ;; This is not the same table, force a full re-align
  7783. (setq org-table-may-need-update t))
  7784. (t ;; realign the current field, based on previous full realign
  7785. (let* ((pos (point)) s
  7786. (col (org-table-current-column))
  7787. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7788. l f n o e)
  7789. (when (> col 0)
  7790. (skip-chars-backward "^|\n")
  7791. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7792. (progn
  7793. (setq s (match-string 1)
  7794. o (match-string 0)
  7795. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7796. e (not (= (match-beginning 2) (match-end 2))))
  7797. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7798. l (if e "|" (setq org-table-may-need-update t) ""))
  7799. n (format f s))
  7800. (if new
  7801. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7802. (setq n (format f new))
  7803. (setq n (concat new "|") org-table-may-need-update t)))
  7804. (or (equal n o)
  7805. (let (org-table-may-need-update)
  7806. (replace-match n t t))))
  7807. (setq org-table-may-need-update t))
  7808. (goto-char pos))))))
  7809. (defun org-table-next-field ()
  7810. "Go to the next field in the current table, creating new lines as needed.
  7811. Before doing so, re-align the table if necessary."
  7812. (interactive)
  7813. (org-table-maybe-eval-formula)
  7814. (org-table-maybe-recalculate-line)
  7815. (if (and org-table-automatic-realign
  7816. org-table-may-need-update)
  7817. (org-table-align))
  7818. (let ((end (org-table-end)))
  7819. (if (org-at-table-hline-p)
  7820. (end-of-line 1))
  7821. (condition-case nil
  7822. (progn
  7823. (re-search-forward "|" end)
  7824. (if (looking-at "[ \t]*$")
  7825. (re-search-forward "|" end))
  7826. (if (and (looking-at "-")
  7827. org-table-tab-jumps-over-hlines
  7828. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7829. (goto-char (match-beginning 1)))
  7830. (if (looking-at "-")
  7831. (progn
  7832. (beginning-of-line 0)
  7833. (org-table-insert-row 'below))
  7834. (if (looking-at " ") (forward-char 1))))
  7835. (error
  7836. (org-table-insert-row 'below)))))
  7837. (defun org-table-previous-field ()
  7838. "Go to the previous field in the table.
  7839. Before doing so, re-align the table if necessary."
  7840. (interactive)
  7841. (org-table-justify-field-maybe)
  7842. (org-table-maybe-recalculate-line)
  7843. (if (and org-table-automatic-realign
  7844. org-table-may-need-update)
  7845. (org-table-align))
  7846. (if (org-at-table-hline-p)
  7847. (end-of-line 1))
  7848. (re-search-backward "|" (org-table-begin))
  7849. (re-search-backward "|" (org-table-begin))
  7850. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7851. (re-search-backward "|" (org-table-begin)))
  7852. (if (looking-at "| ?")
  7853. (goto-char (match-end 0))))
  7854. (defun org-table-next-row ()
  7855. "Go to the next row (same column) in the current table.
  7856. Before doing so, re-align the table if necessary."
  7857. (interactive)
  7858. (org-table-maybe-eval-formula)
  7859. (org-table-maybe-recalculate-line)
  7860. (if (or (looking-at "[ \t]*$")
  7861. (save-excursion (skip-chars-backward " \t") (bolp)))
  7862. (newline)
  7863. (if (and org-table-automatic-realign
  7864. org-table-may-need-update)
  7865. (org-table-align))
  7866. (let ((col (org-table-current-column)))
  7867. (beginning-of-line 2)
  7868. (if (or (not (org-at-table-p))
  7869. (org-at-table-hline-p))
  7870. (progn
  7871. (beginning-of-line 0)
  7872. (org-table-insert-row 'below)))
  7873. (org-table-goto-column col)
  7874. (skip-chars-backward "^|\n\r")
  7875. (if (looking-at " ") (forward-char 1)))))
  7876. (defun org-table-copy-down (n)
  7877. "Copy a field down in the current column.
  7878. If the field at the cursor is empty, copy into it the content of the nearest
  7879. non-empty field above. With argument N, use the Nth non-empty field.
  7880. If the current field is not empty, it is copied down to the next row, and
  7881. the cursor is moved with it. Therefore, repeating this command causes the
  7882. column to be filled row-by-row.
  7883. If the variable `org-table-copy-increment' is non-nil and the field is an
  7884. integer or a timestamp, it will be incremented while copying. In the case of
  7885. a timestamp, if the cursor is on the year, change the year. If it is on the
  7886. month or the day, change that. Point will stay on the current date field
  7887. in order to easily repeat the interval."
  7888. (interactive "p")
  7889. (let* ((colpos (org-table-current-column))
  7890. (col (current-column))
  7891. (field (org-table-get-field))
  7892. (non-empty (string-match "[^ \t]" field))
  7893. (beg (org-table-begin))
  7894. txt)
  7895. (org-table-check-inside-data-field)
  7896. (if non-empty
  7897. (progn
  7898. (setq txt (org-trim field))
  7899. (org-table-next-row)
  7900. (org-table-blank-field))
  7901. (save-excursion
  7902. (setq txt
  7903. (catch 'exit
  7904. (while (progn (beginning-of-line 1)
  7905. (re-search-backward org-table-dataline-regexp
  7906. beg t))
  7907. (org-table-goto-column colpos t)
  7908. (if (and (looking-at
  7909. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7910. (= (setq n (1- n)) 0))
  7911. (throw 'exit (match-string 1))))))))
  7912. (if txt
  7913. (progn
  7914. (if (and org-table-copy-increment
  7915. (string-match "^[0-9]+$" txt))
  7916. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7917. (insert txt)
  7918. (move-to-column col)
  7919. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7920. (org-timestamp-up 1)
  7921. (org-table-maybe-recalculate-line))
  7922. (org-table-align)
  7923. (move-to-column col))
  7924. (error "No non-empty field found"))))
  7925. (defun org-table-check-inside-data-field ()
  7926. "Is point inside a table data field?
  7927. I.e. not on a hline or before the first or after the last column?
  7928. This actually throws an error, so it aborts the current command."
  7929. (if (or (not (org-at-table-p))
  7930. (= (org-table-current-column) 0)
  7931. (org-at-table-hline-p)
  7932. (looking-at "[ \t]*$"))
  7933. (error "Not in table data field")))
  7934. (defvar org-table-clip nil
  7935. "Clipboard for table regions.")
  7936. (defun org-table-blank-field ()
  7937. "Blank the current table field or active region."
  7938. (interactive)
  7939. (org-table-check-inside-data-field)
  7940. (if (and (interactive-p) (org-region-active-p))
  7941. (let (org-table-clip)
  7942. (org-table-cut-region (region-beginning) (region-end)))
  7943. (skip-chars-backward "^|")
  7944. (backward-char 1)
  7945. (if (looking-at "|[^|\n]+")
  7946. (let* ((pos (match-beginning 0))
  7947. (match (match-string 0))
  7948. (len (org-string-width match)))
  7949. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7950. (goto-char (+ 2 pos))
  7951. (substring match 1)))))
  7952. (defun org-table-get-field (&optional n replace)
  7953. "Return the value of the field in column N of current row.
  7954. N defaults to current field.
  7955. If REPLACE is a string, replace field with this value. The return value
  7956. is always the old value."
  7957. (and n (org-table-goto-column n))
  7958. (skip-chars-backward "^|\n")
  7959. (backward-char 1)
  7960. (if (looking-at "|[^|\r\n]*")
  7961. (let* ((pos (match-beginning 0))
  7962. (val (buffer-substring (1+ pos) (match-end 0))))
  7963. (if replace
  7964. (replace-match (concat "|" replace) t t))
  7965. (goto-char (min (point-at-eol) (+ 2 pos)))
  7966. val)
  7967. (forward-char 1) ""))
  7968. (defun org-table-field-info (arg)
  7969. "Show info about the current field, and highlight any reference at point."
  7970. (interactive "P")
  7971. (org-table-get-specials)
  7972. (save-excursion
  7973. (let* ((pos (point))
  7974. (col (org-table-current-column))
  7975. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7976. (name (car (rassoc (list (org-current-line) col)
  7977. org-table-named-field-locations)))
  7978. (eql (org-table-get-stored-formulas))
  7979. (dline (org-table-current-dline))
  7980. (ref (format "@%d$%d" dline col))
  7981. (ref1 (org-table-convert-refs-to-an ref))
  7982. (fequation (or (assoc name eql) (assoc ref eql)))
  7983. (cequation (assoc (int-to-string col) eql))
  7984. (eqn (or fequation cequation)))
  7985. (goto-char pos)
  7986. (condition-case nil
  7987. (org-table-show-reference 'local)
  7988. (error nil))
  7989. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7990. dline col
  7991. (if cname (concat " or $" cname) "")
  7992. dline col ref1
  7993. (if name (concat " or $" name) "")
  7994. ;; FIXME: formula info not correct if special table line
  7995. (if eqn
  7996. (concat ", formula: "
  7997. (org-table-formula-to-user
  7998. (concat
  7999. (if (string-match "^[$@]"(car eqn)) "" "$")
  8000. (car eqn) "=" (cdr eqn))))
  8001. "")))))
  8002. (defun org-table-current-column ()
  8003. "Find out which column we are in.
  8004. When called interactively, column is also displayed in echo area."
  8005. (interactive)
  8006. (if (interactive-p) (org-table-check-inside-data-field))
  8007. (save-excursion
  8008. (let ((cnt 0) (pos (point)))
  8009. (beginning-of-line 1)
  8010. (while (search-forward "|" pos t)
  8011. (setq cnt (1+ cnt)))
  8012. (if (interactive-p) (message "This is table column %d" cnt))
  8013. cnt)))
  8014. (defun org-table-current-dline ()
  8015. "Find out what table data line we are in.
  8016. Only datalins count for this."
  8017. (interactive)
  8018. (if (interactive-p) (org-table-check-inside-data-field))
  8019. (save-excursion
  8020. (let ((cnt 0) (pos (point)))
  8021. (goto-char (org-table-begin))
  8022. (while (<= (point) pos)
  8023. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8024. (beginning-of-line 2))
  8025. (if (interactive-p) (message "This is table line %d" cnt))
  8026. cnt)))
  8027. (defun org-table-goto-column (n &optional on-delim force)
  8028. "Move the cursor to the Nth column in the current table line.
  8029. With optional argument ON-DELIM, stop with point before the left delimiter
  8030. of the field.
  8031. If there are less than N fields, just go to after the last delimiter.
  8032. However, when FORCE is non-nil, create new columns if necessary."
  8033. (interactive "p")
  8034. (let ((pos (point-at-eol)))
  8035. (beginning-of-line 1)
  8036. (when (> n 0)
  8037. (while (and (> (setq n (1- n)) -1)
  8038. (or (search-forward "|" pos t)
  8039. (and force
  8040. (progn (end-of-line 1)
  8041. (skip-chars-backward "^|")
  8042. (insert " | "))))))
  8043. ; (backward-char 2) t)))))
  8044. (when (and force (not (looking-at ".*|")))
  8045. (save-excursion (end-of-line 1) (insert " | ")))
  8046. (if on-delim
  8047. (backward-char 1)
  8048. (if (looking-at " ") (forward-char 1))))))
  8049. (defun org-at-table-p (&optional table-type)
  8050. "Return t if the cursor is inside an org-type table.
  8051. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8052. (if org-enable-table-editor
  8053. (save-excursion
  8054. (beginning-of-line 1)
  8055. (looking-at (if table-type org-table-any-line-regexp
  8056. org-table-line-regexp)))
  8057. nil))
  8058. (defun org-at-table.el-p ()
  8059. "Return t if and only if we are at a table.el table."
  8060. (and (org-at-table-p 'any)
  8061. (save-excursion
  8062. (goto-char (org-table-begin 'any))
  8063. (looking-at org-table1-hline-regexp))))
  8064. (defun org-table-recognize-table.el ()
  8065. "If there is a table.el table nearby, recognize it and move into it."
  8066. (if org-table-tab-recognizes-table.el
  8067. (if (org-at-table.el-p)
  8068. (progn
  8069. (beginning-of-line 1)
  8070. (if (looking-at org-table-dataline-regexp)
  8071. nil
  8072. (if (looking-at org-table1-hline-regexp)
  8073. (progn
  8074. (beginning-of-line 2)
  8075. (if (looking-at org-table-any-border-regexp)
  8076. (beginning-of-line -1)))))
  8077. (if (re-search-forward "|" (org-table-end t) t)
  8078. (progn
  8079. (require 'table)
  8080. (if (table--at-cell-p (point))
  8081. t
  8082. (message "recognizing table.el table...")
  8083. (table-recognize-table)
  8084. (message "recognizing table.el table...done")))
  8085. (error "This should not happen..."))
  8086. t)
  8087. nil)
  8088. nil))
  8089. (defun org-at-table-hline-p ()
  8090. "Return t if the cursor is inside a hline in a table."
  8091. (if org-enable-table-editor
  8092. (save-excursion
  8093. (beginning-of-line 1)
  8094. (looking-at org-table-hline-regexp))
  8095. nil))
  8096. (defun org-table-insert-column ()
  8097. "Insert a new column into the table."
  8098. (interactive)
  8099. (if (not (org-at-table-p))
  8100. (error "Not at a table"))
  8101. (org-table-find-dataline)
  8102. (let* ((col (max 1 (org-table-current-column)))
  8103. (beg (org-table-begin))
  8104. (end (org-table-end))
  8105. ;; Current cursor position
  8106. (linepos (org-current-line))
  8107. (colpos col))
  8108. (goto-char beg)
  8109. (while (< (point) end)
  8110. (if (org-at-table-hline-p)
  8111. nil
  8112. (org-table-goto-column col t)
  8113. (insert "| "))
  8114. (beginning-of-line 2))
  8115. (move-marker end nil)
  8116. (goto-line linepos)
  8117. (org-table-goto-column colpos)
  8118. (org-table-align)
  8119. (org-table-fix-formulas "$" nil (1- col) 1)))
  8120. (defun org-table-find-dataline ()
  8121. "Find a dataline in the current table, which is needed for column commands."
  8122. (if (and (org-at-table-p)
  8123. (not (org-at-table-hline-p)))
  8124. t
  8125. (let ((col (current-column))
  8126. (end (org-table-end)))
  8127. (move-to-column col)
  8128. (while (and (< (point) end)
  8129. (or (not (= (current-column) col))
  8130. (org-at-table-hline-p)))
  8131. (beginning-of-line 2)
  8132. (move-to-column col))
  8133. (if (and (org-at-table-p)
  8134. (not (org-at-table-hline-p)))
  8135. t
  8136. (error
  8137. "Please position cursor in a data line for column operations")))))
  8138. (defun org-table-delete-column ()
  8139. "Delete a column from the table."
  8140. (interactive)
  8141. (if (not (org-at-table-p))
  8142. (error "Not at a table"))
  8143. (org-table-find-dataline)
  8144. (org-table-check-inside-data-field)
  8145. (let* ((col (org-table-current-column))
  8146. (beg (org-table-begin))
  8147. (end (org-table-end))
  8148. ;; Current cursor position
  8149. (linepos (org-current-line))
  8150. (colpos col))
  8151. (goto-char beg)
  8152. (while (< (point) end)
  8153. (if (org-at-table-hline-p)
  8154. nil
  8155. (org-table-goto-column col t)
  8156. (and (looking-at "|[^|\n]+|")
  8157. (replace-match "|")))
  8158. (beginning-of-line 2))
  8159. (move-marker end nil)
  8160. (goto-line linepos)
  8161. (org-table-goto-column colpos)
  8162. (org-table-align)
  8163. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8164. col -1 col)))
  8165. (defun org-table-move-column-right ()
  8166. "Move column to the right."
  8167. (interactive)
  8168. (org-table-move-column nil))
  8169. (defun org-table-move-column-left ()
  8170. "Move column to the left."
  8171. (interactive)
  8172. (org-table-move-column 'left))
  8173. (defun org-table-move-column (&optional left)
  8174. "Move the current column to the right. With arg LEFT, move to the left."
  8175. (interactive "P")
  8176. (if (not (org-at-table-p))
  8177. (error "Not at a table"))
  8178. (org-table-find-dataline)
  8179. (org-table-check-inside-data-field)
  8180. (let* ((col (org-table-current-column))
  8181. (col1 (if left (1- col) col))
  8182. (beg (org-table-begin))
  8183. (end (org-table-end))
  8184. ;; Current cursor position
  8185. (linepos (org-current-line))
  8186. (colpos (if left (1- col) (1+ col))))
  8187. (if (and left (= col 1))
  8188. (error "Cannot move column further left"))
  8189. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8190. (error "Cannot move column further right"))
  8191. (goto-char beg)
  8192. (while (< (point) end)
  8193. (if (org-at-table-hline-p)
  8194. nil
  8195. (org-table-goto-column col1 t)
  8196. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8197. (replace-match "|\\2|\\1|")))
  8198. (beginning-of-line 2))
  8199. (move-marker end nil)
  8200. (goto-line linepos)
  8201. (org-table-goto-column colpos)
  8202. (org-table-align)
  8203. (org-table-fix-formulas
  8204. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8205. (cons (number-to-string colpos) (number-to-string col))))))
  8206. (defun org-table-move-row-down ()
  8207. "Move table row down."
  8208. (interactive)
  8209. (org-table-move-row nil))
  8210. (defun org-table-move-row-up ()
  8211. "Move table row up."
  8212. (interactive)
  8213. (org-table-move-row 'up))
  8214. (defun org-table-move-row (&optional up)
  8215. "Move the current table line down. With arg UP, move it up."
  8216. (interactive "P")
  8217. (let* ((col (current-column))
  8218. (pos (point))
  8219. (hline1p (save-excursion (beginning-of-line 1)
  8220. (looking-at org-table-hline-regexp)))
  8221. (dline1 (org-table-current-dline))
  8222. (dline2 (+ dline1 (if up -1 1)))
  8223. (tonew (if up 0 2))
  8224. txt hline2p)
  8225. (beginning-of-line tonew)
  8226. (unless (org-at-table-p)
  8227. (goto-char pos)
  8228. (error "Cannot move row further"))
  8229. (setq hline2p (looking-at org-table-hline-regexp))
  8230. (goto-char pos)
  8231. (beginning-of-line 1)
  8232. (setq pos (point))
  8233. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8234. (delete-region (point) (1+ (point-at-eol)))
  8235. (beginning-of-line tonew)
  8236. (insert txt)
  8237. (beginning-of-line 0)
  8238. (move-to-column col)
  8239. (unless (or hline1p hline2p)
  8240. (org-table-fix-formulas
  8241. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8242. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8243. (defun org-table-insert-row (&optional arg)
  8244. "Insert a new row above the current line into the table.
  8245. With prefix ARG, insert below the current line."
  8246. (interactive "P")
  8247. (if (not (org-at-table-p))
  8248. (error "Not at a table"))
  8249. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8250. (new (org-table-clean-line line)))
  8251. ;; Fix the first field if necessary
  8252. (if (string-match "^[ \t]*| *[#$] *|" line)
  8253. (setq new (replace-match (match-string 0 line) t t new)))
  8254. (beginning-of-line (if arg 2 1))
  8255. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8256. (beginning-of-line 0)
  8257. (re-search-forward "| ?" (point-at-eol) t)
  8258. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8259. (org-table-align))
  8260. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8261. (defun org-table-insert-hline (&optional above)
  8262. "Insert a horizontal-line below the current line into the table.
  8263. With prefix ABOVE, insert above the current line."
  8264. (interactive "P")
  8265. (if (not (org-at-table-p))
  8266. (error "Not at a table"))
  8267. (let ((line (org-table-clean-line
  8268. (buffer-substring (point-at-bol) (point-at-eol))))
  8269. (col (current-column)))
  8270. (while (string-match "|\\( +\\)|" line)
  8271. (setq line (replace-match
  8272. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8273. ?-) "|") t t line)))
  8274. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8275. (beginning-of-line (if above 1 2))
  8276. (insert line "\n")
  8277. (beginning-of-line (if above 1 -1))
  8278. (move-to-column col)
  8279. (and org-table-overlay-coordinates (org-table-align))))
  8280. (defun org-table-hline-and-move (&optional same-column)
  8281. "Insert a hline and move to the row below that line."
  8282. (interactive "P")
  8283. (let ((col (org-table-current-column)))
  8284. (org-table-maybe-eval-formula)
  8285. (org-table-maybe-recalculate-line)
  8286. (org-table-insert-hline)
  8287. (end-of-line 2)
  8288. (if (looking-at "\n[ \t]*|-")
  8289. (progn (insert "\n|") (org-table-align))
  8290. (org-table-next-field))
  8291. (if same-column (org-table-goto-column col))))
  8292. (defun org-table-clean-line (s)
  8293. "Convert a table line S into a string with only \"|\" and space.
  8294. In particular, this does handle wide and invisible characters."
  8295. (if (string-match "^[ \t]*|-" s)
  8296. ;; It's a hline, just map the characters
  8297. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8298. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8299. (setq s (replace-match
  8300. (concat "|" (make-string (org-string-width (match-string 1 s))
  8301. ?\ ) "|")
  8302. t t s)))
  8303. s))
  8304. (defun org-table-kill-row ()
  8305. "Delete the current row or horizontal line from the table."
  8306. (interactive)
  8307. (if (not (org-at-table-p))
  8308. (error "Not at a table"))
  8309. (let ((col (current-column))
  8310. (dline (org-table-current-dline)))
  8311. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8312. (if (not (org-at-table-p)) (beginning-of-line 0))
  8313. (move-to-column col)
  8314. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8315. dline -1 dline)))
  8316. (defun org-table-sort-lines (with-case &optional sorting-type)
  8317. "Sort table lines according to the column at point.
  8318. The position of point indicates the column to be used for
  8319. sorting, and the range of lines is the range between the nearest
  8320. horizontal separator lines, or the entire table of no such lines
  8321. exist. If point is before the first column, you will be prompted
  8322. for the sorting column. If there is an active region, the mark
  8323. specifies the first line and the sorting column, while point
  8324. should be in the last line to be included into the sorting.
  8325. The command then prompts for the sorting type which can be
  8326. alphabetically, numerically, or by time (as given in a time stamp
  8327. in the field). Sorting in reverse order is also possible.
  8328. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8329. If SORTING-TYPE is specified when this function is called from a Lisp
  8330. program, no prompting will take place. SORTING-TYPE must be a character,
  8331. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8332. should be done in reverse order."
  8333. (interactive "P")
  8334. (let* ((thisline (org-current-line))
  8335. (thiscol (org-table-current-column))
  8336. beg end bcol ecol tend tbeg column lns pos)
  8337. (when (equal thiscol 0)
  8338. (if (interactive-p)
  8339. (setq thiscol
  8340. (string-to-number
  8341. (read-string "Use column N for sorting: ")))
  8342. (setq thiscol 1))
  8343. (org-table-goto-column thiscol))
  8344. (org-table-check-inside-data-field)
  8345. (if (org-region-active-p)
  8346. (progn
  8347. (setq beg (region-beginning) end (region-end))
  8348. (goto-char beg)
  8349. (setq column (org-table-current-column)
  8350. beg (point-at-bol))
  8351. (goto-char end)
  8352. (setq end (point-at-bol 2)))
  8353. (setq column (org-table-current-column)
  8354. pos (point)
  8355. tbeg (org-table-begin)
  8356. tend (org-table-end))
  8357. (if (re-search-backward org-table-hline-regexp tbeg t)
  8358. (setq beg (point-at-bol 2))
  8359. (goto-char tbeg)
  8360. (setq beg (point-at-bol 1)))
  8361. (goto-char pos)
  8362. (if (re-search-forward org-table-hline-regexp tend t)
  8363. (setq end (point-at-bol 1))
  8364. (goto-char tend)
  8365. (setq end (point-at-bol))))
  8366. (setq beg (move-marker (make-marker) beg)
  8367. end (move-marker (make-marker) end))
  8368. (untabify beg end)
  8369. (goto-char beg)
  8370. (org-table-goto-column column)
  8371. (skip-chars-backward "^|")
  8372. (setq bcol (current-column))
  8373. (org-table-goto-column (1+ column))
  8374. (skip-chars-backward "^|")
  8375. (setq ecol (1- (current-column)))
  8376. (org-table-goto-column column)
  8377. (setq lns (mapcar (lambda(x) (cons
  8378. (org-sort-remove-invisible
  8379. (nth (1- column)
  8380. (org-split-string x "[ \t]*|[ \t]*")))
  8381. x))
  8382. (org-split-string (buffer-substring beg end) "\n")))
  8383. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8384. (delete-region beg end)
  8385. (move-marker beg nil)
  8386. (move-marker end nil)
  8387. (insert (mapconcat 'cdr lns "\n") "\n")
  8388. (goto-line thisline)
  8389. (org-table-goto-column thiscol)
  8390. (message "%d lines sorted, based on column %d" (length lns) column)))
  8391. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8392. (defun org-sort-remove-invisible (s)
  8393. (remove-text-properties 0 (length s) org-rm-props s)
  8394. (while (string-match org-bracket-link-regexp s)
  8395. (setq s (replace-match (if (match-end 2)
  8396. (match-string 3 s)
  8397. (match-string 1 s)) t t s)))
  8398. s)
  8399. (defun org-table-cut-region (beg end)
  8400. "Copy region in table to the clipboard and blank all relevant fields."
  8401. (interactive "r")
  8402. (org-table-copy-region beg end 'cut))
  8403. (defun org-table-copy-region (beg end &optional cut)
  8404. "Copy rectangular region in table to clipboard.
  8405. A special clipboard is used which can only be accessed
  8406. with `org-table-paste-rectangle'."
  8407. (interactive "rP")
  8408. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8409. region cols
  8410. (rpl (if cut " " nil)))
  8411. (goto-char beg)
  8412. (org-table-check-inside-data-field)
  8413. (setq l01 (org-current-line)
  8414. c01 (org-table-current-column))
  8415. (goto-char end)
  8416. (org-table-check-inside-data-field)
  8417. (setq l02 (org-current-line)
  8418. c02 (org-table-current-column))
  8419. (setq l1 (min l01 l02) l2 (max l01 l02)
  8420. c1 (min c01 c02) c2 (max c01 c02))
  8421. (catch 'exit
  8422. (while t
  8423. (catch 'nextline
  8424. (if (> l1 l2) (throw 'exit t))
  8425. (goto-line l1)
  8426. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8427. (setq cols nil ic1 c1 ic2 c2)
  8428. (while (< ic1 (1+ ic2))
  8429. (push (org-table-get-field ic1 rpl) cols)
  8430. (setq ic1 (1+ ic1)))
  8431. (push (nreverse cols) region)
  8432. (setq l1 (1+ l1)))))
  8433. (setq org-table-clip (nreverse region))
  8434. (if cut (org-table-align))
  8435. org-table-clip))
  8436. (defun org-table-paste-rectangle ()
  8437. "Paste a rectangular region into a table.
  8438. The upper right corner ends up in the current field. All involved fields
  8439. will be overwritten. If the rectangle does not fit into the present table,
  8440. the table is enlarged as needed. The process ignores horizontal separator
  8441. lines."
  8442. (interactive)
  8443. (unless (and org-table-clip (listp org-table-clip))
  8444. (error "First cut/copy a region to paste!"))
  8445. (org-table-check-inside-data-field)
  8446. (let* ((clip org-table-clip)
  8447. (line (org-current-line))
  8448. (col (org-table-current-column))
  8449. (org-enable-table-editor t)
  8450. (org-table-automatic-realign nil)
  8451. c cols field)
  8452. (while (setq cols (pop clip))
  8453. (while (org-at-table-hline-p) (beginning-of-line 2))
  8454. (if (not (org-at-table-p))
  8455. (progn (end-of-line 0) (org-table-next-field)))
  8456. (setq c col)
  8457. (while (setq field (pop cols))
  8458. (org-table-goto-column c nil 'force)
  8459. (org-table-get-field nil field)
  8460. (setq c (1+ c)))
  8461. (beginning-of-line 2))
  8462. (goto-line line)
  8463. (org-table-goto-column col)
  8464. (org-table-align)))
  8465. (defun org-table-convert ()
  8466. "Convert from `org-mode' table to table.el and back.
  8467. Obviously, this only works within limits. When an Org-mode table is
  8468. converted to table.el, all horizontal separator lines get lost, because
  8469. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8470. A table.el table can be converted to an Org-mode table only if it does not
  8471. do row or column spanning. Multiline cells will become multiple cells.
  8472. Beware, Org-mode does not test if the table can be successfully converted - it
  8473. blindly applies a recipe that works for simple tables."
  8474. (interactive)
  8475. (require 'table)
  8476. (if (org-at-table.el-p)
  8477. ;; convert to Org-mode table
  8478. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8479. (end (move-marker (make-marker) (org-table-end t))))
  8480. (table-unrecognize-region beg end)
  8481. (goto-char beg)
  8482. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8483. (replace-match ""))
  8484. (goto-char beg))
  8485. (if (org-at-table-p)
  8486. ;; convert to table.el table
  8487. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8488. (end (move-marker (make-marker) (org-table-end))))
  8489. ;; first, get rid of all horizontal lines
  8490. (goto-char beg)
  8491. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8492. (replace-match ""))
  8493. ;; insert a hline before first
  8494. (goto-char beg)
  8495. (org-table-insert-hline 'above)
  8496. (beginning-of-line -1)
  8497. ;; insert a hline after each line
  8498. (while (progn (beginning-of-line 3) (< (point) end))
  8499. (org-table-insert-hline))
  8500. (goto-char beg)
  8501. (setq end (move-marker end (org-table-end)))
  8502. ;; replace "+" at beginning and ending of hlines
  8503. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8504. (replace-match "\\1+-"))
  8505. (goto-char beg)
  8506. (while (re-search-forward "-|[ \t]*$" end t)
  8507. (replace-match "-+"))
  8508. (goto-char beg)))))
  8509. (defun org-table-wrap-region (arg)
  8510. "Wrap several fields in a column like a paragraph.
  8511. This is useful if you'd like to spread the contents of a field over several
  8512. lines, in order to keep the table compact.
  8513. If there is an active region, and both point and mark are in the same column,
  8514. the text in the column is wrapped to minimum width for the given number of
  8515. lines. Generally, this makes the table more compact. A prefix ARG may be
  8516. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8517. formats the selected text to two lines. If the region was longer than two
  8518. lines, the remaining lines remain empty. A negative prefix argument reduces
  8519. the current number of lines by that amount. The wrapped text is pasted back
  8520. into the table. If you formatted it to more lines than it was before, fields
  8521. further down in the table get overwritten - so you might need to make space in
  8522. the table first.
  8523. If there is no region, the current field is split at the cursor position and
  8524. the text fragment to the right of the cursor is prepended to the field one
  8525. line down.
  8526. If there is no region, but you specify a prefix ARG, the current field gets
  8527. blank, and the content is appended to the field above."
  8528. (interactive "P")
  8529. (org-table-check-inside-data-field)
  8530. (if (org-region-active-p)
  8531. ;; There is a region: fill as a paragraph
  8532. (let* ((beg (region-beginning))
  8533. (cline (save-excursion (goto-char beg) (org-current-line)))
  8534. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8535. nlines)
  8536. (org-table-cut-region (region-beginning) (region-end))
  8537. (if (> (length (car org-table-clip)) 1)
  8538. (error "Region must be limited to single column"))
  8539. (setq nlines (if arg
  8540. (if (< arg 1)
  8541. (+ (length org-table-clip) arg)
  8542. arg)
  8543. (length org-table-clip)))
  8544. (setq org-table-clip
  8545. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8546. nil nlines)))
  8547. (goto-line cline)
  8548. (org-table-goto-column ccol)
  8549. (org-table-paste-rectangle))
  8550. ;; No region, split the current field at point
  8551. (if arg
  8552. ;; combine with field above
  8553. (let ((s (org-table-blank-field))
  8554. (col (org-table-current-column)))
  8555. (beginning-of-line 0)
  8556. (while (org-at-table-hline-p) (beginning-of-line 0))
  8557. (org-table-goto-column col)
  8558. (skip-chars-forward "^|")
  8559. (skip-chars-backward " ")
  8560. (insert " " (org-trim s))
  8561. (org-table-align))
  8562. ;; split field
  8563. (when (looking-at "\\([^|]+\\)+|")
  8564. (let ((s (match-string 1)))
  8565. (replace-match " |")
  8566. (goto-char (match-beginning 0))
  8567. (org-table-next-row)
  8568. (insert (org-trim s) " ")
  8569. (org-table-align))))))
  8570. (defvar org-field-marker nil)
  8571. (defun org-table-edit-field (arg)
  8572. "Edit table field in a different window.
  8573. This is mainly useful for fields that contain hidden parts.
  8574. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8575. it can be edited in place."
  8576. (interactive "P")
  8577. (if arg
  8578. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8579. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8580. (remove-text-properties b e '(org-cwidth t invisible t
  8581. display t intangible t))
  8582. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8583. (font-lock-fontify-block)))
  8584. (let ((pos (move-marker (make-marker) (point)))
  8585. (field (org-table-get-field))
  8586. (cw (current-window-configuration))
  8587. p)
  8588. (org-switch-to-buffer-other-window "*Org tmp*")
  8589. (erase-buffer)
  8590. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8591. (let ((org-inhibit-startup t)) (org-mode))
  8592. (goto-char (setq p (point-max)))
  8593. (insert (org-trim field))
  8594. (remove-text-properties p (point-max)
  8595. '(invisible t org-cwidth t display t
  8596. intangible t))
  8597. (goto-char p)
  8598. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8599. (org-set-local 'org-window-configuration cw)
  8600. (org-set-local 'org-field-marker pos)
  8601. (message "Edit and finish with C-c C-c"))))
  8602. (defun org-table-finish-edit-field ()
  8603. "Finish editing a table data field.
  8604. Remove all newline characters, insert the result into the table, realign
  8605. the table and kill the editing buffer."
  8606. (let ((pos org-field-marker)
  8607. (cw org-window-configuration)
  8608. (cb (current-buffer))
  8609. text)
  8610. (goto-char (point-min))
  8611. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8612. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8613. (replace-match " "))
  8614. (setq text (org-trim (buffer-string)))
  8615. (set-window-configuration cw)
  8616. (kill-buffer cb)
  8617. (select-window (get-buffer-window (marker-buffer pos)))
  8618. (goto-char pos)
  8619. (move-marker pos nil)
  8620. (org-table-check-inside-data-field)
  8621. (org-table-get-field nil text)
  8622. (org-table-align)
  8623. (message "New field value inserted")))
  8624. (defun org-trim (s)
  8625. "Remove whitespace at beginning and end of string."
  8626. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8627. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8628. s)
  8629. (defun org-wrap (string &optional width lines)
  8630. "Wrap string to either a number of lines, or a width in characters.
  8631. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8632. that costs. If there is a word longer than WIDTH, the text is actually
  8633. wrapped to the length of that word.
  8634. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8635. many lines, whatever width that takes.
  8636. The return value is a list of lines, without newlines at the end."
  8637. (let* ((words (org-split-string string "[ \t\n]+"))
  8638. (maxword (apply 'max (mapcar 'org-string-width words)))
  8639. w ll)
  8640. (cond (width
  8641. (org-do-wrap words (max maxword width)))
  8642. (lines
  8643. (setq w maxword)
  8644. (setq ll (org-do-wrap words maxword))
  8645. (if (<= (length ll) lines)
  8646. ll
  8647. (setq ll words)
  8648. (while (> (length ll) lines)
  8649. (setq w (1+ w))
  8650. (setq ll (org-do-wrap words w)))
  8651. ll))
  8652. (t (error "Cannot wrap this")))))
  8653. (defun org-do-wrap (words width)
  8654. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8655. (let (lines line)
  8656. (while words
  8657. (setq line (pop words))
  8658. (while (and words (< (+ (length line) (length (car words))) width))
  8659. (setq line (concat line " " (pop words))))
  8660. (setq lines (push line lines)))
  8661. (nreverse lines)))
  8662. (defun org-split-string (string &optional separators)
  8663. "Splits STRING into substrings at SEPARATORS.
  8664. No empty strings are returned if there are matches at the beginning
  8665. and end of string."
  8666. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8667. (start 0)
  8668. notfirst
  8669. (list nil))
  8670. (while (and (string-match rexp string
  8671. (if (and notfirst
  8672. (= start (match-beginning 0))
  8673. (< start (length string)))
  8674. (1+ start) start))
  8675. (< (match-beginning 0) (length string)))
  8676. (setq notfirst t)
  8677. (or (eq (match-beginning 0) 0)
  8678. (and (eq (match-beginning 0) (match-end 0))
  8679. (eq (match-beginning 0) start))
  8680. (setq list
  8681. (cons (substring string start (match-beginning 0))
  8682. list)))
  8683. (setq start (match-end 0)))
  8684. (or (eq start (length string))
  8685. (setq list
  8686. (cons (substring string start)
  8687. list)))
  8688. (nreverse list)))
  8689. (defun org-table-map-tables (function)
  8690. "Apply FUNCTION to the start of all tables in the buffer."
  8691. (save-excursion
  8692. (save-restriction
  8693. (widen)
  8694. (goto-char (point-min))
  8695. (while (re-search-forward org-table-any-line-regexp nil t)
  8696. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8697. (beginning-of-line 1)
  8698. (if (looking-at org-table-line-regexp)
  8699. (save-excursion (funcall function)))
  8700. (re-search-forward org-table-any-border-regexp nil 1))))
  8701. (message "Mapping tables: done"))
  8702. (defvar org-timecnt) ; dynamically scoped parameter
  8703. (defun org-table-sum (&optional beg end nlast)
  8704. "Sum numbers in region of current table column.
  8705. The result will be displayed in the echo area, and will be available
  8706. as kill to be inserted with \\[yank].
  8707. If there is an active region, it is interpreted as a rectangle and all
  8708. numbers in that rectangle will be summed. If there is no active
  8709. region and point is located in a table column, sum all numbers in that
  8710. column.
  8711. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8712. numbers are assumed to be times as well (in decimal hours) and the
  8713. numbers are added as such.
  8714. If NLAST is a number, only the NLAST fields will actually be summed."
  8715. (interactive)
  8716. (save-excursion
  8717. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8718. (cond
  8719. ((and beg end)) ; beg and end given explicitly
  8720. ((org-region-active-p)
  8721. (setq beg (region-beginning) end (region-end)))
  8722. (t
  8723. (setq col (org-table-current-column))
  8724. (goto-char (org-table-begin))
  8725. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8726. (error "No table data"))
  8727. (org-table-goto-column col)
  8728. (setq beg (point))
  8729. (goto-char (org-table-end))
  8730. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8731. (error "No table data"))
  8732. (org-table-goto-column col)
  8733. (setq end (point))))
  8734. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8735. (items1 (cond ((not nlast) items)
  8736. ((>= nlast (length items)) items)
  8737. (t (setq items (reverse items))
  8738. (setcdr (nthcdr (1- nlast) items) nil)
  8739. (nreverse items))))
  8740. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8741. items1)))
  8742. (res (apply '+ numbers))
  8743. (sres (if (= org-timecnt 0)
  8744. (format "%g" res)
  8745. (setq diff (* 3600 res)
  8746. h (floor (/ diff 3600)) diff (mod diff 3600)
  8747. m (floor (/ diff 60)) diff (mod diff 60)
  8748. s diff)
  8749. (format "%d:%02d:%02d" h m s))))
  8750. (kill-new sres)
  8751. (if (interactive-p)
  8752. (message "%s"
  8753. (substitute-command-keys
  8754. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8755. (length numbers) sres))))
  8756. sres))))
  8757. (defun org-table-get-number-for-summing (s)
  8758. (let (n)
  8759. (if (string-match "^ *|? *" s)
  8760. (setq s (replace-match "" nil nil s)))
  8761. (if (string-match " *|? *$" s)
  8762. (setq s (replace-match "" nil nil s)))
  8763. (setq n (string-to-number s))
  8764. (cond
  8765. ((and (string-match "0" s)
  8766. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8767. ((string-match "\\`[ \t]+\\'" s) nil)
  8768. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8769. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8770. (m (string-to-number (or (match-string 2 s) "0")))
  8771. (s (string-to-number (or (match-string 4 s) "0"))))
  8772. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8773. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8774. ((equal n 0) nil)
  8775. (t n))))
  8776. (defun org-table-current-field-formula (&optional key noerror)
  8777. "Return the formula active for the current field.
  8778. Assumes that specials are in place.
  8779. If KEY is given, return the key to this formula.
  8780. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8781. (let* ((name (car (rassoc (list (org-current-line)
  8782. (org-table-current-column))
  8783. org-table-named-field-locations)))
  8784. (col (org-table-current-column))
  8785. (scol (int-to-string col))
  8786. (ref (format "@%d$%d" (org-table-current-dline) col))
  8787. (stored-list (org-table-get-stored-formulas noerror))
  8788. (ass (or (assoc name stored-list)
  8789. (assoc ref stored-list)
  8790. (assoc scol stored-list))))
  8791. (if key
  8792. (car ass)
  8793. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8794. (cdr ass))))))
  8795. (defun org-table-get-formula (&optional equation named)
  8796. "Read a formula from the minibuffer, offer stored formula as default.
  8797. When NAMED is non-nil, look for a named equation."
  8798. (let* ((stored-list (org-table-get-stored-formulas))
  8799. (name (car (rassoc (list (org-current-line)
  8800. (org-table-current-column))
  8801. org-table-named-field-locations)))
  8802. (ref (format "@%d$%d" (org-table-current-dline)
  8803. (org-table-current-column)))
  8804. (refass (assoc ref stored-list))
  8805. (scol (if named
  8806. (if name name ref)
  8807. (int-to-string (org-table-current-column))))
  8808. (dummy (and (or name refass) (not named)
  8809. (not (y-or-n-p "Replace field formula with column formula? " ))
  8810. (error "Abort")))
  8811. (name (or name ref))
  8812. (org-table-may-need-update nil)
  8813. (stored (cdr (assoc scol stored-list)))
  8814. (eq (cond
  8815. ((and stored equation (string-match "^ *=? *$" equation))
  8816. stored)
  8817. ((stringp equation)
  8818. equation)
  8819. (t (org-table-formula-from-user
  8820. (read-string
  8821. (org-table-formula-to-user
  8822. (format "%s formula %s%s="
  8823. (if named "Field" "Column")
  8824. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8825. scol))
  8826. (if stored (org-table-formula-to-user stored) "")
  8827. 'org-table-formula-history
  8828. )))))
  8829. mustsave)
  8830. (when (not (string-match "\\S-" eq))
  8831. ;; remove formula
  8832. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8833. (org-table-store-formulas stored-list)
  8834. (error "Formula removed"))
  8835. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8836. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8837. (if (and name (not named))
  8838. ;; We set the column equation, delete the named one.
  8839. (setq stored-list (delq (assoc name stored-list) stored-list)
  8840. mustsave t))
  8841. (if stored
  8842. (setcdr (assoc scol stored-list) eq)
  8843. (setq stored-list (cons (cons scol eq) stored-list)))
  8844. (if (or mustsave (not (equal stored eq)))
  8845. (org-table-store-formulas stored-list))
  8846. eq))
  8847. (defun org-table-store-formulas (alist)
  8848. "Store the list of formulas below the current table."
  8849. (setq alist (sort alist 'org-table-formula-less-p))
  8850. (save-excursion
  8851. (goto-char (org-table-end))
  8852. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8853. (progn
  8854. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8855. (goto-char (match-beginning 2))
  8856. (delete-region (match-beginning 2) (match-end 0)))
  8857. (insert "#+TBLFM:"))
  8858. (insert " "
  8859. (mapconcat (lambda (x)
  8860. (concat
  8861. (if (equal (string-to-char (car x)) ?@) "" "$")
  8862. (car x) "=" (cdr x)))
  8863. alist "::")
  8864. "\n")))
  8865. (defsubst org-table-formula-make-cmp-string (a)
  8866. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8867. (concat
  8868. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8869. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8870. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8871. (defun org-table-formula-less-p (a b)
  8872. "Compare two formulas for sorting."
  8873. (let ((as (org-table-formula-make-cmp-string (car a)))
  8874. (bs (org-table-formula-make-cmp-string (car b))))
  8875. (and as bs (string< as bs))))
  8876. (defun org-table-get-stored-formulas (&optional noerror)
  8877. "Return an alist with the stored formulas directly after current table."
  8878. (interactive)
  8879. (let (scol eq eq-alist strings string seen)
  8880. (save-excursion
  8881. (goto-char (org-table-end))
  8882. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8883. (setq strings (org-split-string (match-string 2) " *:: *"))
  8884. (while (setq string (pop strings))
  8885. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8886. (setq scol (if (match-end 2)
  8887. (match-string 2 string)
  8888. (match-string 1 string))
  8889. eq (match-string 3 string)
  8890. eq-alist (cons (cons scol eq) eq-alist))
  8891. (if (member scol seen)
  8892. (if noerror
  8893. (progn
  8894. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8895. (ding)
  8896. (sit-for 2))
  8897. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8898. (push scol seen))))))
  8899. (nreverse eq-alist)))
  8900. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8901. "Modify the equations after the table structure has been edited.
  8902. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8903. For all numbers larger than LIMIT, shift them by DELTA."
  8904. (save-excursion
  8905. (goto-char (org-table-end))
  8906. (when (looking-at "#\\+TBLFM:")
  8907. (let ((re (concat key "\\([0-9]+\\)"))
  8908. (re2
  8909. (when remove
  8910. (if (equal key "$")
  8911. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8912. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8913. s n a)
  8914. (when remove
  8915. (while (re-search-forward re2 (point-at-eol) t)
  8916. (replace-match "")))
  8917. (while (re-search-forward re (point-at-eol) t)
  8918. (setq s (match-string 1) n (string-to-number s))
  8919. (cond
  8920. ((setq a (assoc s replace))
  8921. (replace-match (concat key (cdr a)) t t))
  8922. ((and limit (> n limit))
  8923. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8924. (defun org-table-get-specials ()
  8925. "Get the column names and local parameters for this table."
  8926. (save-excursion
  8927. (let ((beg (org-table-begin)) (end (org-table-end))
  8928. names name fields fields1 field cnt
  8929. c v l line col types dlines hlines)
  8930. (setq org-table-column-names nil
  8931. org-table-local-parameters nil
  8932. org-table-named-field-locations nil
  8933. org-table-current-begin-line nil
  8934. org-table-current-begin-pos nil
  8935. org-table-current-line-types nil)
  8936. (goto-char beg)
  8937. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8938. (setq names (org-split-string (match-string 1) " *| *")
  8939. cnt 1)
  8940. (while (setq name (pop names))
  8941. (setq cnt (1+ cnt))
  8942. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8943. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8944. (setq org-table-column-names (nreverse org-table-column-names))
  8945. (setq org-table-column-name-regexp
  8946. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8947. (goto-char beg)
  8948. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8949. (setq fields (org-split-string (match-string 1) " *| *"))
  8950. (while (setq field (pop fields))
  8951. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8952. (push (cons (match-string 1 field) (match-string 2 field))
  8953. org-table-local-parameters))))
  8954. (goto-char beg)
  8955. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8956. (setq c (match-string 1)
  8957. fields (org-split-string (match-string 2) " *| *"))
  8958. (save-excursion
  8959. (beginning-of-line (if (equal c "_") 2 0))
  8960. (setq line (org-current-line) col 1)
  8961. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8962. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8963. (while (and fields1 (setq field (pop fields)))
  8964. (setq v (pop fields1) col (1+ col))
  8965. (when (and (stringp field) (stringp v)
  8966. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8967. (push (cons field v) org-table-local-parameters)
  8968. (push (list field line col) org-table-named-field-locations))))
  8969. ;; Analyse the line types
  8970. (goto-char beg)
  8971. (setq org-table-current-begin-line (org-current-line)
  8972. org-table-current-begin-pos (point)
  8973. l org-table-current-begin-line)
  8974. (while (looking-at "[ \t]*|\\(-\\)?")
  8975. (push (if (match-end 1) 'hline 'dline) types)
  8976. (if (match-end 1) (push l hlines) (push l dlines))
  8977. (beginning-of-line 2)
  8978. (setq l (1+ l)))
  8979. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8980. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8981. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8982. (defun org-table-maybe-eval-formula ()
  8983. "Check if the current field starts with \"=\" or \":=\".
  8984. If yes, store the formula and apply it."
  8985. ;; We already know we are in a table. Get field will only return a formula
  8986. ;; when appropriate. It might return a separator line, but no problem.
  8987. (when org-table-formula-evaluate-inline
  8988. (let* ((field (org-trim (or (org-table-get-field) "")))
  8989. named eq)
  8990. (when (string-match "^:?=\\(.*\\)" field)
  8991. (setq named (equal (string-to-char field) ?:)
  8992. eq (match-string 1 field))
  8993. (if (or (fboundp 'calc-eval)
  8994. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8995. (org-table-eval-formula (if named '(4) nil)
  8996. (org-table-formula-from-user eq))
  8997. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8998. (defvar org-recalc-commands nil
  8999. "List of commands triggering the recalculation of a line.
  9000. Will be filled automatically during use.")
  9001. (defvar org-recalc-marks
  9002. '((" " . "Unmarked: no special line, no automatic recalculation")
  9003. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9004. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9005. ("!" . "Column name definition line. Reference in formula as $name.")
  9006. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9007. ("_" . "Names for values in row below this one.")
  9008. ("^" . "Names for values in row above this one.")))
  9009. (defun org-table-rotate-recalc-marks (&optional newchar)
  9010. "Rotate the recalculation mark in the first column.
  9011. If in any row, the first field is not consistent with a mark,
  9012. insert a new column for the markers.
  9013. When there is an active region, change all the lines in the region,
  9014. after prompting for the marking character.
  9015. After each change, a message will be displayed indicating the meaning
  9016. of the new mark."
  9017. (interactive)
  9018. (unless (org-at-table-p) (error "Not at a table"))
  9019. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9020. (beg (org-table-begin))
  9021. (end (org-table-end))
  9022. (l (org-current-line))
  9023. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9024. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9025. (have-col
  9026. (save-excursion
  9027. (goto-char beg)
  9028. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9029. (col (org-table-current-column))
  9030. (forcenew (car (assoc newchar org-recalc-marks)))
  9031. epos new)
  9032. (when l1
  9033. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9034. (setq newchar (char-to-string (read-char-exclusive))
  9035. forcenew (car (assoc newchar org-recalc-marks))))
  9036. (if (and newchar (not forcenew))
  9037. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9038. newchar))
  9039. (if l1 (goto-line l1))
  9040. (save-excursion
  9041. (beginning-of-line 1)
  9042. (unless (looking-at org-table-dataline-regexp)
  9043. (error "Not at a table data line")))
  9044. (unless have-col
  9045. (org-table-goto-column 1)
  9046. (org-table-insert-column)
  9047. (org-table-goto-column (1+ col)))
  9048. (setq epos (point-at-eol))
  9049. (save-excursion
  9050. (beginning-of-line 1)
  9051. (org-table-get-field
  9052. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9053. (concat " "
  9054. (setq new (or forcenew
  9055. (cadr (member (match-string 1) marks))))
  9056. " ")
  9057. " # ")))
  9058. (if (and l1 l2)
  9059. (progn
  9060. (goto-line l1)
  9061. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9062. (and (looking-at org-table-dataline-regexp)
  9063. (org-table-get-field 1 (concat " " new " "))))
  9064. (goto-line l1)))
  9065. (if (not (= epos (point-at-eol))) (org-table-align))
  9066. (goto-line l)
  9067. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9068. (defun org-table-maybe-recalculate-line ()
  9069. "Recompute the current line if marked for it, and if we haven't just done it."
  9070. (interactive)
  9071. (and org-table-allow-automatic-line-recalculation
  9072. (not (and (memq last-command org-recalc-commands)
  9073. (equal org-last-recalc-line (org-current-line))))
  9074. (save-excursion (beginning-of-line 1)
  9075. (looking-at org-table-auto-recalculate-regexp))
  9076. (org-table-recalculate) t))
  9077. (defvar org-table-formula-debug nil
  9078. "Non-nil means, debug table formulas.
  9079. When nil, simply write \"#ERROR\" in corrupted fields.")
  9080. (make-variable-buffer-local 'org-table-formula-debug)
  9081. (defvar modes)
  9082. (defsubst org-set-calc-mode (var &optional value)
  9083. (if (stringp var)
  9084. (setq var (assoc var '(("D" calc-angle-mode deg)
  9085. ("R" calc-angle-mode rad)
  9086. ("F" calc-prefer-frac t)
  9087. ("S" calc-symbolic-mode t)))
  9088. value (nth 2 var) var (nth 1 var)))
  9089. (if (memq var modes)
  9090. (setcar (cdr (memq var modes)) value)
  9091. (cons var (cons value modes)))
  9092. modes)
  9093. (defun org-table-eval-formula (&optional arg equation
  9094. suppress-align suppress-const
  9095. suppress-store suppress-analysis)
  9096. "Replace the table field value at the cursor by the result of a calculation.
  9097. This function makes use of Dave Gillespie's Calc package, in my view the
  9098. most exciting program ever written for GNU Emacs. So you need to have Calc
  9099. installed in order to use this function.
  9100. In a table, this command replaces the value in the current field with the
  9101. result of a formula. It also installs the formula as the \"current\" column
  9102. formula, by storing it in a special line below the table. When called
  9103. with a `C-u' prefix, the current field must ba a named field, and the
  9104. formula is installed as valid in only this specific field.
  9105. When called with two `C-u' prefixes, insert the active equation
  9106. for the field back into the current field, so that it can be
  9107. edited there. This is useful in order to use \\[org-table-show-reference]
  9108. to check the referenced fields.
  9109. When called, the command first prompts for a formula, which is read in
  9110. the minibuffer. Previously entered formulas are available through the
  9111. history list, and the last used formula is offered as a default.
  9112. These stored formulas are adapted correctly when moving, inserting, or
  9113. deleting columns with the corresponding commands.
  9114. The formula can be any algebraic expression understood by the Calc package.
  9115. For details, see the Org-mode manual.
  9116. This function can also be called from Lisp programs and offers
  9117. additional arguments: EQUATION can be the formula to apply. If this
  9118. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9119. used to speed-up recursive calls by by-passing unnecessary aligns.
  9120. SUPPRESS-CONST suppresses the interpretation of constants in the
  9121. formula, assuming that this has been done already outside the function.
  9122. SUPPRESS-STORE means the formula should not be stored, either because
  9123. it is already stored, or because it is a modified equation that should
  9124. not overwrite the stored one."
  9125. (interactive "P")
  9126. (org-table-check-inside-data-field)
  9127. (or suppress-analysis (org-table-get-specials))
  9128. (if (equal arg '(16))
  9129. (let ((eq (org-table-current-field-formula)))
  9130. (or eq (error "No equation active for current field"))
  9131. (org-table-get-field nil eq)
  9132. (org-table-align)
  9133. (setq org-table-may-need-update t))
  9134. (let* (fields
  9135. (ndown (if (integerp arg) arg 1))
  9136. (org-table-automatic-realign nil)
  9137. (case-fold-search nil)
  9138. (down (> ndown 1))
  9139. (formula (if (and equation suppress-store)
  9140. equation
  9141. (org-table-get-formula equation (equal arg '(4)))))
  9142. (n0 (org-table-current-column))
  9143. (modes (copy-sequence org-calc-default-modes))
  9144. (numbers nil) ; was a variable, now fixed default
  9145. (keep-empty nil)
  9146. n form form0 bw fmt x ev orig c lispp literal)
  9147. ;; Parse the format string. Since we have a lot of modes, this is
  9148. ;; a lot of work. However, I think calc still uses most of the time.
  9149. (if (string-match ";" formula)
  9150. (let ((tmp (org-split-string formula ";")))
  9151. (setq formula (car tmp)
  9152. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9153. (nth 1 tmp)))
  9154. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9155. (setq c (string-to-char (match-string 1 fmt))
  9156. n (string-to-number (match-string 2 fmt)))
  9157. (if (= c ?p)
  9158. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9159. (setq modes (org-set-calc-mode
  9160. 'calc-float-format
  9161. (list (cdr (assoc c '((?n . float) (?f . fix)
  9162. (?s . sci) (?e . eng))))
  9163. n))))
  9164. (setq fmt (replace-match "" t t fmt)))
  9165. (if (string-match "[NT]" fmt)
  9166. (setq numbers (equal (match-string 0 fmt) "N")
  9167. fmt (replace-match "" t t fmt)))
  9168. (if (string-match "L" fmt)
  9169. (setq literal t
  9170. fmt (replace-match "" t t fmt)))
  9171. (if (string-match "E" fmt)
  9172. (setq keep-empty t
  9173. fmt (replace-match "" t t fmt)))
  9174. (while (string-match "[DRFS]" fmt)
  9175. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9176. (setq fmt (replace-match "" t t fmt)))
  9177. (unless (string-match "\\S-" fmt)
  9178. (setq fmt nil))))
  9179. (if (and (not suppress-const) org-table-formula-use-constants)
  9180. (setq formula (org-table-formula-substitute-names formula)))
  9181. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9182. (while (> ndown 0)
  9183. (setq fields (org-split-string
  9184. (org-no-properties
  9185. (buffer-substring (point-at-bol) (point-at-eol)))
  9186. " *| *"))
  9187. (if (eq numbers t)
  9188. (setq fields (mapcar
  9189. (lambda (x) (number-to-string (string-to-number x)))
  9190. fields)))
  9191. (setq ndown (1- ndown))
  9192. (setq form (copy-sequence formula)
  9193. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9194. (if (and lispp literal) (setq lispp 'literal))
  9195. ;; Check for old vertical references
  9196. (setq form (org-rewrite-old-row-references form))
  9197. ;; Insert complex ranges
  9198. (while (string-match org-table-range-regexp form)
  9199. (setq form
  9200. (replace-match
  9201. (save-match-data
  9202. (org-table-make-reference
  9203. (org-table-get-range (match-string 0 form) nil n0)
  9204. keep-empty numbers lispp))
  9205. t t form)))
  9206. ;; Insert simple ranges
  9207. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9208. (setq form
  9209. (replace-match
  9210. (save-match-data
  9211. (org-table-make-reference
  9212. (org-sublist
  9213. fields (string-to-number (match-string 1 form))
  9214. (string-to-number (match-string 2 form)))
  9215. keep-empty numbers lispp))
  9216. t t form)))
  9217. (setq form0 form)
  9218. ;; Insert the references to fields in same row
  9219. (while (string-match "\\$\\([0-9]+\\)" form)
  9220. (setq n (string-to-number (match-string 1 form))
  9221. x (nth (1- (if (= n 0) n0 n)) fields))
  9222. (unless x (error "Invalid field specifier \"%s\""
  9223. (match-string 0 form)))
  9224. (setq form (replace-match
  9225. (save-match-data
  9226. (org-table-make-reference x nil numbers lispp))
  9227. t t form)))
  9228. (if lispp
  9229. (setq ev (condition-case nil
  9230. (eval (eval (read form)))
  9231. (error "#ERROR"))
  9232. ev (if (numberp ev) (number-to-string ev) ev))
  9233. (or (fboundp 'calc-eval)
  9234. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9235. (setq ev (calc-eval (cons form modes)
  9236. (if numbers 'num))))
  9237. (when org-table-formula-debug
  9238. (with-output-to-temp-buffer "*Substitution History*"
  9239. (princ (format "Substitution history of formula
  9240. Orig: %s
  9241. $xyz-> %s
  9242. @r$c-> %s
  9243. $1-> %s\n" orig formula form0 form))
  9244. (if (listp ev)
  9245. (princ (format " %s^\nError: %s"
  9246. (make-string (car ev) ?\-) (nth 1 ev)))
  9247. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9248. ev (or fmt "NONE")
  9249. (if fmt (format fmt (string-to-number ev)) ev)))))
  9250. (setq bw (get-buffer-window "*Substitution History*"))
  9251. (shrink-window-if-larger-than-buffer bw)
  9252. (unless (and (interactive-p) (not ndown))
  9253. (unless (let (inhibit-redisplay)
  9254. (y-or-n-p "Debugging Formula. Continue to next? "))
  9255. (org-table-align)
  9256. (error "Abort"))
  9257. (delete-window bw)
  9258. (message "")))
  9259. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9260. (org-table-justify-field-maybe
  9261. (if fmt (format fmt (string-to-number ev)) ev))
  9262. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9263. (call-interactively 'org-return)
  9264. (setq ndown 0)))
  9265. (and down (org-table-maybe-recalculate-line))
  9266. (or suppress-align (and org-table-may-need-update
  9267. (org-table-align))))))
  9268. (defun org-table-put-field-property (prop value)
  9269. (save-excursion
  9270. (put-text-property (progn (skip-chars-backward "^|") (point))
  9271. (progn (skip-chars-forward "^|") (point))
  9272. prop value)))
  9273. (defun org-table-get-range (desc &optional tbeg col highlight)
  9274. "Get a calc vector from a column, accorting to descriptor DESC.
  9275. Optional arguments TBEG and COL can give the beginning of the table and
  9276. the current column, to avoid unnecessary parsing.
  9277. HIGHLIGHT means, just highlight the range."
  9278. (if (not (equal (string-to-char desc) ?@))
  9279. (setq desc (concat "@" desc)))
  9280. (save-excursion
  9281. (or tbeg (setq tbeg (org-table-begin)))
  9282. (or col (setq col (org-table-current-column)))
  9283. (let ((thisline (org-current-line))
  9284. beg end c1 c2 r1 r2 rangep tmp)
  9285. (unless (string-match org-table-range-regexp desc)
  9286. (error "Invalid table range specifier `%s'" desc))
  9287. (setq rangep (match-end 3)
  9288. r1 (and (match-end 1) (match-string 1 desc))
  9289. r2 (and (match-end 4) (match-string 4 desc))
  9290. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9291. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9292. (and c1 (setq c1 (+ (string-to-number c1)
  9293. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9294. (and c2 (setq c2 (+ (string-to-number c2)
  9295. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9296. (if (equal r1 "") (setq r1 nil))
  9297. (if (equal r2 "") (setq r2 nil))
  9298. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9299. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9300. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9301. (if (not r1) (setq r1 thisline))
  9302. (if (not r2) (setq r2 thisline))
  9303. (if (not c1) (setq c1 col))
  9304. (if (not c2) (setq c2 col))
  9305. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9306. ;; just one field
  9307. (progn
  9308. (goto-line r1)
  9309. (while (not (looking-at org-table-dataline-regexp))
  9310. (beginning-of-line 2))
  9311. (prog1 (org-trim (org-table-get-field c1))
  9312. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9313. ;; A range, return a vector
  9314. ;; First sort the numbers to get a regular ractangle
  9315. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9316. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9317. (goto-line r1)
  9318. (while (not (looking-at org-table-dataline-regexp))
  9319. (beginning-of-line 2))
  9320. (org-table-goto-column c1)
  9321. (setq beg (point))
  9322. (goto-line r2)
  9323. (while (not (looking-at org-table-dataline-regexp))
  9324. (beginning-of-line 0))
  9325. (org-table-goto-column c2)
  9326. (setq end (point))
  9327. (if highlight
  9328. (org-table-highlight-rectangle
  9329. beg (progn (skip-chars-forward "^|\n") (point))))
  9330. ;; return string representation of calc vector
  9331. (mapcar 'org-trim
  9332. (apply 'append (org-table-copy-region beg end)))))))
  9333. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9334. "Analyze descriptor DESC and retrieve the corresponding line number.
  9335. The cursor is currently in line CLINE, the table begins in line BLINE,
  9336. and TABLE is a vector with line types."
  9337. (if (string-match "^[0-9]+$" desc)
  9338. (aref org-table-dlines (string-to-number desc))
  9339. (setq cline (or cline (org-current-line))
  9340. bline (or bline org-table-current-begin-line)
  9341. table (or table org-table-current-line-types))
  9342. (if (or
  9343. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9344. ;; 1 2 3 4 5 6
  9345. (and (not (match-end 3)) (not (match-end 6)))
  9346. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9347. (error "invalid row descriptor `%s'" desc))
  9348. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9349. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9350. (odir (and (match-end 5) (match-string 5 desc)))
  9351. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9352. (i (- cline bline))
  9353. (rel (and (match-end 6)
  9354. (or (and (match-end 1) (not (match-end 3)))
  9355. (match-end 5)))))
  9356. (if (and hn (not hdir))
  9357. (progn
  9358. (setq i 0 hdir "+")
  9359. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9360. (if (and (not hn) on (not odir))
  9361. (error "should never happen");;(aref org-table-dlines on)
  9362. (if (and hn (> hn 0))
  9363. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9364. (if on
  9365. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9366. (+ bline i)))))
  9367. (defun org-find-row-type (table i type backwards relative n)
  9368. (let ((l (length table)))
  9369. (while (> n 0)
  9370. (while (and (setq i (+ i (if backwards -1 1)))
  9371. (>= i 0) (< i l)
  9372. (not (eq (aref table i) type))
  9373. (if (and relative (eq (aref table i) 'hline))
  9374. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9375. t)))
  9376. (setq n (1- n)))
  9377. (if (or (< i 0) (>= i l))
  9378. (error "Row descriptior leads outside table")
  9379. i)))
  9380. (defun org-rewrite-old-row-references (s)
  9381. (if (string-match "&[-+0-9I]" s)
  9382. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9383. s))
  9384. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9385. "Convert list ELEMENTS to something appropriate to insert into formula.
  9386. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9387. NUMBERS indicates that everything should be converted to numbers.
  9388. LISPP means to return something appropriate for a Lisp list."
  9389. (if (stringp elements) ; just a single val
  9390. (if lispp
  9391. (if (eq lispp 'literal)
  9392. elements
  9393. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9394. (if (equal elements "") (setq elements "0"))
  9395. (if numbers (number-to-string (string-to-number elements)) elements))
  9396. (unless keep-empty
  9397. (setq elements
  9398. (delq nil
  9399. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9400. elements))))
  9401. (setq elements (or elements '("0")))
  9402. (if lispp
  9403. (mapconcat
  9404. (lambda (x)
  9405. (if (eq lispp 'literal)
  9406. x
  9407. (prin1-to-string (if numbers (string-to-number x) x))))
  9408. elements " ")
  9409. (concat "[" (mapconcat
  9410. (lambda (x)
  9411. (if numbers (number-to-string (string-to-number x)) x))
  9412. elements
  9413. ",") "]"))))
  9414. (defun org-table-recalculate (&optional all noalign)
  9415. "Recalculate the current table line by applying all stored formulas.
  9416. With prefix arg ALL, do this for all lines in the table."
  9417. (interactive "P")
  9418. (or (memq this-command org-recalc-commands)
  9419. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9420. (unless (org-at-table-p) (error "Not at a table"))
  9421. (if (equal all '(16))
  9422. (org-table-iterate)
  9423. (org-table-get-specials)
  9424. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9425. (lambda (a b) (string< (car a) (car b)))))
  9426. (inhibit-redisplay (not debug-on-error))
  9427. (line-re org-table-dataline-regexp)
  9428. (thisline (org-current-line))
  9429. (thiscol (org-table-current-column))
  9430. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9431. ;; Insert constants in all formulas
  9432. (setq eqlist
  9433. (mapcar (lambda (x)
  9434. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9435. x)
  9436. eqlist))
  9437. ;; Split the equation list
  9438. (while (setq eq (pop eqlist))
  9439. (if (<= (string-to-char (car eq)) ?9)
  9440. (push eq eqlnum)
  9441. (push eq eqlname)))
  9442. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9443. (if all
  9444. (progn
  9445. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9446. (goto-char (setq beg (org-table-begin)))
  9447. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9448. ;; This is a table with marked lines, compute selected lines
  9449. (setq line-re org-table-recalculate-regexp)
  9450. ;; Move forward to the first non-header line
  9451. (if (and (re-search-forward org-table-dataline-regexp end t)
  9452. (re-search-forward org-table-hline-regexp end t)
  9453. (re-search-forward org-table-dataline-regexp end t))
  9454. (setq beg (match-beginning 0))
  9455. nil))) ;; just leave beg where it is
  9456. (setq beg (point-at-bol)
  9457. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9458. (goto-char beg)
  9459. (and all (message "Re-applying formulas to full table..."))
  9460. ;; First find the named fields, and mark them untouchanble
  9461. (remove-text-properties beg end '(org-untouchable t))
  9462. (while (setq eq (pop eqlname))
  9463. (setq name (car eq)
  9464. a (assoc name org-table-named-field-locations))
  9465. (and (not a)
  9466. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9467. (setq a (list name
  9468. (aref org-table-dlines
  9469. (string-to-number (match-string 1 name)))
  9470. (string-to-number (match-string 2 name)))))
  9471. (when (and a (or all (equal (nth 1 a) thisline)))
  9472. (message "Re-applying formula to field: %s" name)
  9473. (goto-line (nth 1 a))
  9474. (org-table-goto-column (nth 2 a))
  9475. (push (append a (list (cdr eq))) eqlname1)
  9476. (org-table-put-field-property :org-untouchable t)))
  9477. ;; Now evauluate the column formulas, but skip fields covered by
  9478. ;; field formulas
  9479. (goto-char beg)
  9480. (while (re-search-forward line-re end t)
  9481. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9482. ;; Unprotected line, recalculate
  9483. (and all (message "Re-applying formulas to full table...(line %d)"
  9484. (setq cnt (1+ cnt))))
  9485. (setq org-last-recalc-line (org-current-line))
  9486. (setq eql eqlnum)
  9487. (while (setq entry (pop eql))
  9488. (goto-line org-last-recalc-line)
  9489. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9490. (unless (get-text-property (point) :org-untouchable)
  9491. (org-table-eval-formula nil (cdr entry)
  9492. 'noalign 'nocst 'nostore 'noanalysis)))))
  9493. ;; Now evaluate the field formulas
  9494. (while (setq eq (pop eqlname1))
  9495. (message "Re-applying formula to field: %s" (car eq))
  9496. (goto-line (nth 1 eq))
  9497. (org-table-goto-column (nth 2 eq))
  9498. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9499. 'nostore 'noanalysis))
  9500. (goto-line thisline)
  9501. (org-table-goto-column thiscol)
  9502. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9503. (or noalign (and org-table-may-need-update (org-table-align))
  9504. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9505. ;; back to initial position
  9506. (message "Re-applying formulas...done")
  9507. (goto-line thisline)
  9508. (org-table-goto-column thiscol)
  9509. (or noalign (and org-table-may-need-update (org-table-align))
  9510. (and all (message "Re-applying formulas...done"))))))
  9511. (defun org-table-iterate (&optional arg)
  9512. "Recalculate the table until it does not change anymore."
  9513. (interactive "P")
  9514. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9515. (i 0)
  9516. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9517. thistbl)
  9518. (catch 'exit
  9519. (while (< i imax)
  9520. (setq i (1+ i))
  9521. (org-table-recalculate 'all)
  9522. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9523. (if (not (string= lasttbl thistbl))
  9524. (setq lasttbl thistbl)
  9525. (if (> i 1)
  9526. (message "Convergence after %d iterations" i)
  9527. (message "Table was already stable"))
  9528. (throw 'exit t)))
  9529. (error "No convergence after %d iterations" i))))
  9530. (defun org-table-formula-substitute-names (f)
  9531. "Replace $const with values in string F."
  9532. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9533. ;; First, check for column names
  9534. (while (setq start (string-match org-table-column-name-regexp f start))
  9535. (setq start (1+ start))
  9536. (setq a (assoc (match-string 1 f) org-table-column-names))
  9537. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9538. ;; Parameters and constants
  9539. (setq start 0)
  9540. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9541. (setq start (1+ start))
  9542. (if (setq a (save-match-data
  9543. (org-table-get-constant (match-string 1 f))))
  9544. (setq f (replace-match
  9545. (concat (if pp "(") a (if pp ")")) t t f))))
  9546. (if org-table-formula-debug
  9547. (put-text-property 0 (length f) :orig-formula f1 f))
  9548. f))
  9549. (defun org-table-get-constant (const)
  9550. "Find the value for a parameter or constant in a formula.
  9551. Parameters get priority."
  9552. (or (cdr (assoc const org-table-local-parameters))
  9553. (cdr (assoc const org-table-formula-constants-local))
  9554. (cdr (assoc const org-table-formula-constants))
  9555. (and (fboundp 'constants-get) (constants-get const))
  9556. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9557. (org-entry-get nil (substring const 5) 'inherit))
  9558. "#UNDEFINED_NAME"))
  9559. (defvar org-table-fedit-map
  9560. (let ((map (make-sparse-keymap)))
  9561. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9562. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9563. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9564. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9565. (org-defkey map "\C-c?" 'org-table-show-reference)
  9566. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9567. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9568. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9569. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9570. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9571. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9572. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9573. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9574. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9575. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9576. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9577. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9578. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9579. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9580. map))
  9581. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9582. '("Edit-Formulas"
  9583. ["Finish and Install" org-table-fedit-finish t]
  9584. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9585. ["Abort" org-table-fedit-abort t]
  9586. "--"
  9587. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9588. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9589. "--"
  9590. "Shift Reference at Point"
  9591. ["Up" org-table-fedit-ref-up t]
  9592. ["Down" org-table-fedit-ref-down t]
  9593. ["Left" org-table-fedit-ref-left t]
  9594. ["Right" org-table-fedit-ref-right t]
  9595. "-"
  9596. "Change Test Row for Column Formulas"
  9597. ["Up" org-table-fedit-line-up t]
  9598. ["Down" org-table-fedit-line-down t]
  9599. "--"
  9600. ["Scroll Table Window" org-table-fedit-scroll t]
  9601. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9602. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9603. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9604. org-table-overlay-coordinates)]
  9605. "--"
  9606. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9607. :style toggle :selected org-table-buffer-is-an]))
  9608. (defvar org-pos)
  9609. (defun org-table-edit-formulas ()
  9610. "Edit the formulas of the current table in a separate buffer."
  9611. (interactive)
  9612. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9613. (beginning-of-line 0))
  9614. (unless (org-at-table-p) (error "Not at a table"))
  9615. (org-table-get-specials)
  9616. (let ((key (org-table-current-field-formula 'key 'noerror))
  9617. (eql (sort (org-table-get-stored-formulas 'noerror)
  9618. 'org-table-formula-less-p))
  9619. (pos (move-marker (make-marker) (point)))
  9620. (startline 1)
  9621. (wc (current-window-configuration))
  9622. (titles '((column . "# Column Formulas\n")
  9623. (field . "# Field Formulas\n")
  9624. (named . "# Named Field Formulas\n")))
  9625. entry s type title)
  9626. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9627. (erase-buffer)
  9628. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9629. (let ((font-lock-global-modes '(not fundamental-mode)))
  9630. (fundamental-mode))
  9631. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9632. (org-set-local 'org-pos pos)
  9633. (org-set-local 'org-window-configuration wc)
  9634. (use-local-map org-table-fedit-map)
  9635. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9636. (easy-menu-add org-table-fedit-menu)
  9637. (setq startline (org-current-line))
  9638. (while (setq entry (pop eql))
  9639. (setq type (cond
  9640. ((equal (string-to-char (car entry)) ?@) 'field)
  9641. ((string-match "^[0-9]" (car entry)) 'column)
  9642. (t 'named)))
  9643. (when (setq title (assq type titles))
  9644. (or (bobp) (insert "\n"))
  9645. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9646. (setq titles (delq title titles)))
  9647. (if (equal key (car entry)) (setq startline (org-current-line)))
  9648. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9649. (car entry) " = " (cdr entry) "\n"))
  9650. (remove-text-properties 0 (length s) '(face nil) s)
  9651. (insert s))
  9652. (if (eq org-table-use-standard-references t)
  9653. (org-table-fedit-toggle-ref-type))
  9654. (goto-line startline)
  9655. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9656. (defun org-table-fedit-post-command ()
  9657. (when (not (memq this-command '(lisp-complete-symbol)))
  9658. (let ((win (selected-window)))
  9659. (save-excursion
  9660. (condition-case nil
  9661. (org-table-show-reference)
  9662. (error nil))
  9663. (select-window win)))))
  9664. (defun org-table-formula-to-user (s)
  9665. "Convert a formula from internal to user representation."
  9666. (if (eq org-table-use-standard-references t)
  9667. (org-table-convert-refs-to-an s)
  9668. s))
  9669. (defun org-table-formula-from-user (s)
  9670. "Convert a formula from user to internal representation."
  9671. (if org-table-use-standard-references
  9672. (org-table-convert-refs-to-rc s)
  9673. s))
  9674. (defun org-table-convert-refs-to-rc (s)
  9675. "Convert spreadsheet references from AB7 to @7$28.
  9676. Works for single references, but also for entire formulas and even the
  9677. full TBLFM line."
  9678. (let ((start 0))
  9679. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9680. (cond
  9681. ((match-end 3)
  9682. ;; format match, just advance
  9683. (setq start (match-end 0)))
  9684. ((and (> (match-beginning 0) 0)
  9685. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9686. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9687. ;; 3.e5 or something like this.
  9688. (setq start (match-end 0)))
  9689. (t
  9690. (setq start (match-beginning 0)
  9691. s (replace-match
  9692. (if (equal (match-string 2 s) "&")
  9693. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9694. (format "@%d$%d"
  9695. (string-to-number (match-string 2 s))
  9696. (org-letters-to-number (match-string 1 s))))
  9697. t t s)))))
  9698. s))
  9699. (defun org-table-convert-refs-to-an (s)
  9700. "Convert spreadsheet references from to @7$28 to AB7.
  9701. Works for single references, but also for entire formulas and even the
  9702. full TBLFM line."
  9703. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9704. (setq s (replace-match
  9705. (format "%s%d"
  9706. (org-number-to-letters
  9707. (string-to-number (match-string 2 s)))
  9708. (string-to-number (match-string 1 s)))
  9709. t t s)))
  9710. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9711. (setq s (replace-match (concat "\\1"
  9712. (org-number-to-letters
  9713. (string-to-number (match-string 2 s))) "&")
  9714. t nil s)))
  9715. s)
  9716. (defun org-letters-to-number (s)
  9717. "Convert a base 26 number represented by letters into an integer.
  9718. For example: AB -> 28."
  9719. (let ((n 0))
  9720. (setq s (upcase s))
  9721. (while (> (length s) 0)
  9722. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9723. s (substring s 1)))
  9724. n))
  9725. (defun org-number-to-letters (n)
  9726. "Convert an integer into a base 26 number represented by letters.
  9727. For example: 28 -> AB."
  9728. (let ((s ""))
  9729. (while (> n 0)
  9730. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9731. n (/ (1- n) 26)))
  9732. s))
  9733. (defun org-table-fedit-convert-buffer (function)
  9734. "Convert all references in this buffer, using FUNTION."
  9735. (let ((line (org-current-line)))
  9736. (goto-char (point-min))
  9737. (while (not (eobp))
  9738. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9739. (delete-region (point) (point-at-eol))
  9740. (or (eobp) (forward-char 1)))
  9741. (goto-line line)))
  9742. (defun org-table-fedit-toggle-ref-type ()
  9743. "Convert all references in the buffer from B3 to @3$2 and back."
  9744. (interactive)
  9745. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9746. (org-table-fedit-convert-buffer
  9747. (if org-table-buffer-is-an
  9748. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9749. (message "Reference type switched to %s"
  9750. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9751. (defun org-table-fedit-ref-up ()
  9752. "Shift the reference at point one row/hline up."
  9753. (interactive)
  9754. (org-table-fedit-shift-reference 'up))
  9755. (defun org-table-fedit-ref-down ()
  9756. "Shift the reference at point one row/hline down."
  9757. (interactive)
  9758. (org-table-fedit-shift-reference 'down))
  9759. (defun org-table-fedit-ref-left ()
  9760. "Shift the reference at point one field to the left."
  9761. (interactive)
  9762. (org-table-fedit-shift-reference 'left))
  9763. (defun org-table-fedit-ref-right ()
  9764. "Shift the reference at point one field to the right."
  9765. (interactive)
  9766. (org-table-fedit-shift-reference 'right))
  9767. (defun org-table-fedit-shift-reference (dir)
  9768. (cond
  9769. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9770. (if (memq dir '(left right))
  9771. (org-rematch-and-replace 1 (eq dir 'left))
  9772. (error "Cannot shift reference in this direction")))
  9773. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9774. ;; A B3-like reference
  9775. (if (memq dir '(up down))
  9776. (org-rematch-and-replace 2 (eq dir 'up))
  9777. (org-rematch-and-replace 1 (eq dir 'left))))
  9778. ((org-at-regexp-p
  9779. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9780. ;; An internal reference
  9781. (if (memq dir '(up down))
  9782. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9783. (org-rematch-and-replace 5 (eq dir 'left))))))
  9784. (defun org-rematch-and-replace (n &optional decr hline)
  9785. "Re-match the group N, and replace it with the shifted refrence."
  9786. (or (match-end n) (error "Cannot shift reference in this direction"))
  9787. (goto-char (match-beginning n))
  9788. (and (looking-at (regexp-quote (match-string n)))
  9789. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9790. t t)))
  9791. (defun org-shift-refpart (ref &optional decr hline)
  9792. "Shift a refrence part REF.
  9793. If DECR is set, decrease the references row/column, else increase.
  9794. If HLINE is set, this may be a hline reference, it certainly is not
  9795. a translation reference."
  9796. (save-match-data
  9797. (let* ((sign (string-match "^[-+]" ref)) n)
  9798. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9799. (cond
  9800. ((and hline (string-match "^I+" ref))
  9801. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9802. (setq n (+ n (if decr -1 1)))
  9803. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9804. (if sign
  9805. (setq sign (if (< n 0) "-" "+") n (abs n))
  9806. (setq n (max 1 n)))
  9807. (concat sign (make-string n ?I)))
  9808. ((string-match "^[0-9]+" ref)
  9809. (setq n (string-to-number (concat sign ref)))
  9810. (setq n (+ n (if decr -1 1)))
  9811. (if sign
  9812. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9813. (number-to-string (max 1 n))))
  9814. ((string-match "^[a-zA-Z]+" ref)
  9815. (org-number-to-letters
  9816. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9817. (t (error "Cannot shift reference"))))))
  9818. (defun org-table-fedit-toggle-coordinates ()
  9819. "Toggle the display of coordinates in the refrenced table."
  9820. (interactive)
  9821. (let ((pos (marker-position org-pos)))
  9822. (with-current-buffer (marker-buffer org-pos)
  9823. (save-excursion
  9824. (goto-char pos)
  9825. (org-table-toggle-coordinate-overlays)))))
  9826. (defun org-table-fedit-finish (&optional arg)
  9827. "Parse the buffer for formula definitions and install them.
  9828. With prefix ARG, apply the new formulas to the table."
  9829. (interactive "P")
  9830. (org-table-remove-rectangle-highlight)
  9831. (if org-table-use-standard-references
  9832. (progn
  9833. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9834. (setq org-table-buffer-is-an nil)))
  9835. (let ((pos org-pos) eql var form)
  9836. (goto-char (point-min))
  9837. (while (re-search-forward
  9838. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9839. nil t)
  9840. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9841. form (match-string 3))
  9842. (setq form (org-trim form))
  9843. (when (not (equal form ""))
  9844. (while (string-match "[ \t]*\n[ \t]*" form)
  9845. (setq form (replace-match " " t t form)))
  9846. (when (assoc var eql)
  9847. (error "Double formulas for %s" var))
  9848. (push (cons var form) eql)))
  9849. (setq org-pos nil)
  9850. (set-window-configuration org-window-configuration)
  9851. (select-window (get-buffer-window (marker-buffer pos)))
  9852. (goto-char pos)
  9853. (unless (org-at-table-p)
  9854. (error "Lost table position - cannot install formulae"))
  9855. (org-table-store-formulas eql)
  9856. (move-marker pos nil)
  9857. (kill-buffer "*Edit Formulas*")
  9858. (if arg
  9859. (org-table-recalculate 'all)
  9860. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9861. (defun org-table-fedit-abort ()
  9862. "Abort editing formulas, without installing the changes."
  9863. (interactive)
  9864. (org-table-remove-rectangle-highlight)
  9865. (let ((pos org-pos))
  9866. (set-window-configuration org-window-configuration)
  9867. (select-window (get-buffer-window (marker-buffer pos)))
  9868. (goto-char pos)
  9869. (move-marker pos nil)
  9870. (message "Formula editing aborted without installing changes")))
  9871. (defun org-table-fedit-lisp-indent ()
  9872. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9873. (interactive)
  9874. (let ((pos (point)) beg end ind)
  9875. (beginning-of-line 1)
  9876. (cond
  9877. ((looking-at "[ \t]")
  9878. (goto-char pos)
  9879. (call-interactively 'lisp-indent-line))
  9880. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9881. ((not (fboundp 'pp-buffer))
  9882. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9883. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9884. (goto-char (- (match-end 0) 2))
  9885. (setq beg (point))
  9886. (setq ind (make-string (current-column) ?\ ))
  9887. (condition-case nil (forward-sexp 1)
  9888. (error
  9889. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9890. (setq end (point))
  9891. (save-restriction
  9892. (narrow-to-region beg end)
  9893. (if (eq last-command this-command)
  9894. (progn
  9895. (goto-char (point-min))
  9896. (setq this-command nil)
  9897. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9898. (replace-match " ")))
  9899. (pp-buffer)
  9900. (untabify (point-min) (point-max))
  9901. (goto-char (1+ (point-min)))
  9902. (while (re-search-forward "^." nil t)
  9903. (beginning-of-line 1)
  9904. (insert ind))
  9905. (goto-char (point-max))
  9906. (backward-delete-char 1)))
  9907. (goto-char beg))
  9908. (t nil))))
  9909. (defvar org-show-positions nil)
  9910. (defun org-table-show-reference (&optional local)
  9911. "Show the location/value of the $ expression at point."
  9912. (interactive)
  9913. (org-table-remove-rectangle-highlight)
  9914. (catch 'exit
  9915. (let ((pos (if local (point) org-pos))
  9916. (face2 'highlight)
  9917. (org-inhibit-highlight-removal t)
  9918. (win (selected-window))
  9919. (org-show-positions nil)
  9920. var name e what match dest)
  9921. (if local (org-table-get-specials))
  9922. (setq what (cond
  9923. ((or (org-at-regexp-p org-table-range-regexp2)
  9924. (org-at-regexp-p org-table-translate-regexp)
  9925. (org-at-regexp-p org-table-range-regexp))
  9926. (setq match
  9927. (save-match-data
  9928. (org-table-convert-refs-to-rc (match-string 0))))
  9929. 'range)
  9930. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9931. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9932. ((not local) nil)
  9933. (t (error "No reference at point")))
  9934. match (and what (or match (match-string 0))))
  9935. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9936. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9937. 'secondary-selection))
  9938. (org-add-hook 'before-change-functions
  9939. 'org-table-remove-rectangle-highlight)
  9940. (if (eq what 'name) (setq var (substring match 1)))
  9941. (when (eq what 'range)
  9942. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9943. (setq match (org-table-formula-substitute-names match)))
  9944. (unless local
  9945. (save-excursion
  9946. (end-of-line 1)
  9947. (re-search-backward "^\\S-" nil t)
  9948. (beginning-of-line 1)
  9949. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9950. (setq dest
  9951. (save-match-data
  9952. (org-table-convert-refs-to-rc (match-string 1))))
  9953. (org-table-add-rectangle-overlay
  9954. (match-beginning 1) (match-end 1) face2))))
  9955. (if (and (markerp pos) (marker-buffer pos))
  9956. (if (get-buffer-window (marker-buffer pos))
  9957. (select-window (get-buffer-window (marker-buffer pos)))
  9958. (org-switch-to-buffer-other-window (get-buffer-window
  9959. (marker-buffer pos)))))
  9960. (goto-char pos)
  9961. (org-table-force-dataline)
  9962. (when dest
  9963. (setq name (substring dest 1))
  9964. (cond
  9965. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9966. (setq e (assoc name org-table-named-field-locations))
  9967. (goto-line (nth 1 e))
  9968. (org-table-goto-column (nth 2 e)))
  9969. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9970. (let ((l (string-to-number (match-string 1 dest)))
  9971. (c (string-to-number (match-string 2 dest))))
  9972. (goto-line (aref org-table-dlines l))
  9973. (org-table-goto-column c)))
  9974. (t (org-table-goto-column (string-to-number name))))
  9975. (move-marker pos (point))
  9976. (org-table-highlight-rectangle nil nil face2))
  9977. (cond
  9978. ((equal dest match))
  9979. ((not match))
  9980. ((eq what 'range)
  9981. (condition-case nil
  9982. (save-excursion
  9983. (org-table-get-range match nil nil 'highlight))
  9984. (error nil)))
  9985. ((setq e (assoc var org-table-named-field-locations))
  9986. (goto-line (nth 1 e))
  9987. (org-table-goto-column (nth 2 e))
  9988. (org-table-highlight-rectangle (point) (point))
  9989. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9990. ((setq e (assoc var org-table-column-names))
  9991. (org-table-goto-column (string-to-number (cdr e)))
  9992. (org-table-highlight-rectangle (point) (point))
  9993. (goto-char (org-table-begin))
  9994. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9995. (org-table-end) t)
  9996. (progn
  9997. (goto-char (match-beginning 1))
  9998. (org-table-highlight-rectangle)
  9999. (message "Named column (column %s)" (cdr e)))
  10000. (error "Column name not found")))
  10001. ((eq what 'column)
  10002. ;; column number
  10003. (org-table-goto-column (string-to-number (substring match 1)))
  10004. (org-table-highlight-rectangle (point) (point))
  10005. (message "Column %s" (substring match 1)))
  10006. ((setq e (assoc var org-table-local-parameters))
  10007. (goto-char (org-table-begin))
  10008. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10009. (progn
  10010. (goto-char (match-beginning 1))
  10011. (org-table-highlight-rectangle)
  10012. (message "Local parameter."))
  10013. (error "Parameter not found")))
  10014. (t
  10015. (cond
  10016. ((not var) (error "No reference at point"))
  10017. ((setq e (assoc var org-table-formula-constants-local))
  10018. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10019. var (cdr e)))
  10020. ((setq e (assoc var org-table-formula-constants))
  10021. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10022. var (cdr e)))
  10023. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10024. (message "Constant: $%s=%s, from `constants.el'%s."
  10025. var e (format " (%s units)" constants-unit-system)))
  10026. (t (error "Undefined name $%s" var)))))
  10027. (goto-char pos)
  10028. (when (and org-show-positions
  10029. (not (memq this-command '(org-table-fedit-scroll
  10030. org-table-fedit-scroll-down))))
  10031. (push pos org-show-positions)
  10032. (push org-table-current-begin-pos org-show-positions)
  10033. (let ((min (apply 'min org-show-positions))
  10034. (max (apply 'max org-show-positions)))
  10035. (goto-char min) (recenter 0)
  10036. (goto-char max)
  10037. (or (pos-visible-in-window-p max) (recenter -1))))
  10038. (select-window win))))
  10039. (defun org-table-force-dataline ()
  10040. "Make sure the cursor is in a dataline in a table."
  10041. (unless (save-excursion
  10042. (beginning-of-line 1)
  10043. (looking-at org-table-dataline-regexp))
  10044. (let* ((re org-table-dataline-regexp)
  10045. (p1 (save-excursion (re-search-forward re nil 'move)))
  10046. (p2 (save-excursion (re-search-backward re nil 'move))))
  10047. (cond ((and p1 p2)
  10048. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10049. p1 p2)))
  10050. ((or p1 p2) (goto-char (or p1 p2)))
  10051. (t (error "No table dataline around here"))))))
  10052. (defun org-table-fedit-line-up ()
  10053. "Move cursor one line up in the window showing the table."
  10054. (interactive)
  10055. (org-table-fedit-move 'previous-line))
  10056. (defun org-table-fedit-line-down ()
  10057. "Move cursor one line down in the window showing the table."
  10058. (interactive)
  10059. (org-table-fedit-move 'next-line))
  10060. (defun org-table-fedit-move (command)
  10061. "Move the cursor in the window shoinw the table.
  10062. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10063. (let ((org-table-allow-automatic-line-recalculation nil)
  10064. (pos org-pos) (win (selected-window)) p)
  10065. (select-window (get-buffer-window (marker-buffer org-pos)))
  10066. (setq p (point))
  10067. (call-interactively command)
  10068. (while (and (org-at-table-p)
  10069. (org-at-table-hline-p))
  10070. (call-interactively command))
  10071. (or (org-at-table-p) (goto-char p))
  10072. (move-marker pos (point))
  10073. (select-window win)))
  10074. (defun org-table-fedit-scroll (N)
  10075. (interactive "p")
  10076. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10077. (scroll-other-window N)))
  10078. (defun org-table-fedit-scroll-down (N)
  10079. (interactive "p")
  10080. (org-table-fedit-scroll (- N)))
  10081. (defvar org-table-rectangle-overlays nil)
  10082. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10083. "Add a new overlay."
  10084. (let ((ov (org-make-overlay beg end)))
  10085. (org-overlay-put ov 'face (or face 'secondary-selection))
  10086. (push ov org-table-rectangle-overlays)))
  10087. (defun org-table-highlight-rectangle (&optional beg end face)
  10088. "Highlight rectangular region in a table."
  10089. (setq beg (or beg (point)) end (or end (point)))
  10090. (let ((b (min beg end))
  10091. (e (max beg end))
  10092. l1 c1 l2 c2 tmp)
  10093. (and (boundp 'org-show-positions)
  10094. (setq org-show-positions (cons b (cons e org-show-positions))))
  10095. (goto-char (min beg end))
  10096. (setq l1 (org-current-line)
  10097. c1 (org-table-current-column))
  10098. (goto-char (max beg end))
  10099. (setq l2 (org-current-line)
  10100. c2 (org-table-current-column))
  10101. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10102. (goto-line l1)
  10103. (beginning-of-line 1)
  10104. (loop for line from l1 to l2 do
  10105. (when (looking-at org-table-dataline-regexp)
  10106. (org-table-goto-column c1)
  10107. (skip-chars-backward "^|\n") (setq beg (point))
  10108. (org-table-goto-column c2)
  10109. (skip-chars-forward "^|\n") (setq end (point))
  10110. (org-table-add-rectangle-overlay beg end face))
  10111. (beginning-of-line 2))
  10112. (goto-char b))
  10113. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10114. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10115. "Remove the rectangle overlays."
  10116. (unless org-inhibit-highlight-removal
  10117. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10118. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10119. (setq org-table-rectangle-overlays nil)))
  10120. (defvar org-table-coordinate-overlays nil
  10121. "Collects the cooordinate grid overlays, so that they can be removed.")
  10122. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10123. (defun org-table-overlay-coordinates ()
  10124. "Add overlays to the table at point, to show row/column coordinates."
  10125. (interactive)
  10126. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10127. (setq org-table-coordinate-overlays nil)
  10128. (save-excursion
  10129. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10130. (goto-char (org-table-begin))
  10131. (while (org-at-table-p)
  10132. (setq eol (point-at-eol))
  10133. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10134. (push ov org-table-coordinate-overlays)
  10135. (setq hline (looking-at org-table-hline-regexp))
  10136. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10137. (format "%4d" (setq id (1+ id)))))
  10138. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10139. (when hline
  10140. (setq ic 0)
  10141. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10142. (setq beg (1+ (match-beginning 0))
  10143. ic (1+ ic)
  10144. s1 (concat "$" (int-to-string ic))
  10145. s2 (org-number-to-letters ic)
  10146. str (if (eq org-table-use-standard-references t) s2 s1))
  10147. (setq ov (org-make-overlay beg (+ beg (length str))))
  10148. (push ov org-table-coordinate-overlays)
  10149. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10150. (beginning-of-line 2)))))
  10151. (defun org-table-toggle-coordinate-overlays ()
  10152. "Toggle the display of Row/Column numbers in tables."
  10153. (interactive)
  10154. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10155. (message "Row/Column number display turned %s"
  10156. (if org-table-overlay-coordinates "on" "off"))
  10157. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10158. (org-table-align))
  10159. (unless org-table-overlay-coordinates
  10160. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10161. (setq org-table-coordinate-overlays nil)))
  10162. (defun org-table-toggle-formula-debugger ()
  10163. "Toggle the formula debugger in tables."
  10164. (interactive)
  10165. (setq org-table-formula-debug (not org-table-formula-debug))
  10166. (message "Formula debugging has been turned %s"
  10167. (if org-table-formula-debug "on" "off")))
  10168. ;;; The orgtbl minor mode
  10169. ;; Define a minor mode which can be used in other modes in order to
  10170. ;; integrate the org-mode table editor.
  10171. ;; This is really a hack, because the org-mode table editor uses several
  10172. ;; keys which normally belong to the major mode, for example the TAB and
  10173. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10174. ;; necessary to operate the table editor, but wraps the commands into a
  10175. ;; function which tests if the cursor is currently inside a table. If that
  10176. ;; is the case, the table editor command is executed. However, when any of
  10177. ;; those keys is used outside a table, the function uses `key-binding' to
  10178. ;; look up if the key has an associated command in another currently active
  10179. ;; keymap (minor modes, major mode, global), and executes that command.
  10180. ;; There might be problems if any of the keys used by the table editor is
  10181. ;; otherwise used as a prefix key.
  10182. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10183. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10184. ;; addresses this by checking explicitly for both bindings.
  10185. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10186. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10187. ;; Some modes bind other commands to simple characters, for example
  10188. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10189. ;; active, this binding is ignored inside tables and replaced with a
  10190. ;; modified self-insert.
  10191. (defvar orgtbl-mode nil
  10192. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10193. table editor in arbitrary modes.")
  10194. (make-variable-buffer-local 'orgtbl-mode)
  10195. (defvar orgtbl-mode-map (make-keymap)
  10196. "Keymap for `orgtbl-mode'.")
  10197. ;;;###autoload
  10198. (defun turn-on-orgtbl ()
  10199. "Unconditionally turn on `orgtbl-mode'."
  10200. (orgtbl-mode 1))
  10201. (defvar org-old-auto-fill-inhibit-regexp nil
  10202. "Local variable used by `orgtbl-mode'")
  10203. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10204. "Matches a line belonging to an orgtbl.")
  10205. (defconst orgtbl-extra-font-lock-keywords
  10206. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10207. 0 (quote 'org-table) 'prepend))
  10208. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10209. ;;;###autoload
  10210. (defun orgtbl-mode (&optional arg)
  10211. "The `org-mode' table editor as a minor mode for use in other modes."
  10212. (interactive)
  10213. (if (org-mode-p)
  10214. ;; Exit without error, in case some hook functions calls this
  10215. ;; by accident in org-mode.
  10216. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10217. (setq orgtbl-mode
  10218. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10219. (if orgtbl-mode
  10220. (progn
  10221. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10222. ;; Make sure we are first in minor-mode-map-alist
  10223. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10224. (and c (setq minor-mode-map-alist
  10225. (cons c (delq c minor-mode-map-alist)))))
  10226. (org-set-local (quote org-table-may-need-update) t)
  10227. (org-add-hook 'before-change-functions 'org-before-change-function
  10228. nil 'local)
  10229. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10230. auto-fill-inhibit-regexp)
  10231. (org-set-local 'auto-fill-inhibit-regexp
  10232. (if auto-fill-inhibit-regexp
  10233. (concat orgtbl-line-start-regexp "\\|"
  10234. auto-fill-inhibit-regexp)
  10235. orgtbl-line-start-regexp))
  10236. (org-add-to-invisibility-spec '(org-cwidth))
  10237. (when (fboundp 'font-lock-add-keywords)
  10238. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10239. (org-restart-font-lock))
  10240. (easy-menu-add orgtbl-mode-menu)
  10241. (run-hooks 'orgtbl-mode-hook))
  10242. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10243. (org-cleanup-narrow-column-properties)
  10244. (org-remove-from-invisibility-spec '(org-cwidth))
  10245. (remove-hook 'before-change-functions 'org-before-change-function t)
  10246. (when (fboundp 'font-lock-remove-keywords)
  10247. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10248. (org-restart-font-lock))
  10249. (easy-menu-remove orgtbl-mode-menu)
  10250. (force-mode-line-update 'all))))
  10251. (defun org-cleanup-narrow-column-properties ()
  10252. "Remove all properties related to narrow-column invisibility."
  10253. (let ((s 1))
  10254. (while (setq s (text-property-any s (point-max)
  10255. 'display org-narrow-column-arrow))
  10256. (remove-text-properties s (1+ s) '(display t)))
  10257. (setq s 1)
  10258. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10259. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10260. (setq s 1)
  10261. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10262. (remove-text-properties s (1+ s) '(invisible t)))))
  10263. ;; Install it as a minor mode.
  10264. (put 'orgtbl-mode :included t)
  10265. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10266. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10267. (defun orgtbl-make-binding (fun n &rest keys)
  10268. "Create a function for binding in the table minor mode.
  10269. FUN is the command to call inside a table. N is used to create a unique
  10270. command name. KEYS are keys that should be checked in for a command
  10271. to execute outside of tables."
  10272. (eval
  10273. (list 'defun
  10274. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10275. '(arg)
  10276. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10277. "Outside of tables, run the binding of `"
  10278. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10279. "'.")
  10280. '(interactive "p")
  10281. (list 'if
  10282. '(org-at-table-p)
  10283. (list 'call-interactively (list 'quote fun))
  10284. (list 'let '(orgtbl-mode)
  10285. (list 'call-interactively
  10286. (append '(or)
  10287. (mapcar (lambda (k)
  10288. (list 'key-binding k))
  10289. keys)
  10290. '('orgtbl-error))))))))
  10291. (defun orgtbl-error ()
  10292. "Error when there is no default binding for a table key."
  10293. (interactive)
  10294. (error "This key has no function outside tables"))
  10295. (defun orgtbl-setup ()
  10296. "Setup orgtbl keymaps."
  10297. (let ((nfunc 0)
  10298. (bindings
  10299. (list
  10300. '([(meta shift left)] org-table-delete-column)
  10301. '([(meta left)] org-table-move-column-left)
  10302. '([(meta right)] org-table-move-column-right)
  10303. '([(meta shift right)] org-table-insert-column)
  10304. '([(meta shift up)] org-table-kill-row)
  10305. '([(meta shift down)] org-table-insert-row)
  10306. '([(meta up)] org-table-move-row-up)
  10307. '([(meta down)] org-table-move-row-down)
  10308. '("\C-c\C-w" org-table-cut-region)
  10309. '("\C-c\M-w" org-table-copy-region)
  10310. '("\C-c\C-y" org-table-paste-rectangle)
  10311. '("\C-c-" org-table-insert-hline)
  10312. '("\C-c}" org-table-toggle-coordinate-overlays)
  10313. '("\C-c{" org-table-toggle-formula-debugger)
  10314. '("\C-m" org-table-next-row)
  10315. '([(shift return)] org-table-copy-down)
  10316. '("\C-c\C-q" org-table-wrap-region)
  10317. '("\C-c?" org-table-field-info)
  10318. '("\C-c " org-table-blank-field)
  10319. '("\C-c+" org-table-sum)
  10320. '("\C-c=" org-table-eval-formula)
  10321. '("\C-c'" org-table-edit-formulas)
  10322. '("\C-c`" org-table-edit-field)
  10323. '("\C-c*" org-table-recalculate)
  10324. '("\C-c|" org-table-create-or-convert-from-region)
  10325. '("\C-c^" org-table-sort-lines)
  10326. '([(control ?#)] org-table-rotate-recalc-marks)))
  10327. elt key fun cmd)
  10328. (while (setq elt (pop bindings))
  10329. (setq nfunc (1+ nfunc))
  10330. (setq key (org-key (car elt))
  10331. fun (nth 1 elt)
  10332. cmd (orgtbl-make-binding fun nfunc key))
  10333. (org-defkey orgtbl-mode-map key cmd))
  10334. ;; Special treatment needed for TAB and RET
  10335. (org-defkey orgtbl-mode-map [(return)]
  10336. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10337. (org-defkey orgtbl-mode-map "\C-m"
  10338. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10339. (org-defkey orgtbl-mode-map [(tab)]
  10340. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10341. (org-defkey orgtbl-mode-map "\C-i"
  10342. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10343. (org-defkey orgtbl-mode-map [(shift tab)]
  10344. (orgtbl-make-binding 'org-table-previous-field 104
  10345. [(shift tab)] [(tab)] "\C-i"))
  10346. (org-defkey orgtbl-mode-map "\M-\C-m"
  10347. (orgtbl-make-binding 'org-table-wrap-region 105
  10348. "\M-\C-m" [(meta return)]))
  10349. (org-defkey orgtbl-mode-map [(meta return)]
  10350. (orgtbl-make-binding 'org-table-wrap-region 106
  10351. [(meta return)] "\M-\C-m"))
  10352. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10353. (when orgtbl-optimized
  10354. ;; If the user wants maximum table support, we need to hijack
  10355. ;; some standard editing functions
  10356. (org-remap orgtbl-mode-map
  10357. 'self-insert-command 'orgtbl-self-insert-command
  10358. 'delete-char 'org-delete-char
  10359. 'delete-backward-char 'org-delete-backward-char)
  10360. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10361. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10362. '("OrgTbl"
  10363. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10364. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10365. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10366. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10367. "--"
  10368. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10369. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10370. ["Copy Field from Above"
  10371. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10372. "--"
  10373. ("Column"
  10374. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10375. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10376. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10377. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10378. ("Row"
  10379. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10380. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10381. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10382. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10383. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10384. "--"
  10385. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10386. ("Rectangle"
  10387. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10388. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10389. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10390. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10391. "--"
  10392. ("Radio tables"
  10393. ["Insert table template" orgtbl-insert-radio-table
  10394. (assq major-mode orgtbl-radio-table-templates)]
  10395. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10396. "--"
  10397. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10398. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10399. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10400. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10401. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10402. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10403. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10404. ["Sum Column/Rectangle" org-table-sum
  10405. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10406. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10407. ["Debug Formulas"
  10408. org-table-toggle-formula-debugger :active (org-at-table-p)
  10409. :keys "C-c {"
  10410. :style toggle :selected org-table-formula-debug]
  10411. ["Show Col/Row Numbers"
  10412. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10413. :keys "C-c }"
  10414. :style toggle :selected org-table-overlay-coordinates]
  10415. ))
  10416. t))
  10417. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10418. "If the cursor is inside a table, realign the table.
  10419. It it is a table to be sent away to a receiver, do it.
  10420. With prefix arg, also recompute table."
  10421. (interactive "P")
  10422. (let ((pos (point)) action)
  10423. (save-excursion
  10424. (beginning-of-line 1)
  10425. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10426. ((looking-at "[ \t]*|") pos)
  10427. ((looking-at "#\\+TBLFM:") 'recalc))))
  10428. (cond
  10429. ((integerp action)
  10430. (goto-char action)
  10431. (org-table-maybe-eval-formula)
  10432. (if arg
  10433. (call-interactively 'org-table-recalculate)
  10434. (org-table-maybe-recalculate-line))
  10435. (call-interactively 'org-table-align)
  10436. (orgtbl-send-table 'maybe))
  10437. ((eq action 'recalc)
  10438. (save-excursion
  10439. (beginning-of-line 1)
  10440. (skip-chars-backward " \r\n\t")
  10441. (if (org-at-table-p)
  10442. (org-call-with-arg 'org-table-recalculate t))))
  10443. (t (let (orgtbl-mode)
  10444. (call-interactively (key-binding "\C-c\C-c")))))))
  10445. (defun orgtbl-tab (arg)
  10446. "Justification and field motion for `orgtbl-mode'."
  10447. (interactive "P")
  10448. (if arg (org-table-edit-field t)
  10449. (org-table-justify-field-maybe)
  10450. (org-table-next-field)))
  10451. (defun orgtbl-ret ()
  10452. "Justification and field motion for `orgtbl-mode'."
  10453. (interactive)
  10454. (org-table-justify-field-maybe)
  10455. (org-table-next-row))
  10456. (defun orgtbl-self-insert-command (N)
  10457. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10458. If the cursor is in a table looking at whitespace, the whitespace is
  10459. overwritten, and the table is not marked as requiring realignment."
  10460. (interactive "p")
  10461. (if (and (org-at-table-p)
  10462. (or
  10463. (and org-table-auto-blank-field
  10464. (member last-command
  10465. '(orgtbl-hijacker-command-100
  10466. orgtbl-hijacker-command-101
  10467. orgtbl-hijacker-command-102
  10468. orgtbl-hijacker-command-103
  10469. orgtbl-hijacker-command-104
  10470. orgtbl-hijacker-command-105))
  10471. (org-table-blank-field))
  10472. t)
  10473. (eq N 1)
  10474. (looking-at "[^|\n]* +|"))
  10475. (let (org-table-may-need-update)
  10476. (goto-char (1- (match-end 0)))
  10477. (delete-backward-char 1)
  10478. (goto-char (match-beginning 0))
  10479. (self-insert-command N))
  10480. (setq org-table-may-need-update t)
  10481. (let (orgtbl-mode)
  10482. (call-interactively (key-binding (vector last-input-event))))))
  10483. (defun org-force-self-insert (N)
  10484. "Needed to enforce self-insert under remapping."
  10485. (interactive "p")
  10486. (self-insert-command N))
  10487. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10488. "Regula expression matching exponentials as produced by calc.")
  10489. (defvar org-table-clean-did-remove-column nil)
  10490. (defun orgtbl-export (table target)
  10491. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10492. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10493. org-table-last-alignment org-table-last-column-widths
  10494. maxcol column)
  10495. (if (not (fboundp func))
  10496. (error "Cannot export orgtbl table to %s" target))
  10497. (setq lines (org-table-clean-before-export lines))
  10498. (setq table
  10499. (mapcar
  10500. (lambda (x)
  10501. (if (string-match org-table-hline-regexp x)
  10502. 'hline
  10503. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10504. lines))
  10505. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10506. table)))
  10507. (loop for i from (1- maxcol) downto 0 do
  10508. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10509. (setq column (delq nil column))
  10510. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10511. (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))
  10512. (funcall func table nil)))
  10513. (defun orgtbl-send-table (&optional maybe)
  10514. "Send a tranformed version of this table to the receiver position.
  10515. With argument MAYBE, fail quietly if no transformation is defined for
  10516. this table."
  10517. (interactive)
  10518. (catch 'exit
  10519. (unless (org-at-table-p) (error "Not at a table"))
  10520. ;; when non-interactive, we assume align has just happened.
  10521. (when (interactive-p) (org-table-align))
  10522. (save-excursion
  10523. (goto-char (org-table-begin))
  10524. (beginning-of-line 0)
  10525. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10526. (if maybe
  10527. (throw 'exit nil)
  10528. (error "Don't know how to transform this table."))))
  10529. (let* ((name (match-string 1))
  10530. beg
  10531. (transform (intern (match-string 2)))
  10532. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10533. (skip (plist-get params :skip))
  10534. (skipcols (plist-get params :skipcols))
  10535. (txt (buffer-substring-no-properties
  10536. (org-table-begin) (org-table-end)))
  10537. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10538. (lines (org-table-clean-before-export lines))
  10539. (i0 (if org-table-clean-did-remove-column 2 1))
  10540. (table (mapcar
  10541. (lambda (x)
  10542. (if (string-match org-table-hline-regexp x)
  10543. 'hline
  10544. (org-remove-by-index
  10545. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10546. skipcols i0)))
  10547. lines))
  10548. (fun (if (= i0 2) 'cdr 'identity))
  10549. (org-table-last-alignment
  10550. (org-remove-by-index (funcall fun org-table-last-alignment)
  10551. skipcols i0))
  10552. (org-table-last-column-widths
  10553. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10554. skipcols i0)))
  10555. (unless (fboundp transform)
  10556. (error "No such transformation function %s" transform))
  10557. (setq txt (funcall transform table params))
  10558. ;; Find the insertion place
  10559. (save-excursion
  10560. (goto-char (point-min))
  10561. (unless (re-search-forward
  10562. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10563. (error "Don't know where to insert translated table"))
  10564. (goto-char (match-beginning 0))
  10565. (beginning-of-line 2)
  10566. (setq beg (point))
  10567. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10568. (error "Cannot find end of insertion region"))
  10569. (beginning-of-line 1)
  10570. (delete-region beg (point))
  10571. (goto-char beg)
  10572. (insert txt "\n"))
  10573. (message "Table converted and installed at receiver location"))))
  10574. (defun org-remove-by-index (list indices &optional i0)
  10575. "Remove the elements in LIST with indices in INDICES.
  10576. First element has index 0, or I0 if given."
  10577. (if (not indices)
  10578. list
  10579. (if (integerp indices) (setq indices (list indices)))
  10580. (setq i0 (1- (or i0 0)))
  10581. (delq :rm (mapcar (lambda (x)
  10582. (setq i0 (1+ i0))
  10583. (if (memq i0 indices) :rm x))
  10584. list))))
  10585. (defun orgtbl-toggle-comment ()
  10586. "Comment or uncomment the orgtbl at point."
  10587. (interactive)
  10588. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10589. (re2 (concat "^" orgtbl-line-start-regexp))
  10590. (commented (save-excursion (beginning-of-line 1)
  10591. (cond ((looking-at re1) t)
  10592. ((looking-at re2) nil)
  10593. (t (error "Not at an org table")))))
  10594. (re (if commented re1 re2))
  10595. beg end)
  10596. (save-excursion
  10597. (beginning-of-line 1)
  10598. (while (looking-at re) (beginning-of-line 0))
  10599. (beginning-of-line 2)
  10600. (setq beg (point))
  10601. (while (looking-at re) (beginning-of-line 2))
  10602. (setq end (point)))
  10603. (comment-region beg end (if commented '(4) nil))))
  10604. (defun orgtbl-insert-radio-table ()
  10605. "Insert a radio table template appropriate for this major mode."
  10606. (interactive)
  10607. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10608. (txt (nth 1 e))
  10609. name pos)
  10610. (unless e (error "No radio table setup defined for %s" major-mode))
  10611. (setq name (read-string "Table name: "))
  10612. (while (string-match "%n" txt)
  10613. (setq txt (replace-match name t t txt)))
  10614. (or (bolp) (insert "\n"))
  10615. (setq pos (point))
  10616. (insert txt)
  10617. (goto-char pos)))
  10618. (defun org-get-param (params header i sym &optional hsym)
  10619. "Get parameter value for symbol SYM.
  10620. If this is a header line, actually get the value for the symbol with an
  10621. additional \"h\" inserted after the colon.
  10622. If the value is a protperty list, get the element for the current column.
  10623. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10624. (let ((val (plist-get params sym)))
  10625. (and hsym header (setq val (or (plist-get params hsym) val)))
  10626. (if (consp val) (plist-get val i) val)))
  10627. (defun orgtbl-to-generic (table params)
  10628. "Convert the orgtbl-mode TABLE to some other format.
  10629. This generic routine can be used for many standard cases.
  10630. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10631. separator line, or a list of fields for that line.
  10632. PARAMS is a property list of parameters that can influence the conversion.
  10633. For the generic converter, some parameters are obligatory: You need to
  10634. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10635. :splice, you must have :tstart and :tend.
  10636. Valid parameters are
  10637. :tstart String to start the table. Ignored when :splice is t.
  10638. :tend String to end the table. Ignored when :splice is t.
  10639. :splice When set to t, return only table body lines, don't wrap
  10640. them into :tstart and :tend. Default is nil.
  10641. :hline String to be inserted on horizontal separation lines.
  10642. May be nil to ignore hlines.
  10643. :lstart String to start a new table line.
  10644. :lend String to end a table line
  10645. :sep Separator between two fields
  10646. :lfmt Format for entire line, with enough %s to capture all fields.
  10647. If this is present, :lstart, :lend, and :sep are ignored.
  10648. :fmt A format to be used to wrap the field, should contain
  10649. %s for the original field value. For example, to wrap
  10650. everything in dollars, you could use :fmt \"$%s$\".
  10651. This may also be a property list with column numbers and
  10652. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10653. :hlstart :hlend :hlsep :hlfmt :hfmt
  10654. Same as above, specific for the header lines in the table.
  10655. All lines before the first hline are treated as header.
  10656. If any of these is not present, the data line value is used.
  10657. :efmt Use this format to print numbers with exponentials.
  10658. The format should have %s twice for inserting mantissa
  10659. and exponent, for example \"%s\\\\times10^{%s}\". This
  10660. may also be a property list with column numbers and
  10661. formats. :fmt will still be applied after :efmt.
  10662. In addition to this, the parameters :skip and :skipcols are always handled
  10663. directly by `orgtbl-send-table'. See manual."
  10664. (interactive)
  10665. (let* ((p params)
  10666. (splicep (plist-get p :splice))
  10667. (hline (plist-get p :hline))
  10668. rtn line i fm efm lfmt h)
  10669. ;; Do we have a header?
  10670. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10671. (setq h t))
  10672. ;; Put header
  10673. (unless splicep
  10674. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10675. ;; Now loop over all lines
  10676. (while (setq line (pop table))
  10677. (if (eq line 'hline)
  10678. ;; A horizontal separator line
  10679. (progn (if hline (push hline rtn))
  10680. (setq h nil)) ; no longer in header
  10681. ;; A normal line. Convert the fields, push line onto the result list
  10682. (setq i 0)
  10683. (setq line
  10684. (mapcar
  10685. (lambda (f)
  10686. (setq i (1+ i)
  10687. fm (org-get-param p h i :fmt :hfmt)
  10688. efm (org-get-param p h i :efmt))
  10689. (if (and efm (string-match orgtbl-exp-regexp f))
  10690. (setq f (format
  10691. efm (match-string 1 f) (match-string 2 f))))
  10692. (if fm (setq f (format fm f)))
  10693. f)
  10694. line))
  10695. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10696. (push (apply 'format lfmt line) rtn)
  10697. (push (concat
  10698. (org-get-param p h i :lstart :hlstart)
  10699. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10700. (org-get-param p h i :lend :hlend))
  10701. rtn))))
  10702. (unless splicep
  10703. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10704. (mapconcat 'identity (nreverse rtn) "\n")))
  10705. (defun orgtbl-to-latex (table params)
  10706. "Convert the orgtbl-mode TABLE to LaTeX.
  10707. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10708. separator line, or a list of fields for that line.
  10709. PARAMS is a property list of parameters that can influence the conversion.
  10710. Supports all parameters from `orgtbl-to-generic'. Most important for
  10711. LaTeX are:
  10712. :splice When set to t, return only table body lines, don't wrap
  10713. them into a tabular environment. Default is nil.
  10714. :fmt A format to be used to wrap the field, should contain %s for the
  10715. original field value. For example, to wrap everything in dollars,
  10716. use :fmt \"$%s$\". This may also be a property list with column
  10717. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10718. :efmt Format for transforming numbers with exponentials. The format
  10719. should have %s twice for inserting mantissa and exponent, for
  10720. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10721. This may also be a property list with column numbers and formats.
  10722. The general parameters :skip and :skipcols have already been applied when
  10723. this function is called."
  10724. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10725. org-table-last-alignment ""))
  10726. (params2
  10727. (list
  10728. :tstart (concat "\\begin{tabular}{" alignment "}")
  10729. :tend "\\end{tabular}"
  10730. :lstart "" :lend " \\\\" :sep " & "
  10731. :efmt "%s\\,(%s)" :hline "\\hline")))
  10732. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10733. (defun orgtbl-to-html (table params)
  10734. "Convert the orgtbl-mode TABLE to LaTeX.
  10735. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10736. separator line, or a list of fields for that line.
  10737. PARAMS is a property list of parameters that can influence the conversion.
  10738. Currently this function recognizes the following parameters:
  10739. :splice When set to t, return only table body lines, don't wrap
  10740. them into a <table> environment. Default is nil.
  10741. The general parameters :skip and :skipcols have already been applied when
  10742. this function is called. The function does *not* use `orgtbl-to-generic',
  10743. so you cannot specify parameters for it."
  10744. (let* ((splicep (plist-get params :splice))
  10745. html)
  10746. ;; Just call the formatter we already have
  10747. ;; We need to make text lines for it, so put the fields back together.
  10748. (setq html (org-format-org-table-html
  10749. (mapcar
  10750. (lambda (x)
  10751. (if (eq x 'hline)
  10752. "|----+----|"
  10753. (concat "| " (mapconcat 'identity x " | ") " |")))
  10754. table)
  10755. splicep))
  10756. (if (string-match "\n+\\'" html)
  10757. (setq html (replace-match "" t t html)))
  10758. html))
  10759. (defun orgtbl-to-texinfo (table params)
  10760. "Convert the orgtbl-mode TABLE to TeXInfo.
  10761. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10762. separator line, or a list of fields for that line.
  10763. PARAMS is a property list of parameters that can influence the conversion.
  10764. Supports all parameters from `orgtbl-to-generic'. Most important for
  10765. TeXInfo are:
  10766. :splice nil/t When set to t, return only table body lines, don't wrap
  10767. them into a multitable environment. Default is nil.
  10768. :fmt fmt A format to be used to wrap the field, should contain
  10769. %s for the original field value. For example, to wrap
  10770. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10771. This may also be a property list with column numbers and
  10772. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10773. :cf \"f1 f2..\" The column fractions for the table. By default these
  10774. are computed automatically from the width of the columns
  10775. under org-mode.
  10776. The general parameters :skip and :skipcols have already been applied when
  10777. this function is called."
  10778. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10779. (colfrac (or (plist-get params :cf)
  10780. (mapconcat
  10781. (lambda (x) (format "%.3f" (/ (float x) total)))
  10782. org-table-last-column-widths " ")))
  10783. (params2
  10784. (list
  10785. :tstart (concat "@multitable @columnfractions " colfrac)
  10786. :tend "@end multitable"
  10787. :lstart "@item " :lend "" :sep " @tab "
  10788. :hlstart "@headitem ")))
  10789. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10790. ;;;; Link Stuff
  10791. ;;; Link abbreviations
  10792. (defun org-link-expand-abbrev (link)
  10793. "Apply replacements as defined in `org-link-abbrev-alist."
  10794. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10795. (let* ((key (match-string 1 link))
  10796. (as (or (assoc key org-link-abbrev-alist-local)
  10797. (assoc key org-link-abbrev-alist)))
  10798. (tag (and (match-end 2) (match-string 3 link)))
  10799. rpl)
  10800. (if (not as)
  10801. link
  10802. (setq rpl (cdr as))
  10803. (cond
  10804. ((symbolp rpl) (funcall rpl tag))
  10805. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10806. (t (concat rpl tag)))))
  10807. link))
  10808. ;;; Storing and inserting links
  10809. (defvar org-insert-link-history nil
  10810. "Minibuffer history for links inserted with `org-insert-link'.")
  10811. (defvar org-stored-links nil
  10812. "Contains the links stored with `org-store-link'.")
  10813. (defvar org-store-link-plist nil
  10814. "Plist with info about the most recently link created with `org-store-link'.")
  10815. (defvar org-link-protocols nil
  10816. "Link protocols added to Org-mode using `org-add-link-type'.")
  10817. (defvar org-store-link-functions nil
  10818. "List of functions that are called to create and store a link.
  10819. Each function will be called in turn until one returns a non-nil
  10820. value. Each function should check if it is responsible for creating
  10821. this link (for example by looking at the major mode).
  10822. If not, it must exit and return nil.
  10823. If yes, it should return a non-nil value after a calling
  10824. `org-store-link-props' with a list of properties and values.
  10825. Special properties are:
  10826. :type The link prefix. like \"http\". This must be given.
  10827. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10828. This is obligatory as well.
  10829. :description Optional default description for the second pair
  10830. of brackets in an Org-mode link. The user can still change
  10831. this when inserting this link into an Org-mode buffer.
  10832. In addition to these, any additional properties can be specified
  10833. and then used in remember templates.")
  10834. (defun org-add-link-type (type &optional follow publish)
  10835. "Add TYPE to the list of `org-link-types'.
  10836. Re-compute all regular expressions depending on `org-link-types'
  10837. FOLLOW and PUBLISH are two functions. Both take the link path as
  10838. an argument.
  10839. FOLLOW should do whatever is necessary to follow the link, for example
  10840. to find a file or display a mail message.
  10841. PUBLISH takes the path and retuns the string that should be used when
  10842. this document is published. FIMXE: This is actually not yet implemented."
  10843. (add-to-list 'org-link-types type t)
  10844. (org-make-link-regexps)
  10845. (add-to-list 'org-link-protocols
  10846. (list type follow publish)))
  10847. (defun org-add-agenda-custom-command (entry)
  10848. "Replace or add a command in `org-agenda-custom-commands'.
  10849. This is mostly for hacking and trying a new command - once the command
  10850. works you probably want to add it to `org-agenda-custom-commands' for good."
  10851. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10852. (if ass
  10853. (setcdr ass (cdr entry))
  10854. (push entry org-agenda-custom-commands))))
  10855. ;;;###autoload
  10856. (defun org-store-link (arg)
  10857. "\\<org-mode-map>Store an org-link to the current location.
  10858. This link is added to `org-stored-links' and can later be inserted
  10859. into an org-buffer with \\[org-insert-link].
  10860. For some link types, a prefix arg is interpreted:
  10861. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10862. For file links, arg negates `org-context-in-file-links'."
  10863. (interactive "P")
  10864. (setq org-store-link-plist nil) ; reset
  10865. (require 'org-irc)
  10866. (let (link cpltxt desc description search txt)
  10867. (cond
  10868. ((run-hook-with-args-until-success 'org-store-link-functions)
  10869. (setq link (plist-get org-store-link-plist :link)
  10870. desc (or (plist-get org-store-link-plist :description) link)))
  10871. ((eq major-mode 'bbdb-mode)
  10872. (let ((name (bbdb-record-name (bbdb-current-record)))
  10873. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10874. (setq cpltxt (concat "bbdb:" (or name company))
  10875. link (org-make-link cpltxt))
  10876. (org-store-link-props :type "bbdb" :name name :company company)))
  10877. ((eq major-mode 'Info-mode)
  10878. (setq link (org-make-link "info:"
  10879. (file-name-nondirectory Info-current-file)
  10880. ":" Info-current-node))
  10881. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10882. ":" Info-current-node))
  10883. (org-store-link-props :type "info" :file Info-current-file
  10884. :node Info-current-node))
  10885. ((eq major-mode 'calendar-mode)
  10886. (let ((cd (calendar-cursor-to-date)))
  10887. (setq link
  10888. (format-time-string
  10889. (car org-time-stamp-formats)
  10890. (apply 'encode-time
  10891. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10892. nil nil nil))))
  10893. (org-store-link-props :type "calendar" :date cd)))
  10894. ((or (eq major-mode 'vm-summary-mode)
  10895. (eq major-mode 'vm-presentation-mode))
  10896. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10897. (vm-follow-summary-cursor)
  10898. (save-excursion
  10899. (vm-select-folder-buffer)
  10900. (let* ((message (car vm-message-pointer))
  10901. (folder buffer-file-name)
  10902. (subject (vm-su-subject message))
  10903. (to (vm-get-header-contents message "To"))
  10904. (from (vm-get-header-contents message "From"))
  10905. (message-id (vm-su-message-id message)))
  10906. (org-store-link-props :type "vm" :from from :to to :subject subject
  10907. :message-id message-id)
  10908. (setq message-id (org-remove-angle-brackets message-id))
  10909. (setq folder (abbreviate-file-name folder))
  10910. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10911. folder)
  10912. (setq folder (replace-match "" t t folder)))
  10913. (setq cpltxt (org-email-link-description))
  10914. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10915. ((eq major-mode 'wl-summary-mode)
  10916. (let* ((msgnum (wl-summary-message-number))
  10917. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10918. msgnum 'message-id))
  10919. (wl-message-entity
  10920. (if (fboundp 'elmo-message-entity)
  10921. (elmo-message-entity
  10922. wl-summary-buffer-elmo-folder msgnum)
  10923. (elmo-msgdb-overview-get-entity
  10924. msgnum (wl-summary-buffer-msgdb))))
  10925. (from (wl-summary-line-from))
  10926. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10927. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10928. (wl-summary-line-subject))))
  10929. (org-store-link-props :type "wl" :from from :to to
  10930. :subject subject :message-id message-id)
  10931. (setq message-id (org-remove-angle-brackets message-id))
  10932. (setq cpltxt (org-email-link-description))
  10933. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10934. "#" message-id))))
  10935. ((or (equal major-mode 'mh-folder-mode)
  10936. (equal major-mode 'mh-show-mode))
  10937. (let ((from (org-mhe-get-header "From:"))
  10938. (to (org-mhe-get-header "To:"))
  10939. (message-id (org-mhe-get-header "Message-Id:"))
  10940. (subject (org-mhe-get-header "Subject:")))
  10941. (org-store-link-props :type "mh" :from from :to to
  10942. :subject subject :message-id message-id)
  10943. (setq cpltxt (org-email-link-description))
  10944. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10945. (org-remove-angle-brackets message-id)))))
  10946. ((or (eq major-mode 'rmail-mode)
  10947. (eq major-mode 'rmail-summary-mode))
  10948. (save-window-excursion
  10949. (save-restriction
  10950. (when (eq major-mode 'rmail-summary-mode)
  10951. (rmail-show-message rmail-current-message))
  10952. (rmail-narrow-to-non-pruned-header)
  10953. (let ((folder buffer-file-name)
  10954. (message-id (mail-fetch-field "message-id"))
  10955. (from (mail-fetch-field "from"))
  10956. (to (mail-fetch-field "to"))
  10957. (subject (mail-fetch-field "subject")))
  10958. (org-store-link-props
  10959. :type "rmail" :from from :to to
  10960. :subject subject :message-id message-id)
  10961. (setq message-id (org-remove-angle-brackets message-id))
  10962. (setq cpltxt (org-email-link-description))
  10963. (setq link (org-make-link "rmail:" folder "#" message-id)))
  10964. (rmail-show-message rmail-current-message))))
  10965. ((eq major-mode 'gnus-group-mode)
  10966. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10967. (gnus-group-group-name)) ; version
  10968. ((fboundp 'gnus-group-name)
  10969. (gnus-group-name))
  10970. (t "???"))))
  10971. (unless group (error "Not on a group"))
  10972. (org-store-link-props :type "gnus" :group group)
  10973. (setq cpltxt (concat
  10974. (if (org-xor arg org-usenet-links-prefer-google)
  10975. "http://groups.google.com/groups?group="
  10976. "gnus:")
  10977. group)
  10978. link (org-make-link cpltxt))))
  10979. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10980. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10981. (let* ((group gnus-newsgroup-name)
  10982. (article (gnus-summary-article-number))
  10983. (header (gnus-summary-article-header article))
  10984. (from (mail-header-from header))
  10985. (message-id (mail-header-id header))
  10986. (date (mail-header-date header))
  10987. (subject (gnus-summary-subject-string)))
  10988. (org-store-link-props :type "gnus" :from from :subject subject
  10989. :message-id message-id :group group)
  10990. (setq cpltxt (org-email-link-description))
  10991. (if (org-xor arg org-usenet-links-prefer-google)
  10992. (setq link
  10993. (concat
  10994. cpltxt "\n "
  10995. (format "http://groups.google.com/groups?as_umsgid=%s"
  10996. (org-fixup-message-id-for-http message-id))))
  10997. (setq link (org-make-link "gnus:" group
  10998. "#" (number-to-string article))))))
  10999. ((eq major-mode 'w3-mode)
  11000. (setq cpltxt (url-view-url t)
  11001. link (org-make-link cpltxt))
  11002. (org-store-link-props :type "w3" :url (url-view-url t)))
  11003. ((eq major-mode 'w3m-mode)
  11004. (setq cpltxt (or w3m-current-title w3m-current-url)
  11005. link (org-make-link w3m-current-url))
  11006. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11007. ((setq search (run-hook-with-args-until-success
  11008. 'org-create-file-search-functions))
  11009. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11010. "::" search))
  11011. (setq cpltxt (or description link)))
  11012. ((eq major-mode 'image-mode)
  11013. (setq cpltxt (concat "file:"
  11014. (abbreviate-file-name buffer-file-name))
  11015. link (org-make-link cpltxt))
  11016. (org-store-link-props :type "image" :file buffer-file-name))
  11017. ((eq major-mode 'dired-mode)
  11018. ;; link to the file in the current line
  11019. (setq cpltxt (concat "file:"
  11020. (abbreviate-file-name
  11021. (expand-file-name
  11022. (dired-get-filename nil t))))
  11023. link (org-make-link cpltxt)))
  11024. ((and buffer-file-name (org-mode-p))
  11025. ;; Just link to current headline
  11026. (setq cpltxt (concat "file:"
  11027. (abbreviate-file-name buffer-file-name)))
  11028. ;; Add a context search string
  11029. (when (org-xor org-context-in-file-links arg)
  11030. ;; Check if we are on a target
  11031. (if (org-in-regexp "<<\\(.*?\\)>>")
  11032. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11033. (setq txt (cond
  11034. ((org-on-heading-p) nil)
  11035. ((org-region-active-p)
  11036. (buffer-substring (region-beginning) (region-end)))
  11037. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11038. (when (or (null txt) (string-match "\\S-" txt))
  11039. (setq cpltxt
  11040. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11041. desc "NONE"))))
  11042. (if (string-match "::\\'" cpltxt)
  11043. (setq cpltxt (substring cpltxt 0 -2)))
  11044. (setq link (org-make-link cpltxt)))
  11045. ((buffer-file-name (buffer-base-buffer))
  11046. ;; Just link to this file here.
  11047. (setq cpltxt (concat "file:"
  11048. (abbreviate-file-name
  11049. (buffer-file-name (buffer-base-buffer)))))
  11050. ;; Add a context string
  11051. (when (org-xor org-context-in-file-links arg)
  11052. (setq txt (if (org-region-active-p)
  11053. (buffer-substring (region-beginning) (region-end))
  11054. (buffer-substring (point-at-bol) (point-at-eol))))
  11055. ;; Only use search option if there is some text.
  11056. (when (string-match "\\S-" txt)
  11057. (setq cpltxt
  11058. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11059. desc "NONE")))
  11060. (setq link (org-make-link cpltxt)))
  11061. ((interactive-p)
  11062. (error "Cannot link to a buffer which is not visiting a file"))
  11063. (t (setq link nil)))
  11064. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11065. (setq link (or link cpltxt)
  11066. desc (or desc cpltxt))
  11067. (if (equal desc "NONE") (setq desc nil))
  11068. (if (and (interactive-p) link)
  11069. (progn
  11070. (setq org-stored-links
  11071. (cons (list link desc) org-stored-links))
  11072. (message "Stored: %s" (or desc link)))
  11073. (and link (org-make-link-string link desc)))))
  11074. (defun org-store-link-props (&rest plist)
  11075. "Store link properties, extract names and addresses."
  11076. (let (x adr)
  11077. (when (setq x (plist-get plist :from))
  11078. (setq adr (mail-extract-address-components x))
  11079. (plist-put plist :fromname (car adr))
  11080. (plist-put plist :fromaddress (nth 1 adr)))
  11081. (when (setq x (plist-get plist :to))
  11082. (setq adr (mail-extract-address-components x))
  11083. (plist-put plist :toname (car adr))
  11084. (plist-put plist :toaddress (nth 1 adr))))
  11085. (let ((from (plist-get plist :from))
  11086. (to (plist-get plist :to)))
  11087. (when (and from to org-from-is-user-regexp)
  11088. (plist-put plist :fromto
  11089. (if (string-match org-from-is-user-regexp from)
  11090. (concat "to %t")
  11091. (concat "from %f")))))
  11092. (setq org-store-link-plist plist))
  11093. (defun org-email-link-description (&optional fmt)
  11094. "Return the description part of an email link.
  11095. This takes information from `org-store-link-plist' and formats it
  11096. according to FMT (default from `org-email-link-description-format')."
  11097. (setq fmt (or fmt org-email-link-description-format))
  11098. (let* ((p org-store-link-plist)
  11099. (to (plist-get p :toaddress))
  11100. (from (plist-get p :fromaddress))
  11101. (table
  11102. (list
  11103. (cons "%c" (plist-get p :fromto))
  11104. (cons "%F" (plist-get p :from))
  11105. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11106. (cons "%T" (plist-get p :to))
  11107. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11108. (cons "%s" (plist-get p :subject))
  11109. (cons "%m" (plist-get p :message-id)))))
  11110. (when (string-match "%c" fmt)
  11111. ;; Check if the user wrote this message
  11112. (if (and org-from-is-user-regexp from to
  11113. (save-match-data (string-match org-from-is-user-regexp from)))
  11114. (setq fmt (replace-match "to %t" t t fmt))
  11115. (setq fmt (replace-match "from %f" t t fmt))))
  11116. (org-replace-escapes fmt table)))
  11117. (defun org-make-org-heading-search-string (&optional string heading)
  11118. "Make search string for STRING or current headline."
  11119. (interactive)
  11120. (let ((s (or string (org-get-heading))))
  11121. (unless (and string (not heading))
  11122. ;; We are using a headline, clean up garbage in there.
  11123. (if (string-match org-todo-regexp s)
  11124. (setq s (replace-match "" t t s)))
  11125. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11126. (setq s (replace-match "" t t s)))
  11127. (setq s (org-trim s))
  11128. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11129. org-comment-string "\\)") s)
  11130. (setq s (replace-match "" t t s)))
  11131. (while (string-match org-ts-regexp s)
  11132. (setq s (replace-match "" t t s))))
  11133. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11134. (setq s (replace-match " " t t s)))
  11135. (or string (setq s (concat "*" s))) ; Add * for headlines
  11136. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11137. (defun org-make-link (&rest strings)
  11138. "Concatenate STRINGS."
  11139. (apply 'concat strings))
  11140. (defun org-make-link-string (link &optional description)
  11141. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11142. (unless (string-match "\\S-" link)
  11143. (error "Empty link"))
  11144. (when (stringp description)
  11145. ;; Remove brackets from the description, they are fatal.
  11146. (while (string-match "\\[" description)
  11147. (setq description (replace-match "{" t t description)))
  11148. (while (string-match "\\]" description)
  11149. (setq description (replace-match "}" t t description))))
  11150. (when (equal (org-link-escape link) description)
  11151. ;; No description needed, it is identical
  11152. (setq description nil))
  11153. (when (and (not description)
  11154. (not (equal link (org-link-escape link))))
  11155. (setq description link))
  11156. (concat "[[" (org-link-escape link) "]"
  11157. (if description (concat "[" description "]") "")
  11158. "]"))
  11159. (defconst org-link-escape-chars
  11160. '((?\ . "%20")
  11161. (?\[ . "%5B")
  11162. (?\] . "%5D")
  11163. (?\340 . "%E0") ; `a
  11164. (?\342 . "%E2") ; ^a
  11165. (?\347 . "%E7") ; ,c
  11166. (?\350 . "%E8") ; `e
  11167. (?\351 . "%E9") ; 'e
  11168. (?\352 . "%EA") ; ^e
  11169. (?\356 . "%EE") ; ^i
  11170. (?\364 . "%F4") ; ^o
  11171. (?\371 . "%F9") ; `u
  11172. (?\373 . "%FB") ; ^u
  11173. (?\; . "%3B")
  11174. (?? . "%3F")
  11175. (?= . "%3D")
  11176. (?+ . "%2B")
  11177. )
  11178. "Association list of escapes for some characters problematic in links.
  11179. This is the list that is used for internal purposes.")
  11180. (defconst org-link-escape-chars-browser
  11181. '((?\ . "%20")) ; 32 for the SPC char
  11182. "Association list of escapes for some characters problematic in links.
  11183. This is the list that is used before handing over to the browser.")
  11184. (defun org-link-escape (text &optional table)
  11185. "Escape charaters in TEXT that are problematic for links."
  11186. (setq table (or table org-link-escape-chars))
  11187. (when text
  11188. (let ((re (mapconcat (lambda (x) (regexp-quote
  11189. (char-to-string (car x))))
  11190. table "\\|")))
  11191. (while (string-match re text)
  11192. (setq text
  11193. (replace-match
  11194. (cdr (assoc (string-to-char (match-string 0 text))
  11195. table))
  11196. t t text)))
  11197. text)))
  11198. (defun org-link-unescape (text &optional table)
  11199. "Reverse the action of `org-link-escape'."
  11200. (setq table (or table org-link-escape-chars))
  11201. (when text
  11202. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11203. table "\\|")))
  11204. (while (string-match re text)
  11205. (setq text
  11206. (replace-match
  11207. (char-to-string (car (rassoc (match-string 0 text) table)))
  11208. t t text)))
  11209. text)))
  11210. (defun org-xor (a b)
  11211. "Exclusive or."
  11212. (if a (not b) b))
  11213. (defun org-get-header (header)
  11214. "Find a header field in the current buffer."
  11215. (save-excursion
  11216. (goto-char (point-min))
  11217. (let ((case-fold-search t) s)
  11218. (cond
  11219. ((eq header 'from)
  11220. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11221. (setq s (match-string 1)))
  11222. (while (string-match "\"" s)
  11223. (setq s (replace-match "" t t s)))
  11224. (if (string-match "[<(].*" s)
  11225. (setq s (replace-match "" t t s))))
  11226. ((eq header 'message-id)
  11227. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11228. (setq s (match-string 1))))
  11229. ((eq header 'subject)
  11230. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11231. (setq s (match-string 1)))))
  11232. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11233. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11234. s)))
  11235. (defun org-fixup-message-id-for-http (s)
  11236. "Replace special characters in a message id, so it can be used in an http query."
  11237. (while (string-match "<" s)
  11238. (setq s (replace-match "%3C" t t s)))
  11239. (while (string-match ">" s)
  11240. (setq s (replace-match "%3E" t t s)))
  11241. (while (string-match "@" s)
  11242. (setq s (replace-match "%40" t t s)))
  11243. s)
  11244. ;;;###autoload
  11245. (defun org-insert-link-global ()
  11246. "Insert a link like Org-mode does.
  11247. This command can be called in any mode to insert a link in Org-mode syntax."
  11248. (interactive)
  11249. (org-run-like-in-org-mode 'org-insert-link))
  11250. (defun org-insert-link (&optional complete-file)
  11251. "Insert a link. At the prompt, enter the link.
  11252. Completion can be used to select a link previously stored with
  11253. `org-store-link'. When the empty string is entered (i.e. if you just
  11254. press RET at the prompt), the link defaults to the most recently
  11255. stored link. As SPC triggers completion in the minibuffer, you need to
  11256. use M-SPC or C-q SPC to force the insertion of a space character.
  11257. You will also be prompted for a description, and if one is given, it will
  11258. be displayed in the buffer instead of the link.
  11259. If there is already a link at point, this command will allow you to edit link
  11260. and description parts.
  11261. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11262. selected using completion. The path to the file will be relative to
  11263. the current directory if the file is in the current directory or a
  11264. subdirectory. Otherwise, the link will be the absolute path as
  11265. completed in the minibuffer (i.e. normally ~/path/to/file).
  11266. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11267. is in the current directory or below.
  11268. With three \\[universal-argument] prefixes, negate the meaning of
  11269. `org-keep-stored-link-after-insertion'."
  11270. (interactive "P")
  11271. (let* ((wcf (current-window-configuration))
  11272. (region (if (org-region-active-p)
  11273. (buffer-substring (region-beginning) (region-end))))
  11274. (remove (and region (list (region-beginning) (region-end))))
  11275. (desc region)
  11276. tmphist ; byte-compile incorrectly complains about this
  11277. link entry file)
  11278. (cond
  11279. ((org-in-regexp org-bracket-link-regexp 1)
  11280. ;; We do have a link at point, and we are going to edit it.
  11281. (setq remove (list (match-beginning 0) (match-end 0)))
  11282. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11283. (setq link (read-string "Link: "
  11284. (org-link-unescape
  11285. (org-match-string-no-properties 1)))))
  11286. ((or (org-in-regexp org-angle-link-re)
  11287. (org-in-regexp org-plain-link-re))
  11288. ;; Convert to bracket link
  11289. (setq remove (list (match-beginning 0) (match-end 0))
  11290. link (read-string "Link: "
  11291. (org-remove-angle-brackets (match-string 0)))))
  11292. ((equal complete-file '(4))
  11293. ;; Completing read for file names.
  11294. (setq file (read-file-name "File: "))
  11295. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11296. (pwd1 (file-name-as-directory (abbreviate-file-name
  11297. (expand-file-name ".")))))
  11298. (cond
  11299. ((equal complete-file '(16))
  11300. (setq link (org-make-link
  11301. "file:"
  11302. (abbreviate-file-name (expand-file-name file)))))
  11303. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11304. (setq link (org-make-link "file:" (match-string 1 file))))
  11305. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11306. (expand-file-name file))
  11307. (setq link (org-make-link
  11308. "file:" (match-string 1 (expand-file-name file)))))
  11309. (t (setq link (org-make-link "file:" file))))))
  11310. (t
  11311. ;; Read link, with completion for stored links.
  11312. (with-output-to-temp-buffer "*Org Links*"
  11313. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11314. (when org-stored-links
  11315. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11316. (princ (mapconcat
  11317. (lambda (x)
  11318. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11319. (reverse org-stored-links) "\n"))))
  11320. (let ((cw (selected-window)))
  11321. (select-window (get-buffer-window "*Org Links*"))
  11322. (shrink-window-if-larger-than-buffer)
  11323. (setq truncate-lines t)
  11324. (select-window cw))
  11325. ;; Fake a link history, containing the stored links.
  11326. (setq tmphist (append (mapcar 'car org-stored-links)
  11327. org-insert-link-history))
  11328. (unwind-protect
  11329. (setq link (org-completing-read
  11330. "Link: "
  11331. (append
  11332. (mapcar (lambda (x) (list (concat (car x) ":")))
  11333. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11334. (mapcar (lambda (x) (list (concat x ":")))
  11335. org-link-types))
  11336. nil nil nil
  11337. 'tmphist
  11338. (or (car (car org-stored-links)))))
  11339. (set-window-configuration wcf)
  11340. (kill-buffer "*Org Links*"))
  11341. (setq entry (assoc link org-stored-links))
  11342. (or entry (push link org-insert-link-history))
  11343. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11344. (not org-keep-stored-link-after-insertion))
  11345. (setq org-stored-links (delq (assoc link org-stored-links)
  11346. org-stored-links)))
  11347. (setq desc (or desc (nth 1 entry)))))
  11348. (if (string-match org-plain-link-re link)
  11349. ;; URL-like link, normalize the use of angular brackets.
  11350. (setq link (org-make-link (org-remove-angle-brackets link))))
  11351. ;; Check if we are linking to the current file with a search option
  11352. ;; If yes, simplify the link by using only the search option.
  11353. (when (and buffer-file-name
  11354. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11355. (let* ((path (match-string 1 link))
  11356. (case-fold-search nil)
  11357. (search (match-string 2 link)))
  11358. (save-match-data
  11359. (if (equal (file-truename buffer-file-name) (file-truename path))
  11360. ;; We are linking to this same file, with a search option
  11361. (setq link search)))))
  11362. ;; Check if we can/should use a relative path. If yes, simplify the link
  11363. (when (string-match "\\<file:\\(.*\\)" link)
  11364. (let* ((path (match-string 1 link))
  11365. (origpath path)
  11366. (case-fold-search nil))
  11367. (cond
  11368. ((eq org-link-file-path-type 'absolute)
  11369. (setq path (abbreviate-file-name (expand-file-name path))))
  11370. ((eq org-link-file-path-type 'noabbrev)
  11371. (setq path (expand-file-name path)))
  11372. ((eq org-link-file-path-type 'relative)
  11373. (setq path (file-relative-name path)))
  11374. (t
  11375. (save-match-data
  11376. (if (string-match (concat "^" (regexp-quote
  11377. (file-name-as-directory
  11378. (expand-file-name "."))))
  11379. (expand-file-name path))
  11380. ;; We are linking a file with relative path name.
  11381. (setq path (substring (expand-file-name path)
  11382. (match-end 0)))))))
  11383. (setq link (concat "file:" path))
  11384. (if (equal desc origpath)
  11385. (setq desc path))))
  11386. (setq desc (read-string "Description: " desc))
  11387. (unless (string-match "\\S-" desc) (setq desc nil))
  11388. (if remove (apply 'delete-region remove))
  11389. (insert (org-make-link-string link desc))))
  11390. (defun org-completing-read (&rest args)
  11391. (let ((minibuffer-local-completion-map
  11392. (copy-keymap minibuffer-local-completion-map)))
  11393. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11394. (apply 'completing-read args)))
  11395. ;;; Opening/following a link
  11396. (defvar org-link-search-failed nil)
  11397. (defun org-next-link ()
  11398. "Move forward to the next link.
  11399. If the link is in hidden text, expose it."
  11400. (interactive)
  11401. (when (and org-link-search-failed (eq this-command last-command))
  11402. (goto-char (point-min))
  11403. (message "Link search wrapped back to beginning of buffer"))
  11404. (setq org-link-search-failed nil)
  11405. (let* ((pos (point))
  11406. (ct (org-context))
  11407. (a (assoc :link ct)))
  11408. (if a (goto-char (nth 2 a)))
  11409. (if (re-search-forward org-any-link-re nil t)
  11410. (progn
  11411. (goto-char (match-beginning 0))
  11412. (if (org-invisible-p) (org-show-context)))
  11413. (goto-char pos)
  11414. (setq org-link-search-failed t)
  11415. (error "No further link found"))))
  11416. (defun org-previous-link ()
  11417. "Move backward to the previous link.
  11418. If the link is in hidden text, expose it."
  11419. (interactive)
  11420. (when (and org-link-search-failed (eq this-command last-command))
  11421. (goto-char (point-max))
  11422. (message "Link search wrapped back to end of buffer"))
  11423. (setq org-link-search-failed nil)
  11424. (let* ((pos (point))
  11425. (ct (org-context))
  11426. (a (assoc :link ct)))
  11427. (if a (goto-char (nth 1 a)))
  11428. (if (re-search-backward org-any-link-re nil t)
  11429. (progn
  11430. (goto-char (match-beginning 0))
  11431. (if (org-invisible-p) (org-show-context)))
  11432. (goto-char pos)
  11433. (setq org-link-search-failed t)
  11434. (error "No further link found"))))
  11435. (defun org-find-file-at-mouse (ev)
  11436. "Open file link or URL at mouse."
  11437. (interactive "e")
  11438. (mouse-set-point ev)
  11439. (org-open-at-point 'in-emacs))
  11440. (defun org-open-at-mouse (ev)
  11441. "Open file link or URL at mouse."
  11442. (interactive "e")
  11443. (mouse-set-point ev)
  11444. (org-open-at-point))
  11445. (defvar org-window-config-before-follow-link nil
  11446. "The window configuration before following a link.
  11447. This is saved in case the need arises to restore it.")
  11448. (defvar org-open-link-marker (make-marker)
  11449. "Marker pointing to the location where `org-open-at-point; was called.")
  11450. ;;;###autoload
  11451. (defun org-open-at-point-global ()
  11452. "Follow a link like Org-mode does.
  11453. This command can be called in any mode to follow a link that has
  11454. Org-mode syntax."
  11455. (interactive)
  11456. (org-run-like-in-org-mode 'org-open-at-point))
  11457. (defun org-open-at-point (&optional in-emacs)
  11458. "Open link at or after point.
  11459. If there is no link at point, this function will search forward up to
  11460. the end of the current subtree.
  11461. Normally, files will be opened by an appropriate application. If the
  11462. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11463. (interactive "P")
  11464. (catch 'abort
  11465. (move-marker org-open-link-marker (point))
  11466. (setq org-window-config-before-follow-link (current-window-configuration))
  11467. (org-remove-occur-highlights nil nil t)
  11468. (if (org-at-timestamp-p t)
  11469. (org-follow-timestamp-link)
  11470. (let (type path link line search (pos (point)))
  11471. (catch 'match
  11472. (save-excursion
  11473. (skip-chars-forward "^]\n\r")
  11474. (when (org-in-regexp org-bracket-link-regexp)
  11475. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11476. (while (string-match " *\n *" link)
  11477. (setq link (replace-match " " t t link)))
  11478. (setq link (org-link-expand-abbrev link))
  11479. (if (string-match org-link-re-with-space2 link)
  11480. (setq type (match-string 1 link) path (match-string 2 link))
  11481. (setq type "thisfile" path link))
  11482. (throw 'match t)))
  11483. (when (get-text-property (point) 'org-linked-text)
  11484. (setq type "thisfile"
  11485. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11486. (1+ (point)) (point))
  11487. path (buffer-substring
  11488. (previous-single-property-change pos 'org-linked-text)
  11489. (next-single-property-change pos 'org-linked-text)))
  11490. (throw 'match t))
  11491. (save-excursion
  11492. (when (or (org-in-regexp org-angle-link-re)
  11493. (org-in-regexp org-plain-link-re))
  11494. (setq type (match-string 1) path (match-string 2))
  11495. (throw 'match t)))
  11496. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11497. (setq type "tree-match"
  11498. path (match-string 1))
  11499. (throw 'match t))
  11500. (save-excursion
  11501. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11502. (setq type "tags"
  11503. path (match-string 1))
  11504. (while (string-match ":" path)
  11505. (setq path (replace-match "+" t t path)))
  11506. (throw 'match t))))
  11507. (unless path
  11508. (error "No link found"))
  11509. ;; Remove any trailing spaces in path
  11510. (if (string-match " +\\'" path)
  11511. (setq path (replace-match "" t t path)))
  11512. (cond
  11513. ((assoc type org-link-protocols)
  11514. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11515. ((equal type "mailto")
  11516. (let ((cmd (car org-link-mailto-program))
  11517. (args (cdr org-link-mailto-program)) args1
  11518. (address path) (subject "") a)
  11519. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11520. (setq address (match-string 1 path)
  11521. subject (org-link-escape (match-string 2 path))))
  11522. (while args
  11523. (cond
  11524. ((not (stringp (car args))) (push (pop args) args1))
  11525. (t (setq a (pop args))
  11526. (if (string-match "%a" a)
  11527. (setq a (replace-match address t t a)))
  11528. (if (string-match "%s" a)
  11529. (setq a (replace-match subject t t a)))
  11530. (push a args1))))
  11531. (apply cmd (nreverse args1))))
  11532. ((member type '("http" "https" "ftp" "news"))
  11533. (browse-url (concat type ":" (org-link-escape
  11534. path org-link-escape-chars-browser))))
  11535. ((member type '("message"))
  11536. (browse-url (concat type ":" path)))
  11537. ((string= type "tags")
  11538. (org-tags-view in-emacs path))
  11539. ((string= type "thisfile")
  11540. (if in-emacs
  11541. (switch-to-buffer-other-window
  11542. (org-get-buffer-for-internal-link (current-buffer)))
  11543. (org-mark-ring-push))
  11544. (let ((cmd `(org-link-search
  11545. ,path
  11546. ,(cond ((equal in-emacs '(4)) 'occur)
  11547. ((equal in-emacs '(16)) 'org-occur)
  11548. (t nil))
  11549. ,pos)))
  11550. (condition-case nil (eval cmd)
  11551. (error (progn (widen) (eval cmd))))))
  11552. ((string= type "tree-match")
  11553. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11554. ((string= type "file")
  11555. (if (string-match "::\\([0-9]+\\)\\'" path)
  11556. (setq line (string-to-number (match-string 1 path))
  11557. path (substring path 0 (match-beginning 0)))
  11558. (if (string-match "::\\(.+\\)\\'" path)
  11559. (setq search (match-string 1 path)
  11560. path (substring path 0 (match-beginning 0)))))
  11561. (if (string-match "[*?{]" (file-name-nondirectory path))
  11562. (dired path)
  11563. (org-open-file path in-emacs line search)))
  11564. ((string= type "news")
  11565. (org-follow-gnus-link path))
  11566. ((string= type "bbdb")
  11567. (org-follow-bbdb-link path))
  11568. ((string= type "info")
  11569. (org-follow-info-link path))
  11570. ((string= type "gnus")
  11571. (let (group article)
  11572. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11573. (error "Error in Gnus link"))
  11574. (setq group (match-string 1 path)
  11575. article (match-string 3 path))
  11576. (org-follow-gnus-link group article)))
  11577. ((string= type "vm")
  11578. (let (folder article)
  11579. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11580. (error "Error in VM link"))
  11581. (setq folder (match-string 1 path)
  11582. article (match-string 3 path))
  11583. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11584. (org-follow-vm-link folder article in-emacs)))
  11585. ((string= type "wl")
  11586. (let (folder article)
  11587. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11588. (error "Error in Wanderlust link"))
  11589. (setq folder (match-string 1 path)
  11590. article (match-string 3 path))
  11591. (org-follow-wl-link folder article)))
  11592. ((string= type "mhe")
  11593. (let (folder article)
  11594. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11595. (error "Error in MHE link"))
  11596. (setq folder (match-string 1 path)
  11597. article (match-string 3 path))
  11598. (org-follow-mhe-link folder article)))
  11599. ((string= type "rmail")
  11600. (let (folder article)
  11601. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11602. (error "Error in RMAIL link"))
  11603. (setq folder (match-string 1 path)
  11604. article (match-string 3 path))
  11605. (org-follow-rmail-link folder article)))
  11606. ((string= type "shell")
  11607. (let ((cmd path))
  11608. (if (or (not org-confirm-shell-link-function)
  11609. (funcall org-confirm-shell-link-function
  11610. (format "Execute \"%s\" in shell? "
  11611. (org-add-props cmd nil
  11612. 'face 'org-warning))))
  11613. (progn
  11614. (message "Executing %s" cmd)
  11615. (shell-command cmd))
  11616. (error "Abort"))))
  11617. ((string= type "elisp")
  11618. (let ((cmd path))
  11619. (if (or (not org-confirm-elisp-link-function)
  11620. (funcall org-confirm-elisp-link-function
  11621. (format "Execute \"%s\" as elisp? "
  11622. (org-add-props cmd nil
  11623. 'face 'org-warning))))
  11624. (message "%s => %s" cmd (eval (read cmd)))
  11625. (error "Abort"))))
  11626. (t
  11627. (browse-url-at-point)))))
  11628. (move-marker org-open-link-marker nil)))
  11629. ;;; File search
  11630. (defvar org-create-file-search-functions nil
  11631. "List of functions to construct the right search string for a file link.
  11632. These functions are called in turn with point at the location to
  11633. which the link should point.
  11634. A function in the hook should first test if it would like to
  11635. handle this file type, for example by checking the major-mode or
  11636. the file extension. If it decides not to handle this file, it
  11637. should just return nil to give other functions a chance. If it
  11638. does handle the file, it must return the search string to be used
  11639. when following the link. The search string will be part of the
  11640. file link, given after a double colon, and `org-open-at-point'
  11641. will automatically search for it. If special measures must be
  11642. taken to make the search successful, another function should be
  11643. added to the companion hook `org-execute-file-search-functions',
  11644. which see.
  11645. A function in this hook may also use `setq' to set the variable
  11646. `description' to provide a suggestion for the descriptive text to
  11647. be used for this link when it gets inserted into an Org-mode
  11648. buffer with \\[org-insert-link].")
  11649. (defvar org-execute-file-search-functions nil
  11650. "List of functions to execute a file search triggered by a link.
  11651. Functions added to this hook must accept a single argument, the
  11652. search string that was part of the file link, the part after the
  11653. double colon. The function must first check if it would like to
  11654. handle this search, for example by checking the major-mode or the
  11655. file extension. If it decides not to handle this search, it
  11656. should just return nil to give other functions a chance. If it
  11657. does handle the search, it must return a non-nil value to keep
  11658. other functions from trying.
  11659. Each function can access the current prefix argument through the
  11660. variable `current-prefix-argument'. Note that a single prefix is
  11661. used to force opening a link in Emacs, so it may be good to only
  11662. use a numeric or double prefix to guide the search function.
  11663. In case this is needed, a function in this hook can also restore
  11664. the window configuration before `org-open-at-point' was called using:
  11665. (set-window-configuration org-window-config-before-follow-link)")
  11666. (defun org-link-search (s &optional type avoid-pos)
  11667. "Search for a link search option.
  11668. If S is surrounded by forward slashes, it is interpreted as a
  11669. regular expression. In org-mode files, this will create an `org-occur'
  11670. sparse tree. In ordinary files, `occur' will be used to list matches.
  11671. If the current buffer is in `dired-mode', grep will be used to search
  11672. in all files. If AVOID-POS is given, ignore matches near that position."
  11673. (let ((case-fold-search t)
  11674. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11675. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11676. (append '(("") (" ") ("\t") ("\n"))
  11677. org-emphasis-alist)
  11678. "\\|") "\\)"))
  11679. (pos (point))
  11680. (pre "") (post "")
  11681. words re0 re1 re2 re3 re4 re5 re2a reall)
  11682. (cond
  11683. ;; First check if there are any special
  11684. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11685. ;; Now try the builtin stuff
  11686. ((save-excursion
  11687. (goto-char (point-min))
  11688. (and
  11689. (re-search-forward
  11690. (concat "<<" (regexp-quote s0) ">>") nil t)
  11691. (setq pos (match-beginning 0))))
  11692. ;; There is an exact target for this
  11693. (goto-char pos))
  11694. ((string-match "^/\\(.*\\)/$" s)
  11695. ;; A regular expression
  11696. (cond
  11697. ((org-mode-p)
  11698. (org-occur (match-string 1 s)))
  11699. ;;((eq major-mode 'dired-mode)
  11700. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11701. (t (org-do-occur (match-string 1 s)))))
  11702. (t
  11703. ;; A normal search strings
  11704. (when (equal (string-to-char s) ?*)
  11705. ;; Anchor on headlines, post may include tags.
  11706. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11707. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11708. s (substring s 1)))
  11709. (remove-text-properties
  11710. 0 (length s)
  11711. '(face nil mouse-face nil keymap nil fontified nil) s)
  11712. ;; Make a series of regular expressions to find a match
  11713. (setq words (org-split-string s "[ \n\r\t]+")
  11714. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11715. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11716. "\\)" markers)
  11717. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11718. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11719. re1 (concat pre re2 post)
  11720. re3 (concat pre re4 post)
  11721. re5 (concat pre ".*" re4)
  11722. re2 (concat pre re2)
  11723. re2a (concat pre re2a)
  11724. re4 (concat pre re4)
  11725. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11726. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11727. re5 "\\)"
  11728. ))
  11729. (cond
  11730. ((eq type 'org-occur) (org-occur reall))
  11731. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11732. (t (goto-char (point-min))
  11733. (if (or (org-search-not-self 1 re0 nil t)
  11734. (org-search-not-self 1 re1 nil t)
  11735. (org-search-not-self 1 re2 nil t)
  11736. (org-search-not-self 1 re2a nil t)
  11737. (org-search-not-self 1 re3 nil t)
  11738. (org-search-not-self 1 re4 nil t)
  11739. (org-search-not-self 1 re5 nil t)
  11740. )
  11741. (goto-char (match-beginning 1))
  11742. (goto-char pos)
  11743. (error "No match")))))
  11744. (t
  11745. ;; Normal string-search
  11746. (goto-char (point-min))
  11747. (if (search-forward s nil t)
  11748. (goto-char (match-beginning 0))
  11749. (error "No match"))))
  11750. (and (org-mode-p) (org-show-context 'link-search))))
  11751. (defun org-search-not-self (group &rest args)
  11752. "Execute `re-search-forward', but only accept matches that do not
  11753. enclose the position of `org-open-link-marker'."
  11754. (let ((m org-open-link-marker))
  11755. (catch 'exit
  11756. (while (apply 're-search-forward args)
  11757. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11758. (goto-char (match-end group))
  11759. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11760. (> (match-beginning 0) (marker-position m))
  11761. (< (match-end 0) (marker-position m)))
  11762. (save-match-data
  11763. (or (not (org-in-regexp
  11764. org-bracket-link-analytic-regexp 1))
  11765. (not (match-end 4)) ; no description
  11766. (and (<= (match-beginning 4) (point))
  11767. (>= (match-end 4) (point))))))
  11768. (throw 'exit (point))))))))
  11769. (defun org-get-buffer-for-internal-link (buffer)
  11770. "Return a buffer to be used for displaying the link target of internal links."
  11771. (cond
  11772. ((not org-display-internal-link-with-indirect-buffer)
  11773. buffer)
  11774. ((string-match "(Clone)$" (buffer-name buffer))
  11775. (message "Buffer is already a clone, not making another one")
  11776. ;; we also do not modify visibility in this case
  11777. buffer)
  11778. (t ; make a new indirect buffer for displaying the link
  11779. (let* ((bn (buffer-name buffer))
  11780. (ibn (concat bn "(Clone)"))
  11781. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11782. (with-current-buffer ib (org-overview))
  11783. ib))))
  11784. (defun org-do-occur (regexp &optional cleanup)
  11785. "Call the Emacs command `occur'.
  11786. If CLEANUP is non-nil, remove the printout of the regular expression
  11787. in the *Occur* buffer. This is useful if the regex is long and not useful
  11788. to read."
  11789. (occur regexp)
  11790. (when cleanup
  11791. (let ((cwin (selected-window)) win beg end)
  11792. (when (setq win (get-buffer-window "*Occur*"))
  11793. (select-window win))
  11794. (goto-char (point-min))
  11795. (when (re-search-forward "match[a-z]+" nil t)
  11796. (setq beg (match-end 0))
  11797. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11798. (setq end (1- (match-beginning 0)))))
  11799. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11800. (goto-char (point-min))
  11801. (select-window cwin))))
  11802. ;;; The mark ring for links jumps
  11803. (defvar org-mark-ring nil
  11804. "Mark ring for positions before jumps in Org-mode.")
  11805. (defvar org-mark-ring-last-goto nil
  11806. "Last position in the mark ring used to go back.")
  11807. ;; Fill and close the ring
  11808. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11809. (loop for i from 1 to org-mark-ring-length do
  11810. (push (make-marker) org-mark-ring))
  11811. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11812. org-mark-ring)
  11813. (defun org-mark-ring-push (&optional pos buffer)
  11814. "Put the current position or POS into the mark ring and rotate it."
  11815. (interactive)
  11816. (setq pos (or pos (point)))
  11817. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11818. (move-marker (car org-mark-ring)
  11819. (or pos (point))
  11820. (or buffer (current-buffer)))
  11821. (message "%s"
  11822. (substitute-command-keys
  11823. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11824. (defun org-mark-ring-goto (&optional n)
  11825. "Jump to the previous position in the mark ring.
  11826. With prefix arg N, jump back that many stored positions. When
  11827. called several times in succession, walk through the entire ring.
  11828. Org-mode commands jumping to a different position in the current file,
  11829. or to another Org-mode file, automatically push the old position
  11830. onto the ring."
  11831. (interactive "p")
  11832. (let (p m)
  11833. (if (eq last-command this-command)
  11834. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11835. (setq p org-mark-ring))
  11836. (setq org-mark-ring-last-goto p)
  11837. (setq m (car p))
  11838. (switch-to-buffer (marker-buffer m))
  11839. (goto-char m)
  11840. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11841. (defun org-remove-angle-brackets (s)
  11842. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11843. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11844. s)
  11845. (defun org-add-angle-brackets (s)
  11846. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11847. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11848. s)
  11849. ;;; Following specific links
  11850. (defun org-follow-timestamp-link ()
  11851. (cond
  11852. ((org-at-date-range-p t)
  11853. (let ((org-agenda-start-on-weekday)
  11854. (t1 (match-string 1))
  11855. (t2 (match-string 2)))
  11856. (setq t1 (time-to-days (org-time-string-to-time t1))
  11857. t2 (time-to-days (org-time-string-to-time t2)))
  11858. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11859. ((org-at-timestamp-p t)
  11860. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11861. (substring (match-string 1) 0 10)))
  11862. 1))
  11863. (t (error "This should not happen"))))
  11864. (defun org-follow-bbdb-link (name)
  11865. "Follow a BBDB link to NAME."
  11866. (require 'bbdb)
  11867. (let ((inhibit-redisplay (not debug-on-error))
  11868. (bbdb-electric-p nil))
  11869. (catch 'exit
  11870. ;; Exact match on name
  11871. (bbdb-name (concat "\\`" name "\\'") nil)
  11872. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11873. ;; Exact match on name
  11874. (bbdb-company (concat "\\`" name "\\'") nil)
  11875. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11876. ;; Partial match on name
  11877. (bbdb-name name nil)
  11878. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11879. ;; Partial match on company
  11880. (bbdb-company name nil)
  11881. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11882. ;; General match including network address and notes
  11883. (bbdb name nil)
  11884. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11885. (delete-window (get-buffer-window "*BBDB*"))
  11886. (error "No matching BBDB record")))))
  11887. (defun org-follow-info-link (name)
  11888. "Follow an info file & node link to NAME."
  11889. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11890. (string-match "\\(.*\\)" name))
  11891. (progn
  11892. (require 'info)
  11893. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11894. (Info-find-node (match-string 1 name) (match-string 2 name))
  11895. (Info-find-node (match-string 1 name) "Top")))
  11896. (message "Could not open: %s" name)))
  11897. (defun org-follow-gnus-link (&optional group article)
  11898. "Follow a Gnus link to GROUP and ARTICLE."
  11899. (require 'gnus)
  11900. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11901. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11902. (cond ((and group article)
  11903. (gnus-group-read-group 1 nil group)
  11904. (gnus-summary-goto-article (string-to-number article) nil t))
  11905. (group (gnus-group-jump-to-group group))))
  11906. (defun org-follow-vm-link (&optional folder article readonly)
  11907. "Follow a VM link to FOLDER and ARTICLE."
  11908. (require 'vm)
  11909. (setq article (org-add-angle-brackets article))
  11910. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11911. ;; ange-ftp or efs or tramp access
  11912. (let ((user (or (match-string 1 folder) (user-login-name)))
  11913. (host (match-string 2 folder))
  11914. (file (match-string 3 folder)))
  11915. (cond
  11916. ((featurep 'tramp)
  11917. ;; use tramp to access the file
  11918. (if (featurep 'xemacs)
  11919. (setq folder (format "[%s@%s]%s" user host file))
  11920. (setq folder (format "/%s@%s:%s" user host file))))
  11921. (t
  11922. ;; use ange-ftp or efs
  11923. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11924. (setq folder (format "/%s@%s:%s" user host file))))))
  11925. (when folder
  11926. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11927. (sit-for 0.1)
  11928. (when article
  11929. (vm-select-folder-buffer)
  11930. (widen)
  11931. (let ((case-fold-search t))
  11932. (goto-char (point-min))
  11933. (if (not (re-search-forward
  11934. (concat "^" "message-id: *" (regexp-quote article))))
  11935. (error "Could not find the specified message in this folder"))
  11936. (vm-isearch-update)
  11937. (vm-isearch-narrow)
  11938. (vm-beginning-of-message)
  11939. (vm-summarize)))))
  11940. (defun org-follow-wl-link (folder article)
  11941. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11942. (if (and (string= folder "%")
  11943. article
  11944. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11945. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11946. ;; Thus, we recompose folder and article ids.
  11947. (setq folder (format "%s#%s" folder (match-string 1 article))
  11948. article (match-string 3 article)))
  11949. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11950. (error "No such folder: %s" folder))
  11951. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11952. (and article
  11953. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11954. (wl-summary-redisplay)))
  11955. (defun org-follow-rmail-link (folder article)
  11956. "Follow an RMAIL link to FOLDER and ARTICLE."
  11957. (setq article (org-add-angle-brackets article))
  11958. (let (message-number)
  11959. (save-excursion
  11960. (save-window-excursion
  11961. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11962. (setq message-number
  11963. (save-restriction
  11964. (widen)
  11965. (goto-char (point-max))
  11966. (if (re-search-backward
  11967. (concat "^Message-ID:\\s-+" (regexp-quote
  11968. (or article "")))
  11969. nil t)
  11970. (rmail-what-message))))))
  11971. (if message-number
  11972. (progn
  11973. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11974. (rmail-show-message message-number)
  11975. message-number)
  11976. (error "Message not found"))))
  11977. ;;; mh-e integration based on planner-mode
  11978. (defun org-mhe-get-message-real-folder ()
  11979. "Return the name of the current message real folder, so if you use
  11980. sequences, it will now work."
  11981. (save-excursion
  11982. (let* ((folder
  11983. (if (equal major-mode 'mh-folder-mode)
  11984. mh-current-folder
  11985. ;; Refer to the show buffer
  11986. mh-show-folder-buffer))
  11987. (end-index
  11988. (if (boundp 'mh-index-folder)
  11989. (min (length mh-index-folder) (length folder))))
  11990. )
  11991. ;; a simple test on mh-index-data does not work, because
  11992. ;; mh-index-data is always nil in a show buffer.
  11993. (if (and (boundp 'mh-index-folder)
  11994. (string= mh-index-folder (substring folder 0 end-index)))
  11995. (if (equal major-mode 'mh-show-mode)
  11996. (save-window-excursion
  11997. (let (pop-up-frames)
  11998. (when (buffer-live-p (get-buffer folder))
  11999. (progn
  12000. (pop-to-buffer folder)
  12001. (org-mhe-get-message-folder-from-index)
  12002. )
  12003. )))
  12004. (org-mhe-get-message-folder-from-index)
  12005. )
  12006. folder
  12007. )
  12008. )))
  12009. (defun org-mhe-get-message-folder-from-index ()
  12010. "Returns the name of the message folder in a index folder buffer."
  12011. (save-excursion
  12012. (mh-index-previous-folder)
  12013. (re-search-forward "^\\(+.*\\)$" nil t)
  12014. (message "%s" (match-string 1))))
  12015. (defun org-mhe-get-message-folder ()
  12016. "Return the name of the current message folder. Be careful if you
  12017. use sequences."
  12018. (save-excursion
  12019. (if (equal major-mode 'mh-folder-mode)
  12020. mh-current-folder
  12021. ;; Refer to the show buffer
  12022. mh-show-folder-buffer)))
  12023. (defun org-mhe-get-message-num ()
  12024. "Return the number of the current message. Be careful if you
  12025. use sequences."
  12026. (save-excursion
  12027. (if (equal major-mode 'mh-folder-mode)
  12028. (mh-get-msg-num nil)
  12029. ;; Refer to the show buffer
  12030. (mh-show-buffer-message-number))))
  12031. (defun org-mhe-get-header (header)
  12032. "Return a header of the message in folder mode. This will create a
  12033. show buffer for the corresponding message. If you have a more clever
  12034. idea..."
  12035. (let* ((folder (org-mhe-get-message-folder))
  12036. (num (org-mhe-get-message-num))
  12037. (buffer (get-buffer-create (concat "show-" folder)))
  12038. (header-field))
  12039. (with-current-buffer buffer
  12040. (mh-display-msg num folder)
  12041. (if (equal major-mode 'mh-folder-mode)
  12042. (mh-header-display)
  12043. (mh-show-header-display))
  12044. (set-buffer buffer)
  12045. (setq header-field (mh-get-header-field header))
  12046. (if (equal major-mode 'mh-folder-mode)
  12047. (mh-show)
  12048. (mh-show-show))
  12049. header-field)))
  12050. (defun org-follow-mhe-link (folder article)
  12051. "Follow an MHE link to FOLDER and ARTICLE.
  12052. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12053. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12054. ARTICLE is searched in all folders. Indexed searches (swish++,
  12055. namazu, and others supported by MH-E) will always search in all
  12056. folders."
  12057. (require 'mh-e)
  12058. (require 'mh-search)
  12059. (require 'mh-utils)
  12060. (mh-find-path)
  12061. (if (not article)
  12062. (mh-visit-folder (mh-normalize-folder-name folder))
  12063. (setq article (org-add-angle-brackets article))
  12064. (mh-search-choose)
  12065. (if (equal mh-searcher 'pick)
  12066. (progn
  12067. (mh-search folder (list "--message-id" article))
  12068. (when (and org-mhe-search-all-folders
  12069. (not (org-mhe-get-message-real-folder)))
  12070. (kill-this-buffer)
  12071. (mh-search "+" (list "--message-id" article))))
  12072. (mh-search "+" article))
  12073. (if (org-mhe-get-message-real-folder)
  12074. (mh-show-msg 1)
  12075. (kill-this-buffer)
  12076. (error "Message not found"))))
  12077. ;;; BibTeX links
  12078. ;; Use the custom search meachnism to construct and use search strings for
  12079. ;; file links to BibTeX database entries.
  12080. (defun org-create-file-search-in-bibtex ()
  12081. "Create the search string and description for a BibTeX database entry."
  12082. (when (eq major-mode 'bibtex-mode)
  12083. ;; yes, we want to construct this search string.
  12084. ;; Make a good description for this entry, using names, year and the title
  12085. ;; Put it into the `description' variable which is dynamically scoped.
  12086. (let ((bibtex-autokey-names 1)
  12087. (bibtex-autokey-names-stretch 1)
  12088. (bibtex-autokey-name-case-convert-function 'identity)
  12089. (bibtex-autokey-name-separator " & ")
  12090. (bibtex-autokey-additional-names " et al.")
  12091. (bibtex-autokey-year-length 4)
  12092. (bibtex-autokey-name-year-separator " ")
  12093. (bibtex-autokey-titlewords 3)
  12094. (bibtex-autokey-titleword-separator " ")
  12095. (bibtex-autokey-titleword-case-convert-function 'identity)
  12096. (bibtex-autokey-titleword-length 'infty)
  12097. (bibtex-autokey-year-title-separator ": "))
  12098. (setq description (bibtex-generate-autokey)))
  12099. ;; Now parse the entry, get the key and return it.
  12100. (save-excursion
  12101. (bibtex-beginning-of-entry)
  12102. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12103. (defun org-execute-file-search-in-bibtex (s)
  12104. "Find the link search string S as a key for a database entry."
  12105. (when (eq major-mode 'bibtex-mode)
  12106. ;; Yes, we want to do the search in this file.
  12107. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12108. (goto-char (point-min))
  12109. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12110. (regexp-quote s) "[ \t\n]*,") nil t)
  12111. (goto-char (match-beginning 0)))
  12112. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12113. ;; Use double prefix to indicate that any web link should be browsed
  12114. (let ((b (current-buffer)) (p (point)))
  12115. ;; Restore the window configuration because we just use the web link
  12116. (set-window-configuration org-window-config-before-follow-link)
  12117. (save-excursion (set-buffer b) (goto-char p)
  12118. (bibtex-url)))
  12119. (recenter 0)) ; Move entry start to beginning of window
  12120. ;; return t to indicate that the search is done.
  12121. t))
  12122. ;; Finally add the functions to the right hooks.
  12123. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12124. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12125. ;; end of Bibtex link setup
  12126. ;;; Following file links
  12127. (defun org-open-file (path &optional in-emacs line search)
  12128. "Open the file at PATH.
  12129. First, this expands any special file name abbreviations. Then the
  12130. configuration variable `org-file-apps' is checked if it contains an
  12131. entry for this file type, and if yes, the corresponding command is launched.
  12132. If no application is found, Emacs simply visits the file.
  12133. With optional argument IN-EMACS, Emacs will visit the file.
  12134. Optional LINE specifies a line to go to, optional SEARCH a string to
  12135. search for. If LINE or SEARCH is given, the file will always be
  12136. opened in Emacs.
  12137. If the file does not exist, an error is thrown."
  12138. (setq in-emacs (or in-emacs line search))
  12139. (let* ((file (if (equal path "")
  12140. buffer-file-name
  12141. (substitute-in-file-name (expand-file-name path))))
  12142. (apps (append org-file-apps (org-default-apps)))
  12143. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12144. (dirp (if remp nil (file-directory-p file)))
  12145. (dfile (downcase file))
  12146. (old-buffer (current-buffer))
  12147. (old-pos (point))
  12148. (old-mode major-mode)
  12149. ext cmd)
  12150. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12151. (setq ext (match-string 1 dfile))
  12152. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12153. (setq ext (match-string 1 dfile))))
  12154. (if in-emacs
  12155. (setq cmd 'emacs)
  12156. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12157. (and dirp (cdr (assoc 'directory apps)))
  12158. (cdr (assoc ext apps))
  12159. (cdr (assoc t apps)))))
  12160. (when (eq cmd 'mailcap)
  12161. (require 'mailcap)
  12162. (mailcap-parse-mailcaps)
  12163. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12164. (command (mailcap-mime-info mime-type)))
  12165. (if (stringp command)
  12166. (setq cmd command)
  12167. (setq cmd 'emacs))))
  12168. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12169. (not (file-exists-p file))
  12170. (not org-open-non-existing-files))
  12171. (error "No such file: %s" file))
  12172. (cond
  12173. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12174. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12175. (while (string-match "['\"]%s['\"]" cmd)
  12176. (setq cmd (replace-match "%s" t t cmd)))
  12177. (while (string-match "%s" cmd)
  12178. (setq cmd (replace-match
  12179. (save-match-data (shell-quote-argument file))
  12180. t t cmd)))
  12181. (save-window-excursion
  12182. (start-process-shell-command cmd nil cmd)))
  12183. ((or (stringp cmd)
  12184. (eq cmd 'emacs))
  12185. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12186. (widen)
  12187. (if line (goto-line line)
  12188. (if search (org-link-search search))))
  12189. ((consp cmd)
  12190. (eval cmd))
  12191. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12192. (and (org-mode-p) (eq old-mode 'org-mode)
  12193. (or (not (equal old-buffer (current-buffer)))
  12194. (not (equal old-pos (point))))
  12195. (org-mark-ring-push old-pos old-buffer))))
  12196. (defun org-default-apps ()
  12197. "Return the default applications for this operating system."
  12198. (cond
  12199. ((eq system-type 'darwin)
  12200. org-file-apps-defaults-macosx)
  12201. ((eq system-type 'windows-nt)
  12202. org-file-apps-defaults-windowsnt)
  12203. (t org-file-apps-defaults-gnu)))
  12204. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12205. (defun org-file-remote-p (file)
  12206. "Test whether FILE specifies a location on a remote system.
  12207. Return non-nil if the location is indeed remote.
  12208. For example, the filename \"/user@host:/foo\" specifies a location
  12209. on the system \"/user@host:\"."
  12210. (cond ((fboundp 'file-remote-p)
  12211. (file-remote-p file))
  12212. ((fboundp 'tramp-handle-file-remote-p)
  12213. (tramp-handle-file-remote-p file))
  12214. ((and (boundp 'ange-ftp-name-format)
  12215. (string-match (car ange-ftp-name-format) file))
  12216. t)
  12217. (t nil)))
  12218. ;;;; Hooks for remember.el, and refiling
  12219. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12220. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12221. ;;;###autoload
  12222. (defun org-remember-insinuate ()
  12223. "Setup remember.el for use wiht Org-mode."
  12224. (require 'remember)
  12225. (setq remember-annotation-functions '(org-remember-annotation))
  12226. (setq remember-handler-functions '(org-remember-handler))
  12227. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12228. ;;;###autoload
  12229. (defun org-remember-annotation ()
  12230. "Return a link to the current location as an annotation for remember.el.
  12231. If you are using Org-mode files as target for data storage with
  12232. remember.el, then the annotations should include a link compatible with the
  12233. conventions in Org-mode. This function returns such a link."
  12234. (org-store-link nil))
  12235. (defconst org-remember-help
  12236. "Select a destination location for the note.
  12237. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12238. RET on headline -> Store as sublevel entry to current headline
  12239. RET at beg-of-buf -> Append to file as level 2 headline
  12240. <left>/<right> -> before/after current headline, same headings level")
  12241. (defvar org-remember-previous-location nil)
  12242. (defvar org-force-remember-template-char) ;; dynamically scoped
  12243. (defun org-select-remember-template (&optional use-char)
  12244. (when org-remember-templates
  12245. (let* ((templates (mapcar (lambda (x)
  12246. (if (stringp (car x))
  12247. (append (list (nth 1 x) (car x)) (cddr x))
  12248. (append (list (car x) "") (cdr x))))
  12249. org-remember-templates))
  12250. (char (or use-char
  12251. (cond
  12252. ((= (length templates) 1)
  12253. (caar templates))
  12254. ((and (boundp 'org-force-remember-template-char)
  12255. org-force-remember-template-char)
  12256. (if (stringp org-force-remember-template-char)
  12257. (string-to-char org-force-remember-template-char)
  12258. org-force-remember-template-char))
  12259. (t
  12260. (message "Select template: %s"
  12261. (mapconcat
  12262. (lambda (x)
  12263. (cond
  12264. ((not (string-match "\\S-" (nth 1 x)))
  12265. (format "[%c]" (car x)))
  12266. ((equal (downcase (car x))
  12267. (downcase (aref (nth 1 x) 0)))
  12268. (format "[%c]%s" (car x)
  12269. (substring (nth 1 x) 1)))
  12270. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12271. templates " "))
  12272. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12273. (when (equal char0 ?\C-g)
  12274. (jump-to-register remember-register)
  12275. (kill-buffer remember-buffer))
  12276. char0))))))
  12277. (cddr (assoc char templates)))))
  12278. (defvar x-last-selected-text)
  12279. (defvar x-last-selected-text-primary)
  12280. ;;;###autoload
  12281. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12282. "Initialize *remember* buffer with template, invoke `org-mode'.
  12283. This function should be placed into `remember-mode-hook' and in fact requires
  12284. to be run from that hook to function properly."
  12285. (if org-remember-templates
  12286. (let* ((entry (org-select-remember-template use-char))
  12287. (tpl (car entry))
  12288. (plist-p (if org-store-link-plist t nil))
  12289. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12290. (string-match "\\S-" (nth 1 entry)))
  12291. (nth 1 entry)
  12292. org-default-notes-file))
  12293. (headline (nth 2 entry))
  12294. (v-c (or (and (eq window-system 'x)
  12295. (fboundp 'x-cut-buffer-or-selection-value)
  12296. (x-cut-buffer-or-selection-value))
  12297. (org-bound-and-true-p x-last-selected-text)
  12298. (org-bound-and-true-p x-last-selected-text-primary)
  12299. (and (> (length kill-ring) 0) (current-kill 0))))
  12300. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12301. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12302. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12303. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12304. ;; `initial' and `annotation' are bound in `remember'
  12305. (v-i (if (boundp 'initial) initial))
  12306. (v-a (if (and (boundp 'annotation) annotation)
  12307. (if (equal annotation "[[]]") "" annotation)
  12308. ""))
  12309. (v-A (if (and v-a
  12310. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12311. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12312. v-a))
  12313. (v-n user-full-name)
  12314. (org-startup-folded nil)
  12315. org-time-was-given org-end-time-was-given x
  12316. prompt completions char time pos default histvar)
  12317. (setq org-store-link-plist
  12318. (append (list :annotation v-a :initial v-i)
  12319. org-store-link-plist))
  12320. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12321. (erase-buffer)
  12322. (insert (substitute-command-keys
  12323. (format
  12324. "## Filing location: Select interactively, default, or last used:
  12325. ## %s to select file and header location interactively.
  12326. ## %s \"%s\" -> \"* %s\"
  12327. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12328. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12329. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12330. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12331. (abbreviate-file-name (or file org-default-notes-file))
  12332. (or headline "")
  12333. (or (car org-remember-previous-location) "???")
  12334. (or (cdr org-remember-previous-location) "???"))))
  12335. (insert tpl) (goto-char (point-min))
  12336. ;; Simple %-escapes
  12337. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12338. (when (and initial (equal (match-string 0) "%i"))
  12339. (save-match-data
  12340. (let* ((lead (buffer-substring
  12341. (point-at-bol) (match-beginning 0))))
  12342. (setq v-i (mapconcat 'identity
  12343. (org-split-string initial "\n")
  12344. (concat "\n" lead))))))
  12345. (replace-match
  12346. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12347. t t))
  12348. ;; %[] Insert contents of a file.
  12349. (goto-char (point-min))
  12350. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12351. (let ((start (match-beginning 0))
  12352. (end (match-end 0))
  12353. (filename (expand-file-name (match-string 1))))
  12354. (goto-char start)
  12355. (delete-region start end)
  12356. (condition-case error
  12357. (insert-file-contents filename)
  12358. (error (insert (format "%%![Couldn't insert %s: %s]"
  12359. filename error))))))
  12360. ;; %() embedded elisp
  12361. (goto-char (point-min))
  12362. (while (re-search-forward "%\\((.+)\\)" nil t)
  12363. (goto-char (match-beginning 0))
  12364. (let ((template-start (point)))
  12365. (forward-char 1)
  12366. (let ((result
  12367. (condition-case error
  12368. (eval (read (current-buffer)))
  12369. (error (format "%%![Error: %s]" error)))))
  12370. (delete-region template-start (point))
  12371. (insert result))))
  12372. ;; From the property list
  12373. (when plist-p
  12374. (goto-char (point-min))
  12375. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12376. (and (setq x (or (plist-get org-store-link-plist
  12377. (intern (match-string 1))) ""))
  12378. (replace-match x t t))))
  12379. ;; Turn on org-mode in the remember buffer, set local variables
  12380. (org-mode)
  12381. (org-set-local 'org-finish-function 'org-remember-finalize)
  12382. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12383. (org-set-local 'org-default-notes-file file))
  12384. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12385. (org-set-local 'org-remember-default-headline headline))
  12386. ;; Interactive template entries
  12387. (goto-char (point-min))
  12388. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12389. (setq char (if (match-end 3) (match-string 3))
  12390. prompt (if (match-end 2) (match-string 2)))
  12391. (goto-char (match-beginning 0))
  12392. (replace-match "")
  12393. (setq completions nil default nil)
  12394. (when prompt
  12395. (setq completions (org-split-string prompt "|")
  12396. prompt (pop completions)
  12397. default (car completions)
  12398. histvar (intern (concat
  12399. "org-remember-template-prompt-history::"
  12400. (or prompt "")))
  12401. completions (mapcar 'list completions)))
  12402. (cond
  12403. ((member char '("G" "g"))
  12404. (let* ((org-last-tags-completion-table
  12405. (org-global-tags-completion-table
  12406. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12407. (org-add-colon-after-tag-completion t)
  12408. (ins (completing-read
  12409. (if prompt (concat prompt ": ") "Tags: ")
  12410. 'org-tags-completion-function nil nil nil
  12411. 'org-tags-history)))
  12412. (setq ins (mapconcat 'identity
  12413. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12414. ":"))
  12415. (when (string-match "\\S-" ins)
  12416. (or (equal (char-before) ?:) (insert ":"))
  12417. (insert ins)
  12418. (or (equal (char-after) ?:) (insert ":")))))
  12419. (char
  12420. (setq org-time-was-given (equal (upcase char) char))
  12421. (setq time (org-read-date (equal (upcase char) "U") t nil
  12422. prompt))
  12423. (org-insert-time-stamp time org-time-was-given
  12424. (member char '("u" "U"))
  12425. nil nil (list org-end-time-was-given)))
  12426. (t
  12427. (insert (org-completing-read
  12428. (concat (if prompt prompt "Enter string")
  12429. (if default (concat " [" default "]"))
  12430. ": ")
  12431. completions nil nil nil histvar default)))))
  12432. (goto-char (point-min))
  12433. (if (re-search-forward "%\\?" nil t)
  12434. (replace-match "")
  12435. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12436. (org-mode)
  12437. (org-set-local 'org-finish-function 'org-remember-finalize))
  12438. (when (save-excursion
  12439. (goto-char (point-min))
  12440. (re-search-forward "%!" nil t))
  12441. (replace-match "")
  12442. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12443. (defun org-remember-finish-immediately ()
  12444. "File remember note immediately.
  12445. This should be run in `post-command-hook' and will remove itself
  12446. from that hook."
  12447. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12448. (when org-finish-function
  12449. (funcall org-finish-function)))
  12450. (defvar org-clock-marker) ; Defined below
  12451. (defun org-remember-finalize ()
  12452. "Finalize the remember process."
  12453. (unless (fboundp 'remember-finalize)
  12454. (defalias 'remember-finalize 'remember-buffer))
  12455. (when (and org-clock-marker
  12456. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12457. ;; FIXME: test this, this is w/o notetaking!
  12458. (let (org-log-note-clock-out) (org-clock-out)))
  12459. (when buffer-file-name
  12460. (save-buffer)
  12461. (setq buffer-file-name nil))
  12462. (remember-finalize))
  12463. ;;;###autoload
  12464. (defun org-remember (&optional goto org-force-remember-template-char)
  12465. "Call `remember'. If this is already a remember buffer, re-apply template.
  12466. If there is an active region, make sure remember uses it as initial content
  12467. of the remember buffer.
  12468. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12469. anything, just go to the file/headline where the selected template usually
  12470. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12471. note stored by remember.
  12472. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12473. associated with a template in `org-remember-templates'."
  12474. (interactive "P")
  12475. (cond
  12476. ((equal goto '(4)) (org-go-to-remember-target))
  12477. ((equal goto '(16)) (org-remember-goto-last-stored))
  12478. (t
  12479. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12480. (progn
  12481. (when (< (length org-remember-templates) 2)
  12482. (error "No other template available"))
  12483. (erase-buffer)
  12484. (let ((annotation (plist-get org-store-link-plist :annotation))
  12485. (initial (plist-get org-store-link-plist :initial)))
  12486. (org-remember-apply-template))
  12487. (message "Press C-c C-c to remember data"))
  12488. (if (org-region-active-p)
  12489. (remember (buffer-substring (point) (mark)))
  12490. (call-interactively 'remember))))))
  12491. (defun org-remember-goto-last-stored ()
  12492. "Go to the location where the last remember note was stored."
  12493. (interactive)
  12494. (bookmark-jump "org-remember-last-stored")
  12495. (message "This is the last note stored by remember"))
  12496. (defun org-go-to-remember-target (&optional template-key)
  12497. "Go to the target location of a remember template.
  12498. The user is queried for the template."
  12499. (interactive)
  12500. (let* ((entry (org-select-remember-template template-key))
  12501. (file (nth 1 entry))
  12502. (heading (nth 2 entry))
  12503. visiting)
  12504. (unless (and file (stringp file) (string-match "\\S-" file))
  12505. (setq file org-default-notes-file))
  12506. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12507. (setq heading org-remember-default-headline))
  12508. (setq visiting (org-find-base-buffer-visiting file))
  12509. (if (not visiting) (find-file-noselect file))
  12510. (switch-to-buffer (or visiting (get-file-buffer file)))
  12511. (widen)
  12512. (goto-char (point-min))
  12513. (if (re-search-forward
  12514. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12515. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12516. nil t)
  12517. (goto-char (match-beginning 0))
  12518. (error "Target headline not found: %s" heading))))
  12519. (defvar org-note-abort nil) ; dynamically scoped
  12520. ;;;###autoload
  12521. (defun org-remember-handler ()
  12522. "Store stuff from remember.el into an org file.
  12523. First prompts for an org file. If the user just presses return, the value
  12524. of `org-default-notes-file' is used.
  12525. Then the command offers the headings tree of the selected file in order to
  12526. file the text at a specific location.
  12527. You can either immediately press RET to get the note appended to the
  12528. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12529. find a better place. Then press RET or <left> or <right> in insert the note.
  12530. Key Cursor position Note gets inserted
  12531. -----------------------------------------------------------------------------
  12532. RET buffer-start as level 1 heading at end of file
  12533. RET on headline as sublevel of the heading at cursor
  12534. RET no heading at cursor position, level taken from context.
  12535. Or use prefix arg to specify level manually.
  12536. <left> on headline as same level, before current heading
  12537. <right> on headline as same level, after current heading
  12538. So the fastest way to store the note is to press RET RET to append it to
  12539. the default file. This way your current train of thought is not
  12540. interrupted, in accordance with the principles of remember.el.
  12541. You can also get the fast execution without prompting by using
  12542. C-u C-c C-c to exit the remember buffer. See also the variable
  12543. `org-remember-store-without-prompt'.
  12544. Before being stored away, the function ensures that the text has a
  12545. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12546. is constructed from the current date and some additional data.
  12547. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12548. also indented so that it starts in the same column as the headline
  12549. \(i.e. after the stars).
  12550. See also the variable `org-reverse-note-order'."
  12551. (goto-char (point-min))
  12552. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12553. (replace-match ""))
  12554. (goto-char (point-max))
  12555. (beginning-of-line 1)
  12556. (while (looking-at "[ \t]*$\\|##.*")
  12557. (delete-region (1- (point)) (point-max))
  12558. (beginning-of-line 1))
  12559. (catch 'quit
  12560. (if org-note-abort (throw 'quit nil))
  12561. (let* ((txt (buffer-substring (point-min) (point-max)))
  12562. (fastp (org-xor (equal current-prefix-arg '(4))
  12563. org-remember-store-without-prompt))
  12564. (file (cond
  12565. (fastp org-default-notes-file)
  12566. ((and (eq org-remember-interactive-interface 'refile)
  12567. org-refile-targets)
  12568. org-default-notes-file)
  12569. ((not (and (equal current-prefix-arg '(16))
  12570. org-remember-previous-location))
  12571. (org-get-org-file))))
  12572. (heading org-remember-default-headline)
  12573. (visiting (and file (org-find-base-buffer-visiting file)))
  12574. (org-startup-folded nil)
  12575. (org-startup-align-all-tables nil)
  12576. (org-goto-start-pos 1)
  12577. spos exitcmd level indent reversed)
  12578. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12579. (setq file (car org-remember-previous-location)
  12580. heading (cdr org-remember-previous-location)
  12581. fastp t))
  12582. (setq current-prefix-arg nil)
  12583. (if (string-match "[ \t\n]+\\'" txt)
  12584. (setq txt (replace-match "" t t txt)))
  12585. ;; Modify text so that it becomes a nice subtree which can be inserted
  12586. ;; into an org tree.
  12587. (let* ((lines (split-string txt "\n"))
  12588. first)
  12589. (setq first (car lines) lines (cdr lines))
  12590. (if (string-match "^\\*+ " first)
  12591. ;; Is already a headline
  12592. (setq indent nil)
  12593. ;; We need to add a headline: Use time and first buffer line
  12594. (setq lines (cons first lines)
  12595. first (concat "* " (current-time-string)
  12596. " (" (remember-buffer-desc) ")")
  12597. indent " "))
  12598. (if (and org-adapt-indentation indent)
  12599. (setq lines (mapcar
  12600. (lambda (x)
  12601. (if (string-match "\\S-" x)
  12602. (concat indent x) x))
  12603. lines)))
  12604. (setq txt (concat first "\n"
  12605. (mapconcat 'identity lines "\n"))))
  12606. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12607. (setq txt (replace-match "\n\n" t t txt))
  12608. (if (string-match "[ \t\n]*\\'" txt)
  12609. (setq txt (replace-match "\n" t t txt))))
  12610. ;; Put the modified text back into the remember buffer, for refile.
  12611. (erase-buffer)
  12612. (insert txt)
  12613. (goto-char (point-min))
  12614. (when (and (eq org-remember-interactive-interface 'refile)
  12615. (not fastp))
  12616. (org-refile nil (or visiting (find-file-noselect file)))
  12617. (throw 'quit t))
  12618. ;; Find the file
  12619. (if (not visiting) (find-file-noselect file))
  12620. (with-current-buffer (or visiting (get-file-buffer file))
  12621. (unless (org-mode-p)
  12622. (error "Target files for remember notes must be in Org-mode"))
  12623. (save-excursion
  12624. (save-restriction
  12625. (widen)
  12626. (and (goto-char (point-min))
  12627. (not (re-search-forward "^\\* " nil t))
  12628. (insert "\n* " (or heading "Notes") "\n"))
  12629. (setq reversed (org-notes-order-reversed-p))
  12630. ;; Find the default location
  12631. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12632. (goto-char (point-min))
  12633. (if (re-search-forward
  12634. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12635. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12636. nil t)
  12637. (setq org-goto-start-pos (match-beginning 0))
  12638. (when fastp
  12639. (goto-char (point-max))
  12640. (unless (bolp) (newline))
  12641. (insert "* " heading "\n")
  12642. (setq org-goto-start-pos (point-at-bol 0)))))
  12643. ;; Ask the User for a location, using the appropriate interface
  12644. (cond
  12645. (fastp (setq spos org-goto-start-pos
  12646. exitcmd 'return))
  12647. ((eq org-remember-interactive-interface 'outline)
  12648. (setq spos (org-get-location (current-buffer)
  12649. org-remember-help)
  12650. exitcmd (cdr spos)
  12651. spos (car spos)))
  12652. ((eq org-remember-interactive-interface 'outline-path-completion)
  12653. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12654. (org-refile-use-outline-path t))
  12655. (setq spos (org-refile-get-location "Heading: ")
  12656. exitcmd 'return
  12657. spos (nth 3 spos))))
  12658. (t (error "this should not hapen")))
  12659. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12660. ; not handle this note
  12661. (goto-char spos)
  12662. (cond ((org-on-heading-p t)
  12663. (org-back-to-heading t)
  12664. (setq level (funcall outline-level))
  12665. (cond
  12666. ((eq exitcmd 'return)
  12667. ;; sublevel of current
  12668. (setq org-remember-previous-location
  12669. (cons (abbreviate-file-name file)
  12670. (org-get-heading 'notags)))
  12671. (if reversed
  12672. (outline-next-heading)
  12673. (org-end-of-subtree t)
  12674. (if (not (bolp))
  12675. (if (looking-at "[ \t]*\n")
  12676. (beginning-of-line 2)
  12677. (end-of-line 1)
  12678. (insert "\n"))))
  12679. (bookmark-set "org-remember-last-stored")
  12680. (org-paste-subtree (org-get-legal-level level 1) txt))
  12681. ((eq exitcmd 'left)
  12682. ;; before current
  12683. (bookmark-set "org-remember-last-stored")
  12684. (org-paste-subtree level txt))
  12685. ((eq exitcmd 'right)
  12686. ;; after current
  12687. (org-end-of-subtree t)
  12688. (bookmark-set "org-remember-last-stored")
  12689. (org-paste-subtree level txt))
  12690. (t (error "This should not happen"))))
  12691. ((and (bobp) (not reversed))
  12692. ;; Put it at the end, one level below level 1
  12693. (save-restriction
  12694. (widen)
  12695. (goto-char (point-max))
  12696. (if (not (bolp)) (newline))
  12697. (bookmark-set "org-remember-last-stored")
  12698. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12699. ((and (bobp) reversed)
  12700. ;; Put it at the start, as level 1
  12701. (save-restriction
  12702. (widen)
  12703. (goto-char (point-min))
  12704. (re-search-forward "^\\*+ " nil t)
  12705. (beginning-of-line 1)
  12706. (bookmark-set "org-remember-last-stored")
  12707. (org-paste-subtree 1 txt)))
  12708. (t
  12709. ;; Put it right there, with automatic level determined by
  12710. ;; org-paste-subtree or from prefix arg
  12711. (bookmark-set "org-remember-last-stored")
  12712. (org-paste-subtree
  12713. (if (numberp current-prefix-arg) current-prefix-arg)
  12714. txt)))
  12715. (when remember-save-after-remembering
  12716. (save-buffer)
  12717. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12718. t) ;; return t to indicate that we took care of this note.
  12719. (defun org-get-org-file ()
  12720. "Read a filename, with default directory `org-directory'."
  12721. (let ((default (or org-default-notes-file remember-data-file)))
  12722. (read-file-name (format "File name [%s]: " default)
  12723. (file-name-as-directory org-directory)
  12724. default)))
  12725. (defun org-notes-order-reversed-p ()
  12726. "Check if the current file should receive notes in reversed order."
  12727. (cond
  12728. ((not org-reverse-note-order) nil)
  12729. ((eq t org-reverse-note-order) t)
  12730. ((not (listp org-reverse-note-order)) nil)
  12731. (t (catch 'exit
  12732. (let ((all org-reverse-note-order)
  12733. entry)
  12734. (while (setq entry (pop all))
  12735. (if (string-match (car entry) buffer-file-name)
  12736. (throw 'exit (cdr entry))))
  12737. nil)))))
  12738. ;;; Refiling
  12739. (defvar org-refile-target-table nil
  12740. "The list of refile targets, created by `org-refile'.")
  12741. (defvar org-agenda-new-buffers nil
  12742. "Buffers created to visit agenda files.")
  12743. (defun org-get-refile-targets (&optional default-buffer)
  12744. "Produce a table with refile targets."
  12745. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12746. targets txt re files f desc descre)
  12747. (with-current-buffer (or default-buffer (current-buffer))
  12748. (while (setq entry (pop entries))
  12749. (setq files (car entry) desc (cdr entry))
  12750. (cond
  12751. ((null files) (setq files (list (current-buffer))))
  12752. ((eq files 'org-agenda-files)
  12753. (setq files (org-agenda-files 'unrestricted)))
  12754. ((and (symbolp files) (fboundp files))
  12755. (setq files (funcall files)))
  12756. ((and (symbolp files) (boundp files))
  12757. (setq files (symbol-value files))))
  12758. (if (stringp files) (setq files (list files)))
  12759. (cond
  12760. ((eq (car desc) :tag)
  12761. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12762. ((eq (car desc) :todo)
  12763. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12764. ((eq (car desc) :regexp)
  12765. (setq descre (cdr desc)))
  12766. ((eq (car desc) :level)
  12767. (setq descre (concat "^\\*\\{" (number-to-string
  12768. (if org-odd-levels-only
  12769. (1- (* 2 (cdr desc)))
  12770. (cdr desc)))
  12771. "\\}[ \t]")))
  12772. ((eq (car desc) :maxlevel)
  12773. (setq descre (concat "^\\*\\{1," (number-to-string
  12774. (if org-odd-levels-only
  12775. (1- (* 2 (cdr desc)))
  12776. (cdr desc)))
  12777. "\\}[ \t]")))
  12778. (t (error "Bad refiling target description %s" desc)))
  12779. (while (setq f (pop files))
  12780. (save-excursion
  12781. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12782. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12783. (save-excursion
  12784. (save-restriction
  12785. (widen)
  12786. (goto-char (point-min))
  12787. (while (re-search-forward descre nil t)
  12788. (goto-char (point-at-bol))
  12789. (when (looking-at org-complex-heading-regexp)
  12790. (setq txt (match-string 4)
  12791. re (concat "^" (regexp-quote
  12792. (buffer-substring (match-beginning 1)
  12793. (match-end 4)))))
  12794. (if (match-end 5) (setq re (concat re "[ \t]+"
  12795. (regexp-quote
  12796. (match-string 5)))))
  12797. (setq re (concat re "[ \t]*$"))
  12798. (when org-refile-use-outline-path
  12799. (setq txt (mapconcat 'identity
  12800. (append
  12801. (if (eq org-refile-use-outline-path 'file)
  12802. (list (file-name-nondirectory
  12803. (buffer-file-name (buffer-base-buffer))))
  12804. (if (eq org-refile-use-outline-path 'full-file-path)
  12805. (list (buffer-file-name (buffer-base-buffer)))))
  12806. (org-get-outline-path)
  12807. (list txt))
  12808. "/")))
  12809. (push (list txt f re (point)) targets))
  12810. (goto-char (point-at-eol))))))))
  12811. (nreverse targets))))
  12812. (defun org-get-outline-path ()
  12813. "Return the outline path to the current entry, as a list."
  12814. (let (rtn)
  12815. (save-excursion
  12816. (while (org-up-heading-safe)
  12817. (when (looking-at org-complex-heading-regexp)
  12818. (push (org-match-string-no-properties 4) rtn)))
  12819. rtn)))
  12820. (defvar org-refile-history nil
  12821. "History for refiling operations.")
  12822. (defun org-refile (&optional goto default-buffer)
  12823. "Move the entry at point to another heading.
  12824. The list of target headings is compiled using the information in
  12825. `org-refile-targets', which see. This list is created upon first use, and
  12826. you can update it by calling this command with a double prefix (`C-u C-u').
  12827. FIXME: Can we find a better way of updating?
  12828. At the target location, the entry is filed as a subitem of the target heading.
  12829. Depending on `org-reverse-note-order', the new subitem will either be the
  12830. first of the last subitem.
  12831. With prefix arg GOTO, the command will only visit the target location,
  12832. not actually move anything.
  12833. With a double prefix `C-c C-c', go to the location where the last refiling
  12834. operation has put the subtree.
  12835. With a double prefix argument, the command can be used to jump to any
  12836. heading in the current buffer."
  12837. (interactive "P")
  12838. (let* ((cbuf (current-buffer))
  12839. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12840. pos it nbuf file re level reversed)
  12841. (if (equal goto '(16))
  12842. (org-refile-goto-last-stored)
  12843. (when (setq it (org-refile-get-location
  12844. (if goto "Goto: " "Refile to: ") default-buffer))
  12845. (setq file (nth 1 it)
  12846. re (nth 2 it)
  12847. pos (nth 3 it))
  12848. (setq nbuf (or (find-buffer-visiting file)
  12849. (find-file-noselect file)))
  12850. (if goto
  12851. (progn
  12852. (switch-to-buffer nbuf)
  12853. (goto-char pos)
  12854. (org-show-context 'org-goto))
  12855. (org-copy-special)
  12856. (save-excursion
  12857. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12858. (find-file-noselect file))))
  12859. (setq reversed (org-notes-order-reversed-p))
  12860. (save-excursion
  12861. (save-restriction
  12862. (widen)
  12863. (goto-char pos)
  12864. (looking-at outline-regexp)
  12865. (setq level (org-get-legal-level (funcall outline-level) 1))
  12866. (goto-char
  12867. (if reversed
  12868. (outline-next-heading)
  12869. (or (save-excursion (outline-get-next-sibling))
  12870. (org-end-of-subtree t t)
  12871. (point-max))))
  12872. (bookmark-set "org-refile-last-stored")
  12873. (org-paste-subtree level))))
  12874. (org-cut-special)
  12875. (message "Entry refiled to \"%s\"" (car it)))))))
  12876. (defun org-refile-goto-last-stored ()
  12877. "Go to the location where the last refile was stored."
  12878. (interactive)
  12879. (bookmark-jump "org-refile-last-stored")
  12880. (message "This is the location of the last refile"))
  12881. (defun org-refile-get-location (&optional prompt default-buffer)
  12882. "Prompt the user for a refile location, using PROMPT."
  12883. (let ((org-refile-targets org-refile-targets)
  12884. (org-refile-use-outline-path org-refile-use-outline-path))
  12885. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12886. (unless org-refile-target-table
  12887. (error "No refile targets"))
  12888. (let* ((cbuf (current-buffer))
  12889. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12890. (fname (and filename (file-truename filename)))
  12891. (tbl (mapcar
  12892. (lambda (x)
  12893. (if (not (equal fname (file-truename (nth 1 x))))
  12894. (cons (concat (car x) " (" (file-name-nondirectory
  12895. (nth 1 x)) ")")
  12896. (cdr x))
  12897. x))
  12898. org-refile-target-table))
  12899. (completion-ignore-case t))
  12900. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12901. tbl)))
  12902. ;;;; Dynamic blocks
  12903. (defun org-find-dblock (name)
  12904. "Find the first dynamic block with name NAME in the buffer.
  12905. If not found, stay at current position and return nil."
  12906. (let (pos)
  12907. (save-excursion
  12908. (goto-char (point-min))
  12909. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12910. nil t)
  12911. (match-beginning 0))))
  12912. (if pos (goto-char pos))
  12913. pos))
  12914. (defconst org-dblock-start-re
  12915. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12916. "Matches the startline of a dynamic block, with parameters.")
  12917. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12918. "Matches the end of a dyhamic block.")
  12919. (defun org-create-dblock (plist)
  12920. "Create a dynamic block section, with parameters taken from PLIST.
  12921. PLIST must containe a :name entry which is used as name of the block."
  12922. (unless (bolp) (newline))
  12923. (let ((name (plist-get plist :name)))
  12924. (insert "#+BEGIN: " name)
  12925. (while plist
  12926. (if (eq (car plist) :name)
  12927. (setq plist (cddr plist))
  12928. (insert " " (prin1-to-string (pop plist)))))
  12929. (insert "\n\n#+END:\n")
  12930. (beginning-of-line -2)))
  12931. (defun org-prepare-dblock ()
  12932. "Prepare dynamic block for refresh.
  12933. This empties the block, puts the cursor at the insert position and returns
  12934. the property list including an extra property :name with the block name."
  12935. (unless (looking-at org-dblock-start-re)
  12936. (error "Not at a dynamic block"))
  12937. (let* ((begdel (1+ (match-end 0)))
  12938. (name (org-no-properties (match-string 1)))
  12939. (params (append (list :name name)
  12940. (read (concat "(" (match-string 3) ")")))))
  12941. (unless (re-search-forward org-dblock-end-re nil t)
  12942. (error "Dynamic block not terminated"))
  12943. (delete-region begdel (match-beginning 0))
  12944. (goto-char begdel)
  12945. (open-line 1)
  12946. params))
  12947. (defun org-map-dblocks (&optional command)
  12948. "Apply COMMAND to all dynamic blocks in the current buffer.
  12949. If COMMAND is not given, use `org-update-dblock'."
  12950. (let ((cmd (or command 'org-update-dblock))
  12951. pos)
  12952. (save-excursion
  12953. (goto-char (point-min))
  12954. (while (re-search-forward org-dblock-start-re nil t)
  12955. (goto-char (setq pos (match-beginning 0)))
  12956. (condition-case nil
  12957. (funcall cmd)
  12958. (error (message "Error during update of dynamic block")))
  12959. (goto-char pos)
  12960. (unless (re-search-forward org-dblock-end-re nil t)
  12961. (error "Dynamic block not terminated"))))))
  12962. (defun org-dblock-update (&optional arg)
  12963. "User command for updating dynamic blocks.
  12964. Update the dynamic block at point. With prefix ARG, update all dynamic
  12965. blocks in the buffer."
  12966. (interactive "P")
  12967. (if arg
  12968. (org-update-all-dblocks)
  12969. (or (looking-at org-dblock-start-re)
  12970. (org-beginning-of-dblock))
  12971. (org-update-dblock)))
  12972. (defun org-update-dblock ()
  12973. "Update the dynamic block at point
  12974. This means to empty the block, parse for parameters and then call
  12975. the correct writing function."
  12976. (save-window-excursion
  12977. (let* ((pos (point))
  12978. (line (org-current-line))
  12979. (params (org-prepare-dblock))
  12980. (name (plist-get params :name))
  12981. (cmd (intern (concat "org-dblock-write:" name))))
  12982. (message "Updating dynamic block `%s' at line %d..." name line)
  12983. (funcall cmd params)
  12984. (message "Updating dynamic block `%s' at line %d...done" name line)
  12985. (goto-char pos))))
  12986. (defun org-beginning-of-dblock ()
  12987. "Find the beginning of the dynamic block at point.
  12988. Error if there is no scuh block at point."
  12989. (let ((pos (point))
  12990. beg)
  12991. (end-of-line 1)
  12992. (if (and (re-search-backward org-dblock-start-re nil t)
  12993. (setq beg (match-beginning 0))
  12994. (re-search-forward org-dblock-end-re nil t)
  12995. (> (match-end 0) pos))
  12996. (goto-char beg)
  12997. (goto-char pos)
  12998. (error "Not in a dynamic block"))))
  12999. (defun org-update-all-dblocks ()
  13000. "Update all dynamic blocks in the buffer.
  13001. This function can be used in a hook."
  13002. (when (org-mode-p)
  13003. (org-map-dblocks 'org-update-dblock)))
  13004. ;;;; Completion
  13005. (defconst org-additional-option-like-keywords
  13006. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13007. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13008. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13009. (defun org-complete (&optional arg)
  13010. "Perform completion on word at point.
  13011. At the beginning of a headline, this completes TODO keywords as given in
  13012. `org-todo-keywords'.
  13013. If the current word is preceded by a backslash, completes the TeX symbols
  13014. that are supported for HTML support.
  13015. If the current word is preceded by \"#+\", completes special words for
  13016. setting file options.
  13017. In the line after \"#+STARTUP:, complete valid keywords.\"
  13018. At all other locations, this simply calls the value of
  13019. `org-completion-fallback-command'."
  13020. (interactive "P")
  13021. (org-without-partial-completion
  13022. (catch 'exit
  13023. (let* ((end (point))
  13024. (beg1 (save-excursion
  13025. (skip-chars-backward (org-re "[:alnum:]_@"))
  13026. (point)))
  13027. (beg (save-excursion
  13028. (skip-chars-backward "a-zA-Z0-9_:$")
  13029. (point)))
  13030. (confirm (lambda (x) (stringp (car x))))
  13031. (searchhead (equal (char-before beg) ?*))
  13032. (tag (and (equal (char-before beg1) ?:)
  13033. (equal (char-after (point-at-bol)) ?*)))
  13034. (prop (and (equal (char-before beg1) ?:)
  13035. (not (equal (char-after (point-at-bol)) ?*))))
  13036. (texp (equal (char-before beg) ?\\))
  13037. (link (equal (char-before beg) ?\[))
  13038. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13039. beg)
  13040. "#+"))
  13041. (startup (string-match "^#\\+STARTUP:.*"
  13042. (buffer-substring (point-at-bol) (point))))
  13043. (completion-ignore-case opt)
  13044. (type nil)
  13045. (tbl nil)
  13046. (table (cond
  13047. (opt
  13048. (setq type :opt)
  13049. (append
  13050. (mapcar
  13051. (lambda (x)
  13052. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13053. (cons (match-string 2 x) (match-string 1 x)))
  13054. (org-split-string (org-get-current-options) "\n"))
  13055. (mapcar 'list org-additional-option-like-keywords)))
  13056. (startup
  13057. (setq type :startup)
  13058. org-startup-options)
  13059. (link (append org-link-abbrev-alist-local
  13060. org-link-abbrev-alist))
  13061. (texp
  13062. (setq type :tex)
  13063. org-html-entities)
  13064. ((string-match "\\`\\*+[ \t]+\\'"
  13065. (buffer-substring (point-at-bol) beg))
  13066. (setq type :todo)
  13067. (mapcar 'list org-todo-keywords-1))
  13068. (searchhead
  13069. (setq type :searchhead)
  13070. (save-excursion
  13071. (goto-char (point-min))
  13072. (while (re-search-forward org-todo-line-regexp nil t)
  13073. (push (list
  13074. (org-make-org-heading-search-string
  13075. (match-string 3) t))
  13076. tbl)))
  13077. tbl)
  13078. (tag (setq type :tag beg beg1)
  13079. (or org-tag-alist (org-get-buffer-tags)))
  13080. (prop (setq type :prop beg beg1)
  13081. (mapcar 'list (org-buffer-property-keys nil t t)))
  13082. (t (progn
  13083. (call-interactively org-completion-fallback-command)
  13084. (throw 'exit nil)))))
  13085. (pattern (buffer-substring-no-properties beg end))
  13086. (completion (try-completion pattern table confirm)))
  13087. (cond ((eq completion t)
  13088. (if (not (assoc (upcase pattern) table))
  13089. (message "Already complete")
  13090. (if (equal type :opt)
  13091. (insert (substring (cdr (assoc (upcase pattern) table))
  13092. (length pattern)))
  13093. (if (memq type '(:tag :prop)) (insert ":")))))
  13094. ((null completion)
  13095. (message "Can't find completion for \"%s\"" pattern)
  13096. (ding))
  13097. ((not (string= pattern completion))
  13098. (delete-region beg end)
  13099. (if (string-match " +$" completion)
  13100. (setq completion (replace-match "" t t completion)))
  13101. (insert completion)
  13102. (if (get-buffer-window "*Completions*")
  13103. (delete-window (get-buffer-window "*Completions*")))
  13104. (if (assoc completion table)
  13105. (if (eq type :todo) (insert " ")
  13106. (if (memq type '(:tag :prop)) (insert ":"))))
  13107. (if (and (equal type :opt) (assoc completion table))
  13108. (message "%s" (substitute-command-keys
  13109. "Press \\[org-complete] again to insert example settings"))))
  13110. (t
  13111. (message "Making completion list...")
  13112. (let ((list (sort (all-completions pattern table confirm)
  13113. 'string<)))
  13114. (with-output-to-temp-buffer "*Completions*"
  13115. (condition-case nil
  13116. ;; Protection needed for XEmacs and emacs 21
  13117. (display-completion-list list pattern)
  13118. (error (display-completion-list list)))))
  13119. (message "Making completion list...%s" "done")))))))
  13120. ;;;; TODO, DEADLINE, Comments
  13121. (defun org-toggle-comment ()
  13122. "Change the COMMENT state of an entry."
  13123. (interactive)
  13124. (save-excursion
  13125. (org-back-to-heading)
  13126. (let (case-fold-search)
  13127. (if (looking-at (concat outline-regexp
  13128. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13129. (replace-match "" t t nil 1)
  13130. (if (looking-at outline-regexp)
  13131. (progn
  13132. (goto-char (match-end 0))
  13133. (insert org-comment-string " ")))))))
  13134. (defvar org-last-todo-state-is-todo nil
  13135. "This is non-nil when the last TODO state change led to a TODO state.
  13136. If the last change removed the TODO tag or switched to DONE, then
  13137. this is nil.")
  13138. (defvar org-setting-tags nil) ; dynamically skiped
  13139. ;; FIXME: better place
  13140. (defun org-property-or-variable-value (var &optional inherit)
  13141. "Check if there is a property fixing the value of VAR.
  13142. If yes, return this value. If not, return the current value of the variable."
  13143. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13144. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13145. (read prop)
  13146. (symbol-value var))))
  13147. (defun org-parse-local-options (string var)
  13148. "Parse STRING for startup setting relevant for variable VAR."
  13149. (let ((rtn (symbol-value var))
  13150. e opts)
  13151. (save-match-data
  13152. (if (or (not string) (not (string-match "\\S-" string)))
  13153. rtn
  13154. (setq opts (delq nil (mapcar (lambda (x)
  13155. (setq e (assoc x org-startup-options))
  13156. (if (eq (nth 1 e) var) e nil))
  13157. (org-split-string string "[ \t]+"))))
  13158. (if (not opts)
  13159. rtn
  13160. (setq rtn nil)
  13161. (while (setq e (pop opts))
  13162. (if (not (nth 3 e))
  13163. (setq rtn (nth 2 e))
  13164. (if (not (listp rtn)) (setq rtn nil))
  13165. (push (nth 2 e) rtn)))
  13166. rtn)))))
  13167. (defvar org-blocker-hook nil
  13168. "Hook for functions that are allowed to block a state change.
  13169. Each function gets as its single argument a property list, see
  13170. `org-trigger-hook' for more information about this list.
  13171. If any of the functions in this hook returns nil, the state change
  13172. is blocked.")
  13173. (defvar org-trigger-hook nil
  13174. "Hook for functions that are triggered by a state change.
  13175. Each function gets as its single argument a property list with at least
  13176. the following elements:
  13177. (:type type-of-change :position pos-at-entry-start
  13178. :from old-state :to new-state)
  13179. Depending on the type, more properties may be present.
  13180. This mechanism is currently implemented for:
  13181. TODO state changes
  13182. ------------------
  13183. :type todo-state-change
  13184. :from previous state (keyword as a string), or nil
  13185. :to new state (keyword as a string), or nil")
  13186. (defun org-todo (&optional arg)
  13187. "Change the TODO state of an item.
  13188. The state of an item is given by a keyword at the start of the heading,
  13189. like
  13190. *** TODO Write paper
  13191. *** DONE Call mom
  13192. The different keywords are specified in the variable `org-todo-keywords'.
  13193. By default the available states are \"TODO\" and \"DONE\".
  13194. So for this example: when the item starts with TODO, it is changed to DONE.
  13195. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13196. DONE are present, add TODO at the beginning of the heading.
  13197. With C-u prefix arg, use completion to determine the new state.
  13198. With numeric prefix arg, switch to that state.
  13199. For calling through lisp, arg is also interpreted in the following way:
  13200. 'none -> empty state
  13201. \"\"(empty string) -> switch to empty state
  13202. 'done -> switch to DONE
  13203. 'nextset -> switch to the next set of keywords
  13204. 'previousset -> switch to the previous set of keywords
  13205. \"WAITING\" -> switch to the specified keyword, but only if it
  13206. really is a member of `org-todo-keywords'."
  13207. (interactive "P")
  13208. (save-excursion
  13209. (catch 'exit
  13210. (org-back-to-heading)
  13211. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13212. (or (looking-at (concat " +" org-todo-regexp " *"))
  13213. (looking-at " *"))
  13214. (let* ((match-data (match-data))
  13215. (startpos (point-at-bol))
  13216. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13217. (org-log-done org-log-done)
  13218. (org-log-repeat org-log-repeat)
  13219. (org-todo-log-states org-todo-log-states)
  13220. (this (match-string 1))
  13221. (hl-pos (match-beginning 0))
  13222. (head (org-get-todo-sequence-head this))
  13223. (ass (assoc head org-todo-kwd-alist))
  13224. (interpret (nth 1 ass))
  13225. (done-word (nth 3 ass))
  13226. (final-done-word (nth 4 ass))
  13227. (last-state (or this ""))
  13228. (completion-ignore-case t)
  13229. (member (member this org-todo-keywords-1))
  13230. (tail (cdr member))
  13231. (state (cond
  13232. ((and org-todo-key-trigger
  13233. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13234. (and (not arg) org-use-fast-todo-selection
  13235. (not (eq org-use-fast-todo-selection 'prefix)))))
  13236. ;; Use fast selection
  13237. (org-fast-todo-selection))
  13238. ((and (equal arg '(4))
  13239. (or (not org-use-fast-todo-selection)
  13240. (not org-todo-key-trigger)))
  13241. ;; Read a state with completion
  13242. (completing-read "State: " (mapcar (lambda(x) (list x))
  13243. org-todo-keywords-1)
  13244. nil t))
  13245. ((eq arg 'right)
  13246. (if this
  13247. (if tail (car tail) nil)
  13248. (car org-todo-keywords-1)))
  13249. ((eq arg 'left)
  13250. (if (equal member org-todo-keywords-1)
  13251. nil
  13252. (if this
  13253. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13254. org-todo-keywords-1)
  13255. (org-last org-todo-keywords-1))))
  13256. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13257. (setq arg nil))) ; hack to fall back to cycling
  13258. (arg
  13259. ;; user or caller requests a specific state
  13260. (cond
  13261. ((equal arg "") nil)
  13262. ((eq arg 'none) nil)
  13263. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13264. ((eq arg 'nextset)
  13265. (or (car (cdr (member head org-todo-heads)))
  13266. (car org-todo-heads)))
  13267. ((eq arg 'previousset)
  13268. (let ((org-todo-heads (reverse org-todo-heads)))
  13269. (or (car (cdr (member head org-todo-heads)))
  13270. (car org-todo-heads))))
  13271. ((car (member arg org-todo-keywords-1)))
  13272. ((nth (1- (prefix-numeric-value arg))
  13273. org-todo-keywords-1))))
  13274. ((null member) (or head (car org-todo-keywords-1)))
  13275. ((equal this final-done-word) nil) ;; -> make empty
  13276. ((null tail) nil) ;; -> first entry
  13277. ((eq interpret 'sequence)
  13278. (car tail))
  13279. ((memq interpret '(type priority))
  13280. (if (eq this-command last-command)
  13281. (car tail)
  13282. (if (> (length tail) 0)
  13283. (or done-word (car org-done-keywords))
  13284. nil)))
  13285. (t nil)))
  13286. (next (if state (concat " " state " ") " "))
  13287. (change-plist (list :type 'todo-state-change :from this :to state
  13288. :position startpos))
  13289. dolog now-done-p)
  13290. (when org-blocker-hook
  13291. (unless (save-excursion
  13292. (save-match-data
  13293. (run-hook-with-args-until-failure
  13294. 'org-blocker-hook change-plist)))
  13295. (if (interactive-p)
  13296. (error "TODO state change from %s to %s blocked" this state)
  13297. ;; fail silently
  13298. (message "TODO state change from %s to %s blocked" this state)
  13299. (throw 'exit nil))))
  13300. (store-match-data match-data)
  13301. (replace-match next t t)
  13302. (unless (pos-visible-in-window-p hl-pos)
  13303. (message "TODO state changed to %s" (org-trim next)))
  13304. (unless head
  13305. (setq head (org-get-todo-sequence-head state)
  13306. ass (assoc head org-todo-kwd-alist)
  13307. interpret (nth 1 ass)
  13308. done-word (nth 3 ass)
  13309. final-done-word (nth 4 ass)))
  13310. (when (memq arg '(nextset previousset))
  13311. (message "Keyword-Set %d/%d: %s"
  13312. (- (length org-todo-sets) -1
  13313. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13314. (length org-todo-sets)
  13315. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13316. (setq org-last-todo-state-is-todo
  13317. (not (member state org-done-keywords)))
  13318. (setq now-done-p (and (member state org-done-keywords)
  13319. (not (member this org-done-keywords))))
  13320. (and logging (org-local-logging logging))
  13321. (when (and (or org-todo-log-states org-log-done)
  13322. (not (memq arg '(nextset previousset))))
  13323. ;; we need to look at recording a time and note
  13324. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13325. (nth 2 (assoc this org-todo-log-states))))
  13326. (when (and state
  13327. (member state org-not-done-keywords)
  13328. (not (member this org-not-done-keywords)))
  13329. ;; This is now a todo state and was not one before
  13330. ;; If there was a CLOSED time stamp, get rid of it.
  13331. (org-add-planning-info nil nil 'closed))
  13332. (when (and now-done-p org-log-done)
  13333. ;; It is now done, and it was not done before
  13334. (org-add-planning-info 'closed (org-current-time))
  13335. (if (and (not dolog) (eq 'note org-log-done))
  13336. (org-add-log-maybe 'done state 'findpos 'note)))
  13337. (when (and state dolog)
  13338. ;; This is a non-nil state, and we need to log it
  13339. (org-add-log-maybe 'state state 'findpos dolog)))
  13340. ;; Fixup tag positioning
  13341. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13342. (run-hooks 'org-after-todo-state-change-hook)
  13343. (if (and arg (not (member state org-done-keywords)))
  13344. (setq head (org-get-todo-sequence-head state)))
  13345. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13346. ;; Do we need to trigger a repeat?
  13347. (when now-done-p (org-auto-repeat-maybe state))
  13348. ;; Fixup cursor location if close to the keyword
  13349. (if (and (outline-on-heading-p)
  13350. (not (bolp))
  13351. (save-excursion (beginning-of-line 1)
  13352. (looking-at org-todo-line-regexp))
  13353. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13354. (progn
  13355. (goto-char (or (match-end 2) (match-end 1)))
  13356. (just-one-space)))
  13357. (when org-trigger-hook
  13358. (save-excursion
  13359. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13360. (defun org-local-logging (value)
  13361. "Get logging settings from a property VALUE."
  13362. (let* (words w a)
  13363. ;; directly set the variables, they are already local.
  13364. (setq org-log-done nil
  13365. org-log-repeat nil
  13366. org-todo-log-states nil)
  13367. (setq words (org-split-string value))
  13368. (while (setq w (pop words))
  13369. (cond
  13370. ((setq a (assoc w org-startup-options))
  13371. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13372. (set (nth 1 a) (nth 2 a))))
  13373. ((setq a (org-extract-log-state-settings w))
  13374. (and (member (car a) org-todo-keywords-1)
  13375. (push a org-todo-log-states)))))))
  13376. (defun org-get-todo-sequence-head (kwd)
  13377. "Return the head of the TODO sequence to which KWD belongs.
  13378. If KWD is not set, check if there is a text property remembering the
  13379. right sequence."
  13380. (let (p)
  13381. (cond
  13382. ((not kwd)
  13383. (or (get-text-property (point-at-bol) 'org-todo-head)
  13384. (progn
  13385. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13386. nil (point-at-eol)))
  13387. (get-text-property p 'org-todo-head))))
  13388. ((not (member kwd org-todo-keywords-1))
  13389. (car org-todo-keywords-1))
  13390. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13391. (defun org-fast-todo-selection ()
  13392. "Fast TODO keyword selection with single keys.
  13393. Returns the new TODO keyword, or nil if no state change should occur."
  13394. (let* ((fulltable org-todo-key-alist)
  13395. (done-keywords org-done-keywords) ;; needed for the faces.
  13396. (maxlen (apply 'max (mapcar
  13397. (lambda (x)
  13398. (if (stringp (car x)) (string-width (car x)) 0))
  13399. fulltable)))
  13400. (expert nil)
  13401. (fwidth (+ maxlen 3 1 3))
  13402. (ncol (/ (- (window-width) 4) fwidth))
  13403. tg cnt e c tbl
  13404. groups ingroup)
  13405. (save-window-excursion
  13406. (if expert
  13407. (set-buffer (get-buffer-create " *Org todo*"))
  13408. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13409. (erase-buffer)
  13410. (org-set-local 'org-done-keywords done-keywords)
  13411. (setq tbl fulltable cnt 0)
  13412. (while (setq e (pop tbl))
  13413. (cond
  13414. ((equal e '(:startgroup))
  13415. (push '() groups) (setq ingroup t)
  13416. (when (not (= cnt 0))
  13417. (setq cnt 0)
  13418. (insert "\n"))
  13419. (insert "{ "))
  13420. ((equal e '(:endgroup))
  13421. (setq ingroup nil cnt 0)
  13422. (insert "}\n"))
  13423. (t
  13424. (setq tg (car e) c (cdr e))
  13425. (if ingroup (push tg (car groups)))
  13426. (setq tg (org-add-props tg nil 'face
  13427. (org-get-todo-face tg)))
  13428. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13429. (insert "[" c "] " tg (make-string
  13430. (- fwidth 4 (length tg)) ?\ ))
  13431. (when (= (setq cnt (1+ cnt)) ncol)
  13432. (insert "\n")
  13433. (if ingroup (insert " "))
  13434. (setq cnt 0)))))
  13435. (insert "\n")
  13436. (goto-char (point-min))
  13437. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13438. (fit-window-to-buffer))
  13439. (message "[a-z..]:Set [SPC]:clear")
  13440. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13441. (cond
  13442. ((or (= c ?\C-g)
  13443. (and (= c ?q) (not (rassoc c fulltable))))
  13444. (setq quit-flag t))
  13445. ((= c ?\ ) nil)
  13446. ((setq e (rassoc c fulltable) tg (car e))
  13447. tg)
  13448. (t (setq quit-flag t))))))
  13449. (defun org-get-repeat ()
  13450. "Check if tere is a deadline/schedule with repeater in this entry."
  13451. (save-match-data
  13452. (save-excursion
  13453. (org-back-to-heading t)
  13454. (if (re-search-forward
  13455. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13456. (match-string 1)))))
  13457. (defvar org-last-changed-timestamp)
  13458. (defvar org-log-post-message)
  13459. (defvar org-log-note-purpose)
  13460. (defun org-auto-repeat-maybe (done-word)
  13461. "Check if the current headline contains a repeated deadline/schedule.
  13462. If yes, set TODO state back to what it was and change the base date
  13463. of repeating deadline/scheduled time stamps to new date.
  13464. This function should be run in the `org-after-todo-state-change-hook'."
  13465. ;; last-state is dynamically scoped into this function
  13466. (let* ((repeat (org-get-repeat))
  13467. (aa (assoc last-state org-todo-kwd-alist))
  13468. (interpret (nth 1 aa))
  13469. (head (nth 2 aa))
  13470. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13471. (msg "Entry repeats: ")
  13472. (org-log-done nil)
  13473. re type n what ts)
  13474. (when repeat
  13475. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13476. (org-todo (if (eq interpret 'type) last-state head))
  13477. (when (and org-log-repeat
  13478. (or (not (memq 'org-add-log-note
  13479. (default-value 'post-command-hook)))
  13480. (eq org-log-note-purpose 'done)))
  13481. ;; Make sure a note is taken;
  13482. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13483. 'findpos org-log-repeat))
  13484. (org-back-to-heading t)
  13485. (org-add-planning-info nil nil 'closed)
  13486. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13487. org-deadline-time-regexp "\\)\\|\\("
  13488. org-ts-regexp "\\)"))
  13489. (while (re-search-forward
  13490. re (save-excursion (outline-next-heading) (point)) t)
  13491. (setq type (if (match-end 1) org-scheduled-string
  13492. (if (match-end 3) org-deadline-string "Plain:"))
  13493. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13494. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13495. (setq n (string-to-number (match-string 1 ts))
  13496. what (match-string 2 ts))
  13497. (if (equal what "w") (setq n (* n 7) what "d"))
  13498. (org-timestamp-change n (cdr (assoc what whata)))
  13499. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13500. (setq org-log-post-message msg)
  13501. (message "%s" msg))))
  13502. (defun org-show-todo-tree (arg)
  13503. "Make a compact tree which shows all headlines marked with TODO.
  13504. The tree will show the lines where the regexp matches, and all higher
  13505. headlines above the match.
  13506. With \\[universal-argument] prefix, also show the DONE entries.
  13507. With a numeric prefix N, construct a sparse tree for the Nth element
  13508. of `org-todo-keywords-1'."
  13509. (interactive "P")
  13510. (let ((case-fold-search nil)
  13511. (kwd-re
  13512. (cond ((null arg) org-not-done-regexp)
  13513. ((equal arg '(4))
  13514. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13515. (mapcar 'list org-todo-keywords-1))))
  13516. (concat "\\("
  13517. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13518. "\\)\\>")))
  13519. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13520. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13521. org-todo-keywords-1)))
  13522. (t (error "Invalid prefix argument: %s" arg)))))
  13523. (message "%d TODO entries found"
  13524. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13525. (defun org-deadline (&optional remove)
  13526. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13527. With argument REMOVE, remove any deadline from the item."
  13528. (interactive "P")
  13529. (if remove
  13530. (progn
  13531. (org-remove-timestamp-with-keyword org-deadline-string)
  13532. (message "Item no longer has a deadline."))
  13533. (org-add-planning-info 'deadline nil 'closed)))
  13534. (defun org-schedule (&optional remove)
  13535. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13536. With argument REMOVE, remove any scheduling date from the item."
  13537. (interactive "P")
  13538. (if remove
  13539. (progn
  13540. (org-remove-timestamp-with-keyword org-scheduled-string)
  13541. (message "Item is no longer scheduled."))
  13542. (org-add-planning-info 'scheduled nil 'closed)))
  13543. (defun org-remove-timestamp-with-keyword (keyword)
  13544. "Remove all time stamps with KEYWORD in the current entry."
  13545. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13546. beg)
  13547. (save-excursion
  13548. (org-back-to-heading t)
  13549. (setq beg (point))
  13550. (org-end-of-subtree t t)
  13551. (while (re-search-backward re beg t)
  13552. (replace-match "")
  13553. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13554. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13555. (defun org-add-planning-info (what &optional time &rest remove)
  13556. "Insert new timestamp with keyword in the line directly after the headline.
  13557. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13558. If non is given, the user is prompted for a date.
  13559. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13560. be removed."
  13561. (interactive)
  13562. (let (org-time-was-given org-end-time-was-given)
  13563. (when what (setq time (or time (org-read-date nil 'to-time))))
  13564. (when (and org-insert-labeled-timestamps-at-point
  13565. (member what '(scheduled deadline)))
  13566. (insert
  13567. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13568. (org-insert-time-stamp time org-time-was-given
  13569. nil nil nil (list org-end-time-was-given))
  13570. (setq what nil))
  13571. (save-excursion
  13572. (save-restriction
  13573. (let (col list elt ts buffer-invisibility-spec)
  13574. (org-back-to-heading t)
  13575. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13576. (goto-char (match-end 1))
  13577. (setq col (current-column))
  13578. (goto-char (match-end 0))
  13579. (if (eobp) (insert "\n") (forward-char 1))
  13580. (if (and (not (looking-at outline-regexp))
  13581. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13582. "[^\r\n]*"))
  13583. (not (equal (match-string 1) org-clock-string)))
  13584. (narrow-to-region (match-beginning 0) (match-end 0))
  13585. (insert-before-markers "\n")
  13586. (backward-char 1)
  13587. (narrow-to-region (point) (point))
  13588. (indent-to-column col))
  13589. ;; Check if we have to remove something.
  13590. (setq list (cons what remove))
  13591. (while list
  13592. (setq elt (pop list))
  13593. (goto-char (point-min))
  13594. (when (or (and (eq elt 'scheduled)
  13595. (re-search-forward org-scheduled-time-regexp nil t))
  13596. (and (eq elt 'deadline)
  13597. (re-search-forward org-deadline-time-regexp nil t))
  13598. (and (eq elt 'closed)
  13599. (re-search-forward org-closed-time-regexp nil t)))
  13600. (replace-match "")
  13601. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13602. (if (looking-at " +") (replace-match ""))))
  13603. (goto-char (point-max))
  13604. (when what
  13605. (insert
  13606. (if (not (equal (char-before) ?\ )) " " "")
  13607. (cond ((eq what 'scheduled) org-scheduled-string)
  13608. ((eq what 'deadline) org-deadline-string)
  13609. ((eq what 'closed) org-closed-string))
  13610. " ")
  13611. (setq ts (org-insert-time-stamp
  13612. time
  13613. (or org-time-was-given
  13614. (and (eq what 'closed) org-log-done-with-time))
  13615. (eq what 'closed)
  13616. nil nil (list org-end-time-was-given)))
  13617. (end-of-line 1))
  13618. (goto-char (point-min))
  13619. (widen)
  13620. (if (looking-at "[ \t]+\r?\n")
  13621. (replace-match ""))
  13622. ts)))))
  13623. (defvar org-log-note-marker (make-marker))
  13624. (defvar org-log-note-purpose nil)
  13625. (defvar org-log-note-state nil)
  13626. (defvar org-log-note-how nil)
  13627. (defvar org-log-note-window-configuration nil)
  13628. (defvar org-log-note-return-to (make-marker))
  13629. (defvar org-log-post-message nil
  13630. "Message to be displayed after a log note has been stored.
  13631. The auto-repeater uses this.")
  13632. (defun org-add-log-maybe (&optional purpose state findpos how)
  13633. "Set up the post command hook to take a note.
  13634. If this is about to TODO state change, the new state is expected in STATE.
  13635. When FINDPOS is non-nil, find the correct position for the note in
  13636. the current entry. If not, assume that it can be inserted at point."
  13637. (save-excursion
  13638. (when findpos
  13639. (org-back-to-heading t)
  13640. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13641. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13642. "[^\r\n]*\\)?"))
  13643. (goto-char (match-end 0))
  13644. (unless org-log-states-order-reversed
  13645. (and (= (char-after) ?\n) (forward-char 1))
  13646. (org-skip-over-state-notes)
  13647. (skip-chars-backward " \t\n\r")))
  13648. (move-marker org-log-note-marker (point))
  13649. (setq org-log-note-purpose purpose
  13650. org-log-note-state state
  13651. org-log-note-how how)
  13652. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13653. (defun org-skip-over-state-notes ()
  13654. "Skip past the list of State notes in an entry."
  13655. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13656. (while (looking-at "[ \t]*- State")
  13657. (condition-case nil
  13658. (org-next-item)
  13659. (error (org-end-of-item)))))
  13660. (defun org-add-log-note (&optional purpose)
  13661. "Pop up a window for taking a note, and add this note later at point."
  13662. (remove-hook 'post-command-hook 'org-add-log-note)
  13663. (setq org-log-note-window-configuration (current-window-configuration))
  13664. (delete-other-windows)
  13665. (move-marker org-log-note-return-to (point))
  13666. (switch-to-buffer (marker-buffer org-log-note-marker))
  13667. (goto-char org-log-note-marker)
  13668. (org-switch-to-buffer-other-window "*Org Note*")
  13669. (erase-buffer)
  13670. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13671. (org-store-log-note)
  13672. (let ((org-inhibit-startup t)) (org-mode))
  13673. (insert (format "# Insert note for %s.
  13674. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13675. (cond
  13676. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13677. ((eq org-log-note-purpose 'done) "closed todo item")
  13678. ((eq org-log-note-purpose 'state)
  13679. (format "state change to \"%s\"" org-log-note-state))
  13680. (t (error "This should not happen")))))
  13681. (org-set-local 'org-finish-function 'org-store-log-note)))
  13682. (defun org-store-log-note ()
  13683. "Finish taking a log note, and insert it to where it belongs."
  13684. (let ((txt (buffer-string))
  13685. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13686. lines ind)
  13687. (kill-buffer (current-buffer))
  13688. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13689. (setq txt (replace-match "" t t txt)))
  13690. (if (string-match "\\s-+\\'" txt)
  13691. (setq txt (replace-match "" t t txt)))
  13692. (setq lines (org-split-string txt "\n"))
  13693. (when (and note (string-match "\\S-" note))
  13694. (setq note
  13695. (org-replace-escapes
  13696. note
  13697. (list (cons "%u" (user-login-name))
  13698. (cons "%U" user-full-name)
  13699. (cons "%t" (format-time-string
  13700. (org-time-stamp-format 'long 'inactive)
  13701. (current-time)))
  13702. (cons "%s" (if org-log-note-state
  13703. (concat "\"" org-log-note-state "\"")
  13704. "")))))
  13705. (if lines (setq note (concat note " \\\\")))
  13706. (push note lines))
  13707. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13708. (when lines
  13709. (save-excursion
  13710. (set-buffer (marker-buffer org-log-note-marker))
  13711. (save-excursion
  13712. (goto-char org-log-note-marker)
  13713. (move-marker org-log-note-marker nil)
  13714. (end-of-line 1)
  13715. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13716. (indent-relative nil)
  13717. (insert "- " (pop lines))
  13718. (org-indent-line-function)
  13719. (beginning-of-line 1)
  13720. (looking-at "[ \t]*")
  13721. (setq ind (concat (match-string 0) " "))
  13722. (end-of-line 1)
  13723. (while lines (insert "\n" ind (pop lines)))))))
  13724. (set-window-configuration org-log-note-window-configuration)
  13725. (with-current-buffer (marker-buffer org-log-note-return-to)
  13726. (goto-char org-log-note-return-to))
  13727. (move-marker org-log-note-return-to nil)
  13728. (and org-log-post-message (message "%s" org-log-post-message)))
  13729. ;; FIXME: what else would be useful?
  13730. ;; - priority
  13731. ;; - date
  13732. (defun org-sparse-tree (&optional arg)
  13733. "Create a sparse tree, prompt for the details.
  13734. This command can create sparse trees. You first need to select the type
  13735. of match used to create the tree:
  13736. t Show entries with a specific TODO keyword.
  13737. T Show entries selected by a tags match.
  13738. p Enter a property name and its value (both with completion on existing
  13739. names/values) and show entries with that property.
  13740. r Show entries matching a regular expression
  13741. d Show deadlines due within `org-deadline-warning-days'."
  13742. (interactive "P")
  13743. (let (ans kwd value)
  13744. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13745. (setq ans (read-char-exclusive))
  13746. (cond
  13747. ((equal ans ?d)
  13748. (call-interactively 'org-check-deadlines))
  13749. ((equal ans ?b)
  13750. (call-interactively 'org-check-before-date))
  13751. ((equal ans ?t)
  13752. (org-show-todo-tree '(4)))
  13753. ((equal ans ?T)
  13754. (call-interactively 'org-tags-sparse-tree))
  13755. ((member ans '(?p ?P))
  13756. (setq kwd (completing-read "Property: "
  13757. (mapcar 'list (org-buffer-property-keys))))
  13758. (setq value (completing-read "Value: "
  13759. (mapcar 'list (org-property-values kwd))))
  13760. (unless (string-match "\\`{.*}\\'" value)
  13761. (setq value (concat "\"" value "\"")))
  13762. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13763. ((member ans '(?r ?R ?/))
  13764. (call-interactively 'org-occur))
  13765. (t (error "No such sparse tree command \"%c\"" ans)))))
  13766. (defvar org-occur-highlights nil)
  13767. (make-variable-buffer-local 'org-occur-highlights)
  13768. (defun org-occur (regexp &optional keep-previous callback)
  13769. "Make a compact tree which shows all matches of REGEXP.
  13770. The tree will show the lines where the regexp matches, and all higher
  13771. headlines above the match. It will also show the heading after the match,
  13772. to make sure editing the matching entry is easy.
  13773. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13774. call to `org-occur' will be kept, to allow stacking of calls to this
  13775. command.
  13776. If CALLBACK is non-nil, it is a function which is called to confirm
  13777. that the match should indeed be shown."
  13778. (interactive "sRegexp: \nP")
  13779. (or keep-previous (org-remove-occur-highlights nil nil t))
  13780. (let ((cnt 0))
  13781. (save-excursion
  13782. (goto-char (point-min))
  13783. (if (or (not keep-previous) ; do not want to keep
  13784. (not org-occur-highlights)) ; no previous matches
  13785. ;; hide everything
  13786. (org-overview))
  13787. (while (re-search-forward regexp nil t)
  13788. (when (or (not callback)
  13789. (save-match-data (funcall callback)))
  13790. (setq cnt (1+ cnt))
  13791. (when org-highlight-sparse-tree-matches
  13792. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13793. (org-show-context 'occur-tree))))
  13794. (when org-remove-highlights-with-change
  13795. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13796. nil 'local))
  13797. (unless org-sparse-tree-open-archived-trees
  13798. (org-hide-archived-subtrees (point-min) (point-max)))
  13799. (run-hooks 'org-occur-hook)
  13800. (if (interactive-p)
  13801. (message "%d match(es) for regexp %s" cnt regexp))
  13802. cnt))
  13803. (defun org-show-context (&optional key)
  13804. "Make sure point and context and visible.
  13805. How much context is shown depends upon the variables
  13806. `org-show-hierarchy-above', `org-show-following-heading'. and
  13807. `org-show-siblings'."
  13808. (let ((heading-p (org-on-heading-p t))
  13809. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13810. (following-p (org-get-alist-option org-show-following-heading key))
  13811. (entry-p (org-get-alist-option org-show-entry-below key))
  13812. (siblings-p (org-get-alist-option org-show-siblings key)))
  13813. (catch 'exit
  13814. ;; Show heading or entry text
  13815. (if (and heading-p (not entry-p))
  13816. (org-flag-heading nil) ; only show the heading
  13817. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13818. (org-show-hidden-entry))) ; show entire entry
  13819. (when following-p
  13820. ;; Show next sibling, or heading below text
  13821. (save-excursion
  13822. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13823. (org-flag-heading nil))))
  13824. (when siblings-p (org-show-siblings))
  13825. (when hierarchy-p
  13826. ;; show all higher headings, possibly with siblings
  13827. (save-excursion
  13828. (while (and (condition-case nil
  13829. (progn (org-up-heading-all 1) t)
  13830. (error nil))
  13831. (not (bobp)))
  13832. (org-flag-heading nil)
  13833. (when siblings-p (org-show-siblings))))))))
  13834. (defun org-reveal (&optional siblings)
  13835. "Show current entry, hierarchy above it, and the following headline.
  13836. This can be used to show a consistent set of context around locations
  13837. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13838. not t for the search context.
  13839. With optional argument SIBLINGS, on each level of the hierarchy all
  13840. siblings are shown. This repairs the tree structure to what it would
  13841. look like when opened with hierarchical calls to `org-cycle'."
  13842. (interactive "P")
  13843. (let ((org-show-hierarchy-above t)
  13844. (org-show-following-heading t)
  13845. (org-show-siblings (if siblings t org-show-siblings)))
  13846. (org-show-context nil)))
  13847. (defun org-highlight-new-match (beg end)
  13848. "Highlight from BEG to END and mark the highlight is an occur headline."
  13849. (let ((ov (org-make-overlay beg end)))
  13850. (org-overlay-put ov 'face 'secondary-selection)
  13851. (push ov org-occur-highlights)))
  13852. (defun org-remove-occur-highlights (&optional beg end noremove)
  13853. "Remove the occur highlights from the buffer.
  13854. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13855. from the `before-change-functions' in the current buffer."
  13856. (interactive)
  13857. (unless org-inhibit-highlight-removal
  13858. (mapc 'org-delete-overlay org-occur-highlights)
  13859. (setq org-occur-highlights nil)
  13860. (unless noremove
  13861. (remove-hook 'before-change-functions
  13862. 'org-remove-occur-highlights 'local))))
  13863. ;;;; Priorities
  13864. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13865. "Regular expression matching the priority indicator.")
  13866. (defvar org-remove-priority-next-time nil)
  13867. (defun org-priority-up ()
  13868. "Increase the priority of the current item."
  13869. (interactive)
  13870. (org-priority 'up))
  13871. (defun org-priority-down ()
  13872. "Decrease the priority of the current item."
  13873. (interactive)
  13874. (org-priority 'down))
  13875. (defun org-priority (&optional action)
  13876. "Change the priority of an item by ARG.
  13877. ACTION can be `set', `up', `down', or a character."
  13878. (interactive)
  13879. (setq action (or action 'set))
  13880. (let (current new news have remove)
  13881. (save-excursion
  13882. (org-back-to-heading)
  13883. (if (looking-at org-priority-regexp)
  13884. (setq current (string-to-char (match-string 2))
  13885. have t)
  13886. (setq current org-default-priority))
  13887. (cond
  13888. ((or (eq action 'set) (integerp action))
  13889. (if (integerp action)
  13890. (setq new action)
  13891. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13892. (setq new (read-char-exclusive)))
  13893. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13894. (= (upcase org-lowest-priority) org-lowest-priority))
  13895. (setq new (upcase new)))
  13896. (cond ((equal new ?\ ) (setq remove t))
  13897. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13898. (error "Priority must be between `%c' and `%c'"
  13899. org-highest-priority org-lowest-priority))))
  13900. ((eq action 'up)
  13901. (if (and (not have) (eq last-command this-command))
  13902. (setq new org-lowest-priority)
  13903. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13904. org-default-priority (1- current)))))
  13905. ((eq action 'down)
  13906. (if (and (not have) (eq last-command this-command))
  13907. (setq new org-highest-priority)
  13908. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13909. org-default-priority (1+ current)))))
  13910. (t (error "Invalid action")))
  13911. (if (or (< (upcase new) org-highest-priority)
  13912. (> (upcase new) org-lowest-priority))
  13913. (setq remove t))
  13914. (setq news (format "%c" new))
  13915. (if have
  13916. (if remove
  13917. (replace-match "" t t nil 1)
  13918. (replace-match news t t nil 2))
  13919. (if remove
  13920. (error "No priority cookie found in line")
  13921. (looking-at org-todo-line-regexp)
  13922. (if (match-end 2)
  13923. (progn
  13924. (goto-char (match-end 2))
  13925. (insert " [#" news "]"))
  13926. (goto-char (match-beginning 3))
  13927. (insert "[#" news "] ")))))
  13928. (org-preserve-lc (org-set-tags nil 'align))
  13929. (if remove
  13930. (message "Priority removed")
  13931. (message "Priority of current item set to %s" news))))
  13932. (defun org-get-priority (s)
  13933. "Find priority cookie and return priority."
  13934. (save-match-data
  13935. (if (not (string-match org-priority-regexp s))
  13936. (* 1000 (- org-lowest-priority org-default-priority))
  13937. (* 1000 (- org-lowest-priority
  13938. (string-to-char (match-string 2 s)))))))
  13939. ;;;; Tags
  13940. (defun org-scan-tags (action matcher &optional todo-only)
  13941. "Scan headline tags with inheritance and produce output ACTION.
  13942. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13943. evaluated, testing if a given set of tags qualifies a headline for
  13944. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13945. are included in the output."
  13946. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13947. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13948. (org-re
  13949. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13950. (props (list 'face nil
  13951. 'done-face 'org-done
  13952. 'undone-face nil
  13953. 'mouse-face 'highlight
  13954. 'org-not-done-regexp org-not-done-regexp
  13955. 'org-todo-regexp org-todo-regexp
  13956. 'keymap org-agenda-keymap
  13957. 'help-echo
  13958. (format "mouse-2 or RET jump to org file %s"
  13959. (abbreviate-file-name
  13960. (or (buffer-file-name (buffer-base-buffer))
  13961. (buffer-name (buffer-base-buffer)))))))
  13962. (case-fold-search nil)
  13963. lspos
  13964. tags tags-list tags-alist (llast 0) rtn level category i txt
  13965. todo marker entry priority)
  13966. (save-excursion
  13967. (goto-char (point-min))
  13968. (when (eq action 'sparse-tree)
  13969. (org-overview)
  13970. (org-remove-occur-highlights))
  13971. (while (re-search-forward re nil t)
  13972. (catch :skip
  13973. (setq todo (if (match-end 1) (match-string 2))
  13974. tags (if (match-end 4) (match-string 4)))
  13975. (goto-char (setq lspos (1+ (match-beginning 0))))
  13976. (setq level (org-reduced-level (funcall outline-level))
  13977. category (org-get-category))
  13978. (setq i llast llast level)
  13979. ;; remove tag lists from same and sublevels
  13980. (while (>= i level)
  13981. (when (setq entry (assoc i tags-alist))
  13982. (setq tags-alist (delete entry tags-alist)))
  13983. (setq i (1- i)))
  13984. ;; add the nex tags
  13985. (when tags
  13986. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13987. tags-alist
  13988. (cons (cons level tags) tags-alist)))
  13989. ;; compile tags for current headline
  13990. (setq tags-list
  13991. (if org-use-tag-inheritance
  13992. (apply 'append (mapcar 'cdr tags-alist))
  13993. tags))
  13994. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13995. (eval matcher)
  13996. (or (not org-agenda-skip-archived-trees)
  13997. (not (member org-archive-tag tags-list))))
  13998. (and (eq action 'agenda) (org-agenda-skip))
  13999. ;; list this headline
  14000. (if (eq action 'sparse-tree)
  14001. (progn
  14002. (and org-highlight-sparse-tree-matches
  14003. (org-get-heading) (match-end 0)
  14004. (org-highlight-new-match
  14005. (match-beginning 0) (match-beginning 1)))
  14006. (org-show-context 'tags-tree))
  14007. (setq txt (org-format-agenda-item
  14008. ""
  14009. (concat
  14010. (if org-tags-match-list-sublevels
  14011. (make-string (1- level) ?.) "")
  14012. (org-get-heading))
  14013. category tags-list)
  14014. priority (org-get-priority txt))
  14015. (goto-char lspos)
  14016. (setq marker (org-agenda-new-marker))
  14017. (org-add-props txt props
  14018. 'org-marker marker 'org-hd-marker marker 'org-category category
  14019. 'priority priority 'type "tagsmatch")
  14020. (push txt rtn))
  14021. ;; if we are to skip sublevels, jump to end of subtree
  14022. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14023. (when (and (eq action 'sparse-tree)
  14024. (not org-sparse-tree-open-archived-trees))
  14025. (org-hide-archived-subtrees (point-min) (point-max)))
  14026. (nreverse rtn)))
  14027. (defvar todo-only) ;; dynamically scoped
  14028. (defun org-tags-sparse-tree (&optional todo-only match)
  14029. "Create a sparse tree according to tags string MATCH.
  14030. MATCH can contain positive and negative selection of tags, like
  14031. \"+WORK+URGENT-WITHBOSS\".
  14032. If optional argument TODO_ONLY is non-nil, only select lines that are
  14033. also TODO lines."
  14034. (interactive "P")
  14035. (org-prepare-agenda-buffers (list (current-buffer)))
  14036. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14037. (defvar org-cached-props nil)
  14038. (defun org-cached-entry-get (pom property)
  14039. (if (or (eq t org-use-property-inheritance)
  14040. (member property org-use-property-inheritance))
  14041. ;; Caching is not possible, check it directly
  14042. (org-entry-get pom property 'inherit)
  14043. ;; Get all properties, so that we can do complicated checks easily
  14044. (cdr (assoc property (or org-cached-props
  14045. (setq org-cached-props
  14046. (org-entry-properties pom)))))))
  14047. (defun org-global-tags-completion-table (&optional files)
  14048. "Return the list of all tags in all agenda buffer/files."
  14049. (save-excursion
  14050. (org-uniquify
  14051. (delq nil
  14052. (apply 'append
  14053. (mapcar
  14054. (lambda (file)
  14055. (set-buffer (find-file-noselect file))
  14056. (append (org-get-buffer-tags)
  14057. (mapcar (lambda (x) (if (stringp (car-safe x))
  14058. (list (car-safe x)) nil))
  14059. org-tag-alist)))
  14060. (if (and files (car files))
  14061. files
  14062. (org-agenda-files))))))))
  14063. (defun org-make-tags-matcher (match)
  14064. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14065. ;; todo-only is scoped dynamically into this function, and the function
  14066. ;; may change it it the matcher asksk for it.
  14067. (unless match
  14068. ;; Get a new match request, with completion
  14069. (let ((org-last-tags-completion-table
  14070. (org-global-tags-completion-table)))
  14071. (setq match (completing-read
  14072. "Match: " 'org-tags-completion-function nil nil nil
  14073. 'org-tags-history))))
  14074. ;; Parse the string and create a lisp form
  14075. (let ((match0 match)
  14076. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14077. minus tag mm
  14078. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14079. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14080. (if (string-match "/+" match)
  14081. ;; match contains also a todo-matching request
  14082. (progn
  14083. (setq tagsmatch (substring match 0 (match-beginning 0))
  14084. todomatch (substring match (match-end 0)))
  14085. (if (string-match "^!" todomatch)
  14086. (setq todo-only t todomatch (substring todomatch 1)))
  14087. (if (string-match "^\\s-*$" todomatch)
  14088. (setq todomatch nil)))
  14089. ;; only matching tags
  14090. (setq tagsmatch match todomatch nil))
  14091. ;; Make the tags matcher
  14092. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14093. (setq tagsmatcher t)
  14094. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14095. (while (setq term (pop orterms))
  14096. (while (and (equal (substring term -1) "\\") orterms)
  14097. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14098. (while (string-match re term)
  14099. (setq minus (and (match-end 1)
  14100. (equal (match-string 1 term) "-"))
  14101. tag (match-string 2 term)
  14102. re-p (equal (string-to-char tag) ?{)
  14103. level-p (match-end 3)
  14104. prop-p (match-end 4)
  14105. mm (cond
  14106. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14107. (level-p `(= level ,(string-to-number
  14108. (match-string 3 term))))
  14109. (prop-p
  14110. (setq pn (match-string 4 term)
  14111. pv (match-string 5 term)
  14112. cat-p (equal pn "CATEGORY")
  14113. re-p (equal (string-to-char pv) ?{)
  14114. pv (substring pv 1 -1))
  14115. (if (equal pn "CATEGORY")
  14116. (setq gv '(get-text-property (point) 'org-category))
  14117. (setq gv `(org-cached-entry-get nil ,pn)))
  14118. (if re-p
  14119. `(string-match ,pv (or ,gv ""))
  14120. `(equal ,pv (or ,gv ""))))
  14121. (t `(member ,(downcase tag) tags-list)))
  14122. mm (if minus (list 'not mm) mm)
  14123. term (substring term (match-end 0)))
  14124. (push mm tagsmatcher))
  14125. (push (if (> (length tagsmatcher) 1)
  14126. (cons 'and tagsmatcher)
  14127. (car tagsmatcher))
  14128. orlist)
  14129. (setq tagsmatcher nil))
  14130. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14131. (setq tagsmatcher
  14132. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14133. ;; Make the todo matcher
  14134. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14135. (setq todomatcher t)
  14136. (setq orterms (org-split-string todomatch "|") orlist nil)
  14137. (while (setq term (pop orterms))
  14138. (while (string-match re term)
  14139. (setq minus (and (match-end 1)
  14140. (equal (match-string 1 term) "-"))
  14141. kwd (match-string 2 term)
  14142. re-p (equal (string-to-char kwd) ?{)
  14143. term (substring term (match-end 0))
  14144. mm (if re-p
  14145. `(string-match ,(substring kwd 1 -1) todo)
  14146. (list 'equal 'todo kwd))
  14147. mm (if minus (list 'not mm) mm))
  14148. (push mm todomatcher))
  14149. (push (if (> (length todomatcher) 1)
  14150. (cons 'and todomatcher)
  14151. (car todomatcher))
  14152. orlist)
  14153. (setq todomatcher nil))
  14154. (setq todomatcher (if (> (length orlist) 1)
  14155. (cons 'or orlist) (car orlist))))
  14156. ;; Return the string and lisp forms of the matcher
  14157. (setq matcher (if todomatcher
  14158. (list 'and tagsmatcher todomatcher)
  14159. tagsmatcher))
  14160. (cons match0 matcher)))
  14161. (defun org-match-any-p (re list)
  14162. "Does re match any element of list?"
  14163. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14164. (delq nil list))
  14165. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14166. (defvar org-tags-overlay (org-make-overlay 1 1))
  14167. (org-detach-overlay org-tags-overlay)
  14168. (defun org-align-tags-here (to-col)
  14169. ;; Assumes that this is a headline
  14170. (let ((pos (point)) (col (current-column)) tags)
  14171. (beginning-of-line 1)
  14172. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14173. (< pos (match-beginning 2)))
  14174. (progn
  14175. (setq tags (match-string 2))
  14176. (goto-char (match-beginning 1))
  14177. (insert " ")
  14178. (delete-region (point) (1+ (match-end 0)))
  14179. (backward-char 1)
  14180. (move-to-column
  14181. (max (1+ (current-column))
  14182. (1+ col)
  14183. (if (> to-col 0)
  14184. to-col
  14185. (- (abs to-col) (length tags))))
  14186. t)
  14187. (insert tags)
  14188. (move-to-column (min (current-column) col) t))
  14189. (goto-char pos))))
  14190. (defun org-set-tags (&optional arg just-align)
  14191. "Set the tags for the current headline.
  14192. With prefix ARG, realign all tags in headings in the current buffer."
  14193. (interactive "P")
  14194. (let* ((re (concat "^" outline-regexp))
  14195. (current (org-get-tags-string))
  14196. (col (current-column))
  14197. (org-setting-tags t)
  14198. table current-tags inherited-tags ; computed below when needed
  14199. tags p0 c0 c1 rpl)
  14200. (if arg
  14201. (save-excursion
  14202. (goto-char (point-min))
  14203. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14204. (while (re-search-forward re nil t)
  14205. (org-set-tags nil t)
  14206. (end-of-line 1)))
  14207. (message "All tags realigned to column %d" org-tags-column))
  14208. (if just-align
  14209. (setq tags current)
  14210. ;; Get a new set of tags from the user
  14211. (save-excursion
  14212. (setq table (or org-tag-alist (org-get-buffer-tags))
  14213. org-last-tags-completion-table table
  14214. current-tags (org-split-string current ":")
  14215. inherited-tags (nreverse
  14216. (nthcdr (length current-tags)
  14217. (nreverse (org-get-tags-at))))
  14218. tags
  14219. (if (or (eq t org-use-fast-tag-selection)
  14220. (and org-use-fast-tag-selection
  14221. (delq nil (mapcar 'cdr table))))
  14222. (org-fast-tag-selection
  14223. current-tags inherited-tags table
  14224. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14225. (let ((org-add-colon-after-tag-completion t))
  14226. (org-trim
  14227. (org-without-partial-completion
  14228. (completing-read "Tags: " 'org-tags-completion-function
  14229. nil nil current 'org-tags-history)))))))
  14230. (while (string-match "[-+&]+" tags)
  14231. ;; No boolean logic, just a list
  14232. (setq tags (replace-match ":" t t tags))))
  14233. (if (string-match "\\`[\t ]*\\'" tags)
  14234. (setq tags "")
  14235. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14236. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14237. ;; Insert new tags at the correct column
  14238. (beginning-of-line 1)
  14239. (cond
  14240. ((and (equal current "") (equal tags "")))
  14241. ((re-search-forward
  14242. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14243. (point-at-eol) t)
  14244. (if (equal tags "")
  14245. (setq rpl "")
  14246. (goto-char (match-beginning 0))
  14247. (setq c0 (current-column) p0 (point)
  14248. c1 (max (1+ c0) (if (> org-tags-column 0)
  14249. org-tags-column
  14250. (- (- org-tags-column) (length tags))))
  14251. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14252. (replace-match rpl t t)
  14253. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14254. tags)
  14255. (t (error "Tags alignment failed")))
  14256. (move-to-column col)
  14257. (unless just-align
  14258. (run-hooks 'org-after-tags-change-hook)))))
  14259. (defun org-change-tag-in-region (beg end tag off)
  14260. "Add or remove TAG for each entry in the region.
  14261. This works in the agenda, and also in an org-mode buffer."
  14262. (interactive
  14263. (list (region-beginning) (region-end)
  14264. (let ((org-last-tags-completion-table
  14265. (if (org-mode-p)
  14266. (org-get-buffer-tags)
  14267. (org-global-tags-completion-table))))
  14268. (completing-read
  14269. "Tag: " 'org-tags-completion-function nil nil nil
  14270. 'org-tags-history))
  14271. (progn
  14272. (message "[s]et or [r]emove? ")
  14273. (equal (read-char-exclusive) ?r))))
  14274. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14275. (let ((agendap (equal major-mode 'org-agenda-mode))
  14276. l1 l2 m buf pos newhead (cnt 0))
  14277. (goto-char end)
  14278. (setq l2 (1- (org-current-line)))
  14279. (goto-char beg)
  14280. (setq l1 (org-current-line))
  14281. (loop for l from l1 to l2 do
  14282. (goto-line l)
  14283. (setq m (get-text-property (point) 'org-hd-marker))
  14284. (when (or (and (org-mode-p) (org-on-heading-p))
  14285. (and agendap m))
  14286. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14287. pos (if agendap m (point)))
  14288. (with-current-buffer buf
  14289. (save-excursion
  14290. (save-restriction
  14291. (goto-char pos)
  14292. (setq cnt (1+ cnt))
  14293. (org-toggle-tag tag (if off 'off 'on))
  14294. (setq newhead (org-get-heading)))))
  14295. (and agendap (org-agenda-change-all-lines newhead m))))
  14296. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14297. (defun org-tags-completion-function (string predicate &optional flag)
  14298. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14299. (confirm (lambda (x) (stringp (car x)))))
  14300. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14301. (setq s1 (match-string 1 string)
  14302. s2 (match-string 2 string))
  14303. (setq s1 "" s2 string))
  14304. (cond
  14305. ((eq flag nil)
  14306. ;; try completion
  14307. (setq rtn (try-completion s2 ctable confirm))
  14308. (if (stringp rtn)
  14309. (setq rtn
  14310. (concat s1 s2 (substring rtn (length s2))
  14311. (if (and org-add-colon-after-tag-completion
  14312. (assoc rtn ctable))
  14313. ":" ""))))
  14314. rtn)
  14315. ((eq flag t)
  14316. ;; all-completions
  14317. (all-completions s2 ctable confirm)
  14318. )
  14319. ((eq flag 'lambda)
  14320. ;; exact match?
  14321. (assoc s2 ctable)))
  14322. ))
  14323. (defun org-fast-tag-insert (kwd tags face &optional end)
  14324. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14325. (insert (format "%-12s" (concat kwd ":"))
  14326. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14327. (or end "")))
  14328. (defun org-fast-tag-show-exit (flag)
  14329. (save-excursion
  14330. (goto-line 3)
  14331. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14332. (replace-match ""))
  14333. (when flag
  14334. (end-of-line 1)
  14335. (move-to-column (- (window-width) 19) t)
  14336. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14337. (defun org-set-current-tags-overlay (current prefix)
  14338. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14339. (if (featurep 'xemacs)
  14340. (org-overlay-display org-tags-overlay (concat prefix s)
  14341. 'secondary-selection)
  14342. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14343. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14344. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14345. "Fast tag selection with single keys.
  14346. CURRENT is the current list of tags in the headline, INHERITED is the
  14347. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14348. possibly with grouping information. TODO-TABLE is a similar table with
  14349. TODO keywords, should these have keys assigned to them.
  14350. If the keys are nil, a-z are automatically assigned.
  14351. Returns the new tags string, or nil to not change the current settings."
  14352. (let* ((fulltable (append table todo-table))
  14353. (maxlen (apply 'max (mapcar
  14354. (lambda (x)
  14355. (if (stringp (car x)) (string-width (car x)) 0))
  14356. fulltable)))
  14357. (buf (current-buffer))
  14358. (expert (eq org-fast-tag-selection-single-key 'expert))
  14359. (buffer-tags nil)
  14360. (fwidth (+ maxlen 3 1 3))
  14361. (ncol (/ (- (window-width) 4) fwidth))
  14362. (i-face 'org-done)
  14363. (c-face 'org-todo)
  14364. tg cnt e c char c1 c2 ntable tbl rtn
  14365. ov-start ov-end ov-prefix
  14366. (exit-after-next org-fast-tag-selection-single-key)
  14367. (done-keywords org-done-keywords)
  14368. groups ingroup)
  14369. (save-excursion
  14370. (beginning-of-line 1)
  14371. (if (looking-at
  14372. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14373. (setq ov-start (match-beginning 1)
  14374. ov-end (match-end 1)
  14375. ov-prefix "")
  14376. (setq ov-start (1- (point-at-eol))
  14377. ov-end (1+ ov-start))
  14378. (skip-chars-forward "^\n\r")
  14379. (setq ov-prefix
  14380. (concat
  14381. (buffer-substring (1- (point)) (point))
  14382. (if (> (current-column) org-tags-column)
  14383. " "
  14384. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14385. (org-move-overlay org-tags-overlay ov-start ov-end)
  14386. (save-window-excursion
  14387. (if expert
  14388. (set-buffer (get-buffer-create " *Org tags*"))
  14389. (delete-other-windows)
  14390. (split-window-vertically)
  14391. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14392. (erase-buffer)
  14393. (org-set-local 'org-done-keywords done-keywords)
  14394. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14395. (org-fast-tag-insert "Current" current c-face "\n\n")
  14396. (org-fast-tag-show-exit exit-after-next)
  14397. (org-set-current-tags-overlay current ov-prefix)
  14398. (setq tbl fulltable char ?a cnt 0)
  14399. (while (setq e (pop tbl))
  14400. (cond
  14401. ((equal e '(:startgroup))
  14402. (push '() groups) (setq ingroup t)
  14403. (when (not (= cnt 0))
  14404. (setq cnt 0)
  14405. (insert "\n"))
  14406. (insert "{ "))
  14407. ((equal e '(:endgroup))
  14408. (setq ingroup nil cnt 0)
  14409. (insert "}\n"))
  14410. (t
  14411. (setq tg (car e) c2 nil)
  14412. (if (cdr e)
  14413. (setq c (cdr e))
  14414. ;; automatically assign a character.
  14415. (setq c1 (string-to-char
  14416. (downcase (substring
  14417. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14418. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14419. (while (or (rassoc char ntable) (rassoc char table))
  14420. (setq char (1+ char)))
  14421. (setq c2 c1))
  14422. (setq c (or c2 char)))
  14423. (if ingroup (push tg (car groups)))
  14424. (setq tg (org-add-props tg nil 'face
  14425. (cond
  14426. ((not (assoc tg table))
  14427. (org-get-todo-face tg))
  14428. ((member tg current) c-face)
  14429. ((member tg inherited) i-face)
  14430. (t nil))))
  14431. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14432. (insert "[" c "] " tg (make-string
  14433. (- fwidth 4 (length tg)) ?\ ))
  14434. (push (cons tg c) ntable)
  14435. (when (= (setq cnt (1+ cnt)) ncol)
  14436. (insert "\n")
  14437. (if ingroup (insert " "))
  14438. (setq cnt 0)))))
  14439. (setq ntable (nreverse ntable))
  14440. (insert "\n")
  14441. (goto-char (point-min))
  14442. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14443. (fit-window-to-buffer))
  14444. (setq rtn
  14445. (catch 'exit
  14446. (while t
  14447. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14448. (if groups " [!] no groups" " [!]groups")
  14449. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14450. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14451. (cond
  14452. ((= c ?\r) (throw 'exit t))
  14453. ((= c ?!)
  14454. (setq groups (not groups))
  14455. (goto-char (point-min))
  14456. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14457. ((= c ?\C-c)
  14458. (if (not expert)
  14459. (org-fast-tag-show-exit
  14460. (setq exit-after-next (not exit-after-next)))
  14461. (setq expert nil)
  14462. (delete-other-windows)
  14463. (split-window-vertically)
  14464. (org-switch-to-buffer-other-window " *Org tags*")
  14465. (and (fboundp 'fit-window-to-buffer)
  14466. (fit-window-to-buffer))))
  14467. ((or (= c ?\C-g)
  14468. (and (= c ?q) (not (rassoc c ntable))))
  14469. (org-detach-overlay org-tags-overlay)
  14470. (setq quit-flag t))
  14471. ((= c ?\ )
  14472. (setq current nil)
  14473. (if exit-after-next (setq exit-after-next 'now)))
  14474. ((= c ?\t)
  14475. (condition-case nil
  14476. (setq tg (completing-read
  14477. "Tag: "
  14478. (or buffer-tags
  14479. (with-current-buffer buf
  14480. (org-get-buffer-tags)))))
  14481. (quit (setq tg "")))
  14482. (when (string-match "\\S-" tg)
  14483. (add-to-list 'buffer-tags (list tg))
  14484. (if (member tg current)
  14485. (setq current (delete tg current))
  14486. (push tg current)))
  14487. (if exit-after-next (setq exit-after-next 'now)))
  14488. ((setq e (rassoc c todo-table) tg (car e))
  14489. (with-current-buffer buf
  14490. (save-excursion (org-todo tg)))
  14491. (if exit-after-next (setq exit-after-next 'now)))
  14492. ((setq e (rassoc c ntable) tg (car e))
  14493. (if (member tg current)
  14494. (setq current (delete tg current))
  14495. (loop for g in groups do
  14496. (if (member tg g)
  14497. (mapc (lambda (x)
  14498. (setq current (delete x current)))
  14499. g)))
  14500. (push tg current))
  14501. (if exit-after-next (setq exit-after-next 'now))))
  14502. ;; Create a sorted list
  14503. (setq current
  14504. (sort current
  14505. (lambda (a b)
  14506. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14507. (if (eq exit-after-next 'now) (throw 'exit t))
  14508. (goto-char (point-min))
  14509. (beginning-of-line 2)
  14510. (delete-region (point) (point-at-eol))
  14511. (org-fast-tag-insert "Current" current c-face)
  14512. (org-set-current-tags-overlay current ov-prefix)
  14513. (while (re-search-forward
  14514. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14515. (setq tg (match-string 1))
  14516. (add-text-properties
  14517. (match-beginning 1) (match-end 1)
  14518. (list 'face
  14519. (cond
  14520. ((member tg current) c-face)
  14521. ((member tg inherited) i-face)
  14522. (t (get-text-property (match-beginning 1) 'face))))))
  14523. (goto-char (point-min)))))
  14524. (org-detach-overlay org-tags-overlay)
  14525. (if rtn
  14526. (mapconcat 'identity current ":")
  14527. nil))))
  14528. (defun org-get-tags-string ()
  14529. "Get the TAGS string in the current headline."
  14530. (unless (org-on-heading-p t)
  14531. (error "Not on a heading"))
  14532. (save-excursion
  14533. (beginning-of-line 1)
  14534. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14535. (org-match-string-no-properties 1)
  14536. "")))
  14537. (defun org-get-tags ()
  14538. "Get the list of tags specified in the current headline."
  14539. (org-split-string (org-get-tags-string) ":"))
  14540. (defun org-get-buffer-tags ()
  14541. "Get a table of all tags used in the buffer, for completion."
  14542. (let (tags)
  14543. (save-excursion
  14544. (goto-char (point-min))
  14545. (while (re-search-forward
  14546. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14547. (when (equal (char-after (point-at-bol 0)) ?*)
  14548. (mapc (lambda (x) (add-to-list 'tags x))
  14549. (org-split-string (org-match-string-no-properties 1) ":")))))
  14550. (mapcar 'list tags)))
  14551. ;;;; Properties
  14552. ;;; Setting and retrieving properties
  14553. (defconst org-special-properties
  14554. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14555. "TIMESTAMP" "TIMESTAMP_IA")
  14556. "The special properties valid in Org-mode.
  14557. These are properties that are not defined in the property drawer,
  14558. but in some other way.")
  14559. (defconst org-default-properties
  14560. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14561. "LOCATION" "LOGGING" "COLUMNS")
  14562. "Some properties that are used by Org-mode for various purposes.
  14563. Being in this list makes sure that they are offered for completion.")
  14564. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14565. "Regular expression matching the first line of a property drawer.")
  14566. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14567. "Regular expression matching the first line of a property drawer.")
  14568. (defun org-property-action ()
  14569. "Do an action on properties."
  14570. (interactive)
  14571. (let (c)
  14572. (org-at-property-p)
  14573. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14574. (setq c (read-char-exclusive))
  14575. (cond
  14576. ((equal c ?s)
  14577. (call-interactively 'org-set-property))
  14578. ((equal c ?d)
  14579. (call-interactively 'org-delete-property))
  14580. ((equal c ?D)
  14581. (call-interactively 'org-delete-property-globally))
  14582. ((equal c ?c)
  14583. (call-interactively 'org-compute-property-at-point))
  14584. (t (error "No such property action %c" c)))))
  14585. (defun org-at-property-p ()
  14586. "Is the cursor in a property line?"
  14587. ;; FIXME: Does not check if we are actually in the drawer.
  14588. ;; FIXME: also returns true on any drawers.....
  14589. ;; This is used by C-c C-c for property action.
  14590. (save-excursion
  14591. (beginning-of-line 1)
  14592. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14593. (defmacro org-with-point-at (pom &rest body)
  14594. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14595. (declare (indent 1) (debug t))
  14596. `(save-excursion
  14597. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14598. (save-excursion
  14599. (goto-char (or pom (point)))
  14600. ,@body)))
  14601. (defun org-get-property-block (&optional beg end force)
  14602. "Return the (beg . end) range of the body of the property drawer.
  14603. BEG and END can be beginning and end of subtree, if not given
  14604. they will be found.
  14605. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14606. (catch 'exit
  14607. (save-excursion
  14608. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14609. (end (or end (progn (outline-next-heading) (point)))))
  14610. (goto-char beg)
  14611. (if (re-search-forward org-property-start-re end t)
  14612. (setq beg (1+ (match-end 0)))
  14613. (if force
  14614. (save-excursion
  14615. (org-insert-property-drawer)
  14616. (setq end (progn (outline-next-heading) (point))))
  14617. (throw 'exit nil))
  14618. (goto-char beg)
  14619. (if (re-search-forward org-property-start-re end t)
  14620. (setq beg (1+ (match-end 0)))))
  14621. (if (re-search-forward org-property-end-re end t)
  14622. (setq end (match-beginning 0))
  14623. (or force (throw 'exit nil))
  14624. (goto-char beg)
  14625. (setq end beg)
  14626. (org-indent-line-function)
  14627. (insert ":END:\n"))
  14628. (cons beg end)))))
  14629. (defun org-entry-properties (&optional pom which)
  14630. "Get all properties of the entry at point-or-marker POM.
  14631. This includes the TODO keyword, the tags, time strings for deadline,
  14632. scheduled, and clocking, and any additional properties defined in the
  14633. entry. The return value is an alist, keys may occur multiple times
  14634. if the property key was used several times.
  14635. POM may also be nil, in which case the current entry is used.
  14636. If WHICH is nil or `all', get all properties. If WHICH is
  14637. `special' or `standard', only get that subclass."
  14638. (setq which (or which 'all))
  14639. (org-with-point-at pom
  14640. (let ((clockstr (substring org-clock-string 0 -1))
  14641. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14642. beg end range props sum-props key value string clocksum)
  14643. (save-excursion
  14644. (when (condition-case nil (org-back-to-heading t) (error nil))
  14645. (setq beg (point))
  14646. (setq sum-props (get-text-property (point) 'org-summaries))
  14647. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14648. (outline-next-heading)
  14649. (setq end (point))
  14650. (when (memq which '(all special))
  14651. ;; Get the special properties, like TODO and tags
  14652. (goto-char beg)
  14653. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14654. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14655. (when (looking-at org-priority-regexp)
  14656. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14657. (when (and (setq value (org-get-tags-string))
  14658. (string-match "\\S-" value))
  14659. (push (cons "TAGS" value) props))
  14660. (when (setq value (org-get-tags-at))
  14661. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14662. props))
  14663. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14664. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14665. string (if (equal key clockstr)
  14666. (org-no-properties
  14667. (org-trim
  14668. (buffer-substring
  14669. (match-beginning 3) (goto-char (point-at-eol)))))
  14670. (substring (org-match-string-no-properties 3) 1 -1)))
  14671. (unless key
  14672. (if (= (char-after (match-beginning 3)) ?\[)
  14673. (setq key "TIMESTAMP_IA")
  14674. (setq key "TIMESTAMP")))
  14675. (when (or (equal key clockstr) (not (assoc key props)))
  14676. (push (cons key string) props)))
  14677. )
  14678. (when (memq which '(all standard))
  14679. ;; Get the standard properties, like :PORP: ...
  14680. (setq range (org-get-property-block beg end))
  14681. (when range
  14682. (goto-char (car range))
  14683. (while (re-search-forward
  14684. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14685. (cdr range) t)
  14686. (setq key (org-match-string-no-properties 1)
  14687. value (org-trim (or (org-match-string-no-properties 2) "")))
  14688. (unless (member key excluded)
  14689. (push (cons key (or value "")) props)))))
  14690. (if clocksum
  14691. (push (cons "CLOCKSUM"
  14692. (org-column-number-to-string (/ (float clocksum) 60.)
  14693. 'add_times))
  14694. props))
  14695. (append sum-props (nreverse props)))))))
  14696. (defun org-entry-get (pom property &optional inherit)
  14697. "Get value of PROPERTY for entry at point-or-marker POM.
  14698. If INHERIT is non-nil and the entry does not have the property,
  14699. then also check higher levels of the hierarchy.
  14700. If the property is present but empty, the return value is the empty string.
  14701. If the property is not present at all, nil is returned."
  14702. (org-with-point-at pom
  14703. (if inherit
  14704. (org-entry-get-with-inheritance property)
  14705. (if (member property org-special-properties)
  14706. ;; We need a special property. Use brute force, get all properties.
  14707. (cdr (assoc property (org-entry-properties nil 'special)))
  14708. (let ((range (org-get-property-block)))
  14709. (if (and range
  14710. (goto-char (car range))
  14711. (re-search-forward
  14712. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14713. (cdr range) t))
  14714. ;; Found the property, return it.
  14715. (if (match-end 1)
  14716. (org-match-string-no-properties 1)
  14717. "")))))))
  14718. (defun org-entry-delete (pom property)
  14719. "Delete the property PROPERTY from entry at point-or-marker POM."
  14720. (org-with-point-at pom
  14721. (if (member property org-special-properties)
  14722. nil ; cannot delete these properties.
  14723. (let ((range (org-get-property-block)))
  14724. (if (and range
  14725. (goto-char (car range))
  14726. (re-search-forward
  14727. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14728. (cdr range) t))
  14729. (progn
  14730. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14731. t)
  14732. nil)))))
  14733. ;; Multi-values properties are properties that contain multiple values
  14734. ;; These values are assumed to be single words, separated by whitespace.
  14735. (defun org-entry-add-to-multivalued-property (pom property value)
  14736. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14737. (let* ((old (org-entry-get pom property))
  14738. (values (and old (org-split-string old "[ \t]"))))
  14739. (unless (member value values)
  14740. (setq values (cons value values))
  14741. (org-entry-put pom property
  14742. (mapconcat 'identity values " ")))))
  14743. (defun org-entry-remove-from-multivalued-property (pom property value)
  14744. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14745. (let* ((old (org-entry-get pom property))
  14746. (values (and old (org-split-string old "[ \t]"))))
  14747. (when (member value values)
  14748. (setq values (delete value values))
  14749. (org-entry-put pom property
  14750. (mapconcat 'identity values " ")))))
  14751. (defun org-entry-member-in-multivalued-property (pom property value)
  14752. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14753. (let* ((old (org-entry-get pom property))
  14754. (values (and old (org-split-string old "[ \t]"))))
  14755. (member value values)))
  14756. (defvar org-entry-property-inherited-from (make-marker))
  14757. (defun org-entry-get-with-inheritance (property)
  14758. "Get entry property, and search higher levels if not present."
  14759. (let (tmp)
  14760. (save-excursion
  14761. (save-restriction
  14762. (widen)
  14763. (catch 'ex
  14764. (while t
  14765. (when (setq tmp (org-entry-get nil property))
  14766. (org-back-to-heading t)
  14767. (move-marker org-entry-property-inherited-from (point))
  14768. (throw 'ex tmp))
  14769. (or (org-up-heading-safe) (throw 'ex nil)))))
  14770. (or tmp (cdr (assoc property org-local-properties))
  14771. (cdr (assoc property org-global-properties))))))
  14772. (defun org-entry-put (pom property value)
  14773. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14774. (org-with-point-at pom
  14775. (org-back-to-heading t)
  14776. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14777. range)
  14778. (cond
  14779. ((equal property "TODO")
  14780. (when (and (stringp value) (string-match "\\S-" value)
  14781. (not (member value org-todo-keywords-1)))
  14782. (error "\"%s\" is not a valid TODO state" value))
  14783. (if (or (not value)
  14784. (not (string-match "\\S-" value)))
  14785. (setq value 'none))
  14786. (org-todo value)
  14787. (org-set-tags nil 'align))
  14788. ((equal property "PRIORITY")
  14789. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14790. (string-to-char value) ?\ ))
  14791. (org-set-tags nil 'align))
  14792. ((equal property "SCHEDULED")
  14793. (if (re-search-forward org-scheduled-time-regexp end t)
  14794. (cond
  14795. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14796. ((eq value 'later) (org-timestamp-change 1 'day))
  14797. (t (call-interactively 'org-schedule)))
  14798. (call-interactively 'org-schedule)))
  14799. ((equal property "DEADLINE")
  14800. (if (re-search-forward org-deadline-time-regexp end t)
  14801. (cond
  14802. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14803. ((eq value 'later) (org-timestamp-change 1 'day))
  14804. (t (call-interactively 'org-deadline)))
  14805. (call-interactively 'org-deadline)))
  14806. ((member property org-special-properties)
  14807. (error "The %s property can not yet be set with `org-entry-put'"
  14808. property))
  14809. (t ; a non-special property
  14810. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14811. (setq range (org-get-property-block beg end 'force))
  14812. (goto-char (car range))
  14813. (if (re-search-forward
  14814. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14815. (progn
  14816. (delete-region (match-beginning 1) (match-end 1))
  14817. (goto-char (match-beginning 1)))
  14818. (goto-char (cdr range))
  14819. (insert "\n")
  14820. (backward-char 1)
  14821. (org-indent-line-function)
  14822. (insert ":" property ":"))
  14823. (and value (insert " " value))
  14824. (org-indent-line-function)))))))
  14825. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14826. "Get all property keys in the current buffer.
  14827. With INCLUDE-SPECIALS, also list the special properties that relect things
  14828. like tags and TODO state.
  14829. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14830. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14831. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14832. formats in the current buffer."
  14833. (let (rtn range cfmt cols s p)
  14834. (save-excursion
  14835. (save-restriction
  14836. (widen)
  14837. (goto-char (point-min))
  14838. (while (re-search-forward org-property-start-re nil t)
  14839. (setq range (org-get-property-block))
  14840. (goto-char (car range))
  14841. (while (re-search-forward
  14842. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14843. (cdr range) t)
  14844. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14845. (outline-next-heading))))
  14846. (when include-specials
  14847. (setq rtn (append org-special-properties rtn)))
  14848. (when include-defaults
  14849. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14850. (when include-columns
  14851. (save-excursion
  14852. (save-restriction
  14853. (widen)
  14854. (goto-char (point-min))
  14855. (while (re-search-forward
  14856. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14857. nil t)
  14858. (setq cfmt (match-string 2) s 0)
  14859. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14860. cfmt s)
  14861. (setq s (match-end 0)
  14862. p (match-string 1 cfmt))
  14863. (unless (or (equal p "ITEM")
  14864. (member p org-special-properties))
  14865. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14866. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14867. (defun org-property-values (key)
  14868. "Return a list of all values of property KEY."
  14869. (save-excursion
  14870. (save-restriction
  14871. (widen)
  14872. (goto-char (point-min))
  14873. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14874. values)
  14875. (while (re-search-forward re nil t)
  14876. (add-to-list 'values (org-trim (match-string 1))))
  14877. (delete "" values)))))
  14878. (defun org-insert-property-drawer ()
  14879. "Insert a property drawer into the current entry."
  14880. (interactive)
  14881. (org-back-to-heading t)
  14882. (looking-at outline-regexp)
  14883. (let ((indent (- (match-end 0)(match-beginning 0)))
  14884. (beg (point))
  14885. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14886. end hiddenp)
  14887. (outline-next-heading)
  14888. (setq end (point))
  14889. (goto-char beg)
  14890. (while (re-search-forward re end t))
  14891. (setq hiddenp (org-invisible-p))
  14892. (end-of-line 1)
  14893. (and (equal (char-after) ?\n) (forward-char 1))
  14894. (org-skip-over-state-notes)
  14895. (skip-chars-backward " \t\n\r")
  14896. (if (eq (char-before) ?*) (forward-char 1))
  14897. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14898. (beginning-of-line 0)
  14899. (indent-to-column indent)
  14900. (beginning-of-line 2)
  14901. (indent-to-column indent)
  14902. (beginning-of-line 0)
  14903. (if hiddenp
  14904. (save-excursion
  14905. (org-back-to-heading t)
  14906. (hide-entry))
  14907. (org-flag-drawer t))))
  14908. (defun org-set-property (property value)
  14909. "In the current entry, set PROPERTY to VALUE.
  14910. When called interactively, this will prompt for a property name, offering
  14911. completion on existing and default properties. And then it will prompt
  14912. for a value, offering competion either on allowed values (via an inherited
  14913. xxx_ALL property) or on existing values in other instances of this property
  14914. in the current file."
  14915. (interactive
  14916. (let* ((prop (completing-read
  14917. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14918. (cur (org-entry-get nil prop))
  14919. (allowed (org-property-get-allowed-values nil prop 'table))
  14920. (existing (mapcar 'list (org-property-values prop)))
  14921. (val (if allowed
  14922. (completing-read "Value: " allowed nil 'req-match)
  14923. (completing-read
  14924. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14925. (concat "[" cur "]") "")
  14926. ": ")
  14927. existing nil nil "" nil cur))))
  14928. (list prop (if (equal val "") cur val))))
  14929. (unless (equal (org-entry-get nil property) value)
  14930. (org-entry-put nil property value)))
  14931. (defun org-delete-property (property)
  14932. "In the current entry, delete PROPERTY."
  14933. (interactive
  14934. (let* ((prop (completing-read
  14935. "Property: " (org-entry-properties nil 'standard))))
  14936. (list prop)))
  14937. (message "Property %s %s" property
  14938. (if (org-entry-delete nil property)
  14939. "deleted"
  14940. "was not present in the entry")))
  14941. (defun org-delete-property-globally (property)
  14942. "Remove PROPERTY globally, from all entries."
  14943. (interactive
  14944. (let* ((prop (completing-read
  14945. "Globally remove property: "
  14946. (mapcar 'list (org-buffer-property-keys)))))
  14947. (list prop)))
  14948. (save-excursion
  14949. (save-restriction
  14950. (widen)
  14951. (goto-char (point-min))
  14952. (let ((cnt 0))
  14953. (while (re-search-forward
  14954. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14955. nil t)
  14956. (setq cnt (1+ cnt))
  14957. (replace-match ""))
  14958. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14959. (defvar org-columns-current-fmt-compiled) ; defined below
  14960. (defun org-compute-property-at-point ()
  14961. "Compute the property at point.
  14962. This looks for an enclosing column format, extracts the operator and
  14963. then applies it to the proerty in the column format's scope."
  14964. (interactive)
  14965. (unless (org-at-property-p)
  14966. (error "Not at a property"))
  14967. (let ((prop (org-match-string-no-properties 2)))
  14968. (org-columns-get-format-and-top-level)
  14969. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14970. (error "No operator defined for property %s" prop))
  14971. (org-columns-compute prop)))
  14972. (defun org-property-get-allowed-values (pom property &optional table)
  14973. "Get allowed values for the property PROPERTY.
  14974. When TABLE is non-nil, return an alist that can directly be used for
  14975. completion."
  14976. (let (vals)
  14977. (cond
  14978. ((equal property "TODO")
  14979. (setq vals (org-with-point-at pom
  14980. (append org-todo-keywords-1 '("")))))
  14981. ((equal property "PRIORITY")
  14982. (let ((n org-lowest-priority))
  14983. (while (>= n org-highest-priority)
  14984. (push (char-to-string n) vals)
  14985. (setq n (1- n)))))
  14986. ((member property org-special-properties))
  14987. (t
  14988. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14989. (when (and vals (string-match "\\S-" vals))
  14990. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14991. (setq vals (mapcar (lambda (x)
  14992. (cond ((stringp x) x)
  14993. ((numberp x) (number-to-string x))
  14994. ((symbolp x) (symbol-name x))
  14995. (t "???")))
  14996. vals)))))
  14997. (if table (mapcar 'list vals) vals)))
  14998. (defun org-property-previous-allowed-value (&optional previous)
  14999. "Switch to the next allowed value for this property."
  15000. (interactive)
  15001. (org-property-next-allowed-value t))
  15002. (defun org-property-next-allowed-value (&optional previous)
  15003. "Switch to the next allowed value for this property."
  15004. (interactive)
  15005. (unless (org-at-property-p)
  15006. (error "Not at a property"))
  15007. (let* ((key (match-string 2))
  15008. (value (match-string 3))
  15009. (allowed (or (org-property-get-allowed-values (point) key)
  15010. (and (member value '("[ ]" "[-]" "[X]"))
  15011. '("[ ]" "[X]"))))
  15012. nval)
  15013. (unless allowed
  15014. (error "Allowed values for this property have not been defined"))
  15015. (if previous (setq allowed (reverse allowed)))
  15016. (if (member value allowed)
  15017. (setq nval (car (cdr (member value allowed)))))
  15018. (setq nval (or nval (car allowed)))
  15019. (if (equal nval value)
  15020. (error "Only one allowed value for this property"))
  15021. (org-at-property-p)
  15022. (replace-match (concat " :" key ": " nval) t t)
  15023. (org-indent-line-function)
  15024. (beginning-of-line 1)
  15025. (skip-chars-forward " \t")))
  15026. (defun org-find-entry-with-id (ident)
  15027. "Locate the entry that contains the ID property with exact value IDENT.
  15028. IDENT can be a string, a symbol or a number, this function will search for
  15029. the string representation of it.
  15030. Return the position where this entry starts, or nil if there is no such entry."
  15031. (let ((id (cond
  15032. ((stringp ident) ident)
  15033. ((symbol-name ident) (symbol-name ident))
  15034. ((numberp ident) (number-to-string ident))
  15035. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15036. (case-fold-search nil))
  15037. (save-excursion
  15038. (save-restriction
  15039. (goto-char (point-min))
  15040. (when (re-search-forward
  15041. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15042. nil t)
  15043. (org-back-to-heading)
  15044. (point))))))
  15045. ;;; Column View
  15046. (defvar org-columns-overlays nil
  15047. "Holds the list of current column overlays.")
  15048. (defvar org-columns-current-fmt nil
  15049. "Local variable, holds the currently active column format.")
  15050. (defvar org-columns-current-fmt-compiled nil
  15051. "Local variable, holds the currently active column format.
  15052. This is the compiled version of the format.")
  15053. (defvar org-columns-current-widths nil
  15054. "Loval variable, holds the currently widths of fields.")
  15055. (defvar org-columns-current-maxwidths nil
  15056. "Loval variable, holds the currently active maximum column widths.")
  15057. (defvar org-columns-begin-marker (make-marker)
  15058. "Points to the position where last a column creation command was called.")
  15059. (defvar org-columns-top-level-marker (make-marker)
  15060. "Points to the position where current columns region starts.")
  15061. (defvar org-columns-map (make-sparse-keymap)
  15062. "The keymap valid in column display.")
  15063. (defun org-columns-content ()
  15064. "Switch to contents view while in columns view."
  15065. (interactive)
  15066. (org-overview)
  15067. (org-content))
  15068. (org-defkey org-columns-map "c" 'org-columns-content)
  15069. (org-defkey org-columns-map "o" 'org-overview)
  15070. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15071. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15072. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15073. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15074. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15075. (org-defkey org-columns-map "q" 'org-columns-quit)
  15076. (org-defkey org-columns-map "r" 'org-columns-redo)
  15077. (org-defkey org-columns-map "g" 'org-columns-redo)
  15078. (org-defkey org-columns-map [left] 'backward-char)
  15079. (org-defkey org-columns-map "\M-b" 'backward-char)
  15080. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15081. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15082. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15083. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15084. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15085. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15086. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15087. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15088. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15089. (org-defkey org-columns-map ">" 'org-columns-widen)
  15090. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15091. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15092. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15093. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15094. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15095. '("Column"
  15096. ["Edit property" org-columns-edit-value t]
  15097. ["Next allowed value" org-columns-next-allowed-value t]
  15098. ["Previous allowed value" org-columns-previous-allowed-value t]
  15099. ["Show full value" org-columns-show-value t]
  15100. ["Edit allowed values" org-columns-edit-allowed t]
  15101. "--"
  15102. ["Edit column attributes" org-columns-edit-attributes t]
  15103. ["Increase column width" org-columns-widen t]
  15104. ["Decrease column width" org-columns-narrow t]
  15105. "--"
  15106. ["Move column right" org-columns-move-right t]
  15107. ["Move column left" org-columns-move-left t]
  15108. ["Add column" org-columns-new t]
  15109. ["Delete column" org-columns-delete t]
  15110. "--"
  15111. ["CONTENTS" org-columns-content t]
  15112. ["OVERVIEW" org-overview t]
  15113. ["Refresh columns display" org-columns-redo t]
  15114. "--"
  15115. ["Open link" org-columns-open-link t]
  15116. "--"
  15117. ["Quit" org-columns-quit t]))
  15118. (defun org-columns-new-overlay (beg end &optional string face)
  15119. "Create a new column overlay and add it to the list."
  15120. (let ((ov (org-make-overlay beg end)))
  15121. (org-overlay-put ov 'face (or face 'secondary-selection))
  15122. (org-overlay-display ov string face)
  15123. (push ov org-columns-overlays)
  15124. ov))
  15125. (defun org-columns-display-here (&optional props)
  15126. "Overlay the current line with column display."
  15127. (interactive)
  15128. (let* ((fmt org-columns-current-fmt-compiled)
  15129. (beg (point-at-bol))
  15130. (level-face (save-excursion
  15131. (beginning-of-line 1)
  15132. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15133. (org-get-level-face 2))))
  15134. (color (list :foreground
  15135. (face-attribute (or level-face 'default) :foreground)))
  15136. props pom property ass width f string ov column val modval)
  15137. ;; Check if the entry is in another buffer.
  15138. (unless props
  15139. (if (eq major-mode 'org-agenda-mode)
  15140. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15141. (get-text-property (point) 'org-marker))
  15142. props (if pom (org-entry-properties pom) nil))
  15143. (setq props (org-entry-properties nil))))
  15144. ;; Walk the format
  15145. (while (setq column (pop fmt))
  15146. (setq property (car column)
  15147. ass (if (equal property "ITEM")
  15148. (cons "ITEM"
  15149. (save-match-data
  15150. (org-no-properties
  15151. (org-remove-tabs
  15152. (buffer-substring-no-properties
  15153. (point-at-bol) (point-at-eol))))))
  15154. (assoc property props))
  15155. width (or (cdr (assoc property org-columns-current-maxwidths))
  15156. (nth 2 column)
  15157. (length property))
  15158. f (format "%%-%d.%ds | " width width)
  15159. val (or (cdr ass) "")
  15160. modval (if (equal property "ITEM")
  15161. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15162. string (format f (or modval val)))
  15163. ;; Create the overlay
  15164. (org-unmodified
  15165. (setq ov (org-columns-new-overlay
  15166. beg (setq beg (1+ beg)) string
  15167. (list color 'org-column)))
  15168. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15169. (org-overlay-put ov 'keymap org-columns-map)
  15170. (org-overlay-put ov 'org-columns-key property)
  15171. (org-overlay-put ov 'org-columns-value (cdr ass))
  15172. (org-overlay-put ov 'org-columns-value-modified modval)
  15173. (org-overlay-put ov 'org-columns-pom pom)
  15174. (org-overlay-put ov 'org-columns-format f))
  15175. (if (or (not (char-after beg))
  15176. (equal (char-after beg) ?\n))
  15177. (let ((inhibit-read-only t))
  15178. (save-excursion
  15179. (goto-char beg)
  15180. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15181. ;; Make the rest of the line disappear.
  15182. (org-unmodified
  15183. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15184. (org-overlay-put ov 'invisible t)
  15185. (org-overlay-put ov 'keymap org-columns-map)
  15186. (org-overlay-put ov 'intangible t)
  15187. (push ov org-columns-overlays)
  15188. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15189. (org-overlay-put ov 'keymap org-columns-map)
  15190. (push ov org-columns-overlays)
  15191. (let ((inhibit-read-only t))
  15192. (put-text-property (max (point-min) (1- (point-at-bol)))
  15193. (min (point-max) (1+ (point-at-eol)))
  15194. 'read-only "Type `e' to edit property")))))
  15195. (defvar org-previous-header-line-format nil
  15196. "The header line format before column view was turned on.")
  15197. (defvar org-columns-inhibit-recalculation nil
  15198. "Inhibit recomputing of columns on column view startup.")
  15199. (defvar header-line-format)
  15200. (defun org-columns-display-here-title ()
  15201. "Overlay the newline before the current line with the table title."
  15202. (interactive)
  15203. (let ((fmt org-columns-current-fmt-compiled)
  15204. string (title "")
  15205. property width f column str widths)
  15206. (while (setq column (pop fmt))
  15207. (setq property (car column)
  15208. str (or (nth 1 column) property)
  15209. width (or (cdr (assoc property org-columns-current-maxwidths))
  15210. (nth 2 column)
  15211. (length str))
  15212. widths (push width widths)
  15213. f (format "%%-%d.%ds | " width width)
  15214. string (format f str)
  15215. title (concat title string)))
  15216. (setq title (concat
  15217. (org-add-props " " nil 'display '(space :align-to 0))
  15218. (org-add-props title nil 'face '(:weight bold :underline t))))
  15219. (org-set-local 'org-previous-header-line-format header-line-format)
  15220. (org-set-local 'org-columns-current-widths (nreverse widths))
  15221. (setq header-line-format title)))
  15222. (defun org-columns-remove-overlays ()
  15223. "Remove all currently active column overlays."
  15224. (interactive)
  15225. (when (marker-buffer org-columns-begin-marker)
  15226. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15227. (when (local-variable-p 'org-previous-header-line-format)
  15228. (setq header-line-format org-previous-header-line-format)
  15229. (kill-local-variable 'org-previous-header-line-format))
  15230. (move-marker org-columns-begin-marker nil)
  15231. (move-marker org-columns-top-level-marker nil)
  15232. (org-unmodified
  15233. (mapc 'org-delete-overlay org-columns-overlays)
  15234. (setq org-columns-overlays nil)
  15235. (let ((inhibit-read-only t))
  15236. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15237. (defun org-columns-cleanup-item (item fmt)
  15238. "Remove from ITEM what is a column in the format FMT."
  15239. (if (not org-complex-heading-regexp)
  15240. item
  15241. (when (string-match org-complex-heading-regexp item)
  15242. (concat
  15243. (org-add-props (concat (match-string 1 item) " ") nil
  15244. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15245. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15246. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15247. " " (match-string 4 item)
  15248. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15249. (defun org-columns-show-value ()
  15250. "Show the full value of the property."
  15251. (interactive)
  15252. (let ((value (get-char-property (point) 'org-columns-value)))
  15253. (message "Value is: %s" (or value ""))))
  15254. (defun org-columns-quit ()
  15255. "Remove the column overlays and in this way exit column editing."
  15256. (interactive)
  15257. (org-unmodified
  15258. (org-columns-remove-overlays)
  15259. (let ((inhibit-read-only t))
  15260. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15261. (when (eq major-mode 'org-agenda-mode)
  15262. (message
  15263. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15264. (defun org-columns-check-computed ()
  15265. "Check if this column value is computed.
  15266. If yes, throw an error indicating that changing it does not make sense."
  15267. (let ((val (get-char-property (point) 'org-columns-value)))
  15268. (when (and (stringp val)
  15269. (get-char-property 0 'org-computed val))
  15270. (error "This value is computed from the entry's children"))))
  15271. (defun org-columns-todo (&optional arg)
  15272. "Change the TODO state during column view."
  15273. (interactive "P")
  15274. (org-columns-edit-value "TODO"))
  15275. (defun org-columns-set-tags-or-toggle (&optional arg)
  15276. "Toggle checkbox at point, or set tags for current headline."
  15277. (interactive "P")
  15278. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15279. (get-char-property (point) 'org-columns-value))
  15280. (org-columns-next-allowed-value)
  15281. (org-columns-edit-value "TAGS")))
  15282. (defun org-columns-edit-value (&optional key)
  15283. "Edit the value of the property at point in column view.
  15284. Where possible, use the standard interface for changing this line."
  15285. (interactive)
  15286. (org-columns-check-computed)
  15287. (let* ((external-key key)
  15288. (col (current-column))
  15289. (key (or key (get-char-property (point) 'org-columns-key)))
  15290. (value (get-char-property (point) 'org-columns-value))
  15291. (bol (point-at-bol)) (eol (point-at-eol))
  15292. (pom (or (get-text-property bol 'org-hd-marker)
  15293. (point))) ; keep despite of compiler waring
  15294. (line-overlays
  15295. (delq nil (mapcar (lambda (x)
  15296. (and (eq (overlay-buffer x) (current-buffer))
  15297. (>= (overlay-start x) bol)
  15298. (<= (overlay-start x) eol)
  15299. x))
  15300. org-columns-overlays)))
  15301. nval eval allowed)
  15302. (cond
  15303. ((equal key "CLOCKSUM")
  15304. (error "This special column cannot be edited"))
  15305. ((equal key "ITEM")
  15306. (setq eval '(org-with-point-at pom
  15307. (org-edit-headline))))
  15308. ((equal key "TODO")
  15309. (setq eval '(org-with-point-at pom
  15310. (let ((current-prefix-arg
  15311. (if external-key current-prefix-arg '(4))))
  15312. (call-interactively 'org-todo)))))
  15313. ((equal key "PRIORITY")
  15314. (setq eval '(org-with-point-at pom
  15315. (call-interactively 'org-priority))))
  15316. ((equal key "TAGS")
  15317. (setq eval '(org-with-point-at pom
  15318. (let ((org-fast-tag-selection-single-key
  15319. (if (eq org-fast-tag-selection-single-key 'expert)
  15320. t org-fast-tag-selection-single-key)))
  15321. (call-interactively 'org-set-tags)))))
  15322. ((equal key "DEADLINE")
  15323. (setq eval '(org-with-point-at pom
  15324. (call-interactively 'org-deadline))))
  15325. ((equal key "SCHEDULED")
  15326. (setq eval '(org-with-point-at pom
  15327. (call-interactively 'org-schedule))))
  15328. (t
  15329. (setq allowed (org-property-get-allowed-values pom key 'table))
  15330. (if allowed
  15331. (setq nval (completing-read "Value: " allowed nil t))
  15332. (setq nval (read-string "Edit: " value)))
  15333. (setq nval (org-trim nval))
  15334. (when (not (equal nval value))
  15335. (setq eval '(org-entry-put pom key nval)))))
  15336. (when eval
  15337. (let ((inhibit-read-only t))
  15338. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15339. (unwind-protect
  15340. (progn
  15341. (setq org-columns-overlays
  15342. (org-delete-all line-overlays org-columns-overlays))
  15343. (mapc 'org-delete-overlay line-overlays)
  15344. (org-columns-eval eval))
  15345. (org-columns-display-here))))
  15346. (move-to-column col)
  15347. (if (and (org-mode-p)
  15348. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15349. (org-columns-update key))))
  15350. (defun org-edit-headline () ; FIXME: this is not columns specific
  15351. "Edit the current headline, the part without TODO keyword, TAGS."
  15352. (org-back-to-heading)
  15353. (when (looking-at org-todo-line-regexp)
  15354. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15355. (txt (match-string 3))
  15356. (post "")
  15357. txt2)
  15358. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15359. (setq post (match-string 0 txt)
  15360. txt (substring txt 0 (match-beginning 0))))
  15361. (setq txt2 (read-string "Edit: " txt))
  15362. (when (not (equal txt txt2))
  15363. (beginning-of-line 1)
  15364. (insert pre txt2 post)
  15365. (delete-region (point) (point-at-eol))
  15366. (org-set-tags nil t)))))
  15367. (defun org-columns-edit-allowed ()
  15368. "Edit the list of allowed values for the current property."
  15369. (interactive)
  15370. (let* ((key (get-char-property (point) 'org-columns-key))
  15371. (key1 (concat key "_ALL"))
  15372. (allowed (org-entry-get (point) key1 t))
  15373. nval)
  15374. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15375. (setq nval (read-string "Allowed: " allowed))
  15376. (org-entry-put
  15377. (cond ((marker-position org-entry-property-inherited-from)
  15378. org-entry-property-inherited-from)
  15379. ((marker-position org-columns-top-level-marker)
  15380. org-columns-top-level-marker))
  15381. key1 nval)))
  15382. (defmacro org-no-warnings (&rest body)
  15383. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15384. (defun org-columns-eval (form)
  15385. (let (hidep)
  15386. (save-excursion
  15387. (beginning-of-line 1)
  15388. ;; `next-line' is needed here, because it skips invisible line.
  15389. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15390. (setq hidep (org-on-heading-p 1)))
  15391. (eval form)
  15392. (and hidep (hide-entry))))
  15393. (defun org-columns-previous-allowed-value ()
  15394. "Switch to the previous allowed value for this column."
  15395. (interactive)
  15396. (org-columns-next-allowed-value t))
  15397. (defun org-columns-next-allowed-value (&optional previous)
  15398. "Switch to the next allowed value for this column."
  15399. (interactive)
  15400. (org-columns-check-computed)
  15401. (let* ((col (current-column))
  15402. (key (get-char-property (point) 'org-columns-key))
  15403. (value (get-char-property (point) 'org-columns-value))
  15404. (bol (point-at-bol)) (eol (point-at-eol))
  15405. (pom (or (get-text-property bol 'org-hd-marker)
  15406. (point))) ; keep despite of compiler waring
  15407. (line-overlays
  15408. (delq nil (mapcar (lambda (x)
  15409. (and (eq (overlay-buffer x) (current-buffer))
  15410. (>= (overlay-start x) bol)
  15411. (<= (overlay-start x) eol)
  15412. x))
  15413. org-columns-overlays)))
  15414. (allowed (or (org-property-get-allowed-values pom key)
  15415. (and (memq
  15416. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15417. '(checkbox checkbox-n-of-m checkbox-percent))
  15418. '("[ ]" "[X]"))))
  15419. nval)
  15420. (when (equal key "ITEM")
  15421. (error "Cannot edit item headline from here"))
  15422. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15423. (error "Allowed values for this property have not been defined"))
  15424. (if (member key '("SCHEDULED" "DEADLINE"))
  15425. (setq nval (if previous 'earlier 'later))
  15426. (if previous (setq allowed (reverse allowed)))
  15427. (if (member value allowed)
  15428. (setq nval (car (cdr (member value allowed)))))
  15429. (setq nval (or nval (car allowed)))
  15430. (if (equal nval value)
  15431. (error "Only one allowed value for this property")))
  15432. (let ((inhibit-read-only t))
  15433. (remove-text-properties (1- bol) eol '(read-only t))
  15434. (unwind-protect
  15435. (progn
  15436. (setq org-columns-overlays
  15437. (org-delete-all line-overlays org-columns-overlays))
  15438. (mapc 'org-delete-overlay line-overlays)
  15439. (org-columns-eval '(org-entry-put pom key nval)))
  15440. (org-columns-display-here)))
  15441. (move-to-column col)
  15442. (if (and (org-mode-p)
  15443. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15444. (org-columns-update key))))
  15445. (defun org-verify-version (task)
  15446. (cond
  15447. ((eq task 'columns)
  15448. (if (or (featurep 'xemacs)
  15449. (< emacs-major-version 22))
  15450. (error "Emacs 22 is required for the columns feature")))))
  15451. (defun org-columns-open-link (&optional arg)
  15452. (interactive "P")
  15453. (let ((value (get-char-property (point) 'org-columns-value)))
  15454. (org-open-link-from-string value arg)))
  15455. (defun org-open-link-from-string (s &optional arg)
  15456. "Open a link in the string S, as if it was in Org-mode."
  15457. (interactive)
  15458. (with-temp-buffer
  15459. (let ((org-inhibit-startup t))
  15460. (org-mode)
  15461. (insert s)
  15462. (goto-char (point-min))
  15463. (org-open-at-point arg))))
  15464. (defun org-columns-get-format-and-top-level ()
  15465. (let (fmt)
  15466. (when (condition-case nil (org-back-to-heading) (error nil))
  15467. (move-marker org-entry-property-inherited-from nil)
  15468. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15469. (setq fmt (or fmt org-columns-default-format))
  15470. (org-set-local 'org-columns-current-fmt fmt)
  15471. (org-columns-compile-format fmt)
  15472. (if (marker-position org-entry-property-inherited-from)
  15473. (move-marker org-columns-top-level-marker
  15474. org-entry-property-inherited-from)
  15475. (move-marker org-columns-top-level-marker (point)))
  15476. fmt))
  15477. (defun org-columns ()
  15478. "Turn on column view on an org-mode file."
  15479. (interactive)
  15480. (org-verify-version 'columns)
  15481. (org-columns-remove-overlays)
  15482. (move-marker org-columns-begin-marker (point))
  15483. (let (beg end fmt cache maxwidths)
  15484. (setq fmt (org-columns-get-format-and-top-level))
  15485. (save-excursion
  15486. (goto-char org-columns-top-level-marker)
  15487. (setq beg (point))
  15488. (unless org-columns-inhibit-recalculation
  15489. (org-columns-compute-all))
  15490. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15491. (point-max)))
  15492. ;; Get and cache the properties
  15493. (goto-char beg)
  15494. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15495. (save-excursion
  15496. (save-restriction
  15497. (narrow-to-region beg end)
  15498. (org-clock-sum))))
  15499. (while (re-search-forward (concat "^" outline-regexp) end t)
  15500. (push (cons (org-current-line) (org-entry-properties)) cache))
  15501. (when cache
  15502. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15503. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15504. (org-columns-display-here-title)
  15505. (mapc (lambda (x)
  15506. (goto-line (car x))
  15507. (org-columns-display-here (cdr x)))
  15508. cache)))))
  15509. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15510. "Insert a new column, to the leeft o the current column."
  15511. (interactive)
  15512. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15513. cell)
  15514. (setq prop (completing-read
  15515. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15516. nil nil prop))
  15517. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15518. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15519. (if (string-match "\\S-" width)
  15520. (setq width (string-to-number width))
  15521. (setq width nil))
  15522. (setq fmt (completing-read "Summary [none]: "
  15523. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15524. nil t))
  15525. (if (string-match "\\S-" fmt)
  15526. (setq fmt (intern fmt))
  15527. (setq fmt nil))
  15528. (if (eq fmt 'none) (setq fmt nil))
  15529. (if editp
  15530. (progn
  15531. (setcar editp prop)
  15532. (setcdr editp (list title width nil fmt)))
  15533. (setq cell (nthcdr (1- (current-column))
  15534. org-columns-current-fmt-compiled))
  15535. (setcdr cell (cons (list prop title width nil fmt)
  15536. (cdr cell))))
  15537. (org-columns-store-format)
  15538. (org-columns-redo)))
  15539. (defun org-columns-delete ()
  15540. "Delete the column at point from columns view."
  15541. (interactive)
  15542. (let* ((n (current-column))
  15543. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15544. (when (y-or-n-p
  15545. (format "Are you sure you want to remove column \"%s\"? " title))
  15546. (setq org-columns-current-fmt-compiled
  15547. (delq (nth n org-columns-current-fmt-compiled)
  15548. org-columns-current-fmt-compiled))
  15549. (org-columns-store-format)
  15550. (org-columns-redo)
  15551. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15552. (backward-char 1)))))
  15553. (defun org-columns-edit-attributes ()
  15554. "Edit the attributes of the current column."
  15555. (interactive)
  15556. (let* ((n (current-column))
  15557. (info (nth n org-columns-current-fmt-compiled)))
  15558. (apply 'org-columns-new info)))
  15559. (defun org-columns-widen (arg)
  15560. "Make the column wider by ARG characters."
  15561. (interactive "p")
  15562. (let* ((n (current-column))
  15563. (entry (nth n org-columns-current-fmt-compiled))
  15564. (width (or (nth 2 entry)
  15565. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15566. (setq width (max 1 (+ width arg)))
  15567. (setcar (nthcdr 2 entry) width)
  15568. (org-columns-store-format)
  15569. (org-columns-redo)))
  15570. (defun org-columns-narrow (arg)
  15571. "Make the column nrrower by ARG characters."
  15572. (interactive "p")
  15573. (org-columns-widen (- arg)))
  15574. (defun org-columns-move-right ()
  15575. "Swap this column with the one to the right."
  15576. (interactive)
  15577. (let* ((n (current-column))
  15578. (cell (nthcdr n org-columns-current-fmt-compiled))
  15579. e)
  15580. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15581. (error "Cannot shift this column further to the right"))
  15582. (setq e (car cell))
  15583. (setcar cell (car (cdr cell)))
  15584. (setcdr cell (cons e (cdr (cdr cell))))
  15585. (org-columns-store-format)
  15586. (org-columns-redo)
  15587. (forward-char 1)))
  15588. (defun org-columns-move-left ()
  15589. "Swap this column with the one to the left."
  15590. (interactive)
  15591. (let* ((n (current-column)))
  15592. (when (= n 0)
  15593. (error "Cannot shift this column further to the left"))
  15594. (backward-char 1)
  15595. (org-columns-move-right)
  15596. (backward-char 1)))
  15597. (defun org-columns-store-format ()
  15598. "Store the text version of the current columns format in appropriate place.
  15599. This is either in the COLUMNS property of the node starting the current column
  15600. display, or in the #+COLUMNS line of the current buffer."
  15601. (let (fmt (cnt 0))
  15602. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15603. (org-set-local 'org-columns-current-fmt fmt)
  15604. (if (marker-position org-columns-top-level-marker)
  15605. (save-excursion
  15606. (goto-char org-columns-top-level-marker)
  15607. (if (and (org-at-heading-p)
  15608. (org-entry-get nil "COLUMNS"))
  15609. (org-entry-put nil "COLUMNS" fmt)
  15610. (goto-char (point-min))
  15611. ;; Overwrite all #+COLUMNS lines....
  15612. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15613. (setq cnt (1+ cnt))
  15614. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15615. (unless (> cnt 0)
  15616. (goto-char (point-min))
  15617. (or (org-on-heading-p t) (outline-next-heading))
  15618. (let ((inhibit-read-only t))
  15619. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15620. (org-set-local 'org-columns-default-format fmt))))))
  15621. (defvar org-overriding-columns-format nil
  15622. "When set, overrides any other definition.")
  15623. (defvar org-agenda-view-columns-initially nil
  15624. "When set, switch to columns view immediately after creating the agenda.")
  15625. (defun org-agenda-columns ()
  15626. "Turn on column view in the agenda."
  15627. (interactive)
  15628. (org-verify-version 'columns)
  15629. (org-columns-remove-overlays)
  15630. (move-marker org-columns-begin-marker (point))
  15631. (let (fmt cache maxwidths m)
  15632. (cond
  15633. ((and (local-variable-p 'org-overriding-columns-format)
  15634. org-overriding-columns-format)
  15635. (setq fmt org-overriding-columns-format))
  15636. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15637. (setq fmt (org-entry-get m "COLUMNS" t)))
  15638. ((and (boundp 'org-columns-current-fmt)
  15639. (local-variable-p 'org-columns-current-fmt)
  15640. org-columns-current-fmt)
  15641. (setq fmt org-columns-current-fmt))
  15642. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15643. (setq m (get-text-property m 'org-hd-marker))
  15644. (setq fmt (org-entry-get m "COLUMNS" t))))
  15645. (setq fmt (or fmt org-columns-default-format))
  15646. (org-set-local 'org-columns-current-fmt fmt)
  15647. (org-columns-compile-format fmt)
  15648. (save-excursion
  15649. ;; Get and cache the properties
  15650. (goto-char (point-min))
  15651. (while (not (eobp))
  15652. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15653. (get-text-property (point) 'org-marker)))
  15654. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15655. (beginning-of-line 2))
  15656. (when cache
  15657. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15658. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15659. (org-columns-display-here-title)
  15660. (mapc (lambda (x)
  15661. (goto-line (car x))
  15662. (org-columns-display-here (cdr x)))
  15663. cache)))))
  15664. (defun org-columns-get-autowidth-alist (s cache)
  15665. "Derive the maximum column widths from the format and the cache."
  15666. (let ((start 0) rtn)
  15667. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15668. (push (cons (match-string 1 s) 1) rtn)
  15669. (setq start (match-end 0)))
  15670. (mapc (lambda (x)
  15671. (setcdr x (apply 'max
  15672. (mapcar
  15673. (lambda (y)
  15674. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15675. cache))))
  15676. rtn)
  15677. rtn))
  15678. (defun org-columns-compute-all ()
  15679. "Compute all columns that have operators defined."
  15680. (org-unmodified
  15681. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15682. (let ((columns org-columns-current-fmt-compiled) col)
  15683. (while (setq col (pop columns))
  15684. (when (nth 3 col)
  15685. (save-excursion
  15686. (org-columns-compute (car col)))))))
  15687. (defun org-columns-update (property)
  15688. "Recompute PROPERTY, and update the columns display for it."
  15689. (org-columns-compute property)
  15690. (let (fmt val pos)
  15691. (save-excursion
  15692. (mapc (lambda (ov)
  15693. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15694. (setq pos (org-overlay-start ov))
  15695. (goto-char pos)
  15696. (when (setq val (cdr (assoc property
  15697. (get-text-property
  15698. (point-at-bol) 'org-summaries))))
  15699. (setq fmt (org-overlay-get ov 'org-columns-format))
  15700. (org-overlay-put ov 'org-columns-value val)
  15701. (org-overlay-put ov 'display (format fmt val)))))
  15702. org-columns-overlays))))
  15703. (defun org-columns-compute (property)
  15704. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15705. (interactive)
  15706. (let* ((re (concat "^" outline-regexp))
  15707. (lmax 30) ; Does anyone use deeper levels???
  15708. (lsum (make-vector lmax 0))
  15709. (lflag (make-vector lmax nil))
  15710. (level 0)
  15711. (ass (assoc property org-columns-current-fmt-compiled))
  15712. (format (nth 4 ass))
  15713. (printf (nth 5 ass))
  15714. (beg org-columns-top-level-marker)
  15715. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15716. (save-excursion
  15717. ;; Find the region to compute
  15718. (goto-char beg)
  15719. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15720. (goto-char end)
  15721. ;; Walk the tree from the back and do the computations
  15722. (while (re-search-backward re beg t)
  15723. (setq sumpos (match-beginning 0)
  15724. last-level level
  15725. level (org-outline-level)
  15726. val (org-entry-get nil property)
  15727. valflag (and val (string-match "\\S-" val)))
  15728. (cond
  15729. ((< level last-level)
  15730. ;; put the sum of lower levels here as a property
  15731. (setq sum (aref lsum last-level) ; current sum
  15732. flag (aref lflag last-level) ; any valid entries from children?
  15733. str (org-column-number-to-string sum format printf)
  15734. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15735. useval (if flag str1 (if valflag val ""))
  15736. sum-alist (get-text-property sumpos 'org-summaries))
  15737. (if (assoc property sum-alist)
  15738. (setcdr (assoc property sum-alist) useval)
  15739. (push (cons property useval) sum-alist)
  15740. (org-unmodified
  15741. (add-text-properties sumpos (1+ sumpos)
  15742. (list 'org-summaries sum-alist))))
  15743. (when val
  15744. (org-entry-put nil property (if flag str val)))
  15745. ;; add current to current level accumulator
  15746. (when (or flag valflag)
  15747. (aset lsum level (+ (aref lsum level)
  15748. (if flag sum (org-column-string-to-number
  15749. (if flag str val) format))))
  15750. (aset lflag level t))
  15751. ;; clear accumulators for deeper levels
  15752. (loop for l from (1+ level) to (1- lmax) do
  15753. (aset lsum l 0)
  15754. (aset lflag l nil)))
  15755. ((>= level last-level)
  15756. ;; add what we have here to the accumulator for this level
  15757. (aset lsum level (+ (aref lsum level)
  15758. (org-column-string-to-number (or val "0") format)))
  15759. (and valflag (aset lflag level t)))
  15760. (t (error "This should not happen")))))))
  15761. (defun org-columns-redo ()
  15762. "Construct the column display again."
  15763. (interactive)
  15764. (message "Recomputing columns...")
  15765. (save-excursion
  15766. (if (marker-position org-columns-begin-marker)
  15767. (goto-char org-columns-begin-marker))
  15768. (org-columns-remove-overlays)
  15769. (if (org-mode-p)
  15770. (call-interactively 'org-columns)
  15771. (call-interactively 'org-agenda-columns)))
  15772. (message "Recomputing columns...done"))
  15773. (defun org-columns-not-in-agenda ()
  15774. (if (eq major-mode 'org-agenda-mode)
  15775. (error "This command is only allowed in Org-mode buffers")))
  15776. (defun org-string-to-number (s)
  15777. "Convert string to number, and interpret hh:mm:ss."
  15778. (if (not (string-match ":" s))
  15779. (string-to-number s)
  15780. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15781. (while l
  15782. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15783. sum)))
  15784. (defun org-column-number-to-string (n fmt &optional printf)
  15785. "Convert a computed column number to a string value, according to FMT."
  15786. (cond
  15787. ((eq fmt 'add_times)
  15788. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15789. (format "%d:%02d" h m)))
  15790. ((eq fmt 'checkbox)
  15791. (cond ((= n (floor n)) "[X]")
  15792. ((> n 1.) "[-]")
  15793. (t "[ ]")))
  15794. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15795. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15796. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15797. (printf (format printf n))
  15798. ((eq fmt 'currency)
  15799. (format "%.2f" n))
  15800. (t (number-to-string n))))
  15801. (defun org-nofm-to-completion (n m &optional percent)
  15802. (if (not percent)
  15803. (format "[%d/%d]" n m)
  15804. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15805. (defun org-column-string-to-number (s fmt)
  15806. "Convert a column value to a number that can be used for column computing."
  15807. (cond
  15808. ((string-match ":" s)
  15809. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15810. (while l
  15811. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15812. sum))
  15813. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15814. (if (equal s "[X]") 1. 0.000001))
  15815. (t (string-to-number s))))
  15816. (defun org-columns-uncompile-format (cfmt)
  15817. "Turn the compiled columns format back into a string representation."
  15818. (let ((rtn "") e s prop title op width fmt printf)
  15819. (while (setq e (pop cfmt))
  15820. (setq prop (car e)
  15821. title (nth 1 e)
  15822. width (nth 2 e)
  15823. op (nth 3 e)
  15824. fmt (nth 4 e)
  15825. printf (nth 5 e))
  15826. (cond
  15827. ((eq fmt 'add_times) (setq op ":"))
  15828. ((eq fmt 'checkbox) (setq op "X"))
  15829. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15830. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15831. ((eq fmt 'add_numbers) (setq op "+"))
  15832. ((eq fmt 'currency) (setq op "$")))
  15833. (if (and op printf) (setq op (concat op ";" printf)))
  15834. (if (equal title prop) (setq title nil))
  15835. (setq s (concat "%" (if width (number-to-string width))
  15836. prop
  15837. (if title (concat "(" title ")"))
  15838. (if op (concat "{" op "}"))))
  15839. (setq rtn (concat rtn " " s)))
  15840. (org-trim rtn)))
  15841. (defun org-columns-compile-format (fmt)
  15842. "Turn a column format string into an alist of specifications.
  15843. The alist has one entry for each column in the format. The elements of
  15844. that list are:
  15845. property the property
  15846. title the title field for the columns
  15847. width the column width in characters, can be nil for automatic
  15848. operator the operator if any
  15849. format the output format for computed results, derived from operator
  15850. printf a printf format for computed values"
  15851. (let ((start 0) width prop title op f printf)
  15852. (setq org-columns-current-fmt-compiled nil)
  15853. (while (string-match
  15854. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15855. fmt start)
  15856. (setq start (match-end 0)
  15857. width (match-string 1 fmt)
  15858. prop (match-string 2 fmt)
  15859. title (or (match-string 3 fmt) prop)
  15860. op (match-string 4 fmt)
  15861. f nil
  15862. printf nil)
  15863. (if width (setq width (string-to-number width)))
  15864. (when (and op (string-match ";" op))
  15865. (setq printf (substring op (match-end 0))
  15866. op (substring op 0 (match-beginning 0))))
  15867. (cond
  15868. ((equal op "+") (setq f 'add_numbers))
  15869. ((equal op "$") (setq f 'currency))
  15870. ((equal op ":") (setq f 'add_times))
  15871. ((equal op "X") (setq f 'checkbox))
  15872. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15873. ((equal op "X%") (setq f 'checkbox-percent))
  15874. )
  15875. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15876. (setq org-columns-current-fmt-compiled
  15877. (nreverse org-columns-current-fmt-compiled))))
  15878. ;;; Dynamic block for Column view
  15879. (defun org-columns-capture-view ()
  15880. "Get the column view of the current buffer and return it as a list.
  15881. The list will contains the title row and all other rows. Each row is
  15882. a list of fields."
  15883. (save-excursion
  15884. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15885. (n (length title)) row tbl)
  15886. (goto-char (point-min))
  15887. (while (re-search-forward "^\\*+ " nil t)
  15888. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15889. (setq row nil)
  15890. (loop for i from 0 to (1- n) do
  15891. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15892. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15893. "")
  15894. row))
  15895. (setq row (nreverse row))
  15896. (push row tbl)))
  15897. (append (list title 'hline) (nreverse tbl)))))
  15898. (defun org-dblock-write:columnview (params)
  15899. "Write the column view table.
  15900. PARAMS is a property list of parameters:
  15901. :width enforce same column widths with <N> specifiers.
  15902. :id the :ID: property of the entry where the columns view
  15903. should be built, as a string. When `local', call locally.
  15904. When `global' call column view with the cursor at the beginning
  15905. of the buffer (usually this means that the whole buffer switches
  15906. to column view).
  15907. :hlines When t, insert a hline before each item. When a number, insert
  15908. a hline before each level <= that number.
  15909. :vlines When t, make each column a colgroup to enforce vertical lines."
  15910. (let ((pos (move-marker (make-marker) (point)))
  15911. (hlines (plist-get params :hlines))
  15912. (vlines (plist-get params :vlines))
  15913. tbl id idpos nfields tmp)
  15914. (save-excursion
  15915. (save-restriction
  15916. (when (setq id (plist-get params :id))
  15917. (cond ((not id) nil)
  15918. ((eq id 'global) (goto-char (point-min)))
  15919. ((eq id 'local) nil)
  15920. ((setq idpos (org-find-entry-with-id id))
  15921. (goto-char idpos))
  15922. (t (error "Cannot find entry with :ID: %s" id))))
  15923. (org-columns)
  15924. (setq tbl (org-columns-capture-view))
  15925. (setq nfields (length (car tbl)))
  15926. (org-columns-quit)))
  15927. (goto-char pos)
  15928. (move-marker pos nil)
  15929. (when tbl
  15930. (when (plist-get params :hlines)
  15931. (setq tmp nil)
  15932. (while tbl
  15933. (if (eq (car tbl) 'hline)
  15934. (push (pop tbl) tmp)
  15935. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15936. (if (and (not (eq (car tmp) 'hline))
  15937. (or (eq hlines t)
  15938. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15939. (push 'hline tmp)))
  15940. (push (pop tbl) tmp)))
  15941. (setq tbl (nreverse tmp)))
  15942. (when vlines
  15943. (setq tbl (mapcar (lambda (x)
  15944. (if (eq 'hline x) x (cons "" x)))
  15945. tbl))
  15946. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15947. (setq pos (point))
  15948. (insert (org-listtable-to-string tbl))
  15949. (when (plist-get params :width)
  15950. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15951. org-columns-current-widths "|")))
  15952. (goto-char pos)
  15953. (org-table-align))))
  15954. (defun org-listtable-to-string (tbl)
  15955. "Convert a listtable TBL to a string that contains the Org-mode table.
  15956. The table still need to be alligned. The resulting string has no leading
  15957. and tailing newline characters."
  15958. (mapconcat
  15959. (lambda (x)
  15960. (cond
  15961. ((listp x)
  15962. (concat "|" (mapconcat 'identity x "|") "|"))
  15963. ((eq x 'hline) "|-|")
  15964. (t (error "Garbage in listtable: %s" x))))
  15965. tbl "\n"))
  15966. (defun org-insert-columns-dblock ()
  15967. "Create a dynamic block capturing a column view table."
  15968. (interactive)
  15969. (let ((defaults '(:name "columnview" :hlines 1))
  15970. (id (completing-read
  15971. "Capture columns (local, global, entry with :ID: property) [local]: "
  15972. (append '(("global") ("local"))
  15973. (mapcar 'list (org-property-values "ID"))))))
  15974. (if (equal id "") (setq id 'local))
  15975. (if (equal id "global") (setq id 'global))
  15976. (setq defaults (append defaults (list :id id)))
  15977. (org-create-dblock defaults)
  15978. (org-update-dblock)))
  15979. ;;;; Timestamps
  15980. (defvar org-last-changed-timestamp nil)
  15981. (defvar org-time-was-given) ; dynamically scoped parameter
  15982. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15983. (defvar org-ts-what) ; dynamically scoped parameter
  15984. (defun org-time-stamp (arg)
  15985. "Prompt for a date/time and insert a time stamp.
  15986. If the user specifies a time like HH:MM, or if this command is called
  15987. with a prefix argument, the time stamp will contain date and time.
  15988. Otherwise, only the date will be included. All parts of a date not
  15989. specified by the user will be filled in from the current date/time.
  15990. So if you press just return without typing anything, the time stamp
  15991. will represent the current date/time. If there is already a timestamp
  15992. at the cursor, it will be modified."
  15993. (interactive "P")
  15994. (let* ((ts nil)
  15995. (default-time
  15996. ;; Default time is either today, or, when entering a range,
  15997. ;; the range start.
  15998. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15999. (save-excursion
  16000. (re-search-backward
  16001. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16002. (- (point) 20) t)))
  16003. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16004. (current-time)))
  16005. (default-input (and ts (org-get-compact-tod ts)))
  16006. org-time-was-given org-end-time-was-given time)
  16007. (cond
  16008. ((and (org-at-timestamp-p)
  16009. (eq last-command 'org-time-stamp)
  16010. (eq this-command 'org-time-stamp))
  16011. (insert "--")
  16012. (setq time (let ((this-command this-command))
  16013. (org-read-date arg 'totime nil nil default-time default-input)))
  16014. (org-insert-time-stamp time (or org-time-was-given arg)))
  16015. ((org-at-timestamp-p)
  16016. (setq time (let ((this-command this-command))
  16017. (org-read-date arg 'totime nil nil default-time default-input)))
  16018. (when (org-at-timestamp-p) ; just to get the match data
  16019. (replace-match "")
  16020. (setq org-last-changed-timestamp
  16021. (org-insert-time-stamp
  16022. time (or org-time-was-given arg)
  16023. nil nil nil (list org-end-time-was-given))))
  16024. (message "Timestamp updated"))
  16025. (t
  16026. (setq time (let ((this-command this-command))
  16027. (org-read-date arg 'totime nil nil default-time default-input)))
  16028. (org-insert-time-stamp time (or org-time-was-given arg)
  16029. nil nil nil (list org-end-time-was-given))))))
  16030. ;; FIXME: can we use this for something else????
  16031. ;; like computing time differences?????
  16032. (defun org-get-compact-tod (s)
  16033. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16034. (let* ((t1 (match-string 1 s))
  16035. (h1 (string-to-number (match-string 2 s)))
  16036. (m1 (string-to-number (match-string 3 s)))
  16037. (t2 (and (match-end 4) (match-string 5 s)))
  16038. (h2 (and t2 (string-to-number (match-string 6 s))))
  16039. (m2 (and t2 (string-to-number (match-string 7 s))))
  16040. dh dm)
  16041. (if (not t2)
  16042. t1
  16043. (setq dh (- h2 h1) dm (- m2 m1))
  16044. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16045. (concat t1 "+" (number-to-string dh)
  16046. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16047. (defun org-time-stamp-inactive (&optional arg)
  16048. "Insert an inactive time stamp.
  16049. An inactive time stamp is enclosed in square brackets instead of angle
  16050. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16051. does not link to the calendar and cannot be changed with the S-cursor keys.
  16052. So these are more for recording a certain time/date."
  16053. (interactive "P")
  16054. (let (org-time-was-given org-end-time-was-given time)
  16055. (setq time (org-read-date arg 'totime))
  16056. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16057. nil nil (list org-end-time-was-given))))
  16058. (defvar org-date-ovl (org-make-overlay 1 1))
  16059. (org-overlay-put org-date-ovl 'face 'org-warning)
  16060. (org-detach-overlay org-date-ovl)
  16061. (defvar org-ans1) ; dynamically scoped parameter
  16062. (defvar org-ans2) ; dynamically scoped parameter
  16063. (defvar org-plain-time-of-day-regexp) ; defined below
  16064. (defvar org-read-date-overlay nil)
  16065. (defvar org-dcst nil) ; dynamically scoped
  16066. (defun org-read-date (&optional with-time to-time from-string prompt
  16067. default-time default-input)
  16068. "Read a date, possibly a time, and make things smooth for the user.
  16069. The prompt will suggest to enter an ISO date, but you can also enter anything
  16070. which will at least partially be understood by `parse-time-string'.
  16071. Unrecognized parts of the date will default to the current day, month, year,
  16072. hour and minute. If this command is called to replace a timestamp at point,
  16073. of to enter the second timestamp of a range, the default time is taken from the
  16074. existing stamp. For example,
  16075. 3-2-5 --> 2003-02-05
  16076. feb 15 --> currentyear-02-15
  16077. sep 12 9 --> 2009-09-12
  16078. 12:45 --> today 12:45
  16079. 22 sept 0:34 --> currentyear-09-22 0:34
  16080. 12 --> currentyear-currentmonth-12
  16081. Fri --> nearest Friday (today or later)
  16082. etc.
  16083. Furthermore you can specify a relative date by giving, as the *first* thing
  16084. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16085. change in days weeks, months, years.
  16086. With a single plus or minus, the date is relative to today. With a double
  16087. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16088. +4d --> four days from today
  16089. +4 --> same as above
  16090. +2w --> two weeks from today
  16091. ++5 --> five days from default date
  16092. The function understands only English month and weekday abbreviations,
  16093. but this can be configured with the variables `parse-time-months' and
  16094. `parse-time-weekdays'.
  16095. While prompting, a calendar is popped up - you can also select the
  16096. date with the mouse (button 1). The calendar shows a period of three
  16097. months. To scroll it to other months, use the keys `>' and `<'.
  16098. If you don't like the calendar, turn it off with
  16099. \(setq org-read-date-popup-calendar nil)
  16100. With optional argument TO-TIME, the date will immediately be converted
  16101. to an internal time.
  16102. With an optional argument WITH-TIME, the prompt will suggest to also
  16103. insert a time. Note that when WITH-TIME is not set, you can still
  16104. enter a time, and this function will inform the calling routine about
  16105. this change. The calling routine may then choose to change the format
  16106. used to insert the time stamp into the buffer to include the time.
  16107. With optional argument FROM-STRING, read from this string instead from
  16108. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16109. the time/date that is used for everything that is not specified by the
  16110. user."
  16111. (require 'parse-time)
  16112. (let* ((org-time-stamp-rounding-minutes
  16113. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16114. (org-dcst org-display-custom-times)
  16115. (ct (org-current-time))
  16116. (def (or default-time ct))
  16117. (defdecode (decode-time def))
  16118. (dummy (progn
  16119. (when (< (nth 2 defdecode) org-extend-today-until)
  16120. (setcar (nthcdr 2 defdecode) -1)
  16121. (setcar (nthcdr 1 defdecode) 59)
  16122. (setq def (apply 'encode-time defdecode)
  16123. defdecode (decode-time def)))))
  16124. (calendar-move-hook nil)
  16125. (view-diary-entries-initially nil)
  16126. (view-calendar-holidays-initially nil)
  16127. (timestr (format-time-string
  16128. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16129. (prompt (concat (if prompt (concat prompt " ") "")
  16130. (format "Date+time [%s]: " timestr)))
  16131. ans (org-ans0 "") org-ans1 org-ans2 final)
  16132. (cond
  16133. (from-string (setq ans from-string))
  16134. (org-read-date-popup-calendar
  16135. (save-excursion
  16136. (save-window-excursion
  16137. (calendar)
  16138. (calendar-forward-day (- (time-to-days def)
  16139. (calendar-absolute-from-gregorian
  16140. (calendar-current-date))))
  16141. (org-eval-in-calendar nil t)
  16142. (let* ((old-map (current-local-map))
  16143. (map (copy-keymap calendar-mode-map))
  16144. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16145. (org-defkey map (kbd "RET") 'org-calendar-select)
  16146. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16147. 'org-calendar-select-mouse)
  16148. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16149. 'org-calendar-select-mouse)
  16150. (org-defkey minibuffer-local-map [(meta shift left)]
  16151. (lambda () (interactive)
  16152. (org-eval-in-calendar '(calendar-backward-month 1))))
  16153. (org-defkey minibuffer-local-map [(meta shift right)]
  16154. (lambda () (interactive)
  16155. (org-eval-in-calendar '(calendar-forward-month 1))))
  16156. (org-defkey minibuffer-local-map [(meta shift up)]
  16157. (lambda () (interactive)
  16158. (org-eval-in-calendar '(calendar-backward-year 1))))
  16159. (org-defkey minibuffer-local-map [(meta shift down)]
  16160. (lambda () (interactive)
  16161. (org-eval-in-calendar '(calendar-forward-year 1))))
  16162. (org-defkey minibuffer-local-map [(shift up)]
  16163. (lambda () (interactive)
  16164. (org-eval-in-calendar '(calendar-backward-week 1))))
  16165. (org-defkey minibuffer-local-map [(shift down)]
  16166. (lambda () (interactive)
  16167. (org-eval-in-calendar '(calendar-forward-week 1))))
  16168. (org-defkey minibuffer-local-map [(shift left)]
  16169. (lambda () (interactive)
  16170. (org-eval-in-calendar '(calendar-backward-day 1))))
  16171. (org-defkey minibuffer-local-map [(shift right)]
  16172. (lambda () (interactive)
  16173. (org-eval-in-calendar '(calendar-forward-day 1))))
  16174. (org-defkey minibuffer-local-map ">"
  16175. (lambda () (interactive)
  16176. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16177. (org-defkey minibuffer-local-map "<"
  16178. (lambda () (interactive)
  16179. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16180. (unwind-protect
  16181. (progn
  16182. (use-local-map map)
  16183. (add-hook 'post-command-hook 'org-read-date-display)
  16184. (setq org-ans0 (read-string prompt default-input nil nil))
  16185. ;; org-ans0: from prompt
  16186. ;; org-ans1: from mouse click
  16187. ;; org-ans2: from calendar motion
  16188. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16189. (remove-hook 'post-command-hook 'org-read-date-display)
  16190. (use-local-map old-map)
  16191. (when org-read-date-overlay
  16192. (org-delete-overlay org-read-date-overlay)
  16193. (setq org-read-date-overlay nil)))))))
  16194. (t ; Naked prompt only
  16195. (unwind-protect
  16196. (setq ans (read-string prompt default-input nil timestr))
  16197. (when org-read-date-overlay
  16198. (org-delete-overlay org-read-date-overlay)
  16199. (setq org-read-date-overlay nil)))))
  16200. (setq final (org-read-date-analyze ans def defdecode))
  16201. (if to-time
  16202. (apply 'encode-time final)
  16203. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16204. (format "%04d-%02d-%02d %02d:%02d"
  16205. (nth 5 final) (nth 4 final) (nth 3 final)
  16206. (nth 2 final) (nth 1 final))
  16207. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16208. (defvar def)
  16209. (defvar defdecode)
  16210. (defvar with-time)
  16211. (defun org-read-date-display ()
  16212. "Display the currrent date prompt interpretation in the minibuffer."
  16213. (when org-read-date-display-live
  16214. (when org-read-date-overlay
  16215. (org-delete-overlay org-read-date-overlay))
  16216. (let ((p (point)))
  16217. (end-of-line 1)
  16218. (while (not (equal (buffer-substring
  16219. (max (point-min) (- (point) 4)) (point))
  16220. " "))
  16221. (insert " "))
  16222. (goto-char p))
  16223. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16224. " " (or org-ans1 org-ans2)))
  16225. (org-end-time-was-given nil)
  16226. (f (org-read-date-analyze ans def defdecode))
  16227. (fmts (if org-dcst
  16228. org-time-stamp-custom-formats
  16229. org-time-stamp-formats))
  16230. (fmt (if (or with-time
  16231. (and (boundp 'org-time-was-given) org-time-was-given))
  16232. (cdr fmts)
  16233. (car fmts)))
  16234. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16235. (when (and org-end-time-was-given
  16236. (string-match org-plain-time-of-day-regexp txt))
  16237. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16238. org-end-time-was-given
  16239. (substring txt (match-end 0)))))
  16240. (setq org-read-date-overlay
  16241. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16242. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16243. (defun org-read-date-analyze (ans def defdecode)
  16244. "Analyze the combined answer of the date prompt."
  16245. ;; FIXME: cleanup and comment
  16246. (let (delta deltan deltaw deltadef year month day
  16247. hour minute second wday pm h2 m2 tl wday1)
  16248. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16249. (setq ans (replace-match "" t t ans)
  16250. deltan (car delta)
  16251. deltaw (nth 1 delta)
  16252. deltadef (nth 2 delta)))
  16253. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16254. (when (string-match
  16255. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16256. (setq year (if (match-end 2)
  16257. (string-to-number (match-string 2 ans))
  16258. (string-to-number (format-time-string "%Y")))
  16259. month (string-to-number (match-string 3 ans))
  16260. day (string-to-number (match-string 4 ans)))
  16261. (if (< year 100) (setq year (+ 2000 year)))
  16262. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16263. t nil ans)))
  16264. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16265. ;; If there is a time with am/pm, and *no* time without it, we convert
  16266. ;; so that matching will be successful.
  16267. (loop for i from 1 to 2 do ; twice, for end time as well
  16268. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16269. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16270. (setq hour (string-to-number (match-string 1 ans))
  16271. minute (if (match-end 3)
  16272. (string-to-number (match-string 3 ans))
  16273. 0)
  16274. pm (equal ?p
  16275. (string-to-char (downcase (match-string 4 ans)))))
  16276. (if (and (= hour 12) (not pm))
  16277. (setq hour 0)
  16278. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16279. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16280. t t ans))))
  16281. ;; Check if a time range is given as a duration
  16282. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16283. (setq hour (string-to-number (match-string 1 ans))
  16284. h2 (+ hour (string-to-number (match-string 3 ans)))
  16285. minute (string-to-number (match-string 2 ans))
  16286. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16287. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16288. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16289. ;; Check if there is a time range
  16290. (when (boundp 'org-end-time-was-given)
  16291. (setq org-time-was-given nil)
  16292. (when (and (string-match org-plain-time-of-day-regexp ans)
  16293. (match-end 8))
  16294. (setq org-end-time-was-given (match-string 8 ans))
  16295. (setq ans (concat (substring ans 0 (match-beginning 7))
  16296. (substring ans (match-end 7))))))
  16297. (setq tl (parse-time-string ans)
  16298. day (or (nth 3 tl) (nth 3 defdecode))
  16299. month (or (nth 4 tl)
  16300. (if (and org-read-date-prefer-future
  16301. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16302. (1+ (nth 4 defdecode))
  16303. (nth 4 defdecode)))
  16304. year (or (nth 5 tl)
  16305. (if (and org-read-date-prefer-future
  16306. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16307. (1+ (nth 5 defdecode))
  16308. (nth 5 defdecode)))
  16309. hour (or (nth 2 tl) (nth 2 defdecode))
  16310. minute (or (nth 1 tl) (nth 1 defdecode))
  16311. second (or (nth 0 tl) 0)
  16312. wday (nth 6 tl))
  16313. (when deltan
  16314. (unless deltadef
  16315. (let ((now (decode-time (current-time))))
  16316. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16317. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16318. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16319. ((equal deltaw "m") (setq month (+ month deltan)))
  16320. ((equal deltaw "y") (setq year (+ year deltan)))))
  16321. (when (and wday (not (nth 3 tl)))
  16322. ;; Weekday was given, but no day, so pick that day in the week
  16323. ;; on or after the derived date.
  16324. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16325. (unless (equal wday wday1)
  16326. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16327. (if (and (boundp 'org-time-was-given)
  16328. (nth 2 tl))
  16329. (setq org-time-was-given t))
  16330. (if (< year 100) (setq year (+ 2000 year)))
  16331. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16332. (list second minute hour day month year)))
  16333. (defvar parse-time-weekdays)
  16334. (defun org-read-date-get-relative (s today default)
  16335. "Check string S for special relative date string.
  16336. TODAY and DEFAULT are internal times, for today and for a default.
  16337. Return shift list (N what def-flag)
  16338. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16339. N is the number of WHATs to shift.
  16340. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16341. the DEFAULT date rather than TODAY."
  16342. (when (string-match
  16343. (concat
  16344. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16345. "\\([0-9]+\\)?"
  16346. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16347. "\\([ \t]\\|$\\)") s)
  16348. (let* ((dir (if (match-end 1)
  16349. (string-to-char (substring (match-string 1 s) -1))
  16350. ?+))
  16351. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16352. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16353. (what (if (match-end 3) (match-string 3 s) "d"))
  16354. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16355. (date (if rel default today))
  16356. (wday (nth 6 (decode-time date)))
  16357. delta)
  16358. (if wday1
  16359. (progn
  16360. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16361. (if (= dir ?-) (setq delta (- delta 7)))
  16362. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16363. (list delta "d" rel))
  16364. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16365. (defun org-eval-in-calendar (form &optional keepdate)
  16366. "Eval FORM in the calendar window and return to current window.
  16367. Also, store the cursor date in variable org-ans2."
  16368. (let ((sw (selected-window)))
  16369. (select-window (get-buffer-window "*Calendar*"))
  16370. (eval form)
  16371. (when (and (not keepdate) (calendar-cursor-to-date))
  16372. (let* ((date (calendar-cursor-to-date))
  16373. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16374. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16375. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16376. (select-window sw)))
  16377. ; ;; Update the prompt to show new default date
  16378. ; (save-excursion
  16379. ; (goto-char (point-min))
  16380. ; (when (and org-ans2
  16381. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16382. ; (get-text-property (match-end 0) 'field))
  16383. ; (let ((inhibit-read-only t))
  16384. ; (replace-match (concat "[" org-ans2 "]") t t)
  16385. ; (add-text-properties (point-min) (1+ (match-end 0))
  16386. ; (text-properties-at (1+ (point-min)))))))))
  16387. (defun org-calendar-select ()
  16388. "Return to `org-read-date' with the date currently selected.
  16389. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16390. (interactive)
  16391. (when (calendar-cursor-to-date)
  16392. (let* ((date (calendar-cursor-to-date))
  16393. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16394. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16395. (if (active-minibuffer-window) (exit-minibuffer))))
  16396. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16397. "Insert a date stamp for the date given by the internal TIME.
  16398. WITH-HM means, use the stamp format that includes the time of the day.
  16399. INACTIVE means use square brackets instead of angular ones, so that the
  16400. stamp will not contribute to the agenda.
  16401. PRE and POST are optional strings to be inserted before and after the
  16402. stamp.
  16403. The command returns the inserted time stamp."
  16404. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16405. stamp)
  16406. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16407. (insert-before-markers (or pre ""))
  16408. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16409. (when (listp extra)
  16410. (setq extra (car extra))
  16411. (if (and (stringp extra)
  16412. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16413. (setq extra (format "-%02d:%02d"
  16414. (string-to-number (match-string 1 extra))
  16415. (string-to-number (match-string 2 extra))))
  16416. (setq extra nil)))
  16417. (when extra
  16418. (backward-char 1)
  16419. (insert-before-markers extra)
  16420. (forward-char 1))
  16421. (insert-before-markers (or post ""))
  16422. stamp))
  16423. (defun org-toggle-time-stamp-overlays ()
  16424. "Toggle the use of custom time stamp formats."
  16425. (interactive)
  16426. (setq org-display-custom-times (not org-display-custom-times))
  16427. (unless org-display-custom-times
  16428. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16429. (while (setq p (next-single-property-change p 'display))
  16430. (if (and (get-text-property p 'display)
  16431. (eq (get-text-property p 'face) 'org-date))
  16432. (remove-text-properties
  16433. p (setq p (next-single-property-change p 'display))
  16434. '(display t))))
  16435. (set-buffer-modified-p bmp)))
  16436. (if (featurep 'xemacs)
  16437. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16438. (org-restart-font-lock)
  16439. (setq org-table-may-need-update t)
  16440. (if org-display-custom-times
  16441. (message "Time stamps are overlayed with custom format")
  16442. (message "Time stamp overlays removed")))
  16443. (defun org-display-custom-time (beg end)
  16444. "Overlay modified time stamp format over timestamp between BED and END."
  16445. (let* ((ts (buffer-substring beg end))
  16446. t1 w1 with-hm tf time str w2 (off 0))
  16447. (save-match-data
  16448. (setq t1 (org-parse-time-string ts t))
  16449. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16450. (setq off (- (match-end 0) (match-beginning 0)))))
  16451. (setq end (- end off))
  16452. (setq w1 (- end beg)
  16453. with-hm (and (nth 1 t1) (nth 2 t1))
  16454. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16455. time (org-fix-decoded-time t1)
  16456. str (org-add-props
  16457. (format-time-string
  16458. (substring tf 1 -1) (apply 'encode-time time))
  16459. nil 'mouse-face 'highlight)
  16460. w2 (length str))
  16461. (if (not (= w2 w1))
  16462. (add-text-properties (1+ beg) (+ 2 beg)
  16463. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16464. (if (featurep 'xemacs)
  16465. (progn
  16466. (put-text-property beg end 'invisible t)
  16467. (put-text-property beg end 'end-glyph (make-glyph str)))
  16468. (put-text-property beg end 'display str))))
  16469. (defun org-translate-time (string)
  16470. "Translate all timestamps in STRING to custom format.
  16471. But do this only if the variable `org-display-custom-times' is set."
  16472. (when org-display-custom-times
  16473. (save-match-data
  16474. (let* ((start 0)
  16475. (re org-ts-regexp-both)
  16476. t1 with-hm inactive tf time str beg end)
  16477. (while (setq start (string-match re string start))
  16478. (setq beg (match-beginning 0)
  16479. end (match-end 0)
  16480. t1 (save-match-data
  16481. (org-parse-time-string (substring string beg end) t))
  16482. with-hm (and (nth 1 t1) (nth 2 t1))
  16483. inactive (equal (substring string beg (1+ beg)) "[")
  16484. tf (funcall (if with-hm 'cdr 'car)
  16485. org-time-stamp-custom-formats)
  16486. time (org-fix-decoded-time t1)
  16487. str (format-time-string
  16488. (concat
  16489. (if inactive "[" "<") (substring tf 1 -1)
  16490. (if inactive "]" ">"))
  16491. (apply 'encode-time time))
  16492. string (replace-match str t t string)
  16493. start (+ start (length str)))))))
  16494. string)
  16495. (defun org-fix-decoded-time (time)
  16496. "Set 0 instead of nil for the first 6 elements of time.
  16497. Don't touch the rest."
  16498. (let ((n 0))
  16499. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16500. (defun org-days-to-time (timestamp-string)
  16501. "Difference between TIMESTAMP-STRING and now in days."
  16502. (- (time-to-days (org-time-string-to-time timestamp-string))
  16503. (time-to-days (current-time))))
  16504. (defun org-deadline-close (timestamp-string &optional ndays)
  16505. "Is the time in TIMESTAMP-STRING close to the current date?"
  16506. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16507. (and (< (org-days-to-time timestamp-string) ndays)
  16508. (not (org-entry-is-done-p))))
  16509. (defun org-get-wdays (ts)
  16510. "Get the deadline lead time appropriate for timestring TS."
  16511. (cond
  16512. ((<= org-deadline-warning-days 0)
  16513. ;; 0 or negative, enforce this value no matter what
  16514. (- org-deadline-warning-days))
  16515. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16516. ;; lead time is specified.
  16517. (floor (* (string-to-number (match-string 1 ts))
  16518. (cdr (assoc (match-string 2 ts)
  16519. '(("d" . 1) ("w" . 7)
  16520. ("m" . 30.4) ("y" . 365.25)))))))
  16521. ;; go for the default.
  16522. (t org-deadline-warning-days)))
  16523. (defun org-calendar-select-mouse (ev)
  16524. "Return to `org-read-date' with the date currently selected.
  16525. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16526. (interactive "e")
  16527. (mouse-set-point ev)
  16528. (when (calendar-cursor-to-date)
  16529. (let* ((date (calendar-cursor-to-date))
  16530. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16531. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16532. (if (active-minibuffer-window) (exit-minibuffer))))
  16533. (defun org-check-deadlines (ndays)
  16534. "Check if there are any deadlines due or past due.
  16535. A deadline is considered due if it happens within `org-deadline-warning-days'
  16536. days from today's date. If the deadline appears in an entry marked DONE,
  16537. it is not shown. The prefix arg NDAYS can be used to test that many
  16538. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16539. (interactive "P")
  16540. (let* ((org-warn-days
  16541. (cond
  16542. ((equal ndays '(4)) 100000)
  16543. (ndays (prefix-numeric-value ndays))
  16544. (t (abs org-deadline-warning-days))))
  16545. (case-fold-search nil)
  16546. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16547. (callback
  16548. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16549. (message "%d deadlines past-due or due within %d days"
  16550. (org-occur regexp nil callback)
  16551. org-warn-days)))
  16552. (defun org-check-before-date (date)
  16553. "Check if there are deadlines or scheduled entries before DATE."
  16554. (interactive (list (org-read-date)))
  16555. (let ((case-fold-search nil)
  16556. (regexp (concat "\\<\\(" org-deadline-string
  16557. "\\|" org-scheduled-string
  16558. "\\) *<\\([^>]+\\)>"))
  16559. (callback
  16560. (lambda () (time-less-p
  16561. (org-time-string-to-time (match-string 2))
  16562. (org-time-string-to-time date)))))
  16563. (message "%d entries before %s"
  16564. (org-occur regexp nil callback) date)))
  16565. (defun org-evaluate-time-range (&optional to-buffer)
  16566. "Evaluate a time range by computing the difference between start and end.
  16567. Normally the result is just printed in the echo area, but with prefix arg
  16568. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16569. If the time range is actually in a table, the result is inserted into the
  16570. next column.
  16571. For time difference computation, a year is assumed to be exactly 365
  16572. days in order to avoid rounding problems."
  16573. (interactive "P")
  16574. (or
  16575. (org-clock-update-time-maybe)
  16576. (save-excursion
  16577. (unless (org-at-date-range-p t)
  16578. (goto-char (point-at-bol))
  16579. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16580. (if (not (org-at-date-range-p t))
  16581. (error "Not at a time-stamp range, and none found in current line")))
  16582. (let* ((ts1 (match-string 1))
  16583. (ts2 (match-string 2))
  16584. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16585. (match-end (match-end 0))
  16586. (time1 (org-time-string-to-time ts1))
  16587. (time2 (org-time-string-to-time ts2))
  16588. (t1 (time-to-seconds time1))
  16589. (t2 (time-to-seconds time2))
  16590. (diff (abs (- t2 t1)))
  16591. (negative (< (- t2 t1) 0))
  16592. ;; (ys (floor (* 365 24 60 60)))
  16593. (ds (* 24 60 60))
  16594. (hs (* 60 60))
  16595. (fy "%dy %dd %02d:%02d")
  16596. (fy1 "%dy %dd")
  16597. (fd "%dd %02d:%02d")
  16598. (fd1 "%dd")
  16599. (fh "%02d:%02d")
  16600. y d h m align)
  16601. (if havetime
  16602. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16603. y 0
  16604. d (floor (/ diff ds)) diff (mod diff ds)
  16605. h (floor (/ diff hs)) diff (mod diff hs)
  16606. m (floor (/ diff 60)))
  16607. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16608. y 0
  16609. d (floor (+ (/ diff ds) 0.5))
  16610. h 0 m 0))
  16611. (if (not to-buffer)
  16612. (message "%s" (org-make-tdiff-string y d h m))
  16613. (if (org-at-table-p)
  16614. (progn
  16615. (goto-char match-end)
  16616. (setq align t)
  16617. (and (looking-at " *|") (goto-char (match-end 0))))
  16618. (goto-char match-end))
  16619. (if (looking-at
  16620. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16621. (replace-match ""))
  16622. (if negative (insert " -"))
  16623. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16624. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16625. (insert " " (format fh h m))))
  16626. (if align (org-table-align))
  16627. (message "Time difference inserted")))))
  16628. (defun org-make-tdiff-string (y d h m)
  16629. (let ((fmt "")
  16630. (l nil))
  16631. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16632. l (push y l)))
  16633. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16634. l (push d l)))
  16635. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16636. l (push h l)))
  16637. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16638. l (push m l)))
  16639. (apply 'format fmt (nreverse l))))
  16640. (defun org-time-string-to-time (s)
  16641. (apply 'encode-time (org-parse-time-string s)))
  16642. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16643. "Convert a time stamp to an absolute day number.
  16644. If there is a specifyer for a cyclic time stamp, get the closest date to
  16645. DAYNR."
  16646. (cond
  16647. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16648. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16649. daynr
  16650. (+ daynr 1000)))
  16651. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16652. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16653. (time-to-days (current-time))) (match-string 0 s)
  16654. prefer))
  16655. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16656. (defun org-time-from-absolute (d)
  16657. "Return the time corresponding to date D.
  16658. D may be an absolute day number, or a calendar-type list (month day year)."
  16659. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16660. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16661. (defun org-calendar-holiday ()
  16662. "List of holidays, for Diary display in Org-mode."
  16663. (require 'holidays)
  16664. (let ((hl (funcall
  16665. (if (fboundp 'calendar-check-holidays)
  16666. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16667. (if hl (mapconcat 'identity hl "; "))))
  16668. (defun org-diary-sexp-entry (sexp entry date)
  16669. "Process a SEXP diary ENTRY for DATE."
  16670. (require 'diary-lib)
  16671. (let ((result (if calendar-debug-sexp
  16672. (let ((stack-trace-on-error t))
  16673. (eval (car (read-from-string sexp))))
  16674. (condition-case nil
  16675. (eval (car (read-from-string sexp)))
  16676. (error
  16677. (beep)
  16678. (message "Bad sexp at line %d in %s: %s"
  16679. (org-current-line)
  16680. (buffer-file-name) sexp)
  16681. (sleep-for 2))))))
  16682. (cond ((stringp result) result)
  16683. ((and (consp result)
  16684. (stringp (cdr result))) (cdr result))
  16685. (result entry)
  16686. (t nil))))
  16687. (defun org-diary-to-ical-string (frombuf)
  16688. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16689. This uses the icalendar.el library."
  16690. (let* ((tmpdir (if (featurep 'xemacs)
  16691. (temp-directory)
  16692. temporary-file-directory))
  16693. (tmpfile (make-temp-name
  16694. (expand-file-name "orgics" tmpdir)))
  16695. buf rtn b e)
  16696. (save-excursion
  16697. (set-buffer frombuf)
  16698. (icalendar-export-region (point-min) (point-max) tmpfile)
  16699. (setq buf (find-buffer-visiting tmpfile))
  16700. (set-buffer buf)
  16701. (goto-char (point-min))
  16702. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16703. (setq b (match-beginning 0)))
  16704. (goto-char (point-max))
  16705. (if (re-search-backward "^END:VEVENT" nil t)
  16706. (setq e (match-end 0)))
  16707. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16708. (kill-buffer buf)
  16709. (kill-buffer frombuf)
  16710. (delete-file tmpfile)
  16711. rtn))
  16712. (defun org-closest-date (start current change prefer)
  16713. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16714. When PREFER is `past' return a date that is either CURRENT or past.
  16715. When PREFER is `future', return a date that is either CURRENT or future."
  16716. ;; Make the proper lists from the dates
  16717. (catch 'exit
  16718. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16719. dn dw sday cday n1 n2
  16720. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16721. (setq start (org-date-to-gregorian start)
  16722. current (org-date-to-gregorian
  16723. (if org-agenda-repeating-timestamp-show-all
  16724. current
  16725. (time-to-days (current-time))))
  16726. sday (calendar-absolute-from-gregorian start)
  16727. cday (calendar-absolute-from-gregorian current))
  16728. (if (<= cday sday) (throw 'exit sday))
  16729. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16730. (setq dn (string-to-number (match-string 1 change))
  16731. dw (cdr (assoc (match-string 2 change) a1)))
  16732. (error "Invalid change specifyer: %s" change))
  16733. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16734. (cond
  16735. ((eq dw 'day)
  16736. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16737. n2 (+ n1 dn)))
  16738. ((eq dw 'year)
  16739. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16740. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16741. (setq date1 (list m d y1)
  16742. n1 (calendar-absolute-from-gregorian date1)
  16743. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16744. n2 (calendar-absolute-from-gregorian date2)))
  16745. ((eq dw 'month)
  16746. ;; approx number of month between the tow dates
  16747. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16748. ;; How often does dn fit in there?
  16749. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16750. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16751. m (+ m nm)
  16752. ny (floor (/ m 12))
  16753. y (+ y ny)
  16754. m (- m (* ny 12)))
  16755. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16756. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16757. (setq m2 (+ m dn) y2 y)
  16758. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16759. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16760. (while (< n2 cday)
  16761. (setq n1 n2 m m2 y y2)
  16762. (setq m2 (+ m dn) y2 y)
  16763. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16764. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16765. (if org-agenda-repeating-timestamp-show-all
  16766. (cond
  16767. ((eq prefer 'past) n1)
  16768. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16769. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16770. (cond
  16771. ((eq prefer 'past) n1)
  16772. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16773. (t (if (= cday n1) n1 n2)))))))
  16774. (defun org-date-to-gregorian (date)
  16775. "Turn any specification of DATE into a gregorian date for the calendar."
  16776. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16777. ((and (listp date) (= (length date) 3)) date)
  16778. ((stringp date)
  16779. (setq date (org-parse-time-string date))
  16780. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16781. ((listp date)
  16782. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16783. (defun org-parse-time-string (s &optional nodefault)
  16784. "Parse the standard Org-mode time string.
  16785. This should be a lot faster than the normal `parse-time-string'.
  16786. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16787. hour and minute fields will be nil if not given."
  16788. (if (string-match org-ts-regexp0 s)
  16789. (list 0
  16790. (if (or (match-beginning 8) (not nodefault))
  16791. (string-to-number (or (match-string 8 s) "0")))
  16792. (if (or (match-beginning 7) (not nodefault))
  16793. (string-to-number (or (match-string 7 s) "0")))
  16794. (string-to-number (match-string 4 s))
  16795. (string-to-number (match-string 3 s))
  16796. (string-to-number (match-string 2 s))
  16797. nil nil nil)
  16798. (make-list 9 0)))
  16799. (defun org-timestamp-up (&optional arg)
  16800. "Increase the date item at the cursor by one.
  16801. If the cursor is on the year, change the year. If it is on the month or
  16802. the day, change that.
  16803. With prefix ARG, change by that many units."
  16804. (interactive "p")
  16805. (org-timestamp-change (prefix-numeric-value arg)))
  16806. (defun org-timestamp-down (&optional arg)
  16807. "Decrease the date item at the cursor by one.
  16808. If the cursor is on the year, change the year. If it is on the month or
  16809. the day, change that.
  16810. With prefix ARG, change by that many units."
  16811. (interactive "p")
  16812. (org-timestamp-change (- (prefix-numeric-value arg))))
  16813. (defun org-timestamp-up-day (&optional arg)
  16814. "Increase the date in the time stamp by one day.
  16815. With prefix ARG, change that many days."
  16816. (interactive "p")
  16817. (if (and (not (org-at-timestamp-p t))
  16818. (org-on-heading-p))
  16819. (org-todo 'up)
  16820. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16821. (defun org-timestamp-down-day (&optional arg)
  16822. "Decrease the date in the time stamp by one day.
  16823. With prefix ARG, change that many days."
  16824. (interactive "p")
  16825. (if (and (not (org-at-timestamp-p t))
  16826. (org-on-heading-p))
  16827. (org-todo 'down)
  16828. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16829. (defsubst org-pos-in-match-range (pos n)
  16830. (and (match-beginning n)
  16831. (<= (match-beginning n) pos)
  16832. (>= (match-end n) pos)))
  16833. (defun org-at-timestamp-p (&optional inactive-ok)
  16834. "Determine if the cursor is in or at a timestamp."
  16835. (interactive)
  16836. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16837. (pos (point))
  16838. (ans (or (looking-at tsr)
  16839. (save-excursion
  16840. (skip-chars-backward "^[<\n\r\t")
  16841. (if (> (point) (point-min)) (backward-char 1))
  16842. (and (looking-at tsr)
  16843. (> (- (match-end 0) pos) -1))))))
  16844. (and ans
  16845. (boundp 'org-ts-what)
  16846. (setq org-ts-what
  16847. (cond
  16848. ((= pos (match-beginning 0)) 'bracket)
  16849. ((= pos (1- (match-end 0))) 'bracket)
  16850. ((org-pos-in-match-range pos 2) 'year)
  16851. ((org-pos-in-match-range pos 3) 'month)
  16852. ((org-pos-in-match-range pos 7) 'hour)
  16853. ((org-pos-in-match-range pos 8) 'minute)
  16854. ((or (org-pos-in-match-range pos 4)
  16855. (org-pos-in-match-range pos 5)) 'day)
  16856. ((and (> pos (or (match-end 8) (match-end 5)))
  16857. (< pos (match-end 0)))
  16858. (- pos (or (match-end 8) (match-end 5))))
  16859. (t 'day))))
  16860. ans))
  16861. (defun org-toggle-timestamp-type ()
  16862. ""
  16863. (interactive)
  16864. (when (org-at-timestamp-p t)
  16865. (save-excursion
  16866. (goto-char (match-beginning 0))
  16867. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16868. (goto-char (1- (match-end 0)))
  16869. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16870. (message "Timestamp is now %sactive"
  16871. (if (equal (char-before) ?>) "in" ""))))
  16872. (defun org-timestamp-change (n &optional what)
  16873. "Change the date in the time stamp at point.
  16874. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16875. `year', `minute', `second'. If WHAT is not given, the cursor position
  16876. in the timestamp determines what will be changed."
  16877. (let ((pos (point))
  16878. with-hm inactive
  16879. org-ts-what
  16880. extra
  16881. ts time time0)
  16882. (if (not (org-at-timestamp-p t))
  16883. (error "Not at a timestamp"))
  16884. (if (and (not what) (eq org-ts-what 'bracket))
  16885. (org-toggle-timestamp-type)
  16886. (if (and (not what) (not (eq org-ts-what 'day))
  16887. org-display-custom-times
  16888. (get-text-property (point) 'display)
  16889. (not (get-text-property (1- (point)) 'display)))
  16890. (setq org-ts-what 'day))
  16891. (setq org-ts-what (or what org-ts-what)
  16892. inactive (= (char-after (match-beginning 0)) ?\[)
  16893. ts (match-string 0))
  16894. (replace-match "")
  16895. (if (string-match
  16896. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16897. ts)
  16898. (setq extra (match-string 1 ts)))
  16899. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16900. (setq with-hm t))
  16901. (setq time0 (org-parse-time-string ts))
  16902. (setq time
  16903. (encode-time (or (car time0) 0)
  16904. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16905. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16906. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16907. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16908. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16909. (nthcdr 6 time0)))
  16910. (when (integerp org-ts-what)
  16911. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16912. (if (eq what 'calendar)
  16913. (let ((cal-date (org-get-date-from-calendar)))
  16914. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16915. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16916. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16917. (setcar time0 (or (car time0) 0))
  16918. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16919. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16920. (setq time (apply 'encode-time time0))))
  16921. (setq org-last-changed-timestamp
  16922. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16923. (org-clock-update-time-maybe)
  16924. (goto-char pos)
  16925. ;; Try to recenter the calendar window, if any
  16926. (if (and org-calendar-follow-timestamp-change
  16927. (get-buffer-window "*Calendar*" t)
  16928. (memq org-ts-what '(day month year)))
  16929. (org-recenter-calendar (time-to-days time))))))
  16930. ;; FIXME: does not yet work for lead times
  16931. (defun org-modify-ts-extra (s pos n)
  16932. "Change the different parts of the lead-time and repeat fields in timestamp."
  16933. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16934. ng h m new)
  16935. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16936. (cond
  16937. ((or (org-pos-in-match-range pos 2)
  16938. (org-pos-in-match-range pos 3))
  16939. (setq m (string-to-number (match-string 3 s))
  16940. h (string-to-number (match-string 2 s)))
  16941. (if (org-pos-in-match-range pos 2)
  16942. (setq h (+ h n))
  16943. (setq m (+ m n)))
  16944. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16945. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16946. (setq h (min 24 (max 0 h)))
  16947. (setq ng 1 new (format "-%02d:%02d" h m)))
  16948. ((org-pos-in-match-range pos 6)
  16949. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16950. ((org-pos-in-match-range pos 5)
  16951. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16952. (when ng
  16953. (setq s (concat
  16954. (substring s 0 (match-beginning ng))
  16955. new
  16956. (substring s (match-end ng))))))
  16957. s))
  16958. (defun org-recenter-calendar (date)
  16959. "If the calendar is visible, recenter it to DATE."
  16960. (let* ((win (selected-window))
  16961. (cwin (get-buffer-window "*Calendar*" t))
  16962. (calendar-move-hook nil))
  16963. (when cwin
  16964. (select-window cwin)
  16965. (calendar-goto-date (if (listp date) date
  16966. (calendar-gregorian-from-absolute date)))
  16967. (select-window win))))
  16968. (defun org-goto-calendar (&optional arg)
  16969. "Go to the Emacs calendar at the current date.
  16970. If there is a time stamp in the current line, go to that date.
  16971. A prefix ARG can be used to force the current date."
  16972. (interactive "P")
  16973. (let ((tsr org-ts-regexp) diff
  16974. (calendar-move-hook nil)
  16975. (view-calendar-holidays-initially nil)
  16976. (view-diary-entries-initially nil))
  16977. (if (or (org-at-timestamp-p)
  16978. (save-excursion
  16979. (beginning-of-line 1)
  16980. (looking-at (concat ".*" tsr))))
  16981. (let ((d1 (time-to-days (current-time)))
  16982. (d2 (time-to-days
  16983. (org-time-string-to-time (match-string 1)))))
  16984. (setq diff (- d2 d1))))
  16985. (calendar)
  16986. (calendar-goto-today)
  16987. (if (and diff (not arg)) (calendar-forward-day diff))))
  16988. (defun org-get-date-from-calendar ()
  16989. "Return a list (month day year) of date at point in calendar."
  16990. (with-current-buffer "*Calendar*"
  16991. (save-match-data
  16992. (calendar-cursor-to-date))))
  16993. (defun org-date-from-calendar ()
  16994. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16995. If there is already a time stamp at the cursor position, update it."
  16996. (interactive)
  16997. (if (org-at-timestamp-p t)
  16998. (org-timestamp-change 0 'calendar)
  16999. (let ((cal-date (org-get-date-from-calendar)))
  17000. (org-insert-time-stamp
  17001. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17002. ;; Make appt aware of appointments from the agenda
  17003. ;;;###autoload
  17004. (defun org-agenda-to-appt (&optional filter)
  17005. "Activate appointments found in `org-agenda-files'.
  17006. When prefixed, prompt for a regular expression and use it as a
  17007. filter: only add entries if they match this regular expression.
  17008. FILTER can be a string. In this case, use this string as a
  17009. regular expression to filter results.
  17010. FILTER can also be an alist, with the car of each cell being
  17011. either 'headline or 'category. For example:
  17012. '((headline \"IMPORTANT\")
  17013. (category \"Work\"))
  17014. will only add headlines containing IMPORTANT or headlines
  17015. belonging to the category \"Work\"."
  17016. (interactive "P")
  17017. (require 'calendar)
  17018. (if (equal filter '(4))
  17019. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17020. (let* ((cnt 0) ; count added events
  17021. (org-agenda-new-buffers nil)
  17022. (today (org-date-to-gregorian
  17023. (time-to-days (current-time))))
  17024. (files (org-agenda-files)) entries file)
  17025. ;; Get all entries which may contain an appt
  17026. (while (setq file (pop files))
  17027. (setq entries
  17028. (append entries
  17029. (org-agenda-get-day-entries
  17030. file today
  17031. :timestamp :scheduled :deadline))))
  17032. (setq entries (delq nil entries))
  17033. ;; Map thru entries and find if they pass thru the filter
  17034. (mapc
  17035. (lambda(x)
  17036. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17037. (cat (get-text-property 1 'org-category x))
  17038. (tod (get-text-property 1 'time-of-day x))
  17039. (ok (or (null filter)
  17040. (and (stringp filter) (string-match filter evt))
  17041. (and (listp filter)
  17042. (or (string-match
  17043. (cadr (assoc 'category filter)) cat)
  17044. (string-match
  17045. (cadr (assoc 'headline filter)) evt))))))
  17046. ;; FIXME: Shall we remove text-properties for the appt text?
  17047. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17048. (when (and ok tod)
  17049. (setq tod (number-to-string tod)
  17050. tod (when (string-match
  17051. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17052. (concat (match-string 1 tod) ":"
  17053. (match-string 2 tod))))
  17054. (appt-add tod evt)
  17055. (setq cnt (1+ cnt))))) entries)
  17056. (org-release-buffers org-agenda-new-buffers)
  17057. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  17058. ;;; The clock for measuring work time.
  17059. (defvar org-mode-line-string "")
  17060. (put 'org-mode-line-string 'risky-local-variable t)
  17061. (defvar org-mode-line-timer nil)
  17062. (defvar org-clock-heading "")
  17063. (defvar org-clock-start-time "")
  17064. (defun org-update-mode-line ()
  17065. (let* ((delta (- (time-to-seconds (current-time))
  17066. (time-to-seconds org-clock-start-time)))
  17067. (h (floor delta 3600))
  17068. (m (floor (- delta (* 3600 h)) 60)))
  17069. (setq org-mode-line-string
  17070. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17071. 'help-echo "Org-mode clock is running"))
  17072. (force-mode-line-update)))
  17073. (defvar org-clock-marker (make-marker)
  17074. "Marker recording the last clock-in.")
  17075. (defvar org-clock-mode-line-entry nil
  17076. "Information for the modeline about the running clock.")
  17077. (defun org-clock-in ()
  17078. "Start the clock on the current item.
  17079. If necessary, clock-out of the currently active clock."
  17080. (interactive)
  17081. (org-clock-out t)
  17082. (let (ts)
  17083. (save-excursion
  17084. (org-back-to-heading t)
  17085. (when (and org-clock-in-switch-to-state
  17086. (not (looking-at (concat outline-regexp "[ \t]*"
  17087. org-clock-in-switch-to-state
  17088. "\\>"))))
  17089. (org-todo org-clock-in-switch-to-state))
  17090. (if (and org-clock-heading-function
  17091. (functionp org-clock-heading-function))
  17092. (setq org-clock-heading (funcall org-clock-heading-function))
  17093. (if (looking-at org-complex-heading-regexp)
  17094. (setq org-clock-heading (match-string 4))
  17095. (setq org-clock-heading "???")))
  17096. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17097. (org-clock-find-position)
  17098. (insert "\n") (backward-char 1)
  17099. (indent-relative)
  17100. (insert org-clock-string " ")
  17101. (setq org-clock-start-time (current-time))
  17102. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17103. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17104. (or global-mode-string (setq global-mode-string '("")))
  17105. (or (memq 'org-mode-line-string global-mode-string)
  17106. (setq global-mode-string
  17107. (append global-mode-string '(org-mode-line-string))))
  17108. (org-update-mode-line)
  17109. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17110. (message "Clock started at %s" ts))))
  17111. (defun org-clock-find-position ()
  17112. "Find the location where the next clock line should be inserted."
  17113. (org-back-to-heading t)
  17114. (catch 'exit
  17115. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17116. (re (concat "^[ \t]*" org-clock-string))
  17117. (cnt 0)
  17118. first last)
  17119. (goto-char beg)
  17120. (when (eobp) (newline) (setq end (max (point) end)))
  17121. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17122. ;; we seem to have a CLOCK drawer, so go there.
  17123. (beginning-of-line 2)
  17124. (throw 'exit t))
  17125. ;; Lets count the CLOCK lines
  17126. (goto-char beg)
  17127. (while (re-search-forward re end t)
  17128. (setq first (or first (match-beginning 0))
  17129. last (match-beginning 0)
  17130. cnt (1+ cnt)))
  17131. (when (and (integerp org-clock-into-drawer)
  17132. (>= (1+ cnt) org-clock-into-drawer))
  17133. ;; Wrap current entries into a new drawer
  17134. (goto-char last)
  17135. (beginning-of-line 2)
  17136. (if (org-at-item-p) (org-end-of-item))
  17137. (insert ":END:\n")
  17138. (beginning-of-line 0)
  17139. (org-indent-line-function)
  17140. (goto-char first)
  17141. (insert ":CLOCK:\n")
  17142. (beginning-of-line 0)
  17143. (org-indent-line-function)
  17144. (org-flag-drawer t)
  17145. (beginning-of-line 2)
  17146. (throw 'exit nil))
  17147. (goto-char beg)
  17148. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17149. (not (equal (match-string 1) org-clock-string)))
  17150. ;; Planning info, skip to after it
  17151. (beginning-of-line 2)
  17152. (or (bolp) (newline)))
  17153. (when (eq t org-clock-into-drawer)
  17154. (insert ":CLOCK:\n:END:\n")
  17155. (beginning-of-line -1)
  17156. (org-indent-line-function)
  17157. (org-flag-drawer t)
  17158. (beginning-of-line 2)
  17159. (org-indent-line-function)))))
  17160. (defun org-clock-out (&optional fail-quietly)
  17161. "Stop the currently running clock.
  17162. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17163. (interactive)
  17164. (catch 'exit
  17165. (if (not (marker-buffer org-clock-marker))
  17166. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17167. (let (ts te s h m)
  17168. (save-excursion
  17169. (set-buffer (marker-buffer org-clock-marker))
  17170. (goto-char org-clock-marker)
  17171. (beginning-of-line 1)
  17172. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17173. (equal (match-string 1) org-clock-string))
  17174. (setq ts (match-string 2))
  17175. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17176. (goto-char (match-end 0))
  17177. (delete-region (point) (point-at-eol))
  17178. (insert "--")
  17179. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17180. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17181. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17182. h (floor (/ s 3600))
  17183. s (- s (* 3600 h))
  17184. m (floor (/ s 60))
  17185. s (- s (* 60 s)))
  17186. (insert " => " (format "%2d:%02d" h m))
  17187. (move-marker org-clock-marker nil)
  17188. (when org-log-note-clock-out
  17189. (org-add-log-maybe 'clock-out))
  17190. (when org-mode-line-timer
  17191. (cancel-timer org-mode-line-timer)
  17192. (setq org-mode-line-timer nil))
  17193. (setq global-mode-string
  17194. (delq 'org-mode-line-string global-mode-string))
  17195. (force-mode-line-update)
  17196. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17197. (defun org-clock-cancel ()
  17198. "Cancel the running clock be removing the start timestamp."
  17199. (interactive)
  17200. (if (not (marker-buffer org-clock-marker))
  17201. (error "No active clock"))
  17202. (save-excursion
  17203. (set-buffer (marker-buffer org-clock-marker))
  17204. (goto-char org-clock-marker)
  17205. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17206. (setq global-mode-string
  17207. (delq 'org-mode-line-string global-mode-string))
  17208. (force-mode-line-update)
  17209. (message "Clock canceled"))
  17210. (defun org-clock-goto (&optional delete-windows)
  17211. "Go to the currently clocked-in entry."
  17212. (interactive "P")
  17213. (if (not (marker-buffer org-clock-marker))
  17214. (error "No active clock"))
  17215. (switch-to-buffer-other-window
  17216. (marker-buffer org-clock-marker))
  17217. (if delete-windows (delete-other-windows))
  17218. (goto-char org-clock-marker)
  17219. (org-show-entry)
  17220. (org-back-to-heading)
  17221. (recenter))
  17222. (defvar org-clock-file-total-minutes nil
  17223. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17224. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17225. (defun org-clock-sum (&optional tstart tend)
  17226. "Sum the times for each subtree.
  17227. Puts the resulting times in minutes as a text property on each headline."
  17228. (interactive)
  17229. (let* ((bmp (buffer-modified-p))
  17230. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17231. org-clock-string
  17232. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17233. (lmax 30)
  17234. (ltimes (make-vector lmax 0))
  17235. (t1 0)
  17236. (level 0)
  17237. ts te dt
  17238. time)
  17239. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17240. (save-excursion
  17241. (goto-char (point-max))
  17242. (while (re-search-backward re nil t)
  17243. (cond
  17244. ((match-end 2)
  17245. ;; Two time stamps
  17246. (setq ts (match-string 2)
  17247. te (match-string 3)
  17248. ts (time-to-seconds
  17249. (apply 'encode-time (org-parse-time-string ts)))
  17250. te (time-to-seconds
  17251. (apply 'encode-time (org-parse-time-string te)))
  17252. ts (if tstart (max ts tstart) ts)
  17253. te (if tend (min te tend) te)
  17254. dt (- te ts)
  17255. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17256. ((match-end 4)
  17257. ;; A naket time
  17258. (setq t1 (+ t1 (string-to-number (match-string 5))
  17259. (* 60 (string-to-number (match-string 4))))))
  17260. (t ;; A headline
  17261. (setq level (- (match-end 1) (match-beginning 1)))
  17262. (when (or (> t1 0) (> (aref ltimes level) 0))
  17263. (loop for l from 0 to level do
  17264. (aset ltimes l (+ (aref ltimes l) t1)))
  17265. (setq t1 0 time (aref ltimes level))
  17266. (loop for l from level to (1- lmax) do
  17267. (aset ltimes l 0))
  17268. (goto-char (match-beginning 0))
  17269. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17270. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17271. (set-buffer-modified-p bmp)))
  17272. (defun org-clock-display (&optional total-only)
  17273. "Show subtree times in the entire buffer.
  17274. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17275. in the echo area."
  17276. (interactive)
  17277. (org-remove-clock-overlays)
  17278. (let (time h m p)
  17279. (org-clock-sum)
  17280. (unless total-only
  17281. (save-excursion
  17282. (goto-char (point-min))
  17283. (while (or (and (equal (setq p (point)) (point-min))
  17284. (get-text-property p :org-clock-minutes))
  17285. (setq p (next-single-property-change
  17286. (point) :org-clock-minutes)))
  17287. (goto-char p)
  17288. (when (setq time (get-text-property p :org-clock-minutes))
  17289. (org-put-clock-overlay time (funcall outline-level))))
  17290. (setq h (/ org-clock-file-total-minutes 60)
  17291. m (- org-clock-file-total-minutes (* 60 h)))
  17292. ;; Arrange to remove the overlays upon next change.
  17293. (when org-remove-highlights-with-change
  17294. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17295. nil 'local))))
  17296. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17297. (defvar org-clock-overlays nil)
  17298. (make-variable-buffer-local 'org-clock-overlays)
  17299. (defun org-put-clock-overlay (time &optional level)
  17300. "Put an overlays on the current line, displaying TIME.
  17301. If LEVEL is given, prefix time with a corresponding number of stars.
  17302. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17303. will be easy to remove."
  17304. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17305. (l (if level (org-get-legal-level level 0) 0))
  17306. (off 0)
  17307. ov tx)
  17308. (move-to-column c)
  17309. (unless (eolp) (skip-chars-backward "^ \t"))
  17310. (skip-chars-backward " \t")
  17311. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17312. tx (concat (buffer-substring (1- (point)) (point))
  17313. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17314. (org-add-props (format "%s %2d:%02d%s"
  17315. (make-string l ?*) h m
  17316. (make-string (- 10 l) ?\ ))
  17317. '(face secondary-selection))
  17318. ""))
  17319. (if (not (featurep 'xemacs))
  17320. (org-overlay-put ov 'display tx)
  17321. (org-overlay-put ov 'invisible t)
  17322. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17323. (push ov org-clock-overlays)))
  17324. (defun org-remove-clock-overlays (&optional beg end noremove)
  17325. "Remove the occur highlights from the buffer.
  17326. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17327. from the `before-change-functions' in the current buffer."
  17328. (interactive)
  17329. (unless org-inhibit-highlight-removal
  17330. (mapc 'org-delete-overlay org-clock-overlays)
  17331. (setq org-clock-overlays nil)
  17332. (unless noremove
  17333. (remove-hook 'before-change-functions
  17334. 'org-remove-clock-overlays 'local))))
  17335. (defun org-clock-out-if-current ()
  17336. "Clock out if the current entry contains the running clock.
  17337. This is used to stop the clock after a TODO entry is marked DONE,
  17338. and is only done if the variable `org-clock-out-when-done' is not nil."
  17339. (when (and org-clock-out-when-done
  17340. (member state org-done-keywords)
  17341. (equal (marker-buffer org-clock-marker) (current-buffer))
  17342. (< (point) org-clock-marker)
  17343. (> (save-excursion (outline-next-heading) (point))
  17344. org-clock-marker))
  17345. ;; Clock out, but don't accept a logging message for this.
  17346. (let ((org-log-note-clock-out nil))
  17347. (org-clock-out))))
  17348. (add-hook 'org-after-todo-state-change-hook
  17349. 'org-clock-out-if-current)
  17350. (defun org-check-running-clock ()
  17351. "Check if the current buffer contains the running clock.
  17352. If yes, offer to stop it and to save the buffer with the changes."
  17353. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17354. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17355. (buffer-name))))
  17356. (org-clock-out)
  17357. (when (y-or-n-p "Save changed buffer?")
  17358. (save-buffer))))
  17359. (defun org-clock-report (&optional arg)
  17360. "Create a table containing a report about clocked time.
  17361. If the cursor is inside an existing clocktable block, then the table
  17362. will be updated. If not, a new clocktable will be inserted.
  17363. When called with a prefix argument, move to the first clock table in the
  17364. buffer and update it."
  17365. (interactive "P")
  17366. (org-remove-clock-overlays)
  17367. (when arg
  17368. (org-find-dblock "clocktable")
  17369. (org-show-entry))
  17370. (if (org-in-clocktable-p)
  17371. (goto-char (org-in-clocktable-p))
  17372. (org-create-dblock (list :name "clocktable"
  17373. :maxlevel 2 :scope 'file)))
  17374. (org-update-dblock))
  17375. (defun org-in-clocktable-p ()
  17376. "Check if the cursor is in a clocktable."
  17377. (let ((pos (point)) start)
  17378. (save-excursion
  17379. (end-of-line 1)
  17380. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17381. (setq start (match-beginning 0))
  17382. (re-search-forward "^#\\+END:.*" nil t)
  17383. (>= (match-end 0) pos)
  17384. start))))
  17385. (defun org-clock-update-time-maybe ()
  17386. "If this is a CLOCK line, update it and return t.
  17387. Otherwise, return nil."
  17388. (interactive)
  17389. (save-excursion
  17390. (beginning-of-line 1)
  17391. (skip-chars-forward " \t")
  17392. (when (looking-at org-clock-string)
  17393. (let ((re (concat "[ \t]*" org-clock-string
  17394. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17395. "\\([ \t]*=>.*\\)?"))
  17396. ts te h m s)
  17397. (if (not (looking-at re))
  17398. nil
  17399. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17400. (end-of-line 1)
  17401. (setq ts (match-string 1)
  17402. te (match-string 2))
  17403. (setq s (- (time-to-seconds
  17404. (apply 'encode-time (org-parse-time-string te)))
  17405. (time-to-seconds
  17406. (apply 'encode-time (org-parse-time-string ts))))
  17407. h (floor (/ s 3600))
  17408. s (- s (* 3600 h))
  17409. m (floor (/ s 60))
  17410. s (- s (* 60 s)))
  17411. (insert " => " (format "%2d:%02d" h m))
  17412. t)))))
  17413. (defun org-clock-special-range (key &optional time as-strings)
  17414. "Return two times bordering a special time range.
  17415. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17416. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17417. A week starts Monday 0:00 and ends Sunday 24:00.
  17418. The range is determined relative to TIME. TIME defaults to the current time.
  17419. The return value is a cons cell with two internal times like the ones
  17420. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17421. the returned times will be formatted strings."
  17422. (let* ((tm (decode-time (or time (current-time))))
  17423. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17424. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17425. (dow (nth 6 tm))
  17426. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17427. (cond
  17428. ((eq key 'today)
  17429. (setq h 0 m 0 h1 24 m1 0))
  17430. ((eq key 'yesterday)
  17431. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17432. ((eq key 'thisweek)
  17433. (setq diff (if (= dow 0) 6 (1- dow))
  17434. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17435. ((eq key 'lastweek)
  17436. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17437. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17438. ((eq key 'thismonth)
  17439. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17440. ((eq key 'lastmonth)
  17441. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17442. ((eq key 'thisyear)
  17443. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17444. ((eq key 'lastyear)
  17445. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17446. (t (error "No such time block %s" key)))
  17447. (setq ts (encode-time s m h d month y)
  17448. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17449. (or d1 d) (or month1 month) (or y1 y)))
  17450. (setq fm (cdr org-time-stamp-formats))
  17451. (if as-strings
  17452. (cons (format-time-string fm ts) (format-time-string fm te))
  17453. (cons ts te))))
  17454. (defun org-dblock-write:clocktable (params)
  17455. "Write the standard clocktable."
  17456. (catch 'exit
  17457. (let* ((hlchars '((1 . "*") (2 . "/")))
  17458. (ins (make-marker))
  17459. (total-time nil)
  17460. (scope (plist-get params :scope))
  17461. (tostring (plist-get params :tostring))
  17462. (multifile (plist-get params :multifile))
  17463. (header (plist-get params :header))
  17464. (maxlevel (or (plist-get params :maxlevel) 3))
  17465. (step (plist-get params :step))
  17466. (emph (plist-get params :emphasize))
  17467. (ts (plist-get params :tstart))
  17468. (te (plist-get params :tend))
  17469. (block (plist-get params :block))
  17470. ipos time h m p level hlc hdl
  17471. cc beg end pos tbl)
  17472. (when step
  17473. (org-clocktable-steps params)
  17474. (throw 'exit nil))
  17475. (when block
  17476. (setq cc (org-clock-special-range block nil t)
  17477. ts (car cc) te (cdr cc)))
  17478. (if ts (setq ts (time-to-seconds
  17479. (apply 'encode-time (org-parse-time-string ts)))))
  17480. (if te (setq te (time-to-seconds
  17481. (apply 'encode-time (org-parse-time-string te)))))
  17482. (move-marker ins (point))
  17483. (setq ipos (point))
  17484. ;; Get the right scope
  17485. (setq pos (point))
  17486. (save-restriction
  17487. (cond
  17488. ((not scope))
  17489. ((eq scope 'file) (widen))
  17490. ((eq scope 'subtree) (org-narrow-to-subtree))
  17491. ((eq scope 'tree)
  17492. (while (org-up-heading-safe))
  17493. (org-narrow-to-subtree))
  17494. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17495. (symbol-name scope)))
  17496. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17497. (catch 'exit
  17498. (while (org-up-heading-safe)
  17499. (looking-at outline-regexp)
  17500. (if (<= (org-reduced-level (funcall outline-level)) level)
  17501. (throw 'exit nil))))
  17502. (org-narrow-to-subtree))
  17503. ((or (listp scope) (eq scope 'agenda))
  17504. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17505. (scope 'agenda)
  17506. (p1 (copy-sequence params))
  17507. file)
  17508. (plist-put p1 :tostring t)
  17509. (plist-put p1 :multifile t)
  17510. (plist-put p1 :scope 'file)
  17511. (org-prepare-agenda-buffers files)
  17512. (while (setq file (pop files))
  17513. (with-current-buffer (find-buffer-visiting file)
  17514. (push (org-clocktable-add-file
  17515. file (org-dblock-write:clocktable p1)) tbl)
  17516. (setq total-time (+ (or total-time 0)
  17517. org-clock-file-total-minutes)))))))
  17518. (goto-char pos)
  17519. (unless (eq scope 'agenda)
  17520. (org-clock-sum ts te)
  17521. (goto-char (point-min))
  17522. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17523. (goto-char p)
  17524. (when (setq time (get-text-property p :org-clock-minutes))
  17525. (save-excursion
  17526. (beginning-of-line 1)
  17527. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17528. (setq level (org-reduced-level
  17529. (- (match-end 1) (match-beginning 1))))
  17530. (<= level maxlevel))
  17531. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17532. hdl (match-string 2)
  17533. h (/ time 60)
  17534. m (- time (* 60 h)))
  17535. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17536. (push (concat
  17537. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17538. (make-string (1- level) ?|)
  17539. hlc (format "%d:%02d" h m) hlc
  17540. " |") tbl))))))
  17541. (setq tbl (nreverse tbl))
  17542. (if tostring
  17543. (if tbl (mapconcat 'identity tbl "\n") nil)
  17544. (goto-char ins)
  17545. (insert-before-markers
  17546. (or header
  17547. (concat
  17548. "Clock summary at ["
  17549. (substring
  17550. (format-time-string (cdr org-time-stamp-formats))
  17551. 1 -1)
  17552. "]."
  17553. (if block
  17554. (format " Considered range is /%s/." block)
  17555. "")
  17556. "\n\n"))
  17557. (if (eq scope 'agenda) "|File" "")
  17558. "|L|Headline|Time|\n")
  17559. (setq total-time (or total-time org-clock-file-total-minutes)
  17560. h (/ total-time 60)
  17561. m (- total-time (* 60 h)))
  17562. (insert-before-markers
  17563. "|-\n|"
  17564. (if (eq scope 'agenda) "|" "")
  17565. "|"
  17566. "*Total time*| "
  17567. (format "*%d:%02d*" h m)
  17568. "|\n|-\n")
  17569. (setq tbl (delq nil tbl))
  17570. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17571. (equal (substring (car tbl) 0 2) "|-"))
  17572. (pop tbl))
  17573. (insert-before-markers (mapconcat
  17574. 'identity (delq nil tbl)
  17575. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17576. (backward-delete-char 1)
  17577. (goto-char ipos)
  17578. (skip-chars-forward "^|")
  17579. (org-table-align))))))
  17580. (defun org-clocktable-steps (params)
  17581. (let* ((p1 (copy-sequence params))
  17582. (ts (plist-get p1 :tstart))
  17583. (te (plist-get p1 :tend))
  17584. (step0 (plist-get p1 :step))
  17585. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17586. (block (plist-get p1 :block))
  17587. cc)
  17588. (when block
  17589. (setq cc (org-clock-special-range block nil t)
  17590. ts (car cc) te (cdr cc)))
  17591. (if ts (setq ts (time-to-seconds
  17592. (apply 'encode-time (org-parse-time-string ts)))))
  17593. (if te (setq te (time-to-seconds
  17594. (apply 'encode-time (org-parse-time-string te)))))
  17595. (plist-put p1 :header "")
  17596. (plist-put p1 :step nil)
  17597. (plist-put p1 :block nil)
  17598. (while (< ts te)
  17599. (or (bolp) (insert "\n"))
  17600. (plist-put p1 :tstart (format-time-string
  17601. (car org-time-stamp-formats)
  17602. (seconds-to-time ts)))
  17603. (plist-put p1 :tend (format-time-string
  17604. (car org-time-stamp-formats)
  17605. (seconds-to-time (setq ts (+ ts step)))))
  17606. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17607. (plist-get p1 :tstart) "\n")
  17608. (org-dblock-write:clocktable p1)
  17609. (re-search-forward "#\\+END:")
  17610. (end-of-line 0))))
  17611. (defun org-clocktable-add-file (file table)
  17612. (if table
  17613. (let ((lines (org-split-string table "\n"))
  17614. (ff (file-name-nondirectory file)))
  17615. (mapconcat 'identity
  17616. (mapcar (lambda (x)
  17617. (if (string-match org-table-dataline-regexp x)
  17618. (concat "|" ff x)
  17619. x))
  17620. lines)
  17621. "\n"))))
  17622. ;; FIXME: I don't think anybody uses this, ask David
  17623. (defun org-collect-clock-time-entries ()
  17624. "Return an internal list with clocking information.
  17625. This list has one entry for each CLOCK interval.
  17626. FIXME: describe the elements."
  17627. (interactive)
  17628. (let ((re (concat "^[ \t]*" org-clock-string
  17629. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17630. rtn beg end next cont level title total closedp leafp
  17631. clockpos titlepos h m donep)
  17632. (save-excursion
  17633. (org-clock-sum)
  17634. (goto-char (point-min))
  17635. (while (re-search-forward re nil t)
  17636. (setq clockpos (match-beginning 0)
  17637. beg (match-string 1) end (match-string 2)
  17638. cont (match-end 0))
  17639. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17640. end (apply 'encode-time (org-parse-time-string end)))
  17641. (org-back-to-heading t)
  17642. (setq donep (org-entry-is-done-p))
  17643. (setq titlepos (point)
  17644. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17645. h (/ total 60) m (- total (* 60 h))
  17646. total (cons h m))
  17647. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17648. (setq level (- (match-end 1) (match-beginning 1))
  17649. title (org-match-string-no-properties 2))
  17650. (save-excursion (outline-next-heading) (setq next (point)))
  17651. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17652. (goto-char next)
  17653. (setq leafp (and (looking-at "^\\*+ ")
  17654. (<= (- (match-end 0) (point)) level)))
  17655. (push (list beg end clockpos closedp donep
  17656. total title titlepos level leafp)
  17657. rtn)
  17658. (goto-char cont)))
  17659. (nreverse rtn)))
  17660. ;;;; Agenda, and Diary Integration
  17661. ;;; Define the Org-agenda-mode
  17662. (defvar org-agenda-mode-map (make-sparse-keymap)
  17663. "Keymap for `org-agenda-mode'.")
  17664. (defvar org-agenda-menu) ; defined later in this file.
  17665. (defvar org-agenda-follow-mode nil)
  17666. (defvar org-agenda-show-log nil)
  17667. (defvar org-agenda-redo-command nil)
  17668. (defvar org-agenda-mode-hook nil)
  17669. (defvar org-agenda-type nil)
  17670. (defvar org-agenda-force-single-file nil)
  17671. (defun org-agenda-mode ()
  17672. "Mode for time-sorted view on action items in Org-mode files.
  17673. The following commands are available:
  17674. \\{org-agenda-mode-map}"
  17675. (interactive)
  17676. (kill-all-local-variables)
  17677. (setq org-agenda-undo-list nil
  17678. org-agenda-pending-undo-list nil)
  17679. (setq major-mode 'org-agenda-mode)
  17680. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17681. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17682. (setq mode-name "Org-Agenda")
  17683. (use-local-map org-agenda-mode-map)
  17684. (easy-menu-add org-agenda-menu)
  17685. (if org-startup-truncated (setq truncate-lines t))
  17686. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17687. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17688. ;; Make sure properties are removed when copying text
  17689. (when (boundp 'buffer-substring-filters)
  17690. (org-set-local 'buffer-substring-filters
  17691. (cons (lambda (x)
  17692. (set-text-properties 0 (length x) nil x) x)
  17693. buffer-substring-filters)))
  17694. (unless org-agenda-keep-modes
  17695. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17696. org-agenda-show-log nil))
  17697. (easy-menu-change
  17698. '("Agenda") "Agenda Files"
  17699. (append
  17700. (list
  17701. (vector
  17702. (if (get 'org-agenda-files 'org-restrict)
  17703. "Restricted to single file"
  17704. "Edit File List")
  17705. '(org-edit-agenda-file-list)
  17706. (not (get 'org-agenda-files 'org-restrict)))
  17707. "--")
  17708. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17709. (org-agenda-set-mode-name)
  17710. (apply
  17711. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17712. (list 'org-agenda-mode-hook)))
  17713. (substitute-key-definition 'undo 'org-agenda-undo
  17714. org-agenda-mode-map global-map)
  17715. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17716. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17717. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17718. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17719. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17720. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17721. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17722. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17723. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17724. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17725. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17726. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17727. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17728. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17729. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17730. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17731. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17732. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17733. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17734. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17735. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17736. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17737. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17738. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17739. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17740. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17741. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17742. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17743. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17744. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17745. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17746. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17747. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17748. (while l (org-defkey org-agenda-mode-map
  17749. (int-to-string (pop l)) 'digit-argument)))
  17750. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17751. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17752. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17753. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17754. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17755. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17756. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17757. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17758. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17759. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17760. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17761. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17762. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17763. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17764. (org-defkey org-agenda-mode-map "n" 'next-line)
  17765. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17766. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17767. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17768. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17769. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17770. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17771. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17772. (eval-after-load "calendar"
  17773. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17774. 'org-calendar-goto-agenda))
  17775. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17776. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17777. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17778. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17779. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17780. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17781. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17782. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17783. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17784. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17785. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17786. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17787. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17788. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17789. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17790. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17791. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17792. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17793. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17794. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17795. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17796. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17797. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17798. "Local keymap for agenda entries from Org-mode.")
  17799. (org-defkey org-agenda-keymap
  17800. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17801. (org-defkey org-agenda-keymap
  17802. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17803. (when org-agenda-mouse-1-follows-link
  17804. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17805. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17806. '("Agenda"
  17807. ("Agenda Files")
  17808. "--"
  17809. ["Show" org-agenda-show t]
  17810. ["Go To (other window)" org-agenda-goto t]
  17811. ["Go To (this window)" org-agenda-switch-to t]
  17812. ["Follow Mode" org-agenda-follow-mode
  17813. :style toggle :selected org-agenda-follow-mode :active t]
  17814. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17815. "--"
  17816. ["Cycle TODO" org-agenda-todo t]
  17817. ["Archive subtree" org-agenda-archive t]
  17818. ["Delete subtree" org-agenda-kill t]
  17819. "--"
  17820. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17821. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17822. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17823. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17824. "--"
  17825. ("Tags and Properties"
  17826. ["Show all Tags" org-agenda-show-tags t]
  17827. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17828. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17829. "--"
  17830. ["Column View" org-columns t])
  17831. ("Date/Schedule"
  17832. ["Schedule" org-agenda-schedule t]
  17833. ["Set Deadline" org-agenda-deadline t]
  17834. "--"
  17835. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17836. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17837. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17838. ("Clock"
  17839. ["Clock in" org-agenda-clock-in t]
  17840. ["Clock out" org-agenda-clock-out t]
  17841. ["Clock cancel" org-agenda-clock-cancel t]
  17842. ["Goto running clock" org-clock-goto t])
  17843. ("Priority"
  17844. ["Set Priority" org-agenda-priority t]
  17845. ["Increase Priority" org-agenda-priority-up t]
  17846. ["Decrease Priority" org-agenda-priority-down t]
  17847. ["Show Priority" org-agenda-show-priority t])
  17848. ("Calendar/Diary"
  17849. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17850. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17851. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17852. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17853. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17854. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17855. "--"
  17856. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17857. "--"
  17858. ("View"
  17859. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17860. :style radio :selected (equal org-agenda-ndays 1)]
  17861. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17862. :style radio :selected (equal org-agenda-ndays 7)]
  17863. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17864. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17865. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17866. :style radio :selected (member org-agenda-ndays '(365 366))]
  17867. "--"
  17868. ["Show Logbook entries" org-agenda-log-mode
  17869. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17870. ["Include Diary" org-agenda-toggle-diary
  17871. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17872. ["Use Time Grid" org-agenda-toggle-time-grid
  17873. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17874. ["Write view to file" org-write-agenda t]
  17875. ["Rebuild buffer" org-agenda-redo t]
  17876. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17877. "--"
  17878. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17879. "--"
  17880. ["Quit" org-agenda-quit t]
  17881. ["Exit and Release Buffers" org-agenda-exit t]
  17882. ))
  17883. ;;; Agenda undo
  17884. (defvar org-agenda-allow-remote-undo t
  17885. "Non-nil means, allow remote undo from the agenda buffer.")
  17886. (defvar org-agenda-undo-list nil
  17887. "List of undoable operations in the agenda since last refresh.")
  17888. (defvar org-agenda-undo-has-started-in nil
  17889. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17890. (defvar org-agenda-pending-undo-list nil
  17891. "In a series of undo commands, this is the list of remaning undo items.")
  17892. (defmacro org-if-unprotected (&rest body)
  17893. "Execute BODY if there is no `org-protected' text property at point."
  17894. (declare (debug t))
  17895. `(unless (get-text-property (point) 'org-protected)
  17896. ,@body))
  17897. (defmacro org-with-remote-undo (_buffer &rest _body)
  17898. "Execute BODY while recording undo information in two buffers."
  17899. (declare (indent 1) (debug t))
  17900. `(let ((_cline (org-current-line))
  17901. (_cmd this-command)
  17902. (_buf1 (current-buffer))
  17903. (_buf2 ,_buffer)
  17904. (_undo1 buffer-undo-list)
  17905. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17906. _c1 _c2)
  17907. ,@_body
  17908. (when org-agenda-allow-remote-undo
  17909. (setq _c1 (org-verify-change-for-undo
  17910. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17911. _c2 (org-verify-change-for-undo
  17912. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17913. (when (or _c1 _c2)
  17914. ;; make sure there are undo boundaries
  17915. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17916. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17917. ;; remember which buffer to undo
  17918. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17919. org-agenda-undo-list)))))
  17920. (defun org-agenda-undo ()
  17921. "Undo a remote editing step in the agenda.
  17922. This undoes changes both in the agenda buffer and in the remote buffer
  17923. that have been changed along."
  17924. (interactive)
  17925. (or org-agenda-allow-remote-undo
  17926. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17927. (if (not (eq this-command last-command))
  17928. (setq org-agenda-undo-has-started-in nil
  17929. org-agenda-pending-undo-list org-agenda-undo-list))
  17930. (if (not org-agenda-pending-undo-list)
  17931. (error "No further undo information"))
  17932. (let* ((entry (pop org-agenda-pending-undo-list))
  17933. buf line cmd rembuf)
  17934. (setq cmd (pop entry) line (pop entry))
  17935. (setq rembuf (nth 2 entry))
  17936. (org-with-remote-undo rembuf
  17937. (while (bufferp (setq buf (pop entry)))
  17938. (if (pop entry)
  17939. (with-current-buffer buf
  17940. (let ((last-undo-buffer buf)
  17941. (inhibit-read-only t))
  17942. (unless (memq buf org-agenda-undo-has-started-in)
  17943. (push buf org-agenda-undo-has-started-in)
  17944. (make-local-variable 'pending-undo-list)
  17945. (undo-start))
  17946. (while (and pending-undo-list
  17947. (listp pending-undo-list)
  17948. (not (car pending-undo-list)))
  17949. (pop pending-undo-list))
  17950. (undo-more 1))))))
  17951. (goto-line line)
  17952. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17953. (defun org-verify-change-for-undo (l1 l2)
  17954. "Verify that a real change occurred between the undo lists L1 and L2."
  17955. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17956. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17957. (not (eq l1 l2)))
  17958. ;;; Agenda dispatch
  17959. (defvar org-agenda-restrict nil)
  17960. (defvar org-agenda-restrict-begin (make-marker))
  17961. (defvar org-agenda-restrict-end (make-marker))
  17962. (defvar org-agenda-last-dispatch-buffer nil)
  17963. (defvar org-agenda-overriding-restriction nil)
  17964. ;;;###autoload
  17965. (defun org-agenda (arg &optional keys restriction)
  17966. "Dispatch agenda commands to collect entries to the agenda buffer.
  17967. Prompts for a command to execute. Any prefix arg will be passed
  17968. on to the selected command. The default selections are:
  17969. a Call `org-agenda-list' to display the agenda for current day or week.
  17970. t Call `org-todo-list' to display the global todo list.
  17971. T Call `org-todo-list' to display the global todo list, select only
  17972. entries with a specific TODO keyword (the user gets a prompt).
  17973. m Call `org-tags-view' to display headlines with tags matching
  17974. a condition (the user is prompted for the condition).
  17975. M Like `m', but select only TODO entries, no ordinary headlines.
  17976. L Create a timeline for the current buffer.
  17977. e Export views to associated files.
  17978. More commands can be added by configuring the variable
  17979. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17980. searches can be pre-defined in this way.
  17981. If the current buffer is in Org-mode and visiting a file, you can also
  17982. first press `<' once to indicate that the agenda should be temporarily
  17983. \(until the next use of \\[org-agenda]) restricted to the current file.
  17984. Pressing `<' twice means to restrict to the current subtree or region
  17985. \(if active)."
  17986. (interactive "P")
  17987. (catch 'exit
  17988. (let* ((prefix-descriptions nil)
  17989. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17990. (org-agenda-custom-commands
  17991. ;; normalize different versions
  17992. (delq nil
  17993. (mapcar
  17994. (lambda (x)
  17995. (cond ((stringp (cdr x))
  17996. (push x prefix-descriptions)
  17997. nil)
  17998. ((stringp (nth 1 x)) x)
  17999. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18000. (t (cons (car x) (cons "" (cdr x))))))
  18001. org-agenda-custom-commands)))
  18002. (buf (current-buffer))
  18003. (bfn (buffer-file-name (buffer-base-buffer)))
  18004. entry key type match lprops ans)
  18005. ;; Turn off restriction unless there is an overriding one
  18006. (unless org-agenda-overriding-restriction
  18007. (put 'org-agenda-files 'org-restrict nil)
  18008. (setq org-agenda-restrict nil)
  18009. (move-marker org-agenda-restrict-begin nil)
  18010. (move-marker org-agenda-restrict-end nil))
  18011. ;; Delete old local properties
  18012. (put 'org-agenda-redo-command 'org-lprops nil)
  18013. ;; Remember where this call originated
  18014. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18015. (unless keys
  18016. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18017. keys (car ans)
  18018. restriction (cdr ans)))
  18019. ;; Estabish the restriction, if any
  18020. (when (and (not org-agenda-overriding-restriction) restriction)
  18021. (put 'org-agenda-files 'org-restrict (list bfn))
  18022. (cond
  18023. ((eq restriction 'region)
  18024. (setq org-agenda-restrict t)
  18025. (move-marker org-agenda-restrict-begin (region-beginning))
  18026. (move-marker org-agenda-restrict-end (region-end)))
  18027. ((eq restriction 'subtree)
  18028. (save-excursion
  18029. (setq org-agenda-restrict t)
  18030. (org-back-to-heading t)
  18031. (move-marker org-agenda-restrict-begin (point))
  18032. (move-marker org-agenda-restrict-end
  18033. (progn (org-end-of-subtree t)))))))
  18034. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18035. ;; For example the todo list should not need it (but does...)
  18036. (cond
  18037. ((setq entry (assoc keys org-agenda-custom-commands))
  18038. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18039. (progn
  18040. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18041. (put 'org-agenda-redo-command 'org-lprops lprops)
  18042. (cond
  18043. ((eq type 'agenda)
  18044. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18045. ((eq type 'alltodo)
  18046. (org-let lprops '(org-todo-list current-prefix-arg)))
  18047. ((eq type 'stuck)
  18048. (org-let lprops '(org-agenda-list-stuck-projects
  18049. current-prefix-arg)))
  18050. ((eq type 'tags)
  18051. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18052. ((eq type 'tags-todo)
  18053. (org-let lprops '(org-tags-view '(4) match)))
  18054. ((eq type 'todo)
  18055. (org-let lprops '(org-todo-list match)))
  18056. ((eq type 'tags-tree)
  18057. (org-check-for-org-mode)
  18058. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18059. ((eq type 'todo-tree)
  18060. (org-check-for-org-mode)
  18061. (org-let lprops
  18062. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18063. (regexp-quote match) "\\>"))))
  18064. ((eq type 'occur-tree)
  18065. (org-check-for-org-mode)
  18066. (org-let lprops '(org-occur match)))
  18067. ((functionp type)
  18068. (org-let lprops '(funcall type match)))
  18069. ((fboundp type)
  18070. (org-let lprops '(funcall type match)))
  18071. (t (error "Invalid custom agenda command type %s" type))))
  18072. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18073. ((equal keys "C")
  18074. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18075. (customize-variable 'org-agenda-custom-commands))
  18076. ((equal keys "a") (call-interactively 'org-agenda-list))
  18077. ((equal keys "t") (call-interactively 'org-todo-list))
  18078. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18079. ((equal keys "m") (call-interactively 'org-tags-view))
  18080. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18081. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18082. ((equal keys "L")
  18083. (unless (org-mode-p)
  18084. (error "This is not an Org-mode file"))
  18085. (unless restriction
  18086. (put 'org-agenda-files 'org-restrict (list bfn))
  18087. (org-call-with-arg 'org-timeline arg)))
  18088. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18089. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18090. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18091. (t (error "Invalid agenda key"))))))
  18092. (defun org-agenda-normalize-custom-commands (cmds)
  18093. (delq nil
  18094. (mapcar
  18095. (lambda (x)
  18096. (cond ((stringp (cdr x)) nil)
  18097. ((stringp (nth 1 x)) x)
  18098. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18099. (t (cons (car x) (cons "" (cdr x))))))
  18100. cmds)))
  18101. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18102. "The user interface for selecting an agenda command."
  18103. (catch 'exit
  18104. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18105. (restrict-ok (and bfn (org-mode-p)))
  18106. (region-p (org-region-active-p))
  18107. (custom org-agenda-custom-commands)
  18108. (selstring "")
  18109. restriction second-time
  18110. c entry key type match prefixes rmheader header-end custom1 desc)
  18111. (save-window-excursion
  18112. (delete-other-windows)
  18113. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18114. (erase-buffer)
  18115. (insert (eval-when-compile
  18116. (let ((header
  18117. "
  18118. Press key for an agenda command: < Buffer,subtree/region restriction
  18119. -------------------------------- > Remove restriction
  18120. a Agenda for current week or day e Export agenda views
  18121. t List of all TODO entries T Entries with special TODO kwd
  18122. m Match a TAGS query M Like m, but only TODO entries
  18123. L Timeline for current buffer # List stuck projects (!=configure)
  18124. / Multi-occur C Configure custom agenda commands
  18125. ")
  18126. (start 0))
  18127. (while (string-match
  18128. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18129. header start)
  18130. (setq start (match-end 0))
  18131. (add-text-properties (match-beginning 2) (match-end 2)
  18132. '(face bold) header))
  18133. header)))
  18134. (setq header-end (move-marker (make-marker) (point)))
  18135. (while t
  18136. (setq custom1 custom)
  18137. (when (eq rmheader t)
  18138. (goto-line 1)
  18139. (re-search-forward ":" nil t)
  18140. (delete-region (match-end 0) (point-at-eol))
  18141. (forward-char 1)
  18142. (looking-at "-+")
  18143. (delete-region (match-end 0) (point-at-eol))
  18144. (move-marker header-end (match-end 0)))
  18145. (goto-char header-end)
  18146. (delete-region (point) (point-max))
  18147. (while (setq entry (pop custom1))
  18148. (setq key (car entry) desc (nth 1 entry)
  18149. type (nth 2 entry) match (nth 3 entry))
  18150. (if (> (length key) 1)
  18151. (add-to-list 'prefixes (string-to-char key))
  18152. (insert
  18153. (format
  18154. "\n%-4s%-14s: %s"
  18155. (org-add-props (copy-sequence key)
  18156. '(face bold))
  18157. (cond
  18158. ((string-match "\\S-" desc) desc)
  18159. ((eq type 'agenda) "Agenda for current week or day")
  18160. ((eq type 'alltodo) "List of all TODO entries")
  18161. ((eq type 'stuck) "List of stuck projects")
  18162. ((eq type 'todo) "TODO keyword")
  18163. ((eq type 'tags) "Tags query")
  18164. ((eq type 'tags-todo) "Tags (TODO)")
  18165. ((eq type 'tags-tree) "Tags tree")
  18166. ((eq type 'todo-tree) "TODO kwd tree")
  18167. ((eq type 'occur-tree) "Occur tree")
  18168. ((functionp type) (if (symbolp type)
  18169. (symbol-name type)
  18170. "Lambda expression"))
  18171. (t "???"))
  18172. (cond
  18173. ((stringp match)
  18174. (org-add-props match nil 'face 'org-warning))
  18175. (match
  18176. (format "set of %d commands" (length match)))
  18177. (t ""))))))
  18178. (when prefixes
  18179. (mapc (lambda (x)
  18180. (insert
  18181. (format "\n%s %s"
  18182. (org-add-props (char-to-string x)
  18183. nil 'face 'bold)
  18184. (or (cdr (assoc (concat selstring (char-to-string x))
  18185. prefix-descriptions))
  18186. "Prefix key"))))
  18187. prefixes))
  18188. (goto-char (point-min))
  18189. (when (fboundp 'fit-window-to-buffer)
  18190. (if second-time
  18191. (if (not (pos-visible-in-window-p (point-max)))
  18192. (fit-window-to-buffer))
  18193. (setq second-time t)
  18194. (fit-window-to-buffer)))
  18195. (message "Press key for agenda command%s:"
  18196. (if (or restrict-ok org-agenda-overriding-restriction)
  18197. (if org-agenda-overriding-restriction
  18198. " (restriction lock active)"
  18199. (if restriction
  18200. (format " (restricted to %s)" restriction)
  18201. " (unrestricted)"))
  18202. ""))
  18203. (setq c (read-char-exclusive))
  18204. (message "")
  18205. (cond
  18206. ((assoc (char-to-string c) custom)
  18207. (setq selstring (concat selstring (char-to-string c)))
  18208. (throw 'exit (cons selstring restriction)))
  18209. ((memq c prefixes)
  18210. (setq selstring (concat selstring (char-to-string c))
  18211. prefixes nil
  18212. rmheader (or rmheader t)
  18213. custom (delq nil (mapcar
  18214. (lambda (x)
  18215. (if (or (= (length (car x)) 1)
  18216. (/= (string-to-char (car x)) c))
  18217. nil
  18218. (cons (substring (car x) 1) (cdr x))))
  18219. custom))))
  18220. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18221. (message "Restriction is only possible in Org-mode buffers")
  18222. (ding) (sit-for 1))
  18223. ((eq c ?1)
  18224. (org-agenda-remove-restriction-lock 'noupdate)
  18225. (setq restriction 'buffer))
  18226. ((eq c ?0)
  18227. (org-agenda-remove-restriction-lock 'noupdate)
  18228. (setq restriction (if region-p 'region 'subtree)))
  18229. ((eq c ?<)
  18230. (org-agenda-remove-restriction-lock 'noupdate)
  18231. (setq restriction
  18232. (cond
  18233. ((eq restriction 'buffer)
  18234. (if region-p 'region 'subtree))
  18235. ((memq restriction '(subtree region))
  18236. nil)
  18237. (t 'buffer))))
  18238. ((eq c ?>)
  18239. (org-agenda-remove-restriction-lock 'noupdate)
  18240. (setq restriction nil))
  18241. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18242. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18243. ((and (> (length selstring) 0) (eq c ?\d))
  18244. (delete-window)
  18245. (org-agenda-get-restriction-and-command prefix-descriptions))
  18246. ((equal c ?q) (error "Abort"))
  18247. (t (error "Invalid key %c" c))))))))
  18248. (defun org-run-agenda-series (name series)
  18249. (org-prepare-agenda name)
  18250. (let* ((org-agenda-multi t)
  18251. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18252. (cmds (car series))
  18253. (gprops (nth 1 series))
  18254. match ;; The byte compiler incorrectly complains about this. Keep it!
  18255. cmd type lprops)
  18256. (while (setq cmd (pop cmds))
  18257. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18258. (cond
  18259. ((eq type 'agenda)
  18260. (org-let2 gprops lprops
  18261. '(call-interactively 'org-agenda-list)))
  18262. ((eq type 'alltodo)
  18263. (org-let2 gprops lprops
  18264. '(call-interactively 'org-todo-list)))
  18265. ((eq type 'stuck)
  18266. (org-let2 gprops lprops
  18267. '(call-interactively 'org-agenda-list-stuck-projects)))
  18268. ((eq type 'tags)
  18269. (org-let2 gprops lprops
  18270. '(org-tags-view current-prefix-arg match)))
  18271. ((eq type 'tags-todo)
  18272. (org-let2 gprops lprops
  18273. '(org-tags-view '(4) match)))
  18274. ((eq type 'todo)
  18275. (org-let2 gprops lprops
  18276. '(org-todo-list match)))
  18277. ((fboundp type)
  18278. (org-let2 gprops lprops
  18279. '(funcall type match)))
  18280. (t (error "Invalid type in command series"))))
  18281. (widen)
  18282. (setq org-agenda-redo-command redo)
  18283. (goto-char (point-min)))
  18284. (org-finalize-agenda))
  18285. ;;;###autoload
  18286. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18287. "Run an agenda command in batch mode and send the result to STDOUT.
  18288. If CMD-KEY is a string of length 1, it is used as a key in
  18289. `org-agenda-custom-commands' and triggers this command. If it is a
  18290. longer string it is used as a tags/todo match string.
  18291. Paramters are alternating variable names and values that will be bound
  18292. before running the agenda command."
  18293. (let (pars)
  18294. (while parameters
  18295. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18296. (if (> (length cmd-key) 2)
  18297. (eval (list 'let (nreverse pars)
  18298. (list 'org-tags-view nil cmd-key)))
  18299. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18300. (set-buffer org-agenda-buffer-name)
  18301. (princ (org-encode-for-stdout (buffer-string)))))
  18302. (defun org-encode-for-stdout (string)
  18303. (if (fboundp 'encode-coding-string)
  18304. (encode-coding-string string buffer-file-coding-system)
  18305. string))
  18306. (defvar org-agenda-info nil)
  18307. ;;;###autoload
  18308. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18309. "Run an agenda command in batch mode and send the result to STDOUT.
  18310. If CMD-KEY is a string of length 1, it is used as a key in
  18311. `org-agenda-custom-commands' and triggers this command. If it is a
  18312. longer string it is used as a tags/todo match string.
  18313. Paramters are alternating variable names and values that will be bound
  18314. before running the agenda command.
  18315. The output gives a line for each selected agenda item. Each
  18316. item is a list of comma-separated values, like this:
  18317. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18318. category The category of the item
  18319. head The headline, without TODO kwd, TAGS and PRIORITY
  18320. type The type of the agenda entry, can be
  18321. todo selected in TODO match
  18322. tagsmatch selected in tags match
  18323. diary imported from diary
  18324. deadline a deadline on given date
  18325. scheduled scheduled on given date
  18326. timestamp entry has timestamp on given date
  18327. closed entry was closed on given date
  18328. upcoming-deadline warning about deadline
  18329. past-scheduled forwarded scheduled item
  18330. block entry has date block including g. date
  18331. todo The todo keyword, if any
  18332. tags All tags including inherited ones, separated by colons
  18333. date The relevant date, like 2007-2-14
  18334. time The time, like 15:00-16:50
  18335. extra Sting with extra planning info
  18336. priority-l The priority letter if any was given
  18337. priority-n The computed numerical priority
  18338. agenda-day The day in the agenda where this is listed"
  18339. (let (pars)
  18340. (while parameters
  18341. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18342. (push (list 'org-agenda-remove-tags t) pars)
  18343. (if (> (length cmd-key) 2)
  18344. (eval (list 'let (nreverse pars)
  18345. (list 'org-tags-view nil cmd-key)))
  18346. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18347. (set-buffer org-agenda-buffer-name)
  18348. (let* ((lines (org-split-string (buffer-string) "\n"))
  18349. line)
  18350. (while (setq line (pop lines))
  18351. (catch 'next
  18352. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18353. (setq org-agenda-info
  18354. (org-fix-agenda-info (text-properties-at 0 line)))
  18355. (princ
  18356. (org-encode-for-stdout
  18357. (mapconcat 'org-agenda-export-csv-mapper
  18358. '(org-category txt type todo tags date time-of-day extra
  18359. priority-letter priority agenda-day)
  18360. ",")))
  18361. (princ "\n"))))))
  18362. (defun org-fix-agenda-info (props)
  18363. "Make sure all properties on an agenda item have a canonical form,
  18364. so the export commands can easily use it."
  18365. (let (tmp re)
  18366. (when (setq tmp (plist-get props 'tags))
  18367. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18368. (when (setq tmp (plist-get props 'date))
  18369. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18370. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18371. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18372. (setq tmp (calendar-date-string tmp)))
  18373. (setq props (plist-put props 'date tmp)))
  18374. (when (setq tmp (plist-get props 'day))
  18375. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18376. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18377. (setq tmp (calendar-date-string tmp)))
  18378. (setq props (plist-put props 'day tmp))
  18379. (setq props (plist-put props 'agenda-day tmp)))
  18380. (when (setq tmp (plist-get props 'txt))
  18381. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18382. (plist-put props 'priority-letter (match-string 1 tmp))
  18383. (setq tmp (replace-match "" t t tmp)))
  18384. (when (and (setq re (plist-get props 'org-todo-regexp))
  18385. (setq re (concat "\\`\\.*" re " ?"))
  18386. (string-match re tmp))
  18387. (plist-put props 'todo (match-string 1 tmp))
  18388. (setq tmp (replace-match "" t t tmp)))
  18389. (plist-put props 'txt tmp)))
  18390. props)
  18391. (defun org-agenda-export-csv-mapper (prop)
  18392. (let ((res (plist-get org-agenda-info prop)))
  18393. (setq res
  18394. (cond
  18395. ((not res) "")
  18396. ((stringp res) res)
  18397. (t (prin1-to-string res))))
  18398. (while (string-match "," res)
  18399. (setq res (replace-match ";" t t res)))
  18400. (org-trim res)))
  18401. ;;;###autoload
  18402. (defun org-store-agenda-views (&rest parameters)
  18403. (interactive)
  18404. (eval (list 'org-batch-store-agenda-views)))
  18405. ;; FIXME, why is this a macro?????
  18406. ;;;###autoload
  18407. (defmacro org-batch-store-agenda-views (&rest parameters)
  18408. "Run all custom agenda commands that have a file argument."
  18409. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18410. (pop-up-frames nil)
  18411. (dir default-directory)
  18412. pars cmd thiscmdkey files opts)
  18413. (while parameters
  18414. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18415. (setq pars (reverse pars))
  18416. (save-window-excursion
  18417. (while cmds
  18418. (setq cmd (pop cmds)
  18419. thiscmdkey (car cmd)
  18420. opts (nth 4 cmd)
  18421. files (nth 5 cmd))
  18422. (if (stringp files) (setq files (list files)))
  18423. (when files
  18424. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18425. (list 'org-agenda nil thiscmdkey)))
  18426. (set-buffer org-agenda-buffer-name)
  18427. (while files
  18428. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18429. (list 'org-write-agenda
  18430. (expand-file-name (pop files) dir) t))))
  18431. (and (get-buffer org-agenda-buffer-name)
  18432. (kill-buffer org-agenda-buffer-name)))))))
  18433. (defun org-write-agenda (file &optional nosettings)
  18434. "Write the current buffer (an agenda view) as a file.
  18435. Depending on the extension of the file name, plain text (.txt),
  18436. HTML (.html or .htm) or Postscript (.ps) is produced.
  18437. If NOSETTINGS is given, do not scope the settings of
  18438. `org-agenda-exporter-settings' into the export commands. This is used when
  18439. the settings have already been scoped and we do not wish to overrule other,
  18440. higher priority settings."
  18441. (interactive "FWrite agenda to file: ")
  18442. (if (not (file-writable-p file))
  18443. (error "Cannot write agenda to file %s" file))
  18444. (cond
  18445. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18446. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18447. (org-let (if nosettings nil org-agenda-exporter-settings)
  18448. '(save-excursion
  18449. (save-window-excursion
  18450. (cond
  18451. ((string-match "\\.html?\\'" file)
  18452. (set-buffer (htmlize-buffer (current-buffer)))
  18453. (when (and org-agenda-export-html-style
  18454. (string-match "<style>" org-agenda-export-html-style))
  18455. ;; replace <style> section with org-agenda-export-html-style
  18456. (goto-char (point-min))
  18457. (kill-region (- (search-forward "<style") 6)
  18458. (search-forward "</style>"))
  18459. (insert org-agenda-export-html-style))
  18460. (write-file file)
  18461. (kill-buffer (current-buffer))
  18462. (message "HTML written to %s" file))
  18463. ((string-match "\\.ps\\'" file)
  18464. (ps-print-buffer-with-faces file)
  18465. (message "Postscript written to %s" file))
  18466. (t
  18467. (let ((bs (buffer-string)))
  18468. (find-file file)
  18469. (insert bs)
  18470. (save-buffer 0)
  18471. (kill-buffer (current-buffer))
  18472. (message "Plain text written to %s" file))))))
  18473. (set-buffer org-agenda-buffer-name)))
  18474. (defmacro org-no-read-only (&rest body)
  18475. "Inhibit read-only for BODY."
  18476. `(let ((inhibit-read-only t)) ,@body))
  18477. (defun org-check-for-org-mode ()
  18478. "Make sure current buffer is in org-mode. Error if not."
  18479. (or (org-mode-p)
  18480. (error "Cannot execute org-mode agenda command on buffer in %s."
  18481. major-mode)))
  18482. (defun org-fit-agenda-window ()
  18483. "Fit the window to the buffer size."
  18484. (and (memq org-agenda-window-setup '(reorganize-frame))
  18485. (fboundp 'fit-window-to-buffer)
  18486. (fit-window-to-buffer
  18487. nil
  18488. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18489. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18490. ;;; Agenda file list
  18491. (defun org-agenda-files (&optional unrestricted)
  18492. "Get the list of agenda files.
  18493. Optional UNRESTRICTED means return the full list even if a restriction
  18494. is currently in place."
  18495. (let ((files
  18496. (cond
  18497. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18498. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18499. ((listp org-agenda-files) org-agenda-files)
  18500. (t (error "Invalid value of `org-agenda-files'")))))
  18501. (setq files (apply 'append
  18502. (mapcar (lambda (f)
  18503. (if (file-directory-p f)
  18504. (directory-files f t
  18505. org-agenda-file-regexp)
  18506. (list f)))
  18507. files)))
  18508. (if org-agenda-skip-unavailable-files
  18509. (delq nil
  18510. (mapcar (function
  18511. (lambda (file)
  18512. (and (file-readable-p file) file)))
  18513. files))
  18514. files))) ; `org-check-agenda-file' will remove them from the list
  18515. (defun org-edit-agenda-file-list ()
  18516. "Edit the list of agenda files.
  18517. Depending on setup, this either uses customize to edit the variable
  18518. `org-agenda-files', or it visits the file that is holding the list. In the
  18519. latter case, the buffer is set up in a way that saving it automatically kills
  18520. the buffer and restores the previous window configuration."
  18521. (interactive)
  18522. (if (stringp org-agenda-files)
  18523. (let ((cw (current-window-configuration)))
  18524. (find-file org-agenda-files)
  18525. (org-set-local 'org-window-configuration cw)
  18526. (org-add-hook 'after-save-hook
  18527. (lambda ()
  18528. (set-window-configuration
  18529. (prog1 org-window-configuration
  18530. (kill-buffer (current-buffer))))
  18531. (org-install-agenda-files-menu)
  18532. (message "New agenda file list installed"))
  18533. nil 'local)
  18534. (message "%s" (substitute-command-keys
  18535. "Edit list and finish with \\[save-buffer]")))
  18536. (customize-variable 'org-agenda-files)))
  18537. (defun org-store-new-agenda-file-list (list)
  18538. "Set new value for the agenda file list and save it correcly."
  18539. (if (stringp org-agenda-files)
  18540. (let ((f org-agenda-files) b)
  18541. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18542. (with-temp-file f
  18543. (insert (mapconcat 'identity list "\n") "\n")))
  18544. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18545. (setq org-agenda-files list)
  18546. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18547. (defun org-read-agenda-file-list ()
  18548. "Read the list of agenda files from a file."
  18549. (when (stringp org-agenda-files)
  18550. (with-temp-buffer
  18551. (insert-file-contents org-agenda-files)
  18552. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18553. ;;;###autoload
  18554. (defun org-cycle-agenda-files ()
  18555. "Cycle through the files in `org-agenda-files'.
  18556. If the current buffer visits an agenda file, find the next one in the list.
  18557. If the current buffer does not, find the first agenda file."
  18558. (interactive)
  18559. (let* ((fs (org-agenda-files t))
  18560. (files (append fs (list (car fs))))
  18561. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18562. file)
  18563. (unless files (error "No agenda files"))
  18564. (catch 'exit
  18565. (while (setq file (pop files))
  18566. (if (equal (file-truename file) tcf)
  18567. (when (car files)
  18568. (find-file (car files))
  18569. (throw 'exit t))))
  18570. (find-file (car fs)))
  18571. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18572. (defun org-agenda-file-to-front (&optional to-end)
  18573. "Move/add the current file to the top of the agenda file list.
  18574. If the file is not present in the list, it is added to the front. If it is
  18575. present, it is moved there. With optional argument TO-END, add/move to the
  18576. end of the list."
  18577. (interactive "P")
  18578. (let ((org-agenda-skip-unavailable-files nil)
  18579. (file-alist (mapcar (lambda (x)
  18580. (cons (file-truename x) x))
  18581. (org-agenda-files t)))
  18582. (ctf (file-truename buffer-file-name))
  18583. x had)
  18584. (setq x (assoc ctf file-alist) had x)
  18585. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18586. (if to-end
  18587. (setq file-alist (append (delq x file-alist) (list x)))
  18588. (setq file-alist (cons x (delq x file-alist))))
  18589. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18590. (org-install-agenda-files-menu)
  18591. (message "File %s to %s of agenda file list"
  18592. (if had "moved" "added") (if to-end "end" "front"))))
  18593. (defun org-remove-file (&optional file)
  18594. "Remove current file from the list of files in variable `org-agenda-files'.
  18595. These are the files which are being checked for agenda entries.
  18596. Optional argument FILE means, use this file instead of the current."
  18597. (interactive)
  18598. (let* ((org-agenda-skip-unavailable-files nil)
  18599. (file (or file buffer-file-name))
  18600. (true-file (file-truename file))
  18601. (afile (abbreviate-file-name file))
  18602. (files (delq nil (mapcar
  18603. (lambda (x)
  18604. (if (equal true-file
  18605. (file-truename x))
  18606. nil x))
  18607. (org-agenda-files t)))))
  18608. (if (not (= (length files) (length (org-agenda-files t))))
  18609. (progn
  18610. (org-store-new-agenda-file-list files)
  18611. (org-install-agenda-files-menu)
  18612. (message "Removed file: %s" afile))
  18613. (message "File was not in list: %s (not removed)" afile))))
  18614. (defun org-file-menu-entry (file)
  18615. (vector file (list 'find-file file) t))
  18616. (defun org-check-agenda-file (file)
  18617. "Make sure FILE exists. If not, ask user what to do."
  18618. (when (not (file-exists-p file))
  18619. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18620. (abbreviate-file-name file))
  18621. (let ((r (downcase (read-char-exclusive))))
  18622. (cond
  18623. ((equal r ?r)
  18624. (org-remove-file file)
  18625. (throw 'nextfile t))
  18626. (t (error "Abort"))))))
  18627. ;;; Agenda prepare and finalize
  18628. (defvar org-agenda-multi nil) ; dynammically scoped
  18629. (defvar org-agenda-buffer-name "*Org Agenda*")
  18630. (defvar org-pre-agenda-window-conf nil)
  18631. (defvar org-agenda-name nil)
  18632. (defun org-prepare-agenda (&optional name)
  18633. (setq org-todo-keywords-for-agenda nil)
  18634. (setq org-done-keywords-for-agenda nil)
  18635. (if org-agenda-multi
  18636. (progn
  18637. (setq buffer-read-only nil)
  18638. (goto-char (point-max))
  18639. (unless (or (bobp) org-agenda-compact-blocks)
  18640. (insert "\n" (make-string (window-width) ?=) "\n"))
  18641. (narrow-to-region (point) (point-max)))
  18642. (org-agenda-reset-markers)
  18643. (org-prepare-agenda-buffers (org-agenda-files))
  18644. (setq org-todo-keywords-for-agenda
  18645. (org-uniquify org-todo-keywords-for-agenda))
  18646. (setq org-done-keywords-for-agenda
  18647. (org-uniquify org-done-keywords-for-agenda))
  18648. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18649. (awin (get-buffer-window abuf)))
  18650. (cond
  18651. ((equal (current-buffer) abuf) nil)
  18652. (awin (select-window awin))
  18653. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18654. ((equal org-agenda-window-setup 'current-window)
  18655. (switch-to-buffer abuf))
  18656. ((equal org-agenda-window-setup 'other-window)
  18657. (org-switch-to-buffer-other-window abuf))
  18658. ((equal org-agenda-window-setup 'other-frame)
  18659. (switch-to-buffer-other-frame abuf))
  18660. ((equal org-agenda-window-setup 'reorganize-frame)
  18661. (delete-other-windows)
  18662. (org-switch-to-buffer-other-window abuf))))
  18663. (setq buffer-read-only nil)
  18664. (erase-buffer)
  18665. (org-agenda-mode)
  18666. (and name (not org-agenda-name)
  18667. (org-set-local 'org-agenda-name name)))
  18668. (setq buffer-read-only nil))
  18669. (defun org-finalize-agenda ()
  18670. "Finishing touch for the agenda buffer, called just before displaying it."
  18671. (unless org-agenda-multi
  18672. (save-excursion
  18673. (let ((inhibit-read-only t))
  18674. (goto-char (point-min))
  18675. (while (org-activate-bracket-links (point-max))
  18676. (add-text-properties (match-beginning 0) (match-end 0)
  18677. '(face org-link)))
  18678. (org-agenda-align-tags)
  18679. (unless org-agenda-with-colors
  18680. (remove-text-properties (point-min) (point-max) '(face nil))))
  18681. (if (and (boundp 'org-overriding-columns-format)
  18682. org-overriding-columns-format)
  18683. (org-set-local 'org-overriding-columns-format
  18684. org-overriding-columns-format))
  18685. (if (and (boundp 'org-agenda-view-columns-initially)
  18686. org-agenda-view-columns-initially)
  18687. (org-agenda-columns))
  18688. (when org-agenda-fontify-priorities
  18689. (org-fontify-priorities))
  18690. (run-hooks 'org-finalize-agenda-hook)
  18691. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18692. )))
  18693. (defun org-fontify-priorities ()
  18694. "Make highest priority lines bold, and lowest italic."
  18695. (interactive)
  18696. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18697. (org-delete-overlay o)))
  18698. (org-overlays-in (point-min) (point-max)))
  18699. (save-excursion
  18700. (let ((inhibit-read-only t)
  18701. b e p ov h l)
  18702. (goto-char (point-min))
  18703. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18704. (setq h (or (get-char-property (point) 'org-highest-priority)
  18705. org-highest-priority)
  18706. l (or (get-char-property (point) 'org-lowest-priority)
  18707. org-lowest-priority)
  18708. p (string-to-char (match-string 1))
  18709. b (match-beginning 0) e (point-at-eol)
  18710. ov (org-make-overlay b e))
  18711. (org-overlay-put
  18712. ov 'face
  18713. (cond ((listp org-agenda-fontify-priorities)
  18714. (cdr (assoc p org-agenda-fontify-priorities)))
  18715. ((equal p l) 'italic)
  18716. ((equal p h) 'bold)))
  18717. (org-overlay-put ov 'org-type 'org-priority)))))
  18718. (defun org-prepare-agenda-buffers (files)
  18719. "Create buffers for all agenda files, protect archived trees and comments."
  18720. (interactive)
  18721. (let ((pa '(:org-archived t))
  18722. (pc '(:org-comment t))
  18723. (pall '(:org-archived t :org-comment t))
  18724. (inhibit-read-only t)
  18725. (rea (concat ":" org-archive-tag ":"))
  18726. bmp file re)
  18727. (save-excursion
  18728. (save-restriction
  18729. (while (setq file (pop files))
  18730. (if (bufferp file)
  18731. (set-buffer file)
  18732. (org-check-agenda-file file)
  18733. (set-buffer (org-get-agenda-file-buffer file)))
  18734. (widen)
  18735. (setq bmp (buffer-modified-p))
  18736. (org-refresh-category-properties)
  18737. (setq org-todo-keywords-for-agenda
  18738. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18739. (setq org-done-keywords-for-agenda
  18740. (append org-done-keywords-for-agenda org-done-keywords))
  18741. (save-excursion
  18742. (remove-text-properties (point-min) (point-max) pall)
  18743. (when org-agenda-skip-archived-trees
  18744. (goto-char (point-min))
  18745. (while (re-search-forward rea nil t)
  18746. (if (org-on-heading-p t)
  18747. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18748. (goto-char (point-min))
  18749. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18750. (while (re-search-forward re nil t)
  18751. (add-text-properties
  18752. (match-beginning 0) (org-end-of-subtree t) pc)))
  18753. (set-buffer-modified-p bmp))))))
  18754. (defvar org-agenda-skip-function nil
  18755. "Function to be called at each match during agenda construction.
  18756. If this function returns nil, the current match should not be skipped.
  18757. Otherwise, the function must return a position from where the search
  18758. should be continued.
  18759. This may also be a Lisp form, it will be evaluated.
  18760. Never set this variable using `setq' or so, because then it will apply
  18761. to all future agenda commands. Instead, bind it with `let' to scope
  18762. it dynamically into the agenda-constructing command. A good way to set
  18763. it is through options in org-agenda-custom-commands.")
  18764. (defun org-agenda-skip ()
  18765. "Throw to `:skip' in places that should be skipped.
  18766. Also moves point to the end of the skipped region, so that search can
  18767. continue from there."
  18768. (let ((p (point-at-bol)) to fp)
  18769. (and org-agenda-skip-archived-trees
  18770. (get-text-property p :org-archived)
  18771. (org-end-of-subtree t)
  18772. (throw :skip t))
  18773. (and (get-text-property p :org-comment)
  18774. (org-end-of-subtree t)
  18775. (throw :skip t))
  18776. (if (equal (char-after p) ?#) (throw :skip t))
  18777. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18778. (consp org-agenda-skip-function))
  18779. (setq to (save-excursion
  18780. (save-match-data
  18781. (if fp
  18782. (funcall org-agenda-skip-function)
  18783. (eval org-agenda-skip-function))))))
  18784. (goto-char to)
  18785. (throw :skip t))))
  18786. (defvar org-agenda-markers nil
  18787. "List of all currently active markers created by `org-agenda'.")
  18788. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18789. "Creation time of the last agenda marker.")
  18790. (defun org-agenda-new-marker (&optional pos)
  18791. "Return a new agenda marker.
  18792. Org-mode keeps a list of these markers and resets them when they are
  18793. no longer in use."
  18794. (let ((m (copy-marker (or pos (point)))))
  18795. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18796. (push m org-agenda-markers)
  18797. m))
  18798. (defun org-agenda-reset-markers ()
  18799. "Reset markers created by `org-agenda'."
  18800. (while org-agenda-markers
  18801. (move-marker (pop org-agenda-markers) nil)))
  18802. (defun org-get-agenda-file-buffer (file)
  18803. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18804. it to the list of buffers which might be released later."
  18805. (let ((buf (org-find-base-buffer-visiting file)))
  18806. (if buf
  18807. buf ; just return it
  18808. ;; Make a new buffer and remember it
  18809. (setq buf (find-file-noselect file))
  18810. (if buf (push buf org-agenda-new-buffers))
  18811. buf)))
  18812. (defun org-release-buffers (blist)
  18813. "Release all buffers in list, asking the user for confirmation when needed.
  18814. When a buffer is unmodified, it is just killed. When modified, it is saved
  18815. \(if the user agrees) and then killed."
  18816. (let (buf file)
  18817. (while (setq buf (pop blist))
  18818. (setq file (buffer-file-name buf))
  18819. (when (and (buffer-modified-p buf)
  18820. file
  18821. (y-or-n-p (format "Save file %s? " file)))
  18822. (with-current-buffer buf (save-buffer)))
  18823. (kill-buffer buf))))
  18824. (defun org-get-category (&optional pos)
  18825. "Get the category applying to position POS."
  18826. (get-text-property (or pos (point)) 'org-category))
  18827. ;;; Agenda timeline
  18828. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18829. (defun org-timeline (&optional include-all)
  18830. "Show a time-sorted view of the entries in the current org file.
  18831. Only entries with a time stamp of today or later will be listed. With
  18832. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18833. under the current date.
  18834. If the buffer contains an active region, only check the region for
  18835. dates."
  18836. (interactive "P")
  18837. (require 'calendar)
  18838. (org-compile-prefix-format 'timeline)
  18839. (org-set-sorting-strategy 'timeline)
  18840. (let* ((dopast t)
  18841. (dotodo include-all)
  18842. (doclosed org-agenda-show-log)
  18843. (entry buffer-file-name)
  18844. (date (calendar-current-date))
  18845. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18846. (end (if (org-region-active-p) (region-end) (point-max)))
  18847. (day-numbers (org-get-all-dates beg end 'no-ranges
  18848. t doclosed ; always include today
  18849. org-timeline-show-empty-dates))
  18850. (org-deadline-warning-days 0)
  18851. (org-agenda-only-exact-dates t)
  18852. (today (time-to-days (current-time)))
  18853. (past t)
  18854. args
  18855. s e rtn d emptyp)
  18856. (setq org-agenda-redo-command
  18857. (list 'progn
  18858. (list 'org-switch-to-buffer-other-window (current-buffer))
  18859. (list 'org-timeline (list 'quote include-all))))
  18860. (if (not dopast)
  18861. ;; Remove past dates from the list of dates.
  18862. (setq day-numbers (delq nil (mapcar (lambda(x)
  18863. (if (>= x today) x nil))
  18864. day-numbers))))
  18865. (org-prepare-agenda (concat "Timeline "
  18866. (file-name-nondirectory buffer-file-name)))
  18867. (if doclosed (push :closed args))
  18868. (push :timestamp args)
  18869. (push :deadline args)
  18870. (push :scheduled args)
  18871. (push :sexp args)
  18872. (if dotodo (push :todo args))
  18873. (while (setq d (pop day-numbers))
  18874. (if (and (listp d) (eq (car d) :omitted))
  18875. (progn
  18876. (setq s (point))
  18877. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18878. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18879. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18880. (if (and (>= d today)
  18881. dopast
  18882. past)
  18883. (progn
  18884. (setq past nil)
  18885. (insert (make-string 79 ?-) "\n")))
  18886. (setq date (calendar-gregorian-from-absolute d))
  18887. (setq s (point))
  18888. (setq rtn (and (not emptyp)
  18889. (apply 'org-agenda-get-day-entries entry
  18890. date args)))
  18891. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18892. (progn
  18893. (insert
  18894. (if (stringp org-agenda-format-date)
  18895. (format-time-string org-agenda-format-date
  18896. (org-time-from-absolute date))
  18897. (funcall org-agenda-format-date date))
  18898. "\n")
  18899. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18900. (put-text-property s (1- (point)) 'org-date-line t)
  18901. (if (equal d today)
  18902. (put-text-property s (1- (point)) 'org-today t))
  18903. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18904. (put-text-property s (1- (point)) 'day d)))))
  18905. (goto-char (point-min))
  18906. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18907. (point-min)))
  18908. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18909. (org-finalize-agenda)
  18910. (setq buffer-read-only t)))
  18911. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18912. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18913. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18914. not every single day in the range. If FORCE-TODAY is non-nil, make
  18915. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18916. inactive time stamps (those in square brackets) are included.
  18917. When EMPTY is non-nil, also include days without any entries."
  18918. (let ((re (concat
  18919. (if pre-re pre-re "")
  18920. (if inactive org-ts-regexp-both org-ts-regexp)))
  18921. dates dates1 date day day1 day2 ts1 ts2)
  18922. (if force-today
  18923. (setq dates (list (time-to-days (current-time)))))
  18924. (save-excursion
  18925. (goto-char beg)
  18926. (while (re-search-forward re end t)
  18927. (setq day (time-to-days (org-time-string-to-time
  18928. (substring (match-string 1) 0 10))))
  18929. (or (memq day dates) (push day dates)))
  18930. (unless no-ranges
  18931. (goto-char beg)
  18932. (while (re-search-forward org-tr-regexp end t)
  18933. (setq ts1 (substring (match-string 1) 0 10)
  18934. ts2 (substring (match-string 2) 0 10)
  18935. day1 (time-to-days (org-time-string-to-time ts1))
  18936. day2 (time-to-days (org-time-string-to-time ts2)))
  18937. (while (< (setq day1 (1+ day1)) day2)
  18938. (or (memq day1 dates) (push day1 dates)))))
  18939. (setq dates (sort dates '<))
  18940. (when empty
  18941. (while (setq day (pop dates))
  18942. (setq day2 (car dates))
  18943. (push day dates1)
  18944. (when (and day2 empty)
  18945. (if (or (eq empty t)
  18946. (and (numberp empty) (<= (- day2 day) empty)))
  18947. (while (< (setq day (1+ day)) day2)
  18948. (push (list day) dates1))
  18949. (push (cons :omitted (- day2 day)) dates1))))
  18950. (setq dates (nreverse dates1)))
  18951. dates)))
  18952. ;;; Agenda Daily/Weekly
  18953. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18954. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18955. (defvar org-agenda-last-arguments nil
  18956. "The arguments of the previous call to org-agenda")
  18957. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18958. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18959. (defvar org-include-all-loc nil) ; local variable
  18960. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18961. ;;;###autoload
  18962. (defun org-agenda-list (&optional include-all start-day ndays)
  18963. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18964. The view will be for the current day or week, but from the overview buffer
  18965. you will be able to go to other days/weeks.
  18966. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18967. all unfinished TODO items will also be shown, before the agenda.
  18968. This feature is considered obsolete, please use the TODO list or a block
  18969. agenda instead.
  18970. With a numeric prefix argument in an interactive call, the agenda will
  18971. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18972. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18973. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18974. given in `org-agenda-start-on-weekday'."
  18975. (interactive "P")
  18976. (if (and (integerp include-all) (> include-all 0))
  18977. (setq ndays include-all include-all nil))
  18978. (setq ndays (or ndays org-agenda-ndays)
  18979. start-day (or start-day org-agenda-start-day))
  18980. (if org-agenda-overriding-arguments
  18981. (setq include-all (car org-agenda-overriding-arguments)
  18982. start-day (nth 1 org-agenda-overriding-arguments)
  18983. ndays (nth 2 org-agenda-overriding-arguments)))
  18984. (if (stringp start-day)
  18985. ;; Convert to an absolute day number
  18986. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18987. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18988. (org-compile-prefix-format 'agenda)
  18989. (org-set-sorting-strategy 'agenda)
  18990. (require 'calendar)
  18991. (let* ((org-agenda-start-on-weekday
  18992. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18993. org-agenda-start-on-weekday nil))
  18994. (thefiles (org-agenda-files))
  18995. (files thefiles)
  18996. (today (time-to-days
  18997. (time-subtract (current-time)
  18998. (list 0 (* 3600 org-extend-today-until) 0))))
  18999. (sd (or start-day today))
  19000. (start (if (or (null org-agenda-start-on-weekday)
  19001. (< org-agenda-ndays 7))
  19002. sd
  19003. (let* ((nt (calendar-day-of-week
  19004. (calendar-gregorian-from-absolute sd)))
  19005. (n1 org-agenda-start-on-weekday)
  19006. (d (- nt n1)))
  19007. (- sd (+ (if (< d 0) 7 0) d)))))
  19008. (day-numbers (list start))
  19009. (day-cnt 0)
  19010. (inhibit-redisplay (not debug-on-error))
  19011. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19012. (setq org-agenda-redo-command
  19013. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19014. ;; Make the list of days
  19015. (setq ndays (or ndays org-agenda-ndays)
  19016. nd ndays)
  19017. (while (> ndays 1)
  19018. (push (1+ (car day-numbers)) day-numbers)
  19019. (setq ndays (1- ndays)))
  19020. (setq day-numbers (nreverse day-numbers))
  19021. (org-prepare-agenda "Day/Week")
  19022. (org-set-local 'org-starting-day (car day-numbers))
  19023. (org-set-local 'org-include-all-loc include-all)
  19024. (org-set-local 'org-agenda-span
  19025. (org-agenda-ndays-to-span nd))
  19026. (when (and (or include-all org-agenda-include-all-todo)
  19027. (member today day-numbers))
  19028. (setq files thefiles
  19029. rtnall nil)
  19030. (while (setq file (pop files))
  19031. (catch 'nextfile
  19032. (org-check-agenda-file file)
  19033. (setq date (calendar-gregorian-from-absolute today)
  19034. rtn (org-agenda-get-day-entries
  19035. file date :todo))
  19036. (setq rtnall (append rtnall rtn))))
  19037. (when rtnall
  19038. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19039. (add-text-properties (point-min) (1- (point))
  19040. (list 'face 'org-agenda-structure))
  19041. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19042. (unless org-agenda-compact-blocks
  19043. (setq s (point))
  19044. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19045. "-agenda:\n")
  19046. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19047. 'org-date-line t)))
  19048. (while (setq d (pop day-numbers))
  19049. (setq date (calendar-gregorian-from-absolute d)
  19050. s (point))
  19051. (if (or (setq todayp (= d today))
  19052. (and (not start-pos) (= d sd)))
  19053. (setq start-pos (point))
  19054. (if (and start-pos (not end-pos))
  19055. (setq end-pos (point))))
  19056. (setq files thefiles
  19057. rtnall nil)
  19058. (while (setq file (pop files))
  19059. (catch 'nextfile
  19060. (org-check-agenda-file file)
  19061. (if org-agenda-show-log
  19062. (setq rtn (org-agenda-get-day-entries
  19063. file date
  19064. :deadline :scheduled :timestamp :sexp :closed))
  19065. (setq rtn (org-agenda-get-day-entries
  19066. file date
  19067. :deadline :scheduled :sexp :timestamp)))
  19068. (setq rtnall (append rtnall rtn))))
  19069. (if org-agenda-include-diary
  19070. (progn
  19071. (require 'diary-lib)
  19072. (setq rtn (org-get-entries-from-diary date))
  19073. (setq rtnall (append rtnall rtn))))
  19074. (if (or rtnall org-agenda-show-all-dates)
  19075. (progn
  19076. (setq day-cnt (1+ day-cnt))
  19077. (insert
  19078. (if (stringp org-agenda-format-date)
  19079. (format-time-string org-agenda-format-date
  19080. (org-time-from-absolute date))
  19081. (funcall org-agenda-format-date date))
  19082. "\n")
  19083. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19084. (put-text-property s (1- (point)) 'org-date-line t)
  19085. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19086. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19087. (if rtnall (insert
  19088. (org-finalize-agenda-entries
  19089. (org-agenda-add-time-grid-maybe
  19090. rtnall nd todayp))
  19091. "\n"))
  19092. (put-text-property s (1- (point)) 'day d)
  19093. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19094. (goto-char (point-min))
  19095. (org-fit-agenda-window)
  19096. (unless (and (pos-visible-in-window-p (point-min))
  19097. (pos-visible-in-window-p (point-max)))
  19098. (goto-char (1- (point-max)))
  19099. (recenter -1)
  19100. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19101. (progn
  19102. (goto-char (or start-pos 1))
  19103. (recenter 1))))
  19104. (goto-char (or start-pos 1))
  19105. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19106. (org-finalize-agenda)
  19107. (setq buffer-read-only t)
  19108. (message "")))
  19109. (defun org-agenda-ndays-to-span (n)
  19110. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19111. ;;; Agenda TODO list
  19112. (defvar org-select-this-todo-keyword nil)
  19113. (defvar org-last-arg nil)
  19114. ;;;###autoload
  19115. (defun org-todo-list (arg)
  19116. "Show all TODO entries from all agenda file in a single list.
  19117. The prefix arg can be used to select a specific TODO keyword and limit
  19118. the list to these. When using \\[universal-argument], you will be prompted
  19119. for a keyword. A numeric prefix directly selects the Nth keyword in
  19120. `org-todo-keywords-1'."
  19121. (interactive "P")
  19122. (require 'calendar)
  19123. (org-compile-prefix-format 'todo)
  19124. (org-set-sorting-strategy 'todo)
  19125. (org-prepare-agenda "TODO")
  19126. (let* ((today (time-to-days (current-time)))
  19127. (date (calendar-gregorian-from-absolute today))
  19128. (kwds org-todo-keywords-for-agenda)
  19129. (completion-ignore-case t)
  19130. (org-select-this-todo-keyword
  19131. (if (stringp arg) arg
  19132. (and arg (integerp arg) (> arg 0)
  19133. (nth (1- arg) kwds))))
  19134. rtn rtnall files file pos)
  19135. (when (equal arg '(4))
  19136. (setq org-select-this-todo-keyword
  19137. (completing-read "Keyword (or KWD1|K2D2|...): "
  19138. (mapcar 'list kwds) nil nil)))
  19139. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19140. (org-set-local 'org-last-arg arg)
  19141. (setq org-agenda-redo-command
  19142. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19143. (setq files (org-agenda-files)
  19144. rtnall nil)
  19145. (while (setq file (pop files))
  19146. (catch 'nextfile
  19147. (org-check-agenda-file file)
  19148. (setq rtn (org-agenda-get-day-entries file date :todo))
  19149. (setq rtnall (append rtnall rtn))))
  19150. (if org-agenda-overriding-header
  19151. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19152. nil 'face 'org-agenda-structure) "\n")
  19153. (insert "Global list of TODO items of type: ")
  19154. (add-text-properties (point-min) (1- (point))
  19155. (list 'face 'org-agenda-structure))
  19156. (setq pos (point))
  19157. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19158. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19159. (setq pos (point))
  19160. (unless org-agenda-multi
  19161. (insert "Available with `N r': (0)ALL")
  19162. (let ((n 0) s)
  19163. (mapc (lambda (x)
  19164. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19165. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19166. (insert "\n "))
  19167. (insert " " s))
  19168. kwds))
  19169. (insert "\n"))
  19170. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19171. (when rtnall
  19172. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19173. (goto-char (point-min))
  19174. (org-fit-agenda-window)
  19175. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19176. (org-finalize-agenda)
  19177. (setq buffer-read-only t)))
  19178. ;;; Agenda tags match
  19179. ;;;###autoload
  19180. (defun org-tags-view (&optional todo-only match)
  19181. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19182. The prefix arg TODO-ONLY limits the search to TODO entries."
  19183. (interactive "P")
  19184. (org-compile-prefix-format 'tags)
  19185. (org-set-sorting-strategy 'tags)
  19186. (let* ((org-tags-match-list-sublevels
  19187. (if todo-only t org-tags-match-list-sublevels))
  19188. (completion-ignore-case t)
  19189. rtn rtnall files file pos matcher
  19190. buffer)
  19191. (setq matcher (org-make-tags-matcher match)
  19192. match (car matcher) matcher (cdr matcher))
  19193. (org-prepare-agenda (concat "TAGS " match))
  19194. (setq org-agenda-redo-command
  19195. (list 'org-tags-view (list 'quote todo-only)
  19196. (list 'if 'current-prefix-arg nil match)))
  19197. (setq files (org-agenda-files)
  19198. rtnall nil)
  19199. (while (setq file (pop files))
  19200. (catch 'nextfile
  19201. (org-check-agenda-file file)
  19202. (setq buffer (if (file-exists-p file)
  19203. (org-get-agenda-file-buffer file)
  19204. (error "No such file %s" file)))
  19205. (if (not buffer)
  19206. ;; If file does not exist, merror message to agenda
  19207. (setq rtn (list
  19208. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19209. rtnall (append rtnall rtn))
  19210. (with-current-buffer buffer
  19211. (unless (org-mode-p)
  19212. (error "Agenda file %s is not in `org-mode'" file))
  19213. (save-excursion
  19214. (save-restriction
  19215. (if org-agenda-restrict
  19216. (narrow-to-region org-agenda-restrict-begin
  19217. org-agenda-restrict-end)
  19218. (widen))
  19219. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19220. (setq rtnall (append rtnall rtn))))))))
  19221. (if org-agenda-overriding-header
  19222. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19223. nil 'face 'org-agenda-structure) "\n")
  19224. (insert "Headlines with TAGS match: ")
  19225. (add-text-properties (point-min) (1- (point))
  19226. (list 'face 'org-agenda-structure))
  19227. (setq pos (point))
  19228. (insert match "\n")
  19229. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19230. (setq pos (point))
  19231. (unless org-agenda-multi
  19232. (insert "Press `C-u r' to search again with new search string\n"))
  19233. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19234. (when rtnall
  19235. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19236. (goto-char (point-min))
  19237. (org-fit-agenda-window)
  19238. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19239. (org-finalize-agenda)
  19240. (setq buffer-read-only t)))
  19241. ;;; Agenda Finding stuck projects
  19242. (defvar org-agenda-skip-regexp nil
  19243. "Regular expression used in skipping subtrees for the agenda.
  19244. This is basically a temporary global variable that can be set and then
  19245. used by user-defined selections using `org-agenda-skip-function'.")
  19246. (defvar org-agenda-overriding-header nil
  19247. "When this is set during todo and tags searches, will replace header.")
  19248. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19249. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19250. If yes, it returns the end position of this tree, causing agenda commands
  19251. to skip this subtree. This is a function that can be put into
  19252. `org-agenda-skip-function' for the duration of a command."
  19253. (let ((end (save-excursion (org-end-of-subtree t)))
  19254. skip)
  19255. (save-excursion
  19256. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19257. (and skip end)))
  19258. (defun org-agenda-skip-entry-if (&rest conditions)
  19259. "Skip entry if any of CONDITIONS is true.
  19260. See `org-agenda-skip-if' for details."
  19261. (org-agenda-skip-if nil conditions))
  19262. (defun org-agenda-skip-subtree-if (&rest conditions)
  19263. "Skip entry if any of CONDITIONS is true.
  19264. See `org-agenda-skip-if' for details."
  19265. (org-agenda-skip-if t conditions))
  19266. (defun org-agenda-skip-if (subtree conditions)
  19267. "Checks current entity for CONDITIONS.
  19268. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19269. the entry, i.e. the text before the next heading is checked.
  19270. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19271. from different tests. Valid conditions are:
  19272. scheduled Check if there is a scheduled cookie
  19273. notscheduled Check if there is no scheduled cookie
  19274. deadline Check if there is a deadline
  19275. notdeadline Check if there is no deadline
  19276. regexp Check if regexp matches
  19277. notregexp Check if regexp does not match.
  19278. The regexp is taken from the conditions list, it must come right after
  19279. the `regexp' or `notregexp' element.
  19280. If any of these conditions is met, this function returns the end point of
  19281. the entity, causing the search to continue from there. This is a function
  19282. that can be put into `org-agenda-skip-function' for the duration of a command."
  19283. (let (beg end m)
  19284. (org-back-to-heading t)
  19285. (setq beg (point)
  19286. end (if subtree
  19287. (progn (org-end-of-subtree t) (point))
  19288. (progn (outline-next-heading) (1- (point)))))
  19289. (goto-char beg)
  19290. (and
  19291. (or
  19292. (and (memq 'scheduled conditions)
  19293. (re-search-forward org-scheduled-time-regexp end t))
  19294. (and (memq 'notscheduled conditions)
  19295. (not (re-search-forward org-scheduled-time-regexp end t)))
  19296. (and (memq 'deadline conditions)
  19297. (re-search-forward org-deadline-time-regexp end t))
  19298. (and (memq 'notdeadline conditions)
  19299. (not (re-search-forward org-deadline-time-regexp end t)))
  19300. (and (setq m (memq 'regexp conditions))
  19301. (stringp (nth 1 m))
  19302. (re-search-forward (nth 1 m) end t))
  19303. (and (setq m (memq 'notregexp conditions))
  19304. (stringp (nth 1 m))
  19305. (not (re-search-forward (nth 1 m) end t))))
  19306. end)))
  19307. ;;;###autoload
  19308. (defun org-agenda-list-stuck-projects (&rest ignore)
  19309. "Create agenda view for projects that are stuck.
  19310. Stuck projects are project that have no next actions. For the definitions
  19311. of what a project is and how to check if it stuck, customize the variable
  19312. `org-stuck-projects'.
  19313. MATCH is being ignored."
  19314. (interactive)
  19315. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19316. ;; FIXME: we could have used org-agenda-skip-if here.
  19317. (org-agenda-overriding-header "List of stuck projects: ")
  19318. (matcher (nth 0 org-stuck-projects))
  19319. (todo (nth 1 org-stuck-projects))
  19320. (todo-wds (if (member "*" todo)
  19321. (progn
  19322. (org-prepare-agenda-buffers (org-agenda-files))
  19323. (org-delete-all
  19324. org-done-keywords-for-agenda
  19325. (copy-sequence org-todo-keywords-for-agenda)))
  19326. todo))
  19327. (todo-re (concat "^\\*+[ \t]+\\("
  19328. (mapconcat 'identity todo-wds "\\|")
  19329. "\\)\\>"))
  19330. (tags (nth 2 org-stuck-projects))
  19331. (tags-re (if (member "*" tags)
  19332. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19333. (concat "^\\*+ .*:\\("
  19334. (mapconcat 'identity tags "\\|")
  19335. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19336. (gen-re (nth 3 org-stuck-projects))
  19337. (re-list
  19338. (delq nil
  19339. (list
  19340. (if todo todo-re)
  19341. (if tags tags-re)
  19342. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19343. gen-re)))))
  19344. (setq org-agenda-skip-regexp
  19345. (if re-list
  19346. (mapconcat 'identity re-list "\\|")
  19347. (error "No information how to identify unstuck projects")))
  19348. (org-tags-view nil matcher)
  19349. (with-current-buffer org-agenda-buffer-name
  19350. (setq org-agenda-redo-command
  19351. '(org-agenda-list-stuck-projects
  19352. (or current-prefix-arg org-last-arg))))))
  19353. ;;; Diary integration
  19354. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19355. (defun org-get-entries-from-diary (date)
  19356. "Get the (Emacs Calendar) diary entries for DATE."
  19357. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19358. (diary-display-hook '(fancy-diary-display))
  19359. (pop-up-frames nil)
  19360. (list-diary-entries-hook
  19361. (cons 'org-diary-default-entry list-diary-entries-hook))
  19362. (diary-file-name-prefix-function nil) ; turn this feature off
  19363. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19364. entries
  19365. (org-disable-agenda-to-diary t))
  19366. (save-excursion
  19367. (save-window-excursion
  19368. (funcall (if (fboundp 'diary-list-entries)
  19369. 'diary-list-entries 'list-diary-entries)
  19370. date 1)))
  19371. (if (not (get-buffer fancy-diary-buffer))
  19372. (setq entries nil)
  19373. (with-current-buffer fancy-diary-buffer
  19374. (setq buffer-read-only nil)
  19375. (if (zerop (buffer-size))
  19376. ;; No entries
  19377. (setq entries nil)
  19378. ;; Omit the date and other unnecessary stuff
  19379. (org-agenda-cleanup-fancy-diary)
  19380. ;; Add prefix to each line and extend the text properties
  19381. (if (zerop (buffer-size))
  19382. (setq entries nil)
  19383. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19384. (set-buffer-modified-p nil)
  19385. (kill-buffer fancy-diary-buffer)))
  19386. (when entries
  19387. (setq entries (org-split-string entries "\n"))
  19388. (setq entries
  19389. (mapcar
  19390. (lambda (x)
  19391. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19392. ;; Extend the text properties to the beginning of the line
  19393. (org-add-props x (text-properties-at (1- (length x)) x)
  19394. 'type "diary" 'date date))
  19395. entries)))))
  19396. (defun org-agenda-cleanup-fancy-diary ()
  19397. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19398. This gets rid of the date, the underline under the date, and
  19399. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19400. date. It also removes lines that contain only whitespace."
  19401. (goto-char (point-min))
  19402. (if (looking-at ".*?:[ \t]*")
  19403. (progn
  19404. (replace-match "")
  19405. (re-search-forward "\n=+$" nil t)
  19406. (replace-match "")
  19407. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19408. (re-search-forward "\n=+$" nil t)
  19409. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19410. (goto-char (point-min))
  19411. (while (re-search-forward "^ +\n" nil t)
  19412. (replace-match ""))
  19413. (goto-char (point-min))
  19414. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19415. (replace-match "")))
  19416. ;; Make sure entries from the diary have the right text properties.
  19417. (eval-after-load "diary-lib"
  19418. '(if (boundp 'diary-modify-entry-list-string-function)
  19419. ;; We can rely on the hook, nothing to do
  19420. nil
  19421. ;; Hook not avaiable, must use advice to make this work
  19422. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19423. "Make the position visible."
  19424. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19425. (stringp string)
  19426. buffer-file-name)
  19427. (setq string (org-modify-diary-entry-string string))))))
  19428. (defun org-modify-diary-entry-string (string)
  19429. "Add text properties to string, allowing org-mode to act on it."
  19430. (org-add-props string nil
  19431. 'mouse-face 'highlight
  19432. 'keymap org-agenda-keymap
  19433. 'help-echo (if buffer-file-name
  19434. (format "mouse-2 or RET jump to diary file %s"
  19435. (abbreviate-file-name buffer-file-name))
  19436. "")
  19437. 'org-agenda-diary-link t
  19438. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19439. (defun org-diary-default-entry ()
  19440. "Add a dummy entry to the diary.
  19441. Needed to avoid empty dates which mess up holiday display."
  19442. ;; Catch the error if dealing with the new add-to-diary-alist
  19443. (when org-disable-agenda-to-diary
  19444. (condition-case nil
  19445. (add-to-diary-list original-date "Org-mode dummy" "")
  19446. (error
  19447. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19448. ;;;###autoload
  19449. (defun org-diary (&rest args)
  19450. "Return diary information from org-files.
  19451. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19452. It accesses org files and extracts information from those files to be
  19453. listed in the diary. The function accepts arguments specifying what
  19454. items should be listed. The following arguments are allowed:
  19455. :timestamp List the headlines of items containing a date stamp or
  19456. date range matching the selected date. Deadlines will
  19457. also be listed, on the expiration day.
  19458. :sexp List entries resulting from diary-like sexps.
  19459. :deadline List any deadlines past due, or due within
  19460. `org-deadline-warning-days'. The listing occurs only
  19461. in the diary for *today*, not at any other date. If
  19462. an entry is marked DONE, it is no longer listed.
  19463. :scheduled List all items which are scheduled for the given date.
  19464. The diary for *today* also contains items which were
  19465. scheduled earlier and are not yet marked DONE.
  19466. :todo List all TODO items from the org-file. This may be a
  19467. long list - so this is not turned on by default.
  19468. Like deadlines, these entries only show up in the
  19469. diary for *today*, not at any other date.
  19470. The call in the diary file should look like this:
  19471. &%%(org-diary) ~/path/to/some/orgfile.org
  19472. Use a separate line for each org file to check. Or, if you omit the file name,
  19473. all files listed in `org-agenda-files' will be checked automatically:
  19474. &%%(org-diary)
  19475. If you don't give any arguments (as in the example above), the default
  19476. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19477. So the example above may also be written as
  19478. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19479. The function expects the lisp variables `entry' and `date' to be provided
  19480. by the caller, because this is how the calendar works. Don't use this
  19481. function from a program - use `org-agenda-get-day-entries' instead."
  19482. (when (> (- (time-to-seconds (current-time))
  19483. org-agenda-last-marker-time)
  19484. 5)
  19485. (org-agenda-reset-markers))
  19486. (org-compile-prefix-format 'agenda)
  19487. (org-set-sorting-strategy 'agenda)
  19488. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19489. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19490. (list entry)
  19491. (org-agenda-files t)))
  19492. file rtn results)
  19493. (org-prepare-agenda-buffers files)
  19494. ;; If this is called during org-agenda, don't return any entries to
  19495. ;; the calendar. Org Agenda will list these entries itself.
  19496. (if org-disable-agenda-to-diary (setq files nil))
  19497. (while (setq file (pop files))
  19498. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19499. (setq results (append results rtn)))
  19500. (if results
  19501. (concat (org-finalize-agenda-entries results) "\n"))))
  19502. ;;; Agenda entry finders
  19503. (defun org-agenda-get-day-entries (file date &rest args)
  19504. "Does the work for `org-diary' and `org-agenda'.
  19505. FILE is the path to a file to be checked for entries. DATE is date like
  19506. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19507. which kind of entries should be extracted. For details about these, see
  19508. the documentation of `org-diary'."
  19509. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19510. (let* ((org-startup-folded nil)
  19511. (org-startup-align-all-tables nil)
  19512. (buffer (if (file-exists-p file)
  19513. (org-get-agenda-file-buffer file)
  19514. (error "No such file %s" file)))
  19515. arg results rtn)
  19516. (if (not buffer)
  19517. ;; If file does not exist, make sure an error message ends up in diary
  19518. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19519. (with-current-buffer buffer
  19520. (unless (org-mode-p)
  19521. (error "Agenda file %s is not in `org-mode'" file))
  19522. (let ((case-fold-search nil))
  19523. (save-excursion
  19524. (save-restriction
  19525. (if org-agenda-restrict
  19526. (narrow-to-region org-agenda-restrict-begin
  19527. org-agenda-restrict-end)
  19528. (widen))
  19529. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19530. (while (setq arg (pop args))
  19531. (cond
  19532. ((and (eq arg :todo)
  19533. (equal date (calendar-current-date)))
  19534. (setq rtn (org-agenda-get-todos))
  19535. (setq results (append results rtn)))
  19536. ((eq arg :timestamp)
  19537. (setq rtn (org-agenda-get-blocks))
  19538. (setq results (append results rtn))
  19539. (setq rtn (org-agenda-get-timestamps))
  19540. (setq results (append results rtn)))
  19541. ((eq arg :sexp)
  19542. (setq rtn (org-agenda-get-sexps))
  19543. (setq results (append results rtn)))
  19544. ((eq arg :scheduled)
  19545. (setq rtn (org-agenda-get-scheduled))
  19546. (setq results (append results rtn)))
  19547. ((eq arg :closed)
  19548. (setq rtn (org-agenda-get-closed))
  19549. (setq results (append results rtn)))
  19550. ((eq arg :deadline)
  19551. (setq rtn (org-agenda-get-deadlines))
  19552. (setq results (append results rtn))))))))
  19553. results))))
  19554. (defun org-entry-is-todo-p ()
  19555. (member (org-get-todo-state) org-not-done-keywords))
  19556. (defun org-entry-is-done-p ()
  19557. (member (org-get-todo-state) org-done-keywords))
  19558. (defun org-get-todo-state ()
  19559. (save-excursion
  19560. (org-back-to-heading t)
  19561. (and (looking-at org-todo-line-regexp)
  19562. (match-end 2)
  19563. (match-string 2))))
  19564. (defun org-at-date-range-p (&optional inactive-ok)
  19565. "Is the cursor inside a date range?"
  19566. (interactive)
  19567. (save-excursion
  19568. (catch 'exit
  19569. (let ((pos (point)))
  19570. (skip-chars-backward "^[<\r\n")
  19571. (skip-chars-backward "<[")
  19572. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19573. (>= (match-end 0) pos)
  19574. (throw 'exit t))
  19575. (skip-chars-backward "^<[\r\n")
  19576. (skip-chars-backward "<[")
  19577. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19578. (>= (match-end 0) pos)
  19579. (throw 'exit t)))
  19580. nil)))
  19581. (defun org-agenda-get-todos ()
  19582. "Return the TODO information for agenda display."
  19583. (let* ((props (list 'face nil
  19584. 'done-face 'org-done
  19585. 'org-not-done-regexp org-not-done-regexp
  19586. 'org-todo-regexp org-todo-regexp
  19587. 'mouse-face 'highlight
  19588. 'keymap org-agenda-keymap
  19589. 'help-echo
  19590. (format "mouse-2 or RET jump to org file %s"
  19591. (abbreviate-file-name buffer-file-name))))
  19592. ;; FIXME: get rid of the \n at some point but watch out
  19593. (regexp (concat "^\\*+[ \t]+\\("
  19594. (if org-select-this-todo-keyword
  19595. (if (equal org-select-this-todo-keyword "*")
  19596. org-todo-regexp
  19597. (concat "\\<\\("
  19598. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19599. "\\)\\>"))
  19600. org-not-done-regexp)
  19601. "[^\n\r]*\\)"))
  19602. marker priority category tags
  19603. ee txt beg end)
  19604. (goto-char (point-min))
  19605. (while (re-search-forward regexp nil t)
  19606. (catch :skip
  19607. (save-match-data
  19608. (beginning-of-line)
  19609. (setq beg (point) end (progn (outline-next-heading) (point)))
  19610. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19611. (re-search-forward org-ts-regexp end t))
  19612. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19613. (re-search-forward org-scheduled-time-regexp end t))
  19614. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19615. (re-search-forward org-deadline-time-regexp end t)
  19616. (org-deadline-close (match-string 1))))
  19617. (goto-char (1+ beg))
  19618. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19619. (throw :skip nil)))
  19620. (goto-char beg)
  19621. (org-agenda-skip)
  19622. (goto-char (match-beginning 1))
  19623. (setq marker (org-agenda-new-marker (match-beginning 0))
  19624. category (org-get-category)
  19625. tags (org-get-tags-at (point))
  19626. txt (org-format-agenda-item "" (match-string 1) category tags)
  19627. priority (1+ (org-get-priority txt)))
  19628. (org-add-props txt props
  19629. 'org-marker marker 'org-hd-marker marker
  19630. 'priority priority 'org-category category
  19631. 'type "todo")
  19632. (push txt ee)
  19633. (if org-agenda-todo-list-sublevels
  19634. (goto-char (match-end 1))
  19635. (org-end-of-subtree 'invisible))))
  19636. (nreverse ee)))
  19637. (defconst org-agenda-no-heading-message
  19638. "No heading for this item in buffer or region.")
  19639. (defun org-agenda-get-timestamps ()
  19640. "Return the date stamp information for agenda display."
  19641. (let* ((props (list 'face nil
  19642. 'org-not-done-regexp org-not-done-regexp
  19643. 'org-todo-regexp org-todo-regexp
  19644. 'mouse-face 'highlight
  19645. 'keymap org-agenda-keymap
  19646. 'help-echo
  19647. (format "mouse-2 or RET jump to org file %s"
  19648. (abbreviate-file-name buffer-file-name))))
  19649. (d1 (calendar-absolute-from-gregorian date))
  19650. (remove-re
  19651. (concat
  19652. (regexp-quote
  19653. (format-time-string
  19654. "<%Y-%m-%d"
  19655. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19656. ".*?>"))
  19657. (regexp
  19658. (concat
  19659. (regexp-quote
  19660. (substring
  19661. (format-time-string
  19662. (car org-time-stamp-formats)
  19663. (apply 'encode-time ; DATE bound by calendar
  19664. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19665. 0 11))
  19666. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19667. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19668. marker hdmarker deadlinep scheduledp donep tmp priority category
  19669. ee txt timestr tags b0 b3 e3 head)
  19670. (goto-char (point-min))
  19671. (while (re-search-forward regexp nil t)
  19672. (setq b0 (match-beginning 0)
  19673. b3 (match-beginning 3) e3 (match-end 3))
  19674. (catch :skip
  19675. (and (org-at-date-range-p) (throw :skip nil))
  19676. (org-agenda-skip)
  19677. (if (and (match-end 1)
  19678. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19679. (throw :skip nil))
  19680. (if (and e3
  19681. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19682. (throw :skip nil))
  19683. (setq marker (org-agenda-new-marker b0)
  19684. category (org-get-category b0)
  19685. tmp (buffer-substring (max (point-min)
  19686. (- b0 org-ds-keyword-length))
  19687. b0)
  19688. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19689. deadlinep (string-match org-deadline-regexp tmp)
  19690. scheduledp (string-match org-scheduled-regexp tmp)
  19691. donep (org-entry-is-done-p))
  19692. (if (or scheduledp deadlinep) (throw :skip t))
  19693. (if (string-match ">" timestr)
  19694. ;; substring should only run to end of time stamp
  19695. (setq timestr (substring timestr 0 (match-end 0))))
  19696. (save-excursion
  19697. (if (re-search-backward "^\\*+ " nil t)
  19698. (progn
  19699. (goto-char (match-beginning 0))
  19700. (setq hdmarker (org-agenda-new-marker)
  19701. tags (org-get-tags-at))
  19702. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19703. (setq head (match-string 1))
  19704. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19705. (setq txt (org-format-agenda-item
  19706. nil head category tags timestr nil
  19707. remove-re)))
  19708. (setq txt org-agenda-no-heading-message))
  19709. (setq priority (org-get-priority txt))
  19710. (org-add-props txt props
  19711. 'org-marker marker 'org-hd-marker hdmarker)
  19712. (org-add-props txt nil 'priority priority
  19713. 'org-category category 'date date
  19714. 'type "timestamp")
  19715. (push txt ee))
  19716. (outline-next-heading)))
  19717. (nreverse ee)))
  19718. (defun org-agenda-get-sexps ()
  19719. "Return the sexp information for agenda display."
  19720. (require 'diary-lib)
  19721. (let* ((props (list 'face nil
  19722. 'mouse-face 'highlight
  19723. 'keymap org-agenda-keymap
  19724. 'help-echo
  19725. (format "mouse-2 or RET jump to org file %s"
  19726. (abbreviate-file-name buffer-file-name))))
  19727. (regexp "^&?%%(")
  19728. marker category ee txt tags entry result beg b sexp sexp-entry)
  19729. (goto-char (point-min))
  19730. (while (re-search-forward regexp nil t)
  19731. (catch :skip
  19732. (org-agenda-skip)
  19733. (setq beg (match-beginning 0))
  19734. (goto-char (1- (match-end 0)))
  19735. (setq b (point))
  19736. (forward-sexp 1)
  19737. (setq sexp (buffer-substring b (point)))
  19738. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19739. (org-trim (match-string 1))
  19740. ""))
  19741. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19742. (when result
  19743. (setq marker (org-agenda-new-marker beg)
  19744. category (org-get-category beg))
  19745. (if (string-match "\\S-" result)
  19746. (setq txt result)
  19747. (setq txt "SEXP entry returned empty string"))
  19748. (setq txt (org-format-agenda-item
  19749. "" txt category tags 'time))
  19750. (org-add-props txt props 'org-marker marker)
  19751. (org-add-props txt nil
  19752. 'org-category category 'date date
  19753. 'type "sexp")
  19754. (push txt ee))))
  19755. (nreverse ee)))
  19756. (defun org-agenda-get-closed ()
  19757. "Return the logged TODO entries for agenda display."
  19758. (let* ((props (list 'mouse-face 'highlight
  19759. 'org-not-done-regexp org-not-done-regexp
  19760. 'org-todo-regexp org-todo-regexp
  19761. 'keymap org-agenda-keymap
  19762. 'help-echo
  19763. (format "mouse-2 or RET jump to org file %s"
  19764. (abbreviate-file-name buffer-file-name))))
  19765. (regexp (concat
  19766. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19767. (regexp-quote
  19768. (substring
  19769. (format-time-string
  19770. (car org-time-stamp-formats)
  19771. (apply 'encode-time ; DATE bound by calendar
  19772. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19773. 1 11))))
  19774. marker hdmarker priority category tags closedp
  19775. ee txt timestr)
  19776. (goto-char (point-min))
  19777. (while (re-search-forward regexp nil t)
  19778. (catch :skip
  19779. (org-agenda-skip)
  19780. (setq marker (org-agenda-new-marker (match-beginning 0))
  19781. closedp (equal (match-string 1) org-closed-string)
  19782. category (org-get-category (match-beginning 0))
  19783. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19784. ;; donep (org-entry-is-done-p)
  19785. )
  19786. (if (string-match "\\]" timestr)
  19787. ;; substring should only run to end of time stamp
  19788. (setq timestr (substring timestr 0 (match-end 0))))
  19789. (save-excursion
  19790. (if (re-search-backward "^\\*+ " nil t)
  19791. (progn
  19792. (goto-char (match-beginning 0))
  19793. (setq hdmarker (org-agenda-new-marker)
  19794. tags (org-get-tags-at))
  19795. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19796. (setq txt (org-format-agenda-item
  19797. (if closedp "Closed: " "Clocked: ")
  19798. (match-string 1) category tags timestr)))
  19799. (setq txt org-agenda-no-heading-message))
  19800. (setq priority 100000)
  19801. (org-add-props txt props
  19802. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19803. 'priority priority 'org-category category
  19804. 'type "closed" 'date date
  19805. 'undone-face 'org-warning 'done-face 'org-done)
  19806. (push txt ee))
  19807. (goto-char (point-at-eol))))
  19808. (nreverse ee)))
  19809. (defun org-agenda-get-deadlines ()
  19810. "Return the deadline information for agenda display."
  19811. (let* ((props (list 'mouse-face 'highlight
  19812. 'org-not-done-regexp org-not-done-regexp
  19813. 'org-todo-regexp org-todo-regexp
  19814. 'keymap org-agenda-keymap
  19815. 'help-echo
  19816. (format "mouse-2 or RET jump to org file %s"
  19817. (abbreviate-file-name buffer-file-name))))
  19818. (regexp org-deadline-time-regexp)
  19819. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19820. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19821. d2 diff dfrac wdays pos pos1 category tags
  19822. ee txt head face s upcomingp donep timestr)
  19823. (goto-char (point-min))
  19824. (while (re-search-forward regexp nil t)
  19825. (catch :skip
  19826. (org-agenda-skip)
  19827. (setq s (match-string 1)
  19828. pos (1- (match-beginning 1))
  19829. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19830. diff (- d2 d1)
  19831. wdays (org-get-wdays s)
  19832. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19833. upcomingp (and todayp (> diff 0)))
  19834. ;; When to show a deadline in the calendar:
  19835. ;; If the expiration is within wdays warning time.
  19836. ;; Past-due deadlines are only shown on the current date
  19837. (if (or (and (<= diff wdays)
  19838. (and todayp (not org-agenda-only-exact-dates)))
  19839. (= diff 0))
  19840. (save-excursion
  19841. (setq category (org-get-category))
  19842. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19843. (progn
  19844. (goto-char (match-end 0))
  19845. (setq pos1 (match-beginning 0))
  19846. (setq tags (org-get-tags-at pos1))
  19847. (setq head (buffer-substring-no-properties
  19848. (point)
  19849. (progn (skip-chars-forward "^\r\n")
  19850. (point))))
  19851. (setq donep (string-match org-looking-at-done-regexp head))
  19852. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19853. (setq timestr
  19854. (concat (substring s (match-beginning 1)) " "))
  19855. (setq timestr 'time))
  19856. (if (and donep
  19857. (or org-agenda-skip-deadline-if-done
  19858. (not (= diff 0))))
  19859. (setq txt nil)
  19860. (setq txt (org-format-agenda-item
  19861. (if (= diff 0)
  19862. (car org-agenda-deadline-leaders)
  19863. (format (nth 1 org-agenda-deadline-leaders)
  19864. diff))
  19865. head category tags timestr))))
  19866. (setq txt org-agenda-no-heading-message))
  19867. (when txt
  19868. (setq face (org-agenda-deadline-face dfrac))
  19869. (org-add-props txt props
  19870. 'org-marker (org-agenda-new-marker pos)
  19871. 'org-hd-marker (org-agenda-new-marker pos1)
  19872. 'priority (+ (- diff)
  19873. (org-get-priority txt))
  19874. 'org-category category
  19875. 'type (if upcomingp "upcoming-deadline" "deadline")
  19876. 'date (if upcomingp date d2)
  19877. 'face (if donep 'org-done face)
  19878. 'undone-face face 'done-face 'org-done)
  19879. (push txt ee))))))
  19880. (nreverse ee)))
  19881. (defun org-agenda-deadline-face (fraction)
  19882. "Return the face to displaying a deadline item.
  19883. FRACTION is what fraction of the head-warning time has passed."
  19884. (let ((faces org-agenda-deadline-faces) f)
  19885. (catch 'exit
  19886. (while (setq f (pop faces))
  19887. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19888. (defun org-agenda-get-scheduled ()
  19889. "Return the scheduled information for agenda display."
  19890. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19891. 'org-todo-regexp org-todo-regexp
  19892. 'done-face 'org-done
  19893. 'mouse-face 'highlight
  19894. 'keymap org-agenda-keymap
  19895. 'help-echo
  19896. (format "mouse-2 or RET jump to org file %s"
  19897. (abbreviate-file-name buffer-file-name))))
  19898. (regexp org-scheduled-time-regexp)
  19899. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19900. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19901. d2 diff pos pos1 category tags
  19902. ee txt head pastschedp donep face timestr s)
  19903. (goto-char (point-min))
  19904. (while (re-search-forward regexp nil t)
  19905. (catch :skip
  19906. (org-agenda-skip)
  19907. (setq s (match-string 1)
  19908. pos (1- (match-beginning 1))
  19909. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19910. ;;; is this right?
  19911. ;;; do we need to do this for deadleine too????
  19912. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19913. diff (- d2 d1))
  19914. (setq pastschedp (and todayp (< diff 0)))
  19915. ;; When to show a scheduled item in the calendar:
  19916. ;; If it is on or past the date.
  19917. (if (or (and (< diff 0)
  19918. (and todayp (not org-agenda-only-exact-dates)))
  19919. (= diff 0))
  19920. (save-excursion
  19921. (setq category (org-get-category))
  19922. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19923. (progn
  19924. (goto-char (match-end 0))
  19925. (setq pos1 (match-beginning 0))
  19926. (setq tags (org-get-tags-at))
  19927. (setq head (buffer-substring-no-properties
  19928. (point)
  19929. (progn (skip-chars-forward "^\r\n") (point))))
  19930. (setq donep (string-match org-looking-at-done-regexp head))
  19931. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19932. (setq timestr
  19933. (concat (substring s (match-beginning 1)) " "))
  19934. (setq timestr 'time))
  19935. (if (and donep
  19936. (or org-agenda-skip-scheduled-if-done
  19937. (not (= diff 0))))
  19938. (setq txt nil)
  19939. (setq txt (org-format-agenda-item
  19940. (if (= diff 0)
  19941. (car org-agenda-scheduled-leaders)
  19942. (format (nth 1 org-agenda-scheduled-leaders)
  19943. (- 1 diff)))
  19944. head category tags timestr))))
  19945. (setq txt org-agenda-no-heading-message))
  19946. (when txt
  19947. (setq face (if pastschedp
  19948. 'org-scheduled-previously
  19949. 'org-scheduled-today))
  19950. (org-add-props txt props
  19951. 'undone-face face
  19952. 'face (if donep 'org-done face)
  19953. 'org-marker (org-agenda-new-marker pos)
  19954. 'org-hd-marker (org-agenda-new-marker pos1)
  19955. 'type (if pastschedp "past-scheduled" "scheduled")
  19956. 'date (if pastschedp d2 date)
  19957. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19958. 'org-category category)
  19959. (push txt ee))))))
  19960. (nreverse ee)))
  19961. (defun org-agenda-get-blocks ()
  19962. "Return the date-range information for agenda display."
  19963. (let* ((props (list 'face nil
  19964. 'org-not-done-regexp org-not-done-regexp
  19965. 'org-todo-regexp org-todo-regexp
  19966. 'mouse-face 'highlight
  19967. 'keymap org-agenda-keymap
  19968. 'help-echo
  19969. (format "mouse-2 or RET jump to org file %s"
  19970. (abbreviate-file-name buffer-file-name))))
  19971. (regexp org-tr-regexp)
  19972. (d0 (calendar-absolute-from-gregorian date))
  19973. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19974. donep head)
  19975. (goto-char (point-min))
  19976. (while (re-search-forward regexp nil t)
  19977. (catch :skip
  19978. (org-agenda-skip)
  19979. (setq pos (point))
  19980. (setq timestr (match-string 0)
  19981. s1 (match-string 1)
  19982. s2 (match-string 2)
  19983. d1 (time-to-days (org-time-string-to-time s1))
  19984. d2 (time-to-days (org-time-string-to-time s2)))
  19985. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19986. ;; Only allow days between the limits, because the normal
  19987. ;; date stamps will catch the limits.
  19988. (save-excursion
  19989. (setq marker (org-agenda-new-marker (point)))
  19990. (setq category (org-get-category))
  19991. (if (re-search-backward "^\\*+ " nil t)
  19992. (progn
  19993. (goto-char (match-beginning 0))
  19994. (setq hdmarker (org-agenda-new-marker (point)))
  19995. (setq tags (org-get-tags-at))
  19996. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19997. (setq head (match-string 1))
  19998. (and org-agenda-skip-timestamp-if-done
  19999. (org-entry-is-done-p)
  20000. (throw :skip t))
  20001. (setq txt (org-format-agenda-item
  20002. (format (if (= d1 d2) "" "(%d/%d): ")
  20003. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20004. head category tags
  20005. (if (= d0 d1) timestr))))
  20006. (setq txt org-agenda-no-heading-message))
  20007. (org-add-props txt props
  20008. 'org-marker marker 'org-hd-marker hdmarker
  20009. 'type "block" 'date date
  20010. 'priority (org-get-priority txt) 'org-category category)
  20011. (push txt ee)))
  20012. (goto-char pos)))
  20013. ;; Sort the entries by expiration date.
  20014. (nreverse ee)))
  20015. ;;; Agenda presentation and sorting
  20016. (defconst org-plain-time-of-day-regexp
  20017. (concat
  20018. "\\(\\<[012]?[0-9]"
  20019. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20020. "\\(--?"
  20021. "\\(\\<[012]?[0-9]"
  20022. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20023. "\\)?")
  20024. "Regular expression to match a plain time or time range.
  20025. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20026. groups carry important information:
  20027. 0 the full match
  20028. 1 the first time, range or not
  20029. 8 the second time, if it is a range.")
  20030. (defconst org-plain-time-extension-regexp
  20031. (concat
  20032. "\\(\\<[012]?[0-9]"
  20033. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20034. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20035. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20036. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20037. groups carry important information:
  20038. 0 the full match
  20039. 7 hours of duration
  20040. 9 minutes of duration")
  20041. (defconst org-stamp-time-of-day-regexp
  20042. (concat
  20043. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20044. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20045. "\\(--?"
  20046. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20047. "Regular expression to match a timestamp time or time range.
  20048. After a match, the following groups carry important information:
  20049. 0 the full match
  20050. 1 date plus weekday, for backreferencing to make sure both times on same day
  20051. 2 the first time, range or not
  20052. 4 the second time, if it is a range.")
  20053. (defvar org-prefix-has-time nil
  20054. "A flag, set by `org-compile-prefix-format'.
  20055. The flag is set if the currently compiled format contains a `%t'.")
  20056. (defvar org-prefix-has-tag nil
  20057. "A flag, set by `org-compile-prefix-format'.
  20058. The flag is set if the currently compiled format contains a `%T'.")
  20059. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20060. noprefix remove-re)
  20061. "Format TXT to be inserted into the agenda buffer.
  20062. In particular, it adds the prefix and corresponding text properties. EXTRA
  20063. must be a string and replaces the `%s' specifier in the prefix format.
  20064. CATEGORY (string, symbol or nil) may be used to overrule the default
  20065. category taken from local variable or file name. It will replace the `%c'
  20066. specifier in the format. DOTIME, when non-nil, indicates that a
  20067. time-of-day should be extracted from TXT for sorting of this entry, and for
  20068. the `%t' specifier in the format. When DOTIME is a string, this string is
  20069. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20070. only the correctly processes TXT should be returned - this is used by
  20071. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20072. Any match of REMOVE-RE will be removed from TXT."
  20073. (save-match-data
  20074. ;; Diary entries sometimes have extra whitespace at the beginning
  20075. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20076. (let* ((category (or category
  20077. org-category
  20078. (if buffer-file-name
  20079. (file-name-sans-extension
  20080. (file-name-nondirectory buffer-file-name))
  20081. "")))
  20082. (tag (if tags (nth (1- (length tags)) tags) ""))
  20083. time ; time and tag are needed for the eval of the prefix format
  20084. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20085. (time-of-day (and dotime (org-get-time-of-day ts)))
  20086. stamp plain s0 s1 s2 rtn srp)
  20087. (when (and dotime time-of-day org-prefix-has-time)
  20088. ;; Extract starting and ending time and move them to prefix
  20089. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20090. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20091. (setq s0 (match-string 0 ts)
  20092. srp (and stamp (match-end 3))
  20093. s1 (match-string (if plain 1 2) ts)
  20094. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20095. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20096. ;; them, we might want to remove them there to avoid duplication.
  20097. ;; The user can turn this off with a variable.
  20098. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20099. (string-match (concat (regexp-quote s0) " *") txt)
  20100. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20101. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20102. (= (match-beginning 0) 0)
  20103. t))
  20104. (setq txt (replace-match "" nil nil txt))))
  20105. ;; Normalize the time(s) to 24 hour
  20106. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20107. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20108. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20109. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20110. (let ((m (+ (string-to-number (match-string 2 s1))
  20111. (* 60 (string-to-number (match-string 1 s1)))
  20112. org-agenda-default-appointment-duration))
  20113. h)
  20114. (setq h (/ m 60) m (- m (* h 60)))
  20115. (setq s2 (format "%02d:%02d" h m))))
  20116. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20117. txt)
  20118. ;; Tags are in the string
  20119. (if (or (eq org-agenda-remove-tags t)
  20120. (and org-agenda-remove-tags
  20121. org-prefix-has-tag))
  20122. (setq txt (replace-match "" t t txt))
  20123. (setq txt (replace-match
  20124. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20125. (match-string 2 txt))
  20126. t t txt))))
  20127. (when remove-re
  20128. (while (string-match remove-re txt)
  20129. (setq txt (replace-match "" t t txt))))
  20130. ;; Create the final string
  20131. (if noprefix
  20132. (setq rtn txt)
  20133. ;; Prepare the variables needed in the eval of the compiled format
  20134. (setq time (cond (s2 (concat s1 "-" s2))
  20135. (s1 (concat s1 "......"))
  20136. (t ""))
  20137. extra (or extra "")
  20138. category (if (symbolp category) (symbol-name category) category))
  20139. ;; Evaluate the compiled format
  20140. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20141. ;; And finally add the text properties
  20142. (org-add-props rtn nil
  20143. 'org-category (downcase category) 'tags tags
  20144. 'org-highest-priority org-highest-priority
  20145. 'org-lowest-priority org-lowest-priority
  20146. 'prefix-length (- (length rtn) (length txt))
  20147. 'time-of-day time-of-day
  20148. 'txt txt
  20149. 'time time
  20150. 'extra extra
  20151. 'dotime dotime))))
  20152. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20153. (defvar org-agenda-sorting-strategy-selected nil)
  20154. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20155. (catch 'exit
  20156. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20157. ((and todayp (member 'today (car org-agenda-time-grid))))
  20158. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20159. ((member 'weekly (car org-agenda-time-grid)))
  20160. (t (throw 'exit list)))
  20161. (let* ((have (delq nil (mapcar
  20162. (lambda (x) (get-text-property 1 'time-of-day x))
  20163. list)))
  20164. (string (nth 1 org-agenda-time-grid))
  20165. (gridtimes (nth 2 org-agenda-time-grid))
  20166. (req (car org-agenda-time-grid))
  20167. (remove (member 'remove-match req))
  20168. new time)
  20169. (if (and (member 'require-timed req) (not have))
  20170. ;; don't show empty grid
  20171. (throw 'exit list))
  20172. (while (setq time (pop gridtimes))
  20173. (unless (and remove (member time have))
  20174. (setq time (int-to-string time))
  20175. (push (org-format-agenda-item
  20176. nil string "" nil
  20177. (concat (substring time 0 -2) ":" (substring time -2)))
  20178. new)
  20179. (put-text-property
  20180. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20181. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20182. (append new list)
  20183. (append list new)))))
  20184. (defun org-compile-prefix-format (key)
  20185. "Compile the prefix format into a Lisp form that can be evaluated.
  20186. The resulting form is returned and stored in the variable
  20187. `org-prefix-format-compiled'."
  20188. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20189. (let ((s (cond
  20190. ((stringp org-agenda-prefix-format)
  20191. org-agenda-prefix-format)
  20192. ((assq key org-agenda-prefix-format)
  20193. (cdr (assq key org-agenda-prefix-format)))
  20194. (t " %-12:c%?-12t% s")))
  20195. (start 0)
  20196. varform vars var e c f opt)
  20197. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20198. s start)
  20199. (setq var (cdr (assoc (match-string 4 s)
  20200. '(("c" . category) ("t" . time) ("s" . extra)
  20201. ("T" . tag))))
  20202. c (or (match-string 3 s) "")
  20203. opt (match-beginning 1)
  20204. start (1+ (match-beginning 0)))
  20205. (if (equal var 'time) (setq org-prefix-has-time t))
  20206. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20207. (setq f (concat "%" (match-string 2 s) "s"))
  20208. (if opt
  20209. (setq varform
  20210. `(if (equal "" ,var)
  20211. ""
  20212. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20213. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20214. (setq s (replace-match "%s" t nil s))
  20215. (push varform vars))
  20216. (setq vars (nreverse vars))
  20217. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20218. (defun org-set-sorting-strategy (key)
  20219. (if (symbolp (car org-agenda-sorting-strategy))
  20220. ;; the old format
  20221. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20222. (setq org-agenda-sorting-strategy-selected
  20223. (or (cdr (assq key org-agenda-sorting-strategy))
  20224. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20225. '(time-up category-keep priority-down)))))
  20226. (defun org-get-time-of-day (s &optional string mod24)
  20227. "Check string S for a time of day.
  20228. If found, return it as a military time number between 0 and 2400.
  20229. If not found, return nil.
  20230. The optional STRING argument forces conversion into a 5 character wide string
  20231. HH:MM."
  20232. (save-match-data
  20233. (when
  20234. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20235. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20236. (let* ((h (string-to-number (match-string 1 s)))
  20237. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20238. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20239. (am-p (equal ampm "am"))
  20240. (h1 (cond ((not ampm) h)
  20241. ((= h 12) (if am-p 0 12))
  20242. (t (+ h (if am-p 0 12)))))
  20243. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20244. (mod h1 24) h1))
  20245. (t0 (+ (* 100 h2) m))
  20246. (t1 (concat (if (>= h1 24) "+" " ")
  20247. (if (< t0 100) "0" "")
  20248. (if (< t0 10) "0" "")
  20249. (int-to-string t0))))
  20250. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20251. (defun org-finalize-agenda-entries (list &optional nosort)
  20252. "Sort and concatenate the agenda items."
  20253. (setq list (mapcar 'org-agenda-highlight-todo list))
  20254. (if nosort
  20255. list
  20256. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20257. (defun org-agenda-highlight-todo (x)
  20258. (let (re pl)
  20259. (if (eq x 'line)
  20260. (save-excursion
  20261. (beginning-of-line 1)
  20262. (setq re (get-text-property (point) 'org-todo-regexp))
  20263. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20264. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20265. (add-text-properties (match-beginning 0) (match-end 0)
  20266. (list 'face (org-get-todo-face 0)))
  20267. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20268. (delete-region (match-beginning 1) (1- (match-end 0)))
  20269. (goto-char (match-beginning 1))
  20270. (insert (format org-agenda-todo-keyword-format s)))))
  20271. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20272. pl (get-text-property 0 'prefix-length x))
  20273. (when (and re
  20274. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20275. x (or pl 0)) pl))
  20276. (add-text-properties
  20277. (or (match-end 1) (match-end 0)) (match-end 0)
  20278. (list 'face (org-get-todo-face (match-string 2 x)))
  20279. x)
  20280. (setq x (concat (substring x 0 (match-end 1))
  20281. (format org-agenda-todo-keyword-format
  20282. (match-string 2 x))
  20283. " "
  20284. (substring x (match-end 3)))))
  20285. x)))
  20286. (defsubst org-cmp-priority (a b)
  20287. "Compare the priorities of string A and B."
  20288. (let ((pa (or (get-text-property 1 'priority a) 0))
  20289. (pb (or (get-text-property 1 'priority b) 0)))
  20290. (cond ((> pa pb) +1)
  20291. ((< pa pb) -1)
  20292. (t nil))))
  20293. (defsubst org-cmp-category (a b)
  20294. "Compare the string values of categories of strings A and B."
  20295. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20296. (cb (or (get-text-property 1 'org-category b) "")))
  20297. (cond ((string-lessp ca cb) -1)
  20298. ((string-lessp cb ca) +1)
  20299. (t nil))))
  20300. (defsubst org-cmp-tag (a b)
  20301. "Compare the string values of categories of strings A and B."
  20302. (let ((ta (car (last (get-text-property 1 'tags a))))
  20303. (tb (car (last (get-text-property 1 'tags b)))))
  20304. (cond ((not ta) +1)
  20305. ((not tb) -1)
  20306. ((string-lessp ta tb) -1)
  20307. ((string-lessp tb ta) +1)
  20308. (t nil))))
  20309. (defsubst org-cmp-time (a b)
  20310. "Compare the time-of-day values of strings A and B."
  20311. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20312. (ta (or (get-text-property 1 'time-of-day a) def))
  20313. (tb (or (get-text-property 1 'time-of-day b) def)))
  20314. (cond ((< ta tb) -1)
  20315. ((< tb ta) +1)
  20316. (t nil))))
  20317. (defun org-entries-lessp (a b)
  20318. "Predicate for sorting agenda entries."
  20319. ;; The following variables will be used when the form is evaluated.
  20320. ;; So even though the compiler complains, keep them.
  20321. (let* ((time-up (org-cmp-time a b))
  20322. (time-down (if time-up (- time-up) nil))
  20323. (priority-up (org-cmp-priority a b))
  20324. (priority-down (if priority-up (- priority-up) nil))
  20325. (category-up (org-cmp-category a b))
  20326. (category-down (if category-up (- category-up) nil))
  20327. (category-keep (if category-up +1 nil))
  20328. (tag-up (org-cmp-tag a b))
  20329. (tag-down (if tag-up (- tag-up) nil)))
  20330. (cdr (assoc
  20331. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20332. '((-1 . t) (1 . nil) (nil . nil))))))
  20333. ;;; Agenda restriction lock
  20334. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20335. "Overlay to mark the headline to which arenda commands are restricted.")
  20336. (org-overlay-put org-agenda-restriction-lock-overlay
  20337. 'face 'org-agenda-restriction-lock)
  20338. (org-overlay-put org-agenda-restriction-lock-overlay
  20339. 'help-echo "Agendas are currently limited to this subtree.")
  20340. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20341. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20342. "Overlay marking the agenda restriction line in speedbar.")
  20343. (org-overlay-put org-speedbar-restriction-lock-overlay
  20344. 'face 'org-agenda-restriction-lock)
  20345. (org-overlay-put org-speedbar-restriction-lock-overlay
  20346. 'help-echo "Agendas are currently limited to this item.")
  20347. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20348. (defun org-agenda-set-restriction-lock (&optional type)
  20349. "Set restriction lock for agenda, to current subtree or file.
  20350. Restriction will be the file if TYPE is `file', or if type is the
  20351. universal prefix '(4), or if the cursor is before the first headline
  20352. in the file. Otherwise, restriction will be to the current subtree."
  20353. (interactive "P")
  20354. (and (equal type '(4)) (setq type 'file))
  20355. (setq type (cond
  20356. (type type)
  20357. ((org-at-heading-p) 'subtree)
  20358. ((condition-case nil (org-back-to-heading t) (error nil))
  20359. 'subtree)
  20360. (t 'file)))
  20361. (if (eq type 'subtree)
  20362. (progn
  20363. (setq org-agenda-restrict t)
  20364. (setq org-agenda-overriding-restriction 'subtree)
  20365. (put 'org-agenda-files 'org-restrict
  20366. (list (buffer-file-name (buffer-base-buffer))))
  20367. (org-back-to-heading t)
  20368. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20369. (move-marker org-agenda-restrict-begin (point))
  20370. (move-marker org-agenda-restrict-end
  20371. (save-excursion (org-end-of-subtree t)))
  20372. (message "Locking agenda restriction to subtree"))
  20373. (put 'org-agenda-files 'org-restrict
  20374. (list (buffer-file-name (buffer-base-buffer))))
  20375. (setq org-agenda-restrict nil)
  20376. (setq org-agenda-overriding-restriction 'file)
  20377. (move-marker org-agenda-restrict-begin nil)
  20378. (move-marker org-agenda-restrict-end nil)
  20379. (message "Locking agenda restriction to file"))
  20380. (setq current-prefix-arg nil)
  20381. (org-agenda-maybe-redo))
  20382. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20383. "Remove the agenda restriction lock."
  20384. (interactive "P")
  20385. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20386. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20387. (setq org-agenda-overriding-restriction nil)
  20388. (setq org-agenda-restrict nil)
  20389. (put 'org-agenda-files 'org-restrict nil)
  20390. (move-marker org-agenda-restrict-begin nil)
  20391. (move-marker org-agenda-restrict-end nil)
  20392. (setq current-prefix-arg nil)
  20393. (message "Agenda restriction lock removed")
  20394. (or noupdate (org-agenda-maybe-redo)))
  20395. (defun org-agenda-maybe-redo ()
  20396. "If there is any window showing the agenda view, update it."
  20397. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20398. (w0 (selected-window)))
  20399. (when w
  20400. (select-window w)
  20401. (org-agenda-redo)
  20402. (select-window w0)
  20403. (if org-agenda-overriding-restriction
  20404. (message "Agenda view shifted to new %s restriction"
  20405. org-agenda-overriding-restriction)
  20406. (message "Agenda restriction lock removed")))))
  20407. ;;; Agenda commands
  20408. (defun org-agenda-check-type (error &rest types)
  20409. "Check if agenda buffer is of allowed type.
  20410. If ERROR is non-nil, throw an error, otherwise just return nil."
  20411. (if (memq org-agenda-type types)
  20412. t
  20413. (if error
  20414. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20415. nil)))
  20416. (defun org-agenda-quit ()
  20417. "Exit agenda by removing the window or the buffer."
  20418. (interactive)
  20419. (let ((buf (current-buffer)))
  20420. (if (not (one-window-p)) (delete-window))
  20421. (kill-buffer buf)
  20422. (org-agenda-reset-markers)
  20423. (org-columns-remove-overlays))
  20424. ;; Maybe restore the pre-agenda window configuration.
  20425. (and org-agenda-restore-windows-after-quit
  20426. (not (eq org-agenda-window-setup 'other-frame))
  20427. org-pre-agenda-window-conf
  20428. (set-window-configuration org-pre-agenda-window-conf)))
  20429. (defun org-agenda-exit ()
  20430. "Exit agenda by removing the window or the buffer.
  20431. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20432. Org-mode buffers visited directly by the user will not be touched."
  20433. (interactive)
  20434. (org-release-buffers org-agenda-new-buffers)
  20435. (setq org-agenda-new-buffers nil)
  20436. (org-agenda-quit))
  20437. (defun org-agenda-execute (arg)
  20438. "Execute another agenda command, keeping same window.\\<global-map>
  20439. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20440. (interactive "P")
  20441. (let ((org-agenda-window-setup 'current-window))
  20442. (org-agenda arg)))
  20443. (defun org-save-all-org-buffers ()
  20444. "Save all Org-mode buffers without user confirmation."
  20445. (interactive)
  20446. (message "Saving all Org-mode buffers...")
  20447. (save-some-buffers t 'org-mode-p)
  20448. (message "Saving all Org-mode buffers... done"))
  20449. (defun org-agenda-redo ()
  20450. "Rebuild Agenda.
  20451. When this is the global TODO list, a prefix argument will be interpreted."
  20452. (interactive)
  20453. (let* ((org-agenda-keep-modes t)
  20454. (line (org-current-line))
  20455. (window-line (- line (org-current-line (window-start))))
  20456. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20457. (message "Rebuilding agenda buffer...")
  20458. (org-let lprops '(eval org-agenda-redo-command))
  20459. (setq org-agenda-undo-list nil
  20460. org-agenda-pending-undo-list nil)
  20461. (message "Rebuilding agenda buffer...done")
  20462. (goto-line line)
  20463. (recenter window-line)))
  20464. (defun org-agenda-goto-date (date)
  20465. "Jump to DATE in agenda."
  20466. (interactive (list (org-read-date)))
  20467. (org-agenda-list nil date))
  20468. (defun org-agenda-goto-today ()
  20469. "Go to today."
  20470. (interactive)
  20471. (org-agenda-check-type t 'timeline 'agenda)
  20472. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20473. (cond
  20474. (tdpos (goto-char tdpos))
  20475. ((eq org-agenda-type 'agenda)
  20476. (let* ((sd (time-to-days
  20477. (time-subtract (current-time)
  20478. (list 0 (* 3600 org-extend-today-until) 0))))
  20479. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20480. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20481. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20482. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20483. (org-agenda-redo)
  20484. (org-agenda-find-same-or-today-or-agenda)))
  20485. (t (error "Cannot find today")))))
  20486. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20487. (goto-char
  20488. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20489. (text-property-any (point-min) (point-max) 'org-today t)
  20490. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20491. (point-min))))
  20492. (defun org-agenda-later (arg)
  20493. "Go forward in time by thee current span.
  20494. With prefix ARG, go forward that many times the current span."
  20495. (interactive "p")
  20496. (org-agenda-check-type t 'agenda)
  20497. (let* ((span org-agenda-span)
  20498. (sd org-starting-day)
  20499. (greg (calendar-gregorian-from-absolute sd))
  20500. (cnt (get-text-property (point) 'org-day-cnt))
  20501. greg2 nd)
  20502. (cond
  20503. ((eq span 'day)
  20504. (setq sd (+ arg sd) nd 1))
  20505. ((eq span 'week)
  20506. (setq sd (+ (* 7 arg) sd) nd 7))
  20507. ((eq span 'month)
  20508. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20509. sd (calendar-absolute-from-gregorian greg2))
  20510. (setcar greg2 (1+ (car greg2)))
  20511. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20512. ((eq span 'year)
  20513. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20514. sd (calendar-absolute-from-gregorian greg2))
  20515. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20516. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20517. (let ((org-agenda-overriding-arguments
  20518. (list (car org-agenda-last-arguments) sd nd t)))
  20519. (org-agenda-redo)
  20520. (org-agenda-find-same-or-today-or-agenda cnt))))
  20521. (defun org-agenda-earlier (arg)
  20522. "Go backward in time by the current span.
  20523. With prefix ARG, go backward that many times the current span."
  20524. (interactive "p")
  20525. (org-agenda-later (- arg)))
  20526. (defun org-agenda-day-view ()
  20527. "Switch to daily view for agenda."
  20528. (interactive)
  20529. (setq org-agenda-ndays 1)
  20530. (org-agenda-change-time-span 'day))
  20531. (defun org-agenda-week-view ()
  20532. "Switch to daily view for agenda."
  20533. (interactive)
  20534. (setq org-agenda-ndays 7)
  20535. (org-agenda-change-time-span 'week))
  20536. (defun org-agenda-month-view ()
  20537. "Switch to daily view for agenda."
  20538. (interactive)
  20539. (org-agenda-change-time-span 'month))
  20540. (defun org-agenda-year-view ()
  20541. "Switch to daily view for agenda."
  20542. (interactive)
  20543. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20544. (org-agenda-change-time-span 'year)
  20545. (error "Abort")))
  20546. (defun org-agenda-change-time-span (span)
  20547. "Change the agenda view to SPAN.
  20548. SPAN may be `day', `week', `month', `year'."
  20549. (org-agenda-check-type t 'agenda)
  20550. (if (equal org-agenda-span span)
  20551. (error "Viewing span is already \"%s\"" span))
  20552. (let* ((sd (or (get-text-property (point) 'day)
  20553. org-starting-day))
  20554. (computed (org-agenda-compute-time-span sd span))
  20555. (org-agenda-overriding-arguments
  20556. (list (car org-agenda-last-arguments)
  20557. (car computed) (cdr computed) t)))
  20558. (org-agenda-redo)
  20559. (org-agenda-find-same-or-today-or-agenda))
  20560. (org-agenda-set-mode-name)
  20561. (message "Switched to %s view" span))
  20562. (defun org-agenda-compute-time-span (sd span)
  20563. "Compute starting date and number of days for agenda.
  20564. SPAN may be `day', `week', `month', `year'. The return value
  20565. is a cons cell with the starting date and the number of days,
  20566. so that the date SD will be in that range."
  20567. (let* ((greg (calendar-gregorian-from-absolute sd))
  20568. nd)
  20569. (cond
  20570. ((eq span 'day)
  20571. (setq nd 1))
  20572. ((eq span 'week)
  20573. (let* ((nt (calendar-day-of-week
  20574. (calendar-gregorian-from-absolute sd)))
  20575. (d (if org-agenda-start-on-weekday
  20576. (- nt org-agenda-start-on-weekday)
  20577. 0)))
  20578. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20579. (setq nd 7)))
  20580. ((eq span 'month)
  20581. (setq sd (calendar-absolute-from-gregorian
  20582. (list (car greg) 1 (nth 2 greg)))
  20583. nd (- (calendar-absolute-from-gregorian
  20584. (list (1+ (car greg)) 1 (nth 2 greg)))
  20585. sd)))
  20586. ((eq span 'year)
  20587. (setq sd (calendar-absolute-from-gregorian
  20588. (list 1 1 (nth 2 greg)))
  20589. nd (- (calendar-absolute-from-gregorian
  20590. (list 1 1 (1+ (nth 2 greg))))
  20591. sd))))
  20592. (cons sd nd)))
  20593. ;; FIXME: does not work if user makes date format that starts with a blank
  20594. (defun org-agenda-next-date-line (&optional arg)
  20595. "Jump to the next line indicating a date in agenda buffer."
  20596. (interactive "p")
  20597. (org-agenda-check-type t 'agenda 'timeline)
  20598. (beginning-of-line 1)
  20599. (if (looking-at "^\\S-") (forward-char 1))
  20600. (if (not (re-search-forward "^\\S-" nil t arg))
  20601. (progn
  20602. (backward-char 1)
  20603. (error "No next date after this line in this buffer")))
  20604. (goto-char (match-beginning 0)))
  20605. (defun org-agenda-previous-date-line (&optional arg)
  20606. "Jump to the previous line indicating a date in agenda buffer."
  20607. (interactive "p")
  20608. (org-agenda-check-type t 'agenda 'timeline)
  20609. (beginning-of-line 1)
  20610. (if (not (re-search-backward "^\\S-" nil t arg))
  20611. (error "No previous date before this line in this buffer")))
  20612. ;; Initialize the highlight
  20613. (defvar org-hl (org-make-overlay 1 1))
  20614. (org-overlay-put org-hl 'face 'highlight)
  20615. (defun org-highlight (begin end &optional buffer)
  20616. "Highlight a region with overlay."
  20617. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20618. org-hl begin end (or buffer (current-buffer))))
  20619. (defun org-unhighlight ()
  20620. "Detach overlay INDEX."
  20621. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20622. ;; FIXME this is currently not used.
  20623. (defun org-highlight-until-next-command (beg end &optional buffer)
  20624. (org-highlight beg end buffer)
  20625. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20626. (defun org-unhighlight-once ()
  20627. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20628. (org-unhighlight))
  20629. (defun org-agenda-follow-mode ()
  20630. "Toggle follow mode in an agenda buffer."
  20631. (interactive)
  20632. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20633. (org-agenda-set-mode-name)
  20634. (message "Follow mode is %s"
  20635. (if org-agenda-follow-mode "on" "off")))
  20636. (defun org-agenda-log-mode ()
  20637. "Toggle log mode in an agenda buffer."
  20638. (interactive)
  20639. (org-agenda-check-type t 'agenda 'timeline)
  20640. (setq org-agenda-show-log (not org-agenda-show-log))
  20641. (org-agenda-set-mode-name)
  20642. (org-agenda-redo)
  20643. (message "Log mode is %s"
  20644. (if org-agenda-show-log "on" "off")))
  20645. (defun org-agenda-toggle-diary ()
  20646. "Toggle diary inclusion in an agenda buffer."
  20647. (interactive)
  20648. (org-agenda-check-type t 'agenda)
  20649. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20650. (org-agenda-redo)
  20651. (org-agenda-set-mode-name)
  20652. (message "Diary inclusion turned %s"
  20653. (if org-agenda-include-diary "on" "off")))
  20654. (defun org-agenda-toggle-time-grid ()
  20655. "Toggle time grid in an agenda buffer."
  20656. (interactive)
  20657. (org-agenda-check-type t 'agenda)
  20658. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20659. (org-agenda-redo)
  20660. (org-agenda-set-mode-name)
  20661. (message "Time-grid turned %s"
  20662. (if org-agenda-use-time-grid "on" "off")))
  20663. (defun org-agenda-set-mode-name ()
  20664. "Set the mode name to indicate all the small mode settings."
  20665. (setq mode-name
  20666. (concat "Org-Agenda"
  20667. (if (equal org-agenda-ndays 1) " Day" "")
  20668. (if (equal org-agenda-ndays 7) " Week" "")
  20669. (if org-agenda-follow-mode " Follow" "")
  20670. (if org-agenda-include-diary " Diary" "")
  20671. (if org-agenda-use-time-grid " Grid" "")
  20672. (if org-agenda-show-log " Log" "")))
  20673. (force-mode-line-update))
  20674. (defun org-agenda-post-command-hook ()
  20675. (and (eolp) (not (bolp)) (backward-char 1))
  20676. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20677. (if (and org-agenda-follow-mode
  20678. (get-text-property (point) 'org-marker))
  20679. (org-agenda-show)))
  20680. (defun org-agenda-show-priority ()
  20681. "Show the priority of the current item.
  20682. This priority is composed of the main priority given with the [#A] cookies,
  20683. and by additional input from the age of a schedules or deadline entry."
  20684. (interactive)
  20685. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20686. (message "Priority is %d" (if pri pri -1000))))
  20687. (defun org-agenda-show-tags ()
  20688. "Show the tags applicable to the current item."
  20689. (interactive)
  20690. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20691. (if tags
  20692. (message "Tags are :%s:"
  20693. (org-no-properties (mapconcat 'identity tags ":")))
  20694. (message "No tags associated with this line"))))
  20695. (defun org-agenda-goto (&optional highlight)
  20696. "Go to the Org-mode file which contains the item at point."
  20697. (interactive)
  20698. (let* ((marker (or (get-text-property (point) 'org-marker)
  20699. (org-agenda-error)))
  20700. (buffer (marker-buffer marker))
  20701. (pos (marker-position marker)))
  20702. (switch-to-buffer-other-window buffer)
  20703. (widen)
  20704. (goto-char pos)
  20705. (when (org-mode-p)
  20706. (org-show-context 'agenda)
  20707. (save-excursion
  20708. (and (outline-next-heading)
  20709. (org-flag-heading nil)))) ; show the next heading
  20710. (recenter (/ (window-height) 2))
  20711. (run-hooks 'org-agenda-after-show-hook)
  20712. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20713. (defvar org-agenda-after-show-hook nil
  20714. "Normal hook run after an item has been shown from the agenda.
  20715. Point is in the buffer where the item originated.")
  20716. (defun org-agenda-kill ()
  20717. "Kill the entry or subtree belonging to the current agenda entry."
  20718. (interactive)
  20719. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20720. (let* ((marker (or (get-text-property (point) 'org-marker)
  20721. (org-agenda-error)))
  20722. (buffer (marker-buffer marker))
  20723. (pos (marker-position marker))
  20724. (type (get-text-property (point) 'type))
  20725. dbeg dend (n 0) conf)
  20726. (org-with-remote-undo buffer
  20727. (with-current-buffer buffer
  20728. (save-excursion
  20729. (goto-char pos)
  20730. (if (and (org-mode-p) (not (member type '("sexp"))))
  20731. (setq dbeg (progn (org-back-to-heading t) (point))
  20732. dend (org-end-of-subtree t t))
  20733. (setq dbeg (point-at-bol)
  20734. dend (min (point-max) (1+ (point-at-eol)))))
  20735. (goto-char dbeg)
  20736. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20737. (setq conf (or (eq t org-agenda-confirm-kill)
  20738. (and (numberp org-agenda-confirm-kill)
  20739. (> n org-agenda-confirm-kill))))
  20740. (and conf
  20741. (not (y-or-n-p
  20742. (format "Delete entry with %d lines in buffer \"%s\"? "
  20743. n (buffer-name buffer))))
  20744. (error "Abort"))
  20745. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20746. (with-current-buffer buffer (delete-region dbeg dend))
  20747. (message "Agenda item and source killed"))))
  20748. (defun org-agenda-archive ()
  20749. "Kill the entry or subtree belonging to the current agenda entry."
  20750. (interactive)
  20751. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20752. (let* ((marker (or (get-text-property (point) 'org-marker)
  20753. (org-agenda-error)))
  20754. (buffer (marker-buffer marker))
  20755. (pos (marker-position marker)))
  20756. (org-with-remote-undo buffer
  20757. (with-current-buffer buffer
  20758. (if (org-mode-p)
  20759. (save-excursion
  20760. (goto-char pos)
  20761. (org-remove-subtree-entries-from-agenda)
  20762. (org-back-to-heading t)
  20763. (org-archive-subtree))
  20764. (error "Archiving works only in Org-mode files"))))))
  20765. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20766. "Remove all lines in the agenda that correspond to a given subtree.
  20767. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20768. If this information is not given, the function uses the tree at point."
  20769. (let ((buf (or buf (current-buffer))) m p)
  20770. (save-excursion
  20771. (unless (and beg end)
  20772. (org-back-to-heading t)
  20773. (setq beg (point))
  20774. (org-end-of-subtree t)
  20775. (setq end (point)))
  20776. (set-buffer (get-buffer org-agenda-buffer-name))
  20777. (save-excursion
  20778. (goto-char (point-max))
  20779. (beginning-of-line 1)
  20780. (while (not (bobp))
  20781. (when (and (setq m (get-text-property (point) 'org-marker))
  20782. (equal buf (marker-buffer m))
  20783. (setq p (marker-position m))
  20784. (>= p beg)
  20785. (<= p end))
  20786. (let ((inhibit-read-only t))
  20787. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20788. (beginning-of-line 0))))))
  20789. (defun org-agenda-open-link ()
  20790. "Follow the link in the current line, if any."
  20791. (interactive)
  20792. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20793. (save-excursion
  20794. (save-restriction
  20795. (narrow-to-region (point-at-bol) (point-at-eol))
  20796. (org-open-at-point))))
  20797. (defun org-agenda-copy-local-variable (var)
  20798. "Get a variable from a referenced buffer and install it here."
  20799. (let ((m (get-text-property (point) 'org-marker)))
  20800. (when (and m (buffer-live-p (marker-buffer m)))
  20801. (org-set-local var (with-current-buffer (marker-buffer m)
  20802. (symbol-value var))))))
  20803. (defun org-agenda-switch-to (&optional delete-other-windows)
  20804. "Go to the Org-mode file which contains the item at point."
  20805. (interactive)
  20806. (let* ((marker (or (get-text-property (point) 'org-marker)
  20807. (org-agenda-error)))
  20808. (buffer (marker-buffer marker))
  20809. (pos (marker-position marker)))
  20810. (switch-to-buffer buffer)
  20811. (and delete-other-windows (delete-other-windows))
  20812. (widen)
  20813. (goto-char pos)
  20814. (when (org-mode-p)
  20815. (org-show-context 'agenda)
  20816. (save-excursion
  20817. (and (outline-next-heading)
  20818. (org-flag-heading nil)))))) ; show the next heading
  20819. (defun org-agenda-goto-mouse (ev)
  20820. "Go to the Org-mode file which contains the item at the mouse click."
  20821. (interactive "e")
  20822. (mouse-set-point ev)
  20823. (org-agenda-goto))
  20824. (defun org-agenda-show ()
  20825. "Display the Org-mode file which contains the item at point."
  20826. (interactive)
  20827. (let ((win (selected-window)))
  20828. (org-agenda-goto t)
  20829. (select-window win)))
  20830. (defun org-agenda-recenter (arg)
  20831. "Display the Org-mode file which contains the item at point and recenter."
  20832. (interactive "P")
  20833. (let ((win (selected-window)))
  20834. (org-agenda-goto t)
  20835. (recenter arg)
  20836. (select-window win)))
  20837. (defun org-agenda-show-mouse (ev)
  20838. "Display the Org-mode file which contains the item at the mouse click."
  20839. (interactive "e")
  20840. (mouse-set-point ev)
  20841. (org-agenda-show))
  20842. (defun org-agenda-check-no-diary ()
  20843. "Check if the entry is a diary link and abort if yes."
  20844. (if (get-text-property (point) 'org-agenda-diary-link)
  20845. (org-agenda-error)))
  20846. (defun org-agenda-error ()
  20847. (error "Command not allowed in this line"))
  20848. (defun org-agenda-tree-to-indirect-buffer ()
  20849. "Show the subtree corresponding to the current entry in an indirect buffer.
  20850. This calls the command `org-tree-to-indirect-buffer' from the original
  20851. Org-mode buffer.
  20852. With numerical prefix arg ARG, go up to this level and then take that tree.
  20853. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20854. dedicated frame)."
  20855. (interactive)
  20856. (org-agenda-check-no-diary)
  20857. (let* ((marker (or (get-text-property (point) 'org-marker)
  20858. (org-agenda-error)))
  20859. (buffer (marker-buffer marker))
  20860. (pos (marker-position marker)))
  20861. (with-current-buffer buffer
  20862. (save-excursion
  20863. (goto-char pos)
  20864. (call-interactively 'org-tree-to-indirect-buffer)))))
  20865. (defvar org-last-heading-marker (make-marker)
  20866. "Marker pointing to the headline that last changed its TODO state
  20867. by a remote command from the agenda.")
  20868. (defun org-agenda-todo-nextset ()
  20869. "Switch TODO entry to next sequence."
  20870. (interactive)
  20871. (org-agenda-todo 'nextset))
  20872. (defun org-agenda-todo-previousset ()
  20873. "Switch TODO entry to previous sequence."
  20874. (interactive)
  20875. (org-agenda-todo 'previousset))
  20876. (defun org-agenda-todo (&optional arg)
  20877. "Cycle TODO state of line at point, also in Org-mode file.
  20878. This changes the line at point, all other lines in the agenda referring to
  20879. the same tree node, and the headline of the tree node in the Org-mode file."
  20880. (interactive "P")
  20881. (org-agenda-check-no-diary)
  20882. (let* ((col (current-column))
  20883. (marker (or (get-text-property (point) 'org-marker)
  20884. (org-agenda-error)))
  20885. (buffer (marker-buffer marker))
  20886. (pos (marker-position marker))
  20887. (hdmarker (get-text-property (point) 'org-hd-marker))
  20888. (inhibit-read-only t)
  20889. newhead)
  20890. (org-with-remote-undo buffer
  20891. (with-current-buffer buffer
  20892. (widen)
  20893. (goto-char pos)
  20894. (org-show-context 'agenda)
  20895. (save-excursion
  20896. (and (outline-next-heading)
  20897. (org-flag-heading nil))) ; show the next heading
  20898. (org-todo arg)
  20899. (and (bolp) (forward-char 1))
  20900. (setq newhead (org-get-heading))
  20901. (save-excursion
  20902. (org-back-to-heading)
  20903. (move-marker org-last-heading-marker (point))))
  20904. (beginning-of-line 1)
  20905. (save-excursion
  20906. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20907. (move-to-column col))))
  20908. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20909. "Change all lines in the agenda buffer which match HDMARKER.
  20910. The new content of the line will be NEWHEAD (as modified by
  20911. `org-format-agenda-item'). HDMARKER is checked with
  20912. `equal' against all `org-hd-marker' text properties in the file.
  20913. If FIXFACE is non-nil, the face of each item is modified acording to
  20914. the new TODO state."
  20915. (let* ((inhibit-read-only t)
  20916. props m pl undone-face done-face finish new dotime cat tags)
  20917. (save-excursion
  20918. (goto-char (point-max))
  20919. (beginning-of-line 1)
  20920. (while (not finish)
  20921. (setq finish (bobp))
  20922. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20923. (equal m hdmarker))
  20924. (setq props (text-properties-at (point))
  20925. dotime (get-text-property (point) 'dotime)
  20926. cat (get-text-property (point) 'org-category)
  20927. tags (get-text-property (point) 'tags)
  20928. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20929. pl (get-text-property (point) 'prefix-length)
  20930. undone-face (get-text-property (point) 'undone-face)
  20931. done-face (get-text-property (point) 'done-face))
  20932. (move-to-column pl)
  20933. (cond
  20934. ((equal new "")
  20935. (beginning-of-line 1)
  20936. (and (looking-at ".*\n?") (replace-match "")))
  20937. ((looking-at ".*")
  20938. (replace-match new t t)
  20939. (beginning-of-line 1)
  20940. (add-text-properties (point-at-bol) (point-at-eol) props)
  20941. (when fixface
  20942. (add-text-properties
  20943. (point-at-bol) (point-at-eol)
  20944. (list 'face
  20945. (if org-last-todo-state-is-todo
  20946. undone-face done-face))))
  20947. (org-agenda-highlight-todo 'line)
  20948. (beginning-of-line 1))
  20949. (t (error "Line update did not work"))))
  20950. (beginning-of-line 0)))
  20951. (org-finalize-agenda)))
  20952. (defun org-agenda-align-tags (&optional line)
  20953. "Align all tags in agenda items to `org-agenda-tags-column'."
  20954. (let ((inhibit-read-only t) l c)
  20955. (save-excursion
  20956. (goto-char (if line (point-at-bol) (point-min)))
  20957. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20958. (if line (point-at-eol) nil) t)
  20959. (add-text-properties
  20960. (match-beginning 2) (match-end 2)
  20961. (list 'face (list 'org-tag (get-text-property
  20962. (match-beginning 2) 'face))))
  20963. (setq l (- (match-end 2) (match-beginning 2))
  20964. c (if (< org-agenda-tags-column 0)
  20965. (- (abs org-agenda-tags-column) l)
  20966. org-agenda-tags-column))
  20967. (delete-region (match-beginning 1) (match-end 1))
  20968. (goto-char (match-beginning 1))
  20969. (insert (org-add-props
  20970. (make-string (max 1 (- c (current-column))) ?\ )
  20971. (text-properties-at (point))))))))
  20972. (defun org-agenda-priority-up ()
  20973. "Increase the priority of line at point, also in Org-mode file."
  20974. (interactive)
  20975. (org-agenda-priority 'up))
  20976. (defun org-agenda-priority-down ()
  20977. "Decrease the priority of line at point, also in Org-mode file."
  20978. (interactive)
  20979. (org-agenda-priority 'down))
  20980. (defun org-agenda-priority (&optional force-direction)
  20981. "Set the priority of line at point, also in Org-mode file.
  20982. This changes the line at point, all other lines in the agenda referring to
  20983. the same tree node, and the headline of the tree node in the Org-mode file."
  20984. (interactive)
  20985. (org-agenda-check-no-diary)
  20986. (let* ((marker (or (get-text-property (point) 'org-marker)
  20987. (org-agenda-error)))
  20988. (hdmarker (get-text-property (point) 'org-hd-marker))
  20989. (buffer (marker-buffer hdmarker))
  20990. (pos (marker-position hdmarker))
  20991. (inhibit-read-only t)
  20992. newhead)
  20993. (org-with-remote-undo buffer
  20994. (with-current-buffer buffer
  20995. (widen)
  20996. (goto-char pos)
  20997. (org-show-context 'agenda)
  20998. (save-excursion
  20999. (and (outline-next-heading)
  21000. (org-flag-heading nil))) ; show the next heading
  21001. (funcall 'org-priority force-direction)
  21002. (end-of-line 1)
  21003. (setq newhead (org-get-heading)))
  21004. (org-agenda-change-all-lines newhead hdmarker)
  21005. (beginning-of-line 1))))
  21006. (defun org-get-tags-at (&optional pos)
  21007. "Get a list of all headline tags applicable at POS.
  21008. POS defaults to point. If tags are inherited, the list contains
  21009. the targets in the same sequence as the headlines appear, i.e.
  21010. the tags of the current headline come last."
  21011. (interactive)
  21012. (let (tags lastpos)
  21013. (save-excursion
  21014. (save-restriction
  21015. (widen)
  21016. (goto-char (or pos (point)))
  21017. (save-match-data
  21018. (org-back-to-heading t)
  21019. (condition-case nil
  21020. (while (not (equal lastpos (point)))
  21021. (setq lastpos (point))
  21022. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21023. (setq tags (append (org-split-string
  21024. (org-match-string-no-properties 1) ":")
  21025. tags)))
  21026. (or org-use-tag-inheritance (error ""))
  21027. (org-up-heading-all 1))
  21028. (error nil))))
  21029. tags)))
  21030. ;; FIXME: should fix the tags property of the agenda line.
  21031. (defun org-agenda-set-tags ()
  21032. "Set tags for the current headline."
  21033. (interactive)
  21034. (org-agenda-check-no-diary)
  21035. (if (and (org-region-active-p) (interactive-p))
  21036. (call-interactively 'org-change-tag-in-region)
  21037. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21038. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21039. (org-agenda-error)))
  21040. (buffer (marker-buffer hdmarker))
  21041. (pos (marker-position hdmarker))
  21042. (inhibit-read-only t)
  21043. newhead)
  21044. (org-with-remote-undo buffer
  21045. (with-current-buffer buffer
  21046. (widen)
  21047. (goto-char pos)
  21048. (save-excursion
  21049. (org-show-context 'agenda))
  21050. (save-excursion
  21051. (and (outline-next-heading)
  21052. (org-flag-heading nil))) ; show the next heading
  21053. (goto-char pos)
  21054. (call-interactively 'org-set-tags)
  21055. (end-of-line 1)
  21056. (setq newhead (org-get-heading)))
  21057. (org-agenda-change-all-lines newhead hdmarker)
  21058. (beginning-of-line 1)))))
  21059. (defun org-agenda-toggle-archive-tag ()
  21060. "Toggle the archive tag for the current entry."
  21061. (interactive)
  21062. (org-agenda-check-no-diary)
  21063. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21064. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21065. (org-agenda-error)))
  21066. (buffer (marker-buffer hdmarker))
  21067. (pos (marker-position hdmarker))
  21068. (inhibit-read-only t)
  21069. newhead)
  21070. (org-with-remote-undo buffer
  21071. (with-current-buffer buffer
  21072. (widen)
  21073. (goto-char pos)
  21074. (org-show-context 'agenda)
  21075. (save-excursion
  21076. (and (outline-next-heading)
  21077. (org-flag-heading nil))) ; show the next heading
  21078. (call-interactively 'org-toggle-archive-tag)
  21079. (end-of-line 1)
  21080. (setq newhead (org-get-heading)))
  21081. (org-agenda-change-all-lines newhead hdmarker)
  21082. (beginning-of-line 1))))
  21083. (defun org-agenda-date-later (arg &optional what)
  21084. "Change the date of this item to one day later."
  21085. (interactive "p")
  21086. (org-agenda-check-type t 'agenda 'timeline)
  21087. (org-agenda-check-no-diary)
  21088. (let* ((marker (or (get-text-property (point) 'org-marker)
  21089. (org-agenda-error)))
  21090. (buffer (marker-buffer marker))
  21091. (pos (marker-position marker)))
  21092. (org-with-remote-undo buffer
  21093. (with-current-buffer buffer
  21094. (widen)
  21095. (goto-char pos)
  21096. (if (not (org-at-timestamp-p))
  21097. (error "Cannot find time stamp"))
  21098. (org-timestamp-change arg (or what 'day)))
  21099. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21100. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21101. (defun org-agenda-date-earlier (arg &optional what)
  21102. "Change the date of this item to one day earlier."
  21103. (interactive "p")
  21104. (org-agenda-date-later (- arg) what))
  21105. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21106. "Show new date stamp via text properties."
  21107. ;; We use text properties to make this undoable
  21108. (let ((inhibit-read-only t))
  21109. (setq stamp (concat " " prefix " => " stamp))
  21110. (save-excursion
  21111. (goto-char (point-max))
  21112. (while (not (bobp))
  21113. (when (equal marker (get-text-property (point) 'org-marker))
  21114. (move-to-column (- (window-width) (length stamp)) t)
  21115. (if (featurep 'xemacs)
  21116. ;; Use `duplicable' property to trigger undo recording
  21117. (let ((ex (make-extent nil nil))
  21118. (gl (make-glyph stamp)))
  21119. (set-glyph-face gl 'secondary-selection)
  21120. (set-extent-properties
  21121. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21122. (insert-extent ex (1- (point)) (point-at-eol)))
  21123. (add-text-properties
  21124. (1- (point)) (point-at-eol)
  21125. (list 'display (org-add-props stamp nil
  21126. 'face 'secondary-selection))))
  21127. (beginning-of-line 1))
  21128. (beginning-of-line 0)))))
  21129. (defun org-agenda-date-prompt (arg)
  21130. "Change the date of this item. Date is prompted for, with default today.
  21131. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21132. be used to request time specification in the time stamp."
  21133. (interactive "P")
  21134. (org-agenda-check-type t 'agenda 'timeline)
  21135. (org-agenda-check-no-diary)
  21136. (let* ((marker (or (get-text-property (point) 'org-marker)
  21137. (org-agenda-error)))
  21138. (buffer (marker-buffer marker))
  21139. (pos (marker-position marker)))
  21140. (org-with-remote-undo buffer
  21141. (with-current-buffer buffer
  21142. (widen)
  21143. (goto-char pos)
  21144. (if (not (org-at-timestamp-p))
  21145. (error "Cannot find time stamp"))
  21146. (org-time-stamp arg)
  21147. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21148. (defun org-agenda-schedule (arg)
  21149. "Schedule the item at point."
  21150. (interactive "P")
  21151. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21152. (org-agenda-check-no-diary)
  21153. (let* ((marker (or (get-text-property (point) 'org-marker)
  21154. (org-agenda-error)))
  21155. (buffer (marker-buffer marker))
  21156. (pos (marker-position marker))
  21157. (org-insert-labeled-timestamps-at-point nil)
  21158. ts)
  21159. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21160. (set-marker-insertion-type marker t)
  21161. (org-with-remote-undo buffer
  21162. (with-current-buffer buffer
  21163. (widen)
  21164. (goto-char pos)
  21165. (setq ts (org-schedule arg)))
  21166. (org-agenda-show-new-time marker ts "S"))
  21167. (message "Item scheduled for %s" ts)))
  21168. (defun org-agenda-deadline (arg)
  21169. "Schedule the item at point."
  21170. (interactive "P")
  21171. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21172. (org-agenda-check-no-diary)
  21173. (let* ((marker (or (get-text-property (point) 'org-marker)
  21174. (org-agenda-error)))
  21175. (buffer (marker-buffer marker))
  21176. (pos (marker-position marker))
  21177. (org-insert-labeled-timestamps-at-point nil)
  21178. ts)
  21179. (org-with-remote-undo buffer
  21180. (with-current-buffer buffer
  21181. (widen)
  21182. (goto-char pos)
  21183. (setq ts (org-deadline arg)))
  21184. (org-agenda-show-new-time marker ts "S"))
  21185. (message "Deadline for this item set to %s" ts)))
  21186. (defun org-get-heading (&optional no-tags)
  21187. "Return the heading of the current entry, without the stars."
  21188. (save-excursion
  21189. (org-back-to-heading t)
  21190. (if (looking-at
  21191. (if no-tags
  21192. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21193. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21194. (match-string 1) "")))
  21195. (defun org-agenda-clock-in (&optional arg)
  21196. "Start the clock on the currently selected item."
  21197. (interactive "P")
  21198. (org-agenda-check-no-diary)
  21199. (let* ((marker (or (get-text-property (point) 'org-marker)
  21200. (org-agenda-error)))
  21201. (pos (marker-position marker)))
  21202. (org-with-remote-undo (marker-buffer marker)
  21203. (with-current-buffer (marker-buffer marker)
  21204. (widen)
  21205. (goto-char pos)
  21206. (org-clock-in)))))
  21207. (defun org-agenda-clock-out (&optional arg)
  21208. "Stop the currently running clock."
  21209. (interactive "P")
  21210. (unless (marker-buffer org-clock-marker)
  21211. (error "No running clock"))
  21212. (org-with-remote-undo (marker-buffer org-clock-marker)
  21213. (org-clock-out)))
  21214. (defun org-agenda-clock-cancel (&optional arg)
  21215. "Cancel the currently running clock."
  21216. (interactive "P")
  21217. (unless (marker-buffer org-clock-marker)
  21218. (error "No running clock"))
  21219. (org-with-remote-undo (marker-buffer org-clock-marker)
  21220. (org-clock-cancel)))
  21221. (defun org-agenda-diary-entry ()
  21222. "Make a diary entry, like the `i' command from the calendar.
  21223. All the standard commands work: block, weekly etc."
  21224. (interactive)
  21225. (org-agenda-check-type t 'agenda 'timeline)
  21226. (require 'diary-lib)
  21227. (let* ((char (progn
  21228. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21229. (read-char-exclusive)))
  21230. (cmd (cdr (assoc char
  21231. '((?d . insert-diary-entry)
  21232. (?w . insert-weekly-diary-entry)
  21233. (?m . insert-monthly-diary-entry)
  21234. (?y . insert-yearly-diary-entry)
  21235. (?a . insert-anniversary-diary-entry)
  21236. (?b . insert-block-diary-entry)
  21237. (?c . insert-cyclic-diary-entry)))))
  21238. (oldf (symbol-function 'calendar-cursor-to-date))
  21239. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21240. (point (point))
  21241. (mark (or (mark t) (point))))
  21242. (unless cmd
  21243. (error "No command associated with <%c>" char))
  21244. (unless (and (get-text-property point 'day)
  21245. (or (not (equal ?b char))
  21246. (get-text-property mark 'day)))
  21247. (error "Don't know which date to use for diary entry"))
  21248. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21249. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21250. (let ((calendar-mark-ring
  21251. (list (calendar-gregorian-from-absolute
  21252. (or (get-text-property mark 'day)
  21253. (get-text-property point 'day))))))
  21254. (unwind-protect
  21255. (progn
  21256. (fset 'calendar-cursor-to-date
  21257. (lambda (&optional error)
  21258. (calendar-gregorian-from-absolute
  21259. (get-text-property point 'day))))
  21260. (call-interactively cmd))
  21261. (fset 'calendar-cursor-to-date oldf)))))
  21262. (defun org-agenda-execute-calendar-command (cmd)
  21263. "Execute a calendar command from the agenda, with the date associated to
  21264. the cursor position."
  21265. (org-agenda-check-type t 'agenda 'timeline)
  21266. (require 'diary-lib)
  21267. (unless (get-text-property (point) 'day)
  21268. (error "Don't know which date to use for calendar command"))
  21269. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21270. (point (point))
  21271. (date (calendar-gregorian-from-absolute
  21272. (get-text-property point 'day)))
  21273. ;; the following 3 vars are needed in the calendar
  21274. (displayed-day (extract-calendar-day date))
  21275. (displayed-month (extract-calendar-month date))
  21276. (displayed-year (extract-calendar-year date)))
  21277. (unwind-protect
  21278. (progn
  21279. (fset 'calendar-cursor-to-date
  21280. (lambda (&optional error)
  21281. (calendar-gregorian-from-absolute
  21282. (get-text-property point 'day))))
  21283. (call-interactively cmd))
  21284. (fset 'calendar-cursor-to-date oldf))))
  21285. (defun org-agenda-phases-of-moon ()
  21286. "Display the phases of the moon for the 3 months around the cursor date."
  21287. (interactive)
  21288. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21289. (defun org-agenda-holidays ()
  21290. "Display the holidays for the 3 months around the cursor date."
  21291. (interactive)
  21292. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21293. (defun org-agenda-sunrise-sunset (arg)
  21294. "Display sunrise and sunset for the cursor date.
  21295. Latitude and longitude can be specified with the variables
  21296. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21297. argument, latitude and longitude will be prompted for."
  21298. (interactive "P")
  21299. (let ((calendar-longitude (if arg nil calendar-longitude))
  21300. (calendar-latitude (if arg nil calendar-latitude))
  21301. (calendar-location-name
  21302. (if arg "the given coordinates" calendar-location-name)))
  21303. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21304. (defun org-agenda-goto-calendar ()
  21305. "Open the Emacs calendar with the date at the cursor."
  21306. (interactive)
  21307. (org-agenda-check-type t 'agenda 'timeline)
  21308. (let* ((day (or (get-text-property (point) 'day)
  21309. (error "Don't know which date to open in calendar")))
  21310. (date (calendar-gregorian-from-absolute day))
  21311. (calendar-move-hook nil)
  21312. (view-calendar-holidays-initially nil)
  21313. (view-diary-entries-initially nil))
  21314. (calendar)
  21315. (calendar-goto-date date)))
  21316. (defun org-calendar-goto-agenda ()
  21317. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21318. This is a command that has to be installed in `calendar-mode-map'."
  21319. (interactive)
  21320. (org-agenda-list nil (calendar-absolute-from-gregorian
  21321. (calendar-cursor-to-date))
  21322. nil))
  21323. (defun org-agenda-convert-date ()
  21324. (interactive)
  21325. (org-agenda-check-type t 'agenda 'timeline)
  21326. (let ((day (get-text-property (point) 'day))
  21327. date s)
  21328. (unless day
  21329. (error "Don't know which date to convert"))
  21330. (setq date (calendar-gregorian-from-absolute day))
  21331. (setq s (concat
  21332. "Gregorian: " (calendar-date-string date) "\n"
  21333. "ISO: " (calendar-iso-date-string date) "\n"
  21334. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21335. "Julian: " (calendar-julian-date-string date) "\n"
  21336. "Astron. JD: " (calendar-astro-date-string date)
  21337. " (Julian date number at noon UTC)\n"
  21338. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21339. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21340. "French: " (calendar-french-date-string date) "\n"
  21341. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21342. "Mayan: " (calendar-mayan-date-string date) "\n"
  21343. "Coptic: " (calendar-coptic-date-string date) "\n"
  21344. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21345. "Persian: " (calendar-persian-date-string date) "\n"
  21346. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21347. (with-output-to-temp-buffer "*Dates*"
  21348. (princ s))
  21349. (if (fboundp 'fit-window-to-buffer)
  21350. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21351. ;;;; Embedded LaTeX
  21352. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21353. "Keymap for the minor `org-cdlatex-mode'.")
  21354. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21355. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21356. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21357. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21358. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21359. (defvar org-cdlatex-texmathp-advice-is-done nil
  21360. "Flag remembering if we have applied the advice to texmathp already.")
  21361. (define-minor-mode org-cdlatex-mode
  21362. "Toggle the minor `org-cdlatex-mode'.
  21363. This mode supports entering LaTeX environment and math in LaTeX fragments
  21364. in Org-mode.
  21365. \\{org-cdlatex-mode-map}"
  21366. nil " OCDL" nil
  21367. (when org-cdlatex-mode (require 'cdlatex))
  21368. (unless org-cdlatex-texmathp-advice-is-done
  21369. (setq org-cdlatex-texmathp-advice-is-done t)
  21370. (defadvice texmathp (around org-math-always-on activate)
  21371. "Always return t in org-mode buffers.
  21372. This is because we want to insert math symbols without dollars even outside
  21373. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21374. en embedded LaTeX fragement, let texmathp do its job.
  21375. \\[org-cdlatex-mode-map]"
  21376. (interactive)
  21377. (let (p)
  21378. (cond
  21379. ((not (org-mode-p)) ad-do-it)
  21380. ((eq this-command 'cdlatex-math-symbol)
  21381. (setq ad-return-value t
  21382. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21383. (t
  21384. (let ((p (org-inside-LaTeX-fragment-p)))
  21385. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21386. (setq ad-return-value t
  21387. texmathp-why '("Org-mode embedded math" . 0))
  21388. (if p ad-do-it)))))))))
  21389. (defun turn-on-org-cdlatex ()
  21390. "Unconditionally turn on `org-cdlatex-mode'."
  21391. (org-cdlatex-mode 1))
  21392. (defun org-inside-LaTeX-fragment-p ()
  21393. "Test if point is inside a LaTeX fragment.
  21394. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21395. sequence appearing also before point.
  21396. Even though the matchers for math are configurable, this function assumes
  21397. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21398. delimiters are skipped when they have been removed by customization.
  21399. The return value is nil, or a cons cell with the delimiter and
  21400. and the position of this delimiter.
  21401. This function does a reasonably good job, but can locally be fooled by
  21402. for example currency specifications. For example it will assume being in
  21403. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21404. fragments that are properly closed, but during editing, we have to live
  21405. with the uncertainty caused by missing closing delimiters. This function
  21406. looks only before point, not after."
  21407. (catch 'exit
  21408. (let ((pos (point))
  21409. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21410. (lim (progn
  21411. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21412. (point)))
  21413. dd-on str (start 0) m re)
  21414. (goto-char pos)
  21415. (when dodollar
  21416. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21417. re (nth 1 (assoc "$" org-latex-regexps)))
  21418. (while (string-match re str start)
  21419. (cond
  21420. ((= (match-end 0) (length str))
  21421. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21422. ((= (match-end 0) (- (length str) 5))
  21423. (throw 'exit nil))
  21424. (t (setq start (match-end 0))))))
  21425. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21426. (goto-char pos)
  21427. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21428. (and (match-beginning 2) (throw 'exit nil))
  21429. ;; count $$
  21430. (while (re-search-backward "\\$\\$" lim t)
  21431. (setq dd-on (not dd-on)))
  21432. (goto-char pos)
  21433. (if dd-on (cons "$$" m))))))
  21434. (defun org-try-cdlatex-tab ()
  21435. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21436. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21437. - inside a LaTeX fragment, or
  21438. - after the first word in a line, where an abbreviation expansion could
  21439. insert a LaTeX environment."
  21440. (when org-cdlatex-mode
  21441. (cond
  21442. ((save-excursion
  21443. (skip-chars-backward "a-zA-Z0-9*")
  21444. (skip-chars-backward " \t")
  21445. (bolp))
  21446. (cdlatex-tab) t)
  21447. ((org-inside-LaTeX-fragment-p)
  21448. (cdlatex-tab) t)
  21449. (t nil))))
  21450. (defun org-cdlatex-underscore-caret (&optional arg)
  21451. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21452. Revert to the normal definition outside of these fragments."
  21453. (interactive "P")
  21454. (if (org-inside-LaTeX-fragment-p)
  21455. (call-interactively 'cdlatex-sub-superscript)
  21456. (let (org-cdlatex-mode)
  21457. (call-interactively (key-binding (vector last-input-event))))))
  21458. (defun org-cdlatex-math-modify (&optional arg)
  21459. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21460. Revert to the normal definition outside of these fragments."
  21461. (interactive "P")
  21462. (if (org-inside-LaTeX-fragment-p)
  21463. (call-interactively 'cdlatex-math-modify)
  21464. (let (org-cdlatex-mode)
  21465. (call-interactively (key-binding (vector last-input-event))))))
  21466. (defvar org-latex-fragment-image-overlays nil
  21467. "List of overlays carrying the images of latex fragments.")
  21468. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21469. (defun org-remove-latex-fragment-image-overlays ()
  21470. "Remove all overlays with LaTeX fragment images in current buffer."
  21471. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21472. (setq org-latex-fragment-image-overlays nil))
  21473. (defun org-preview-latex-fragment (&optional subtree)
  21474. "Preview the LaTeX fragment at point, or all locally or globally.
  21475. If the cursor is in a LaTeX fragment, create the image and overlay
  21476. it over the source code. If there is no fragment at point, display
  21477. all fragments in the current text, from one headline to the next. With
  21478. prefix SUBTREE, display all fragments in the current subtree. With a
  21479. double prefix `C-u C-u', or when the cursor is before the first headline,
  21480. display all fragments in the buffer.
  21481. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21482. (interactive "P")
  21483. (org-remove-latex-fragment-image-overlays)
  21484. (save-excursion
  21485. (save-restriction
  21486. (let (beg end at msg)
  21487. (cond
  21488. ((or (equal subtree '(16))
  21489. (not (save-excursion
  21490. (re-search-backward (concat "^" outline-regexp) nil t))))
  21491. (setq beg (point-min) end (point-max)
  21492. msg "Creating images for buffer...%s"))
  21493. ((equal subtree '(4))
  21494. (org-back-to-heading)
  21495. (setq beg (point) end (org-end-of-subtree t)
  21496. msg "Creating images for subtree...%s"))
  21497. (t
  21498. (if (setq at (org-inside-LaTeX-fragment-p))
  21499. (goto-char (max (point-min) (- (cdr at) 2)))
  21500. (org-back-to-heading))
  21501. (setq beg (point) end (progn (outline-next-heading) (point))
  21502. msg (if at "Creating image...%s"
  21503. "Creating images for entry...%s"))))
  21504. (message msg "")
  21505. (narrow-to-region beg end)
  21506. (goto-char beg)
  21507. (org-format-latex
  21508. (concat "ltxpng/" (file-name-sans-extension
  21509. (file-name-nondirectory
  21510. buffer-file-name)))
  21511. default-directory 'overlays msg at 'forbuffer)
  21512. (message msg "done. Use `C-c C-c' to remove images.")))))
  21513. (defvar org-latex-regexps
  21514. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21515. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21516. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21517. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21518. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21519. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21520. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21521. "Regular expressions for matching embedded LaTeX.")
  21522. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21523. "Replace LaTeX fragments with links to an image, and produce images."
  21524. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21525. (let* ((prefixnodir (file-name-nondirectory prefix))
  21526. (absprefix (expand-file-name prefix dir))
  21527. (todir (file-name-directory absprefix))
  21528. (opt org-format-latex-options)
  21529. (matchers (plist-get opt :matchers))
  21530. (re-list org-latex-regexps)
  21531. (cnt 0) txt link beg end re e checkdir
  21532. m n block linkfile movefile ov)
  21533. ;; Check if there are old images files with this prefix, and remove them
  21534. (when (file-directory-p todir)
  21535. (mapc 'delete-file
  21536. (directory-files
  21537. todir 'full
  21538. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21539. ;; Check the different regular expressions
  21540. (while (setq e (pop re-list))
  21541. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21542. block (if (nth 3 e) "\n\n" ""))
  21543. (when (member m matchers)
  21544. (goto-char (point-min))
  21545. (while (re-search-forward re nil t)
  21546. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21547. (setq txt (match-string n)
  21548. beg (match-beginning n) end (match-end n)
  21549. cnt (1+ cnt)
  21550. linkfile (format "%s_%04d.png" prefix cnt)
  21551. movefile (format "%s_%04d.png" absprefix cnt)
  21552. link (concat block "[[file:" linkfile "]]" block))
  21553. (if msg (message msg cnt))
  21554. (goto-char beg)
  21555. (unless checkdir ; make sure the directory exists
  21556. (setq checkdir t)
  21557. (or (file-directory-p todir) (make-directory todir)))
  21558. (org-create-formula-image
  21559. txt movefile opt forbuffer)
  21560. (if overlays
  21561. (progn
  21562. (setq ov (org-make-overlay beg end))
  21563. (if (featurep 'xemacs)
  21564. (progn
  21565. (org-overlay-put ov 'invisible t)
  21566. (org-overlay-put
  21567. ov 'end-glyph
  21568. (make-glyph (vector 'png :file movefile))))
  21569. (org-overlay-put
  21570. ov 'display
  21571. (list 'image :type 'png :file movefile :ascent 'center)))
  21572. (push ov org-latex-fragment-image-overlays)
  21573. (goto-char end))
  21574. (delete-region beg end)
  21575. (insert link))))))))
  21576. ;; This function borrows from Ganesh Swami's latex2png.el
  21577. (defun org-create-formula-image (string tofile options buffer)
  21578. (let* ((tmpdir (if (featurep 'xemacs)
  21579. (temp-directory)
  21580. temporary-file-directory))
  21581. (texfilebase (make-temp-name
  21582. (expand-file-name "orgtex" tmpdir)))
  21583. (texfile (concat texfilebase ".tex"))
  21584. (dvifile (concat texfilebase ".dvi"))
  21585. (pngfile (concat texfilebase ".png"))
  21586. (fnh (face-attribute 'default :height nil))
  21587. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21588. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21589. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21590. "Black"))
  21591. (bg (or (plist-get options (if buffer :background :html-background))
  21592. "Transparent")))
  21593. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21594. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21595. (with-temp-file texfile
  21596. (insert org-format-latex-header
  21597. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21598. (let ((dir default-directory))
  21599. (condition-case nil
  21600. (progn
  21601. (cd tmpdir)
  21602. (call-process "latex" nil nil nil texfile))
  21603. (error nil))
  21604. (cd dir))
  21605. (if (not (file-exists-p dvifile))
  21606. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21607. (call-process "dvipng" nil nil nil
  21608. "-E" "-fg" fg "-bg" bg
  21609. "-D" dpi
  21610. ;;"-x" scale "-y" scale
  21611. "-T" "tight"
  21612. "-o" pngfile
  21613. dvifile)
  21614. (if (not (file-exists-p pngfile))
  21615. (progn (message "Failed to create png file from %s" texfile) nil)
  21616. ;; Use the requested file name and clean up
  21617. (copy-file pngfile tofile 'replace)
  21618. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21619. (delete-file (concat texfilebase e)))
  21620. pngfile))))
  21621. (defun org-dvipng-color (attr)
  21622. "Return an rgb color specification for dvipng."
  21623. (apply 'format "rgb %s %s %s"
  21624. (mapcar 'org-normalize-color
  21625. (color-values (face-attribute 'default attr nil)))))
  21626. (defun org-normalize-color (value)
  21627. "Return string to be used as color value for an RGB component."
  21628. (format "%g" (/ value 65535.0)))
  21629. ;;;; Exporting
  21630. ;;; Variables, constants, and parameter plists
  21631. (defconst org-level-max 20)
  21632. (defvar org-export-html-preamble nil
  21633. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21634. (defvar org-export-html-postamble nil
  21635. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21636. (defvar org-export-html-auto-preamble t
  21637. "Should default preamble be inserted? Set by publishing functions.")
  21638. (defvar org-export-html-auto-postamble t
  21639. "Should default postamble be inserted? Set by publishing functions.")
  21640. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21641. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21642. (defconst org-export-plist-vars
  21643. '((:language . org-export-default-language)
  21644. (:customtime . org-display-custom-times)
  21645. (:headline-levels . org-export-headline-levels)
  21646. (:section-numbers . org-export-with-section-numbers)
  21647. (:table-of-contents . org-export-with-toc)
  21648. (:preserve-breaks . org-export-preserve-breaks)
  21649. (:archived-trees . org-export-with-archived-trees)
  21650. (:emphasize . org-export-with-emphasize)
  21651. (:sub-superscript . org-export-with-sub-superscripts)
  21652. (:special-strings . org-export-with-special-strings)
  21653. (:footnotes . org-export-with-footnotes)
  21654. (:drawers . org-export-with-drawers)
  21655. (:tags . org-export-with-tags)
  21656. (:TeX-macros . org-export-with-TeX-macros)
  21657. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21658. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21659. (:fixed-width . org-export-with-fixed-width)
  21660. (:timestamps . org-export-with-timestamps)
  21661. (:author-info . org-export-author-info)
  21662. (:time-stamp-file . org-export-time-stamp-file)
  21663. (:tables . org-export-with-tables)
  21664. (:table-auto-headline . org-export-highlight-first-table-line)
  21665. (:style . org-export-html-style)
  21666. (:agenda-style . org-agenda-export-html-style)
  21667. (:convert-org-links . org-export-html-link-org-files-as-html)
  21668. (:inline-images . org-export-html-inline-images)
  21669. (:html-extension . org-export-html-extension)
  21670. (:html-table-tag . org-export-html-table-tag)
  21671. (:expand-quoted-html . org-export-html-expand)
  21672. (:timestamp . org-export-html-with-timestamp)
  21673. (:publishing-directory . org-export-publishing-directory)
  21674. (:preamble . org-export-html-preamble)
  21675. (:postamble . org-export-html-postamble)
  21676. (:auto-preamble . org-export-html-auto-preamble)
  21677. (:auto-postamble . org-export-html-auto-postamble)
  21678. (:author . user-full-name)
  21679. (:email . user-mail-address)))
  21680. (defun org-default-export-plist ()
  21681. "Return the property list with default settings for the export variables."
  21682. (let ((l org-export-plist-vars) rtn e)
  21683. (while (setq e (pop l))
  21684. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21685. rtn))
  21686. (defun org-infile-export-plist ()
  21687. "Return the property list with file-local settings for export."
  21688. (save-excursion
  21689. (save-restriction
  21690. (widen)
  21691. (goto-char 0)
  21692. (let ((re (org-make-options-regexp
  21693. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21694. p key val text options)
  21695. (while (re-search-forward re nil t)
  21696. (setq key (org-match-string-no-properties 1)
  21697. val (org-match-string-no-properties 2))
  21698. (cond
  21699. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21700. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21701. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21702. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21703. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21704. ((string-equal key "TEXT")
  21705. (setq text (if text (concat text "\n" val) val)))
  21706. ((string-equal key "OPTIONS") (setq options val))))
  21707. (setq p (plist-put p :text text))
  21708. (when options
  21709. (let ((op '(("H" . :headline-levels)
  21710. ("num" . :section-numbers)
  21711. ("toc" . :table-of-contents)
  21712. ("\\n" . :preserve-breaks)
  21713. ("@" . :expand-quoted-html)
  21714. (":" . :fixed-width)
  21715. ("|" . :tables)
  21716. ("^" . :sub-superscript)
  21717. ("-" . :special-strings)
  21718. ("f" . :footnotes)
  21719. ("d" . :drawers)
  21720. ("tags" . :tags)
  21721. ("*" . :emphasize)
  21722. ("TeX" . :TeX-macros)
  21723. ("LaTeX" . :LaTeX-fragments)
  21724. ("skip" . :skip-before-1st-heading)
  21725. ("author" . :author-info)
  21726. ("timestamp" . :time-stamp-file)))
  21727. o)
  21728. (while (setq o (pop op))
  21729. (if (string-match (concat (regexp-quote (car o))
  21730. ":\\([^ \t\n\r;,.]*\\)")
  21731. options)
  21732. (setq p (plist-put p (cdr o)
  21733. (car (read-from-string
  21734. (match-string 1 options)))))))))
  21735. p))))
  21736. (defun org-export-directory (type plist)
  21737. (let* ((val (plist-get plist :publishing-directory))
  21738. (dir (if (listp val)
  21739. (or (cdr (assoc type val)) ".")
  21740. val)))
  21741. dir))
  21742. (defun org-skip-comments (lines)
  21743. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21744. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21745. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21746. (case-fold-search nil)
  21747. rtn line level)
  21748. (while (setq line (pop lines))
  21749. (cond
  21750. ((and (string-match re1 line)
  21751. (setq level (- (match-end 1) (match-beginning 1))))
  21752. ;; Beginning of a COMMENT subtree. Skip it.
  21753. (while (and (setq line (pop lines))
  21754. (or (not (string-match re2 line))
  21755. (> (- (match-end 1) (match-beginning 1)) level))))
  21756. (setq lines (cons line lines)))
  21757. ((string-match "^#" line)
  21758. ;; an ordinary comment line
  21759. )
  21760. ((and org-export-table-remove-special-lines
  21761. (string-match "^[ \t]*|" line)
  21762. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21763. (and (string-match "| *<[0-9]+> *|" line)
  21764. (not (string-match "| *[^ <|]" line)))))
  21765. ;; a special table line that should be removed
  21766. )
  21767. (t (setq rtn (cons line rtn)))))
  21768. (nreverse rtn)))
  21769. (defun org-export (&optional arg)
  21770. (interactive)
  21771. (let ((help "[t] insert the export option template
  21772. \[v] limit export to visible part of outline tree
  21773. \[a] export as ASCII
  21774. \[h] export as HTML
  21775. \[H] export as HTML to temporary buffer
  21776. \[R] export region as HTML
  21777. \[b] export as HTML and browse immediately
  21778. \[x] export as XOXO
  21779. \[l] export as LaTeX
  21780. \[L] export as LaTeX to temporary buffer
  21781. \[i] export current file as iCalendar file
  21782. \[I] export all agenda files as iCalendar files
  21783. \[c] export agenda files into combined iCalendar file
  21784. \[F] publish current file
  21785. \[P] publish current project
  21786. \[X] publish... (project will be prompted for)
  21787. \[A] publish all projects")
  21788. (cmds
  21789. '((?t . org-insert-export-options-template)
  21790. (?v . org-export-visible)
  21791. (?a . org-export-as-ascii)
  21792. (?h . org-export-as-html)
  21793. (?b . org-export-as-html-and-open)
  21794. (?H . org-export-as-html-to-buffer)
  21795. (?R . org-export-region-as-html)
  21796. (?x . org-export-as-xoxo)
  21797. (?l . org-export-as-latex)
  21798. (?L . org-export-as-latex-to-buffer)
  21799. (?i . org-export-icalendar-this-file)
  21800. (?I . org-export-icalendar-all-agenda-files)
  21801. (?c . org-export-icalendar-combine-agenda-files)
  21802. (?F . org-publish-current-file)
  21803. (?P . org-publish-current-project)
  21804. (?X . org-publish)
  21805. (?A . org-publish-all)))
  21806. r1 r2 ass)
  21807. (save-window-excursion
  21808. (delete-other-windows)
  21809. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21810. (princ help))
  21811. (message "Select command: ")
  21812. (setq r1 (read-char-exclusive)))
  21813. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21814. (if (setq ass (assq r2 cmds))
  21815. (call-interactively (cdr ass))
  21816. (error "No command associated with key %c" r1))))
  21817. (defconst org-html-entities
  21818. '(("nbsp")
  21819. ("iexcl")
  21820. ("cent")
  21821. ("pound")
  21822. ("curren")
  21823. ("yen")
  21824. ("brvbar")
  21825. ("vert" . "&#124;")
  21826. ("sect")
  21827. ("uml")
  21828. ("copy")
  21829. ("ordf")
  21830. ("laquo")
  21831. ("not")
  21832. ("shy")
  21833. ("reg")
  21834. ("macr")
  21835. ("deg")
  21836. ("plusmn")
  21837. ("sup2")
  21838. ("sup3")
  21839. ("acute")
  21840. ("micro")
  21841. ("para")
  21842. ("middot")
  21843. ("odot"."o")
  21844. ("star"."*")
  21845. ("cedil")
  21846. ("sup1")
  21847. ("ordm")
  21848. ("raquo")
  21849. ("frac14")
  21850. ("frac12")
  21851. ("frac34")
  21852. ("iquest")
  21853. ("Agrave")
  21854. ("Aacute")
  21855. ("Acirc")
  21856. ("Atilde")
  21857. ("Auml")
  21858. ("Aring") ("AA"."&Aring;")
  21859. ("AElig")
  21860. ("Ccedil")
  21861. ("Egrave")
  21862. ("Eacute")
  21863. ("Ecirc")
  21864. ("Euml")
  21865. ("Igrave")
  21866. ("Iacute")
  21867. ("Icirc")
  21868. ("Iuml")
  21869. ("ETH")
  21870. ("Ntilde")
  21871. ("Ograve")
  21872. ("Oacute")
  21873. ("Ocirc")
  21874. ("Otilde")
  21875. ("Ouml")
  21876. ("times")
  21877. ("Oslash")
  21878. ("Ugrave")
  21879. ("Uacute")
  21880. ("Ucirc")
  21881. ("Uuml")
  21882. ("Yacute")
  21883. ("THORN")
  21884. ("szlig")
  21885. ("agrave")
  21886. ("aacute")
  21887. ("acirc")
  21888. ("atilde")
  21889. ("auml")
  21890. ("aring")
  21891. ("aelig")
  21892. ("ccedil")
  21893. ("egrave")
  21894. ("eacute")
  21895. ("ecirc")
  21896. ("euml")
  21897. ("igrave")
  21898. ("iacute")
  21899. ("icirc")
  21900. ("iuml")
  21901. ("eth")
  21902. ("ntilde")
  21903. ("ograve")
  21904. ("oacute")
  21905. ("ocirc")
  21906. ("otilde")
  21907. ("ouml")
  21908. ("divide")
  21909. ("oslash")
  21910. ("ugrave")
  21911. ("uacute")
  21912. ("ucirc")
  21913. ("uuml")
  21914. ("yacute")
  21915. ("thorn")
  21916. ("yuml")
  21917. ("fnof")
  21918. ("Alpha")
  21919. ("Beta")
  21920. ("Gamma")
  21921. ("Delta")
  21922. ("Epsilon")
  21923. ("Zeta")
  21924. ("Eta")
  21925. ("Theta")
  21926. ("Iota")
  21927. ("Kappa")
  21928. ("Lambda")
  21929. ("Mu")
  21930. ("Nu")
  21931. ("Xi")
  21932. ("Omicron")
  21933. ("Pi")
  21934. ("Rho")
  21935. ("Sigma")
  21936. ("Tau")
  21937. ("Upsilon")
  21938. ("Phi")
  21939. ("Chi")
  21940. ("Psi")
  21941. ("Omega")
  21942. ("alpha")
  21943. ("beta")
  21944. ("gamma")
  21945. ("delta")
  21946. ("epsilon")
  21947. ("varepsilon"."&epsilon;")
  21948. ("zeta")
  21949. ("eta")
  21950. ("theta")
  21951. ("iota")
  21952. ("kappa")
  21953. ("lambda")
  21954. ("mu")
  21955. ("nu")
  21956. ("xi")
  21957. ("omicron")
  21958. ("pi")
  21959. ("rho")
  21960. ("sigmaf") ("varsigma"."&sigmaf;")
  21961. ("sigma")
  21962. ("tau")
  21963. ("upsilon")
  21964. ("phi")
  21965. ("chi")
  21966. ("psi")
  21967. ("omega")
  21968. ("thetasym") ("vartheta"."&thetasym;")
  21969. ("upsih")
  21970. ("piv")
  21971. ("bull") ("bullet"."&bull;")
  21972. ("hellip") ("dots"."&hellip;")
  21973. ("prime")
  21974. ("Prime")
  21975. ("oline")
  21976. ("frasl")
  21977. ("weierp")
  21978. ("image")
  21979. ("real")
  21980. ("trade")
  21981. ("alefsym")
  21982. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21983. ("uarr") ("uparrow"."&uarr;")
  21984. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21985. ("darr")("downarrow"."&darr;")
  21986. ("harr") ("leftrightarrow"."&harr;")
  21987. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21988. ("lArr") ("Leftarrow"."&lArr;")
  21989. ("uArr") ("Uparrow"."&uArr;")
  21990. ("rArr") ("Rightarrow"."&rArr;")
  21991. ("dArr") ("Downarrow"."&dArr;")
  21992. ("hArr") ("Leftrightarrow"."&hArr;")
  21993. ("forall")
  21994. ("part") ("partial"."&part;")
  21995. ("exist") ("exists"."&exist;")
  21996. ("empty") ("emptyset"."&empty;")
  21997. ("nabla")
  21998. ("isin") ("in"."&isin;")
  21999. ("notin")
  22000. ("ni")
  22001. ("prod")
  22002. ("sum")
  22003. ("minus")
  22004. ("lowast") ("ast"."&lowast;")
  22005. ("radic")
  22006. ("prop") ("proptp"."&prop;")
  22007. ("infin") ("infty"."&infin;")
  22008. ("ang") ("angle"."&ang;")
  22009. ("and") ("wedge"."&and;")
  22010. ("or") ("vee"."&or;")
  22011. ("cap")
  22012. ("cup")
  22013. ("int")
  22014. ("there4")
  22015. ("sim")
  22016. ("cong") ("simeq"."&cong;")
  22017. ("asymp")("approx"."&asymp;")
  22018. ("ne") ("neq"."&ne;")
  22019. ("equiv")
  22020. ("le")
  22021. ("ge")
  22022. ("sub") ("subset"."&sub;")
  22023. ("sup") ("supset"."&sup;")
  22024. ("nsub")
  22025. ("sube")
  22026. ("supe")
  22027. ("oplus")
  22028. ("otimes")
  22029. ("perp")
  22030. ("sdot") ("cdot"."&sdot;")
  22031. ("lceil")
  22032. ("rceil")
  22033. ("lfloor")
  22034. ("rfloor")
  22035. ("lang")
  22036. ("rang")
  22037. ("loz") ("Diamond"."&loz;")
  22038. ("spades") ("spadesuit"."&spades;")
  22039. ("clubs") ("clubsuit"."&clubs;")
  22040. ("hearts") ("diamondsuit"."&hearts;")
  22041. ("diams") ("diamondsuit"."&diams;")
  22042. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22043. ("quot")
  22044. ("amp")
  22045. ("lt")
  22046. ("gt")
  22047. ("OElig")
  22048. ("oelig")
  22049. ("Scaron")
  22050. ("scaron")
  22051. ("Yuml")
  22052. ("circ")
  22053. ("tilde")
  22054. ("ensp")
  22055. ("emsp")
  22056. ("thinsp")
  22057. ("zwnj")
  22058. ("zwj")
  22059. ("lrm")
  22060. ("rlm")
  22061. ("ndash")
  22062. ("mdash")
  22063. ("lsquo")
  22064. ("rsquo")
  22065. ("sbquo")
  22066. ("ldquo")
  22067. ("rdquo")
  22068. ("bdquo")
  22069. ("dagger")
  22070. ("Dagger")
  22071. ("permil")
  22072. ("lsaquo")
  22073. ("rsaquo")
  22074. ("euro")
  22075. ("arccos"."arccos")
  22076. ("arcsin"."arcsin")
  22077. ("arctan"."arctan")
  22078. ("arg"."arg")
  22079. ("cos"."cos")
  22080. ("cosh"."cosh")
  22081. ("cot"."cot")
  22082. ("coth"."coth")
  22083. ("csc"."csc")
  22084. ("deg"."deg")
  22085. ("det"."det")
  22086. ("dim"."dim")
  22087. ("exp"."exp")
  22088. ("gcd"."gcd")
  22089. ("hom"."hom")
  22090. ("inf"."inf")
  22091. ("ker"."ker")
  22092. ("lg"."lg")
  22093. ("lim"."lim")
  22094. ("liminf"."liminf")
  22095. ("limsup"."limsup")
  22096. ("ln"."ln")
  22097. ("log"."log")
  22098. ("max"."max")
  22099. ("min"."min")
  22100. ("Pr"."Pr")
  22101. ("sec"."sec")
  22102. ("sin"."sin")
  22103. ("sinh"."sinh")
  22104. ("sup"."sup")
  22105. ("tan"."tan")
  22106. ("tanh"."tanh")
  22107. )
  22108. "Entities for TeX->HTML translation.
  22109. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22110. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22111. In that case, \"\\ent\" will be translated to \"&other;\".
  22112. The list contains HTML entities for Latin-1, Greek and other symbols.
  22113. It is supplemented by a number of commonly used TeX macros with appropriate
  22114. translations. There is currently no way for users to extend this.")
  22115. ;;; General functions for all backends
  22116. (defun org-cleaned-string-for-export (string &rest parameters)
  22117. "Cleanup a buffer STRING so that links can be created safely."
  22118. (interactive)
  22119. (let* ((re-radio (and org-target-link-regexp
  22120. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22121. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22122. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22123. (re-archive (concat ":" org-archive-tag ":"))
  22124. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22125. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22126. (htmlp (plist-get parameters :for-html))
  22127. (asciip (plist-get parameters :for-ascii))
  22128. (latexp (plist-get parameters :for-LaTeX))
  22129. (commentsp (plist-get parameters :comments))
  22130. (archived-trees (plist-get parameters :archived-trees))
  22131. (inhibit-read-only t)
  22132. (drawers org-drawers)
  22133. (exp-drawers (plist-get parameters :drawers))
  22134. (outline-regexp "\\*+ ")
  22135. a b xx
  22136. rtn p)
  22137. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22138. (erase-buffer)
  22139. (insert string)
  22140. ;; Remove license-to-kill stuff
  22141. (while (setq p (text-property-any (point-min) (point-max)
  22142. :org-license-to-kill t))
  22143. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22144. (let ((org-inhibit-startup t)) (org-mode))
  22145. (untabify (point-min) (point-max))
  22146. ;; Get rid of drawers
  22147. (unless (eq t exp-drawers)
  22148. (goto-char (point-min))
  22149. (let ((re (concat "^[ \t]*:\\("
  22150. (mapconcat
  22151. 'identity
  22152. (org-delete-all exp-drawers
  22153. (copy-sequence drawers))
  22154. "\\|")
  22155. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22156. (while (re-search-forward re nil t)
  22157. (replace-match ""))))
  22158. ;; Get the correct stuff before the first headline
  22159. (when (plist-get parameters :skip-before-1st-heading)
  22160. (goto-char (point-min))
  22161. (when (re-search-forward "^\\*+[ \t]" nil t)
  22162. (delete-region (point-min) (match-beginning 0))
  22163. (goto-char (point-min))
  22164. (insert "\n")))
  22165. (when (plist-get parameters :add-text)
  22166. (goto-char (point-min))
  22167. (insert (plist-get parameters :add-text) "\n"))
  22168. ;; Get rid of archived trees
  22169. (when (not (eq archived-trees t))
  22170. (goto-char (point-min))
  22171. (while (re-search-forward re-archive nil t)
  22172. (if (not (org-on-heading-p t))
  22173. (org-end-of-subtree t)
  22174. (beginning-of-line 1)
  22175. (setq a (if archived-trees
  22176. (1+ (point-at-eol)) (point))
  22177. b (org-end-of-subtree t))
  22178. (if (> b a) (delete-region a b)))))
  22179. ;; Find targets in comments and move them out of comments,
  22180. ;; but mark them as targets that should be invisible
  22181. (goto-char (point-min))
  22182. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22183. (replace-match "\\1(INVISIBLE)"))
  22184. ;; Protect backend specific stuff, throw away the others.
  22185. (let ((formatters
  22186. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22187. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22188. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22189. fmt)
  22190. (goto-char (point-min))
  22191. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22192. (goto-char (match-end 0))
  22193. (while (not (looking-at "#\\+END_EXAMPLE"))
  22194. (insert ": ")
  22195. (beginning-of-line 2)))
  22196. (goto-char (point-min))
  22197. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22198. (add-text-properties (match-beginning 0) (match-end 0)
  22199. '(org-protected t)))
  22200. (while formatters
  22201. (setq fmt (pop formatters))
  22202. (when (car fmt)
  22203. (goto-char (point-min))
  22204. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22205. ":[ \t]*\\(.*\\)") nil t)
  22206. (replace-match "\\1" t)
  22207. (add-text-properties
  22208. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22209. '(org-protected t))))
  22210. (goto-char (point-min))
  22211. (while (re-search-forward
  22212. (concat "^#\\+"
  22213. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22214. (cadddr fmt) "\\>.*\n?") nil t)
  22215. (if (car fmt)
  22216. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22217. '(org-protected t))
  22218. (delete-region (match-beginning 0) (match-end 0))))))
  22219. ;; Protect quoted subtrees
  22220. (goto-char (point-min))
  22221. (while (re-search-forward re-quote nil t)
  22222. (goto-char (match-beginning 0))
  22223. (end-of-line 1)
  22224. (add-text-properties (point) (org-end-of-subtree t)
  22225. '(org-protected t)))
  22226. ;; Protect verbatim elements
  22227. (goto-char (point-min))
  22228. (while (re-search-forward org-verbatim-re nil t)
  22229. (add-text-properties (match-beginning 4) (match-end 4)
  22230. '(org-protected t))
  22231. (goto-char (1+ (match-end 4))))
  22232. ;; Remove subtrees that are commented
  22233. (goto-char (point-min))
  22234. (while (re-search-forward re-commented nil t)
  22235. (goto-char (match-beginning 0))
  22236. (delete-region (point) (org-end-of-subtree t)))
  22237. ;; Remove special table lines
  22238. (when org-export-table-remove-special-lines
  22239. (goto-char (point-min))
  22240. (while (re-search-forward "^[ \t]*|" nil t)
  22241. (beginning-of-line 1)
  22242. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22243. (and (looking-at ".*?| *<[0-9]+> *|")
  22244. (not (looking-at ".*?| *[^ <|]"))))
  22245. (delete-region (max (point-min) (1- (point-at-bol)))
  22246. (point-at-eol))
  22247. (end-of-line 1))))
  22248. ;; Specific LaTeX stuff
  22249. (when latexp
  22250. (require 'org-export-latex nil)
  22251. (org-export-latex-cleaned-string))
  22252. (when asciip
  22253. (org-export-ascii-clean-string))
  22254. ;; Specific HTML stuff
  22255. (when htmlp
  22256. ;; Convert LaTeX fragments to images
  22257. (when (plist-get parameters :LaTeX-fragments)
  22258. (org-format-latex
  22259. (concat "ltxpng/" (file-name-sans-extension
  22260. (file-name-nondirectory
  22261. org-current-export-file)))
  22262. org-current-export-dir nil "Creating LaTeX image %s"))
  22263. (message "Exporting..."))
  22264. ;; Remove or replace comments
  22265. (goto-char (point-min))
  22266. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22267. (if commentsp
  22268. (progn (add-text-properties
  22269. (match-beginning 0) (match-end 0) '(org-protected t))
  22270. (replace-match (format commentsp (match-string 1)) t t))
  22271. (replace-match "")))
  22272. ;; Find matches for radio targets and turn them into internal links
  22273. (goto-char (point-min))
  22274. (when re-radio
  22275. (while (re-search-forward re-radio nil t)
  22276. (org-if-unprotected
  22277. (replace-match "\\1[[\\2]]"))))
  22278. ;; Find all links that contain a newline and put them into a single line
  22279. (goto-char (point-min))
  22280. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22281. (org-if-unprotected
  22282. (replace-match "\\1 \\3")
  22283. (goto-char (match-beginning 0))))
  22284. ;; Normalize links: Convert angle and plain links into bracket links
  22285. ;; Expand link abbreviations
  22286. (goto-char (point-min))
  22287. (while (re-search-forward re-plain-link nil t)
  22288. (goto-char (1- (match-end 0)))
  22289. (org-if-unprotected
  22290. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22291. ":" (match-string 3) "]]")))
  22292. ;; added 'org-link face to links
  22293. (put-text-property 0 (length s) 'face 'org-link s)
  22294. (replace-match s t t))))
  22295. (goto-char (point-min))
  22296. (while (re-search-forward re-angle-link nil t)
  22297. (goto-char (1- (match-end 0)))
  22298. (org-if-unprotected
  22299. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22300. ":" (match-string 3) "]]")))
  22301. (put-text-property 0 (length s) 'face 'org-link s)
  22302. (replace-match s t t))))
  22303. (goto-char (point-min))
  22304. (while (re-search-forward org-bracket-link-regexp nil t)
  22305. (org-if-unprotected
  22306. (let* ((s (concat "[[" (setq xx (save-match-data
  22307. (org-link-expand-abbrev (match-string 1))))
  22308. "]"
  22309. (if (match-end 3)
  22310. (match-string 2)
  22311. (concat "[" xx "]"))
  22312. "]")))
  22313. (put-text-property 0 (length s) 'face 'org-link s)
  22314. (replace-match s t t))))
  22315. ;; Find multiline emphasis and put them into single line
  22316. (when (plist-get parameters :emph-multiline)
  22317. (goto-char (point-min))
  22318. (while (re-search-forward org-emph-re nil t)
  22319. (if (not (= (char-after (match-beginning 3))
  22320. (char-after (match-beginning 4))))
  22321. (org-if-unprotected
  22322. (subst-char-in-region (match-beginning 0) (match-end 0)
  22323. ?\n ?\ t)
  22324. (goto-char (1- (match-end 0))))
  22325. (goto-char (1+ (match-beginning 0))))))
  22326. (setq rtn (buffer-string)))
  22327. (kill-buffer " org-mode-tmp")
  22328. rtn))
  22329. (defun org-export-grab-title-from-buffer ()
  22330. "Get a title for the current document, from looking at the buffer."
  22331. (let ((inhibit-read-only t))
  22332. (save-excursion
  22333. (goto-char (point-min))
  22334. (let ((end (save-excursion (outline-next-heading) (point))))
  22335. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22336. ;; Mark the line so that it will not be exported as normal text.
  22337. (org-unmodified
  22338. (add-text-properties (match-beginning 0) (match-end 0)
  22339. (list :org-license-to-kill t)))
  22340. ;; Return the title string
  22341. (org-trim (match-string 0)))))))
  22342. (defun org-export-get-title-from-subtree ()
  22343. "Return subtree title and exclude it from export."
  22344. (let (title (m (mark)))
  22345. (save-excursion
  22346. (goto-char (region-beginning))
  22347. (when (and (org-at-heading-p)
  22348. (>= (org-end-of-subtree t t) (region-end)))
  22349. ;; This is a subtree, we take the title from the first heading
  22350. (goto-char (region-beginning))
  22351. (looking-at org-todo-line-regexp)
  22352. (setq title (match-string 3))
  22353. (org-unmodified
  22354. (add-text-properties (point) (1+ (point-at-eol))
  22355. (list :org-license-to-kill t)))))
  22356. title))
  22357. (defun org-solidify-link-text (s &optional alist)
  22358. "Take link text and make a safe target out of it."
  22359. (save-match-data
  22360. (let* ((rtn
  22361. (mapconcat
  22362. 'identity
  22363. (org-split-string s "[ \t\r\n]+") "--"))
  22364. (a (assoc rtn alist)))
  22365. (or (cdr a) rtn))))
  22366. (defun org-get-min-level (lines)
  22367. "Get the minimum level in LINES."
  22368. (let ((re "^\\(\\*+\\) ") l min)
  22369. (catch 'exit
  22370. (while (setq l (pop lines))
  22371. (if (string-match re l)
  22372. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22373. 1)))
  22374. ;; Variable holding the vector with section numbers
  22375. (defvar org-section-numbers (make-vector org-level-max 0))
  22376. (defun org-init-section-numbers ()
  22377. "Initialize the vector for the section numbers."
  22378. (let* ((level -1)
  22379. (numbers (nreverse (org-split-string "" "\\.")))
  22380. (depth (1- (length org-section-numbers)))
  22381. (i depth) number-string)
  22382. (while (>= i 0)
  22383. (if (> i level)
  22384. (aset org-section-numbers i 0)
  22385. (setq number-string (or (car numbers) "0"))
  22386. (if (string-match "\\`[A-Z]\\'" number-string)
  22387. (aset org-section-numbers i
  22388. (- (string-to-char number-string) ?A -1))
  22389. (aset org-section-numbers i (string-to-number number-string)))
  22390. (pop numbers))
  22391. (setq i (1- i)))))
  22392. (defun org-section-number (&optional level)
  22393. "Return a string with the current section number.
  22394. When LEVEL is non-nil, increase section numbers on that level."
  22395. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22396. (when level
  22397. (when (> level -1)
  22398. (aset org-section-numbers
  22399. level (1+ (aref org-section-numbers level))))
  22400. (setq idx (1+ level))
  22401. (while (<= idx depth)
  22402. (if (not (= idx 1))
  22403. (aset org-section-numbers idx 0))
  22404. (setq idx (1+ idx))))
  22405. (setq idx 0)
  22406. (while (<= idx depth)
  22407. (setq n (aref org-section-numbers idx))
  22408. (setq string (concat string (if (not (string= string "")) "." "")
  22409. (int-to-string n)))
  22410. (setq idx (1+ idx)))
  22411. (save-match-data
  22412. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22413. (setq string (replace-match "" t nil string)))
  22414. (if (string-match "\\(\\.0\\)+\\'" string)
  22415. (setq string (replace-match "" t nil string))))
  22416. string))
  22417. ;;; ASCII export
  22418. (defvar org-last-level nil) ; dynamically scoped variable
  22419. (defvar org-min-level nil) ; dynamically scoped variable
  22420. (defvar org-levels-open nil) ; dynamically scoped parameter
  22421. (defvar org-ascii-current-indentation nil) ; For communication
  22422. (defun org-export-as-ascii (arg)
  22423. "Export the outline as a pretty ASCII file.
  22424. If there is an active region, export only the region.
  22425. The prefix ARG specifies how many levels of the outline should become
  22426. underlined headlines. The default is 3."
  22427. (interactive "P")
  22428. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22429. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22430. (org-infile-export-plist)))
  22431. (region-p (org-region-active-p))
  22432. (subtree-p
  22433. (when region-p
  22434. (save-excursion
  22435. (goto-char (region-beginning))
  22436. (and (org-at-heading-p)
  22437. (>= (org-end-of-subtree t t) (region-end))))))
  22438. (custom-times org-display-custom-times)
  22439. (org-ascii-current-indentation '(0 . 0))
  22440. (level 0) line txt
  22441. (umax nil)
  22442. (umax-toc nil)
  22443. (case-fold-search nil)
  22444. (filename (concat (file-name-as-directory
  22445. (org-export-directory :ascii opt-plist))
  22446. (file-name-sans-extension
  22447. (or (and subtree-p
  22448. (org-entry-get (region-beginning)
  22449. "EXPORT_FILE_NAME" t))
  22450. (file-name-nondirectory buffer-file-name)))
  22451. ".txt"))
  22452. (filename (if (equal (file-truename filename)
  22453. (file-truename buffer-file-name))
  22454. (concat filename ".txt")
  22455. filename))
  22456. (buffer (find-file-noselect filename))
  22457. (org-levels-open (make-vector org-level-max nil))
  22458. (odd org-odd-levels-only)
  22459. (date (plist-get opt-plist :date))
  22460. (author (plist-get opt-plist :author))
  22461. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22462. (plist-get opt-plist :title)
  22463. (and (not
  22464. (plist-get opt-plist :skip-before-1st-heading))
  22465. (org-export-grab-title-from-buffer))
  22466. (file-name-sans-extension
  22467. (file-name-nondirectory buffer-file-name))))
  22468. (email (plist-get opt-plist :email))
  22469. (language (plist-get opt-plist :language))
  22470. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22471. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22472. (todo nil)
  22473. (lang-words nil)
  22474. (region
  22475. (buffer-substring
  22476. (if (org-region-active-p) (region-beginning) (point-min))
  22477. (if (org-region-active-p) (region-end) (point-max))))
  22478. (lines (org-split-string
  22479. (org-cleaned-string-for-export
  22480. region
  22481. :for-ascii t
  22482. :skip-before-1st-heading
  22483. (plist-get opt-plist :skip-before-1st-heading)
  22484. :drawers (plist-get opt-plist :drawers)
  22485. :verbatim-multiline t
  22486. :archived-trees
  22487. (plist-get opt-plist :archived-trees)
  22488. :add-text (plist-get opt-plist :text))
  22489. "\n"))
  22490. thetoc have-headings first-heading-pos
  22491. table-open table-buffer)
  22492. (let ((inhibit-read-only t))
  22493. (org-unmodified
  22494. (remove-text-properties (point-min) (point-max)
  22495. '(:org-license-to-kill t))))
  22496. (setq org-min-level (org-get-min-level lines))
  22497. (setq org-last-level org-min-level)
  22498. (org-init-section-numbers)
  22499. (find-file-noselect filename)
  22500. (setq lang-words (or (assoc language org-export-language-setup)
  22501. (assoc "en" org-export-language-setup)))
  22502. (switch-to-buffer-other-window buffer)
  22503. (erase-buffer)
  22504. (fundamental-mode)
  22505. ;; create local variables for all options, to make sure all called
  22506. ;; functions get the correct information
  22507. (mapc (lambda (x)
  22508. (set (make-local-variable (cdr x))
  22509. (plist-get opt-plist (car x))))
  22510. org-export-plist-vars)
  22511. (org-set-local 'org-odd-levels-only odd)
  22512. (setq umax (if arg (prefix-numeric-value arg)
  22513. org-export-headline-levels))
  22514. (setq umax-toc (if (integerp org-export-with-toc)
  22515. (min org-export-with-toc umax)
  22516. umax))
  22517. ;; File header
  22518. (if title (org-insert-centered title ?=))
  22519. (insert "\n")
  22520. (if (and (or author email)
  22521. org-export-author-info)
  22522. (insert (concat (nth 1 lang-words) ": " (or author "")
  22523. (if email (concat " <" email ">") "")
  22524. "\n")))
  22525. (cond
  22526. ((and date (string-match "%" date))
  22527. (setq date (format-time-string date (current-time))))
  22528. (date)
  22529. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22530. (if (and date org-export-time-stamp-file)
  22531. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22532. (insert "\n\n")
  22533. (if org-export-with-toc
  22534. (progn
  22535. (push (concat (nth 3 lang-words) "\n") thetoc)
  22536. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22537. (mapc '(lambda (line)
  22538. (if (string-match org-todo-line-regexp
  22539. line)
  22540. ;; This is a headline
  22541. (progn
  22542. (setq have-headings t)
  22543. (setq level (- (match-end 1) (match-beginning 1))
  22544. level (org-tr-level level)
  22545. txt (match-string 3 line)
  22546. todo
  22547. (or (and org-export-mark-todo-in-toc
  22548. (match-beginning 2)
  22549. (not (member (match-string 2 line)
  22550. org-done-keywords)))
  22551. ; TODO, not DONE
  22552. (and org-export-mark-todo-in-toc
  22553. (= level umax-toc)
  22554. (org-search-todo-below
  22555. line lines level))))
  22556. (setq txt (org-html-expand-for-ascii txt))
  22557. (while (string-match org-bracket-link-regexp txt)
  22558. (setq txt
  22559. (replace-match
  22560. (match-string (if (match-end 2) 3 1) txt)
  22561. t t txt)))
  22562. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22563. (string-match
  22564. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22565. txt))
  22566. (setq txt (replace-match "" t t txt)))
  22567. (if (string-match quote-re0 txt)
  22568. (setq txt (replace-match "" t t txt)))
  22569. (if org-export-with-section-numbers
  22570. (setq txt (concat (org-section-number level)
  22571. " " txt)))
  22572. (if (<= level umax-toc)
  22573. (progn
  22574. (push
  22575. (concat
  22576. (make-string
  22577. (* (max 0 (- level org-min-level)) 4) ?\ )
  22578. (format (if todo "%s (*)\n" "%s\n") txt))
  22579. thetoc)
  22580. (setq org-last-level level))
  22581. ))))
  22582. lines)
  22583. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22584. (org-init-section-numbers)
  22585. (while (setq line (pop lines))
  22586. ;; Remove the quoted HTML tags.
  22587. (setq line (org-html-expand-for-ascii line))
  22588. ;; Remove targets
  22589. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22590. (setq line (replace-match "" t t line)))
  22591. ;; Replace internal links
  22592. (while (string-match org-bracket-link-regexp line)
  22593. (setq line (replace-match
  22594. (if (match-end 3) "[\\3]" "[\\1]")
  22595. t nil line)))
  22596. (when custom-times
  22597. (setq line (org-translate-time line)))
  22598. (cond
  22599. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22600. ;; a Headline
  22601. (setq first-heading-pos (or first-heading-pos (point)))
  22602. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22603. txt (match-string 2 line))
  22604. (org-ascii-level-start level txt umax lines))
  22605. ((and org-export-with-tables
  22606. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22607. (if (not table-open)
  22608. ;; New table starts
  22609. (setq table-open t table-buffer nil))
  22610. ;; Accumulate lines
  22611. (setq table-buffer (cons line table-buffer))
  22612. (when (or (not lines)
  22613. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22614. (car lines))))
  22615. (setq table-open nil
  22616. table-buffer (nreverse table-buffer))
  22617. (insert (mapconcat
  22618. (lambda (x)
  22619. (org-fix-indentation x org-ascii-current-indentation))
  22620. (org-format-table-ascii table-buffer)
  22621. "\n") "\n")))
  22622. (t
  22623. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22624. (if (and org-export-with-fixed-width
  22625. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22626. (setq line (replace-match "\\1" nil nil line)))
  22627. (insert line "\n"))))
  22628. (normal-mode)
  22629. ;; insert the table of contents
  22630. (when thetoc
  22631. (goto-char (point-min))
  22632. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22633. (progn
  22634. (goto-char (match-beginning 0))
  22635. (replace-match ""))
  22636. (goto-char first-heading-pos))
  22637. (mapc 'insert thetoc)
  22638. (or (looking-at "[ \t]*\n[ \t]*\n")
  22639. (insert "\n\n")))
  22640. ;; Convert whitespace place holders
  22641. (goto-char (point-min))
  22642. (let (beg end)
  22643. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22644. (setq end (next-single-property-change beg 'org-whitespace))
  22645. (goto-char beg)
  22646. (delete-region beg end)
  22647. (insert (make-string (- end beg) ?\ ))))
  22648. (save-buffer)
  22649. ;; remove display and invisible chars
  22650. (let (beg end)
  22651. (goto-char (point-min))
  22652. (while (setq beg (next-single-property-change (point) 'display))
  22653. (setq end (next-single-property-change beg 'display))
  22654. (delete-region beg end)
  22655. (goto-char beg)
  22656. (insert "=>"))
  22657. (goto-char (point-min))
  22658. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22659. (setq end (next-single-property-change beg 'org-cwidth))
  22660. (delete-region beg end)
  22661. (goto-char beg)))
  22662. (goto-char (point-min))))
  22663. (defun org-export-ascii-clean-string ()
  22664. "Do extra work for ASCII export"
  22665. (goto-char (point-min))
  22666. (while (re-search-forward org-verbatim-re nil t)
  22667. (goto-char (match-end 2))
  22668. (backward-delete-char 1) (insert "'")
  22669. (goto-char (match-beginning 2))
  22670. (delete-char 1) (insert "`")
  22671. (goto-char (match-end 2))))
  22672. (defun org-search-todo-below (line lines level)
  22673. "Search the subtree below LINE for any TODO entries."
  22674. (let ((rest (cdr (memq line lines)))
  22675. (re org-todo-line-regexp)
  22676. line lv todo)
  22677. (catch 'exit
  22678. (while (setq line (pop rest))
  22679. (if (string-match re line)
  22680. (progn
  22681. (setq lv (- (match-end 1) (match-beginning 1))
  22682. todo (and (match-beginning 2)
  22683. (not (member (match-string 2 line)
  22684. org-done-keywords))))
  22685. ; TODO, not DONE
  22686. (if (<= lv level) (throw 'exit nil))
  22687. (if todo (throw 'exit t))))))))
  22688. (defun org-html-expand-for-ascii (line)
  22689. "Handle quoted HTML for ASCII export."
  22690. (if org-export-html-expand
  22691. (while (string-match "@<[^<>\n]*>" line)
  22692. ;; We just remove the tags for now.
  22693. (setq line (replace-match "" nil nil line))))
  22694. line)
  22695. (defun org-insert-centered (s &optional underline)
  22696. "Insert the string S centered and underline it with character UNDERLINE."
  22697. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22698. (insert (make-string ind ?\ ) s "\n")
  22699. (if underline
  22700. (insert (make-string ind ?\ )
  22701. (make-string (string-width s) underline)
  22702. "\n"))))
  22703. (defun org-ascii-level-start (level title umax &optional lines)
  22704. "Insert a new level in ASCII export."
  22705. (let (char (n (- level umax 1)) (ind 0))
  22706. (if (> level umax)
  22707. (progn
  22708. (insert (make-string (* 2 n) ?\ )
  22709. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22710. org-export-ascii-bullets))
  22711. " " title "\n")
  22712. ;; find the indentation of the next non-empty line
  22713. (catch 'stop
  22714. (while lines
  22715. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22716. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22717. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22718. (pop lines)))
  22719. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22720. (if (or (not (equal (char-before) ?\n))
  22721. (not (equal (char-before (1- (point))) ?\n)))
  22722. (insert "\n"))
  22723. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22724. (unless org-export-with-tags
  22725. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22726. (setq title (replace-match "" t t title))))
  22727. (if org-export-with-section-numbers
  22728. (setq title (concat (org-section-number level) " " title)))
  22729. (insert title "\n" (make-string (string-width title) char) "\n")
  22730. (setq org-ascii-current-indentation '(0 . 0)))))
  22731. (defun org-export-visible (type arg)
  22732. "Create a copy of the visible part of the current buffer, and export it.
  22733. The copy is created in a temporary buffer and removed after use.
  22734. TYPE is the final key (as a string) that also select the export command in
  22735. the `C-c C-e' export dispatcher.
  22736. As a special case, if the you type SPC at the prompt, the temporary
  22737. org-mode file will not be removed but presented to you so that you can
  22738. continue to use it. The prefix arg ARG is passed through to the exporting
  22739. command."
  22740. (interactive
  22741. (list (progn
  22742. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22743. (read-char-exclusive))
  22744. current-prefix-arg))
  22745. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22746. (error "Invalid export key"))
  22747. (let* ((binding (cdr (assoc type
  22748. '((?a . org-export-as-ascii)
  22749. (?\C-a . org-export-as-ascii)
  22750. (?b . org-export-as-html-and-open)
  22751. (?\C-b . org-export-as-html-and-open)
  22752. (?h . org-export-as-html)
  22753. (?H . org-export-as-html-to-buffer)
  22754. (?R . org-export-region-as-html)
  22755. (?x . org-export-as-xoxo)))))
  22756. (keepp (equal type ?\ ))
  22757. (file buffer-file-name)
  22758. (buffer (get-buffer-create "*Org Export Visible*"))
  22759. s e)
  22760. ;; Need to hack the drawers here.
  22761. (save-excursion
  22762. (goto-char (point-min))
  22763. (while (re-search-forward org-drawer-regexp nil t)
  22764. (goto-char (match-beginning 1))
  22765. (or (org-invisible-p) (org-flag-drawer nil))))
  22766. (with-current-buffer buffer (erase-buffer))
  22767. (save-excursion
  22768. (setq s (goto-char (point-min)))
  22769. (while (not (= (point) (point-max)))
  22770. (goto-char (org-find-invisible))
  22771. (append-to-buffer buffer s (point))
  22772. (setq s (goto-char (org-find-visible))))
  22773. (org-cycle-hide-drawers 'all)
  22774. (goto-char (point-min))
  22775. (unless keepp
  22776. ;; Copy all comment lines to the end, to make sure #+ settings are
  22777. ;; still available for the second export step. Kind of a hack, but
  22778. ;; does do the trick.
  22779. (if (looking-at "#[^\r\n]*")
  22780. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22781. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22782. (append-to-buffer buffer (1+ (match-beginning 0))
  22783. (min (point-max) (1+ (match-end 0))))))
  22784. (set-buffer buffer)
  22785. (let ((buffer-file-name file)
  22786. (org-inhibit-startup t))
  22787. (org-mode)
  22788. (show-all)
  22789. (unless keepp (funcall binding arg))))
  22790. (if (not keepp)
  22791. (kill-buffer buffer)
  22792. (switch-to-buffer-other-window buffer)
  22793. (goto-char (point-min)))))
  22794. (defun org-find-visible ()
  22795. (let ((s (point)))
  22796. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22797. (get-char-property s 'invisible)))
  22798. s))
  22799. (defun org-find-invisible ()
  22800. (let ((s (point)))
  22801. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22802. (not (get-char-property s 'invisible))))
  22803. s))
  22804. ;;; HTML export
  22805. (defun org-get-current-options ()
  22806. "Return a string with current options as keyword options.
  22807. Does include HTML export options as well as TODO and CATEGORY stuff."
  22808. (format
  22809. "#+TITLE: %s
  22810. #+AUTHOR: %s
  22811. #+EMAIL: %s
  22812. #+LANGUAGE: %s
  22813. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22814. #+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
  22815. #+CATEGORY: %s
  22816. #+SEQ_TODO: %s
  22817. #+TYP_TODO: %s
  22818. #+PRIORITIES: %c %c %c
  22819. #+DRAWERS: %s
  22820. #+STARTUP: %s %s %s %s %s
  22821. #+TAGS: %s
  22822. #+ARCHIVE: %s
  22823. #+LINK: %s
  22824. "
  22825. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22826. org-export-headline-levels
  22827. org-export-with-section-numbers
  22828. org-export-with-toc
  22829. org-export-preserve-breaks
  22830. org-export-html-expand
  22831. org-export-with-fixed-width
  22832. org-export-with-tables
  22833. org-export-with-sub-superscripts
  22834. org-export-with-special-strings
  22835. org-export-with-footnotes
  22836. org-export-with-emphasize
  22837. org-export-with-TeX-macros
  22838. org-export-with-LaTeX-fragments
  22839. org-export-skip-text-before-1st-heading
  22840. org-export-with-drawers
  22841. org-export-with-tags
  22842. (file-name-nondirectory buffer-file-name)
  22843. "TODO FEEDBACK VERIFY DONE"
  22844. "Me Jason Marie DONE"
  22845. org-highest-priority org-lowest-priority org-default-priority
  22846. (mapconcat 'identity org-drawers " ")
  22847. (cdr (assoc org-startup-folded
  22848. '((nil . "showall") (t . "overview") (content . "content"))))
  22849. (if org-odd-levels-only "odd" "oddeven")
  22850. (if org-hide-leading-stars "hidestars" "showstars")
  22851. (if org-startup-align-all-tables "align" "noalign")
  22852. (cond ((eq org-log-done t) "logdone")
  22853. ((equal org-log-done 'note) "lognotedone")
  22854. ((not org-log-done) "nologdone"))
  22855. (or (mapconcat (lambda (x)
  22856. (cond
  22857. ((equal '(:startgroup) x) "{")
  22858. ((equal '(:endgroup) x) "}")
  22859. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22860. (t (car x))))
  22861. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22862. org-archive-location
  22863. "org file:~/org/%s.org"
  22864. ))
  22865. (defun org-insert-export-options-template ()
  22866. "Insert into the buffer a template with information for exporting."
  22867. (interactive)
  22868. (if (not (bolp)) (newline))
  22869. (let ((s (org-get-current-options)))
  22870. (and (string-match "#\\+CATEGORY" s)
  22871. (setq s (substring s 0 (match-beginning 0))))
  22872. (insert s)))
  22873. (defun org-toggle-fixed-width-section (arg)
  22874. "Toggle the fixed-width export.
  22875. If there is no active region, the QUOTE keyword at the current headline is
  22876. inserted or removed. When present, it causes the text between this headline
  22877. and the next to be exported as fixed-width text, and unmodified.
  22878. If there is an active region, this command adds or removes a colon as the
  22879. first character of this line. If the first character of a line is a colon,
  22880. this line is also exported in fixed-width font."
  22881. (interactive "P")
  22882. (let* ((cc 0)
  22883. (regionp (org-region-active-p))
  22884. (beg (if regionp (region-beginning) (point)))
  22885. (end (if regionp (region-end)))
  22886. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22887. (case-fold-search nil)
  22888. (re "[ \t]*\\(:\\)")
  22889. off)
  22890. (if regionp
  22891. (save-excursion
  22892. (goto-char beg)
  22893. (setq cc (current-column))
  22894. (beginning-of-line 1)
  22895. (setq off (looking-at re))
  22896. (while (> nlines 0)
  22897. (setq nlines (1- nlines))
  22898. (beginning-of-line 1)
  22899. (cond
  22900. (arg
  22901. (move-to-column cc t)
  22902. (insert ":\n")
  22903. (forward-line -1))
  22904. ((and off (looking-at re))
  22905. (replace-match "" t t nil 1))
  22906. ((not off) (move-to-column cc t) (insert ":")))
  22907. (forward-line 1)))
  22908. (save-excursion
  22909. (org-back-to-heading)
  22910. (if (looking-at (concat outline-regexp
  22911. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22912. (replace-match "" t t nil 1)
  22913. (if (looking-at outline-regexp)
  22914. (progn
  22915. (goto-char (match-end 0))
  22916. (insert org-quote-string " "))))))))
  22917. (defun org-export-as-html-and-open (arg)
  22918. "Export the outline as HTML and immediately open it with a browser.
  22919. If there is an active region, export only the region.
  22920. The prefix ARG specifies how many levels of the outline should become
  22921. headlines. The default is 3. Lower levels will become bulleted lists."
  22922. (interactive "P")
  22923. (org-export-as-html arg 'hidden)
  22924. (org-open-file buffer-file-name))
  22925. (defun org-export-as-html-batch ()
  22926. "Call `org-export-as-html', may be used in batch processing as
  22927. emacs --batch
  22928. --load=$HOME/lib/emacs/org.el
  22929. --eval \"(setq org-export-headline-levels 2)\"
  22930. --visit=MyFile --funcall org-export-as-html-batch"
  22931. (org-export-as-html org-export-headline-levels 'hidden))
  22932. (defun org-export-as-html-to-buffer (arg)
  22933. "Call `org-exort-as-html` with output to a temporary buffer.
  22934. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22935. (interactive "P")
  22936. (org-export-as-html arg nil nil "*Org HTML Export*")
  22937. (switch-to-buffer-other-window "*Org HTML Export*"))
  22938. (defun org-replace-region-by-html (beg end)
  22939. "Assume the current region has org-mode syntax, and convert it to HTML.
  22940. This can be used in any buffer. For example, you could write an
  22941. itemized list in org-mode syntax in an HTML buffer and then use this
  22942. command to convert it."
  22943. (interactive "r")
  22944. (let (reg html buf pop-up-frames)
  22945. (save-window-excursion
  22946. (if (org-mode-p)
  22947. (setq html (org-export-region-as-html
  22948. beg end t 'string))
  22949. (setq reg (buffer-substring beg end)
  22950. buf (get-buffer-create "*Org tmp*"))
  22951. (with-current-buffer buf
  22952. (erase-buffer)
  22953. (insert reg)
  22954. (org-mode)
  22955. (setq html (org-export-region-as-html
  22956. (point-min) (point-max) t 'string)))
  22957. (kill-buffer buf)))
  22958. (delete-region beg end)
  22959. (insert html)))
  22960. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22961. "Convert region from BEG to END in org-mode buffer to HTML.
  22962. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22963. contents, and only produce the region of converted text, useful for
  22964. cut-and-paste operations.
  22965. If BUFFER is a buffer or a string, use/create that buffer as a target
  22966. of the converted HTML. If BUFFER is the symbol `string', return the
  22967. produced HTML as a string and leave not buffer behind. For example,
  22968. a Lisp program could call this function in the following way:
  22969. (setq html (org-export-region-as-html beg end t 'string))
  22970. When called interactively, the output buffer is selected, and shown
  22971. in a window. A non-interactive call will only retunr the buffer."
  22972. (interactive "r\nP")
  22973. (when (interactive-p)
  22974. (setq buffer "*Org HTML Export*"))
  22975. (let ((transient-mark-mode t) (zmacs-regions t)
  22976. rtn)
  22977. (goto-char end)
  22978. (set-mark (point)) ;; to activate the region
  22979. (goto-char beg)
  22980. (setq rtn (org-export-as-html
  22981. nil nil nil
  22982. buffer body-only))
  22983. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22984. (if (and (interactive-p) (bufferp rtn))
  22985. (switch-to-buffer-other-window rtn)
  22986. rtn)))
  22987. (defvar html-table-tag nil) ; dynamically scoped into this.
  22988. (defun org-export-as-html (arg &optional hidden ext-plist
  22989. to-buffer body-only)
  22990. "Export the outline as a pretty HTML file.
  22991. If there is an active region, export only the region. The prefix
  22992. ARG specifies how many levels of the outline should become
  22993. headlines. The default is 3. Lower levels will become bulleted
  22994. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22995. EXT-PLIST is a property list with external parameters overriding
  22996. org-mode's default settings, but still inferior to file-local
  22997. settings. When TO-BUFFER is non-nil, create a buffer with that
  22998. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22999. don't leave any buffer behind but just return the resulting HTML as
  23000. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23001. simply return the content of <body>...</body>, without even
  23002. the body tags themselves."
  23003. (interactive "P")
  23004. ;; Make sure we have a file name when we need it.
  23005. (when (and (not (or to-buffer body-only))
  23006. (not buffer-file-name))
  23007. (if (buffer-base-buffer)
  23008. (org-set-local 'buffer-file-name
  23009. (with-current-buffer (buffer-base-buffer)
  23010. buffer-file-name))
  23011. (error "Need a file name to be able to export.")))
  23012. (message "Exporting...")
  23013. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23014. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23015. (setq-default org-done-keywords org-done-keywords)
  23016. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23017. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23018. ext-plist
  23019. (org-infile-export-plist)))
  23020. (style (plist-get opt-plist :style))
  23021. (html-extension (plist-get opt-plist :html-extension))
  23022. (link-validate (plist-get opt-plist :link-validation-function))
  23023. valid thetoc have-headings first-heading-pos
  23024. (odd org-odd-levels-only)
  23025. (region-p (org-region-active-p))
  23026. (subtree-p
  23027. (when region-p
  23028. (save-excursion
  23029. (goto-char (region-beginning))
  23030. (and (org-at-heading-p)
  23031. (>= (org-end-of-subtree t t) (region-end))))))
  23032. ;; The following two are dynamically scoped into other
  23033. ;; routines below.
  23034. (org-current-export-dir (org-export-directory :html opt-plist))
  23035. (org-current-export-file buffer-file-name)
  23036. (level 0) (line "") (origline "") txt todo
  23037. (umax nil)
  23038. (umax-toc nil)
  23039. (filename (if to-buffer nil
  23040. (expand-file-name
  23041. (concat
  23042. (file-name-sans-extension
  23043. (or (and subtree-p
  23044. (org-entry-get (region-beginning)
  23045. "EXPORT_FILE_NAME" t))
  23046. (file-name-nondirectory buffer-file-name)))
  23047. "." html-extension)
  23048. (file-name-as-directory
  23049. (org-export-directory :html opt-plist)))))
  23050. (current-dir (if buffer-file-name
  23051. (file-name-directory buffer-file-name)
  23052. default-directory))
  23053. (buffer (if to-buffer
  23054. (cond
  23055. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23056. (t (get-buffer-create to-buffer)))
  23057. (find-file-noselect filename)))
  23058. (org-levels-open (make-vector org-level-max nil))
  23059. (date (plist-get opt-plist :date))
  23060. (author (plist-get opt-plist :author))
  23061. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23062. (plist-get opt-plist :title)
  23063. (and (not
  23064. (plist-get opt-plist :skip-before-1st-heading))
  23065. (org-export-grab-title-from-buffer))
  23066. (and buffer-file-name
  23067. (file-name-sans-extension
  23068. (file-name-nondirectory buffer-file-name)))
  23069. "UNTITLED"))
  23070. (html-table-tag (plist-get opt-plist :html-table-tag))
  23071. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23072. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23073. (inquote nil)
  23074. (infixed nil)
  23075. (in-local-list nil)
  23076. (local-list-num nil)
  23077. (local-list-indent nil)
  23078. (llt org-plain-list-ordered-item-terminator)
  23079. (email (plist-get opt-plist :email))
  23080. (language (plist-get opt-plist :language))
  23081. (lang-words nil)
  23082. (target-alist nil) tg
  23083. (head-count 0) cnt
  23084. (start 0)
  23085. (coding-system (and (boundp 'buffer-file-coding-system)
  23086. buffer-file-coding-system))
  23087. (coding-system-for-write (or org-export-html-coding-system
  23088. coding-system))
  23089. (save-buffer-coding-system (or org-export-html-coding-system
  23090. coding-system))
  23091. (charset (and coding-system-for-write
  23092. (fboundp 'coding-system-get)
  23093. (coding-system-get coding-system-for-write
  23094. 'mime-charset)))
  23095. (region
  23096. (buffer-substring
  23097. (if region-p (region-beginning) (point-min))
  23098. (if region-p (region-end) (point-max))))
  23099. (lines
  23100. (org-split-string
  23101. (org-cleaned-string-for-export
  23102. region
  23103. :emph-multiline t
  23104. :for-html t
  23105. :skip-before-1st-heading
  23106. (plist-get opt-plist :skip-before-1st-heading)
  23107. :drawers (plist-get opt-plist :drawers)
  23108. :archived-trees
  23109. (plist-get opt-plist :archived-trees)
  23110. :add-text
  23111. (plist-get opt-plist :text)
  23112. :LaTeX-fragments
  23113. (plist-get opt-plist :LaTeX-fragments))
  23114. "[\r\n]"))
  23115. table-open type
  23116. table-buffer table-orig-buffer
  23117. ind start-is-num starter didclose
  23118. rpl path desc descp desc1 desc2 link
  23119. )
  23120. (let ((inhibit-read-only t))
  23121. (org-unmodified
  23122. (remove-text-properties (point-min) (point-max)
  23123. '(:org-license-to-kill t))))
  23124. (message "Exporting...")
  23125. (setq org-min-level (org-get-min-level lines))
  23126. (setq org-last-level org-min-level)
  23127. (org-init-section-numbers)
  23128. (cond
  23129. ((and date (string-match "%" date))
  23130. (setq date (format-time-string date (current-time))))
  23131. (date)
  23132. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23133. ;; Get the language-dependent settings
  23134. (setq lang-words (or (assoc language org-export-language-setup)
  23135. (assoc "en" org-export-language-setup)))
  23136. ;; Switch to the output buffer
  23137. (set-buffer buffer)
  23138. (let ((inhibit-read-only t)) (erase-buffer))
  23139. (fundamental-mode)
  23140. (and (fboundp 'set-buffer-file-coding-system)
  23141. (set-buffer-file-coding-system coding-system-for-write))
  23142. (let ((case-fold-search nil)
  23143. (org-odd-levels-only odd))
  23144. ;; create local variables for all options, to make sure all called
  23145. ;; functions get the correct information
  23146. (mapc (lambda (x)
  23147. (set (make-local-variable (cdr x))
  23148. (plist-get opt-plist (car x))))
  23149. org-export-plist-vars)
  23150. (setq umax (if arg (prefix-numeric-value arg)
  23151. org-export-headline-levels))
  23152. (setq umax-toc (if (integerp org-export-with-toc)
  23153. (min org-export-with-toc umax)
  23154. umax))
  23155. (unless body-only
  23156. ;; File header
  23157. (insert (format
  23158. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23159. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23160. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23161. lang=\"%s\" xml:lang=\"%s\">
  23162. <head>
  23163. <title>%s</title>
  23164. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23165. <meta name=\"generator\" content=\"Org-mode\"/>
  23166. <meta name=\"generated\" content=\"%s\"/>
  23167. <meta name=\"author\" content=\"%s\"/>
  23168. %s
  23169. </head><body>
  23170. "
  23171. language language (org-html-expand title)
  23172. (or charset "iso-8859-1") date author style))
  23173. (insert (or (plist-get opt-plist :preamble) ""))
  23174. (when (plist-get opt-plist :auto-preamble)
  23175. (if title (insert (format org-export-html-title-format
  23176. (org-html-expand title))))))
  23177. (if (and org-export-with-toc (not body-only))
  23178. (progn
  23179. (push (format "<h%d>%s</h%d>\n"
  23180. org-export-html-toplevel-hlevel
  23181. (nth 3 lang-words)
  23182. org-export-html-toplevel-hlevel)
  23183. thetoc)
  23184. (push "<ul>\n<li>" thetoc)
  23185. (setq lines
  23186. (mapcar '(lambda (line)
  23187. (if (string-match org-todo-line-regexp line)
  23188. ;; This is a headline
  23189. (progn
  23190. (setq have-headings t)
  23191. (setq level (- (match-end 1) (match-beginning 1))
  23192. level (org-tr-level level)
  23193. txt (save-match-data
  23194. (org-html-expand
  23195. (org-export-cleanup-toc-line
  23196. (match-string 3 line))))
  23197. todo
  23198. (or (and org-export-mark-todo-in-toc
  23199. (match-beginning 2)
  23200. (not (member (match-string 2 line)
  23201. org-done-keywords)))
  23202. ; TODO, not DONE
  23203. (and org-export-mark-todo-in-toc
  23204. (= level umax-toc)
  23205. (org-search-todo-below
  23206. line lines level))))
  23207. (if (string-match
  23208. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23209. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23210. (if (string-match quote-re0 txt)
  23211. (setq txt (replace-match "" t t txt)))
  23212. (if org-export-with-section-numbers
  23213. (setq txt (concat (org-section-number level)
  23214. " " txt)))
  23215. (if (<= level (max umax umax-toc))
  23216. (setq head-count (+ head-count 1)))
  23217. (if (<= level umax-toc)
  23218. (progn
  23219. (if (> level org-last-level)
  23220. (progn
  23221. (setq cnt (- level org-last-level))
  23222. (while (>= (setq cnt (1- cnt)) 0)
  23223. (push "\n<ul>\n<li>" thetoc))
  23224. (push "\n" thetoc)))
  23225. (if (< level org-last-level)
  23226. (progn
  23227. (setq cnt (- org-last-level level))
  23228. (while (>= (setq cnt (1- cnt)) 0)
  23229. (push "</li>\n</ul>" thetoc))
  23230. (push "\n" thetoc)))
  23231. ;; Check for targets
  23232. (while (string-match org-target-regexp line)
  23233. (setq tg (match-string 1 line)
  23234. line (replace-match
  23235. (concat "@<span class=\"target\">" tg "@</span> ")
  23236. t t line))
  23237. (push (cons (org-solidify-link-text tg)
  23238. (format "sec-%d" head-count))
  23239. target-alist))
  23240. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23241. (setq txt (replace-match "" t t txt)))
  23242. (push
  23243. (format
  23244. (if todo
  23245. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23246. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23247. head-count txt) thetoc)
  23248. (setq org-last-level level))
  23249. )))
  23250. line)
  23251. lines))
  23252. (while (> org-last-level (1- org-min-level))
  23253. (setq org-last-level (1- org-last-level))
  23254. (push "</li>\n</ul>\n" thetoc))
  23255. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23256. (setq head-count 0)
  23257. (org-init-section-numbers)
  23258. (while (setq line (pop lines) origline line)
  23259. (catch 'nextline
  23260. ;; end of quote section?
  23261. (when (and inquote (string-match "^\\*+ " line))
  23262. (insert "</pre>\n")
  23263. (setq inquote nil))
  23264. ;; inside a quote section?
  23265. (when inquote
  23266. (insert (org-html-protect line) "\n")
  23267. (throw 'nextline nil))
  23268. ;; verbatim lines
  23269. (when (and org-export-with-fixed-width
  23270. (string-match "^[ \t]*:\\(.*\\)" line))
  23271. (when (not infixed)
  23272. (setq infixed t)
  23273. (insert "<pre>\n"))
  23274. (insert (org-html-protect (match-string 1 line)) "\n")
  23275. (when (and lines
  23276. (not (string-match "^[ \t]*\\(:.*\\)"
  23277. (car lines))))
  23278. (setq infixed nil)
  23279. (insert "</pre>\n"))
  23280. (throw 'nextline nil))
  23281. ;; Protected HTML
  23282. (when (get-text-property 0 'org-protected line)
  23283. (let (par)
  23284. (when (re-search-backward
  23285. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23286. (setq par (match-string 1))
  23287. (replace-match "\\2\n"))
  23288. (insert line "\n")
  23289. (while (and lines
  23290. (or (= (length (car lines)) 0)
  23291. (get-text-property 0 'org-protected (car lines))))
  23292. (insert (pop lines) "\n"))
  23293. (and par (insert "<p>\n")))
  23294. (throw 'nextline nil))
  23295. ;; Horizontal line
  23296. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23297. (insert "\n<hr/>\n")
  23298. (throw 'nextline nil))
  23299. ;; make targets to anchors
  23300. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23301. (cond
  23302. ((match-end 2)
  23303. (setq line (replace-match
  23304. (concat "@<a name=\""
  23305. (org-solidify-link-text (match-string 1 line))
  23306. "\">\\nbsp@</a>")
  23307. t t line)))
  23308. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23309. (setq line (replace-match
  23310. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23311. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23312. t t line)))
  23313. (t
  23314. (setq line (replace-match
  23315. (concat "@<a name=\""
  23316. (org-solidify-link-text (match-string 1 line))
  23317. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23318. t t line)))))
  23319. (setq line (org-html-handle-time-stamps line))
  23320. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23321. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23322. ;; Also handle sub_superscripts and checkboxes
  23323. (or (string-match org-table-hline-regexp line)
  23324. (setq line (org-html-expand line)))
  23325. ;; Format the links
  23326. (setq start 0)
  23327. (while (string-match org-bracket-link-analytic-regexp line start)
  23328. (setq start (match-beginning 0))
  23329. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23330. (setq path (match-string 3 line))
  23331. (setq desc1 (if (match-end 5) (match-string 5 line))
  23332. desc2 (if (match-end 2) (concat type ":" path) path)
  23333. descp (and desc1 (not (equal desc1 desc2)))
  23334. desc (or desc1 desc2))
  23335. ;; Make an image out of the description if that is so wanted
  23336. (when (and descp (org-file-image-p desc))
  23337. (save-match-data
  23338. (if (string-match "^file:" desc)
  23339. (setq desc (substring desc (match-end 0)))))
  23340. (setq desc (concat "<img src=\"" desc "\"/>")))
  23341. ;; FIXME: do we need to unescape here somewhere?
  23342. (cond
  23343. ((equal type "internal")
  23344. (setq rpl
  23345. (concat
  23346. "<a href=\"#"
  23347. (org-solidify-link-text
  23348. (save-match-data (org-link-unescape path)) target-alist)
  23349. "\">" desc "</a>")))
  23350. ((member type '("http" "https"))
  23351. ;; standard URL, just check if we need to inline an image
  23352. (if (and (or (eq t org-export-html-inline-images)
  23353. (and org-export-html-inline-images (not descp)))
  23354. (org-file-image-p path))
  23355. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23356. (setq link (concat type ":" path))
  23357. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23358. ((member type '("ftp" "mailto" "news"))
  23359. ;; standard URL
  23360. (setq link (concat type ":" path))
  23361. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23362. ((string= type "file")
  23363. ;; FILE link
  23364. (let* ((filename path)
  23365. (abs-p (file-name-absolute-p filename))
  23366. thefile file-is-image-p search)
  23367. (save-match-data
  23368. (if (string-match "::\\(.*\\)" filename)
  23369. (setq search (match-string 1 filename)
  23370. filename (replace-match "" t nil filename)))
  23371. (setq valid
  23372. (if (functionp link-validate)
  23373. (funcall link-validate filename current-dir)
  23374. t))
  23375. (setq file-is-image-p (org-file-image-p filename))
  23376. (setq thefile (if abs-p (expand-file-name filename) filename))
  23377. (when (and org-export-html-link-org-files-as-html
  23378. (string-match "\\.org$" thefile))
  23379. (setq thefile (concat (substring thefile 0
  23380. (match-beginning 0))
  23381. "." html-extension))
  23382. (if (and search
  23383. ;; make sure this is can be used as target search
  23384. (not (string-match "^[0-9]*$" search))
  23385. (not (string-match "^\\*" search))
  23386. (not (string-match "^/.*/$" search)))
  23387. (setq thefile (concat thefile "#"
  23388. (org-solidify-link-text
  23389. (org-link-unescape search)))))
  23390. (when (string-match "^file:" desc)
  23391. (setq desc (replace-match "" t t desc))
  23392. (if (string-match "\\.org$" desc)
  23393. (setq desc (replace-match "" t t desc))))))
  23394. (setq rpl (if (and file-is-image-p
  23395. (or (eq t org-export-html-inline-images)
  23396. (and org-export-html-inline-images
  23397. (not descp))))
  23398. (concat "<img src=\"" thefile "\"/>")
  23399. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23400. (if (not valid) (setq rpl desc))))
  23401. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23402. (setq rpl (concat "<i>&lt;" type ":"
  23403. (save-match-data (org-link-unescape path))
  23404. "&gt;</i>"))))
  23405. (setq line (replace-match rpl t t line)
  23406. start (+ start (length rpl))))
  23407. ;; TODO items
  23408. (if (and (string-match org-todo-line-regexp line)
  23409. (match-beginning 2))
  23410. (setq line
  23411. (concat (substring line 0 (match-beginning 2))
  23412. "<span class=\""
  23413. (if (member (match-string 2 line)
  23414. org-done-keywords)
  23415. "done" "todo")
  23416. "\">" (match-string 2 line)
  23417. "</span>" (substring line (match-end 2)))))
  23418. ;; Does this contain a reference to a footnote?
  23419. (when org-export-with-footnotes
  23420. (setq start 0)
  23421. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23422. (if (get-text-property (match-beginning 2) 'org-protected line)
  23423. (setq start (match-end 2))
  23424. (let ((n (match-string 2 line)))
  23425. (setq line
  23426. (replace-match
  23427. (format
  23428. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23429. (match-string 1 line) n n n)
  23430. t t line))))))
  23431. (cond
  23432. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23433. ;; This is a headline
  23434. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23435. txt (match-string 2 line))
  23436. (if (string-match quote-re0 txt)
  23437. (setq txt (replace-match "" t t txt)))
  23438. (if (<= level (max umax umax-toc))
  23439. (setq head-count (+ head-count 1)))
  23440. (when in-local-list
  23441. ;; Close any local lists before inserting a new header line
  23442. (while local-list-num
  23443. (org-close-li)
  23444. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23445. (pop local-list-num))
  23446. (setq local-list-indent nil
  23447. in-local-list nil))
  23448. (setq first-heading-pos (or first-heading-pos (point)))
  23449. (org-html-level-start level txt umax
  23450. (and org-export-with-toc (<= level umax))
  23451. head-count)
  23452. ;; QUOTES
  23453. (when (string-match quote-re line)
  23454. (insert "<pre>")
  23455. (setq inquote t)))
  23456. ((and org-export-with-tables
  23457. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23458. (if (not table-open)
  23459. ;; New table starts
  23460. (setq table-open t table-buffer nil table-orig-buffer nil))
  23461. ;; Accumulate lines
  23462. (setq table-buffer (cons line table-buffer)
  23463. table-orig-buffer (cons origline table-orig-buffer))
  23464. (when (or (not lines)
  23465. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23466. (car lines))))
  23467. (setq table-open nil
  23468. table-buffer (nreverse table-buffer)
  23469. table-orig-buffer (nreverse table-orig-buffer))
  23470. (org-close-par-maybe)
  23471. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23472. (t
  23473. ;; Normal lines
  23474. (when (string-match
  23475. (cond
  23476. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23477. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23478. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23479. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23480. line)
  23481. (setq ind (org-get-string-indentation line)
  23482. start-is-num (match-beginning 4)
  23483. starter (if (match-beginning 2)
  23484. (substring (match-string 2 line) 0 -1))
  23485. line (substring line (match-beginning 5)))
  23486. (unless (string-match "[^ \t]" line)
  23487. ;; empty line. Pretend indentation is large.
  23488. (setq ind (if org-empty-line-terminates-plain-lists
  23489. 0
  23490. (1+ (or (car local-list-indent) 1)))))
  23491. (setq didclose nil)
  23492. (while (and in-local-list
  23493. (or (and (= ind (car local-list-indent))
  23494. (not starter))
  23495. (< ind (car local-list-indent))))
  23496. (setq didclose t)
  23497. (org-close-li)
  23498. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23499. (pop local-list-num) (pop local-list-indent)
  23500. (setq in-local-list local-list-indent))
  23501. (cond
  23502. ((and starter
  23503. (or (not in-local-list)
  23504. (> ind (car local-list-indent))))
  23505. ;; Start new (level of) list
  23506. (org-close-par-maybe)
  23507. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23508. (push start-is-num local-list-num)
  23509. (push ind local-list-indent)
  23510. (setq in-local-list t))
  23511. (starter
  23512. ;; continue current list
  23513. (org-close-li)
  23514. (insert "<li>\n"))
  23515. (didclose
  23516. ;; we did close a list, normal text follows: need <p>
  23517. (org-open-par)))
  23518. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23519. (setq line
  23520. (replace-match
  23521. (if (equal (match-string 1 line) "X")
  23522. "<b>[X]</b>"
  23523. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23524. t t line))))
  23525. ;; Empty lines start a new paragraph. If hand-formatted lists
  23526. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23527. ;; also start a new paragraph.
  23528. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23529. ;; Is this the start of a footnote?
  23530. (when org-export-with-footnotes
  23531. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23532. (org-close-par-maybe)
  23533. (let ((n (match-string 1 line)))
  23534. (setq line (replace-match
  23535. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23536. ;; Check if the line break needs to be conserved
  23537. (cond
  23538. ((string-match "\\\\\\\\[ \t]*$" line)
  23539. (setq line (replace-match "<br/>" t t line)))
  23540. (org-export-preserve-breaks
  23541. (setq line (concat line "<br/>"))))
  23542. (insert line "\n")))))
  23543. ;; Properly close all local lists and other lists
  23544. (when inquote (insert "</pre>\n"))
  23545. (when in-local-list
  23546. ;; Close any local lists before inserting a new header line
  23547. (while local-list-num
  23548. (org-close-li)
  23549. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23550. (pop local-list-num))
  23551. (setq local-list-indent nil
  23552. in-local-list nil))
  23553. (org-html-level-start 1 nil umax
  23554. (and org-export-with-toc (<= level umax))
  23555. head-count)
  23556. (unless body-only
  23557. (when (plist-get opt-plist :auto-postamble)
  23558. (insert "<div id=\"postamble\">")
  23559. (when (and org-export-author-info author)
  23560. (insert "<p class=\"author\"> "
  23561. (nth 1 lang-words) ": " author "\n")
  23562. (when email
  23563. (if (listp (split-string email ",+ *"))
  23564. (mapc (lambda(e)
  23565. (insert "<a href=\"mailto:" e "\">&lt;"
  23566. e "&gt;</a>\n"))
  23567. (split-string email ",+ *"))
  23568. (insert "<a href=\"mailto:" email "\">&lt;"
  23569. email "&gt;</a>\n")))
  23570. (insert "</p>\n"))
  23571. (when (and date org-export-time-stamp-file)
  23572. (insert "<p class=\"date\"> "
  23573. (nth 2 lang-words) ": "
  23574. date "</p>\n"))
  23575. (insert "</div>"))
  23576. (if org-export-html-with-timestamp
  23577. (insert org-export-html-html-helper-timestamp))
  23578. (insert (or (plist-get opt-plist :postamble) ""))
  23579. (insert "</body>\n</html>\n"))
  23580. (normal-mode)
  23581. (if (eq major-mode default-major-mode) (html-mode))
  23582. ;; insert the table of contents
  23583. (goto-char (point-min))
  23584. (when thetoc
  23585. (if (or (re-search-forward
  23586. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23587. (re-search-forward
  23588. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23589. (progn
  23590. (goto-char (match-beginning 0))
  23591. (replace-match ""))
  23592. (goto-char first-heading-pos)
  23593. (when (looking-at "\\s-*</p>")
  23594. (goto-char (match-end 0))
  23595. (insert "\n")))
  23596. (insert "<div id=\"table-of-contents\">\n")
  23597. (mapc 'insert thetoc)
  23598. (insert "</div>\n"))
  23599. ;; remove empty paragraphs and lists
  23600. (goto-char (point-min))
  23601. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23602. (replace-match ""))
  23603. (goto-char (point-min))
  23604. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23605. (replace-match ""))
  23606. (goto-char (point-min))
  23607. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23608. (replace-match ""))
  23609. ;; Convert whitespace place holders
  23610. (goto-char (point-min))
  23611. (let (beg end n)
  23612. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23613. (setq n (get-text-property beg 'org-whitespace)
  23614. end (next-single-property-change beg 'org-whitespace))
  23615. (goto-char beg)
  23616. (delete-region beg end)
  23617. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23618. (make-string n ?x)))))
  23619. (or to-buffer (save-buffer))
  23620. (goto-char (point-min))
  23621. (message "Exporting... done")
  23622. (if (eq to-buffer 'string)
  23623. (prog1 (buffer-substring (point-min) (point-max))
  23624. (kill-buffer (current-buffer)))
  23625. (current-buffer)))))
  23626. (defvar org-table-colgroup-info nil)
  23627. (defun org-format-table-ascii (lines)
  23628. "Format a table for ascii export."
  23629. (if (stringp lines)
  23630. (setq lines (org-split-string lines "\n")))
  23631. (if (not (string-match "^[ \t]*|" (car lines)))
  23632. ;; Table made by table.el - test for spanning
  23633. lines
  23634. ;; A normal org table
  23635. ;; Get rid of hlines at beginning and end
  23636. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23637. (setq lines (nreverse lines))
  23638. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23639. (setq lines (nreverse lines))
  23640. (when org-export-table-remove-special-lines
  23641. ;; Check if the table has a marking column. If yes remove the
  23642. ;; column and the special lines
  23643. (setq lines (org-table-clean-before-export lines)))
  23644. ;; Get rid of the vertical lines except for grouping
  23645. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23646. rtn line vl1 start)
  23647. (while (setq line (pop lines))
  23648. (if (string-match org-table-hline-regexp line)
  23649. (and (string-match "|\\(.*\\)|" line)
  23650. (setq line (replace-match " \\1" t nil line)))
  23651. (setq start 0 vl1 vl)
  23652. (while (string-match "|" line start)
  23653. (setq start (match-end 0))
  23654. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23655. (push line rtn))
  23656. (nreverse rtn))))
  23657. (defun org-colgroup-info-to-vline-list (info)
  23658. (let (vl new last)
  23659. (while info
  23660. (setq last new new (pop info))
  23661. (if (or (memq last '(:end :startend))
  23662. (memq new '(:start :startend)))
  23663. (push t vl)
  23664. (push nil vl)))
  23665. (setq vl (nreverse vl))
  23666. (and vl (setcar vl nil))
  23667. vl))
  23668. (defun org-format-table-html (lines olines)
  23669. "Find out which HTML converter to use and return the HTML code."
  23670. (if (stringp lines)
  23671. (setq lines (org-split-string lines "\n")))
  23672. (if (string-match "^[ \t]*|" (car lines))
  23673. ;; A normal org table
  23674. (org-format-org-table-html lines)
  23675. ;; Table made by table.el - test for spanning
  23676. (let* ((hlines (delq nil (mapcar
  23677. (lambda (x)
  23678. (if (string-match "^[ \t]*\\+-" x) x
  23679. nil))
  23680. lines)))
  23681. (first (car hlines))
  23682. (ll (and (string-match "\\S-+" first)
  23683. (match-string 0 first)))
  23684. (re (concat "^[ \t]*" (regexp-quote ll)))
  23685. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23686. hlines))))
  23687. (if (and (not spanning)
  23688. (not org-export-prefer-native-exporter-for-tables))
  23689. ;; We can use my own converter with HTML conversions
  23690. (org-format-table-table-html lines)
  23691. ;; Need to use the code generator in table.el, with the original text.
  23692. (org-format-table-table-html-using-table-generate-source olines)))))
  23693. (defun org-format-org-table-html (lines &optional splice)
  23694. "Format a table into HTML."
  23695. ;; Get rid of hlines at beginning and end
  23696. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23697. (setq lines (nreverse lines))
  23698. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23699. (setq lines (nreverse lines))
  23700. (when org-export-table-remove-special-lines
  23701. ;; Check if the table has a marking column. If yes remove the
  23702. ;; column and the special lines
  23703. (setq lines (org-table-clean-before-export lines)))
  23704. (let ((head (and org-export-highlight-first-table-line
  23705. (delq nil (mapcar
  23706. (lambda (x) (string-match "^[ \t]*|-" x))
  23707. (cdr lines)))))
  23708. (nlines 0) fnum i
  23709. tbopen line fields html gr colgropen)
  23710. (if splice (setq head nil))
  23711. (unless splice (push (if head "<thead>" "<tbody>") html))
  23712. (setq tbopen t)
  23713. (while (setq line (pop lines))
  23714. (catch 'next-line
  23715. (if (string-match "^[ \t]*|-" line)
  23716. (progn
  23717. (unless splice
  23718. (push (if head "</thead>" "</tbody>") html)
  23719. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23720. (setq head nil) ;; head ends here, first time around
  23721. ;; ignore this line
  23722. (throw 'next-line t)))
  23723. ;; Break the line into fields
  23724. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23725. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23726. (setq nlines (1+ nlines) i -1)
  23727. (push (concat "<tr>"
  23728. (mapconcat
  23729. (lambda (x)
  23730. (setq i (1+ i))
  23731. (if (and (< i nlines)
  23732. (string-match org-table-number-regexp x))
  23733. (incf (aref fnum i)))
  23734. (if head
  23735. (concat (car org-export-table-header-tags) x
  23736. (cdr org-export-table-header-tags))
  23737. (concat (car org-export-table-data-tags) x
  23738. (cdr org-export-table-data-tags))))
  23739. fields "")
  23740. "</tr>")
  23741. html)))
  23742. (unless splice (if tbopen (push "</tbody>" html)))
  23743. (unless splice (push "</table>\n" html))
  23744. (setq html (nreverse html))
  23745. (unless splice
  23746. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23747. (push (mapconcat
  23748. (lambda (x)
  23749. (setq gr (pop org-table-colgroup-info))
  23750. (format "%s<col align=\"%s\"></col>%s"
  23751. (if (memq gr '(:start :startend))
  23752. (prog1
  23753. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23754. (setq colgropen t))
  23755. "")
  23756. (if (> (/ (float x) nlines) org-table-number-fraction)
  23757. "right" "left")
  23758. (if (memq gr '(:end :startend))
  23759. (progn (setq colgropen nil) "</colgroup>")
  23760. "")))
  23761. fnum "")
  23762. html)
  23763. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23764. (push html-table-tag html))
  23765. (concat (mapconcat 'identity html "\n") "\n")))
  23766. (defun org-table-clean-before-export (lines)
  23767. "Check if the table has a marking column.
  23768. If yes remove the column and the special lines."
  23769. (setq org-table-colgroup-info nil)
  23770. (if (memq nil
  23771. (mapcar
  23772. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23773. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23774. lines))
  23775. (progn
  23776. (setq org-table-clean-did-remove-column nil)
  23777. (delq nil
  23778. (mapcar
  23779. (lambda (x)
  23780. (cond
  23781. ((string-match "^[ \t]*| */ *|" x)
  23782. (setq org-table-colgroup-info
  23783. (mapcar (lambda (x)
  23784. (cond ((member x '("<" "&lt;")) :start)
  23785. ((member x '(">" "&gt;")) :end)
  23786. ((member x '("<>" "&lt;&gt;")) :startend)
  23787. (t nil)))
  23788. (org-split-string x "[ \t]*|[ \t]*")))
  23789. nil)
  23790. (t x)))
  23791. lines)))
  23792. (setq org-table-clean-did-remove-column t)
  23793. (delq nil
  23794. (mapcar
  23795. (lambda (x)
  23796. (cond
  23797. ((string-match "^[ \t]*| */ *|" x)
  23798. (setq org-table-colgroup-info
  23799. (mapcar (lambda (x)
  23800. (cond ((member x '("<" "&lt;")) :start)
  23801. ((member x '(">" "&gt;")) :end)
  23802. ((member x '("<>" "&lt;&gt;")) :startend)
  23803. (t nil)))
  23804. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23805. nil)
  23806. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23807. nil) ; ignore this line
  23808. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23809. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23810. ;; remove the first column
  23811. (replace-match "\\1|" t nil x))))
  23812. lines))))
  23813. (defun org-format-table-table-html (lines)
  23814. "Format a table generated by table.el into HTML.
  23815. This conversion does *not* use `table-generate-source' from table.el.
  23816. This has the advantage that Org-mode's HTML conversions can be used.
  23817. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23818. (let (line field-buffer
  23819. (head org-export-highlight-first-table-line)
  23820. fields html empty)
  23821. (setq html (concat html-table-tag "\n"))
  23822. (while (setq line (pop lines))
  23823. (setq empty "&nbsp;")
  23824. (catch 'next-line
  23825. (if (string-match "^[ \t]*\\+-" line)
  23826. (progn
  23827. (if field-buffer
  23828. (progn
  23829. (setq
  23830. html
  23831. (concat
  23832. html
  23833. "<tr>"
  23834. (mapconcat
  23835. (lambda (x)
  23836. (if (equal x "") (setq x empty))
  23837. (if head
  23838. (concat (car org-export-table-header-tags) x
  23839. (cdr org-export-table-header-tags))
  23840. (concat (car org-export-table-data-tags) x
  23841. (cdr org-export-table-data-tags))))
  23842. field-buffer "\n")
  23843. "</tr>\n"))
  23844. (setq head nil)
  23845. (setq field-buffer nil)))
  23846. ;; Ignore this line
  23847. (throw 'next-line t)))
  23848. ;; Break the line into fields and store the fields
  23849. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23850. (if field-buffer
  23851. (setq field-buffer (mapcar
  23852. (lambda (x)
  23853. (concat x "<br/>" (pop fields)))
  23854. field-buffer))
  23855. (setq field-buffer fields))))
  23856. (setq html (concat html "</table>\n"))
  23857. html))
  23858. (defun org-format-table-table-html-using-table-generate-source (lines)
  23859. "Format a table into html, using `table-generate-source' from table.el.
  23860. This has the advantage that cell- or row-spanning is allowed.
  23861. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23862. (require 'table)
  23863. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23864. (erase-buffer)
  23865. (insert (mapconcat 'identity lines "\n"))
  23866. (goto-char (point-min))
  23867. (if (not (re-search-forward "|[^+]" nil t))
  23868. (error "Error processing table"))
  23869. (table-recognize-table)
  23870. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23871. (table-generate-source 'html " org-tmp2 ")
  23872. (set-buffer " org-tmp2 ")
  23873. (buffer-substring (point-min) (point-max))))
  23874. (defun org-html-handle-time-stamps (s)
  23875. "Format time stamps in string S, or remove them."
  23876. (catch 'exit
  23877. (let (r b)
  23878. (while (string-match org-maybe-keyword-time-regexp s)
  23879. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23880. ;; never export CLOCK
  23881. (throw 'exit ""))
  23882. (or b (setq b (substring s 0 (match-beginning 0))))
  23883. (if (not org-export-with-timestamps)
  23884. (setq r (concat r (substring s 0 (match-beginning 0)))
  23885. s (substring s (match-end 0)))
  23886. (setq r (concat
  23887. r (substring s 0 (match-beginning 0))
  23888. (if (match-end 1)
  23889. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23890. (match-string 1 s)))
  23891. (format " @<span class=\"timestamp\">%s@</span>"
  23892. (substring
  23893. (org-translate-time (match-string 3 s)) 1 -1)))
  23894. s (substring s (match-end 0)))))
  23895. ;; Line break if line started and ended with time stamp stuff
  23896. (if (not r)
  23897. s
  23898. (setq r (concat r s))
  23899. (unless (string-match "\\S-" (concat b s))
  23900. (setq r (concat r "@<br/>")))
  23901. r))))
  23902. (defun org-html-protect (s)
  23903. ;; convert & to &amp;, < to &lt; and > to &gt;
  23904. (let ((start 0))
  23905. (while (string-match "&" s start)
  23906. (setq s (replace-match "&amp;" t t s)
  23907. start (1+ (match-beginning 0))))
  23908. (while (string-match "<" s)
  23909. (setq s (replace-match "&lt;" t t s)))
  23910. (while (string-match ">" s)
  23911. (setq s (replace-match "&gt;" t t s))))
  23912. s)
  23913. (defun org-export-cleanup-toc-line (s)
  23914. "Remove tags and time staps from lines going into the toc."
  23915. (when (memq org-export-with-tags '(not-in-toc nil))
  23916. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23917. (setq s (replace-match "" t t s))))
  23918. (when org-export-remove-timestamps-from-toc
  23919. (while (string-match org-maybe-keyword-time-regexp s)
  23920. (setq s (replace-match "" t t s))))
  23921. (while (string-match org-bracket-link-regexp s)
  23922. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23923. t t s)))
  23924. s)
  23925. (defun org-html-expand (string)
  23926. "Prepare STRING for HTML export. Applies all active conversions.
  23927. If there are links in the string, don't modify these."
  23928. (let* ((re (concat org-bracket-link-regexp "\\|"
  23929. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23930. m s l res)
  23931. (while (setq m (string-match re string))
  23932. (setq s (substring string 0 m)
  23933. l (match-string 0 string)
  23934. string (substring string (match-end 0)))
  23935. (push (org-html-do-expand s) res)
  23936. (push l res))
  23937. (push (org-html-do-expand string) res)
  23938. (apply 'concat (nreverse res))))
  23939. (defun org-html-do-expand (s)
  23940. "Apply all active conversions to translate special ASCII to HTML."
  23941. (setq s (org-html-protect s))
  23942. (if org-export-html-expand
  23943. (let ((start 0))
  23944. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23945. (setq s (replace-match "<\\1>" t nil s)))))
  23946. (if org-export-with-emphasize
  23947. (setq s (org-export-html-convert-emphasize s)))
  23948. (if org-export-with-special-strings
  23949. (setq s (org-export-html-convert-special-strings s)))
  23950. (if org-export-with-sub-superscripts
  23951. (setq s (org-export-html-convert-sub-super s)))
  23952. (if org-export-with-TeX-macros
  23953. (let ((start 0) wd ass)
  23954. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23955. (if (get-text-property (match-beginning 0) 'org-protected s)
  23956. (setq start (match-end 0))
  23957. (setq wd (match-string 1 s))
  23958. (if (setq ass (assoc wd org-html-entities))
  23959. (setq s (replace-match (or (cdr ass)
  23960. (concat "&" (car ass) ";"))
  23961. t t s))
  23962. (setq start (+ start (length wd))))))))
  23963. s)
  23964. (defun org-create-multibrace-regexp (left right n)
  23965. "Create a regular expression which will match a balanced sexp.
  23966. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23967. as single character strings.
  23968. The regexp returned will match the entire expression including the
  23969. delimiters. It will also define a single group which contains the
  23970. match except for the outermost delimiters. The maximum depth of
  23971. stacked delimiters is N. Escaping delimiters is not possible."
  23972. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23973. (or "\\|")
  23974. (re nothing)
  23975. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23976. (while (> n 1)
  23977. (setq n (1- n)
  23978. re (concat re or next)
  23979. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23980. (concat left "\\(" re "\\)" right)))
  23981. (defvar org-match-substring-regexp
  23982. (concat
  23983. "\\([^\\]\\)\\([_^]\\)\\("
  23984. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23985. "\\|"
  23986. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23987. "\\|"
  23988. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23989. "The regular expression matching a sub- or superscript.")
  23990. (defvar org-match-substring-with-braces-regexp
  23991. (concat
  23992. "\\([^\\]\\)\\([_^]\\)\\("
  23993. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23994. "\\)")
  23995. "The regular expression matching a sub- or superscript, forcing braces.")
  23996. (defconst org-export-html-special-string-regexps
  23997. '(("\\\\-" . "&shy;")
  23998. ("---\\([^-]\\)" . "&mdash;\\1")
  23999. ("--\\([^-]\\)" . "&ndash;\\1")
  24000. ("\\.\\.\\." . "&hellip;"))
  24001. "Regular expressions for special string conversion.")
  24002. (defun org-export-html-convert-special-strings (string)
  24003. "Convert special characters in STRING to HTML."
  24004. (let ((all org-export-html-special-string-regexps)
  24005. e a re rpl start)
  24006. (while (setq a (pop all))
  24007. (setq re (car a) rpl (cdr a) start 0)
  24008. (while (string-match re string start)
  24009. (if (get-text-property (match-beginning 0) 'org-protected string)
  24010. (setq start (match-end 0))
  24011. (setq string (replace-match rpl t nil string)))))
  24012. string))
  24013. (defun org-export-html-convert-sub-super (string)
  24014. "Convert sub- and superscripts in STRING to HTML."
  24015. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24016. (while (string-match org-match-substring-regexp string s)
  24017. (cond
  24018. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24019. ((get-text-property (match-beginning 2) 'org-protected string)
  24020. (setq s (match-end 2)))
  24021. (t
  24022. (setq s (match-end 1)
  24023. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24024. c (or (match-string 8 string)
  24025. (match-string 6 string)
  24026. (match-string 5 string))
  24027. string (replace-match
  24028. (concat (match-string 1 string)
  24029. "<" key ">" c "</" key ">")
  24030. t t string)))))
  24031. (while (string-match "\\\\\\([_^]\\)" string)
  24032. (setq string (replace-match (match-string 1 string) t t string)))
  24033. string))
  24034. (defun org-export-html-convert-emphasize (string)
  24035. "Apply emphasis."
  24036. (let ((s 0) rpl)
  24037. (while (string-match org-emph-re string s)
  24038. (if (not (equal
  24039. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24040. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24041. (setq s (match-beginning 0)
  24042. rpl
  24043. (concat
  24044. (match-string 1 string)
  24045. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24046. (match-string 4 string)
  24047. (nth 3 (assoc (match-string 3 string)
  24048. org-emphasis-alist))
  24049. (match-string 5 string))
  24050. string (replace-match rpl t t string)
  24051. s (+ s (- (length rpl) 2)))
  24052. (setq s (1+ s))))
  24053. string))
  24054. (defvar org-par-open nil)
  24055. (defun org-open-par ()
  24056. "Insert <p>, but first close previous paragraph if any."
  24057. (org-close-par-maybe)
  24058. (insert "\n<p>")
  24059. (setq org-par-open t))
  24060. (defun org-close-par-maybe ()
  24061. "Close paragraph if there is one open."
  24062. (when org-par-open
  24063. (insert "</p>")
  24064. (setq org-par-open nil)))
  24065. (defun org-close-li ()
  24066. "Close <li> if necessary."
  24067. (org-close-par-maybe)
  24068. (insert "</li>\n"))
  24069. (defvar body-only) ; dynamically scoped into this.
  24070. (defun org-html-level-start (level title umax with-toc head-count)
  24071. "Insert a new level in HTML export.
  24072. When TITLE is nil, just close all open levels."
  24073. (org-close-par-maybe)
  24074. (let ((l org-level-max))
  24075. (while (>= l level)
  24076. (if (aref org-levels-open (1- l))
  24077. (progn
  24078. (org-html-level-close l umax)
  24079. (aset org-levels-open (1- l) nil)))
  24080. (setq l (1- l)))
  24081. (when title
  24082. ;; If title is nil, this means this function is called to close
  24083. ;; all levels, so the rest is done only if title is given
  24084. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24085. (setq title (replace-match
  24086. (if org-export-with-tags
  24087. (save-match-data
  24088. (concat
  24089. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24090. (mapconcat 'identity (org-split-string
  24091. (match-string 1 title) ":")
  24092. "&nbsp;")
  24093. "</span>"))
  24094. "")
  24095. t t title)))
  24096. (if (> level umax)
  24097. (progn
  24098. (if (aref org-levels-open (1- level))
  24099. (progn
  24100. (org-close-li)
  24101. (insert "<li>" title "<br/>\n"))
  24102. (aset org-levels-open (1- level) t)
  24103. (org-close-par-maybe)
  24104. (insert "<ul>\n<li>" title "<br/>\n")))
  24105. (aset org-levels-open (1- level) t)
  24106. (if (and org-export-with-section-numbers (not body-only))
  24107. (setq title (concat (org-section-number level) " " title)))
  24108. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24109. (if with-toc
  24110. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24111. level level head-count title level))
  24112. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24113. (org-open-par)))))
  24114. (defun org-html-level-close (level max-outline-level)
  24115. "Terminate one level in HTML export."
  24116. (if (<= level max-outline-level)
  24117. (insert "</div>\n")
  24118. (org-close-li)
  24119. (insert "</ul>\n")))
  24120. ;;; iCalendar export
  24121. ;;;###autoload
  24122. (defun org-export-icalendar-this-file ()
  24123. "Export current file as an iCalendar file.
  24124. The iCalendar file will be located in the same directory as the Org-mode
  24125. file, but with extension `.ics'."
  24126. (interactive)
  24127. (org-export-icalendar nil buffer-file-name))
  24128. ;;;###autoload
  24129. (defun org-export-icalendar-all-agenda-files ()
  24130. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24131. Each iCalendar file will be located in the same directory as the Org-mode
  24132. file, but with extension `.ics'."
  24133. (interactive)
  24134. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24135. ;;;###autoload
  24136. (defun org-export-icalendar-combine-agenda-files ()
  24137. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24138. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24139. (interactive)
  24140. (apply 'org-export-icalendar t (org-agenda-files t)))
  24141. (defun org-export-icalendar (combine &rest files)
  24142. "Create iCalendar files for all elements of FILES.
  24143. If COMBINE is non-nil, combine all calendar entries into a single large
  24144. file and store it under the name `org-combined-agenda-icalendar-file'."
  24145. (save-excursion
  24146. (org-prepare-agenda-buffers files)
  24147. (let* ((dir (org-export-directory
  24148. :ical (list :publishing-directory
  24149. org-export-publishing-directory)))
  24150. file ical-file ical-buffer category started org-agenda-new-buffers)
  24151. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24152. (when combine
  24153. (setq ical-file
  24154. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24155. org-combined-agenda-icalendar-file
  24156. (expand-file-name org-combined-agenda-icalendar-file dir))
  24157. ical-buffer (org-get-agenda-file-buffer ical-file))
  24158. (set-buffer ical-buffer) (erase-buffer))
  24159. (while (setq file (pop files))
  24160. (catch 'nextfile
  24161. (org-check-agenda-file file)
  24162. (set-buffer (org-get-agenda-file-buffer file))
  24163. (unless combine
  24164. (setq ical-file (concat (file-name-as-directory dir)
  24165. (file-name-sans-extension
  24166. (file-name-nondirectory buffer-file-name))
  24167. ".ics"))
  24168. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24169. (with-current-buffer ical-buffer (erase-buffer)))
  24170. (setq category (or org-category
  24171. (file-name-sans-extension
  24172. (file-name-nondirectory buffer-file-name))))
  24173. (if (symbolp category) (setq category (symbol-name category)))
  24174. (let ((standard-output ical-buffer))
  24175. (if combine
  24176. (and (not started) (setq started t)
  24177. (org-start-icalendar-file org-icalendar-combined-name))
  24178. (org-start-icalendar-file category))
  24179. (org-print-icalendar-entries combine)
  24180. (when (or (and combine (not files)) (not combine))
  24181. (org-finish-icalendar-file)
  24182. (set-buffer ical-buffer)
  24183. (save-buffer)
  24184. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24185. (org-release-buffers org-agenda-new-buffers))))
  24186. (defvar org-after-save-iCalendar-file-hook nil
  24187. "Hook run after an iCalendar file has been saved.
  24188. The iCalendar buffer is still current when this hook is run.
  24189. A good way to use this is to tell a desktop calenndar application to re-read
  24190. the iCalendar file.")
  24191. (defun org-print-icalendar-entries (&optional combine)
  24192. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24193. When COMBINE is non nil, add the category to each line."
  24194. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24195. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24196. (dts (org-ical-ts-to-string
  24197. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24198. "DTSTART"))
  24199. hd ts ts2 state status (inc t) pos b sexp rrule
  24200. scheduledp deadlinep tmp pri category entry location summary desc
  24201. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24202. (org-refresh-category-properties)
  24203. (save-excursion
  24204. (goto-char (point-min))
  24205. (while (re-search-forward re1 nil t)
  24206. (catch :skip
  24207. (org-agenda-skip)
  24208. (setq pos (match-beginning 0)
  24209. ts (match-string 0)
  24210. inc t
  24211. hd (org-get-heading)
  24212. summary (org-icalendar-cleanup-string
  24213. (org-entry-get nil "SUMMARY"))
  24214. desc (org-icalendar-cleanup-string
  24215. (or (org-entry-get nil "DESCRIPTION")
  24216. (and org-icalendar-include-body (org-get-entry)))
  24217. t org-icalendar-include-body)
  24218. location (org-icalendar-cleanup-string
  24219. (org-entry-get nil "LOCATION"))
  24220. category (org-get-category))
  24221. (if (looking-at re2)
  24222. (progn
  24223. (goto-char (match-end 0))
  24224. (setq ts2 (match-string 1) inc nil))
  24225. (setq tmp (buffer-substring (max (point-min)
  24226. (- pos org-ds-keyword-length))
  24227. pos)
  24228. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24229. (progn
  24230. (setq inc nil)
  24231. (replace-match "\\1" t nil ts))
  24232. ts)
  24233. deadlinep (string-match org-deadline-regexp tmp)
  24234. scheduledp (string-match org-scheduled-regexp tmp)
  24235. ;; donep (org-entry-is-done-p)
  24236. ))
  24237. (if (or (string-match org-tr-regexp hd)
  24238. (string-match org-ts-regexp hd))
  24239. (setq hd (replace-match "" t t hd)))
  24240. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24241. (setq rrule
  24242. (concat "\nRRULE:FREQ="
  24243. (cdr (assoc
  24244. (match-string 2 ts)
  24245. '(("d" . "DAILY")("w" . "WEEKLY")
  24246. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24247. ";INTERVAL=" (match-string 1 ts)))
  24248. (setq rrule ""))
  24249. (setq summary (or summary hd))
  24250. (if (string-match org-bracket-link-regexp summary)
  24251. (setq summary
  24252. (replace-match (if (match-end 3)
  24253. (match-string 3 summary)
  24254. (match-string 1 summary))
  24255. t t summary)))
  24256. (if deadlinep (setq summary (concat "DL: " summary)))
  24257. (if scheduledp (setq summary (concat "S: " summary)))
  24258. (if (string-match "\\`<%%" ts)
  24259. (with-current-buffer sexp-buffer
  24260. (insert (substring ts 1 -1) " " summary "\n"))
  24261. (princ (format "BEGIN:VEVENT
  24262. %s
  24263. %s%s
  24264. SUMMARY:%s%s%s
  24265. CATEGORIES:%s
  24266. END:VEVENT\n"
  24267. (org-ical-ts-to-string ts "DTSTART")
  24268. (org-ical-ts-to-string ts2 "DTEND" inc)
  24269. rrule summary
  24270. (if (and desc (string-match "\\S-" desc))
  24271. (concat "\nDESCRIPTION: " desc) "")
  24272. (if (and location (string-match "\\S-" location))
  24273. (concat "\nLOCATION: " location) "")
  24274. category)))))
  24275. (when (and org-icalendar-include-sexps
  24276. (condition-case nil (require 'icalendar) (error nil))
  24277. (fboundp 'icalendar-export-region))
  24278. ;; Get all the literal sexps
  24279. (goto-char (point-min))
  24280. (while (re-search-forward "^&?%%(" nil t)
  24281. (catch :skip
  24282. (org-agenda-skip)
  24283. (setq b (match-beginning 0))
  24284. (goto-char (1- (match-end 0)))
  24285. (forward-sexp 1)
  24286. (end-of-line 1)
  24287. (setq sexp (buffer-substring b (point)))
  24288. (with-current-buffer sexp-buffer
  24289. (insert sexp "\n"))
  24290. (princ (org-diary-to-ical-string sexp-buffer)))))
  24291. (when org-icalendar-include-todo
  24292. (goto-char (point-min))
  24293. (while (re-search-forward org-todo-line-regexp nil t)
  24294. (catch :skip
  24295. (org-agenda-skip)
  24296. (setq state (match-string 2))
  24297. (setq status (if (member state org-done-keywords)
  24298. "COMPLETED" "NEEDS-ACTION"))
  24299. (when (and state
  24300. (or (not (member state org-done-keywords))
  24301. (eq org-icalendar-include-todo 'all))
  24302. (not (member org-archive-tag (org-get-tags-at)))
  24303. )
  24304. (setq hd (match-string 3)
  24305. summary (org-icalendar-cleanup-string
  24306. (org-entry-get nil "SUMMARY"))
  24307. desc (org-icalendar-cleanup-string
  24308. (or (org-entry-get nil "DESCRIPTION")
  24309. (and org-icalendar-include-body (org-get-entry)))
  24310. t org-icalendar-include-body)
  24311. location (org-icalendar-cleanup-string
  24312. (org-entry-get nil "LOCATION")))
  24313. (if (string-match org-bracket-link-regexp hd)
  24314. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24315. (match-string 1 hd))
  24316. t t hd)))
  24317. (if (string-match org-priority-regexp hd)
  24318. (setq pri (string-to-char (match-string 2 hd))
  24319. hd (concat (substring hd 0 (match-beginning 1))
  24320. (substring hd (match-end 1))))
  24321. (setq pri org-default-priority))
  24322. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24323. (- org-lowest-priority org-highest-priority))))))
  24324. (princ (format "BEGIN:VTODO
  24325. %s
  24326. SUMMARY:%s%s%s
  24327. CATEGORIES:%s
  24328. SEQUENCE:1
  24329. PRIORITY:%d
  24330. STATUS:%s
  24331. END:VTODO\n"
  24332. dts
  24333. (or summary hd)
  24334. (if (and location (string-match "\\S-" location))
  24335. (concat "\nLOCATION: " location) "")
  24336. (if (and desc (string-match "\\S-" desc))
  24337. (concat "\nDESCRIPTION: " desc) "")
  24338. category pri status)))))))))
  24339. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24340. "Take out stuff and quote what needs to be quoted.
  24341. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24342. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24343. characters."
  24344. (if (not s)
  24345. nil
  24346. (when is-body
  24347. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24348. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24349. (while (string-match re s) (setq s (replace-match "" t t s)))
  24350. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24351. (let ((start 0))
  24352. (while (string-match "\\([,;\\]\\)" s start)
  24353. (setq start (+ (match-beginning 0) 2)
  24354. s (replace-match "\\\\\\1" nil nil s))))
  24355. (when is-body
  24356. (while (string-match "[ \t]*\n[ \t]*" s)
  24357. (setq s (replace-match "\\n" t t s))))
  24358. (setq s (org-trim s))
  24359. (if is-body
  24360. (if maxlength
  24361. (if (and (numberp maxlength)
  24362. (> (length s) maxlength))
  24363. (setq s (substring s 0 maxlength)))))
  24364. s))
  24365. (defun org-get-entry ()
  24366. "Clean-up description string."
  24367. (save-excursion
  24368. (org-back-to-heading t)
  24369. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24370. (defun org-start-icalendar-file (name)
  24371. "Start an iCalendar file by inserting the header."
  24372. (let ((user user-full-name)
  24373. (name (or name "unknown"))
  24374. (timezone (cadr (current-time-zone))))
  24375. (princ
  24376. (format "BEGIN:VCALENDAR
  24377. VERSION:2.0
  24378. X-WR-CALNAME:%s
  24379. PRODID:-//%s//Emacs with Org-mode//EN
  24380. X-WR-TIMEZONE:%s
  24381. CALSCALE:GREGORIAN\n" name user timezone))))
  24382. (defun org-finish-icalendar-file ()
  24383. "Finish an iCalendar file by inserting the END statement."
  24384. (princ "END:VCALENDAR\n"))
  24385. (defun org-ical-ts-to-string (s keyword &optional inc)
  24386. "Take a time string S and convert it to iCalendar format.
  24387. KEYWORD is added in front, to make a complete line like DTSTART....
  24388. When INC is non-nil, increase the hour by two (if time string contains
  24389. a time), or the day by one (if it does not contain a time)."
  24390. (let ((t1 (org-parse-time-string s 'nodefault))
  24391. t2 fmt have-time time)
  24392. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24393. (setq t2 t1 have-time t)
  24394. (setq t2 (org-parse-time-string s)))
  24395. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24396. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24397. (when inc
  24398. (if have-time
  24399. (if org-agenda-default-appointment-duration
  24400. (setq mi (+ org-agenda-default-appointment-duration mi))
  24401. (setq h (+ 2 h)))
  24402. (setq d (1+ d))))
  24403. (setq time (encode-time s mi h d m y)))
  24404. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24405. (concat keyword (format-time-string fmt time))))
  24406. ;;; XOXO export
  24407. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24408. (with-current-buffer buffer
  24409. (apply 'insert output)))
  24410. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24411. (defun org-export-as-xoxo (&optional buffer)
  24412. "Export the org buffer as XOXO.
  24413. The XOXO buffer is named *xoxo-<source buffer name>*"
  24414. (interactive (list (current-buffer)))
  24415. ;; A quickie abstraction
  24416. ;; Output everything as XOXO
  24417. (with-current-buffer (get-buffer buffer)
  24418. (let* ((pos (point))
  24419. (opt-plist (org-combine-plists (org-default-export-plist)
  24420. (org-infile-export-plist)))
  24421. (filename (concat (file-name-as-directory
  24422. (org-export-directory :xoxo opt-plist))
  24423. (file-name-sans-extension
  24424. (file-name-nondirectory buffer-file-name))
  24425. ".html"))
  24426. (out (find-file-noselect filename))
  24427. (last-level 1)
  24428. (hanging-li nil))
  24429. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24430. ;; Check the output buffer is empty.
  24431. (with-current-buffer out (erase-buffer))
  24432. ;; Kick off the output
  24433. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24434. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24435. (let* ((hd (match-string-no-properties 1))
  24436. (level (length hd))
  24437. (text (concat
  24438. (match-string-no-properties 2)
  24439. (save-excursion
  24440. (goto-char (match-end 0))
  24441. (let ((str ""))
  24442. (catch 'loop
  24443. (while 't
  24444. (forward-line)
  24445. (if (looking-at "^[ \t]\\(.*\\)")
  24446. (setq str (concat str (match-string-no-properties 1)))
  24447. (throw 'loop str)))))))))
  24448. ;; Handle level rendering
  24449. (cond
  24450. ((> level last-level)
  24451. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24452. ((< level last-level)
  24453. (dotimes (- (- last-level level) 1)
  24454. (if hanging-li
  24455. (org-export-as-xoxo-insert-into out "</li>\n"))
  24456. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24457. (when hanging-li
  24458. (org-export-as-xoxo-insert-into out "</li>\n")
  24459. (setq hanging-li nil)))
  24460. ((equal level last-level)
  24461. (if hanging-li
  24462. (org-export-as-xoxo-insert-into out "</li>\n")))
  24463. )
  24464. (setq last-level level)
  24465. ;; And output the new li
  24466. (setq hanging-li 't)
  24467. (if (equal ?+ (elt text 0))
  24468. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24469. (org-export-as-xoxo-insert-into out "<li>" text))))
  24470. ;; Finally finish off the ol
  24471. (dotimes (- last-level 1)
  24472. (if hanging-li
  24473. (org-export-as-xoxo-insert-into out "</li>\n"))
  24474. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24475. (goto-char pos)
  24476. ;; Finish the buffer off and clean it up.
  24477. (switch-to-buffer-other-window out)
  24478. (indent-region (point-min) (point-max) nil)
  24479. (save-buffer)
  24480. (goto-char (point-min))
  24481. )))
  24482. ;;;; Key bindings
  24483. ;; Make `C-c C-x' a prefix key
  24484. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24485. ;; TAB key with modifiers
  24486. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24487. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24488. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24489. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24490. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24491. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24492. ;; The following line is necessary under Suse GNU/Linux
  24493. (unless (featurep 'xemacs)
  24494. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24495. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24496. (define-key org-mode-map [backtab] 'org-shifttab)
  24497. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24498. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24499. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24500. ;; Cursor keys with modifiers
  24501. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24502. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24503. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24504. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24505. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24506. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24507. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24508. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24509. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24510. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24511. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24512. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24513. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24514. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24515. ;;; Extra keys for tty access.
  24516. ;; We only set them when really needed because otherwise the
  24517. ;; menus don't show the simple keys
  24518. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24519. (not window-system))
  24520. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24521. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24522. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24523. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24524. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24525. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24526. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24527. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24528. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24529. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24530. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24531. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24532. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24533. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24534. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24535. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24536. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24537. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24538. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24539. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24540. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24541. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24542. ;; All the other keys
  24543. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24544. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24545. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24546. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24547. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24548. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24549. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24550. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24551. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24552. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24553. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24554. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24555. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24556. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24557. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24558. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24559. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24560. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24561. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24562. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24563. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24564. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24565. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24566. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24567. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24568. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24569. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24570. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24571. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24572. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24573. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24574. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24575. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24576. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24577. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24578. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24579. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24580. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24581. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24582. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24583. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24584. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24585. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24586. (org-defkey org-mode-map "\C-m" 'org-return)
  24587. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24588. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24589. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24590. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24591. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24592. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24593. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24594. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24595. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24596. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24597. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24598. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24599. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24600. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24601. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24602. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24603. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24604. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24605. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24606. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24607. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24608. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24609. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24610. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24611. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24612. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24613. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24614. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24615. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24616. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24617. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24618. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24619. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24620. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24621. (when (featurep 'xemacs)
  24622. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24623. (defsubst org-table-p () (org-at-table-p))
  24624. (defun org-self-insert-command (N)
  24625. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24626. If the cursor is in a table looking at whitespace, the whitespace is
  24627. overwritten, and the table is not marked as requiring realignment."
  24628. (interactive "p")
  24629. (if (and (org-table-p)
  24630. (progn
  24631. ;; check if we blank the field, and if that triggers align
  24632. (and org-table-auto-blank-field
  24633. (member last-command
  24634. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24635. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24636. ;; got extra space, this field does not determine column width
  24637. (let (org-table-may-need-update) (org-table-blank-field))
  24638. ;; no extra space, this field may determine column width
  24639. (org-table-blank-field)))
  24640. t)
  24641. (eq N 1)
  24642. (looking-at "[^|\n]* |"))
  24643. (let (org-table-may-need-update)
  24644. (goto-char (1- (match-end 0)))
  24645. (delete-backward-char 1)
  24646. (goto-char (match-beginning 0))
  24647. (self-insert-command N))
  24648. (setq org-table-may-need-update t)
  24649. (self-insert-command N)
  24650. (org-fix-tags-on-the-fly)))
  24651. (defun org-fix-tags-on-the-fly ()
  24652. (when (and (equal (char-after (point-at-bol)) ?*)
  24653. (org-on-heading-p))
  24654. (org-align-tags-here org-tags-column)))
  24655. (defun org-delete-backward-char (N)
  24656. "Like `delete-backward-char', insert whitespace at field end in tables.
  24657. When deleting backwards, in tables this function will insert whitespace in
  24658. front of the next \"|\" separator, to keep the table aligned. The table will
  24659. still be marked for re-alignment if the field did fill the entire column,
  24660. because, in this case the deletion might narrow the column."
  24661. (interactive "p")
  24662. (if (and (org-table-p)
  24663. (eq N 1)
  24664. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24665. (looking-at ".*?|"))
  24666. (let ((pos (point))
  24667. (noalign (looking-at "[^|\n\r]* |"))
  24668. (c org-table-may-need-update))
  24669. (backward-delete-char N)
  24670. (skip-chars-forward "^|")
  24671. (insert " ")
  24672. (goto-char (1- pos))
  24673. ;; noalign: if there were two spaces at the end, this field
  24674. ;; does not determine the width of the column.
  24675. (if noalign (setq org-table-may-need-update c)))
  24676. (backward-delete-char N)
  24677. (org-fix-tags-on-the-fly)))
  24678. (defun org-delete-char (N)
  24679. "Like `delete-char', but insert whitespace at field end in tables.
  24680. When deleting characters, in tables this function will insert whitespace in
  24681. front of the next \"|\" separator, to keep the table aligned. The table will
  24682. still be marked for re-alignment if the field did fill the entire column,
  24683. because, in this case the deletion might narrow the column."
  24684. (interactive "p")
  24685. (if (and (org-table-p)
  24686. (not (bolp))
  24687. (not (= (char-after) ?|))
  24688. (eq N 1))
  24689. (if (looking-at ".*?|")
  24690. (let ((pos (point))
  24691. (noalign (looking-at "[^|\n\r]* |"))
  24692. (c org-table-may-need-update))
  24693. (replace-match (concat
  24694. (substring (match-string 0) 1 -1)
  24695. " |"))
  24696. (goto-char pos)
  24697. ;; noalign: if there were two spaces at the end, this field
  24698. ;; does not determine the width of the column.
  24699. (if noalign (setq org-table-may-need-update c)))
  24700. (delete-char N))
  24701. (delete-char N)
  24702. (org-fix-tags-on-the-fly)))
  24703. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24704. (put 'org-self-insert-command 'delete-selection t)
  24705. (put 'orgtbl-self-insert-command 'delete-selection t)
  24706. (put 'org-delete-char 'delete-selection 'supersede)
  24707. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24708. ;; Make `flyspell-mode' delay after some commands
  24709. (put 'org-self-insert-command 'flyspell-delayed t)
  24710. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24711. (put 'org-delete-char 'flyspell-delayed t)
  24712. (put 'org-delete-backward-char 'flyspell-delayed t)
  24713. ;; Make pabbrev-mode expand after org-mode commands
  24714. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24715. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24716. ;; How to do this: Measure non-white length of current string
  24717. ;; If equal to column width, we should realign.
  24718. (defun org-remap (map &rest commands)
  24719. "In MAP, remap the functions given in COMMANDS.
  24720. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24721. (let (new old)
  24722. (while commands
  24723. (setq old (pop commands) new (pop commands))
  24724. (if (fboundp 'command-remapping)
  24725. (org-defkey map (vector 'remap old) new)
  24726. (substitute-key-definition old new map global-map)))))
  24727. (when (eq org-enable-table-editor 'optimized)
  24728. ;; If the user wants maximum table support, we need to hijack
  24729. ;; some standard editing functions
  24730. (org-remap org-mode-map
  24731. 'self-insert-command 'org-self-insert-command
  24732. 'delete-char 'org-delete-char
  24733. 'delete-backward-char 'org-delete-backward-char)
  24734. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24735. (defun org-shiftcursor-error ()
  24736. "Throw an error because Shift-Cursor command was applied in wrong context."
  24737. (error "This command is active in special context like tables, headlines or timestamps"))
  24738. (defun org-shifttab (&optional arg)
  24739. "Global visibility cycling or move to previous table field.
  24740. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24741. on context.
  24742. See the individual commands for more information."
  24743. (interactive "P")
  24744. (cond
  24745. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24746. (arg (message "Content view to level: ")
  24747. (org-content (prefix-numeric-value arg))
  24748. (setq org-cycle-global-status 'overview))
  24749. (t (call-interactively 'org-global-cycle))))
  24750. (defun org-shiftmetaleft ()
  24751. "Promote subtree or delete table column.
  24752. Calls `org-promote-subtree', `org-outdent-item',
  24753. or `org-table-delete-column', depending on context.
  24754. See the individual commands for more information."
  24755. (interactive)
  24756. (cond
  24757. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24758. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24759. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24760. (t (org-shiftcursor-error))))
  24761. (defun org-shiftmetaright ()
  24762. "Demote subtree or insert table column.
  24763. Calls `org-demote-subtree', `org-indent-item',
  24764. or `org-table-insert-column', depending on context.
  24765. See the individual commands for more information."
  24766. (interactive)
  24767. (cond
  24768. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24769. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24770. ((org-at-item-p) (call-interactively 'org-indent-item))
  24771. (t (org-shiftcursor-error))))
  24772. (defun org-shiftmetaup (&optional arg)
  24773. "Move subtree up or kill table row.
  24774. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24775. `org-move-item-up' depending on context. See the individual commands
  24776. for more information."
  24777. (interactive "P")
  24778. (cond
  24779. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24780. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24781. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24782. (t (org-shiftcursor-error))))
  24783. (defun org-shiftmetadown (&optional arg)
  24784. "Move subtree down or insert table row.
  24785. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24786. `org-move-item-down', depending on context. See the individual
  24787. commands for more information."
  24788. (interactive "P")
  24789. (cond
  24790. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24791. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24792. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24793. (t (org-shiftcursor-error))))
  24794. (defun org-metaleft (&optional arg)
  24795. "Promote heading or move table column to left.
  24796. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24797. With no specific context, calls the Emacs default `backward-word'.
  24798. See the individual commands for more information."
  24799. (interactive "P")
  24800. (cond
  24801. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24802. ((or (org-on-heading-p) (org-region-active-p))
  24803. (call-interactively 'org-do-promote))
  24804. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24805. (t (call-interactively 'backward-word))))
  24806. (defun org-metaright (&optional arg)
  24807. "Demote subtree or move table column to right.
  24808. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24809. With no specific context, calls the Emacs default `forward-word'.
  24810. See the individual commands for more information."
  24811. (interactive "P")
  24812. (cond
  24813. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24814. ((or (org-on-heading-p) (org-region-active-p))
  24815. (call-interactively 'org-do-demote))
  24816. ((org-at-item-p) (call-interactively 'org-indent-item))
  24817. (t (call-interactively 'forward-word))))
  24818. (defun org-metaup (&optional arg)
  24819. "Move subtree up or move table row up.
  24820. Calls `org-move-subtree-up' or `org-table-move-row' or
  24821. `org-move-item-up', depending on context. See the individual commands
  24822. for more information."
  24823. (interactive "P")
  24824. (cond
  24825. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24826. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24827. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24828. (t (transpose-lines 1) (beginning-of-line -1))))
  24829. (defun org-metadown (&optional arg)
  24830. "Move subtree down or move table row down.
  24831. Calls `org-move-subtree-down' or `org-table-move-row' or
  24832. `org-move-item-down', depending on context. See the individual
  24833. commands for more information."
  24834. (interactive "P")
  24835. (cond
  24836. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24837. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24838. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24839. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24840. (defun org-shiftup (&optional arg)
  24841. "Increase item in timestamp or increase priority of current headline.
  24842. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24843. depending on context. See the individual commands for more information."
  24844. (interactive "P")
  24845. (cond
  24846. ((org-at-timestamp-p t)
  24847. (call-interactively (if org-edit-timestamp-down-means-later
  24848. 'org-timestamp-down 'org-timestamp-up)))
  24849. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24850. ((org-at-item-p) (call-interactively 'org-previous-item))
  24851. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24852. (defun org-shiftdown (&optional arg)
  24853. "Decrease item in timestamp or decrease priority of current headline.
  24854. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24855. depending on context. See the individual commands for more information."
  24856. (interactive "P")
  24857. (cond
  24858. ((org-at-timestamp-p t)
  24859. (call-interactively (if org-edit-timestamp-down-means-later
  24860. 'org-timestamp-up 'org-timestamp-down)))
  24861. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24862. (t (call-interactively 'org-next-item))))
  24863. (defun org-shiftright ()
  24864. "Next TODO keyword or timestamp one day later, depending on context."
  24865. (interactive)
  24866. (cond
  24867. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24868. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24869. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24870. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24871. (t (org-shiftcursor-error))))
  24872. (defun org-shiftleft ()
  24873. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24874. (interactive)
  24875. (cond
  24876. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24877. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24878. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24879. ((org-at-property-p)
  24880. (call-interactively 'org-property-previous-allowed-value))
  24881. (t (org-shiftcursor-error))))
  24882. (defun org-shiftcontrolright ()
  24883. "Switch to next TODO set."
  24884. (interactive)
  24885. (cond
  24886. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24887. (t (org-shiftcursor-error))))
  24888. (defun org-shiftcontrolleft ()
  24889. "Switch to previous TODO set."
  24890. (interactive)
  24891. (cond
  24892. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24893. (t (org-shiftcursor-error))))
  24894. (defun org-ctrl-c-ret ()
  24895. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24896. (interactive)
  24897. (cond
  24898. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24899. (t (call-interactively 'org-insert-heading))))
  24900. (defun org-copy-special ()
  24901. "Copy region in table or copy current subtree.
  24902. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24903. See the individual commands for more information."
  24904. (interactive)
  24905. (call-interactively
  24906. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24907. (defun org-cut-special ()
  24908. "Cut region in table or cut current subtree.
  24909. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24910. See the individual commands for more information."
  24911. (interactive)
  24912. (call-interactively
  24913. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24914. (defun org-paste-special (arg)
  24915. "Paste rectangular region into table, or past subtree relative to level.
  24916. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24917. See the individual commands for more information."
  24918. (interactive "P")
  24919. (if (org-at-table-p)
  24920. (org-table-paste-rectangle)
  24921. (org-paste-subtree arg)))
  24922. (defun org-ctrl-c-ctrl-c (&optional arg)
  24923. "Set tags in headline, or update according to changed information at point.
  24924. This command does many different things, depending on context:
  24925. - If the cursor is in a headline, prompt for tags and insert them
  24926. into the current line, aligned to `org-tags-column'. When called
  24927. with prefix arg, realign all tags in the current buffer.
  24928. - If the cursor is in one of the special #+KEYWORD lines, this
  24929. triggers scanning the buffer for these lines and updating the
  24930. information.
  24931. - If the cursor is inside a table, realign the table. This command
  24932. works even if the automatic table editor has been turned off.
  24933. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24934. the entire table.
  24935. - If the cursor is a the beginning of a dynamic block, update it.
  24936. - If the cursor is inside a table created by the table.el package,
  24937. activate that table.
  24938. - If the current buffer is a remember buffer, close note and file it.
  24939. with a prefix argument, file it without further interaction to the default
  24940. location.
  24941. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24942. links in this buffer.
  24943. - If the cursor is on a numbered item in a plain list, renumber the
  24944. ordered list.
  24945. - If the cursor is on a checkbox, toggle it."
  24946. (interactive "P")
  24947. (let ((org-enable-table-editor t))
  24948. (cond
  24949. ((or org-clock-overlays
  24950. org-occur-highlights
  24951. org-latex-fragment-image-overlays)
  24952. (org-remove-clock-overlays)
  24953. (org-remove-occur-highlights)
  24954. (org-remove-latex-fragment-image-overlays)
  24955. (message "Temporary highlights/overlays removed from current buffer"))
  24956. ((and (local-variable-p 'org-finish-function (current-buffer))
  24957. (fboundp org-finish-function))
  24958. (funcall org-finish-function))
  24959. ((org-at-property-p)
  24960. (call-interactively 'org-property-action))
  24961. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24962. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24963. ((org-at-table.el-p)
  24964. (require 'table)
  24965. (beginning-of-line 1)
  24966. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24967. (call-interactively 'table-recognize-table))
  24968. ((org-at-table-p)
  24969. (org-table-maybe-eval-formula)
  24970. (if arg
  24971. (call-interactively 'org-table-recalculate)
  24972. (org-table-maybe-recalculate-line))
  24973. (call-interactively 'org-table-align))
  24974. ((org-at-item-checkbox-p)
  24975. (call-interactively 'org-toggle-checkbox))
  24976. ((org-at-item-p)
  24977. (call-interactively 'org-maybe-renumber-ordered-list))
  24978. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24979. ;; Dynamic block
  24980. (beginning-of-line 1)
  24981. (org-update-dblock))
  24982. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24983. (cond
  24984. ((equal (match-string 1) "TBLFM")
  24985. ;; Recalculate the table before this line
  24986. (save-excursion
  24987. (beginning-of-line 1)
  24988. (skip-chars-backward " \r\n\t")
  24989. (if (org-at-table-p)
  24990. (org-call-with-arg 'org-table-recalculate t))))
  24991. (t
  24992. (call-interactively 'org-mode-restart))))
  24993. (t (error "C-c C-c can do nothing useful at this location.")))))
  24994. (defun org-mode-restart ()
  24995. "Restart Org-mode, to scan again for special lines.
  24996. Also updates the keyword regular expressions."
  24997. (interactive)
  24998. (let ((org-inhibit-startup t)) (org-mode))
  24999. (message "Org-mode restarted to refresh keyword and special line setup"))
  25000. (defun org-kill-note-or-show-branches ()
  25001. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25002. (interactive)
  25003. (if (not org-finish-function)
  25004. (call-interactively 'show-branches)
  25005. (let ((org-note-abort t))
  25006. (funcall org-finish-function))))
  25007. (defun org-return (&optional indent)
  25008. "Goto next table row or insert a newline.
  25009. Calls `org-table-next-row' or `newline', depending on context.
  25010. See the individual commands for more information."
  25011. (interactive)
  25012. (cond
  25013. ((bobp) (if indent (newline-and-indent) (newline)))
  25014. ((and (org-at-heading-p)
  25015. (looking-at
  25016. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25017. (org-show-entry)
  25018. (end-of-line 1)
  25019. (newline))
  25020. ((org-at-table-p)
  25021. (org-table-justify-field-maybe)
  25022. (call-interactively 'org-table-next-row))
  25023. (t (if indent (newline-and-indent) (newline)))))
  25024. (defun org-return-indent ()
  25025. (interactive)
  25026. "Goto next table row or insert a newline and indent.
  25027. Calls `org-table-next-row' or `newline-and-indent', depending on
  25028. context. See the individual commands for more information."
  25029. (org-return t))
  25030. (defun org-ctrl-c-minus ()
  25031. "Insert separator line in table or modify bullet type in list.
  25032. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25033. depending on context."
  25034. (interactive)
  25035. (cond
  25036. ((org-at-table-p)
  25037. (call-interactively 'org-table-insert-hline))
  25038. ((org-on-heading-p)
  25039. ;; Convert to item
  25040. (save-excursion
  25041. (beginning-of-line 1)
  25042. (if (looking-at "\\*+ ")
  25043. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25044. ((org-in-item-p)
  25045. (call-interactively 'org-cycle-list-bullet))
  25046. (t (error "`C-c -' does have no function here."))))
  25047. (defun org-meta-return (&optional arg)
  25048. "Insert a new heading or wrap a region in a table.
  25049. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25050. See the individual commands for more information."
  25051. (interactive "P")
  25052. (cond
  25053. ((org-at-table-p)
  25054. (call-interactively 'org-table-wrap-region))
  25055. (t (call-interactively 'org-insert-heading))))
  25056. ;;; Menu entries
  25057. ;; Define the Org-mode menus
  25058. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25059. '("Tbl"
  25060. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25061. ["Next Field" org-cycle (org-at-table-p)]
  25062. ["Previous Field" org-shifttab (org-at-table-p)]
  25063. ["Next Row" org-return (org-at-table-p)]
  25064. "--"
  25065. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25066. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25067. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25068. "--"
  25069. ("Column"
  25070. ["Move Column Left" org-metaleft (org-at-table-p)]
  25071. ["Move Column Right" org-metaright (org-at-table-p)]
  25072. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25073. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25074. ("Row"
  25075. ["Move Row Up" org-metaup (org-at-table-p)]
  25076. ["Move Row Down" org-metadown (org-at-table-p)]
  25077. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25078. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25079. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25080. "--"
  25081. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25082. ("Rectangle"
  25083. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25084. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25085. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25086. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25087. "--"
  25088. ("Calculate"
  25089. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25090. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25091. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25092. "--"
  25093. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25094. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25095. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25096. "--"
  25097. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25098. "--"
  25099. ["Sum Column/Rectangle" org-table-sum
  25100. (or (org-at-table-p) (org-region-active-p))]
  25101. ["Which Column?" org-table-current-column (org-at-table-p)])
  25102. ["Debug Formulas"
  25103. org-table-toggle-formula-debugger
  25104. :style toggle :selected org-table-formula-debug]
  25105. ["Show Col/Row Numbers"
  25106. org-table-toggle-coordinate-overlays
  25107. :style toggle :selected org-table-overlay-coordinates]
  25108. "--"
  25109. ["Create" org-table-create (and (not (org-at-table-p))
  25110. org-enable-table-editor)]
  25111. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25112. ["Import from File" org-table-import (not (org-at-table-p))]
  25113. ["Export to File" org-table-export (org-at-table-p)]
  25114. "--"
  25115. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25116. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25117. '("Org"
  25118. ("Show/Hide"
  25119. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25120. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25121. ["Sparse Tree" org-occur t]
  25122. ["Reveal Context" org-reveal t]
  25123. ["Show All" show-all t]
  25124. "--"
  25125. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25126. "--"
  25127. ["New Heading" org-insert-heading t]
  25128. ("Navigate Headings"
  25129. ["Up" outline-up-heading t]
  25130. ["Next" outline-next-visible-heading t]
  25131. ["Previous" outline-previous-visible-heading t]
  25132. ["Next Same Level" outline-forward-same-level t]
  25133. ["Previous Same Level" outline-backward-same-level t]
  25134. "--"
  25135. ["Jump" org-goto t])
  25136. ("Edit Structure"
  25137. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25138. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25139. "--"
  25140. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25141. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25142. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25143. "--"
  25144. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25145. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25146. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25147. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25148. "--"
  25149. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25150. "--"
  25151. ["Convert to odd levels" org-convert-to-odd-levels t]
  25152. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25153. ("Editing"
  25154. ["Emphasis..." org-emphasize t])
  25155. ("Archive"
  25156. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25157. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25158. ; :active t :keys "C-u C-c C-x C-a"]
  25159. ["Sparse trees open ARCHIVE trees"
  25160. (setq org-sparse-tree-open-archived-trees
  25161. (not org-sparse-tree-open-archived-trees))
  25162. :style toggle :selected org-sparse-tree-open-archived-trees]
  25163. ["Cycling opens ARCHIVE trees"
  25164. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25165. :style toggle :selected org-cycle-open-archived-trees]
  25166. ["Agenda includes ARCHIVE trees"
  25167. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25168. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25169. "--"
  25170. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25171. ; ["Check and Move Children" (org-archive-subtree '(4))
  25172. ; :active t :keys "C-u C-c C-x C-s"]
  25173. )
  25174. "--"
  25175. ("TODO Lists"
  25176. ["TODO/DONE/-" org-todo t]
  25177. ("Select keyword"
  25178. ["Next keyword" org-shiftright (org-on-heading-p)]
  25179. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25180. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25181. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25182. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25183. ["Show TODO Tree" org-show-todo-tree t]
  25184. ["Global TODO list" org-todo-list t]
  25185. "--"
  25186. ["Set Priority" org-priority t]
  25187. ["Priority Up" org-shiftup t]
  25188. ["Priority Down" org-shiftdown t])
  25189. ("TAGS and Properties"
  25190. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25191. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25192. "--"
  25193. ["Set property" 'org-set-property t]
  25194. ["Column view of properties" org-columns t]
  25195. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25196. ("Dates and Scheduling"
  25197. ["Timestamp" org-time-stamp t]
  25198. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25199. ("Change Date"
  25200. ["1 Day Later" org-shiftright t]
  25201. ["1 Day Earlier" org-shiftleft t]
  25202. ["1 ... Later" org-shiftup t]
  25203. ["1 ... Earlier" org-shiftdown t])
  25204. ["Compute Time Range" org-evaluate-time-range t]
  25205. ["Schedule Item" org-schedule t]
  25206. ["Deadline" org-deadline t]
  25207. "--"
  25208. ["Custom time format" org-toggle-time-stamp-overlays
  25209. :style radio :selected org-display-custom-times]
  25210. "--"
  25211. ["Goto Calendar" org-goto-calendar t]
  25212. ["Date from Calendar" org-date-from-calendar t])
  25213. ("Logging work"
  25214. ["Clock in" org-clock-in t]
  25215. ["Clock out" org-clock-out t]
  25216. ["Clock cancel" org-clock-cancel t]
  25217. ["Goto running clock" org-clock-goto t]
  25218. ["Display times" org-clock-display t]
  25219. ["Create clock table" org-clock-report t]
  25220. "--"
  25221. ["Record DONE time"
  25222. (progn (setq org-log-done (not org-log-done))
  25223. (message "Switching to %s will %s record a timestamp"
  25224. (car org-done-keywords)
  25225. (if org-log-done "automatically" "not")))
  25226. :style toggle :selected org-log-done])
  25227. "--"
  25228. ["Agenda Command..." org-agenda t]
  25229. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25230. ("File List for Agenda")
  25231. ("Special views current file"
  25232. ["TODO Tree" org-show-todo-tree t]
  25233. ["Check Deadlines" org-check-deadlines t]
  25234. ["Timeline" org-timeline t]
  25235. ["Tags Tree" org-tags-sparse-tree t])
  25236. "--"
  25237. ("Hyperlinks"
  25238. ["Store Link (Global)" org-store-link t]
  25239. ["Insert Link" org-insert-link t]
  25240. ["Follow Link" org-open-at-point t]
  25241. "--"
  25242. ["Next link" org-next-link t]
  25243. ["Previous link" org-previous-link t]
  25244. "--"
  25245. ["Descriptive Links"
  25246. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25247. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25248. ["Literal Links"
  25249. (progn
  25250. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25251. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25252. "--"
  25253. ["Export/Publish..." org-export t]
  25254. ("LaTeX"
  25255. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25256. :selected org-cdlatex-mode]
  25257. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25258. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25259. ["Modify math symbol" org-cdlatex-math-modify
  25260. (org-inside-LaTeX-fragment-p)]
  25261. ["Export LaTeX fragments as images"
  25262. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25263. :style toggle :selected org-export-with-LaTeX-fragments])
  25264. "--"
  25265. ("Documentation"
  25266. ["Show Version" org-version t]
  25267. ["Info Documentation" org-info t])
  25268. ("Customize"
  25269. ["Browse Org Group" org-customize t]
  25270. "--"
  25271. ["Expand This Menu" org-create-customize-menu
  25272. (fboundp 'customize-menu-create)])
  25273. "--"
  25274. ["Refresh setup" org-mode-restart t]
  25275. ))
  25276. (defun org-info (&optional node)
  25277. "Read documentation for Org-mode in the info system.
  25278. With optional NODE, go directly to that node."
  25279. (interactive)
  25280. (require 'info)
  25281. (Info-goto-node (format "(org)%s" (or node ""))))
  25282. (defun org-install-agenda-files-menu ()
  25283. (let ((bl (buffer-list)))
  25284. (save-excursion
  25285. (while bl
  25286. (set-buffer (pop bl))
  25287. (if (org-mode-p) (setq bl nil)))
  25288. (when (org-mode-p)
  25289. (easy-menu-change
  25290. '("Org") "File List for Agenda"
  25291. (append
  25292. (list
  25293. ["Edit File List" (org-edit-agenda-file-list) t]
  25294. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25295. ["Remove Current File from List" org-remove-file t]
  25296. ["Cycle through agenda files" org-cycle-agenda-files t]
  25297. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25298. "--")
  25299. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25300. ;;;; Documentation
  25301. (defun org-customize ()
  25302. "Call the customize function with org as argument."
  25303. (interactive)
  25304. (customize-browse 'org))
  25305. (defun org-create-customize-menu ()
  25306. "Create a full customization menu for Org-mode, insert it into the menu."
  25307. (interactive)
  25308. (if (fboundp 'customize-menu-create)
  25309. (progn
  25310. (easy-menu-change
  25311. '("Org") "Customize"
  25312. `(["Browse Org group" org-customize t]
  25313. "--"
  25314. ,(customize-menu-create 'org)
  25315. ["Set" Custom-set t]
  25316. ["Save" Custom-save t]
  25317. ["Reset to Current" Custom-reset-current t]
  25318. ["Reset to Saved" Custom-reset-saved t]
  25319. ["Reset to Standard Settings" Custom-reset-standard t]))
  25320. (message "\"Org\"-menu now contains full customization menu"))
  25321. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25322. ;;;; Miscellaneous stuff
  25323. ;;; Generally useful functions
  25324. (defun org-context ()
  25325. "Return a list of contexts of the current cursor position.
  25326. If several contexts apply, all are returned.
  25327. Each context entry is a list with a symbol naming the context, and
  25328. two positions indicating start and end of the context. Possible
  25329. contexts are:
  25330. :headline anywhere in a headline
  25331. :headline-stars on the leading stars in a headline
  25332. :todo-keyword on a TODO keyword (including DONE) in a headline
  25333. :tags on the TAGS in a headline
  25334. :priority on the priority cookie in a headline
  25335. :item on the first line of a plain list item
  25336. :item-bullet on the bullet/number of a plain list item
  25337. :checkbox on the checkbox in a plain list item
  25338. :table in an org-mode table
  25339. :table-special on a special filed in a table
  25340. :table-table in a table.el table
  25341. :link on a hyperlink
  25342. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25343. :target on a <<target>>
  25344. :radio-target on a <<<radio-target>>>
  25345. :latex-fragment on a LaTeX fragment
  25346. :latex-preview on a LaTeX fragment with overlayed preview image
  25347. This function expects the position to be visible because it uses font-lock
  25348. faces as a help to recognize the following contexts: :table-special, :link,
  25349. and :keyword."
  25350. (let* ((f (get-text-property (point) 'face))
  25351. (faces (if (listp f) f (list f)))
  25352. (p (point)) clist o)
  25353. ;; First the large context
  25354. (cond
  25355. ((org-on-heading-p t)
  25356. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25357. (when (progn
  25358. (beginning-of-line 1)
  25359. (looking-at org-todo-line-tags-regexp))
  25360. (push (org-point-in-group p 1 :headline-stars) clist)
  25361. (push (org-point-in-group p 2 :todo-keyword) clist)
  25362. (push (org-point-in-group p 4 :tags) clist))
  25363. (goto-char p)
  25364. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25365. (if (looking-at "\\[#[A-Z0-9]\\]")
  25366. (push (org-point-in-group p 0 :priority) clist)))
  25367. ((org-at-item-p)
  25368. (push (org-point-in-group p 2 :item-bullet) clist)
  25369. (push (list :item (point-at-bol)
  25370. (save-excursion (org-end-of-item) (point)))
  25371. clist)
  25372. (and (org-at-item-checkbox-p)
  25373. (push (org-point-in-group p 0 :checkbox) clist)))
  25374. ((org-at-table-p)
  25375. (push (list :table (org-table-begin) (org-table-end)) clist)
  25376. (if (memq 'org-formula faces)
  25377. (push (list :table-special
  25378. (previous-single-property-change p 'face)
  25379. (next-single-property-change p 'face)) clist)))
  25380. ((org-at-table-p 'any)
  25381. (push (list :table-table) clist)))
  25382. (goto-char p)
  25383. ;; Now the small context
  25384. (cond
  25385. ((org-at-timestamp-p)
  25386. (push (org-point-in-group p 0 :timestamp) clist))
  25387. ((memq 'org-link faces)
  25388. (push (list :link
  25389. (previous-single-property-change p 'face)
  25390. (next-single-property-change p 'face)) clist))
  25391. ((memq 'org-special-keyword faces)
  25392. (push (list :keyword
  25393. (previous-single-property-change p 'face)
  25394. (next-single-property-change p 'face)) clist))
  25395. ((org-on-target-p)
  25396. (push (org-point-in-group p 0 :target) clist)
  25397. (goto-char (1- (match-beginning 0)))
  25398. (if (looking-at org-radio-target-regexp)
  25399. (push (org-point-in-group p 0 :radio-target) clist))
  25400. (goto-char p))
  25401. ((setq o (car (delq nil
  25402. (mapcar
  25403. (lambda (x)
  25404. (if (memq x org-latex-fragment-image-overlays) x))
  25405. (org-overlays-at (point))))))
  25406. (push (list :latex-fragment
  25407. (org-overlay-start o) (org-overlay-end o)) clist)
  25408. (push (list :latex-preview
  25409. (org-overlay-start o) (org-overlay-end o)) clist))
  25410. ((org-inside-LaTeX-fragment-p)
  25411. ;; FIXME: positions wrong.
  25412. (push (list :latex-fragment (point) (point)) clist)))
  25413. (setq clist (nreverse (delq nil clist)))
  25414. clist))
  25415. ;; FIXME: Compare with at-regexp-p Do we need both?
  25416. (defun org-in-regexp (re &optional nlines visually)
  25417. "Check if point is inside a match of regexp.
  25418. Normally only the current line is checked, but you can include NLINES extra
  25419. lines both before and after point into the search.
  25420. If VISUALLY is set, require that the cursor is not after the match but
  25421. really on, so that the block visually is on the match."
  25422. (catch 'exit
  25423. (let ((pos (point))
  25424. (eol (point-at-eol (+ 1 (or nlines 0))))
  25425. (inc (if visually 1 0)))
  25426. (save-excursion
  25427. (beginning-of-line (- 1 (or nlines 0)))
  25428. (while (re-search-forward re eol t)
  25429. (if (and (<= (match-beginning 0) pos)
  25430. (>= (+ inc (match-end 0)) pos))
  25431. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25432. (defun org-at-regexp-p (regexp)
  25433. "Is point inside a match of REGEXP in the current line?"
  25434. (catch 'exit
  25435. (save-excursion
  25436. (let ((pos (point)) (end (point-at-eol)))
  25437. (beginning-of-line 1)
  25438. (while (re-search-forward regexp end t)
  25439. (if (and (<= (match-beginning 0) pos)
  25440. (>= (match-end 0) pos))
  25441. (throw 'exit t)))
  25442. nil))))
  25443. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25444. "Call `multi-occur' with buffers for all agenda files."
  25445. (interactive "sOrg-files matching: \np")
  25446. (let* ((files (org-agenda-files))
  25447. (tnames (mapcar 'file-truename files))
  25448. (extra org-agenda-multi-occur-extra-files)
  25449. f)
  25450. (while (setq f (pop extra))
  25451. (unless (member (file-truename f) tnames)
  25452. (add-to-list 'files f 'append)
  25453. (add-to-list 'tnames (file-truename f) 'append)))
  25454. (multi-occur
  25455. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25456. regexp)))
  25457. (if (boundp 'occur-mode-find-occurrence-hook)
  25458. ;; Emacs 23
  25459. (add-hook 'occur-mode-find-occurrence-hook
  25460. (lambda ()
  25461. (when (org-mode-p)
  25462. (org-reveal))))
  25463. ;; Emacs 22
  25464. (defadvice occur-mode-goto-occurrence
  25465. (after org-occur-reveal activate)
  25466. (and (org-mode-p) (org-reveal)))
  25467. (defadvice occur-mode-goto-occurrence-other-window
  25468. (after org-occur-reveal activate)
  25469. (and (org-mode-p) (org-reveal)))
  25470. (defadvice occur-mode-display-occurrence
  25471. (after org-occur-reveal activate)
  25472. (when (org-mode-p)
  25473. (let ((pos (occur-mode-find-occurrence)))
  25474. (with-current-buffer (marker-buffer pos)
  25475. (save-excursion
  25476. (goto-char pos)
  25477. (org-reveal)))))))
  25478. (defun org-uniquify (list)
  25479. "Remove duplicate elements from LIST."
  25480. (let (res)
  25481. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25482. res))
  25483. (defun org-delete-all (elts list)
  25484. "Remove all elements in ELTS from LIST."
  25485. (while elts
  25486. (setq list (delete (pop elts) list)))
  25487. list)
  25488. (defun org-back-over-empty-lines ()
  25489. "Move backwards over witespace, to the beginning of the first empty line.
  25490. Returns the number o empty lines passed."
  25491. (let ((pos (point)))
  25492. (skip-chars-backward " \t\n\r")
  25493. (beginning-of-line 2)
  25494. (goto-char (min (point) pos))
  25495. (count-lines (point) pos)))
  25496. (defun org-skip-whitespace ()
  25497. (skip-chars-forward " \t\n\r"))
  25498. (defun org-point-in-group (point group &optional context)
  25499. "Check if POINT is in match-group GROUP.
  25500. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25501. match. If the match group does ot exist or point is not inside it,
  25502. return nil."
  25503. (and (match-beginning group)
  25504. (>= point (match-beginning group))
  25505. (<= point (match-end group))
  25506. (if context
  25507. (list context (match-beginning group) (match-end group))
  25508. t)))
  25509. (defun org-switch-to-buffer-other-window (&rest args)
  25510. "Switch to buffer in a second window on the current frame.
  25511. In particular, do not allow pop-up frames."
  25512. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25513. special-display-function)
  25514. (apply 'switch-to-buffer-other-window args)))
  25515. (defun org-combine-plists (&rest plists)
  25516. "Create a single property list from all plists in PLISTS.
  25517. The process starts by copying the first list, and then setting properties
  25518. from the other lists. Settings in the last list are the most significant
  25519. ones and overrule settings in the other lists."
  25520. (let ((rtn (copy-sequence (pop plists)))
  25521. p v ls)
  25522. (while plists
  25523. (setq ls (pop plists))
  25524. (while ls
  25525. (setq p (pop ls) v (pop ls))
  25526. (setq rtn (plist-put rtn p v))))
  25527. rtn))
  25528. (defun org-move-line-down (arg)
  25529. "Move the current line down. With prefix argument, move it past ARG lines."
  25530. (interactive "p")
  25531. (let ((col (current-column))
  25532. beg end pos)
  25533. (beginning-of-line 1) (setq beg (point))
  25534. (beginning-of-line 2) (setq end (point))
  25535. (beginning-of-line (+ 1 arg))
  25536. (setq pos (move-marker (make-marker) (point)))
  25537. (insert (delete-and-extract-region beg end))
  25538. (goto-char pos)
  25539. (move-to-column col)))
  25540. (defun org-move-line-up (arg)
  25541. "Move the current line up. With prefix argument, move it past ARG lines."
  25542. (interactive "p")
  25543. (let ((col (current-column))
  25544. beg end pos)
  25545. (beginning-of-line 1) (setq beg (point))
  25546. (beginning-of-line 2) (setq end (point))
  25547. (beginning-of-line (- arg))
  25548. (setq pos (move-marker (make-marker) (point)))
  25549. (insert (delete-and-extract-region beg end))
  25550. (goto-char pos)
  25551. (move-to-column col)))
  25552. (defun org-replace-escapes (string table)
  25553. "Replace %-escapes in STRING with values in TABLE.
  25554. TABLE is an association list with keys like \"%a\" and string values.
  25555. The sequences in STRING may contain normal field width and padding information,
  25556. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25557. so values can contain further %-escapes if they are define later in TABLE."
  25558. (let ((case-fold-search nil)
  25559. e re rpl)
  25560. (while (setq e (pop table))
  25561. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25562. (while (string-match re string)
  25563. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25564. (cdr e)))
  25565. (setq string (replace-match rpl t t string))))
  25566. string))
  25567. (defun org-sublist (list start end)
  25568. "Return a section of LIST, from START to END.
  25569. Counting starts at 1."
  25570. (let (rtn (c start))
  25571. (setq list (nthcdr (1- start) list))
  25572. (while (and list (<= c end))
  25573. (push (pop list) rtn)
  25574. (setq c (1+ c)))
  25575. (nreverse rtn)))
  25576. (defun org-find-base-buffer-visiting (file)
  25577. "Like `find-buffer-visiting' but alway return the base buffer and
  25578. not an indirect buffer"
  25579. (let ((buf (find-buffer-visiting file)))
  25580. (if buf
  25581. (or (buffer-base-buffer buf) buf)
  25582. nil)))
  25583. (defun org-image-file-name-regexp ()
  25584. "Return regexp matching the file names of images."
  25585. (if (fboundp 'image-file-name-regexp)
  25586. (image-file-name-regexp)
  25587. (let ((image-file-name-extensions
  25588. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25589. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25590. (concat "\\."
  25591. (regexp-opt (nconc (mapcar 'upcase
  25592. image-file-name-extensions)
  25593. image-file-name-extensions)
  25594. t)
  25595. "\\'"))))
  25596. (defun org-file-image-p (file)
  25597. "Return non-nil if FILE is an image."
  25598. (save-match-data
  25599. (string-match (org-image-file-name-regexp) file)))
  25600. ;;; Paragraph filling stuff.
  25601. ;; We want this to be just right, so use the full arsenal.
  25602. (defun org-indent-line-function ()
  25603. "Indent line like previous, but further if previous was headline or item."
  25604. (interactive)
  25605. (let* ((pos (point))
  25606. (itemp (org-at-item-p))
  25607. column bpos bcol tpos tcol bullet btype bullet-type)
  25608. ;; Find the previous relevant line
  25609. (beginning-of-line 1)
  25610. (cond
  25611. ((looking-at "#") (setq column 0))
  25612. ((looking-at "\\*+ ") (setq column 0))
  25613. (t
  25614. (beginning-of-line 0)
  25615. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25616. (beginning-of-line 0))
  25617. (cond
  25618. ((looking-at "\\*+[ \t]+")
  25619. (goto-char (match-end 0))
  25620. (setq column (current-column)))
  25621. ((org-in-item-p)
  25622. (org-beginning-of-item)
  25623. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25624. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25625. (setq bpos (match-beginning 1) tpos (match-end 0)
  25626. bcol (progn (goto-char bpos) (current-column))
  25627. tcol (progn (goto-char tpos) (current-column))
  25628. bullet (match-string 1)
  25629. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25630. (if (not itemp)
  25631. (setq column tcol)
  25632. (goto-char pos)
  25633. (beginning-of-line 1)
  25634. (if (looking-at "\\S-")
  25635. (progn
  25636. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25637. (setq bullet (match-string 1)
  25638. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25639. (setq column (if (equal btype bullet-type) bcol tcol)))
  25640. (setq column (org-get-indentation)))))
  25641. (t (setq column (org-get-indentation))))))
  25642. (goto-char pos)
  25643. (if (<= (current-column) (current-indentation))
  25644. (indent-line-to column)
  25645. (save-excursion (indent-line-to column)))
  25646. (setq column (current-column))
  25647. (beginning-of-line 1)
  25648. (if (looking-at
  25649. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25650. (replace-match (concat "\\1" (format org-property-format
  25651. (match-string 2) (match-string 3)))
  25652. t nil))
  25653. (move-to-column column)))
  25654. (defun org-set-autofill-regexps ()
  25655. (interactive)
  25656. ;; In the paragraph separator we include headlines, because filling
  25657. ;; text in a line directly attached to a headline would otherwise
  25658. ;; fill the headline as well.
  25659. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25660. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25661. ;; The paragraph starter includes hand-formatted lists.
  25662. (org-set-local 'paragraph-start
  25663. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25664. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25665. ;; But only if the user has not turned off tables or fixed-width regions
  25666. (org-set-local
  25667. 'auto-fill-inhibit-regexp
  25668. (concat "\\*+ \\|#\\+"
  25669. "\\|[ \t]*" org-keyword-time-regexp
  25670. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25671. (concat
  25672. "\\|[ \t]*["
  25673. (if org-enable-table-editor "|" "")
  25674. (if org-enable-fixed-width-editor ":" "")
  25675. "]"))))
  25676. ;; We use our own fill-paragraph function, to make sure that tables
  25677. ;; and fixed-width regions are not wrapped. That function will pass
  25678. ;; through to `fill-paragraph' when appropriate.
  25679. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25680. ; Adaptive filling: To get full control, first make sure that
  25681. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25682. (org-set-local 'adaptive-fill-regexp "\000")
  25683. (org-set-local 'adaptive-fill-function
  25684. 'org-adaptive-fill-function)
  25685. (org-set-local
  25686. 'align-mode-rules-list
  25687. '((org-in-buffer-settings
  25688. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25689. (modes . '(org-mode))))))
  25690. (defun org-fill-paragraph (&optional justify)
  25691. "Re-align a table, pass through to fill-paragraph if no table."
  25692. (let ((table-p (org-at-table-p))
  25693. (table.el-p (org-at-table.el-p)))
  25694. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25695. (save-excursion (goto-char (point-at-bol))
  25696. (looking-at outline-regexp)))
  25697. t) ; skip headlines
  25698. (table.el-p t) ; skip table.el tables
  25699. (table-p (org-table-align) t) ; align org-mode tables
  25700. (t nil)))) ; call paragraph-fill
  25701. ;; For reference, this is the default value of adaptive-fill-regexp
  25702. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25703. (defun org-adaptive-fill-function ()
  25704. "Return a fill prefix for org-mode files.
  25705. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25706. work correctly."
  25707. (cond ((looking-at "#[ \t]+")
  25708. (match-string 0))
  25709. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25710. (save-excursion
  25711. (goto-char (match-end 0))
  25712. (make-string (current-column) ?\ )))
  25713. (t nil)))
  25714. ;;;; Functions extending outline functionality
  25715. (defun org-beginning-of-line (&optional arg)
  25716. "Go to the beginning of the current line. If that is invisible, continue
  25717. to a visible line beginning. This makes the function of C-a more intuitive.
  25718. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25719. first attempt, and only move to after the tags when the cursor is already
  25720. beyond the end of the headline."
  25721. (interactive "P")
  25722. (let ((pos (point)))
  25723. (beginning-of-line 1)
  25724. (if (bobp)
  25725. nil
  25726. (backward-char 1)
  25727. (if (org-invisible-p)
  25728. (while (and (not (bobp)) (org-invisible-p))
  25729. (backward-char 1)
  25730. (beginning-of-line 1))
  25731. (forward-char 1)))
  25732. (when org-special-ctrl-a/e
  25733. (cond
  25734. ((and (looking-at org-todo-line-regexp)
  25735. (= (char-after (match-end 1)) ?\ ))
  25736. (goto-char
  25737. (if (eq org-special-ctrl-a/e t)
  25738. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25739. ((= pos (point)) (match-beginning 3))
  25740. (t (point)))
  25741. (cond ((> pos (point)) (point))
  25742. ((not (eq last-command this-command)) (point))
  25743. (t (match-beginning 3))))))
  25744. ((org-at-item-p)
  25745. (goto-char
  25746. (if (eq org-special-ctrl-a/e t)
  25747. (cond ((> pos (match-end 4)) (match-end 4))
  25748. ((= pos (point)) (match-end 4))
  25749. (t (point)))
  25750. (cond ((> pos (point)) (point))
  25751. ((not (eq last-command this-command)) (point))
  25752. (t (match-end 4))))))))))
  25753. (defun org-end-of-line (&optional arg)
  25754. "Go to the end of the line.
  25755. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25756. first attempt, and only move to after the tags when the cursor is already
  25757. beyond the end of the headline."
  25758. (interactive "P")
  25759. (if (or (not org-special-ctrl-a/e)
  25760. (not (org-on-heading-p)))
  25761. (end-of-line arg)
  25762. (let ((pos (point)))
  25763. (beginning-of-line 1)
  25764. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25765. (if (eq org-special-ctrl-a/e t)
  25766. (if (or (< pos (match-beginning 1))
  25767. (= pos (match-end 0)))
  25768. (goto-char (match-beginning 1))
  25769. (goto-char (match-end 0)))
  25770. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25771. (goto-char (match-end 0))
  25772. (goto-char (match-beginning 1))))
  25773. (end-of-line arg)))))
  25774. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25775. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25776. (defun org-kill-line (&optional arg)
  25777. "Kill line, to tags or end of line."
  25778. (interactive "P")
  25779. (cond
  25780. ((or (not org-special-ctrl-k)
  25781. (bolp)
  25782. (not (org-on-heading-p)))
  25783. (call-interactively 'kill-line))
  25784. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25785. (kill-region (point) (match-beginning 1))
  25786. (org-set-tags nil t))
  25787. (t (kill-region (point) (point-at-eol)))))
  25788. (define-key org-mode-map "\C-k" 'org-kill-line)
  25789. (defun org-invisible-p ()
  25790. "Check if point is at a character currently not visible."
  25791. ;; Early versions of noutline don't have `outline-invisible-p'.
  25792. (if (fboundp 'outline-invisible-p)
  25793. (outline-invisible-p)
  25794. (get-char-property (point) 'invisible)))
  25795. (defun org-invisible-p2 ()
  25796. "Check if point is at a character currently not visible."
  25797. (save-excursion
  25798. (if (and (eolp) (not (bobp))) (backward-char 1))
  25799. ;; Early versions of noutline don't have `outline-invisible-p'.
  25800. (if (fboundp 'outline-invisible-p)
  25801. (outline-invisible-p)
  25802. (get-char-property (point) 'invisible))))
  25803. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25804. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25805. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25806. (defun org-at-heading-or-item-p ()
  25807. (or (org-on-heading-p) (org-at-item-p)))
  25808. (defun org-on-target-p ()
  25809. (or (org-in-regexp org-radio-target-regexp)
  25810. (org-in-regexp org-target-regexp)))
  25811. (defun org-up-heading-all (arg)
  25812. "Move to the heading line of which the present line is a subheading.
  25813. This function considers both visible and invisible heading lines.
  25814. With argument, move up ARG levels."
  25815. (if (fboundp 'outline-up-heading-all)
  25816. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25817. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25818. (defun org-up-heading-safe ()
  25819. "Move to the heading line of which the present line is a subheading.
  25820. This version will not throw an error. It will return the level of the
  25821. headline found, or nil if no higher level is found."
  25822. (let ((pos (point)) start-level level
  25823. (re (concat "^" outline-regexp)))
  25824. (catch 'exit
  25825. (outline-back-to-heading t)
  25826. (setq start-level (funcall outline-level))
  25827. (if (equal start-level 1) (throw 'exit nil))
  25828. (while (re-search-backward re nil t)
  25829. (setq level (funcall outline-level))
  25830. (if (< level start-level) (throw 'exit level)))
  25831. nil)))
  25832. (defun org-first-sibling-p ()
  25833. "Is this heading the first child of its parents?"
  25834. (interactive)
  25835. (let ((re (concat "^" outline-regexp))
  25836. level l)
  25837. (unless (org-at-heading-p t)
  25838. (error "Not at a heading"))
  25839. (setq level (funcall outline-level))
  25840. (save-excursion
  25841. (if (not (re-search-backward re nil t))
  25842. t
  25843. (setq l (funcall outline-level))
  25844. (< l level)))))
  25845. (defun org-goto-sibling (&optional previous)
  25846. "Goto the next sibling, even if it is invisible.
  25847. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25848. when a sibling was found. When none is found, return nil and don't
  25849. move point."
  25850. (let ((fun (if previous 're-search-backward 're-search-forward))
  25851. (pos (point))
  25852. (re (concat "^" outline-regexp))
  25853. level l)
  25854. (when (condition-case nil (org-back-to-heading t) (error nil))
  25855. (setq level (funcall outline-level))
  25856. (catch 'exit
  25857. (or previous (forward-char 1))
  25858. (while (funcall fun re nil t)
  25859. (setq l (funcall outline-level))
  25860. (when (< l level) (goto-char pos) (throw 'exit nil))
  25861. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25862. (goto-char pos)
  25863. nil))))
  25864. (defun org-show-siblings ()
  25865. "Show all siblings of the current headline."
  25866. (save-excursion
  25867. (while (org-goto-sibling) (org-flag-heading nil)))
  25868. (save-excursion
  25869. (while (org-goto-sibling 'previous)
  25870. (org-flag-heading nil))))
  25871. (defun org-show-hidden-entry ()
  25872. "Show an entry where even the heading is hidden."
  25873. (save-excursion
  25874. (org-show-entry)))
  25875. (defun org-flag-heading (flag &optional entry)
  25876. "Flag the current heading. FLAG non-nil means make invisible.
  25877. When ENTRY is non-nil, show the entire entry."
  25878. (save-excursion
  25879. (org-back-to-heading t)
  25880. ;; Check if we should show the entire entry
  25881. (if entry
  25882. (progn
  25883. (org-show-entry)
  25884. (save-excursion
  25885. (and (outline-next-heading)
  25886. (org-flag-heading nil))))
  25887. (outline-flag-region (max (point-min) (1- (point)))
  25888. (save-excursion (outline-end-of-heading) (point))
  25889. flag))))
  25890. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25891. ;; This is an exact copy of the original function, but it uses
  25892. ;; `org-back-to-heading', to make it work also in invisible
  25893. ;; trees. And is uses an invisible-OK argument.
  25894. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25895. (org-back-to-heading invisible-OK)
  25896. (let ((first t)
  25897. (level (funcall outline-level)))
  25898. (while (and (not (eobp))
  25899. (or first (> (funcall outline-level) level)))
  25900. (setq first nil)
  25901. (outline-next-heading))
  25902. (unless to-heading
  25903. (if (memq (preceding-char) '(?\n ?\^M))
  25904. (progn
  25905. ;; Go to end of line before heading
  25906. (forward-char -1)
  25907. (if (memq (preceding-char) '(?\n ?\^M))
  25908. ;; leave blank line before heading
  25909. (forward-char -1))))))
  25910. (point))
  25911. (defun org-show-subtree ()
  25912. "Show everything after this heading at deeper levels."
  25913. (outline-flag-region
  25914. (point)
  25915. (save-excursion
  25916. (outline-end-of-subtree) (outline-next-heading) (point))
  25917. nil))
  25918. (defun org-show-entry ()
  25919. "Show the body directly following this heading.
  25920. Show the heading too, if it is currently invisible."
  25921. (interactive)
  25922. (save-excursion
  25923. (condition-case nil
  25924. (progn
  25925. (org-back-to-heading t)
  25926. (outline-flag-region
  25927. (max (point-min) (1- (point)))
  25928. (save-excursion
  25929. (re-search-forward
  25930. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25931. (or (match-beginning 1) (point-max)))
  25932. nil))
  25933. (error nil))))
  25934. (defun org-make-options-regexp (kwds)
  25935. "Make a regular expression for keyword lines."
  25936. (concat
  25937. "^"
  25938. "#?[ \t]*\\+\\("
  25939. (mapconcat 'regexp-quote kwds "\\|")
  25940. "\\):[ \t]*"
  25941. "\\(.+\\)"))
  25942. ;; Make isearch reveal the necessary context
  25943. (defun org-isearch-end ()
  25944. "Reveal context after isearch exits."
  25945. (when isearch-success ; only if search was successful
  25946. (if (featurep 'xemacs)
  25947. ;; Under XEmacs, the hook is run in the correct place,
  25948. ;; we directly show the context.
  25949. (org-show-context 'isearch)
  25950. ;; In Emacs the hook runs *before* restoring the overlays.
  25951. ;; So we have to use a one-time post-command-hook to do this.
  25952. ;; (Emacs 22 has a special variable, see function `org-mode')
  25953. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25954. isearch-mode-end-hook-quit)
  25955. ;; Only when the isearch was not quitted.
  25956. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25957. 'append 'local)))))
  25958. (defun org-isearch-post-command ()
  25959. "Remove self from hook, and show context."
  25960. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25961. (org-show-context 'isearch))
  25962. ;;;; Integration with and fixes for other packages
  25963. ;;; Imenu support
  25964. (defvar org-imenu-markers nil
  25965. "All markers currently used by Imenu.")
  25966. (make-variable-buffer-local 'org-imenu-markers)
  25967. (defun org-imenu-new-marker (&optional pos)
  25968. "Return a new marker for use by Imenu, and remember the marker."
  25969. (let ((m (make-marker)))
  25970. (move-marker m (or pos (point)))
  25971. (push m org-imenu-markers)
  25972. m))
  25973. (defun org-imenu-get-tree ()
  25974. "Produce the index for Imenu."
  25975. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25976. (setq org-imenu-markers nil)
  25977. (let* ((n org-imenu-depth)
  25978. (re (concat "^" outline-regexp))
  25979. (subs (make-vector (1+ n) nil))
  25980. (last-level 0)
  25981. m tree level head)
  25982. (save-excursion
  25983. (save-restriction
  25984. (widen)
  25985. (goto-char (point-max))
  25986. (while (re-search-backward re nil t)
  25987. (setq level (org-reduced-level (funcall outline-level)))
  25988. (when (<= level n)
  25989. (looking-at org-complex-heading-regexp)
  25990. (setq head (org-match-string-no-properties 4)
  25991. m (org-imenu-new-marker))
  25992. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25993. (if (>= level last-level)
  25994. (push (cons head m) (aref subs level))
  25995. (push (cons head (aref subs (1+ level))) (aref subs level))
  25996. (loop for i from (1+ level) to n do (aset subs i nil)))
  25997. (setq last-level level)))))
  25998. (aref subs 1)))
  25999. (eval-after-load "imenu"
  26000. '(progn
  26001. (add-hook 'imenu-after-jump-hook
  26002. (lambda () (org-show-context 'org-goto)))))
  26003. ;; Speedbar support
  26004. (defun org-speedbar-set-agenda-restriction ()
  26005. "Restrict future agenda commands to the location at point in speedbar.
  26006. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26007. (interactive)
  26008. (let (p m tp np dir txt w)
  26009. (cond
  26010. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26011. 'org-imenu t))
  26012. (setq m (get-text-property p 'org-imenu-marker))
  26013. (save-excursion
  26014. (save-restriction
  26015. (set-buffer (marker-buffer m))
  26016. (goto-char m)
  26017. (org-agenda-set-restriction-lock 'subtree))))
  26018. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26019. 'speedbar-function 'speedbar-find-file))
  26020. (setq tp (previous-single-property-change
  26021. (1+ p) 'speedbar-function)
  26022. np (next-single-property-change
  26023. tp 'speedbar-function)
  26024. dir (speedbar-line-directory)
  26025. txt (buffer-substring-no-properties (or tp (point-min))
  26026. (or np (point-max))))
  26027. (save-excursion
  26028. (save-restriction
  26029. (set-buffer (find-file-noselect
  26030. (let ((default-directory dir))
  26031. (expand-file-name txt))))
  26032. (unless (org-mode-p)
  26033. (error "Cannot restrict to non-Org-mode file"))
  26034. (org-agenda-set-restriction-lock 'file))))
  26035. (t (error "Don't know how to restrict Org-mode's agenda")))
  26036. (org-move-overlay org-speedbar-restriction-lock-overlay
  26037. (point-at-bol) (point-at-eol))
  26038. (setq current-prefix-arg nil)
  26039. (org-agenda-maybe-redo)))
  26040. (eval-after-load "speedbar"
  26041. '(progn
  26042. (speedbar-add-supported-extension ".org")
  26043. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26044. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26045. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26046. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26047. (add-hook 'speedbar-visiting-tag-hook
  26048. (lambda () (org-show-context 'org-goto)))))
  26049. ;;; Fixes and Hacks
  26050. ;; Make flyspell not check words in links, to not mess up our keymap
  26051. (defun org-mode-flyspell-verify ()
  26052. "Don't let flyspell put overlays at active buttons."
  26053. (not (get-text-property (point) 'keymap)))
  26054. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26055. (eval-after-load "bookmark"
  26056. '(if (boundp 'bookmark-after-jump-hook)
  26057. ;; We can use the hook
  26058. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26059. ;; Hook not available, use advice
  26060. (defadvice bookmark-jump (after org-make-visible activate)
  26061. "Make the position visible."
  26062. (org-bookmark-jump-unhide))))
  26063. (defun org-bookmark-jump-unhide ()
  26064. "Unhide the current position, to show the bookmark location."
  26065. (and (org-mode-p)
  26066. (or (org-invisible-p)
  26067. (save-excursion (goto-char (max (point-min) (1- (point))))
  26068. (org-invisible-p)))
  26069. (org-show-context 'bookmark-jump)))
  26070. ;; Fix a bug in htmlize where there are text properties (face nil)
  26071. (eval-after-load "htmlize"
  26072. '(progn
  26073. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26074. "Make sure there are no nil faces"
  26075. (setq ad-return-value (delq nil ad-return-value)))))
  26076. ;; Make session.el ignore our circular variable
  26077. (eval-after-load "session"
  26078. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26079. ;;;; Experimental code
  26080. (defun org-closed-in-range ()
  26081. "Sparse tree of items closed in a certain time range.
  26082. Still experimental, may disappear in the future."
  26083. (interactive)
  26084. ;; Get the time interval from the user.
  26085. (let* ((time1 (time-to-seconds
  26086. (org-read-date nil 'to-time nil "Starting date: ")))
  26087. (time2 (time-to-seconds
  26088. (org-read-date nil 'to-time nil "End date:")))
  26089. ;; callback function
  26090. (callback (lambda ()
  26091. (let ((time
  26092. (time-to-seconds
  26093. (apply 'encode-time
  26094. (org-parse-time-string
  26095. (match-string 1))))))
  26096. ;; check if time in interval
  26097. (and (>= time time1) (<= time time2))))))
  26098. ;; make tree, check each match with the callback
  26099. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26100. ;;;; Finish up
  26101. (provide 'org)
  26102. (run-hooks 'org-load-hook)
  26103. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26104. ;;; org.el ends here