org.el 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.21+
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.21+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. ;; FIXME: Needs a separate group...
  138. (defcustom org-completion-fallback-command 'hippie-expand
  139. "The expansion command called by \\[org-complete] in normal context.
  140. Normal means, no org-mode-specific context."
  141. :group 'org
  142. :type 'function)
  143. (defgroup org-startup nil
  144. "Options concerning startup of Org-mode."
  145. :tag "Org Startup"
  146. :group 'org)
  147. (defcustom org-startup-folded t
  148. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  149. This can also be configured on a per-file basis by adding one of
  150. the following lines anywhere in the buffer:
  151. #+STARTUP: fold
  152. #+STARTUP: nofold
  153. #+STARTUP: content"
  154. :group 'org-startup
  155. :type '(choice
  156. (const :tag "nofold: show all" nil)
  157. (const :tag "fold: overview" t)
  158. (const :tag "content: all headlines" content)))
  159. (defcustom org-startup-truncated t
  160. "Non-nil means, entering Org-mode will set `truncate-lines'.
  161. This is useful since some lines containing links can be very long and
  162. uninteresting. Also tables look terrible when wrapped."
  163. :group 'org-startup
  164. :type 'boolean)
  165. (defcustom org-startup-align-all-tables nil
  166. "Non-nil means, align all tables when visiting a file.
  167. This is useful when the column width in tables is forced with <N> cookies
  168. in table fields. Such tables will look correct only after the first re-align.
  169. This can also be configured on a per-file basis by adding one of
  170. the following lines anywhere in the buffer:
  171. #+STARTUP: align
  172. #+STARTUP: noalign"
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-insert-mode-line-in-empty-file nil
  176. "Non-nil means insert the first line setting Org-mode in empty files.
  177. When the function `org-mode' is called interactively in an empty file, this
  178. normally means that the file name does not automatically trigger Org-mode.
  179. To ensure that the file will always be in Org-mode in the future, a
  180. line enforcing Org-mode will be inserted into the buffer, if this option
  181. has been set."
  182. :group 'org-startup
  183. :type 'boolean)
  184. (defcustom org-replace-disputed-keys nil
  185. "Non-nil means use alternative key bindings for some keys.
  186. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  187. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  188. If you want to use Org-mode together with one of these other modes,
  189. or more generally if you would like to move some Org-mode commands to
  190. other keys, set this variable and configure the keys with the variable
  191. `org-disputed-keys'.
  192. This option is only relevant at load-time of Org-mode, and must be set
  193. *before* org.el is loaded. Changing it requires a restart of Emacs to
  194. become effective."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (if (fboundp 'defvaralias)
  198. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  199. (defcustom org-disputed-keys
  200. '(([(shift up)] . [(meta p)])
  201. ([(shift down)] . [(meta n)])
  202. ([(shift left)] . [(meta -)])
  203. ([(shift right)] . [(meta +)])
  204. ([(control shift right)] . [(meta shift +)])
  205. ([(control shift left)] . [(meta shift -)]))
  206. "Keys for which Org-mode and other modes compete.
  207. This is an alist, cars are the default keys, second element specifies
  208. the alternative to use when `org-replace-disputed-keys' is t.
  209. Keys can be specified in any syntax supported by `define-key'.
  210. The value of this option takes effect only at Org-mode's startup,
  211. therefore you'll have to restart Emacs to apply it after changing."
  212. :group 'org-startup
  213. :type 'alist)
  214. (defun org-key (key)
  215. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  216. Or return the original if not disputed."
  217. (if org-replace-disputed-keys
  218. (let* ((nkey (key-description key))
  219. (x (org-find-if (lambda (x)
  220. (equal (key-description (car x)) nkey))
  221. org-disputed-keys)))
  222. (if x (cdr x) key))
  223. key))
  224. (defun org-find-if (predicate seq)
  225. (catch 'exit
  226. (while seq
  227. (if (funcall predicate (car seq))
  228. (throw 'exit (car seq))
  229. (pop seq)))))
  230. (defun org-defkey (keymap key def)
  231. "Define a key, possibly translated, as returned by `org-key'."
  232. (define-key keymap (org-key key) def))
  233. (defcustom org-ellipsis nil
  234. "The ellipsis to use in the Org-mode outline.
  235. When nil, just use the standard three dots. When a string, use that instead,
  236. When a face, use the standart 3 dots, but with the specified face.
  237. The change affects only Org-mode (which will then use its own display table).
  238. Changing this requires executing `M-x org-mode' in a buffer to become
  239. effective."
  240. :group 'org-startup
  241. :type '(choice (const :tag "Default" nil)
  242. (face :tag "Face" :value org-warning)
  243. (string :tag "String" :value "...#")))
  244. (defvar org-display-table nil
  245. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  246. (defgroup org-keywords nil
  247. "Keywords in Org-mode."
  248. :tag "Org Keywords"
  249. :group 'org)
  250. (defcustom org-deadline-string "DEADLINE:"
  251. "String to mark deadline entries.
  252. A deadline is this string, followed by a time stamp. Should be a word,
  253. terminated by a colon. You can insert a schedule keyword and
  254. a timestamp with \\[org-deadline].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-scheduled-string "SCHEDULED:"
  259. "String to mark scheduled TODO entries.
  260. A schedule is this string, followed by a time stamp. Should be a word,
  261. terminated by a colon. You can insert a schedule keyword and
  262. a timestamp with \\[org-schedule].
  263. Changes become only effective after restarting Emacs."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defcustom org-closed-string "CLOSED:"
  267. "String used as the prefix for timestamps logging closing a TODO entry."
  268. :group 'org-keywords
  269. :type 'string)
  270. (defcustom org-clock-string "CLOCK:"
  271. "String used as prefix for timestamps clocking work hours on an item."
  272. :group 'org-keywords
  273. :type 'string)
  274. (defcustom org-comment-string "COMMENT"
  275. "Entries starting with this keyword will never be exported.
  276. An entry can be toggled between COMMENT and normal with
  277. \\[org-toggle-comment].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-quote-string "QUOTE"
  282. "Entries starting with this keyword will be exported in fixed-width font.
  283. Quoting applies only to the text in the entry following the headline, and does
  284. not extend beyond the next headline, even if that is lower level.
  285. An entry can be toggled between QUOTE and normal with
  286. \\[org-toggle-fixed-width-section]."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defconst org-repeat-re
  290. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  291. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  292. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  293. "Regular expression for specifying repeated events.
  294. After a match, group 1 contains the repeat expression.")
  295. (defgroup org-structure nil
  296. "Options concerning the general structure of Org-mode files."
  297. :tag "Org Structure"
  298. :group 'org)
  299. (defgroup org-reveal-location nil
  300. "Options about how to make context of a location visible."
  301. :tag "Org Reveal Location"
  302. :group 'org-structure)
  303. (defconst org-context-choice
  304. '(choice
  305. (const :tag "Always" t)
  306. (const :tag "Never" nil)
  307. (repeat :greedy t :tag "Individual contexts"
  308. (cons
  309. (choice :tag "Context"
  310. (const agenda)
  311. (const org-goto)
  312. (const occur-tree)
  313. (const tags-tree)
  314. (const link-search)
  315. (const mark-goto)
  316. (const bookmark-jump)
  317. (const isearch)
  318. (const default))
  319. (boolean))))
  320. "Contexts for the reveal options.")
  321. (defcustom org-show-hierarchy-above '((default . t))
  322. "Non-nil means, show full hierarchy when revealing a location.
  323. Org-mode often shows locations in an org-mode file which might have
  324. been invisible before. When this is set, the hierarchy of headings
  325. above the exposed location is shown.
  326. Turning this off for example for sparse trees makes them very compact.
  327. Instead of t, this can also be an alist specifying this option for different
  328. contexts. Valid contexts are
  329. agenda when exposing an entry from the agenda
  330. org-goto when using the command `org-goto' on key C-c C-j
  331. occur-tree when using the command `org-occur' on key C-c /
  332. tags-tree when constructing a sparse tree based on tags matches
  333. link-search when exposing search matches associated with a link
  334. mark-goto when exposing the jump goal of a mark
  335. bookmark-jump when exposing a bookmark location
  336. isearch when exiting from an incremental search
  337. default default for all contexts not set explicitly"
  338. :group 'org-reveal-location
  339. :type org-context-choice)
  340. (defcustom org-show-following-heading '((default . nil))
  341. "Non-nil means, show following heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the heading following the
  344. match is shown.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type org-context-choice)
  352. (defcustom org-show-siblings '((default . nil) (isearch t))
  353. "Non-nil means, show all sibling heading when revealing a location.
  354. Org-mode often shows locations in an org-mode file which might have
  355. been invisible before. When this is set, the sibling of the current entry
  356. heading are all made visible. If `org-show-hierarchy-above' is t,
  357. the same happens on each level of the hierarchy above the current entry.
  358. By default this is on for the isearch context, off for all other contexts.
  359. Turning this off for example for sparse trees makes them very compact,
  360. but makes it harder to edit the location of the match. In such a case,
  361. use the command \\[org-reveal] to show more context.
  362. Instead of t, this can also be an alist specifying this option for different
  363. contexts. See `org-show-hierarchy-above' for valid contexts."
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-entry-below '((default . nil))
  367. "Non-nil means, show the entry below a headline when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the text below the headline that is
  370. exposed is also shown.
  371. By default this is off for all contexts.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defgroup org-cycle nil
  377. "Options concerning visibility cycling in Org-mode."
  378. :tag "Org Cycle"
  379. :group 'org-structure)
  380. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  381. "Names of drawers. Drawers are not opened by cycling on the headline above.
  382. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  383. this:
  384. :DRAWERNAME:
  385. .....
  386. :END:
  387. The drawer \"PROPERTIES\" is special for capturing properties through
  388. the property API.
  389. Drawers can be defined on the per-file basis with a line like:
  390. #+DRAWERS: HIDDEN STATE PROPERTIES"
  391. :group 'org-structure
  392. :type '(repeat (string :tag "Drawer Name")))
  393. (defcustom org-cycle-global-at-bob nil
  394. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  395. This makes it possible to do global cycling without having to use S-TAB or
  396. C-u TAB. For this special case to work, the first line of the buffer
  397. must not be a headline - it may be empty ot some other text. When used in
  398. this way, `org-cycle-hook' is disables temporarily, to make sure the
  399. cursor stays at the beginning of the buffer.
  400. When this option is nil, don't do anything special at the beginning
  401. of the buffer."
  402. :group 'org-cycle
  403. :type 'boolean)
  404. (defcustom org-cycle-emulate-tab t
  405. "Where should `org-cycle' emulate TAB.
  406. nil Never
  407. white Only in completely white lines
  408. whitestart Only at the beginning of lines, before the first non-white char
  409. t Everywhere except in headlines
  410. exc-hl-bol Everywhere except at the start of a headline
  411. If TAB is used in a place where it does not emulate TAB, the current subtree
  412. visibility is cycled."
  413. :group 'org-cycle
  414. :type '(choice (const :tag "Never" nil)
  415. (const :tag "Only in completely white lines" white)
  416. (const :tag "Before first char in a line" whitestart)
  417. (const :tag "Everywhere except in headlines" t)
  418. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  419. ))
  420. (defcustom org-cycle-separator-lines 2
  421. "Number of empty lines needed to keep an empty line between collapsed trees.
  422. If you leave an empty line between the end of a subtree and the following
  423. headline, this empty line is hidden when the subtree is folded.
  424. Org-mode will leave (exactly) one empty line visible if the number of
  425. empty lines is equal or larger to the number given in this variable.
  426. So the default 2 means, at least 2 empty lines after the end of a subtree
  427. are needed to produce free space between a collapsed subtree and the
  428. following headline.
  429. Special case: when 0, never leave empty lines in collapsed view."
  430. :group 'org-cycle
  431. :type 'integer)
  432. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  433. org-cycle-hide-drawers
  434. org-cycle-show-empty-lines
  435. org-optimize-window-after-visibility-change)
  436. "Hook that is run after `org-cycle' has changed the buffer visibility.
  437. The function(s) in this hook must accept a single argument which indicates
  438. the new state that was set by the most recent `org-cycle' command. The
  439. argument is a symbol. After a global state change, it can have the values
  440. `overview', `content', or `all'. After a local state change, it can have
  441. the values `folded', `children', or `subtree'."
  442. :group 'org-cycle
  443. :type 'hook)
  444. (defgroup org-edit-structure nil
  445. "Options concerning structure editing in Org-mode."
  446. :tag "Org Edit Structure"
  447. :group 'org-structure)
  448. (defcustom org-special-ctrl-a/e nil
  449. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  450. When t, `C-a' will bring back the cursor to the beginning of the
  451. headline text, i.e. after the stars and after a possible TODO keyword.
  452. In an item, this will be the position after the bullet.
  453. When the cursor is already at that position, another `C-a' will bring
  454. it to the beginning of the line.
  455. `C-e' will jump to the end of the headline, ignoring the presence of tags
  456. in the headline. A second `C-e' will then jump to the true end of the
  457. line, after any tags.
  458. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  459. and only a directly following, identical keypress will bring the cursor
  460. to the special positions."
  461. :group 'org-edit-structure
  462. :type '(choice
  463. (const :tag "off" nil)
  464. (const :tag "after bullet first" t)
  465. (const :tag "border first" reversed)))
  466. (if (fboundp 'defvaralias)
  467. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  468. (defcustom org-special-ctrl-k nil
  469. "Non-nil means `C-k' will behave specially in headlines.
  470. When nil, `C-k' will call the default `kill-line' command.
  471. When t, the following will happen while the cursor is in the headline:
  472. - When the cursor is at the beginning of a headline, kill the entire
  473. line and possible the folded subtree below the line.
  474. - When in the middle of the headline text, kill the headline up to the tags.
  475. - When after the headline text, kill the tags."
  476. :group 'org-edit-structure
  477. :type 'boolean)
  478. (defcustom org-odd-levels-only nil
  479. "Non-nil means, skip even levels and only use odd levels for the outline.
  480. This has the effect that two stars are being added/taken away in
  481. promotion/demotion commands. It also influences how levels are
  482. handled by the exporters.
  483. Changing it requires restart of `font-lock-mode' to become effective
  484. for fontification also in regions already fontified.
  485. You may also set this on a per-file basis by adding one of the following
  486. lines to the buffer:
  487. #+STARTUP: odd
  488. #+STARTUP: oddeven"
  489. :group 'org-edit-structure
  490. :group 'org-font-lock
  491. :type 'boolean)
  492. (defcustom org-adapt-indentation t
  493. "Non-nil means, adapt indentation when promoting and demoting.
  494. When this is set and the *entire* text in an entry is indented, the
  495. indentation is increased by one space in a demotion command, and
  496. decreased by one in a promotion command. If any line in the entry
  497. body starts at column 0, indentation is not changed at all."
  498. :group 'org-edit-structure
  499. :type 'boolean)
  500. (defcustom org-blank-before-new-entry '((heading . nil)
  501. (plain-list-item . nil))
  502. "Should `org-insert-heading' leave a blank line before new heading/item?
  503. The value is an alist, with `heading' and `plain-list-item' as car,
  504. and a boolean flag as cdr."
  505. :group 'org-edit-structure
  506. :type '(list
  507. (cons (const heading) (boolean))
  508. (cons (const plain-list-item) (boolean))))
  509. (defcustom org-insert-heading-hook nil
  510. "Hook being run after inserting a new heading."
  511. :group 'org-edit-structure
  512. :type 'hook)
  513. (defcustom org-enable-fixed-width-editor t
  514. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  515. This currently only means, they are never auto-wrapped.
  516. When nil, such lines will be treated like ordinary lines.
  517. See also the QUOTE keyword."
  518. :group 'org-edit-structure
  519. :type 'boolean)
  520. (defcustom org-goto-auto-isearch t
  521. "Non-nil means, typing characters in org-goto starts incremental search."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defgroup org-sparse-trees nil
  525. "Options concerning sparse trees in Org-mode."
  526. :tag "Org Sparse Trees"
  527. :group 'org-structure)
  528. (defcustom org-highlight-sparse-tree-matches t
  529. "Non-nil means, highlight all matches that define a sparse tree.
  530. The highlights will automatically disappear the next time the buffer is
  531. changed by an edit command."
  532. :group 'org-sparse-trees
  533. :type 'boolean)
  534. (defcustom org-remove-highlights-with-change t
  535. "Non-nil means, any change to the buffer will remove temporary highlights.
  536. Such highlights are created by `org-occur' and `org-clock-display'.
  537. When nil, `C-c C-c needs to be used to get rid of the highlights.
  538. The highlights created by `org-preview-latex-fragment' always need
  539. `C-c C-c' to be removed."
  540. :group 'org-sparse-trees
  541. :group 'org-time
  542. :type 'boolean)
  543. (defcustom org-occur-hook '(org-first-headline-recenter)
  544. "Hook that is run after `org-occur' has constructed a sparse tree.
  545. This can be used to recenter the window to show as much of the structure
  546. as possible."
  547. :group 'org-sparse-trees
  548. :type 'hook)
  549. (defgroup org-plain-lists nil
  550. "Options concerning plain lists in Org-mode."
  551. :tag "Org Plain lists"
  552. :group 'org-structure)
  553. (defcustom org-cycle-include-plain-lists nil
  554. "Non-nil means, include plain lists into visibility cycling.
  555. This means that during cycling, plain list items will *temporarily* be
  556. interpreted as outline headlines with a level given by 1000+i where i is the
  557. indentation of the bullet. In all other operations, plain list items are
  558. not seen as headlines. For example, you cannot assign a TODO keyword to
  559. such an item."
  560. :group 'org-plain-lists
  561. :type 'boolean)
  562. (defcustom org-plain-list-ordered-item-terminator t
  563. "The character that makes a line with leading number an ordered list item.
  564. Valid values are ?. and ?\). To get both terminators, use t. While
  565. ?. may look nicer, it creates the danger that a line with leading
  566. number may be incorrectly interpreted as an item. ?\) therefore is
  567. the safe choice."
  568. :group 'org-plain-lists
  569. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  570. (const :tag "paren like in \"2)\"" ?\))
  571. (const :tab "both" t)))
  572. (defcustom org-auto-renumber-ordered-lists t
  573. "Non-nil means, automatically renumber ordered plain lists.
  574. Renumbering happens when the sequence have been changed with
  575. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  576. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defcustom org-provide-checkbox-statistics t
  580. "Non-nil means, update checkbox statistics after insert and toggle.
  581. When this is set, checkbox statistics is updated each time you either insert
  582. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  583. with \\[org-ctrl-c-ctrl-c\\]."
  584. :group 'org-plain-lists
  585. :type 'boolean)
  586. (defgroup org-archive nil
  587. "Options concerning archiving in Org-mode."
  588. :tag "Org Archive"
  589. :group 'org-structure)
  590. (defcustom org-archive-tag "ARCHIVE"
  591. "The tag that marks a subtree as archived.
  592. An archived subtree does not open during visibility cycling, and does
  593. not contribute to the agenda listings.
  594. After changing this, font-lock must be restarted in the relevant buffers to
  595. get the proper fontification."
  596. :group 'org-archive
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-agenda-skip-archived-trees t
  600. "Non-nil means, the agenda will skip any items located in archived trees.
  601. An archived tree is a tree marked with the tag ARCHIVE."
  602. :group 'org-archive
  603. :group 'org-agenda-skip
  604. :type 'boolean)
  605. (defcustom org-cycle-open-archived-trees nil
  606. "Non-nil means, `org-cycle' will open archived trees.
  607. An archived tree is a tree marked with the tag ARCHIVE.
  608. When nil, archived trees will stay folded. You can still open them with
  609. normal outline commands like `show-all', but not with the cycling commands."
  610. :group 'org-archive
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-sparse-tree-open-archived-trees nil
  614. "Non-nil means sparse tree construction shows matches in archived trees.
  615. When nil, matches in these trees are highlighted, but the trees are kept in
  616. collapsed state."
  617. :group 'org-archive
  618. :group 'org-sparse-trees
  619. :type 'boolean)
  620. (defcustom org-archive-location "%s_archive::"
  621. "The location where subtrees should be archived.
  622. This string consists of two parts, separated by a double-colon.
  623. The first part is a file name - when omitted, archiving happens in the same
  624. file. %s will be replaced by the current file name (without directory part).
  625. Archiving to a different file is useful to keep archived entries from
  626. contributing to the Org-mode Agenda.
  627. The part after the double colon is a headline. The archived entries will be
  628. filed under that headline. When omitted, the subtrees are simply filed away
  629. at the end of the file, as top-level entries.
  630. Here are a few examples:
  631. \"%s_archive::\"
  632. If the current file is Projects.org, archive in file
  633. Projects.org_archive, as top-level trees. This is the default.
  634. \"::* Archived Tasks\"
  635. Archive in the current file, under the top-level headline
  636. \"* Archived Tasks\".
  637. \"~/org/archive.org::\"
  638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  639. \"basement::** Finished Tasks\"
  640. Archive in file ./basement (relative path), as level 3 trees
  641. below the level 2 heading \"** Finished Tasks\".
  642. You may set this option on a per-file basis by adding to the buffer a
  643. line like
  644. #+ARCHIVE: basement::** Finished Tasks"
  645. :group 'org-archive
  646. :type 'string)
  647. (defcustom org-archive-mark-done t
  648. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  649. This can be a string to set the keyword to use. When t, Org-mode will
  650. use the first keyword in its list that means done."
  651. :group 'org-archive
  652. :type '(choice
  653. (const :tag "No" nil)
  654. (const :tag "Yes" t)
  655. (string :tag "Use this keyword")))
  656. (defcustom org-archive-stamp-time t
  657. "Non-nil means, add a time stamp to entries moved to an archive file.
  658. This variable is obsolete and has no effect anymore, instead add ot remove
  659. `time' from the variablle `org-archive-save-context-info'."
  660. :group 'org-archive
  661. :type 'boolean)
  662. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  663. "Parts of context info that should be stored as properties when archiving.
  664. When a subtree is moved to an archive file, it looses information given by
  665. context, like inherited tags, the category, and possibly also the TODO
  666. state (depending on the variable `org-archive-mark-done').
  667. This variable can be a list of any of the following symbols:
  668. time The time of archiving.
  669. file The file where the entry originates.
  670. itags The local tags, in the headline of the subtree.
  671. ltags The tags the subtree inherits from further up the hierarchy.
  672. todo The pre-archive TODO state.
  673. category The category, taken from file name or #+CATEGORY lines.
  674. olpath The outline path to the item. These are all headlines above
  675. the current item, separated by /, like a file path.
  676. For each symbol present in the list, a property will be created in
  677. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  678. information."
  679. :group 'org-archive
  680. :type '(set :greedy t
  681. (const :tag "Time" time)
  682. (const :tag "File" file)
  683. (const :tag "Category" category)
  684. (const :tag "TODO state" todo)
  685. (const :tag "TODO state" priority)
  686. (const :tag "Inherited tags" itags)
  687. (const :tag "Outline path" olpath)
  688. (const :tag "Local tags" ltags)))
  689. (defgroup org-imenu-and-speedbar nil
  690. "Options concerning imenu and speedbar in Org-mode."
  691. :tag "Org Imenu and Speedbar"
  692. :group 'org-structure)
  693. (defcustom org-imenu-depth 2
  694. "The maximum level for Imenu access to Org-mode headlines.
  695. This also applied for speedbar access."
  696. :group 'org-imenu-and-speedbar
  697. :type 'number)
  698. (defgroup org-table nil
  699. "Options concerning tables in Org-mode."
  700. :tag "Org Table"
  701. :group 'org)
  702. (defcustom org-enable-table-editor 'optimized
  703. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  704. When nil, such lines will be treated like ordinary lines.
  705. When equal to the symbol `optimized', the table editor will be optimized to
  706. do the following:
  707. - Automatic overwrite mode in front of whitespace in table fields.
  708. This makes the structure of the table stay in tact as long as the edited
  709. field does not exceed the column width.
  710. - Minimize the number of realigns. Normally, the table is aligned each time
  711. TAB or RET are pressed to move to another field. With optimization this
  712. happens only if changes to a field might have changed the column width.
  713. Optimization requires replacing the functions `self-insert-command',
  714. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  715. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  716. very good at guessing when a re-align will be necessary, but you can always
  717. force one with \\[org-ctrl-c-ctrl-c].
  718. If you would like to use the optimized version in Org-mode, but the
  719. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  720. This variable can be used to turn on and off the table editor during a session,
  721. but in order to toggle optimization, a restart is required.
  722. See also the variable `org-table-auto-blank-field'."
  723. :group 'org-table
  724. :type '(choice
  725. (const :tag "off" nil)
  726. (const :tag "on" t)
  727. (const :tag "on, optimized" optimized)))
  728. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  729. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  730. In the optimized version, the table editor takes over all simple keys that
  731. normally just insert a character. In tables, the characters are inserted
  732. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  733. for empty fields). Outside tables, the correct binding of the keys is
  734. restored.
  735. The default for this option is t if the optimized version is also used in
  736. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  737. this variable requires a restart of Emacs to become effective."
  738. :group 'org-table
  739. :type 'boolean)
  740. (defcustom orgtbl-radio-table-templates
  741. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  742. % END RECEIVE ORGTBL %n
  743. \\begin{comment}
  744. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  745. | | |
  746. \\end{comment}\n")
  747. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  748. @c END RECEIVE ORGTBL %n
  749. @ignore
  750. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  751. | | |
  752. @end ignore\n")
  753. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  754. <!-- END RECEIVE ORGTBL %n -->
  755. <!--
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. -->\n"))
  759. "Templates for radio tables in different major modes.
  760. All occurrences of %n in a template will be replaced with the name of the
  761. table, obtained by prompting the user."
  762. :group 'org-table
  763. :type '(repeat
  764. (list (symbol :tag "Major mode")
  765. (string :tag "Format"))))
  766. (defgroup org-table-settings nil
  767. "Settings for tables in Org-mode."
  768. :tag "Org Table Settings"
  769. :group 'org-table)
  770. (defcustom org-table-default-size "5x2"
  771. "The default size for newly created tables, Columns x Rows."
  772. :group 'org-table-settings
  773. :type 'string)
  774. (defcustom org-table-number-regexp
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  776. "Regular expression for recognizing numbers in table columns.
  777. If a table column contains mostly numbers, it will be aligned to the
  778. right. If not, it will be aligned to the left.
  779. The default value of this option is a regular expression which allows
  780. anything which looks remotely like a number as used in scientific
  781. context. For example, all of the following will be considered a
  782. number:
  783. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  784. Other options offered by the customize interface are more restrictive."
  785. :group 'org-table-settings
  786. :type '(choice
  787. (const :tag "Positive Integers"
  788. "^[0-9]+$")
  789. (const :tag "Integers"
  790. "^[-+]?[0-9]+$")
  791. (const :tag "Floating Point Numbers"
  792. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  793. (const :tag "Floating Point Number or Integer"
  794. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  795. (const :tag "Exponential, Floating point, Integer"
  796. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  797. (const :tag "Very General Number-Like, including hex"
  798. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  799. (string :tag "Regexp:")))
  800. (defcustom org-table-number-fraction 0.5
  801. "Fraction of numbers in a column required to make the column align right.
  802. In a column all non-white fields are considered. If at least this
  803. fraction of fields is matched by `org-table-number-fraction',
  804. alignment to the right border applies."
  805. :group 'org-table-settings
  806. :type 'number)
  807. (defgroup org-table-editing nil
  808. "Behavior of tables during editing in Org-mode."
  809. :tag "Org Table Editing"
  810. :group 'org-table)
  811. (defcustom org-table-automatic-realign t
  812. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  813. When nil, aligning is only done with \\[org-table-align], or after column
  814. removal/insertion."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-auto-blank-field t
  818. "Non-nil means, automatically blank table field when starting to type into it.
  819. This only happens when typing immediately after a field motion
  820. command (TAB, S-TAB or RET).
  821. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  822. :group 'org-table-editing
  823. :type 'boolean)
  824. (defcustom org-table-tab-jumps-over-hlines t
  825. "Non-nil means, tab in the last column of a table with jump over a hline.
  826. If a horizontal separator line is following the current line,
  827. `org-table-next-field' can either create a new row before that line, or jump
  828. over the line. When this option is nil, a new line will be created before
  829. this line."
  830. :group 'org-table-editing
  831. :type 'boolean)
  832. (defcustom org-table-tab-recognizes-table.el t
  833. "Non-nil means, TAB will automatically notice a table.el table.
  834. When it sees such a table, it moves point into it and - if necessary -
  835. calls `table-recognize-table'."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defgroup org-table-calculation nil
  839. "Options concerning tables in Org-mode."
  840. :tag "Org Table Calculation"
  841. :group 'org-table)
  842. (defcustom org-table-use-standard-references t
  843. "Should org-mode work with table refrences like B3 instead of @3$2?
  844. Possible values are:
  845. nil never use them
  846. from accept as input, do not present for editing
  847. t: accept as input and present for editing"
  848. :group 'org-table-calculation
  849. :type '(choice
  850. (const :tag "Never, don't even check unser input for them" nil)
  851. (const :tag "Always, both as user input, and when editing" t)
  852. (const :tag "Convert user input, don't offer during editing" 'from)))
  853. (defcustom org-table-copy-increment t
  854. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  855. :group 'org-table-calculation
  856. :type 'boolean)
  857. (defcustom org-calc-default-modes
  858. '(calc-internal-prec 12
  859. calc-float-format (float 5)
  860. calc-angle-mode deg
  861. calc-prefer-frac nil
  862. calc-symbolic-mode nil
  863. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  864. calc-display-working-message t
  865. )
  866. "List with Calc mode settings for use in calc-eval for table formulas.
  867. The list must contain alternating symbols (Calc modes variables and values).
  868. Don't remove any of the default settings, just change the values. Org-mode
  869. relies on the variables to be present in the list."
  870. :group 'org-table-calculation
  871. :type 'plist)
  872. (defcustom org-table-formula-evaluate-inline t
  873. "Non-nil means, TAB and RET evaluate a formula in current table field.
  874. If the current field starts with an equal sign, it is assumed to be a formula
  875. which should be evaluated as described in the manual and in the documentation
  876. string of the command `org-table-eval-formula'. This feature requires the
  877. Emacs calc package.
  878. When this variable is nil, formula calculation is only available through
  879. the command \\[org-table-eval-formula]."
  880. :group 'org-table-calculation
  881. :type 'boolean)
  882. (defcustom org-table-formula-use-constants t
  883. "Non-nil means, interpret constants in formulas in tables.
  884. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  885. by the value given in `org-table-formula-constants', or by a value obtained
  886. from the `constants.el' package."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defcustom org-table-formula-constants nil
  890. "Alist with constant names and values, for use in table formulas.
  891. The car of each element is a name of a constant, without the `$' before it.
  892. The cdr is the value as a string. For example, if you'd like to use the
  893. speed of light in a formula, you would configure
  894. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  895. and then use it in an equation like `$1*$c'.
  896. Constants can also be defined on a per-file basis using a line like
  897. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  898. :group 'org-table-calculation
  899. :type '(repeat
  900. (cons (string :tag "name")
  901. (string :tag "value"))))
  902. (defvar org-table-formula-constants-local nil
  903. "Local version of `org-table-formula-constants'.")
  904. (make-variable-buffer-local 'org-table-formula-constants-local)
  905. (defcustom org-table-allow-automatic-line-recalculation t
  906. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  907. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  908. :group 'org-table-calculation
  909. :type 'boolean)
  910. (defgroup org-link nil
  911. "Options concerning links in Org-mode."
  912. :tag "Org Link"
  913. :group 'org)
  914. (defvar org-link-abbrev-alist-local nil
  915. "Buffer-local version of `org-link-abbrev-alist', which see.
  916. The value of this is taken from the #+LINK lines.")
  917. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  918. (defcustom org-link-abbrev-alist nil
  919. "Alist of link abbreviations.
  920. The car of each element is a string, to be replaced at the start of a link.
  921. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  922. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  923. [[linkkey:tag][description]]
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there.
  926. REPLACE may also be a function that will be called with the tag as the
  927. only argument to create the link, which should be returned as a string.
  928. See the manual for examples."
  929. :group 'org-link
  930. :type 'alist)
  931. (defcustom org-descriptive-links t
  932. "Non-nil means, hide link part and only show description of bracket links.
  933. Bracket links are like [[link][descritpion]]. This variable sets the initial
  934. state in new org-mode buffers. The setting can then be toggled on a
  935. per-buffer basis from the Org->Hyperlinks menu."
  936. :group 'org-link
  937. :type 'boolean)
  938. (defcustom org-link-file-path-type 'adaptive
  939. "How the path name in file links should be stored.
  940. Valid values are:
  941. relative Relative to the current directory, i.e. the directory of the file
  942. into which the link is being inserted.
  943. absolute Absolute path, if possible with ~ for home directory.
  944. noabbrev Absolute path, no abbreviation of home directory.
  945. adaptive Use relative path for files in the current directory and sub-
  946. directories of it. For other files, use an absolute path."
  947. :group 'org-link
  948. :type '(choice
  949. (const relative)
  950. (const absolute)
  951. (const noabbrev)
  952. (const adaptive)))
  953. (defcustom org-activate-links '(bracket angle plain radio tag date)
  954. "Types of links that should be activated in Org-mode files.
  955. This is a list of symbols, each leading to the activation of a certain link
  956. type. In principle, it does not hurt to turn on most link types - there may
  957. be a small gain when turning off unused link types. The types are:
  958. bracket The recommended [[link][description]] or [[link]] links with hiding.
  959. angular Links in angular brackes that may contain whitespace like
  960. <bbdb:Carsten Dominik>.
  961. plain Plain links in normal text, no whitespace, like http://google.com.
  962. radio Text that is matched by a radio target, see manual for details.
  963. tag Tag settings in a headline (link to tag search).
  964. date Time stamps (link to calendar).
  965. Changing this variable requires a restart of Emacs to become effective."
  966. :group 'org-link
  967. :type '(set (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Tags" target)
  973. (const :tag "Timestamps" date)))
  974. (defgroup org-link-store nil
  975. "Options concerning storing links in Org-mode"
  976. :tag "Org Store Link"
  977. :group 'org-link)
  978. (defcustom org-email-link-description-format "Email %c: %.30s"
  979. "Format of the description part of a link to an email or usenet message.
  980. The following %-excapes will be replaced by corresponding information:
  981. %F full \"From\" field
  982. %f name, taken from \"From\" field, address if no name
  983. %T full \"To\" field
  984. %t first name in \"To\" field, address if no name
  985. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  986. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  987. %s subject
  988. %m message-id.
  989. You may use normal field width specification between the % and the letter.
  990. This is for example useful to limit the length of the subject.
  991. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  992. :group 'org-link-store
  993. :type 'string)
  994. (defcustom org-from-is-user-regexp
  995. (let (r1 r2)
  996. (when (and user-mail-address (not (string= user-mail-address "")))
  997. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  998. (when (and user-full-name (not (string= user-full-name "")))
  999. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1000. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1001. "Regexp mached against the \"From:\" header of an email or usenet message.
  1002. It should match if the message is from the user him/herself."
  1003. :group 'org-link-store
  1004. :type 'regexp)
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defcustom org-usenet-links-prefer-google nil
  1026. "Non-nil means, `org-store-link' will create web links to Google groups.
  1027. When nil, Gnus will be used for such links.
  1028. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1029. negates this setting for the duration of the command."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode"
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-tab-follows-link nil
  1037. "Non-nil means, on links TAB will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-return-follows-link nil
  1042. "Non-nil means, on links RET will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mouse-1-follows-link t
  1047. "Non-nil means, mouse-1 on a link will follow the link.
  1048. A longer mouse click will still set point. Does not wortk on XEmacs.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mark-ring-length 4
  1053. "Number of different positions to be recorded in the ring
  1054. Changing this requires a restart of Emacs to work correctly."
  1055. :group 'org-link-follow
  1056. :type 'interger)
  1057. (defcustom org-link-frame-setup
  1058. '((vm . vm-visit-folder-other-frame)
  1059. (gnus . gnus-other-frame)
  1060. (file . find-file-other-window))
  1061. "Setup the frame configuration for following links.
  1062. When following a link with Emacs, it may often be useful to display
  1063. this link in another window or frame. This variable can be used to
  1064. set this up for the different types of links.
  1065. For VM, use any of
  1066. `vm-visit-folder'
  1067. `vm-visit-folder-other-frame'
  1068. For Gnus, use any of
  1069. `gnus'
  1070. `gnus-other-frame'
  1071. For FILE, use any of
  1072. `find-file'
  1073. `find-file-other-window'
  1074. `find-file-other-frame'
  1075. For the calendar, use the variable `calendar-setup'.
  1076. For BBDB, it is currently only possible to display the matches in
  1077. another window."
  1078. :group 'org-link-follow
  1079. :type '(list
  1080. (cons (const vm)
  1081. (choice
  1082. (const vm-visit-folder)
  1083. (const vm-visit-folder-other-window)
  1084. (const vm-visit-folder-other-frame)))
  1085. (cons (const gnus)
  1086. (choice
  1087. (const gnus)
  1088. (const gnus-other-frame)))
  1089. (cons (const file)
  1090. (choice
  1091. (const find-file)
  1092. (const find-file-other-window)
  1093. (const find-file-other-frame)))))
  1094. (defcustom org-display-internal-link-with-indirect-buffer nil
  1095. "Non-nil means, use indirect buffer to display infile links.
  1096. Activating internal links (from one location in a file to another location
  1097. in the same file) normally just jumps to the location. When the link is
  1098. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1099. another window. When this option is set, the other window actually displays
  1100. an indirect buffer clone of the current buffer, to avoid any visibility
  1101. changes to the current buffer."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-open-non-existing-files nil
  1105. "Non-nil means, `org-open-file' will open non-existing files.
  1106. When nil, an error will be generated."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1110. "Function and arguments to call for following mailto links.
  1111. This is a list with the first element being a lisp function, and the
  1112. remaining elements being arguments to the function. In string arguments,
  1113. %a will be replaced by the address, and %s will be replaced by the subject
  1114. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1118. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1119. (const :tag "message-mail" (message-mail "%a" "%s"))
  1120. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1121. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1122. "Non-nil means, ask for confirmation before executing shell links.
  1123. Shell links can be dangerous: just think about a link
  1124. [[shell:rm -rf ~/*][Google Search]]
  1125. This link would show up in your Org-mode document as \"Google Search\",
  1126. but really it would remove your entire home directory.
  1127. Therefore we advise against setting this variable to nil.
  1128. Just change it to `y-or-n-p' of you want to confirm with a
  1129. single keystroke rather than having to type \"yes\"."
  1130. :group 'org-link-follow
  1131. :type '(choice
  1132. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1133. (const :tag "with y-or-n (faster)" y-or-n-p)
  1134. (const :tag "no confirmation (dangerous)" nil)))
  1135. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1137. Elisp links can be dangerous: just think about a link
  1138. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' of you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defconst org-file-apps-defaults-gnu
  1150. '((remote . emacs)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. ("ps" . "gv %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps" . "gv %s")
  1160. ("eps.gz" . "gv %s")
  1161. ("dvi" . "xdvi %s")
  1162. ("fig" . "xfig %s"))
  1163. "Default file applications on a MacOS X system.
  1164. The system \"open\" is known as a default, but we use X11 applications
  1165. for some files for which the OS does not have a good default.
  1166. See `org-file-apps'.")
  1167. (defconst org-file-apps-defaults-windowsnt
  1168. (list
  1169. '(remote . emacs)
  1170. (cons t
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. ("txt" . emacs)
  1181. ("tex" . emacs)
  1182. ("ltx" . emacs)
  1183. ("org" . emacs)
  1184. ("el" . emacs)
  1185. ("bib" . emacs)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"ext\" A string identifying an extension
  1194. `directory' Matches a directory
  1195. `remote' Matches a remote file, accessible through tramp or efs.
  1196. Remote files most likely should be visited through Emacs
  1197. because external applications cannot handle such paths.
  1198. t Default for all remaining files
  1199. Possible values for the command are:
  1200. `emacs' The file will be visited by the current Emacs process.
  1201. `default' Use the default application for this file type.
  1202. string A command to be executed by a shell; %s will be replaced
  1203. by the path to the file.
  1204. sexp A Lisp form which will be evaluated. The file path will
  1205. be available in the Lisp variable `file'.
  1206. For more examples, see the system specific constants
  1207. `org-file-apps-defaults-macosx'
  1208. `org-file-apps-defaults-windowsnt'
  1209. `org-file-apps-defaults-gnu'."
  1210. :group 'org-link-follow
  1211. :type '(repeat
  1212. (cons (choice :value ""
  1213. (string :tag "Extension")
  1214. (const :tag "Default for unrecognized files" t)
  1215. (const :tag "Remote file" remote)
  1216. (const :tag "Links to a directory" directory))
  1217. (choice :value ""
  1218. (const :tag "Visit with Emacs" emacs)
  1219. (const :tag "Use system default" default)
  1220. (string :tag "Command")
  1221. (sexp :tag "Lisp form")))))
  1222. (defcustom org-mhe-search-all-folders nil
  1223. "Non-nil means, that the search for the mh-message will be extended to
  1224. all folders if the message cannot be found in the folder given in the link.
  1225. Searching all folders is very efficient with one of the search engines
  1226. supported by MH-E, but will be slow with pick."
  1227. :group 'org-link-follow
  1228. :type 'boolean)
  1229. (defgroup org-remember nil
  1230. "Options concerning interaction with remember.el."
  1231. :tag "Org Remember"
  1232. :group 'org)
  1233. (defcustom org-directory "~/org"
  1234. "Directory with org files.
  1235. This directory will be used as default to prompt for org files.
  1236. Used by the hooks for remember.el."
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file "~/.notes"
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-remember
  1246. :type '(choice
  1247. (const :tag "Default from remember-data-file" nil)
  1248. file))
  1249. (defcustom org-remember-store-without-prompt t
  1250. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1251. In this case, you need `C-u C-c C-c' to get the prompts for
  1252. note file and headline.
  1253. When this variable is nil, `C-c C-c' give you the prompts, and
  1254. `C-u C-c C-c' trigger the fasttrack."
  1255. :group 'org-remember
  1256. :type 'boolean)
  1257. (defcustom org-remember-interactive-interface 'refile
  1258. "The interface to be used for interactive filing of remember notes.
  1259. This is only used when the interactive mode for selecting a filing
  1260. location is used (see the variable `org-remember-store-without-prompt').
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion.
  1267. refile Use the refile interface, and offer headlines,
  1268. possibly from different buffers."
  1269. :group 'org-remember
  1270. :type '(choice
  1271. (const :tag "Refile" refile)
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-interface 'outline
  1275. "The default interface to be used for `org-goto'.
  1276. Allowed vaues are:
  1277. outline The interface shows an outline of the relevant file
  1278. and the correct heading is found by moving through
  1279. the outline or by searching with incremental search.
  1280. outline-path-completion Headlines in the current buffer are offered via
  1281. completion."
  1282. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1283. :type '(choice
  1284. (const :tag "Outline" outline)
  1285. (const :tag "Outline-path-completion" outline-path-completion)))
  1286. (defcustom org-remember-default-headline ""
  1287. "The headline that should be the default location in the notes file.
  1288. When filing remember notes, the cursor will start at that position.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type 'string)
  1293. (defcustom org-remember-templates nil
  1294. "Templates for the creation of remember buffers.
  1295. When nil, just let remember make the buffer.
  1296. When not nil, this is a list of 5-element lists. In each entry, the first
  1297. element is the name of the template, which should be a single short word.
  1298. The second element is a character, a unique key to select this template.
  1299. The third element is the template. The fourth element is optional and can
  1300. specify a destination file for remember items created with this template.
  1301. The default file is given by `org-default-notes-file'. An optional fifth
  1302. element can specify the headline in that file that should be offered
  1303. first when the user is asked to file the entry. The default headline is
  1304. given in the variable `org-remember-default-headline'.
  1305. The template specifies the structure of the remember buffer. It should have
  1306. a first line starting with a star, to act as the org-mode headline.
  1307. Furthermore, the following %-escapes will be replaced with content:
  1308. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1309. A default value and a completion table ca be specified like this:
  1310. %^{prompt|default|completion2|completion3|...}
  1311. %t time stamp, date only
  1312. %T time stamp with date and time
  1313. %u, %U like the above, but inactive time stamps
  1314. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1315. You may define a prompt like %^{Please specify birthday}t
  1316. %n user name (taken from `user-full-name')
  1317. %a annotation, normally the link created with org-store-link
  1318. %i initial content, the region active. If %i is indented,
  1319. the entire inserted text will be indented as well.
  1320. %c content of the clipboard, or current kill ring head
  1321. %^g prompt for tags, with completion on tags in target file
  1322. %^G prompt for tags, with completion all tags in all agenda files
  1323. %:keyword specific information for certain link types, see below
  1324. %[pathname] insert the contents of the file given by `pathname'
  1325. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1326. %! Store this note immediately after filling the template
  1327. %? After completing the template, position cursor here.
  1328. Apart from these general escapes, you can access information specific to the
  1329. link type that is created. For example, calling `remember' in emails or gnus
  1330. will record the author and the subject of the message, which you can access
  1331. with %:author and %:subject, respectively. Here is a complete list of what
  1332. is recorded for each link type.
  1333. Link type | Available information
  1334. -------------------+------------------------------------------------------
  1335. bbdb | %:type %:name %:company
  1336. vm, wl, mh, rmail | %:type %:subject %:message-id
  1337. | %:from %:fromname %:fromaddress
  1338. | %:to %:toname %:toaddress
  1339. | %:fromto (either \"to NAME\" or \"from NAME\")
  1340. gnus | %:group, for messages also all email fields
  1341. w3, w3m | %:type %:url
  1342. info | %:type %:file %:node
  1343. calendar | %:type %:date"
  1344. :group 'org-remember
  1345. :get (lambda (var) ; Make sure all entries have 5 elements
  1346. (mapcar (lambda (x)
  1347. (if (not (stringp (car x))) (setq x (cons "" x)))
  1348. (cond ((= (length x) 4) (append x '("")))
  1349. ((= (length x) 3) (append x '("" "")))
  1350. (t x)))
  1351. (default-value var)))
  1352. :type '(repeat
  1353. :tag "enabled"
  1354. (list :value ("" ?a "\n" nil nil)
  1355. (string :tag "Name")
  1356. (character :tag "Selection Key")
  1357. (string :tag "Template")
  1358. (choice
  1359. (file :tag "Destination file")
  1360. (const :tag "Prompt for file" nil))
  1361. (choice
  1362. (string :tag "Destination headline")
  1363. (const :tag "Selection interface for heading")))))
  1364. (defcustom org-reverse-note-order nil
  1365. "Non-nil means, store new notes at the beginning of a file or entry.
  1366. When nil, new notes will be filed to the end of a file or entry.
  1367. This can also be a list with cons cells of regular expressions that
  1368. are matched against file names, and values."
  1369. :group 'org-remember
  1370. :type '(choice
  1371. (const :tag "Reverse always" t)
  1372. (const :tag "Reverse never" nil)
  1373. (repeat :tag "By file name regexp"
  1374. (cons regexp boolean))))
  1375. (defcustom org-refile-targets nil
  1376. "Targets for refiling entries with \\[org-refile].
  1377. This is list of cons cells. Each cell contains:
  1378. - a specification of the files to be considered, either a list of files,
  1379. or a symbol whose function or value fields will be used to retrieve
  1380. a file name or a list of file names. Nil means, refile to a different
  1381. heading in the current buffer.
  1382. - A specification of how to find candidate refile targets. This may be
  1383. any of
  1384. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1385. This tag has to be present in all target headlines, inheritance will
  1386. not be considered.
  1387. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1388. todo keyword.
  1389. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1390. headlines that are refiling targets.
  1391. - a cons cell (:level . N). Any headline of level N is considered a target.
  1392. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1393. ;; FIXME: what if there are a var and func with same name???
  1394. :group 'org-remember
  1395. :type '(repeat
  1396. (cons
  1397. (choice :value org-agenda-files
  1398. (const :tag "All agenda files" org-agenda-files)
  1399. (const :tag "Current buffer" nil)
  1400. (function) (variable) (file))
  1401. (choice :tag "Identify target headline by"
  1402. (cons :tag "Specific tag" (const :tag) (string))
  1403. (cons :tag "TODO keyword" (const :todo) (string))
  1404. (cons :tag "Regular expression" (const :regexp) (regexp))
  1405. (cons :tag "Level number" (const :level) (integer))
  1406. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1407. (defcustom org-refile-use-outline-path nil
  1408. "Non-nil means, provide refile targets as paths.
  1409. So a level 3 headline will be available as level1/level2/level3.
  1410. When the value is `file', also include the file name (without directory)
  1411. into the path. When `full-file-path', include the full file path."
  1412. :group 'org-remember
  1413. :type '(choice
  1414. (const :tag "Not" nil)
  1415. (const :tag "Yes" t)
  1416. (const :tag "Start with file name" file)
  1417. (const :tag "Start with full file path" full-file-path)))
  1418. (defgroup org-todo nil
  1419. "Options concerning TODO items in Org-mode."
  1420. :tag "Org TODO"
  1421. :group 'org)
  1422. (defgroup org-progress nil
  1423. "Options concerning Progress logging in Org-mode."
  1424. :tag "Org Progress"
  1425. :group 'org-time)
  1426. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1427. "List of TODO entry keyword sequences and their interpretation.
  1428. \\<org-mode-map>This is a list of sequences.
  1429. Each sequence starts with a symbol, either `sequence' or `type',
  1430. indicating if the keywords should be interpreted as a sequence of
  1431. action steps, or as different types of TODO items. The first
  1432. keywords are states requiring action - these states will select a headline
  1433. for inclusion into the global TODO list Org-mode produces. If one of
  1434. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1435. signify that no further action is necessary. If \"|\" is not found,
  1436. the last keyword is treated as the only DONE state of the sequence.
  1437. The command \\[org-todo] cycles an entry through these states, and one
  1438. additional state where no keyword is present. For details about this
  1439. cycling, see the manual.
  1440. TODO keywords and interpretation can also be set on a per-file basis with
  1441. the special #+SEQ_TODO and #+TYP_TODO lines.
  1442. 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. (let (link cpltxt desc description search txt)
  10866. (cond
  10867. ((run-hook-with-args-until-success 'org-store-link-functions)
  10868. (setq link (plist-get org-store-link-plist :link)
  10869. desc (or (plist-get org-store-link-plist :description) link)))
  10870. ((eq major-mode 'bbdb-mode)
  10871. (let ((name (bbdb-record-name (bbdb-current-record)))
  10872. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10873. (setq cpltxt (concat "bbdb:" (or name company))
  10874. link (org-make-link cpltxt))
  10875. (org-store-link-props :type "bbdb" :name name :company company)))
  10876. ((eq major-mode 'Info-mode)
  10877. (setq link (org-make-link "info:"
  10878. (file-name-nondirectory Info-current-file)
  10879. ":" Info-current-node))
  10880. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10881. ":" Info-current-node))
  10882. (org-store-link-props :type "info" :file Info-current-file
  10883. :node Info-current-node))
  10884. ((eq major-mode 'calendar-mode)
  10885. (let ((cd (calendar-cursor-to-date)))
  10886. (setq link
  10887. (format-time-string
  10888. (car org-time-stamp-formats)
  10889. (apply 'encode-time
  10890. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10891. nil nil nil))))
  10892. (org-store-link-props :type "calendar" :date cd)))
  10893. ((or (eq major-mode 'vm-summary-mode)
  10894. (eq major-mode 'vm-presentation-mode))
  10895. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10896. (vm-follow-summary-cursor)
  10897. (save-excursion
  10898. (vm-select-folder-buffer)
  10899. (let* ((message (car vm-message-pointer))
  10900. (folder buffer-file-name)
  10901. (subject (vm-su-subject message))
  10902. (to (vm-get-header-contents message "To"))
  10903. (from (vm-get-header-contents message "From"))
  10904. (message-id (vm-su-message-id message)))
  10905. (org-store-link-props :type "vm" :from from :to to :subject subject
  10906. :message-id message-id)
  10907. (setq message-id (org-remove-angle-brackets message-id))
  10908. (setq folder (abbreviate-file-name folder))
  10909. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10910. folder)
  10911. (setq folder (replace-match "" t t folder)))
  10912. (setq cpltxt (org-email-link-description))
  10913. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10914. ((eq major-mode 'wl-summary-mode)
  10915. (let* ((msgnum (wl-summary-message-number))
  10916. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10917. msgnum 'message-id))
  10918. (wl-message-entity
  10919. (if (fboundp 'elmo-message-entity)
  10920. (elmo-message-entity
  10921. wl-summary-buffer-elmo-folder msgnum)
  10922. (elmo-msgdb-overview-get-entity
  10923. msgnum (wl-summary-buffer-msgdb))))
  10924. (from (wl-summary-line-from))
  10925. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10926. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10927. (wl-summary-line-subject))))
  10928. (org-store-link-props :type "wl" :from from :to to
  10929. :subject subject :message-id message-id)
  10930. (setq message-id (org-remove-angle-brackets message-id))
  10931. (setq cpltxt (org-email-link-description))
  10932. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10933. "#" message-id))))
  10934. ((or (equal major-mode 'mh-folder-mode)
  10935. (equal major-mode 'mh-show-mode))
  10936. (let ((from (org-mhe-get-header "From:"))
  10937. (to (org-mhe-get-header "To:"))
  10938. (message-id (org-mhe-get-header "Message-Id:"))
  10939. (subject (org-mhe-get-header "Subject:")))
  10940. (org-store-link-props :type "mh" :from from :to to
  10941. :subject subject :message-id message-id)
  10942. (setq cpltxt (org-email-link-description))
  10943. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10944. (org-remove-angle-brackets message-id)))))
  10945. ((or (eq major-mode 'rmail-mode)
  10946. (eq major-mode 'rmail-summary-mode))
  10947. (save-window-excursion
  10948. (save-restriction
  10949. (when (eq major-mode 'rmail-summary-mode)
  10950. (rmail-show-message rmail-current-message))
  10951. (rmail-narrow-to-non-pruned-header)
  10952. (let ((folder buffer-file-name)
  10953. (message-id (mail-fetch-field "message-id"))
  10954. (from (mail-fetch-field "from"))
  10955. (to (mail-fetch-field "to"))
  10956. (subject (mail-fetch-field "subject")))
  10957. (org-store-link-props
  10958. :type "rmail" :from from :to to
  10959. :subject subject :message-id message-id)
  10960. (setq message-id (org-remove-angle-brackets message-id))
  10961. (setq cpltxt (org-email-link-description))
  10962. (setq link (org-make-link "rmail:" folder "#" message-id)))
  10963. (rmail-show-message rmail-current-message))))
  10964. ((eq major-mode 'gnus-group-mode)
  10965. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10966. (gnus-group-group-name)) ; version
  10967. ((fboundp 'gnus-group-name)
  10968. (gnus-group-name))
  10969. (t "???"))))
  10970. (unless group (error "Not on a group"))
  10971. (org-store-link-props :type "gnus" :group group)
  10972. (setq cpltxt (concat
  10973. (if (org-xor arg org-usenet-links-prefer-google)
  10974. "http://groups.google.com/groups?group="
  10975. "gnus:")
  10976. group)
  10977. link (org-make-link cpltxt))))
  10978. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10979. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10980. (let* ((group gnus-newsgroup-name)
  10981. (article (gnus-summary-article-number))
  10982. (header (gnus-summary-article-header article))
  10983. (from (mail-header-from header))
  10984. (message-id (mail-header-id header))
  10985. (date (mail-header-date header))
  10986. (subject (gnus-summary-subject-string)))
  10987. (org-store-link-props :type "gnus" :from from :subject subject
  10988. :message-id message-id :group group)
  10989. (setq cpltxt (org-email-link-description))
  10990. (if (org-xor arg org-usenet-links-prefer-google)
  10991. (setq link
  10992. (concat
  10993. cpltxt "\n "
  10994. (format "http://groups.google.com/groups?as_umsgid=%s"
  10995. (org-fixup-message-id-for-http message-id))))
  10996. (setq link (org-make-link "gnus:" group
  10997. "#" (number-to-string article))))))
  10998. ((eq major-mode 'w3-mode)
  10999. (setq cpltxt (url-view-url t)
  11000. link (org-make-link cpltxt))
  11001. (org-store-link-props :type "w3" :url (url-view-url t)))
  11002. ((eq major-mode 'w3m-mode)
  11003. (setq cpltxt (or w3m-current-title w3m-current-url)
  11004. link (org-make-link w3m-current-url))
  11005. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11006. ((setq search (run-hook-with-args-until-success
  11007. 'org-create-file-search-functions))
  11008. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11009. "::" search))
  11010. (setq cpltxt (or description link)))
  11011. ((eq major-mode 'image-mode)
  11012. (setq cpltxt (concat "file:"
  11013. (abbreviate-file-name buffer-file-name))
  11014. link (org-make-link cpltxt))
  11015. (org-store-link-props :type "image" :file buffer-file-name))
  11016. ((eq major-mode 'dired-mode)
  11017. ;; link to the file in the current line
  11018. (setq cpltxt (concat "file:"
  11019. (abbreviate-file-name
  11020. (expand-file-name
  11021. (dired-get-filename nil t))))
  11022. link (org-make-link cpltxt)))
  11023. ((and buffer-file-name (org-mode-p))
  11024. ;; Just link to current headline
  11025. (setq cpltxt (concat "file:"
  11026. (abbreviate-file-name buffer-file-name)))
  11027. ;; Add a context search string
  11028. (when (org-xor org-context-in-file-links arg)
  11029. ;; Check if we are on a target
  11030. (if (org-in-regexp "<<\\(.*?\\)>>")
  11031. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11032. (setq txt (cond
  11033. ((org-on-heading-p) nil)
  11034. ((org-region-active-p)
  11035. (buffer-substring (region-beginning) (region-end)))
  11036. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11037. (when (or (null txt) (string-match "\\S-" txt))
  11038. (setq cpltxt
  11039. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11040. desc "NONE"))))
  11041. (if (string-match "::\\'" cpltxt)
  11042. (setq cpltxt (substring cpltxt 0 -2)))
  11043. (setq link (org-make-link cpltxt)))
  11044. ((buffer-file-name (buffer-base-buffer))
  11045. ;; Just link to this file here.
  11046. (setq cpltxt (concat "file:"
  11047. (abbreviate-file-name
  11048. (buffer-file-name (buffer-base-buffer)))))
  11049. ;; Add a context string
  11050. (when (org-xor org-context-in-file-links arg)
  11051. (setq txt (if (org-region-active-p)
  11052. (buffer-substring (region-beginning) (region-end))
  11053. (buffer-substring (point-at-bol) (point-at-eol))))
  11054. ;; Only use search option if there is some text.
  11055. (when (string-match "\\S-" txt)
  11056. (setq cpltxt
  11057. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11058. desc "NONE")))
  11059. (setq link (org-make-link cpltxt)))
  11060. ((interactive-p)
  11061. (error "Cannot link to a buffer which is not visiting a file"))
  11062. (t (setq link nil)))
  11063. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11064. (setq link (or link cpltxt)
  11065. desc (or desc cpltxt))
  11066. (if (equal desc "NONE") (setq desc nil))
  11067. (if (and (interactive-p) link)
  11068. (progn
  11069. (setq org-stored-links
  11070. (cons (list link desc) org-stored-links))
  11071. (message "Stored: %s" (or desc link)))
  11072. (and link (org-make-link-string link desc)))))
  11073. (defun org-store-link-props (&rest plist)
  11074. "Store link properties, extract names and addresses."
  11075. (let (x adr)
  11076. (when (setq x (plist-get plist :from))
  11077. (setq adr (mail-extract-address-components x))
  11078. (plist-put plist :fromname (car adr))
  11079. (plist-put plist :fromaddress (nth 1 adr)))
  11080. (when (setq x (plist-get plist :to))
  11081. (setq adr (mail-extract-address-components x))
  11082. (plist-put plist :toname (car adr))
  11083. (plist-put plist :toaddress (nth 1 adr))))
  11084. (let ((from (plist-get plist :from))
  11085. (to (plist-get plist :to)))
  11086. (when (and from to org-from-is-user-regexp)
  11087. (plist-put plist :fromto
  11088. (if (string-match org-from-is-user-regexp from)
  11089. (concat "to %t")
  11090. (concat "from %f")))))
  11091. (setq org-store-link-plist plist))
  11092. (defun org-email-link-description (&optional fmt)
  11093. "Return the description part of an email link.
  11094. This takes information from `org-store-link-plist' and formats it
  11095. according to FMT (default from `org-email-link-description-format')."
  11096. (setq fmt (or fmt org-email-link-description-format))
  11097. (let* ((p org-store-link-plist)
  11098. (to (plist-get p :toaddress))
  11099. (from (plist-get p :fromaddress))
  11100. (table
  11101. (list
  11102. (cons "%c" (plist-get p :fromto))
  11103. (cons "%F" (plist-get p :from))
  11104. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11105. (cons "%T" (plist-get p :to))
  11106. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11107. (cons "%s" (plist-get p :subject))
  11108. (cons "%m" (plist-get p :message-id)))))
  11109. (when (string-match "%c" fmt)
  11110. ;; Check if the user wrote this message
  11111. (if (and org-from-is-user-regexp from to
  11112. (save-match-data (string-match org-from-is-user-regexp from)))
  11113. (setq fmt (replace-match "to %t" t t fmt))
  11114. (setq fmt (replace-match "from %f" t t fmt))))
  11115. (org-replace-escapes fmt table)))
  11116. (defun org-make-org-heading-search-string (&optional string heading)
  11117. "Make search string for STRING or current headline."
  11118. (interactive)
  11119. (let ((s (or string (org-get-heading))))
  11120. (unless (and string (not heading))
  11121. ;; We are using a headline, clean up garbage in there.
  11122. (if (string-match org-todo-regexp s)
  11123. (setq s (replace-match "" t t s)))
  11124. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11125. (setq s (replace-match "" t t s)))
  11126. (setq s (org-trim s))
  11127. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11128. org-comment-string "\\)") s)
  11129. (setq s (replace-match "" t t s)))
  11130. (while (string-match org-ts-regexp s)
  11131. (setq s (replace-match "" t t s))))
  11132. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11133. (setq s (replace-match " " t t s)))
  11134. (or string (setq s (concat "*" s))) ; Add * for headlines
  11135. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11136. (defun org-make-link (&rest strings)
  11137. "Concatenate STRINGS."
  11138. (apply 'concat strings))
  11139. (defun org-make-link-string (link &optional description)
  11140. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11141. (unless (string-match "\\S-" link)
  11142. (error "Empty link"))
  11143. (when (stringp description)
  11144. ;; Remove brackets from the description, they are fatal.
  11145. (while (string-match "\\[" description)
  11146. (setq description (replace-match "{" t t description)))
  11147. (while (string-match "\\]" description)
  11148. (setq description (replace-match "}" t t description))))
  11149. (when (equal (org-link-escape link) description)
  11150. ;; No description needed, it is identical
  11151. (setq description nil))
  11152. (when (and (not description)
  11153. (not (equal link (org-link-escape link))))
  11154. (setq description link))
  11155. (concat "[[" (org-link-escape link) "]"
  11156. (if description (concat "[" description "]") "")
  11157. "]"))
  11158. (defconst org-link-escape-chars
  11159. '((?\ . "%20")
  11160. (?\[ . "%5B")
  11161. (?\] . "%5D")
  11162. (?\340 . "%E0") ; `a
  11163. (?\342 . "%E2") ; ^a
  11164. (?\347 . "%E7") ; ,c
  11165. (?\350 . "%E8") ; `e
  11166. (?\351 . "%E9") ; 'e
  11167. (?\352 . "%EA") ; ^e
  11168. (?\356 . "%EE") ; ^i
  11169. (?\364 . "%F4") ; ^o
  11170. (?\371 . "%F9") ; `u
  11171. (?\373 . "%FB") ; ^u
  11172. (?\; . "%3B")
  11173. (?? . "%3F")
  11174. (?= . "%3D")
  11175. (?+ . "%2B")
  11176. )
  11177. "Association list of escapes for some characters problematic in links.
  11178. This is the list that is used for internal purposes.")
  11179. (defconst org-link-escape-chars-browser
  11180. '((?\ . "%20")) ; 32 for the SPC char
  11181. "Association list of escapes for some characters problematic in links.
  11182. This is the list that is used before handing over to the browser.")
  11183. (defun org-link-escape (text &optional table)
  11184. "Escape charaters in TEXT that are problematic for links."
  11185. (setq table (or table org-link-escape-chars))
  11186. (when text
  11187. (let ((re (mapconcat (lambda (x) (regexp-quote
  11188. (char-to-string (car x))))
  11189. table "\\|")))
  11190. (while (string-match re text)
  11191. (setq text
  11192. (replace-match
  11193. (cdr (assoc (string-to-char (match-string 0 text))
  11194. table))
  11195. t t text)))
  11196. text)))
  11197. (defun org-link-unescape (text &optional table)
  11198. "Reverse the action of `org-link-escape'."
  11199. (setq table (or table org-link-escape-chars))
  11200. (when text
  11201. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11202. table "\\|")))
  11203. (while (string-match re text)
  11204. (setq text
  11205. (replace-match
  11206. (char-to-string (car (rassoc (match-string 0 text) table)))
  11207. t t text)))
  11208. text)))
  11209. (defun org-xor (a b)
  11210. "Exclusive or."
  11211. (if a (not b) b))
  11212. (defun org-get-header (header)
  11213. "Find a header field in the current buffer."
  11214. (save-excursion
  11215. (goto-char (point-min))
  11216. (let ((case-fold-search t) s)
  11217. (cond
  11218. ((eq header 'from)
  11219. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11220. (setq s (match-string 1)))
  11221. (while (string-match "\"" s)
  11222. (setq s (replace-match "" t t s)))
  11223. (if (string-match "[<(].*" s)
  11224. (setq s (replace-match "" t t s))))
  11225. ((eq header 'message-id)
  11226. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11227. (setq s (match-string 1))))
  11228. ((eq header 'subject)
  11229. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11230. (setq s (match-string 1)))))
  11231. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11232. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11233. s)))
  11234. (defun org-fixup-message-id-for-http (s)
  11235. "Replace special characters in a message id, so it can be used in an http query."
  11236. (while (string-match "<" s)
  11237. (setq s (replace-match "%3C" t t s)))
  11238. (while (string-match ">" s)
  11239. (setq s (replace-match "%3E" t t s)))
  11240. (while (string-match "@" s)
  11241. (setq s (replace-match "%40" t t s)))
  11242. s)
  11243. ;;;###autoload
  11244. (defun org-insert-link-global ()
  11245. "Insert a link like Org-mode does.
  11246. This command can be called in any mode to insert a link in Org-mode syntax."
  11247. (interactive)
  11248. (org-run-like-in-org-mode 'org-insert-link))
  11249. (defun org-insert-link (&optional complete-file)
  11250. "Insert a link. At the prompt, enter the link.
  11251. Completion can be used to select a link previously stored with
  11252. `org-store-link'. When the empty string is entered (i.e. if you just
  11253. press RET at the prompt), the link defaults to the most recently
  11254. stored link. As SPC triggers completion in the minibuffer, you need to
  11255. use M-SPC or C-q SPC to force the insertion of a space character.
  11256. You will also be prompted for a description, and if one is given, it will
  11257. be displayed in the buffer instead of the link.
  11258. If there is already a link at point, this command will allow you to edit link
  11259. and description parts.
  11260. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11261. selected using completion. The path to the file will be relative to
  11262. the current directory if the file is in the current directory or a
  11263. subdirectory. Otherwise, the link will be the absolute path as
  11264. completed in the minibuffer (i.e. normally ~/path/to/file).
  11265. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11266. is in the current directory or below.
  11267. With three \\[universal-argument] prefixes, negate the meaning of
  11268. `org-keep-stored-link-after-insertion'."
  11269. (interactive "P")
  11270. (let* ((wcf (current-window-configuration))
  11271. (region (if (org-region-active-p)
  11272. (buffer-substring (region-beginning) (region-end))))
  11273. (remove (and region (list (region-beginning) (region-end))))
  11274. (desc region)
  11275. tmphist ; byte-compile incorrectly complains about this
  11276. link entry file)
  11277. (cond
  11278. ((org-in-regexp org-bracket-link-regexp 1)
  11279. ;; We do have a link at point, and we are going to edit it.
  11280. (setq remove (list (match-beginning 0) (match-end 0)))
  11281. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11282. (setq link (read-string "Link: "
  11283. (org-link-unescape
  11284. (org-match-string-no-properties 1)))))
  11285. ((or (org-in-regexp org-angle-link-re)
  11286. (org-in-regexp org-plain-link-re))
  11287. ;; Convert to bracket link
  11288. (setq remove (list (match-beginning 0) (match-end 0))
  11289. link (read-string "Link: "
  11290. (org-remove-angle-brackets (match-string 0)))))
  11291. ((equal complete-file '(4))
  11292. ;; Completing read for file names.
  11293. (setq file (read-file-name "File: "))
  11294. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11295. (pwd1 (file-name-as-directory (abbreviate-file-name
  11296. (expand-file-name ".")))))
  11297. (cond
  11298. ((equal complete-file '(16))
  11299. (setq link (org-make-link
  11300. "file:"
  11301. (abbreviate-file-name (expand-file-name file)))))
  11302. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11303. (setq link (org-make-link "file:" (match-string 1 file))))
  11304. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11305. (expand-file-name file))
  11306. (setq link (org-make-link
  11307. "file:" (match-string 1 (expand-file-name file)))))
  11308. (t (setq link (org-make-link "file:" file))))))
  11309. (t
  11310. ;; Read link, with completion for stored links.
  11311. (with-output-to-temp-buffer "*Org Links*"
  11312. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11313. (when org-stored-links
  11314. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11315. (princ (mapconcat
  11316. (lambda (x)
  11317. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11318. (reverse org-stored-links) "\n"))))
  11319. (let ((cw (selected-window)))
  11320. (select-window (get-buffer-window "*Org Links*"))
  11321. (shrink-window-if-larger-than-buffer)
  11322. (setq truncate-lines t)
  11323. (select-window cw))
  11324. ;; Fake a link history, containing the stored links.
  11325. (setq tmphist (append (mapcar 'car org-stored-links)
  11326. org-insert-link-history))
  11327. (unwind-protect
  11328. (setq link (org-completing-read
  11329. "Link: "
  11330. (append
  11331. (mapcar (lambda (x) (list (concat (car x) ":")))
  11332. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11333. (mapcar (lambda (x) (list (concat x ":")))
  11334. org-link-types))
  11335. nil nil nil
  11336. 'tmphist
  11337. (or (car (car org-stored-links)))))
  11338. (set-window-configuration wcf)
  11339. (kill-buffer "*Org Links*"))
  11340. (setq entry (assoc link org-stored-links))
  11341. (or entry (push link org-insert-link-history))
  11342. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11343. (not org-keep-stored-link-after-insertion))
  11344. (setq org-stored-links (delq (assoc link org-stored-links)
  11345. org-stored-links)))
  11346. (setq desc (or desc (nth 1 entry)))))
  11347. (if (string-match org-plain-link-re link)
  11348. ;; URL-like link, normalize the use of angular brackets.
  11349. (setq link (org-make-link (org-remove-angle-brackets link))))
  11350. ;; Check if we are linking to the current file with a search option
  11351. ;; If yes, simplify the link by using only the search option.
  11352. (when (and buffer-file-name
  11353. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11354. (let* ((path (match-string 1 link))
  11355. (case-fold-search nil)
  11356. (search (match-string 2 link)))
  11357. (save-match-data
  11358. (if (equal (file-truename buffer-file-name) (file-truename path))
  11359. ;; We are linking to this same file, with a search option
  11360. (setq link search)))))
  11361. ;; Check if we can/should use a relative path. If yes, simplify the link
  11362. (when (string-match "\\<file:\\(.*\\)" link)
  11363. (let* ((path (match-string 1 link))
  11364. (origpath path)
  11365. (case-fold-search nil))
  11366. (cond
  11367. ((eq org-link-file-path-type 'absolute)
  11368. (setq path (abbreviate-file-name (expand-file-name path))))
  11369. ((eq org-link-file-path-type 'noabbrev)
  11370. (setq path (expand-file-name path)))
  11371. ((eq org-link-file-path-type 'relative)
  11372. (setq path (file-relative-name path)))
  11373. (t
  11374. (save-match-data
  11375. (if (string-match (concat "^" (regexp-quote
  11376. (file-name-as-directory
  11377. (expand-file-name "."))))
  11378. (expand-file-name path))
  11379. ;; We are linking a file with relative path name.
  11380. (setq path (substring (expand-file-name path)
  11381. (match-end 0)))))))
  11382. (setq link (concat "file:" path))
  11383. (if (equal desc origpath)
  11384. (setq desc path))))
  11385. (setq desc (read-string "Description: " desc))
  11386. (unless (string-match "\\S-" desc) (setq desc nil))
  11387. (if remove (apply 'delete-region remove))
  11388. (insert (org-make-link-string link desc))))
  11389. (defun org-completing-read (&rest args)
  11390. (let ((minibuffer-local-completion-map
  11391. (copy-keymap minibuffer-local-completion-map)))
  11392. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11393. (apply 'completing-read args)))
  11394. ;;; Opening/following a link
  11395. (defvar org-link-search-failed nil)
  11396. (defun org-next-link ()
  11397. "Move forward to the next link.
  11398. If the link is in hidden text, expose it."
  11399. (interactive)
  11400. (when (and org-link-search-failed (eq this-command last-command))
  11401. (goto-char (point-min))
  11402. (message "Link search wrapped back to beginning of buffer"))
  11403. (setq org-link-search-failed nil)
  11404. (let* ((pos (point))
  11405. (ct (org-context))
  11406. (a (assoc :link ct)))
  11407. (if a (goto-char (nth 2 a)))
  11408. (if (re-search-forward org-any-link-re nil t)
  11409. (progn
  11410. (goto-char (match-beginning 0))
  11411. (if (org-invisible-p) (org-show-context)))
  11412. (goto-char pos)
  11413. (setq org-link-search-failed t)
  11414. (error "No further link found"))))
  11415. (defun org-previous-link ()
  11416. "Move backward to the previous link.
  11417. If the link is in hidden text, expose it."
  11418. (interactive)
  11419. (when (and org-link-search-failed (eq this-command last-command))
  11420. (goto-char (point-max))
  11421. (message "Link search wrapped back to end of buffer"))
  11422. (setq org-link-search-failed nil)
  11423. (let* ((pos (point))
  11424. (ct (org-context))
  11425. (a (assoc :link ct)))
  11426. (if a (goto-char (nth 1 a)))
  11427. (if (re-search-backward org-any-link-re nil t)
  11428. (progn
  11429. (goto-char (match-beginning 0))
  11430. (if (org-invisible-p) (org-show-context)))
  11431. (goto-char pos)
  11432. (setq org-link-search-failed t)
  11433. (error "No further link found"))))
  11434. (defun org-find-file-at-mouse (ev)
  11435. "Open file link or URL at mouse."
  11436. (interactive "e")
  11437. (mouse-set-point ev)
  11438. (org-open-at-point 'in-emacs))
  11439. (defun org-open-at-mouse (ev)
  11440. "Open file link or URL at mouse."
  11441. (interactive "e")
  11442. (mouse-set-point ev)
  11443. (org-open-at-point))
  11444. (defvar org-window-config-before-follow-link nil
  11445. "The window configuration before following a link.
  11446. This is saved in case the need arises to restore it.")
  11447. (defvar org-open-link-marker (make-marker)
  11448. "Marker pointing to the location where `org-open-at-point; was called.")
  11449. ;;;###autoload
  11450. (defun org-open-at-point-global ()
  11451. "Follow a link like Org-mode does.
  11452. This command can be called in any mode to follow a link that has
  11453. Org-mode syntax."
  11454. (interactive)
  11455. (org-run-like-in-org-mode 'org-open-at-point))
  11456. (defun org-open-at-point (&optional in-emacs)
  11457. "Open link at or after point.
  11458. If there is no link at point, this function will search forward up to
  11459. the end of the current subtree.
  11460. Normally, files will be opened by an appropriate application. If the
  11461. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11462. (interactive "P")
  11463. (catch 'abort
  11464. (move-marker org-open-link-marker (point))
  11465. (setq org-window-config-before-follow-link (current-window-configuration))
  11466. (org-remove-occur-highlights nil nil t)
  11467. (if (org-at-timestamp-p t)
  11468. (org-follow-timestamp-link)
  11469. (let (type path link line search (pos (point)))
  11470. (catch 'match
  11471. (save-excursion
  11472. (skip-chars-forward "^]\n\r")
  11473. (when (org-in-regexp org-bracket-link-regexp)
  11474. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11475. (while (string-match " *\n *" link)
  11476. (setq link (replace-match " " t t link)))
  11477. (setq link (org-link-expand-abbrev link))
  11478. (if (string-match org-link-re-with-space2 link)
  11479. (setq type (match-string 1 link) path (match-string 2 link))
  11480. (setq type "thisfile" path link))
  11481. (throw 'match t)))
  11482. (when (get-text-property (point) 'org-linked-text)
  11483. (setq type "thisfile"
  11484. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11485. (1+ (point)) (point))
  11486. path (buffer-substring
  11487. (previous-single-property-change pos 'org-linked-text)
  11488. (next-single-property-change pos 'org-linked-text)))
  11489. (throw 'match t))
  11490. (save-excursion
  11491. (when (or (org-in-regexp org-angle-link-re)
  11492. (org-in-regexp org-plain-link-re))
  11493. (setq type (match-string 1) path (match-string 2))
  11494. (throw 'match t)))
  11495. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11496. (setq type "tree-match"
  11497. path (match-string 1))
  11498. (throw 'match t))
  11499. (save-excursion
  11500. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11501. (setq type "tags"
  11502. path (match-string 1))
  11503. (while (string-match ":" path)
  11504. (setq path (replace-match "+" t t path)))
  11505. (throw 'match t))))
  11506. (unless path
  11507. (error "No link found"))
  11508. ;; Remove any trailing spaces in path
  11509. (if (string-match " +\\'" path)
  11510. (setq path (replace-match "" t t path)))
  11511. (cond
  11512. ((assoc type org-link-protocols)
  11513. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11514. ((equal type "mailto")
  11515. (let ((cmd (car org-link-mailto-program))
  11516. (args (cdr org-link-mailto-program)) args1
  11517. (address path) (subject "") a)
  11518. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11519. (setq address (match-string 1 path)
  11520. subject (org-link-escape (match-string 2 path))))
  11521. (while args
  11522. (cond
  11523. ((not (stringp (car args))) (push (pop args) args1))
  11524. (t (setq a (pop args))
  11525. (if (string-match "%a" a)
  11526. (setq a (replace-match address t t a)))
  11527. (if (string-match "%s" a)
  11528. (setq a (replace-match subject t t a)))
  11529. (push a args1))))
  11530. (apply cmd (nreverse args1))))
  11531. ((member type '("http" "https" "ftp" "news"))
  11532. (browse-url (concat type ":" (org-link-escape
  11533. path org-link-escape-chars-browser))))
  11534. ((member type '("message"))
  11535. (browse-url (concat type ":" path)))
  11536. ((string= type "tags")
  11537. (org-tags-view in-emacs path))
  11538. ((string= type "thisfile")
  11539. (if in-emacs
  11540. (switch-to-buffer-other-window
  11541. (org-get-buffer-for-internal-link (current-buffer)))
  11542. (org-mark-ring-push))
  11543. (let ((cmd `(org-link-search
  11544. ,path
  11545. ,(cond ((equal in-emacs '(4)) 'occur)
  11546. ((equal in-emacs '(16)) 'org-occur)
  11547. (t nil))
  11548. ,pos)))
  11549. (condition-case nil (eval cmd)
  11550. (error (progn (widen) (eval cmd))))))
  11551. ((string= type "tree-match")
  11552. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11553. ((string= type "file")
  11554. (if (string-match "::\\([0-9]+\\)\\'" path)
  11555. (setq line (string-to-number (match-string 1 path))
  11556. path (substring path 0 (match-beginning 0)))
  11557. (if (string-match "::\\(.+\\)\\'" path)
  11558. (setq search (match-string 1 path)
  11559. path (substring path 0 (match-beginning 0)))))
  11560. (if (string-match "[*?{]" (file-name-nondirectory path))
  11561. (dired path)
  11562. (org-open-file path in-emacs line search)))
  11563. ((string= type "news")
  11564. (org-follow-gnus-link path))
  11565. ((string= type "bbdb")
  11566. (org-follow-bbdb-link path))
  11567. ((string= type "info")
  11568. (org-follow-info-link path))
  11569. ((string= type "gnus")
  11570. (let (group article)
  11571. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11572. (error "Error in Gnus link"))
  11573. (setq group (match-string 1 path)
  11574. article (match-string 3 path))
  11575. (org-follow-gnus-link group article)))
  11576. ((string= type "vm")
  11577. (let (folder article)
  11578. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11579. (error "Error in VM link"))
  11580. (setq folder (match-string 1 path)
  11581. article (match-string 3 path))
  11582. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11583. (org-follow-vm-link folder article in-emacs)))
  11584. ((string= type "wl")
  11585. (let (folder article)
  11586. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11587. (error "Error in Wanderlust link"))
  11588. (setq folder (match-string 1 path)
  11589. article (match-string 3 path))
  11590. (org-follow-wl-link folder article)))
  11591. ((string= type "mhe")
  11592. (let (folder article)
  11593. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11594. (error "Error in MHE link"))
  11595. (setq folder (match-string 1 path)
  11596. article (match-string 3 path))
  11597. (org-follow-mhe-link folder article)))
  11598. ((string= type "rmail")
  11599. (let (folder article)
  11600. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11601. (error "Error in RMAIL link"))
  11602. (setq folder (match-string 1 path)
  11603. article (match-string 3 path))
  11604. (org-follow-rmail-link folder article)))
  11605. ((string= type "shell")
  11606. (let ((cmd path))
  11607. (if (or (not org-confirm-shell-link-function)
  11608. (funcall org-confirm-shell-link-function
  11609. (format "Execute \"%s\" in shell? "
  11610. (org-add-props cmd nil
  11611. 'face 'org-warning))))
  11612. (progn
  11613. (message "Executing %s" cmd)
  11614. (shell-command cmd))
  11615. (error "Abort"))))
  11616. ((string= type "elisp")
  11617. (let ((cmd path))
  11618. (if (or (not org-confirm-elisp-link-function)
  11619. (funcall org-confirm-elisp-link-function
  11620. (format "Execute \"%s\" as elisp? "
  11621. (org-add-props cmd nil
  11622. 'face 'org-warning))))
  11623. (message "%s => %s" cmd (eval (read cmd)))
  11624. (error "Abort"))))
  11625. (t
  11626. (browse-url-at-point)))))
  11627. (move-marker org-open-link-marker nil)))
  11628. ;;; File search
  11629. (defvar org-create-file-search-functions nil
  11630. "List of functions to construct the right search string for a file link.
  11631. These functions are called in turn with point at the location to
  11632. which the link should point.
  11633. A function in the hook should first test if it would like to
  11634. handle this file type, for example by checking the major-mode or
  11635. the file extension. If it decides not to handle this file, it
  11636. should just return nil to give other functions a chance. If it
  11637. does handle the file, it must return the search string to be used
  11638. when following the link. The search string will be part of the
  11639. file link, given after a double colon, and `org-open-at-point'
  11640. will automatically search for it. If special measures must be
  11641. taken to make the search successful, another function should be
  11642. added to the companion hook `org-execute-file-search-functions',
  11643. which see.
  11644. A function in this hook may also use `setq' to set the variable
  11645. `description' to provide a suggestion for the descriptive text to
  11646. be used for this link when it gets inserted into an Org-mode
  11647. buffer with \\[org-insert-link].")
  11648. (defvar org-execute-file-search-functions nil
  11649. "List of functions to execute a file search triggered by a link.
  11650. Functions added to this hook must accept a single argument, the
  11651. search string that was part of the file link, the part after the
  11652. double colon. The function must first check if it would like to
  11653. handle this search, for example by checking the major-mode or the
  11654. file extension. If it decides not to handle this search, it
  11655. should just return nil to give other functions a chance. If it
  11656. does handle the search, it must return a non-nil value to keep
  11657. other functions from trying.
  11658. Each function can access the current prefix argument through the
  11659. variable `current-prefix-argument'. Note that a single prefix is
  11660. used to force opening a link in Emacs, so it may be good to only
  11661. use a numeric or double prefix to guide the search function.
  11662. In case this is needed, a function in this hook can also restore
  11663. the window configuration before `org-open-at-point' was called using:
  11664. (set-window-configuration org-window-config-before-follow-link)")
  11665. (defun org-link-search (s &optional type avoid-pos)
  11666. "Search for a link search option.
  11667. If S is surrounded by forward slashes, it is interpreted as a
  11668. regular expression. In org-mode files, this will create an `org-occur'
  11669. sparse tree. In ordinary files, `occur' will be used to list matches.
  11670. If the current buffer is in `dired-mode', grep will be used to search
  11671. in all files. If AVOID-POS is given, ignore matches near that position."
  11672. (let ((case-fold-search t)
  11673. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11674. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11675. (append '(("") (" ") ("\t") ("\n"))
  11676. org-emphasis-alist)
  11677. "\\|") "\\)"))
  11678. (pos (point))
  11679. (pre "") (post "")
  11680. words re0 re1 re2 re3 re4 re5 re2a reall)
  11681. (cond
  11682. ;; First check if there are any special
  11683. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11684. ;; Now try the builtin stuff
  11685. ((save-excursion
  11686. (goto-char (point-min))
  11687. (and
  11688. (re-search-forward
  11689. (concat "<<" (regexp-quote s0) ">>") nil t)
  11690. (setq pos (match-beginning 0))))
  11691. ;; There is an exact target for this
  11692. (goto-char pos))
  11693. ((string-match "^/\\(.*\\)/$" s)
  11694. ;; A regular expression
  11695. (cond
  11696. ((org-mode-p)
  11697. (org-occur (match-string 1 s)))
  11698. ;;((eq major-mode 'dired-mode)
  11699. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11700. (t (org-do-occur (match-string 1 s)))))
  11701. (t
  11702. ;; A normal search strings
  11703. (when (equal (string-to-char s) ?*)
  11704. ;; Anchor on headlines, post may include tags.
  11705. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11706. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11707. s (substring s 1)))
  11708. (remove-text-properties
  11709. 0 (length s)
  11710. '(face nil mouse-face nil keymap nil fontified nil) s)
  11711. ;; Make a series of regular expressions to find a match
  11712. (setq words (org-split-string s "[ \n\r\t]+")
  11713. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11714. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11715. "\\)" markers)
  11716. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11717. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11718. re1 (concat pre re2 post)
  11719. re3 (concat pre re4 post)
  11720. re5 (concat pre ".*" re4)
  11721. re2 (concat pre re2)
  11722. re2a (concat pre re2a)
  11723. re4 (concat pre re4)
  11724. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11725. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11726. re5 "\\)"
  11727. ))
  11728. (cond
  11729. ((eq type 'org-occur) (org-occur reall))
  11730. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11731. (t (goto-char (point-min))
  11732. (if (or (org-search-not-self 1 re0 nil t)
  11733. (org-search-not-self 1 re1 nil t)
  11734. (org-search-not-self 1 re2 nil t)
  11735. (org-search-not-self 1 re2a nil t)
  11736. (org-search-not-self 1 re3 nil t)
  11737. (org-search-not-self 1 re4 nil t)
  11738. (org-search-not-self 1 re5 nil t)
  11739. )
  11740. (goto-char (match-beginning 1))
  11741. (goto-char pos)
  11742. (error "No match")))))
  11743. (t
  11744. ;; Normal string-search
  11745. (goto-char (point-min))
  11746. (if (search-forward s nil t)
  11747. (goto-char (match-beginning 0))
  11748. (error "No match"))))
  11749. (and (org-mode-p) (org-show-context 'link-search))))
  11750. (defun org-search-not-self (group &rest args)
  11751. "Execute `re-search-forward', but only accept matches that do not
  11752. enclose the position of `org-open-link-marker'."
  11753. (let ((m org-open-link-marker))
  11754. (catch 'exit
  11755. (while (apply 're-search-forward args)
  11756. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11757. (goto-char (match-end group))
  11758. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11759. (> (match-beginning 0) (marker-position m))
  11760. (< (match-end 0) (marker-position m)))
  11761. (save-match-data
  11762. (or (not (org-in-regexp
  11763. org-bracket-link-analytic-regexp 1))
  11764. (not (match-end 4)) ; no description
  11765. (and (<= (match-beginning 4) (point))
  11766. (>= (match-end 4) (point))))))
  11767. (throw 'exit (point))))))))
  11768. (defun org-get-buffer-for-internal-link (buffer)
  11769. "Return a buffer to be used for displaying the link target of internal links."
  11770. (cond
  11771. ((not org-display-internal-link-with-indirect-buffer)
  11772. buffer)
  11773. ((string-match "(Clone)$" (buffer-name buffer))
  11774. (message "Buffer is already a clone, not making another one")
  11775. ;; we also do not modify visibility in this case
  11776. buffer)
  11777. (t ; make a new indirect buffer for displaying the link
  11778. (let* ((bn (buffer-name buffer))
  11779. (ibn (concat bn "(Clone)"))
  11780. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11781. (with-current-buffer ib (org-overview))
  11782. ib))))
  11783. (defun org-do-occur (regexp &optional cleanup)
  11784. "Call the Emacs command `occur'.
  11785. If CLEANUP is non-nil, remove the printout of the regular expression
  11786. in the *Occur* buffer. This is useful if the regex is long and not useful
  11787. to read."
  11788. (occur regexp)
  11789. (when cleanup
  11790. (let ((cwin (selected-window)) win beg end)
  11791. (when (setq win (get-buffer-window "*Occur*"))
  11792. (select-window win))
  11793. (goto-char (point-min))
  11794. (when (re-search-forward "match[a-z]+" nil t)
  11795. (setq beg (match-end 0))
  11796. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11797. (setq end (1- (match-beginning 0)))))
  11798. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11799. (goto-char (point-min))
  11800. (select-window cwin))))
  11801. ;;; The mark ring for links jumps
  11802. (defvar org-mark-ring nil
  11803. "Mark ring for positions before jumps in Org-mode.")
  11804. (defvar org-mark-ring-last-goto nil
  11805. "Last position in the mark ring used to go back.")
  11806. ;; Fill and close the ring
  11807. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11808. (loop for i from 1 to org-mark-ring-length do
  11809. (push (make-marker) org-mark-ring))
  11810. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11811. org-mark-ring)
  11812. (defun org-mark-ring-push (&optional pos buffer)
  11813. "Put the current position or POS into the mark ring and rotate it."
  11814. (interactive)
  11815. (setq pos (or pos (point)))
  11816. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11817. (move-marker (car org-mark-ring)
  11818. (or pos (point))
  11819. (or buffer (current-buffer)))
  11820. (message "%s"
  11821. (substitute-command-keys
  11822. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11823. (defun org-mark-ring-goto (&optional n)
  11824. "Jump to the previous position in the mark ring.
  11825. With prefix arg N, jump back that many stored positions. When
  11826. called several times in succession, walk through the entire ring.
  11827. Org-mode commands jumping to a different position in the current file,
  11828. or to another Org-mode file, automatically push the old position
  11829. onto the ring."
  11830. (interactive "p")
  11831. (let (p m)
  11832. (if (eq last-command this-command)
  11833. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11834. (setq p org-mark-ring))
  11835. (setq org-mark-ring-last-goto p)
  11836. (setq m (car p))
  11837. (switch-to-buffer (marker-buffer m))
  11838. (goto-char m)
  11839. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11840. (defun org-remove-angle-brackets (s)
  11841. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11842. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11843. s)
  11844. (defun org-add-angle-brackets (s)
  11845. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11846. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11847. s)
  11848. ;;; Following specific links
  11849. (defun org-follow-timestamp-link ()
  11850. (cond
  11851. ((org-at-date-range-p t)
  11852. (let ((org-agenda-start-on-weekday)
  11853. (t1 (match-string 1))
  11854. (t2 (match-string 2)))
  11855. (setq t1 (time-to-days (org-time-string-to-time t1))
  11856. t2 (time-to-days (org-time-string-to-time t2)))
  11857. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11858. ((org-at-timestamp-p t)
  11859. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11860. (substring (match-string 1) 0 10)))
  11861. 1))
  11862. (t (error "This should not happen"))))
  11863. (defun org-follow-bbdb-link (name)
  11864. "Follow a BBDB link to NAME."
  11865. (require 'bbdb)
  11866. (let ((inhibit-redisplay (not debug-on-error))
  11867. (bbdb-electric-p nil))
  11868. (catch 'exit
  11869. ;; Exact match on name
  11870. (bbdb-name (concat "\\`" name "\\'") nil)
  11871. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11872. ;; Exact match on name
  11873. (bbdb-company (concat "\\`" name "\\'") nil)
  11874. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11875. ;; Partial match on name
  11876. (bbdb-name name nil)
  11877. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11878. ;; Partial match on company
  11879. (bbdb-company name nil)
  11880. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11881. ;; General match including network address and notes
  11882. (bbdb name nil)
  11883. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11884. (delete-window (get-buffer-window "*BBDB*"))
  11885. (error "No matching BBDB record")))))
  11886. (defun org-follow-info-link (name)
  11887. "Follow an info file & node link to NAME."
  11888. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11889. (string-match "\\(.*\\)" name))
  11890. (progn
  11891. (require 'info)
  11892. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11893. (Info-find-node (match-string 1 name) (match-string 2 name))
  11894. (Info-find-node (match-string 1 name) "Top")))
  11895. (message "Could not open: %s" name)))
  11896. (defun org-follow-gnus-link (&optional group article)
  11897. "Follow a Gnus link to GROUP and ARTICLE."
  11898. (require 'gnus)
  11899. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11900. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11901. (cond ((and group article)
  11902. (gnus-group-read-group 1 nil group)
  11903. (gnus-summary-goto-article (string-to-number article) nil t))
  11904. (group (gnus-group-jump-to-group group))))
  11905. (defun org-follow-vm-link (&optional folder article readonly)
  11906. "Follow a VM link to FOLDER and ARTICLE."
  11907. (require 'vm)
  11908. (setq article (org-add-angle-brackets article))
  11909. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11910. ;; ange-ftp or efs or tramp access
  11911. (let ((user (or (match-string 1 folder) (user-login-name)))
  11912. (host (match-string 2 folder))
  11913. (file (match-string 3 folder)))
  11914. (cond
  11915. ((featurep 'tramp)
  11916. ;; use tramp to access the file
  11917. (if (featurep 'xemacs)
  11918. (setq folder (format "[%s@%s]%s" user host file))
  11919. (setq folder (format "/%s@%s:%s" user host file))))
  11920. (t
  11921. ;; use ange-ftp or efs
  11922. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11923. (setq folder (format "/%s@%s:%s" user host file))))))
  11924. (when folder
  11925. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11926. (sit-for 0.1)
  11927. (when article
  11928. (vm-select-folder-buffer)
  11929. (widen)
  11930. (let ((case-fold-search t))
  11931. (goto-char (point-min))
  11932. (if (not (re-search-forward
  11933. (concat "^" "message-id: *" (regexp-quote article))))
  11934. (error "Could not find the specified message in this folder"))
  11935. (vm-isearch-update)
  11936. (vm-isearch-narrow)
  11937. (vm-beginning-of-message)
  11938. (vm-summarize)))))
  11939. (defun org-follow-wl-link (folder article)
  11940. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11941. (if (and (string= folder "%")
  11942. article
  11943. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11944. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11945. ;; Thus, we recompose folder and article ids.
  11946. (setq folder (format "%s#%s" folder (match-string 1 article))
  11947. article (match-string 3 article)))
  11948. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11949. (error "No such folder: %s" folder))
  11950. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11951. (and article
  11952. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11953. (wl-summary-redisplay)))
  11954. (defun org-follow-rmail-link (folder article)
  11955. "Follow an RMAIL link to FOLDER and ARTICLE."
  11956. (setq article (org-add-angle-brackets article))
  11957. (let (message-number)
  11958. (save-excursion
  11959. (save-window-excursion
  11960. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11961. (setq message-number
  11962. (save-restriction
  11963. (widen)
  11964. (goto-char (point-max))
  11965. (if (re-search-backward
  11966. (concat "^Message-ID:\\s-+" (regexp-quote
  11967. (or article "")))
  11968. nil t)
  11969. (rmail-what-message))))))
  11970. (if message-number
  11971. (progn
  11972. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11973. (rmail-show-message message-number)
  11974. message-number)
  11975. (error "Message not found"))))
  11976. ;;; mh-e integration based on planner-mode
  11977. (defun org-mhe-get-message-real-folder ()
  11978. "Return the name of the current message real folder, so if you use
  11979. sequences, it will now work."
  11980. (save-excursion
  11981. (let* ((folder
  11982. (if (equal major-mode 'mh-folder-mode)
  11983. mh-current-folder
  11984. ;; Refer to the show buffer
  11985. mh-show-folder-buffer))
  11986. (end-index
  11987. (if (boundp 'mh-index-folder)
  11988. (min (length mh-index-folder) (length folder))))
  11989. )
  11990. ;; a simple test on mh-index-data does not work, because
  11991. ;; mh-index-data is always nil in a show buffer.
  11992. (if (and (boundp 'mh-index-folder)
  11993. (string= mh-index-folder (substring folder 0 end-index)))
  11994. (if (equal major-mode 'mh-show-mode)
  11995. (save-window-excursion
  11996. (let (pop-up-frames)
  11997. (when (buffer-live-p (get-buffer folder))
  11998. (progn
  11999. (pop-to-buffer folder)
  12000. (org-mhe-get-message-folder-from-index)
  12001. )
  12002. )))
  12003. (org-mhe-get-message-folder-from-index)
  12004. )
  12005. folder
  12006. )
  12007. )))
  12008. (defun org-mhe-get-message-folder-from-index ()
  12009. "Returns the name of the message folder in a index folder buffer."
  12010. (save-excursion
  12011. (mh-index-previous-folder)
  12012. (re-search-forward "^\\(+.*\\)$" nil t)
  12013. (message "%s" (match-string 1))))
  12014. (defun org-mhe-get-message-folder ()
  12015. "Return the name of the current message folder. Be careful if you
  12016. use sequences."
  12017. (save-excursion
  12018. (if (equal major-mode 'mh-folder-mode)
  12019. mh-current-folder
  12020. ;; Refer to the show buffer
  12021. mh-show-folder-buffer)))
  12022. (defun org-mhe-get-message-num ()
  12023. "Return the number of the current message. Be careful if you
  12024. use sequences."
  12025. (save-excursion
  12026. (if (equal major-mode 'mh-folder-mode)
  12027. (mh-get-msg-num nil)
  12028. ;; Refer to the show buffer
  12029. (mh-show-buffer-message-number))))
  12030. (defun org-mhe-get-header (header)
  12031. "Return a header of the message in folder mode. This will create a
  12032. show buffer for the corresponding message. If you have a more clever
  12033. idea..."
  12034. (let* ((folder (org-mhe-get-message-folder))
  12035. (num (org-mhe-get-message-num))
  12036. (buffer (get-buffer-create (concat "show-" folder)))
  12037. (header-field))
  12038. (with-current-buffer buffer
  12039. (mh-display-msg num folder)
  12040. (if (equal major-mode 'mh-folder-mode)
  12041. (mh-header-display)
  12042. (mh-show-header-display))
  12043. (set-buffer buffer)
  12044. (setq header-field (mh-get-header-field header))
  12045. (if (equal major-mode 'mh-folder-mode)
  12046. (mh-show)
  12047. (mh-show-show))
  12048. header-field)))
  12049. (defun org-follow-mhe-link (folder article)
  12050. "Follow an MHE link to FOLDER and ARTICLE.
  12051. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12052. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12053. ARTICLE is searched in all folders. Indexed searches (swish++,
  12054. namazu, and others supported by MH-E) will always search in all
  12055. folders."
  12056. (require 'mh-e)
  12057. (require 'mh-search)
  12058. (require 'mh-utils)
  12059. (mh-find-path)
  12060. (if (not article)
  12061. (mh-visit-folder (mh-normalize-folder-name folder))
  12062. (setq article (org-add-angle-brackets article))
  12063. (mh-search-choose)
  12064. (if (equal mh-searcher 'pick)
  12065. (progn
  12066. (mh-search folder (list "--message-id" article))
  12067. (when (and org-mhe-search-all-folders
  12068. (not (org-mhe-get-message-real-folder)))
  12069. (kill-this-buffer)
  12070. (mh-search "+" (list "--message-id" article))))
  12071. (mh-search "+" article))
  12072. (if (org-mhe-get-message-real-folder)
  12073. (mh-show-msg 1)
  12074. (kill-this-buffer)
  12075. (error "Message not found"))))
  12076. ;;; BibTeX links
  12077. ;; Use the custom search meachnism to construct and use search strings for
  12078. ;; file links to BibTeX database entries.
  12079. (defun org-create-file-search-in-bibtex ()
  12080. "Create the search string and description for a BibTeX database entry."
  12081. (when (eq major-mode 'bibtex-mode)
  12082. ;; yes, we want to construct this search string.
  12083. ;; Make a good description for this entry, using names, year and the title
  12084. ;; Put it into the `description' variable which is dynamically scoped.
  12085. (let ((bibtex-autokey-names 1)
  12086. (bibtex-autokey-names-stretch 1)
  12087. (bibtex-autokey-name-case-convert-function 'identity)
  12088. (bibtex-autokey-name-separator " & ")
  12089. (bibtex-autokey-additional-names " et al.")
  12090. (bibtex-autokey-year-length 4)
  12091. (bibtex-autokey-name-year-separator " ")
  12092. (bibtex-autokey-titlewords 3)
  12093. (bibtex-autokey-titleword-separator " ")
  12094. (bibtex-autokey-titleword-case-convert-function 'identity)
  12095. (bibtex-autokey-titleword-length 'infty)
  12096. (bibtex-autokey-year-title-separator ": "))
  12097. (setq description (bibtex-generate-autokey)))
  12098. ;; Now parse the entry, get the key and return it.
  12099. (save-excursion
  12100. (bibtex-beginning-of-entry)
  12101. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12102. (defun org-execute-file-search-in-bibtex (s)
  12103. "Find the link search string S as a key for a database entry."
  12104. (when (eq major-mode 'bibtex-mode)
  12105. ;; Yes, we want to do the search in this file.
  12106. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12107. (goto-char (point-min))
  12108. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12109. (regexp-quote s) "[ \t\n]*,") nil t)
  12110. (goto-char (match-beginning 0)))
  12111. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12112. ;; Use double prefix to indicate that any web link should be browsed
  12113. (let ((b (current-buffer)) (p (point)))
  12114. ;; Restore the window configuration because we just use the web link
  12115. (set-window-configuration org-window-config-before-follow-link)
  12116. (save-excursion (set-buffer b) (goto-char p)
  12117. (bibtex-url)))
  12118. (recenter 0)) ; Move entry start to beginning of window
  12119. ;; return t to indicate that the search is done.
  12120. t))
  12121. ;; Finally add the functions to the right hooks.
  12122. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12123. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12124. ;; end of Bibtex link setup
  12125. ;;; Following file links
  12126. (defun org-open-file (path &optional in-emacs line search)
  12127. "Open the file at PATH.
  12128. First, this expands any special file name abbreviations. Then the
  12129. configuration variable `org-file-apps' is checked if it contains an
  12130. entry for this file type, and if yes, the corresponding command is launched.
  12131. If no application is found, Emacs simply visits the file.
  12132. With optional argument IN-EMACS, Emacs will visit the file.
  12133. Optional LINE specifies a line to go to, optional SEARCH a string to
  12134. search for. If LINE or SEARCH is given, the file will always be
  12135. opened in Emacs.
  12136. If the file does not exist, an error is thrown."
  12137. (setq in-emacs (or in-emacs line search))
  12138. (let* ((file (if (equal path "")
  12139. buffer-file-name
  12140. (substitute-in-file-name (expand-file-name path))))
  12141. (apps (append org-file-apps (org-default-apps)))
  12142. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12143. (dirp (if remp nil (file-directory-p file)))
  12144. (dfile (downcase file))
  12145. (old-buffer (current-buffer))
  12146. (old-pos (point))
  12147. (old-mode major-mode)
  12148. ext cmd)
  12149. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12150. (setq ext (match-string 1 dfile))
  12151. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12152. (setq ext (match-string 1 dfile))))
  12153. (if in-emacs
  12154. (setq cmd 'emacs)
  12155. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12156. (and dirp (cdr (assoc 'directory apps)))
  12157. (cdr (assoc ext apps))
  12158. (cdr (assoc t apps)))))
  12159. (when (eq cmd 'mailcap)
  12160. (require 'mailcap)
  12161. (mailcap-parse-mailcaps)
  12162. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12163. (command (mailcap-mime-info mime-type)))
  12164. (if (stringp command)
  12165. (setq cmd command)
  12166. (setq cmd 'emacs))))
  12167. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12168. (not (file-exists-p file))
  12169. (not org-open-non-existing-files))
  12170. (error "No such file: %s" file))
  12171. (cond
  12172. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12173. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12174. (while (string-match "['\"]%s['\"]" cmd)
  12175. (setq cmd (replace-match "%s" t t cmd)))
  12176. (while (string-match "%s" cmd)
  12177. (setq cmd (replace-match
  12178. (save-match-data (shell-quote-argument file))
  12179. t t cmd)))
  12180. (save-window-excursion
  12181. (start-process-shell-command cmd nil cmd)))
  12182. ((or (stringp cmd)
  12183. (eq cmd 'emacs))
  12184. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12185. (widen)
  12186. (if line (goto-line line)
  12187. (if search (org-link-search search))))
  12188. ((consp cmd)
  12189. (eval cmd))
  12190. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12191. (and (org-mode-p) (eq old-mode 'org-mode)
  12192. (or (not (equal old-buffer (current-buffer)))
  12193. (not (equal old-pos (point))))
  12194. (org-mark-ring-push old-pos old-buffer))))
  12195. (defun org-default-apps ()
  12196. "Return the default applications for this operating system."
  12197. (cond
  12198. ((eq system-type 'darwin)
  12199. org-file-apps-defaults-macosx)
  12200. ((eq system-type 'windows-nt)
  12201. org-file-apps-defaults-windowsnt)
  12202. (t org-file-apps-defaults-gnu)))
  12203. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12204. (defun org-file-remote-p (file)
  12205. "Test whether FILE specifies a location on a remote system.
  12206. Return non-nil if the location is indeed remote.
  12207. For example, the filename \"/user@host:/foo\" specifies a location
  12208. on the system \"/user@host:\"."
  12209. (cond ((fboundp 'file-remote-p)
  12210. (file-remote-p file))
  12211. ((fboundp 'tramp-handle-file-remote-p)
  12212. (tramp-handle-file-remote-p file))
  12213. ((and (boundp 'ange-ftp-name-format)
  12214. (string-match (car ange-ftp-name-format) file))
  12215. t)
  12216. (t nil)))
  12217. ;;;; Hooks for remember.el, and refiling
  12218. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12219. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12220. ;;;###autoload
  12221. (defun org-remember-insinuate ()
  12222. "Setup remember.el for use wiht Org-mode."
  12223. (require 'remember)
  12224. (setq remember-annotation-functions '(org-remember-annotation))
  12225. (setq remember-handler-functions '(org-remember-handler))
  12226. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12227. ;;;###autoload
  12228. (defun org-remember-annotation ()
  12229. "Return a link to the current location as an annotation for remember.el.
  12230. If you are using Org-mode files as target for data storage with
  12231. remember.el, then the annotations should include a link compatible with the
  12232. conventions in Org-mode. This function returns such a link."
  12233. (org-store-link nil))
  12234. (defconst org-remember-help
  12235. "Select a destination location for the note.
  12236. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12237. RET on headline -> Store as sublevel entry to current headline
  12238. RET at beg-of-buf -> Append to file as level 2 headline
  12239. <left>/<right> -> before/after current headline, same headings level")
  12240. (defvar org-remember-previous-location nil)
  12241. (defvar org-force-remember-template-char) ;; dynamically scoped
  12242. (defun org-select-remember-template (&optional use-char)
  12243. (when org-remember-templates
  12244. (let* ((templates (mapcar (lambda (x)
  12245. (if (stringp (car x))
  12246. (append (list (nth 1 x) (car x)) (cddr x))
  12247. (append (list (car x) "") (cdr x))))
  12248. org-remember-templates))
  12249. (char (or use-char
  12250. (cond
  12251. ((= (length templates) 1)
  12252. (caar templates))
  12253. ((and (boundp 'org-force-remember-template-char)
  12254. org-force-remember-template-char)
  12255. (if (stringp org-force-remember-template-char)
  12256. (string-to-char org-force-remember-template-char)
  12257. org-force-remember-template-char))
  12258. (t
  12259. (message "Select template: %s"
  12260. (mapconcat
  12261. (lambda (x)
  12262. (cond
  12263. ((not (string-match "\\S-" (nth 1 x)))
  12264. (format "[%c]" (car x)))
  12265. ((equal (downcase (car x))
  12266. (downcase (aref (nth 1 x) 0)))
  12267. (format "[%c]%s" (car x)
  12268. (substring (nth 1 x) 1)))
  12269. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12270. templates " "))
  12271. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12272. (when (equal char0 ?\C-g)
  12273. (jump-to-register remember-register)
  12274. (kill-buffer remember-buffer))
  12275. char0))))))
  12276. (cddr (assoc char templates)))))
  12277. (defvar x-last-selected-text)
  12278. (defvar x-last-selected-text-primary)
  12279. ;;;###autoload
  12280. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12281. "Initialize *remember* buffer with template, invoke `org-mode'.
  12282. This function should be placed into `remember-mode-hook' and in fact requires
  12283. to be run from that hook to function properly."
  12284. (if org-remember-templates
  12285. (let* ((entry (org-select-remember-template use-char))
  12286. (tpl (car entry))
  12287. (plist-p (if org-store-link-plist t nil))
  12288. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12289. (string-match "\\S-" (nth 1 entry)))
  12290. (nth 1 entry)
  12291. org-default-notes-file))
  12292. (headline (nth 2 entry))
  12293. (v-c (or (and (eq window-system 'x)
  12294. (fboundp 'x-cut-buffer-or-selection-value)
  12295. (x-cut-buffer-or-selection-value))
  12296. (org-bound-and-true-p x-last-selected-text)
  12297. (org-bound-and-true-p x-last-selected-text-primary)
  12298. (and (> (length kill-ring) 0) (current-kill 0))))
  12299. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12300. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12301. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12302. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12303. ;; `initial' and `annotation' are bound in `remember'
  12304. (v-i (if (boundp 'initial) initial))
  12305. (v-a (if (and (boundp 'annotation) annotation)
  12306. (if (equal annotation "[[]]") "" annotation)
  12307. ""))
  12308. (v-A (if (and v-a
  12309. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12310. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12311. v-a))
  12312. (v-n user-full-name)
  12313. (org-startup-folded nil)
  12314. org-time-was-given org-end-time-was-given x
  12315. prompt completions char time pos default histvar)
  12316. (setq org-store-link-plist
  12317. (append (list :annotation v-a :initial v-i)
  12318. org-store-link-plist))
  12319. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12320. (erase-buffer)
  12321. (insert (substitute-command-keys
  12322. (format
  12323. "## Filing location: Select interactively, default, or last used:
  12324. ## %s to select file and header location interactively.
  12325. ## %s \"%s\" -> \"* %s\"
  12326. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12327. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12328. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12329. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12330. (abbreviate-file-name (or file org-default-notes-file))
  12331. (or headline "")
  12332. (or (car org-remember-previous-location) "???")
  12333. (or (cdr org-remember-previous-location) "???"))))
  12334. (insert tpl) (goto-char (point-min))
  12335. ;; Simple %-escapes
  12336. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12337. (when (and initial (equal (match-string 0) "%i"))
  12338. (save-match-data
  12339. (let* ((lead (buffer-substring
  12340. (point-at-bol) (match-beginning 0))))
  12341. (setq v-i (mapconcat 'identity
  12342. (org-split-string initial "\n")
  12343. (concat "\n" lead))))))
  12344. (replace-match
  12345. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12346. t t))
  12347. ;; %[] Insert contents of a file.
  12348. (goto-char (point-min))
  12349. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12350. (let ((start (match-beginning 0))
  12351. (end (match-end 0))
  12352. (filename (expand-file-name (match-string 1))))
  12353. (goto-char start)
  12354. (delete-region start end)
  12355. (condition-case error
  12356. (insert-file-contents filename)
  12357. (error (insert (format "%%![Couldn't insert %s: %s]"
  12358. filename error))))))
  12359. ;; %() embedded elisp
  12360. (goto-char (point-min))
  12361. (while (re-search-forward "%\\((.+)\\)" nil t)
  12362. (goto-char (match-beginning 0))
  12363. (let ((template-start (point)))
  12364. (forward-char 1)
  12365. (let ((result
  12366. (condition-case error
  12367. (eval (read (current-buffer)))
  12368. (error (format "%%![Error: %s]" error)))))
  12369. (delete-region template-start (point))
  12370. (insert result))))
  12371. ;; From the property list
  12372. (when plist-p
  12373. (goto-char (point-min))
  12374. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12375. (and (setq x (or (plist-get org-store-link-plist
  12376. (intern (match-string 1))) ""))
  12377. (replace-match x t t))))
  12378. ;; Turn on org-mode in the remember buffer, set local variables
  12379. (org-mode)
  12380. (org-set-local 'org-finish-function 'org-remember-finalize)
  12381. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12382. (org-set-local 'org-default-notes-file file))
  12383. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12384. (org-set-local 'org-remember-default-headline headline))
  12385. ;; Interactive template entries
  12386. (goto-char (point-min))
  12387. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12388. (setq char (if (match-end 3) (match-string 3))
  12389. prompt (if (match-end 2) (match-string 2)))
  12390. (goto-char (match-beginning 0))
  12391. (replace-match "")
  12392. (setq completions nil default nil)
  12393. (when prompt
  12394. (setq completions (org-split-string prompt "|")
  12395. prompt (pop completions)
  12396. default (car completions)
  12397. histvar (intern (concat
  12398. "org-remember-template-prompt-history::"
  12399. (or prompt "")))
  12400. completions (mapcar 'list completions)))
  12401. (cond
  12402. ((member char '("G" "g"))
  12403. (let* ((org-last-tags-completion-table
  12404. (org-global-tags-completion-table
  12405. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12406. (org-add-colon-after-tag-completion t)
  12407. (ins (completing-read
  12408. (if prompt (concat prompt ": ") "Tags: ")
  12409. 'org-tags-completion-function nil nil nil
  12410. 'org-tags-history)))
  12411. (setq ins (mapconcat 'identity
  12412. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12413. ":"))
  12414. (when (string-match "\\S-" ins)
  12415. (or (equal (char-before) ?:) (insert ":"))
  12416. (insert ins)
  12417. (or (equal (char-after) ?:) (insert ":")))))
  12418. (char
  12419. (setq org-time-was-given (equal (upcase char) char))
  12420. (setq time (org-read-date (equal (upcase char) "U") t nil
  12421. prompt))
  12422. (org-insert-time-stamp time org-time-was-given
  12423. (member char '("u" "U"))
  12424. nil nil (list org-end-time-was-given)))
  12425. (t
  12426. (insert (org-completing-read
  12427. (concat (if prompt prompt "Enter string")
  12428. (if default (concat " [" default "]"))
  12429. ": ")
  12430. completions nil nil nil histvar default)))))
  12431. (goto-char (point-min))
  12432. (if (re-search-forward "%\\?" nil t)
  12433. (replace-match "")
  12434. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12435. (org-mode)
  12436. (org-set-local 'org-finish-function 'org-remember-finalize))
  12437. (when (save-excursion
  12438. (goto-char (point-min))
  12439. (re-search-forward "%!" nil t))
  12440. (replace-match "")
  12441. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12442. (defun org-remember-finish-immediately ()
  12443. "File remember note immediately.
  12444. This should be run in `post-command-hook' and will remove itself
  12445. from that hook."
  12446. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12447. (when org-finish-function
  12448. (funcall org-finish-function)))
  12449. (defvar org-clock-marker) ; Defined below
  12450. (defun org-remember-finalize ()
  12451. "Finalize the remember process."
  12452. (unless (fboundp 'remember-finalize)
  12453. (defalias 'remember-finalize 'remember-buffer))
  12454. (when (and org-clock-marker
  12455. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12456. ;; FIXME: test this, this is w/o notetaking!
  12457. (let (org-log-note-clock-out) (org-clock-out)))
  12458. (when buffer-file-name
  12459. (save-buffer)
  12460. (setq buffer-file-name nil))
  12461. (remember-finalize))
  12462. ;;;###autoload
  12463. (defun org-remember (&optional goto org-force-remember-template-char)
  12464. "Call `remember'. If this is already a remember buffer, re-apply template.
  12465. If there is an active region, make sure remember uses it as initial content
  12466. of the remember buffer.
  12467. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12468. anything, just go to the file/headline where the selected template usually
  12469. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12470. note stored by remember.
  12471. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12472. associated with a template in `org-remember-templates'."
  12473. (interactive "P")
  12474. (cond
  12475. ((equal goto '(4)) (org-go-to-remember-target))
  12476. ((equal goto '(16)) (org-remember-goto-last-stored))
  12477. (t
  12478. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12479. (progn
  12480. (when (< (length org-remember-templates) 2)
  12481. (error "No other template available"))
  12482. (erase-buffer)
  12483. (let ((annotation (plist-get org-store-link-plist :annotation))
  12484. (initial (plist-get org-store-link-plist :initial)))
  12485. (org-remember-apply-template))
  12486. (message "Press C-c C-c to remember data"))
  12487. (if (org-region-active-p)
  12488. (remember (buffer-substring (point) (mark)))
  12489. (call-interactively 'remember))))))
  12490. (defun org-remember-goto-last-stored ()
  12491. "Go to the location where the last remember note was stored."
  12492. (interactive)
  12493. (bookmark-jump "org-remember-last-stored")
  12494. (message "This is the last note stored by remember"))
  12495. (defun org-go-to-remember-target (&optional template-key)
  12496. "Go to the target location of a remember template.
  12497. The user is queried for the template."
  12498. (interactive)
  12499. (let* ((entry (org-select-remember-template template-key))
  12500. (file (nth 1 entry))
  12501. (heading (nth 2 entry))
  12502. visiting)
  12503. (unless (and file (stringp file) (string-match "\\S-" file))
  12504. (setq file org-default-notes-file))
  12505. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12506. (setq heading org-remember-default-headline))
  12507. (setq visiting (org-find-base-buffer-visiting file))
  12508. (if (not visiting) (find-file-noselect file))
  12509. (switch-to-buffer (or visiting (get-file-buffer file)))
  12510. (widen)
  12511. (goto-char (point-min))
  12512. (if (re-search-forward
  12513. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12514. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12515. nil t)
  12516. (goto-char (match-beginning 0))
  12517. (error "Target headline not found: %s" heading))))
  12518. (defvar org-note-abort nil) ; dynamically scoped
  12519. ;;;###autoload
  12520. (defun org-remember-handler ()
  12521. "Store stuff from remember.el into an org file.
  12522. First prompts for an org file. If the user just presses return, the value
  12523. of `org-default-notes-file' is used.
  12524. Then the command offers the headings tree of the selected file in order to
  12525. file the text at a specific location.
  12526. You can either immediately press RET to get the note appended to the
  12527. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12528. find a better place. Then press RET or <left> or <right> in insert the note.
  12529. Key Cursor position Note gets inserted
  12530. -----------------------------------------------------------------------------
  12531. RET buffer-start as level 1 heading at end of file
  12532. RET on headline as sublevel of the heading at cursor
  12533. RET no heading at cursor position, level taken from context.
  12534. Or use prefix arg to specify level manually.
  12535. <left> on headline as same level, before current heading
  12536. <right> on headline as same level, after current heading
  12537. So the fastest way to store the note is to press RET RET to append it to
  12538. the default file. This way your current train of thought is not
  12539. interrupted, in accordance with the principles of remember.el.
  12540. You can also get the fast execution without prompting by using
  12541. C-u C-c C-c to exit the remember buffer. See also the variable
  12542. `org-remember-store-without-prompt'.
  12543. Before being stored away, the function ensures that the text has a
  12544. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12545. is constructed from the current date and some additional data.
  12546. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12547. also indented so that it starts in the same column as the headline
  12548. \(i.e. after the stars).
  12549. See also the variable `org-reverse-note-order'."
  12550. (goto-char (point-min))
  12551. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12552. (replace-match ""))
  12553. (goto-char (point-max))
  12554. (beginning-of-line 1)
  12555. (while (looking-at "[ \t]*$\\|##.*")
  12556. (delete-region (1- (point)) (point-max))
  12557. (beginning-of-line 1))
  12558. (catch 'quit
  12559. (if org-note-abort (throw 'quit nil))
  12560. (let* ((txt (buffer-substring (point-min) (point-max)))
  12561. (fastp (org-xor (equal current-prefix-arg '(4))
  12562. org-remember-store-without-prompt))
  12563. (file (cond
  12564. (fastp org-default-notes-file)
  12565. ((and (eq org-remember-interactive-interface 'refile)
  12566. org-refile-targets)
  12567. org-default-notes-file)
  12568. ((not (and (equal current-prefix-arg '(16))
  12569. org-remember-previous-location))
  12570. (org-get-org-file))))
  12571. (heading org-remember-default-headline)
  12572. (visiting (and file (org-find-base-buffer-visiting file)))
  12573. (org-startup-folded nil)
  12574. (org-startup-align-all-tables nil)
  12575. (org-goto-start-pos 1)
  12576. spos exitcmd level indent reversed)
  12577. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12578. (setq file (car org-remember-previous-location)
  12579. heading (cdr org-remember-previous-location)
  12580. fastp t))
  12581. (setq current-prefix-arg nil)
  12582. (if (string-match "[ \t\n]+\\'" txt)
  12583. (setq txt (replace-match "" t t txt)))
  12584. ;; Modify text so that it becomes a nice subtree which can be inserted
  12585. ;; into an org tree.
  12586. (let* ((lines (split-string txt "\n"))
  12587. first)
  12588. (setq first (car lines) lines (cdr lines))
  12589. (if (string-match "^\\*+ " first)
  12590. ;; Is already a headline
  12591. (setq indent nil)
  12592. ;; We need to add a headline: Use time and first buffer line
  12593. (setq lines (cons first lines)
  12594. first (concat "* " (current-time-string)
  12595. " (" (remember-buffer-desc) ")")
  12596. indent " "))
  12597. (if (and org-adapt-indentation indent)
  12598. (setq lines (mapcar
  12599. (lambda (x)
  12600. (if (string-match "\\S-" x)
  12601. (concat indent x) x))
  12602. lines)))
  12603. (setq txt (concat first "\n"
  12604. (mapconcat 'identity lines "\n"))))
  12605. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12606. (setq txt (replace-match "\n\n" t t txt))
  12607. (if (string-match "[ \t\n]*\\'" txt)
  12608. (setq txt (replace-match "\n" t t txt))))
  12609. ;; Put the modified text back into the remember buffer, for refile.
  12610. (erase-buffer)
  12611. (insert txt)
  12612. (goto-char (point-min))
  12613. (when (and (eq org-remember-interactive-interface 'refile)
  12614. (not fastp))
  12615. (org-refile nil (or visiting (find-file-noselect file)))
  12616. (throw 'quit t))
  12617. ;; Find the file
  12618. (if (not visiting) (find-file-noselect file))
  12619. (with-current-buffer (or visiting (get-file-buffer file))
  12620. (unless (org-mode-p)
  12621. (error "Target files for remember notes must be in Org-mode"))
  12622. (save-excursion
  12623. (save-restriction
  12624. (widen)
  12625. (and (goto-char (point-min))
  12626. (not (re-search-forward "^\\* " nil t))
  12627. (insert "\n* " (or heading "Notes") "\n"))
  12628. (setq reversed (org-notes-order-reversed-p))
  12629. ;; Find the default location
  12630. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12631. (goto-char (point-min))
  12632. (if (re-search-forward
  12633. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12634. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12635. nil t)
  12636. (setq org-goto-start-pos (match-beginning 0))
  12637. (when fastp
  12638. (goto-char (point-max))
  12639. (unless (bolp) (newline))
  12640. (insert "* " heading "\n")
  12641. (setq org-goto-start-pos (point-at-bol 0)))))
  12642. ;; Ask the User for a location, using the appropriate interface
  12643. (cond
  12644. (fastp (setq spos org-goto-start-pos
  12645. exitcmd 'return))
  12646. ((eq org-remember-interactive-interface 'outline)
  12647. (setq spos (org-get-location (current-buffer)
  12648. org-remember-help)
  12649. exitcmd (cdr spos)
  12650. spos (car spos)))
  12651. ((eq org-remember-interactive-interface 'outline-path-completion)
  12652. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12653. (org-refile-use-outline-path t))
  12654. (setq spos (org-refile-get-location "Heading: ")
  12655. exitcmd 'return
  12656. spos (nth 3 spos))))
  12657. (t (error "this should not hapen")))
  12658. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12659. ; not handle this note
  12660. (goto-char spos)
  12661. (cond ((org-on-heading-p t)
  12662. (org-back-to-heading t)
  12663. (setq level (funcall outline-level))
  12664. (cond
  12665. ((eq exitcmd 'return)
  12666. ;; sublevel of current
  12667. (setq org-remember-previous-location
  12668. (cons (abbreviate-file-name file)
  12669. (org-get-heading 'notags)))
  12670. (if reversed
  12671. (outline-next-heading)
  12672. (org-end-of-subtree t)
  12673. (if (not (bolp))
  12674. (if (looking-at "[ \t]*\n")
  12675. (beginning-of-line 2)
  12676. (end-of-line 1)
  12677. (insert "\n"))))
  12678. (bookmark-set "org-remember-last-stored")
  12679. (org-paste-subtree (org-get-legal-level level 1) txt))
  12680. ((eq exitcmd 'left)
  12681. ;; before current
  12682. (bookmark-set "org-remember-last-stored")
  12683. (org-paste-subtree level txt))
  12684. ((eq exitcmd 'right)
  12685. ;; after current
  12686. (org-end-of-subtree t)
  12687. (bookmark-set "org-remember-last-stored")
  12688. (org-paste-subtree level txt))
  12689. (t (error "This should not happen"))))
  12690. ((and (bobp) (not reversed))
  12691. ;; Put it at the end, one level below level 1
  12692. (save-restriction
  12693. (widen)
  12694. (goto-char (point-max))
  12695. (if (not (bolp)) (newline))
  12696. (bookmark-set "org-remember-last-stored")
  12697. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12698. ((and (bobp) reversed)
  12699. ;; Put it at the start, as level 1
  12700. (save-restriction
  12701. (widen)
  12702. (goto-char (point-min))
  12703. (re-search-forward "^\\*+ " nil t)
  12704. (beginning-of-line 1)
  12705. (bookmark-set "org-remember-last-stored")
  12706. (org-paste-subtree 1 txt)))
  12707. (t
  12708. ;; Put it right there, with automatic level determined by
  12709. ;; org-paste-subtree or from prefix arg
  12710. (bookmark-set "org-remember-last-stored")
  12711. (org-paste-subtree
  12712. (if (numberp current-prefix-arg) current-prefix-arg)
  12713. txt)))
  12714. (when remember-save-after-remembering
  12715. (save-buffer)
  12716. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12717. t) ;; return t to indicate that we took care of this note.
  12718. (defun org-get-org-file ()
  12719. "Read a filename, with default directory `org-directory'."
  12720. (let ((default (or org-default-notes-file remember-data-file)))
  12721. (read-file-name (format "File name [%s]: " default)
  12722. (file-name-as-directory org-directory)
  12723. default)))
  12724. (defun org-notes-order-reversed-p ()
  12725. "Check if the current file should receive notes in reversed order."
  12726. (cond
  12727. ((not org-reverse-note-order) nil)
  12728. ((eq t org-reverse-note-order) t)
  12729. ((not (listp org-reverse-note-order)) nil)
  12730. (t (catch 'exit
  12731. (let ((all org-reverse-note-order)
  12732. entry)
  12733. (while (setq entry (pop all))
  12734. (if (string-match (car entry) buffer-file-name)
  12735. (throw 'exit (cdr entry))))
  12736. nil)))))
  12737. ;;; Refiling
  12738. (defvar org-refile-target-table nil
  12739. "The list of refile targets, created by `org-refile'.")
  12740. (defvar org-agenda-new-buffers nil
  12741. "Buffers created to visit agenda files.")
  12742. (defun org-get-refile-targets (&optional default-buffer)
  12743. "Produce a table with refile targets."
  12744. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12745. targets txt re files f desc descre)
  12746. (with-current-buffer (or default-buffer (current-buffer))
  12747. (while (setq entry (pop entries))
  12748. (setq files (car entry) desc (cdr entry))
  12749. (cond
  12750. ((null files) (setq files (list (current-buffer))))
  12751. ((eq files 'org-agenda-files)
  12752. (setq files (org-agenda-files 'unrestricted)))
  12753. ((and (symbolp files) (fboundp files))
  12754. (setq files (funcall files)))
  12755. ((and (symbolp files) (boundp files))
  12756. (setq files (symbol-value files))))
  12757. (if (stringp files) (setq files (list files)))
  12758. (cond
  12759. ((eq (car desc) :tag)
  12760. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12761. ((eq (car desc) :todo)
  12762. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12763. ((eq (car desc) :regexp)
  12764. (setq descre (cdr desc)))
  12765. ((eq (car desc) :level)
  12766. (setq descre (concat "^\\*\\{" (number-to-string
  12767. (if org-odd-levels-only
  12768. (1- (* 2 (cdr desc)))
  12769. (cdr desc)))
  12770. "\\}[ \t]")))
  12771. ((eq (car desc) :maxlevel)
  12772. (setq descre (concat "^\\*\\{1," (number-to-string
  12773. (if org-odd-levels-only
  12774. (1- (* 2 (cdr desc)))
  12775. (cdr desc)))
  12776. "\\}[ \t]")))
  12777. (t (error "Bad refiling target description %s" desc)))
  12778. (while (setq f (pop files))
  12779. (save-excursion
  12780. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12781. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12782. (save-excursion
  12783. (save-restriction
  12784. (widen)
  12785. (goto-char (point-min))
  12786. (while (re-search-forward descre nil t)
  12787. (goto-char (point-at-bol))
  12788. (when (looking-at org-complex-heading-regexp)
  12789. (setq txt (match-string 4)
  12790. re (concat "^" (regexp-quote
  12791. (buffer-substring (match-beginning 1)
  12792. (match-end 4)))))
  12793. (if (match-end 5) (setq re (concat re "[ \t]+"
  12794. (regexp-quote
  12795. (match-string 5)))))
  12796. (setq re (concat re "[ \t]*$"))
  12797. (when org-refile-use-outline-path
  12798. (setq txt (mapconcat 'identity
  12799. (append
  12800. (if (eq org-refile-use-outline-path 'file)
  12801. (list (file-name-nondirectory
  12802. (buffer-file-name (buffer-base-buffer))))
  12803. (if (eq org-refile-use-outline-path 'full-file-path)
  12804. (list (buffer-file-name (buffer-base-buffer)))))
  12805. (org-get-outline-path)
  12806. (list txt))
  12807. "/")))
  12808. (push (list txt f re (point)) targets))
  12809. (goto-char (point-at-eol))))))))
  12810. (nreverse targets))))
  12811. (defun org-get-outline-path ()
  12812. "Return the outline path to the current entry, as a list."
  12813. (let (rtn)
  12814. (save-excursion
  12815. (while (org-up-heading-safe)
  12816. (when (looking-at org-complex-heading-regexp)
  12817. (push (org-match-string-no-properties 4) rtn)))
  12818. rtn)))
  12819. (defvar org-refile-history nil
  12820. "History for refiling operations.")
  12821. (defun org-refile (&optional goto default-buffer)
  12822. "Move the entry at point to another heading.
  12823. The list of target headings is compiled using the information in
  12824. `org-refile-targets', which see. This list is created upon first use, and
  12825. you can update it by calling this command with a double prefix (`C-u C-u').
  12826. FIXME: Can we find a better way of updating?
  12827. At the target location, the entry is filed as a subitem of the target heading.
  12828. Depending on `org-reverse-note-order', the new subitem will either be the
  12829. first of the last subitem.
  12830. With prefix arg GOTO, the command will only visit the target location,
  12831. not actually move anything.
  12832. With a double prefix `C-c C-c', go to the location where the last refiling
  12833. operation has put the subtree.
  12834. With a double prefix argument, the command can be used to jump to any
  12835. heading in the current buffer."
  12836. (interactive "P")
  12837. (let* ((cbuf (current-buffer))
  12838. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12839. pos it nbuf file re level reversed)
  12840. (if (equal goto '(16))
  12841. (org-refile-goto-last-stored)
  12842. (when (setq it (org-refile-get-location
  12843. (if goto "Goto: " "Refile to: ") default-buffer))
  12844. (setq file (nth 1 it)
  12845. re (nth 2 it)
  12846. pos (nth 3 it))
  12847. (setq nbuf (or (find-buffer-visiting file)
  12848. (find-file-noselect file)))
  12849. (if goto
  12850. (progn
  12851. (switch-to-buffer nbuf)
  12852. (goto-char pos)
  12853. (org-show-context 'org-goto))
  12854. (org-copy-special)
  12855. (save-excursion
  12856. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12857. (find-file-noselect file))))
  12858. (setq reversed (org-notes-order-reversed-p))
  12859. (save-excursion
  12860. (save-restriction
  12861. (widen)
  12862. (goto-char pos)
  12863. (looking-at outline-regexp)
  12864. (setq level (org-get-legal-level (funcall outline-level) 1))
  12865. (goto-char
  12866. (if reversed
  12867. (outline-next-heading)
  12868. (or (save-excursion (outline-get-next-sibling))
  12869. (org-end-of-subtree t t)
  12870. (point-max))))
  12871. (bookmark-set "org-refile-last-stored")
  12872. (org-paste-subtree level))))
  12873. (org-cut-special)
  12874. (message "Entry refiled to \"%s\"" (car it)))))))
  12875. (defun org-refile-goto-last-stored ()
  12876. "Go to the location where the last refile was stored."
  12877. (interactive)
  12878. (bookmark-jump "org-refile-last-stored")
  12879. (message "This is the location of the last refile"))
  12880. (defun org-refile-get-location (&optional prompt default-buffer)
  12881. "Prompt the user for a refile location, using PROMPT."
  12882. (let ((org-refile-targets org-refile-targets)
  12883. (org-refile-use-outline-path org-refile-use-outline-path))
  12884. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12885. (unless org-refile-target-table
  12886. (error "No refile targets"))
  12887. (let* ((cbuf (current-buffer))
  12888. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12889. (fname (and filename (file-truename filename)))
  12890. (tbl (mapcar
  12891. (lambda (x)
  12892. (if (not (equal fname (file-truename (nth 1 x))))
  12893. (cons (concat (car x) " (" (file-name-nondirectory
  12894. (nth 1 x)) ")")
  12895. (cdr x))
  12896. x))
  12897. org-refile-target-table))
  12898. (completion-ignore-case t))
  12899. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12900. tbl)))
  12901. ;;;; Dynamic blocks
  12902. (defun org-find-dblock (name)
  12903. "Find the first dynamic block with name NAME in the buffer.
  12904. If not found, stay at current position and return nil."
  12905. (let (pos)
  12906. (save-excursion
  12907. (goto-char (point-min))
  12908. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12909. nil t)
  12910. (match-beginning 0))))
  12911. (if pos (goto-char pos))
  12912. pos))
  12913. (defconst org-dblock-start-re
  12914. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12915. "Matches the startline of a dynamic block, with parameters.")
  12916. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12917. "Matches the end of a dyhamic block.")
  12918. (defun org-create-dblock (plist)
  12919. "Create a dynamic block section, with parameters taken from PLIST.
  12920. PLIST must containe a :name entry which is used as name of the block."
  12921. (unless (bolp) (newline))
  12922. (let ((name (plist-get plist :name)))
  12923. (insert "#+BEGIN: " name)
  12924. (while plist
  12925. (if (eq (car plist) :name)
  12926. (setq plist (cddr plist))
  12927. (insert " " (prin1-to-string (pop plist)))))
  12928. (insert "\n\n#+END:\n")
  12929. (beginning-of-line -2)))
  12930. (defun org-prepare-dblock ()
  12931. "Prepare dynamic block for refresh.
  12932. This empties the block, puts the cursor at the insert position and returns
  12933. the property list including an extra property :name with the block name."
  12934. (unless (looking-at org-dblock-start-re)
  12935. (error "Not at a dynamic block"))
  12936. (let* ((begdel (1+ (match-end 0)))
  12937. (name (org-no-properties (match-string 1)))
  12938. (params (append (list :name name)
  12939. (read (concat "(" (match-string 3) ")")))))
  12940. (unless (re-search-forward org-dblock-end-re nil t)
  12941. (error "Dynamic block not terminated"))
  12942. (delete-region begdel (match-beginning 0))
  12943. (goto-char begdel)
  12944. (open-line 1)
  12945. params))
  12946. (defun org-map-dblocks (&optional command)
  12947. "Apply COMMAND to all dynamic blocks in the current buffer.
  12948. If COMMAND is not given, use `org-update-dblock'."
  12949. (let ((cmd (or command 'org-update-dblock))
  12950. pos)
  12951. (save-excursion
  12952. (goto-char (point-min))
  12953. (while (re-search-forward org-dblock-start-re nil t)
  12954. (goto-char (setq pos (match-beginning 0)))
  12955. (condition-case nil
  12956. (funcall cmd)
  12957. (error (message "Error during update of dynamic block")))
  12958. (goto-char pos)
  12959. (unless (re-search-forward org-dblock-end-re nil t)
  12960. (error "Dynamic block not terminated"))))))
  12961. (defun org-dblock-update (&optional arg)
  12962. "User command for updating dynamic blocks.
  12963. Update the dynamic block at point. With prefix ARG, update all dynamic
  12964. blocks in the buffer."
  12965. (interactive "P")
  12966. (if arg
  12967. (org-update-all-dblocks)
  12968. (or (looking-at org-dblock-start-re)
  12969. (org-beginning-of-dblock))
  12970. (org-update-dblock)))
  12971. (defun org-update-dblock ()
  12972. "Update the dynamic block at point
  12973. This means to empty the block, parse for parameters and then call
  12974. the correct writing function."
  12975. (save-window-excursion
  12976. (let* ((pos (point))
  12977. (line (org-current-line))
  12978. (params (org-prepare-dblock))
  12979. (name (plist-get params :name))
  12980. (cmd (intern (concat "org-dblock-write:" name))))
  12981. (message "Updating dynamic block `%s' at line %d..." name line)
  12982. (funcall cmd params)
  12983. (message "Updating dynamic block `%s' at line %d...done" name line)
  12984. (goto-char pos))))
  12985. (defun org-beginning-of-dblock ()
  12986. "Find the beginning of the dynamic block at point.
  12987. Error if there is no scuh block at point."
  12988. (let ((pos (point))
  12989. beg)
  12990. (end-of-line 1)
  12991. (if (and (re-search-backward org-dblock-start-re nil t)
  12992. (setq beg (match-beginning 0))
  12993. (re-search-forward org-dblock-end-re nil t)
  12994. (> (match-end 0) pos))
  12995. (goto-char beg)
  12996. (goto-char pos)
  12997. (error "Not in a dynamic block"))))
  12998. (defun org-update-all-dblocks ()
  12999. "Update all dynamic blocks in the buffer.
  13000. This function can be used in a hook."
  13001. (when (org-mode-p)
  13002. (org-map-dblocks 'org-update-dblock)))
  13003. ;;;; Completion
  13004. (defconst org-additional-option-like-keywords
  13005. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13006. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13007. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13008. (defun org-complete (&optional arg)
  13009. "Perform completion on word at point.
  13010. At the beginning of a headline, this completes TODO keywords as given in
  13011. `org-todo-keywords'.
  13012. If the current word is preceded by a backslash, completes the TeX symbols
  13013. that are supported for HTML support.
  13014. If the current word is preceded by \"#+\", completes special words for
  13015. setting file options.
  13016. In the line after \"#+STARTUP:, complete valid keywords.\"
  13017. At all other locations, this simply calls the value of
  13018. `org-completion-fallback-command'."
  13019. (interactive "P")
  13020. (org-without-partial-completion
  13021. (catch 'exit
  13022. (let* ((end (point))
  13023. (beg1 (save-excursion
  13024. (skip-chars-backward (org-re "[:alnum:]_@"))
  13025. (point)))
  13026. (beg (save-excursion
  13027. (skip-chars-backward "a-zA-Z0-9_:$")
  13028. (point)))
  13029. (confirm (lambda (x) (stringp (car x))))
  13030. (searchhead (equal (char-before beg) ?*))
  13031. (tag (and (equal (char-before beg1) ?:)
  13032. (equal (char-after (point-at-bol)) ?*)))
  13033. (prop (and (equal (char-before beg1) ?:)
  13034. (not (equal (char-after (point-at-bol)) ?*))))
  13035. (texp (equal (char-before beg) ?\\))
  13036. (link (equal (char-before beg) ?\[))
  13037. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13038. beg)
  13039. "#+"))
  13040. (startup (string-match "^#\\+STARTUP:.*"
  13041. (buffer-substring (point-at-bol) (point))))
  13042. (completion-ignore-case opt)
  13043. (type nil)
  13044. (tbl nil)
  13045. (table (cond
  13046. (opt
  13047. (setq type :opt)
  13048. (append
  13049. (mapcar
  13050. (lambda (x)
  13051. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13052. (cons (match-string 2 x) (match-string 1 x)))
  13053. (org-split-string (org-get-current-options) "\n"))
  13054. (mapcar 'list org-additional-option-like-keywords)))
  13055. (startup
  13056. (setq type :startup)
  13057. org-startup-options)
  13058. (link (append org-link-abbrev-alist-local
  13059. org-link-abbrev-alist))
  13060. (texp
  13061. (setq type :tex)
  13062. org-html-entities)
  13063. ((string-match "\\`\\*+[ \t]+\\'"
  13064. (buffer-substring (point-at-bol) beg))
  13065. (setq type :todo)
  13066. (mapcar 'list org-todo-keywords-1))
  13067. (searchhead
  13068. (setq type :searchhead)
  13069. (save-excursion
  13070. (goto-char (point-min))
  13071. (while (re-search-forward org-todo-line-regexp nil t)
  13072. (push (list
  13073. (org-make-org-heading-search-string
  13074. (match-string 3) t))
  13075. tbl)))
  13076. tbl)
  13077. (tag (setq type :tag beg beg1)
  13078. (or org-tag-alist (org-get-buffer-tags)))
  13079. (prop (setq type :prop beg beg1)
  13080. (mapcar 'list (org-buffer-property-keys nil t t)))
  13081. (t (progn
  13082. (call-interactively org-completion-fallback-command)
  13083. (throw 'exit nil)))))
  13084. (pattern (buffer-substring-no-properties beg end))
  13085. (completion (try-completion pattern table confirm)))
  13086. (cond ((eq completion t)
  13087. (if (not (assoc (upcase pattern) table))
  13088. (message "Already complete")
  13089. (if (equal type :opt)
  13090. (insert (substring (cdr (assoc (upcase pattern) table))
  13091. (length pattern)))
  13092. (if (memq type '(:tag :prop)) (insert ":")))))
  13093. ((null completion)
  13094. (message "Can't find completion for \"%s\"" pattern)
  13095. (ding))
  13096. ((not (string= pattern completion))
  13097. (delete-region beg end)
  13098. (if (string-match " +$" completion)
  13099. (setq completion (replace-match "" t t completion)))
  13100. (insert completion)
  13101. (if (get-buffer-window "*Completions*")
  13102. (delete-window (get-buffer-window "*Completions*")))
  13103. (if (assoc completion table)
  13104. (if (eq type :todo) (insert " ")
  13105. (if (memq type '(:tag :prop)) (insert ":"))))
  13106. (if (and (equal type :opt) (assoc completion table))
  13107. (message "%s" (substitute-command-keys
  13108. "Press \\[org-complete] again to insert example settings"))))
  13109. (t
  13110. (message "Making completion list...")
  13111. (let ((list (sort (all-completions pattern table confirm)
  13112. 'string<)))
  13113. (with-output-to-temp-buffer "*Completions*"
  13114. (condition-case nil
  13115. ;; Protection needed for XEmacs and emacs 21
  13116. (display-completion-list list pattern)
  13117. (error (display-completion-list list)))))
  13118. (message "Making completion list...%s" "done")))))))
  13119. ;;;; TODO, DEADLINE, Comments
  13120. (defun org-toggle-comment ()
  13121. "Change the COMMENT state of an entry."
  13122. (interactive)
  13123. (save-excursion
  13124. (org-back-to-heading)
  13125. (let (case-fold-search)
  13126. (if (looking-at (concat outline-regexp
  13127. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13128. (replace-match "" t t nil 1)
  13129. (if (looking-at outline-regexp)
  13130. (progn
  13131. (goto-char (match-end 0))
  13132. (insert org-comment-string " ")))))))
  13133. (defvar org-last-todo-state-is-todo nil
  13134. "This is non-nil when the last TODO state change led to a TODO state.
  13135. If the last change removed the TODO tag or switched to DONE, then
  13136. this is nil.")
  13137. (defvar org-setting-tags nil) ; dynamically skiped
  13138. ;; FIXME: better place
  13139. (defun org-property-or-variable-value (var &optional inherit)
  13140. "Check if there is a property fixing the value of VAR.
  13141. If yes, return this value. If not, return the current value of the variable."
  13142. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13143. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13144. (read prop)
  13145. (symbol-value var))))
  13146. (defun org-parse-local-options (string var)
  13147. "Parse STRING for startup setting relevant for variable VAR."
  13148. (let ((rtn (symbol-value var))
  13149. e opts)
  13150. (save-match-data
  13151. (if (or (not string) (not (string-match "\\S-" string)))
  13152. rtn
  13153. (setq opts (delq nil (mapcar (lambda (x)
  13154. (setq e (assoc x org-startup-options))
  13155. (if (eq (nth 1 e) var) e nil))
  13156. (org-split-string string "[ \t]+"))))
  13157. (if (not opts)
  13158. rtn
  13159. (setq rtn nil)
  13160. (while (setq e (pop opts))
  13161. (if (not (nth 3 e))
  13162. (setq rtn (nth 2 e))
  13163. (if (not (listp rtn)) (setq rtn nil))
  13164. (push (nth 2 e) rtn)))
  13165. rtn)))))
  13166. (defvar org-blocker-hook nil
  13167. "Hook for functions that are allowed to block a state change.
  13168. Each function gets as its single argument a property list, see
  13169. `org-trigger-hook' for more information about this list.
  13170. If any of the functions in this hook returns nil, the state change
  13171. is blocked.")
  13172. (defvar org-trigger-hook nil
  13173. "Hook for functions that are triggered by a state change.
  13174. Each function gets as its single argument a property list with at least
  13175. the following elements:
  13176. (:type type-of-change :position pos-at-entry-start
  13177. :from old-state :to new-state)
  13178. Depending on the type, more properties may be present.
  13179. This mechanism is currently implemented for:
  13180. TODO state changes
  13181. ------------------
  13182. :type todo-state-change
  13183. :from previous state (keyword as a string), or nil
  13184. :to new state (keyword as a string), or nil")
  13185. (defun org-todo (&optional arg)
  13186. "Change the TODO state of an item.
  13187. The state of an item is given by a keyword at the start of the heading,
  13188. like
  13189. *** TODO Write paper
  13190. *** DONE Call mom
  13191. The different keywords are specified in the variable `org-todo-keywords'.
  13192. By default the available states are \"TODO\" and \"DONE\".
  13193. So for this example: when the item starts with TODO, it is changed to DONE.
  13194. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13195. DONE are present, add TODO at the beginning of the heading.
  13196. With C-u prefix arg, use completion to determine the new state.
  13197. With numeric prefix arg, switch to that state.
  13198. For calling through lisp, arg is also interpreted in the following way:
  13199. 'none -> empty state
  13200. \"\"(empty string) -> switch to empty state
  13201. 'done -> switch to DONE
  13202. 'nextset -> switch to the next set of keywords
  13203. 'previousset -> switch to the previous set of keywords
  13204. \"WAITING\" -> switch to the specified keyword, but only if it
  13205. really is a member of `org-todo-keywords'."
  13206. (interactive "P")
  13207. (save-excursion
  13208. (catch 'exit
  13209. (org-back-to-heading)
  13210. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13211. (or (looking-at (concat " +" org-todo-regexp " *"))
  13212. (looking-at " *"))
  13213. (let* ((match-data (match-data))
  13214. (startpos (point-at-bol))
  13215. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13216. (org-log-done org-log-done)
  13217. (org-log-repeat org-log-repeat)
  13218. (org-todo-log-states org-todo-log-states)
  13219. (this (match-string 1))
  13220. (hl-pos (match-beginning 0))
  13221. (head (org-get-todo-sequence-head this))
  13222. (ass (assoc head org-todo-kwd-alist))
  13223. (interpret (nth 1 ass))
  13224. (done-word (nth 3 ass))
  13225. (final-done-word (nth 4 ass))
  13226. (last-state (or this ""))
  13227. (completion-ignore-case t)
  13228. (member (member this org-todo-keywords-1))
  13229. (tail (cdr member))
  13230. (state (cond
  13231. ((and org-todo-key-trigger
  13232. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13233. (and (not arg) org-use-fast-todo-selection
  13234. (not (eq org-use-fast-todo-selection 'prefix)))))
  13235. ;; Use fast selection
  13236. (org-fast-todo-selection))
  13237. ((and (equal arg '(4))
  13238. (or (not org-use-fast-todo-selection)
  13239. (not org-todo-key-trigger)))
  13240. ;; Read a state with completion
  13241. (completing-read "State: " (mapcar (lambda(x) (list x))
  13242. org-todo-keywords-1)
  13243. nil t))
  13244. ((eq arg 'right)
  13245. (if this
  13246. (if tail (car tail) nil)
  13247. (car org-todo-keywords-1)))
  13248. ((eq arg 'left)
  13249. (if (equal member org-todo-keywords-1)
  13250. nil
  13251. (if this
  13252. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13253. org-todo-keywords-1)
  13254. (org-last org-todo-keywords-1))))
  13255. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13256. (setq arg nil))) ; hack to fall back to cycling
  13257. (arg
  13258. ;; user or caller requests a specific state
  13259. (cond
  13260. ((equal arg "") nil)
  13261. ((eq arg 'none) nil)
  13262. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13263. ((eq arg 'nextset)
  13264. (or (car (cdr (member head org-todo-heads)))
  13265. (car org-todo-heads)))
  13266. ((eq arg 'previousset)
  13267. (let ((org-todo-heads (reverse org-todo-heads)))
  13268. (or (car (cdr (member head org-todo-heads)))
  13269. (car org-todo-heads))))
  13270. ((car (member arg org-todo-keywords-1)))
  13271. ((nth (1- (prefix-numeric-value arg))
  13272. org-todo-keywords-1))))
  13273. ((null member) (or head (car org-todo-keywords-1)))
  13274. ((equal this final-done-word) nil) ;; -> make empty
  13275. ((null tail) nil) ;; -> first entry
  13276. ((eq interpret 'sequence)
  13277. (car tail))
  13278. ((memq interpret '(type priority))
  13279. (if (eq this-command last-command)
  13280. (car tail)
  13281. (if (> (length tail) 0)
  13282. (or done-word (car org-done-keywords))
  13283. nil)))
  13284. (t nil)))
  13285. (next (if state (concat " " state " ") " "))
  13286. (change-plist (list :type 'todo-state-change :from this :to state
  13287. :position startpos))
  13288. dolog now-done-p)
  13289. (when org-blocker-hook
  13290. (unless (save-excursion
  13291. (save-match-data
  13292. (run-hook-with-args-until-failure
  13293. 'org-blocker-hook change-plist)))
  13294. (if (interactive-p)
  13295. (error "TODO state change from %s to %s blocked" this state)
  13296. ;; fail silently
  13297. (message "TODO state change from %s to %s blocked" this state)
  13298. (throw 'exit nil))))
  13299. (store-match-data match-data)
  13300. (replace-match next t t)
  13301. (unless (pos-visible-in-window-p hl-pos)
  13302. (message "TODO state changed to %s" (org-trim next)))
  13303. (unless head
  13304. (setq head (org-get-todo-sequence-head state)
  13305. ass (assoc head org-todo-kwd-alist)
  13306. interpret (nth 1 ass)
  13307. done-word (nth 3 ass)
  13308. final-done-word (nth 4 ass)))
  13309. (when (memq arg '(nextset previousset))
  13310. (message "Keyword-Set %d/%d: %s"
  13311. (- (length org-todo-sets) -1
  13312. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13313. (length org-todo-sets)
  13314. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13315. (setq org-last-todo-state-is-todo
  13316. (not (member state org-done-keywords)))
  13317. (setq now-done-p (and (member state org-done-keywords)
  13318. (not (member this org-done-keywords))))
  13319. (and logging (org-local-logging logging))
  13320. (when (and (or org-todo-log-states org-log-done)
  13321. (not (memq arg '(nextset previousset))))
  13322. ;; we need to look at recording a time and note
  13323. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13324. (nth 2 (assoc this org-todo-log-states))))
  13325. (when (and state
  13326. (member state org-not-done-keywords)
  13327. (not (member this org-not-done-keywords)))
  13328. ;; This is now a todo state and was not one before
  13329. ;; If there was a CLOSED time stamp, get rid of it.
  13330. (org-add-planning-info nil nil 'closed))
  13331. (when (and now-done-p org-log-done)
  13332. ;; It is now done, and it was not done before
  13333. (org-add-planning-info 'closed (org-current-time))
  13334. (if (and (not dolog) (eq 'note org-log-done))
  13335. (org-add-log-maybe 'done state 'findpos 'note)))
  13336. (when (and state dolog)
  13337. ;; This is a non-nil state, and we need to log it
  13338. (org-add-log-maybe 'state state 'findpos dolog)))
  13339. ;; Fixup tag positioning
  13340. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13341. (run-hooks 'org-after-todo-state-change-hook)
  13342. (if (and arg (not (member state org-done-keywords)))
  13343. (setq head (org-get-todo-sequence-head state)))
  13344. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13345. ;; Do we need to trigger a repeat?
  13346. (when now-done-p (org-auto-repeat-maybe state))
  13347. ;; Fixup cursor location if close to the keyword
  13348. (if (and (outline-on-heading-p)
  13349. (not (bolp))
  13350. (save-excursion (beginning-of-line 1)
  13351. (looking-at org-todo-line-regexp))
  13352. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13353. (progn
  13354. (goto-char (or (match-end 2) (match-end 1)))
  13355. (just-one-space)))
  13356. (when org-trigger-hook
  13357. (save-excursion
  13358. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13359. (defun org-local-logging (value)
  13360. "Get logging settings from a property VALUE."
  13361. (let* (words w a)
  13362. ;; directly set the variables, they are already local.
  13363. (setq org-log-done nil
  13364. org-log-repeat nil
  13365. org-todo-log-states nil)
  13366. (setq words (org-split-string value))
  13367. (while (setq w (pop words))
  13368. (cond
  13369. ((setq a (assoc w org-startup-options))
  13370. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13371. (set (nth 1 a) (nth 2 a))))
  13372. ((setq a (org-extract-log-state-settings w))
  13373. (and (member (car a) org-todo-keywords-1)
  13374. (push a org-todo-log-states)))))))
  13375. (defun org-get-todo-sequence-head (kwd)
  13376. "Return the head of the TODO sequence to which KWD belongs.
  13377. If KWD is not set, check if there is a text property remembering the
  13378. right sequence."
  13379. (let (p)
  13380. (cond
  13381. ((not kwd)
  13382. (or (get-text-property (point-at-bol) 'org-todo-head)
  13383. (progn
  13384. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13385. nil (point-at-eol)))
  13386. (get-text-property p 'org-todo-head))))
  13387. ((not (member kwd org-todo-keywords-1))
  13388. (car org-todo-keywords-1))
  13389. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13390. (defun org-fast-todo-selection ()
  13391. "Fast TODO keyword selection with single keys.
  13392. Returns the new TODO keyword, or nil if no state change should occur."
  13393. (let* ((fulltable org-todo-key-alist)
  13394. (done-keywords org-done-keywords) ;; needed for the faces.
  13395. (maxlen (apply 'max (mapcar
  13396. (lambda (x)
  13397. (if (stringp (car x)) (string-width (car x)) 0))
  13398. fulltable)))
  13399. (expert nil)
  13400. (fwidth (+ maxlen 3 1 3))
  13401. (ncol (/ (- (window-width) 4) fwidth))
  13402. tg cnt e c tbl
  13403. groups ingroup)
  13404. (save-window-excursion
  13405. (if expert
  13406. (set-buffer (get-buffer-create " *Org todo*"))
  13407. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13408. (erase-buffer)
  13409. (org-set-local 'org-done-keywords done-keywords)
  13410. (setq tbl fulltable cnt 0)
  13411. (while (setq e (pop tbl))
  13412. (cond
  13413. ((equal e '(:startgroup))
  13414. (push '() groups) (setq ingroup t)
  13415. (when (not (= cnt 0))
  13416. (setq cnt 0)
  13417. (insert "\n"))
  13418. (insert "{ "))
  13419. ((equal e '(:endgroup))
  13420. (setq ingroup nil cnt 0)
  13421. (insert "}\n"))
  13422. (t
  13423. (setq tg (car e) c (cdr e))
  13424. (if ingroup (push tg (car groups)))
  13425. (setq tg (org-add-props tg nil 'face
  13426. (org-get-todo-face tg)))
  13427. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13428. (insert "[" c "] " tg (make-string
  13429. (- fwidth 4 (length tg)) ?\ ))
  13430. (when (= (setq cnt (1+ cnt)) ncol)
  13431. (insert "\n")
  13432. (if ingroup (insert " "))
  13433. (setq cnt 0)))))
  13434. (insert "\n")
  13435. (goto-char (point-min))
  13436. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13437. (fit-window-to-buffer))
  13438. (message "[a-z..]:Set [SPC]:clear")
  13439. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13440. (cond
  13441. ((or (= c ?\C-g)
  13442. (and (= c ?q) (not (rassoc c fulltable))))
  13443. (setq quit-flag t))
  13444. ((= c ?\ ) nil)
  13445. ((setq e (rassoc c fulltable) tg (car e))
  13446. tg)
  13447. (t (setq quit-flag t))))))
  13448. (defun org-get-repeat ()
  13449. "Check if tere is a deadline/schedule with repeater in this entry."
  13450. (save-match-data
  13451. (save-excursion
  13452. (org-back-to-heading t)
  13453. (if (re-search-forward
  13454. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13455. (match-string 1)))))
  13456. (defvar org-last-changed-timestamp)
  13457. (defvar org-log-post-message)
  13458. (defvar org-log-note-purpose)
  13459. (defun org-auto-repeat-maybe (done-word)
  13460. "Check if the current headline contains a repeated deadline/schedule.
  13461. If yes, set TODO state back to what it was and change the base date
  13462. of repeating deadline/scheduled time stamps to new date.
  13463. This function should be run in the `org-after-todo-state-change-hook'."
  13464. ;; last-state is dynamically scoped into this function
  13465. (let* ((repeat (org-get-repeat))
  13466. (aa (assoc last-state org-todo-kwd-alist))
  13467. (interpret (nth 1 aa))
  13468. (head (nth 2 aa))
  13469. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13470. (msg "Entry repeats: ")
  13471. (org-log-done nil)
  13472. re type n what ts)
  13473. (when repeat
  13474. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13475. (org-todo (if (eq interpret 'type) last-state head))
  13476. (when (and org-log-repeat
  13477. (or (not (memq 'org-add-log-note
  13478. (default-value 'post-command-hook)))
  13479. (eq org-log-note-purpose 'done)))
  13480. ;; Make sure a note is taken;
  13481. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13482. 'findpos org-log-repeat))
  13483. (org-back-to-heading t)
  13484. (org-add-planning-info nil nil 'closed)
  13485. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13486. org-deadline-time-regexp "\\)\\|\\("
  13487. org-ts-regexp "\\)"))
  13488. (while (re-search-forward
  13489. re (save-excursion (outline-next-heading) (point)) t)
  13490. (setq type (if (match-end 1) org-scheduled-string
  13491. (if (match-end 3) org-deadline-string "Plain:"))
  13492. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13493. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13494. (setq n (string-to-number (match-string 1 ts))
  13495. what (match-string 2 ts))
  13496. (if (equal what "w") (setq n (* n 7) what "d"))
  13497. (org-timestamp-change n (cdr (assoc what whata)))
  13498. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13499. (setq org-log-post-message msg)
  13500. (message "%s" msg))))
  13501. (defun org-show-todo-tree (arg)
  13502. "Make a compact tree which shows all headlines marked with TODO.
  13503. The tree will show the lines where the regexp matches, and all higher
  13504. headlines above the match.
  13505. With \\[universal-argument] prefix, also show the DONE entries.
  13506. With a numeric prefix N, construct a sparse tree for the Nth element
  13507. of `org-todo-keywords-1'."
  13508. (interactive "P")
  13509. (let ((case-fold-search nil)
  13510. (kwd-re
  13511. (cond ((null arg) org-not-done-regexp)
  13512. ((equal arg '(4))
  13513. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13514. (mapcar 'list org-todo-keywords-1))))
  13515. (concat "\\("
  13516. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13517. "\\)\\>")))
  13518. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13519. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13520. org-todo-keywords-1)))
  13521. (t (error "Invalid prefix argument: %s" arg)))))
  13522. (message "%d TODO entries found"
  13523. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13524. (defun org-deadline (&optional remove)
  13525. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13526. With argument REMOVE, remove any deadline from the item."
  13527. (interactive "P")
  13528. (if remove
  13529. (progn
  13530. (org-remove-timestamp-with-keyword org-deadline-string)
  13531. (message "Item no longer has a deadline."))
  13532. (org-add-planning-info 'deadline nil 'closed)))
  13533. (defun org-schedule (&optional remove)
  13534. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13535. With argument REMOVE, remove any scheduling date from the item."
  13536. (interactive "P")
  13537. (if remove
  13538. (progn
  13539. (org-remove-timestamp-with-keyword org-scheduled-string)
  13540. (message "Item is no longer scheduled."))
  13541. (org-add-planning-info 'scheduled nil 'closed)))
  13542. (defun org-remove-timestamp-with-keyword (keyword)
  13543. "Remove all time stamps with KEYWORD in the current entry."
  13544. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13545. beg)
  13546. (save-excursion
  13547. (org-back-to-heading t)
  13548. (setq beg (point))
  13549. (org-end-of-subtree t t)
  13550. (while (re-search-backward re beg t)
  13551. (replace-match "")
  13552. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13553. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13554. (defun org-add-planning-info (what &optional time &rest remove)
  13555. "Insert new timestamp with keyword in the line directly after the headline.
  13556. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13557. If non is given, the user is prompted for a date.
  13558. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13559. be removed."
  13560. (interactive)
  13561. (let (org-time-was-given org-end-time-was-given)
  13562. (when what (setq time (or time (org-read-date nil 'to-time))))
  13563. (when (and org-insert-labeled-timestamps-at-point
  13564. (member what '(scheduled deadline)))
  13565. (insert
  13566. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13567. (org-insert-time-stamp time org-time-was-given
  13568. nil nil nil (list org-end-time-was-given))
  13569. (setq what nil))
  13570. (save-excursion
  13571. (save-restriction
  13572. (let (col list elt ts buffer-invisibility-spec)
  13573. (org-back-to-heading t)
  13574. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13575. (goto-char (match-end 1))
  13576. (setq col (current-column))
  13577. (goto-char (match-end 0))
  13578. (if (eobp) (insert "\n") (forward-char 1))
  13579. (if (and (not (looking-at outline-regexp))
  13580. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13581. "[^\r\n]*"))
  13582. (not (equal (match-string 1) org-clock-string)))
  13583. (narrow-to-region (match-beginning 0) (match-end 0))
  13584. (insert-before-markers "\n")
  13585. (backward-char 1)
  13586. (narrow-to-region (point) (point))
  13587. (indent-to-column col))
  13588. ;; Check if we have to remove something.
  13589. (setq list (cons what remove))
  13590. (while list
  13591. (setq elt (pop list))
  13592. (goto-char (point-min))
  13593. (when (or (and (eq elt 'scheduled)
  13594. (re-search-forward org-scheduled-time-regexp nil t))
  13595. (and (eq elt 'deadline)
  13596. (re-search-forward org-deadline-time-regexp nil t))
  13597. (and (eq elt 'closed)
  13598. (re-search-forward org-closed-time-regexp nil t)))
  13599. (replace-match "")
  13600. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13601. (if (looking-at " +") (replace-match ""))))
  13602. (goto-char (point-max))
  13603. (when what
  13604. (insert
  13605. (if (not (equal (char-before) ?\ )) " " "")
  13606. (cond ((eq what 'scheduled) org-scheduled-string)
  13607. ((eq what 'deadline) org-deadline-string)
  13608. ((eq what 'closed) org-closed-string))
  13609. " ")
  13610. (setq ts (org-insert-time-stamp
  13611. time
  13612. (or org-time-was-given
  13613. (and (eq what 'closed) org-log-done-with-time))
  13614. (eq what 'closed)
  13615. nil nil (list org-end-time-was-given)))
  13616. (end-of-line 1))
  13617. (goto-char (point-min))
  13618. (widen)
  13619. (if (looking-at "[ \t]+\r?\n")
  13620. (replace-match ""))
  13621. ts)))))
  13622. (defvar org-log-note-marker (make-marker))
  13623. (defvar org-log-note-purpose nil)
  13624. (defvar org-log-note-state nil)
  13625. (defvar org-log-note-how nil)
  13626. (defvar org-log-note-window-configuration nil)
  13627. (defvar org-log-note-return-to (make-marker))
  13628. (defvar org-log-post-message nil
  13629. "Message to be displayed after a log note has been stored.
  13630. The auto-repeater uses this.")
  13631. (defun org-add-log-maybe (&optional purpose state findpos how)
  13632. "Set up the post command hook to take a note.
  13633. If this is about to TODO state change, the new state is expected in STATE.
  13634. When FINDPOS is non-nil, find the correct position for the note in
  13635. the current entry. If not, assume that it can be inserted at point."
  13636. (save-excursion
  13637. (when findpos
  13638. (org-back-to-heading t)
  13639. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13640. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13641. "[^\r\n]*\\)?"))
  13642. (goto-char (match-end 0))
  13643. (unless org-log-states-order-reversed
  13644. (and (= (char-after) ?\n) (forward-char 1))
  13645. (org-skip-over-state-notes)
  13646. (skip-chars-backward " \t\n\r")))
  13647. (move-marker org-log-note-marker (point))
  13648. (setq org-log-note-purpose purpose
  13649. org-log-note-state state
  13650. org-log-note-how how)
  13651. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13652. (defun org-skip-over-state-notes ()
  13653. "Skip past the list of State notes in an entry."
  13654. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13655. (while (looking-at "[ \t]*- State")
  13656. (condition-case nil
  13657. (org-next-item)
  13658. (error (org-end-of-item)))))
  13659. (defun org-add-log-note (&optional purpose)
  13660. "Pop up a window for taking a note, and add this note later at point."
  13661. (remove-hook 'post-command-hook 'org-add-log-note)
  13662. (setq org-log-note-window-configuration (current-window-configuration))
  13663. (delete-other-windows)
  13664. (move-marker org-log-note-return-to (point))
  13665. (switch-to-buffer (marker-buffer org-log-note-marker))
  13666. (goto-char org-log-note-marker)
  13667. (org-switch-to-buffer-other-window "*Org Note*")
  13668. (erase-buffer)
  13669. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13670. (org-store-log-note)
  13671. (let ((org-inhibit-startup t)) (org-mode))
  13672. (insert (format "# Insert note for %s.
  13673. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13674. (cond
  13675. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13676. ((eq org-log-note-purpose 'done) "closed todo item")
  13677. ((eq org-log-note-purpose 'state)
  13678. (format "state change to \"%s\"" org-log-note-state))
  13679. (t (error "This should not happen")))))
  13680. (org-set-local 'org-finish-function 'org-store-log-note)))
  13681. (defun org-store-log-note ()
  13682. "Finish taking a log note, and insert it to where it belongs."
  13683. (let ((txt (buffer-string))
  13684. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13685. lines ind)
  13686. (kill-buffer (current-buffer))
  13687. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13688. (setq txt (replace-match "" t t txt)))
  13689. (if (string-match "\\s-+\\'" txt)
  13690. (setq txt (replace-match "" t t txt)))
  13691. (setq lines (org-split-string txt "\n"))
  13692. (when (and note (string-match "\\S-" note))
  13693. (setq note
  13694. (org-replace-escapes
  13695. note
  13696. (list (cons "%u" (user-login-name))
  13697. (cons "%U" user-full-name)
  13698. (cons "%t" (format-time-string
  13699. (org-time-stamp-format 'long 'inactive)
  13700. (current-time)))
  13701. (cons "%s" (if org-log-note-state
  13702. (concat "\"" org-log-note-state "\"")
  13703. "")))))
  13704. (if lines (setq note (concat note " \\\\")))
  13705. (push note lines))
  13706. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13707. (when lines
  13708. (save-excursion
  13709. (set-buffer (marker-buffer org-log-note-marker))
  13710. (save-excursion
  13711. (goto-char org-log-note-marker)
  13712. (move-marker org-log-note-marker nil)
  13713. (end-of-line 1)
  13714. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13715. (indent-relative nil)
  13716. (insert "- " (pop lines))
  13717. (org-indent-line-function)
  13718. (beginning-of-line 1)
  13719. (looking-at "[ \t]*")
  13720. (setq ind (concat (match-string 0) " "))
  13721. (end-of-line 1)
  13722. (while lines (insert "\n" ind (pop lines)))))))
  13723. (set-window-configuration org-log-note-window-configuration)
  13724. (with-current-buffer (marker-buffer org-log-note-return-to)
  13725. (goto-char org-log-note-return-to))
  13726. (move-marker org-log-note-return-to nil)
  13727. (and org-log-post-message (message "%s" org-log-post-message)))
  13728. ;; FIXME: what else would be useful?
  13729. ;; - priority
  13730. ;; - date
  13731. (defun org-sparse-tree (&optional arg)
  13732. "Create a sparse tree, prompt for the details.
  13733. This command can create sparse trees. You first need to select the type
  13734. of match used to create the tree:
  13735. t Show entries with a specific TODO keyword.
  13736. T Show entries selected by a tags match.
  13737. p Enter a property name and its value (both with completion on existing
  13738. names/values) and show entries with that property.
  13739. r Show entries matching a regular expression
  13740. d Show deadlines due within `org-deadline-warning-days'."
  13741. (interactive "P")
  13742. (let (ans kwd value)
  13743. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13744. (setq ans (read-char-exclusive))
  13745. (cond
  13746. ((equal ans ?d)
  13747. (call-interactively 'org-check-deadlines))
  13748. ((equal ans ?b)
  13749. (call-interactively 'org-check-before-date))
  13750. ((equal ans ?t)
  13751. (org-show-todo-tree '(4)))
  13752. ((equal ans ?T)
  13753. (call-interactively 'org-tags-sparse-tree))
  13754. ((member ans '(?p ?P))
  13755. (setq kwd (completing-read "Property: "
  13756. (mapcar 'list (org-buffer-property-keys))))
  13757. (setq value (completing-read "Value: "
  13758. (mapcar 'list (org-property-values kwd))))
  13759. (unless (string-match "\\`{.*}\\'" value)
  13760. (setq value (concat "\"" value "\"")))
  13761. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13762. ((member ans '(?r ?R ?/))
  13763. (call-interactively 'org-occur))
  13764. (t (error "No such sparse tree command \"%c\"" ans)))))
  13765. (defvar org-occur-highlights nil)
  13766. (make-variable-buffer-local 'org-occur-highlights)
  13767. (defun org-occur (regexp &optional keep-previous callback)
  13768. "Make a compact tree which shows all matches of REGEXP.
  13769. The tree will show the lines where the regexp matches, and all higher
  13770. headlines above the match. It will also show the heading after the match,
  13771. to make sure editing the matching entry is easy.
  13772. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13773. call to `org-occur' will be kept, to allow stacking of calls to this
  13774. command.
  13775. If CALLBACK is non-nil, it is a function which is called to confirm
  13776. that the match should indeed be shown."
  13777. (interactive "sRegexp: \nP")
  13778. (or keep-previous (org-remove-occur-highlights nil nil t))
  13779. (let ((cnt 0))
  13780. (save-excursion
  13781. (goto-char (point-min))
  13782. (if (or (not keep-previous) ; do not want to keep
  13783. (not org-occur-highlights)) ; no previous matches
  13784. ;; hide everything
  13785. (org-overview))
  13786. (while (re-search-forward regexp nil t)
  13787. (when (or (not callback)
  13788. (save-match-data (funcall callback)))
  13789. (setq cnt (1+ cnt))
  13790. (when org-highlight-sparse-tree-matches
  13791. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13792. (org-show-context 'occur-tree))))
  13793. (when org-remove-highlights-with-change
  13794. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13795. nil 'local))
  13796. (unless org-sparse-tree-open-archived-trees
  13797. (org-hide-archived-subtrees (point-min) (point-max)))
  13798. (run-hooks 'org-occur-hook)
  13799. (if (interactive-p)
  13800. (message "%d match(es) for regexp %s" cnt regexp))
  13801. cnt))
  13802. (defun org-show-context (&optional key)
  13803. "Make sure point and context and visible.
  13804. How much context is shown depends upon the variables
  13805. `org-show-hierarchy-above', `org-show-following-heading'. and
  13806. `org-show-siblings'."
  13807. (let ((heading-p (org-on-heading-p t))
  13808. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13809. (following-p (org-get-alist-option org-show-following-heading key))
  13810. (entry-p (org-get-alist-option org-show-entry-below key))
  13811. (siblings-p (org-get-alist-option org-show-siblings key)))
  13812. (catch 'exit
  13813. ;; Show heading or entry text
  13814. (if (and heading-p (not entry-p))
  13815. (org-flag-heading nil) ; only show the heading
  13816. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13817. (org-show-hidden-entry))) ; show entire entry
  13818. (when following-p
  13819. ;; Show next sibling, or heading below text
  13820. (save-excursion
  13821. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13822. (org-flag-heading nil))))
  13823. (when siblings-p (org-show-siblings))
  13824. (when hierarchy-p
  13825. ;; show all higher headings, possibly with siblings
  13826. (save-excursion
  13827. (while (and (condition-case nil
  13828. (progn (org-up-heading-all 1) t)
  13829. (error nil))
  13830. (not (bobp)))
  13831. (org-flag-heading nil)
  13832. (when siblings-p (org-show-siblings))))))))
  13833. (defun org-reveal (&optional siblings)
  13834. "Show current entry, hierarchy above it, and the following headline.
  13835. This can be used to show a consistent set of context around locations
  13836. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13837. not t for the search context.
  13838. With optional argument SIBLINGS, on each level of the hierarchy all
  13839. siblings are shown. This repairs the tree structure to what it would
  13840. look like when opened with hierarchical calls to `org-cycle'."
  13841. (interactive "P")
  13842. (let ((org-show-hierarchy-above t)
  13843. (org-show-following-heading t)
  13844. (org-show-siblings (if siblings t org-show-siblings)))
  13845. (org-show-context nil)))
  13846. (defun org-highlight-new-match (beg end)
  13847. "Highlight from BEG to END and mark the highlight is an occur headline."
  13848. (let ((ov (org-make-overlay beg end)))
  13849. (org-overlay-put ov 'face 'secondary-selection)
  13850. (push ov org-occur-highlights)))
  13851. (defun org-remove-occur-highlights (&optional beg end noremove)
  13852. "Remove the occur highlights from the buffer.
  13853. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13854. from the `before-change-functions' in the current buffer."
  13855. (interactive)
  13856. (unless org-inhibit-highlight-removal
  13857. (mapc 'org-delete-overlay org-occur-highlights)
  13858. (setq org-occur-highlights nil)
  13859. (unless noremove
  13860. (remove-hook 'before-change-functions
  13861. 'org-remove-occur-highlights 'local))))
  13862. ;;;; Priorities
  13863. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13864. "Regular expression matching the priority indicator.")
  13865. (defvar org-remove-priority-next-time nil)
  13866. (defun org-priority-up ()
  13867. "Increase the priority of the current item."
  13868. (interactive)
  13869. (org-priority 'up))
  13870. (defun org-priority-down ()
  13871. "Decrease the priority of the current item."
  13872. (interactive)
  13873. (org-priority 'down))
  13874. (defun org-priority (&optional action)
  13875. "Change the priority of an item by ARG.
  13876. ACTION can be `set', `up', `down', or a character."
  13877. (interactive)
  13878. (setq action (or action 'set))
  13879. (let (current new news have remove)
  13880. (save-excursion
  13881. (org-back-to-heading)
  13882. (if (looking-at org-priority-regexp)
  13883. (setq current (string-to-char (match-string 2))
  13884. have t)
  13885. (setq current org-default-priority))
  13886. (cond
  13887. ((or (eq action 'set) (integerp action))
  13888. (if (integerp action)
  13889. (setq new action)
  13890. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13891. (setq new (read-char-exclusive)))
  13892. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13893. (= (upcase org-lowest-priority) org-lowest-priority))
  13894. (setq new (upcase new)))
  13895. (cond ((equal new ?\ ) (setq remove t))
  13896. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13897. (error "Priority must be between `%c' and `%c'"
  13898. org-highest-priority org-lowest-priority))))
  13899. ((eq action 'up)
  13900. (if (and (not have) (eq last-command this-command))
  13901. (setq new org-lowest-priority)
  13902. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13903. org-default-priority (1- current)))))
  13904. ((eq action 'down)
  13905. (if (and (not have) (eq last-command this-command))
  13906. (setq new org-highest-priority)
  13907. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13908. org-default-priority (1+ current)))))
  13909. (t (error "Invalid action")))
  13910. (if (or (< (upcase new) org-highest-priority)
  13911. (> (upcase new) org-lowest-priority))
  13912. (setq remove t))
  13913. (setq news (format "%c" new))
  13914. (if have
  13915. (if remove
  13916. (replace-match "" t t nil 1)
  13917. (replace-match news t t nil 2))
  13918. (if remove
  13919. (error "No priority cookie found in line")
  13920. (looking-at org-todo-line-regexp)
  13921. (if (match-end 2)
  13922. (progn
  13923. (goto-char (match-end 2))
  13924. (insert " [#" news "]"))
  13925. (goto-char (match-beginning 3))
  13926. (insert "[#" news "] ")))))
  13927. (org-preserve-lc (org-set-tags nil 'align))
  13928. (if remove
  13929. (message "Priority removed")
  13930. (message "Priority of current item set to %s" news))))
  13931. (defun org-get-priority (s)
  13932. "Find priority cookie and return priority."
  13933. (save-match-data
  13934. (if (not (string-match org-priority-regexp s))
  13935. (* 1000 (- org-lowest-priority org-default-priority))
  13936. (* 1000 (- org-lowest-priority
  13937. (string-to-char (match-string 2 s)))))))
  13938. ;;;; Tags
  13939. (defun org-scan-tags (action matcher &optional todo-only)
  13940. "Scan headline tags with inheritance and produce output ACTION.
  13941. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13942. evaluated, testing if a given set of tags qualifies a headline for
  13943. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13944. are included in the output."
  13945. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13946. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13947. (org-re
  13948. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13949. (props (list 'face nil
  13950. 'done-face 'org-done
  13951. 'undone-face nil
  13952. 'mouse-face 'highlight
  13953. 'org-not-done-regexp org-not-done-regexp
  13954. 'org-todo-regexp org-todo-regexp
  13955. 'keymap org-agenda-keymap
  13956. 'help-echo
  13957. (format "mouse-2 or RET jump to org file %s"
  13958. (abbreviate-file-name
  13959. (or (buffer-file-name (buffer-base-buffer))
  13960. (buffer-name (buffer-base-buffer)))))))
  13961. (case-fold-search nil)
  13962. lspos
  13963. tags tags-list tags-alist (llast 0) rtn level category i txt
  13964. todo marker entry priority)
  13965. (save-excursion
  13966. (goto-char (point-min))
  13967. (when (eq action 'sparse-tree)
  13968. (org-overview)
  13969. (org-remove-occur-highlights))
  13970. (while (re-search-forward re nil t)
  13971. (catch :skip
  13972. (setq todo (if (match-end 1) (match-string 2))
  13973. tags (if (match-end 4) (match-string 4)))
  13974. (goto-char (setq lspos (1+ (match-beginning 0))))
  13975. (setq level (org-reduced-level (funcall outline-level))
  13976. category (org-get-category))
  13977. (setq i llast llast level)
  13978. ;; remove tag lists from same and sublevels
  13979. (while (>= i level)
  13980. (when (setq entry (assoc i tags-alist))
  13981. (setq tags-alist (delete entry tags-alist)))
  13982. (setq i (1- i)))
  13983. ;; add the nex tags
  13984. (when tags
  13985. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13986. tags-alist
  13987. (cons (cons level tags) tags-alist)))
  13988. ;; compile tags for current headline
  13989. (setq tags-list
  13990. (if org-use-tag-inheritance
  13991. (apply 'append (mapcar 'cdr tags-alist))
  13992. tags))
  13993. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13994. (eval matcher)
  13995. (or (not org-agenda-skip-archived-trees)
  13996. (not (member org-archive-tag tags-list))))
  13997. (and (eq action 'agenda) (org-agenda-skip))
  13998. ;; list this headline
  13999. (if (eq action 'sparse-tree)
  14000. (progn
  14001. (and org-highlight-sparse-tree-matches
  14002. (org-get-heading) (match-end 0)
  14003. (org-highlight-new-match
  14004. (match-beginning 0) (match-beginning 1)))
  14005. (org-show-context 'tags-tree))
  14006. (setq txt (org-format-agenda-item
  14007. ""
  14008. (concat
  14009. (if org-tags-match-list-sublevels
  14010. (make-string (1- level) ?.) "")
  14011. (org-get-heading))
  14012. category tags-list)
  14013. priority (org-get-priority txt))
  14014. (goto-char lspos)
  14015. (setq marker (org-agenda-new-marker))
  14016. (org-add-props txt props
  14017. 'org-marker marker 'org-hd-marker marker 'org-category category
  14018. 'priority priority 'type "tagsmatch")
  14019. (push txt rtn))
  14020. ;; if we are to skip sublevels, jump to end of subtree
  14021. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14022. (when (and (eq action 'sparse-tree)
  14023. (not org-sparse-tree-open-archived-trees))
  14024. (org-hide-archived-subtrees (point-min) (point-max)))
  14025. (nreverse rtn)))
  14026. (defvar todo-only) ;; dynamically scoped
  14027. (defun org-tags-sparse-tree (&optional todo-only match)
  14028. "Create a sparse tree according to tags string MATCH.
  14029. MATCH can contain positive and negative selection of tags, like
  14030. \"+WORK+URGENT-WITHBOSS\".
  14031. If optional argument TODO_ONLY is non-nil, only select lines that are
  14032. also TODO lines."
  14033. (interactive "P")
  14034. (org-prepare-agenda-buffers (list (current-buffer)))
  14035. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14036. (defvar org-cached-props nil)
  14037. (defun org-cached-entry-get (pom property)
  14038. (if (or (eq t org-use-property-inheritance)
  14039. (member property org-use-property-inheritance))
  14040. ;; Caching is not possible, check it directly
  14041. (org-entry-get pom property 'inherit)
  14042. ;; Get all properties, so that we can do complicated checks easily
  14043. (cdr (assoc property (or org-cached-props
  14044. (setq org-cached-props
  14045. (org-entry-properties pom)))))))
  14046. (defun org-global-tags-completion-table (&optional files)
  14047. "Return the list of all tags in all agenda buffer/files."
  14048. (save-excursion
  14049. (org-uniquify
  14050. (delq nil
  14051. (apply 'append
  14052. (mapcar
  14053. (lambda (file)
  14054. (set-buffer (find-file-noselect file))
  14055. (append (org-get-buffer-tags)
  14056. (mapcar (lambda (x) (if (stringp (car-safe x))
  14057. (list (car-safe x)) nil))
  14058. org-tag-alist)))
  14059. (if (and files (car files))
  14060. files
  14061. (org-agenda-files))))))))
  14062. (defun org-make-tags-matcher (match)
  14063. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14064. ;; todo-only is scoped dynamically into this function, and the function
  14065. ;; may change it it the matcher asksk for it.
  14066. (unless match
  14067. ;; Get a new match request, with completion
  14068. (let ((org-last-tags-completion-table
  14069. (org-global-tags-completion-table)))
  14070. (setq match (completing-read
  14071. "Match: " 'org-tags-completion-function nil nil nil
  14072. 'org-tags-history))))
  14073. ;; Parse the string and create a lisp form
  14074. (let ((match0 match)
  14075. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14076. minus tag mm
  14077. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14078. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14079. (if (string-match "/+" match)
  14080. ;; match contains also a todo-matching request
  14081. (progn
  14082. (setq tagsmatch (substring match 0 (match-beginning 0))
  14083. todomatch (substring match (match-end 0)))
  14084. (if (string-match "^!" todomatch)
  14085. (setq todo-only t todomatch (substring todomatch 1)))
  14086. (if (string-match "^\\s-*$" todomatch)
  14087. (setq todomatch nil)))
  14088. ;; only matching tags
  14089. (setq tagsmatch match todomatch nil))
  14090. ;; Make the tags matcher
  14091. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14092. (setq tagsmatcher t)
  14093. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14094. (while (setq term (pop orterms))
  14095. (while (and (equal (substring term -1) "\\") orterms)
  14096. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14097. (while (string-match re term)
  14098. (setq minus (and (match-end 1)
  14099. (equal (match-string 1 term) "-"))
  14100. tag (match-string 2 term)
  14101. re-p (equal (string-to-char tag) ?{)
  14102. level-p (match-end 3)
  14103. prop-p (match-end 4)
  14104. mm (cond
  14105. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14106. (level-p `(= level ,(string-to-number
  14107. (match-string 3 term))))
  14108. (prop-p
  14109. (setq pn (match-string 4 term)
  14110. pv (match-string 5 term)
  14111. cat-p (equal pn "CATEGORY")
  14112. re-p (equal (string-to-char pv) ?{)
  14113. pv (substring pv 1 -1))
  14114. (if (equal pn "CATEGORY")
  14115. (setq gv '(get-text-property (point) 'org-category))
  14116. (setq gv `(org-cached-entry-get nil ,pn)))
  14117. (if re-p
  14118. `(string-match ,pv (or ,gv ""))
  14119. `(equal ,pv (or ,gv ""))))
  14120. (t `(member ,(downcase tag) tags-list)))
  14121. mm (if minus (list 'not mm) mm)
  14122. term (substring term (match-end 0)))
  14123. (push mm tagsmatcher))
  14124. (push (if (> (length tagsmatcher) 1)
  14125. (cons 'and tagsmatcher)
  14126. (car tagsmatcher))
  14127. orlist)
  14128. (setq tagsmatcher nil))
  14129. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14130. (setq tagsmatcher
  14131. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14132. ;; Make the todo matcher
  14133. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14134. (setq todomatcher t)
  14135. (setq orterms (org-split-string todomatch "|") orlist nil)
  14136. (while (setq term (pop orterms))
  14137. (while (string-match re term)
  14138. (setq minus (and (match-end 1)
  14139. (equal (match-string 1 term) "-"))
  14140. kwd (match-string 2 term)
  14141. re-p (equal (string-to-char kwd) ?{)
  14142. term (substring term (match-end 0))
  14143. mm (if re-p
  14144. `(string-match ,(substring kwd 1 -1) todo)
  14145. (list 'equal 'todo kwd))
  14146. mm (if minus (list 'not mm) mm))
  14147. (push mm todomatcher))
  14148. (push (if (> (length todomatcher) 1)
  14149. (cons 'and todomatcher)
  14150. (car todomatcher))
  14151. orlist)
  14152. (setq todomatcher nil))
  14153. (setq todomatcher (if (> (length orlist) 1)
  14154. (cons 'or orlist) (car orlist))))
  14155. ;; Return the string and lisp forms of the matcher
  14156. (setq matcher (if todomatcher
  14157. (list 'and tagsmatcher todomatcher)
  14158. tagsmatcher))
  14159. (cons match0 matcher)))
  14160. (defun org-match-any-p (re list)
  14161. "Does re match any element of list?"
  14162. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14163. (delq nil list))
  14164. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14165. (defvar org-tags-overlay (org-make-overlay 1 1))
  14166. (org-detach-overlay org-tags-overlay)
  14167. (defun org-align-tags-here (to-col)
  14168. ;; Assumes that this is a headline
  14169. (let ((pos (point)) (col (current-column)) tags)
  14170. (beginning-of-line 1)
  14171. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14172. (< pos (match-beginning 2)))
  14173. (progn
  14174. (setq tags (match-string 2))
  14175. (goto-char (match-beginning 1))
  14176. (insert " ")
  14177. (delete-region (point) (1+ (match-end 0)))
  14178. (backward-char 1)
  14179. (move-to-column
  14180. (max (1+ (current-column))
  14181. (1+ col)
  14182. (if (> to-col 0)
  14183. to-col
  14184. (- (abs to-col) (length tags))))
  14185. t)
  14186. (insert tags)
  14187. (move-to-column (min (current-column) col) t))
  14188. (goto-char pos))))
  14189. (defun org-set-tags (&optional arg just-align)
  14190. "Set the tags for the current headline.
  14191. With prefix ARG, realign all tags in headings in the current buffer."
  14192. (interactive "P")
  14193. (let* ((re (concat "^" outline-regexp))
  14194. (current (org-get-tags-string))
  14195. (col (current-column))
  14196. (org-setting-tags t)
  14197. table current-tags inherited-tags ; computed below when needed
  14198. tags p0 c0 c1 rpl)
  14199. (if arg
  14200. (save-excursion
  14201. (goto-char (point-min))
  14202. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14203. (while (re-search-forward re nil t)
  14204. (org-set-tags nil t)
  14205. (end-of-line 1)))
  14206. (message "All tags realigned to column %d" org-tags-column))
  14207. (if just-align
  14208. (setq tags current)
  14209. ;; Get a new set of tags from the user
  14210. (save-excursion
  14211. (setq table (or org-tag-alist (org-get-buffer-tags))
  14212. org-last-tags-completion-table table
  14213. current-tags (org-split-string current ":")
  14214. inherited-tags (nreverse
  14215. (nthcdr (length current-tags)
  14216. (nreverse (org-get-tags-at))))
  14217. tags
  14218. (if (or (eq t org-use-fast-tag-selection)
  14219. (and org-use-fast-tag-selection
  14220. (delq nil (mapcar 'cdr table))))
  14221. (org-fast-tag-selection
  14222. current-tags inherited-tags table
  14223. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14224. (let ((org-add-colon-after-tag-completion t))
  14225. (org-trim
  14226. (org-without-partial-completion
  14227. (completing-read "Tags: " 'org-tags-completion-function
  14228. nil nil current 'org-tags-history)))))))
  14229. (while (string-match "[-+&]+" tags)
  14230. ;; No boolean logic, just a list
  14231. (setq tags (replace-match ":" t t tags))))
  14232. (if (string-match "\\`[\t ]*\\'" tags)
  14233. (setq tags "")
  14234. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14235. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14236. ;; Insert new tags at the correct column
  14237. (beginning-of-line 1)
  14238. (cond
  14239. ((and (equal current "") (equal tags "")))
  14240. ((re-search-forward
  14241. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14242. (point-at-eol) t)
  14243. (if (equal tags "")
  14244. (setq rpl "")
  14245. (goto-char (match-beginning 0))
  14246. (setq c0 (current-column) p0 (point)
  14247. c1 (max (1+ c0) (if (> org-tags-column 0)
  14248. org-tags-column
  14249. (- (- org-tags-column) (length tags))))
  14250. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14251. (replace-match rpl t t)
  14252. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14253. tags)
  14254. (t (error "Tags alignment failed")))
  14255. (move-to-column col)
  14256. (unless just-align
  14257. (run-hooks 'org-after-tags-change-hook)))))
  14258. (defun org-change-tag-in-region (beg end tag off)
  14259. "Add or remove TAG for each entry in the region.
  14260. This works in the agenda, and also in an org-mode buffer."
  14261. (interactive
  14262. (list (region-beginning) (region-end)
  14263. (let ((org-last-tags-completion-table
  14264. (if (org-mode-p)
  14265. (org-get-buffer-tags)
  14266. (org-global-tags-completion-table))))
  14267. (completing-read
  14268. "Tag: " 'org-tags-completion-function nil nil nil
  14269. 'org-tags-history))
  14270. (progn
  14271. (message "[s]et or [r]emove? ")
  14272. (equal (read-char-exclusive) ?r))))
  14273. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14274. (let ((agendap (equal major-mode 'org-agenda-mode))
  14275. l1 l2 m buf pos newhead (cnt 0))
  14276. (goto-char end)
  14277. (setq l2 (1- (org-current-line)))
  14278. (goto-char beg)
  14279. (setq l1 (org-current-line))
  14280. (loop for l from l1 to l2 do
  14281. (goto-line l)
  14282. (setq m (get-text-property (point) 'org-hd-marker))
  14283. (when (or (and (org-mode-p) (org-on-heading-p))
  14284. (and agendap m))
  14285. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14286. pos (if agendap m (point)))
  14287. (with-current-buffer buf
  14288. (save-excursion
  14289. (save-restriction
  14290. (goto-char pos)
  14291. (setq cnt (1+ cnt))
  14292. (org-toggle-tag tag (if off 'off 'on))
  14293. (setq newhead (org-get-heading)))))
  14294. (and agendap (org-agenda-change-all-lines newhead m))))
  14295. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14296. (defun org-tags-completion-function (string predicate &optional flag)
  14297. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14298. (confirm (lambda (x) (stringp (car x)))))
  14299. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14300. (setq s1 (match-string 1 string)
  14301. s2 (match-string 2 string))
  14302. (setq s1 "" s2 string))
  14303. (cond
  14304. ((eq flag nil)
  14305. ;; try completion
  14306. (setq rtn (try-completion s2 ctable confirm))
  14307. (if (stringp rtn)
  14308. (setq rtn
  14309. (concat s1 s2 (substring rtn (length s2))
  14310. (if (and org-add-colon-after-tag-completion
  14311. (assoc rtn ctable))
  14312. ":" ""))))
  14313. rtn)
  14314. ((eq flag t)
  14315. ;; all-completions
  14316. (all-completions s2 ctable confirm)
  14317. )
  14318. ((eq flag 'lambda)
  14319. ;; exact match?
  14320. (assoc s2 ctable)))
  14321. ))
  14322. (defun org-fast-tag-insert (kwd tags face &optional end)
  14323. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14324. (insert (format "%-12s" (concat kwd ":"))
  14325. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14326. (or end "")))
  14327. (defun org-fast-tag-show-exit (flag)
  14328. (save-excursion
  14329. (goto-line 3)
  14330. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14331. (replace-match ""))
  14332. (when flag
  14333. (end-of-line 1)
  14334. (move-to-column (- (window-width) 19) t)
  14335. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14336. (defun org-set-current-tags-overlay (current prefix)
  14337. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14338. (if (featurep 'xemacs)
  14339. (org-overlay-display org-tags-overlay (concat prefix s)
  14340. 'secondary-selection)
  14341. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14342. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14343. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14344. "Fast tag selection with single keys.
  14345. CURRENT is the current list of tags in the headline, INHERITED is the
  14346. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14347. possibly with grouping information. TODO-TABLE is a similar table with
  14348. TODO keywords, should these have keys assigned to them.
  14349. If the keys are nil, a-z are automatically assigned.
  14350. Returns the new tags string, or nil to not change the current settings."
  14351. (let* ((fulltable (append table todo-table))
  14352. (maxlen (apply 'max (mapcar
  14353. (lambda (x)
  14354. (if (stringp (car x)) (string-width (car x)) 0))
  14355. fulltable)))
  14356. (buf (current-buffer))
  14357. (expert (eq org-fast-tag-selection-single-key 'expert))
  14358. (buffer-tags nil)
  14359. (fwidth (+ maxlen 3 1 3))
  14360. (ncol (/ (- (window-width) 4) fwidth))
  14361. (i-face 'org-done)
  14362. (c-face 'org-todo)
  14363. tg cnt e c char c1 c2 ntable tbl rtn
  14364. ov-start ov-end ov-prefix
  14365. (exit-after-next org-fast-tag-selection-single-key)
  14366. (done-keywords org-done-keywords)
  14367. groups ingroup)
  14368. (save-excursion
  14369. (beginning-of-line 1)
  14370. (if (looking-at
  14371. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14372. (setq ov-start (match-beginning 1)
  14373. ov-end (match-end 1)
  14374. ov-prefix "")
  14375. (setq ov-start (1- (point-at-eol))
  14376. ov-end (1+ ov-start))
  14377. (skip-chars-forward "^\n\r")
  14378. (setq ov-prefix
  14379. (concat
  14380. (buffer-substring (1- (point)) (point))
  14381. (if (> (current-column) org-tags-column)
  14382. " "
  14383. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14384. (org-move-overlay org-tags-overlay ov-start ov-end)
  14385. (save-window-excursion
  14386. (if expert
  14387. (set-buffer (get-buffer-create " *Org tags*"))
  14388. (delete-other-windows)
  14389. (split-window-vertically)
  14390. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14391. (erase-buffer)
  14392. (org-set-local 'org-done-keywords done-keywords)
  14393. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14394. (org-fast-tag-insert "Current" current c-face "\n\n")
  14395. (org-fast-tag-show-exit exit-after-next)
  14396. (org-set-current-tags-overlay current ov-prefix)
  14397. (setq tbl fulltable char ?a cnt 0)
  14398. (while (setq e (pop tbl))
  14399. (cond
  14400. ((equal e '(:startgroup))
  14401. (push '() groups) (setq ingroup t)
  14402. (when (not (= cnt 0))
  14403. (setq cnt 0)
  14404. (insert "\n"))
  14405. (insert "{ "))
  14406. ((equal e '(:endgroup))
  14407. (setq ingroup nil cnt 0)
  14408. (insert "}\n"))
  14409. (t
  14410. (setq tg (car e) c2 nil)
  14411. (if (cdr e)
  14412. (setq c (cdr e))
  14413. ;; automatically assign a character.
  14414. (setq c1 (string-to-char
  14415. (downcase (substring
  14416. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14417. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14418. (while (or (rassoc char ntable) (rassoc char table))
  14419. (setq char (1+ char)))
  14420. (setq c2 c1))
  14421. (setq c (or c2 char)))
  14422. (if ingroup (push tg (car groups)))
  14423. (setq tg (org-add-props tg nil 'face
  14424. (cond
  14425. ((not (assoc tg table))
  14426. (org-get-todo-face tg))
  14427. ((member tg current) c-face)
  14428. ((member tg inherited) i-face)
  14429. (t nil))))
  14430. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14431. (insert "[" c "] " tg (make-string
  14432. (- fwidth 4 (length tg)) ?\ ))
  14433. (push (cons tg c) ntable)
  14434. (when (= (setq cnt (1+ cnt)) ncol)
  14435. (insert "\n")
  14436. (if ingroup (insert " "))
  14437. (setq cnt 0)))))
  14438. (setq ntable (nreverse ntable))
  14439. (insert "\n")
  14440. (goto-char (point-min))
  14441. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14442. (fit-window-to-buffer))
  14443. (setq rtn
  14444. (catch 'exit
  14445. (while t
  14446. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14447. (if groups " [!] no groups" " [!]groups")
  14448. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14449. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14450. (cond
  14451. ((= c ?\r) (throw 'exit t))
  14452. ((= c ?!)
  14453. (setq groups (not groups))
  14454. (goto-char (point-min))
  14455. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14456. ((= c ?\C-c)
  14457. (if (not expert)
  14458. (org-fast-tag-show-exit
  14459. (setq exit-after-next (not exit-after-next)))
  14460. (setq expert nil)
  14461. (delete-other-windows)
  14462. (split-window-vertically)
  14463. (org-switch-to-buffer-other-window " *Org tags*")
  14464. (and (fboundp 'fit-window-to-buffer)
  14465. (fit-window-to-buffer))))
  14466. ((or (= c ?\C-g)
  14467. (and (= c ?q) (not (rassoc c ntable))))
  14468. (org-detach-overlay org-tags-overlay)
  14469. (setq quit-flag t))
  14470. ((= c ?\ )
  14471. (setq current nil)
  14472. (if exit-after-next (setq exit-after-next 'now)))
  14473. ((= c ?\t)
  14474. (condition-case nil
  14475. (setq tg (completing-read
  14476. "Tag: "
  14477. (or buffer-tags
  14478. (with-current-buffer buf
  14479. (org-get-buffer-tags)))))
  14480. (quit (setq tg "")))
  14481. (when (string-match "\\S-" tg)
  14482. (add-to-list 'buffer-tags (list tg))
  14483. (if (member tg current)
  14484. (setq current (delete tg current))
  14485. (push tg current)))
  14486. (if exit-after-next (setq exit-after-next 'now)))
  14487. ((setq e (rassoc c todo-table) tg (car e))
  14488. (with-current-buffer buf
  14489. (save-excursion (org-todo tg)))
  14490. (if exit-after-next (setq exit-after-next 'now)))
  14491. ((setq e (rassoc c ntable) tg (car e))
  14492. (if (member tg current)
  14493. (setq current (delete tg current))
  14494. (loop for g in groups do
  14495. (if (member tg g)
  14496. (mapc (lambda (x)
  14497. (setq current (delete x current)))
  14498. g)))
  14499. (push tg current))
  14500. (if exit-after-next (setq exit-after-next 'now))))
  14501. ;; Create a sorted list
  14502. (setq current
  14503. (sort current
  14504. (lambda (a b)
  14505. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14506. (if (eq exit-after-next 'now) (throw 'exit t))
  14507. (goto-char (point-min))
  14508. (beginning-of-line 2)
  14509. (delete-region (point) (point-at-eol))
  14510. (org-fast-tag-insert "Current" current c-face)
  14511. (org-set-current-tags-overlay current ov-prefix)
  14512. (while (re-search-forward
  14513. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14514. (setq tg (match-string 1))
  14515. (add-text-properties
  14516. (match-beginning 1) (match-end 1)
  14517. (list 'face
  14518. (cond
  14519. ((member tg current) c-face)
  14520. ((member tg inherited) i-face)
  14521. (t (get-text-property (match-beginning 1) 'face))))))
  14522. (goto-char (point-min)))))
  14523. (org-detach-overlay org-tags-overlay)
  14524. (if rtn
  14525. (mapconcat 'identity current ":")
  14526. nil))))
  14527. (defun org-get-tags-string ()
  14528. "Get the TAGS string in the current headline."
  14529. (unless (org-on-heading-p t)
  14530. (error "Not on a heading"))
  14531. (save-excursion
  14532. (beginning-of-line 1)
  14533. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14534. (org-match-string-no-properties 1)
  14535. "")))
  14536. (defun org-get-tags ()
  14537. "Get the list of tags specified in the current headline."
  14538. (org-split-string (org-get-tags-string) ":"))
  14539. (defun org-get-buffer-tags ()
  14540. "Get a table of all tags used in the buffer, for completion."
  14541. (let (tags)
  14542. (save-excursion
  14543. (goto-char (point-min))
  14544. (while (re-search-forward
  14545. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14546. (when (equal (char-after (point-at-bol 0)) ?*)
  14547. (mapc (lambda (x) (add-to-list 'tags x))
  14548. (org-split-string (org-match-string-no-properties 1) ":")))))
  14549. (mapcar 'list tags)))
  14550. ;;;; Properties
  14551. ;;; Setting and retrieving properties
  14552. (defconst org-special-properties
  14553. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14554. "TIMESTAMP" "TIMESTAMP_IA")
  14555. "The special properties valid in Org-mode.
  14556. These are properties that are not defined in the property drawer,
  14557. but in some other way.")
  14558. (defconst org-default-properties
  14559. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14560. "LOCATION" "LOGGING" "COLUMNS")
  14561. "Some properties that are used by Org-mode for various purposes.
  14562. Being in this list makes sure that they are offered for completion.")
  14563. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14564. "Regular expression matching the first line of a property drawer.")
  14565. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14566. "Regular expression matching the first line of a property drawer.")
  14567. (defun org-property-action ()
  14568. "Do an action on properties."
  14569. (interactive)
  14570. (let (c)
  14571. (org-at-property-p)
  14572. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14573. (setq c (read-char-exclusive))
  14574. (cond
  14575. ((equal c ?s)
  14576. (call-interactively 'org-set-property))
  14577. ((equal c ?d)
  14578. (call-interactively 'org-delete-property))
  14579. ((equal c ?D)
  14580. (call-interactively 'org-delete-property-globally))
  14581. ((equal c ?c)
  14582. (call-interactively 'org-compute-property-at-point))
  14583. (t (error "No such property action %c" c)))))
  14584. (defun org-at-property-p ()
  14585. "Is the cursor in a property line?"
  14586. ;; FIXME: Does not check if we are actually in the drawer.
  14587. ;; FIXME: also returns true on any drawers.....
  14588. ;; This is used by C-c C-c for property action.
  14589. (save-excursion
  14590. (beginning-of-line 1)
  14591. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14592. (defmacro org-with-point-at (pom &rest body)
  14593. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14594. (declare (indent 1) (debug t))
  14595. `(save-excursion
  14596. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14597. (save-excursion
  14598. (goto-char (or pom (point)))
  14599. ,@body)))
  14600. (defun org-get-property-block (&optional beg end force)
  14601. "Return the (beg . end) range of the body of the property drawer.
  14602. BEG and END can be beginning and end of subtree, if not given
  14603. they will be found.
  14604. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14605. (catch 'exit
  14606. (save-excursion
  14607. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14608. (end (or end (progn (outline-next-heading) (point)))))
  14609. (goto-char beg)
  14610. (if (re-search-forward org-property-start-re end t)
  14611. (setq beg (1+ (match-end 0)))
  14612. (if force
  14613. (save-excursion
  14614. (org-insert-property-drawer)
  14615. (setq end (progn (outline-next-heading) (point))))
  14616. (throw 'exit nil))
  14617. (goto-char beg)
  14618. (if (re-search-forward org-property-start-re end t)
  14619. (setq beg (1+ (match-end 0)))))
  14620. (if (re-search-forward org-property-end-re end t)
  14621. (setq end (match-beginning 0))
  14622. (or force (throw 'exit nil))
  14623. (goto-char beg)
  14624. (setq end beg)
  14625. (org-indent-line-function)
  14626. (insert ":END:\n"))
  14627. (cons beg end)))))
  14628. (defun org-entry-properties (&optional pom which)
  14629. "Get all properties of the entry at point-or-marker POM.
  14630. This includes the TODO keyword, the tags, time strings for deadline,
  14631. scheduled, and clocking, and any additional properties defined in the
  14632. entry. The return value is an alist, keys may occur multiple times
  14633. if the property key was used several times.
  14634. POM may also be nil, in which case the current entry is used.
  14635. If WHICH is nil or `all', get all properties. If WHICH is
  14636. `special' or `standard', only get that subclass."
  14637. (setq which (or which 'all))
  14638. (org-with-point-at pom
  14639. (let ((clockstr (substring org-clock-string 0 -1))
  14640. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14641. beg end range props sum-props key value string clocksum)
  14642. (save-excursion
  14643. (when (condition-case nil (org-back-to-heading t) (error nil))
  14644. (setq beg (point))
  14645. (setq sum-props (get-text-property (point) 'org-summaries))
  14646. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14647. (outline-next-heading)
  14648. (setq end (point))
  14649. (when (memq which '(all special))
  14650. ;; Get the special properties, like TODO and tags
  14651. (goto-char beg)
  14652. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14653. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14654. (when (looking-at org-priority-regexp)
  14655. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14656. (when (and (setq value (org-get-tags-string))
  14657. (string-match "\\S-" value))
  14658. (push (cons "TAGS" value) props))
  14659. (when (setq value (org-get-tags-at))
  14660. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14661. props))
  14662. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14663. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14664. string (if (equal key clockstr)
  14665. (org-no-properties
  14666. (org-trim
  14667. (buffer-substring
  14668. (match-beginning 3) (goto-char (point-at-eol)))))
  14669. (substring (org-match-string-no-properties 3) 1 -1)))
  14670. (unless key
  14671. (if (= (char-after (match-beginning 3)) ?\[)
  14672. (setq key "TIMESTAMP_IA")
  14673. (setq key "TIMESTAMP")))
  14674. (when (or (equal key clockstr) (not (assoc key props)))
  14675. (push (cons key string) props)))
  14676. )
  14677. (when (memq which '(all standard))
  14678. ;; Get the standard properties, like :PORP: ...
  14679. (setq range (org-get-property-block beg end))
  14680. (when range
  14681. (goto-char (car range))
  14682. (while (re-search-forward
  14683. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14684. (cdr range) t)
  14685. (setq key (org-match-string-no-properties 1)
  14686. value (org-trim (or (org-match-string-no-properties 2) "")))
  14687. (unless (member key excluded)
  14688. (push (cons key (or value "")) props)))))
  14689. (if clocksum
  14690. (push (cons "CLOCKSUM"
  14691. (org-column-number-to-string (/ (float clocksum) 60.)
  14692. 'add_times))
  14693. props))
  14694. (append sum-props (nreverse props)))))))
  14695. (defun org-entry-get (pom property &optional inherit)
  14696. "Get value of PROPERTY for entry at point-or-marker POM.
  14697. If INHERIT is non-nil and the entry does not have the property,
  14698. then also check higher levels of the hierarchy.
  14699. If the property is present but empty, the return value is the empty string.
  14700. If the property is not present at all, nil is returned."
  14701. (org-with-point-at pom
  14702. (if inherit
  14703. (org-entry-get-with-inheritance property)
  14704. (if (member property org-special-properties)
  14705. ;; We need a special property. Use brute force, get all properties.
  14706. (cdr (assoc property (org-entry-properties nil 'special)))
  14707. (let ((range (org-get-property-block)))
  14708. (if (and range
  14709. (goto-char (car range))
  14710. (re-search-forward
  14711. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14712. (cdr range) t))
  14713. ;; Found the property, return it.
  14714. (if (match-end 1)
  14715. (org-match-string-no-properties 1)
  14716. "")))))))
  14717. (defun org-entry-delete (pom property)
  14718. "Delete the property PROPERTY from entry at point-or-marker POM."
  14719. (org-with-point-at pom
  14720. (if (member property org-special-properties)
  14721. nil ; cannot delete these properties.
  14722. (let ((range (org-get-property-block)))
  14723. (if (and range
  14724. (goto-char (car range))
  14725. (re-search-forward
  14726. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14727. (cdr range) t))
  14728. (progn
  14729. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14730. t)
  14731. nil)))))
  14732. ;; Multi-values properties are properties that contain multiple values
  14733. ;; These values are assumed to be single words, separated by whitespace.
  14734. (defun org-entry-add-to-multivalued-property (pom property value)
  14735. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14736. (let* ((old (org-entry-get pom property))
  14737. (values (and old (org-split-string old "[ \t]"))))
  14738. (unless (member value values)
  14739. (setq values (cons value values))
  14740. (org-entry-put pom property
  14741. (mapconcat 'identity values " ")))))
  14742. (defun org-entry-remove-from-multivalued-property (pom property value)
  14743. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14744. (let* ((old (org-entry-get pom property))
  14745. (values (and old (org-split-string old "[ \t]"))))
  14746. (when (member value values)
  14747. (setq values (delete value values))
  14748. (org-entry-put pom property
  14749. (mapconcat 'identity values " ")))))
  14750. (defun org-entry-member-in-multivalued-property (pom property value)
  14751. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14752. (let* ((old (org-entry-get pom property))
  14753. (values (and old (org-split-string old "[ \t]"))))
  14754. (member value values)))
  14755. (defvar org-entry-property-inherited-from (make-marker))
  14756. (defun org-entry-get-with-inheritance (property)
  14757. "Get entry property, and search higher levels if not present."
  14758. (let (tmp)
  14759. (save-excursion
  14760. (save-restriction
  14761. (widen)
  14762. (catch 'ex
  14763. (while t
  14764. (when (setq tmp (org-entry-get nil property))
  14765. (org-back-to-heading t)
  14766. (move-marker org-entry-property-inherited-from (point))
  14767. (throw 'ex tmp))
  14768. (or (org-up-heading-safe) (throw 'ex nil)))))
  14769. (or tmp (cdr (assoc property org-local-properties))
  14770. (cdr (assoc property org-global-properties))))))
  14771. (defun org-entry-put (pom property value)
  14772. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14773. (org-with-point-at pom
  14774. (org-back-to-heading t)
  14775. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14776. range)
  14777. (cond
  14778. ((equal property "TODO")
  14779. (when (and (stringp value) (string-match "\\S-" value)
  14780. (not (member value org-todo-keywords-1)))
  14781. (error "\"%s\" is not a valid TODO state" value))
  14782. (if (or (not value)
  14783. (not (string-match "\\S-" value)))
  14784. (setq value 'none))
  14785. (org-todo value)
  14786. (org-set-tags nil 'align))
  14787. ((equal property "PRIORITY")
  14788. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14789. (string-to-char value) ?\ ))
  14790. (org-set-tags nil 'align))
  14791. ((equal property "SCHEDULED")
  14792. (if (re-search-forward org-scheduled-time-regexp end t)
  14793. (cond
  14794. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14795. ((eq value 'later) (org-timestamp-change 1 'day))
  14796. (t (call-interactively 'org-schedule)))
  14797. (call-interactively 'org-schedule)))
  14798. ((equal property "DEADLINE")
  14799. (if (re-search-forward org-deadline-time-regexp end t)
  14800. (cond
  14801. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14802. ((eq value 'later) (org-timestamp-change 1 'day))
  14803. (t (call-interactively 'org-deadline)))
  14804. (call-interactively 'org-deadline)))
  14805. ((member property org-special-properties)
  14806. (error "The %s property can not yet be set with `org-entry-put'"
  14807. property))
  14808. (t ; a non-special property
  14809. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14810. (setq range (org-get-property-block beg end 'force))
  14811. (goto-char (car range))
  14812. (if (re-search-forward
  14813. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14814. (progn
  14815. (delete-region (match-beginning 1) (match-end 1))
  14816. (goto-char (match-beginning 1)))
  14817. (goto-char (cdr range))
  14818. (insert "\n")
  14819. (backward-char 1)
  14820. (org-indent-line-function)
  14821. (insert ":" property ":"))
  14822. (and value (insert " " value))
  14823. (org-indent-line-function)))))))
  14824. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14825. "Get all property keys in the current buffer.
  14826. With INCLUDE-SPECIALS, also list the special properties that relect things
  14827. like tags and TODO state.
  14828. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14829. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14830. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14831. formats in the current buffer."
  14832. (let (rtn range cfmt cols s p)
  14833. (save-excursion
  14834. (save-restriction
  14835. (widen)
  14836. (goto-char (point-min))
  14837. (while (re-search-forward org-property-start-re nil t)
  14838. (setq range (org-get-property-block))
  14839. (goto-char (car range))
  14840. (while (re-search-forward
  14841. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14842. (cdr range) t)
  14843. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14844. (outline-next-heading))))
  14845. (when include-specials
  14846. (setq rtn (append org-special-properties rtn)))
  14847. (when include-defaults
  14848. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14849. (when include-columns
  14850. (save-excursion
  14851. (save-restriction
  14852. (widen)
  14853. (goto-char (point-min))
  14854. (while (re-search-forward
  14855. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14856. nil t)
  14857. (setq cfmt (match-string 2) s 0)
  14858. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14859. cfmt s)
  14860. (setq s (match-end 0)
  14861. p (match-string 1 cfmt))
  14862. (unless (or (equal p "ITEM")
  14863. (member p org-special-properties))
  14864. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14865. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14866. (defun org-property-values (key)
  14867. "Return a list of all values of property KEY."
  14868. (save-excursion
  14869. (save-restriction
  14870. (widen)
  14871. (goto-char (point-min))
  14872. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14873. values)
  14874. (while (re-search-forward re nil t)
  14875. (add-to-list 'values (org-trim (match-string 1))))
  14876. (delete "" values)))))
  14877. (defun org-insert-property-drawer ()
  14878. "Insert a property drawer into the current entry."
  14879. (interactive)
  14880. (org-back-to-heading t)
  14881. (looking-at outline-regexp)
  14882. (let ((indent (- (match-end 0)(match-beginning 0)))
  14883. (beg (point))
  14884. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14885. end hiddenp)
  14886. (outline-next-heading)
  14887. (setq end (point))
  14888. (goto-char beg)
  14889. (while (re-search-forward re end t))
  14890. (setq hiddenp (org-invisible-p))
  14891. (end-of-line 1)
  14892. (and (equal (char-after) ?\n) (forward-char 1))
  14893. (org-skip-over-state-notes)
  14894. (skip-chars-backward " \t\n\r")
  14895. (if (eq (char-before) ?*) (forward-char 1))
  14896. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14897. (beginning-of-line 0)
  14898. (indent-to-column indent)
  14899. (beginning-of-line 2)
  14900. (indent-to-column indent)
  14901. (beginning-of-line 0)
  14902. (if hiddenp
  14903. (save-excursion
  14904. (org-back-to-heading t)
  14905. (hide-entry))
  14906. (org-flag-drawer t))))
  14907. (defun org-set-property (property value)
  14908. "In the current entry, set PROPERTY to VALUE.
  14909. When called interactively, this will prompt for a property name, offering
  14910. completion on existing and default properties. And then it will prompt
  14911. for a value, offering competion either on allowed values (via an inherited
  14912. xxx_ALL property) or on existing values in other instances of this property
  14913. in the current file."
  14914. (interactive
  14915. (let* ((prop (completing-read
  14916. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14917. (cur (org-entry-get nil prop))
  14918. (allowed (org-property-get-allowed-values nil prop 'table))
  14919. (existing (mapcar 'list (org-property-values prop)))
  14920. (val (if allowed
  14921. (completing-read "Value: " allowed nil 'req-match)
  14922. (completing-read
  14923. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14924. (concat "[" cur "]") "")
  14925. ": ")
  14926. existing nil nil "" nil cur))))
  14927. (list prop (if (equal val "") cur val))))
  14928. (unless (equal (org-entry-get nil property) value)
  14929. (org-entry-put nil property value)))
  14930. (defun org-delete-property (property)
  14931. "In the current entry, delete PROPERTY."
  14932. (interactive
  14933. (let* ((prop (completing-read
  14934. "Property: " (org-entry-properties nil 'standard))))
  14935. (list prop)))
  14936. (message "Property %s %s" property
  14937. (if (org-entry-delete nil property)
  14938. "deleted"
  14939. "was not present in the entry")))
  14940. (defun org-delete-property-globally (property)
  14941. "Remove PROPERTY globally, from all entries."
  14942. (interactive
  14943. (let* ((prop (completing-read
  14944. "Globally remove property: "
  14945. (mapcar 'list (org-buffer-property-keys)))))
  14946. (list prop)))
  14947. (save-excursion
  14948. (save-restriction
  14949. (widen)
  14950. (goto-char (point-min))
  14951. (let ((cnt 0))
  14952. (while (re-search-forward
  14953. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14954. nil t)
  14955. (setq cnt (1+ cnt))
  14956. (replace-match ""))
  14957. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14958. (defvar org-columns-current-fmt-compiled) ; defined below
  14959. (defun org-compute-property-at-point ()
  14960. "Compute the property at point.
  14961. This looks for an enclosing column format, extracts the operator and
  14962. then applies it to the proerty in the column format's scope."
  14963. (interactive)
  14964. (unless (org-at-property-p)
  14965. (error "Not at a property"))
  14966. (let ((prop (org-match-string-no-properties 2)))
  14967. (org-columns-get-format-and-top-level)
  14968. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14969. (error "No operator defined for property %s" prop))
  14970. (org-columns-compute prop)))
  14971. (defun org-property-get-allowed-values (pom property &optional table)
  14972. "Get allowed values for the property PROPERTY.
  14973. When TABLE is non-nil, return an alist that can directly be used for
  14974. completion."
  14975. (let (vals)
  14976. (cond
  14977. ((equal property "TODO")
  14978. (setq vals (org-with-point-at pom
  14979. (append org-todo-keywords-1 '("")))))
  14980. ((equal property "PRIORITY")
  14981. (let ((n org-lowest-priority))
  14982. (while (>= n org-highest-priority)
  14983. (push (char-to-string n) vals)
  14984. (setq n (1- n)))))
  14985. ((member property org-special-properties))
  14986. (t
  14987. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14988. (when (and vals (string-match "\\S-" vals))
  14989. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14990. (setq vals (mapcar (lambda (x)
  14991. (cond ((stringp x) x)
  14992. ((numberp x) (number-to-string x))
  14993. ((symbolp x) (symbol-name x))
  14994. (t "???")))
  14995. vals)))))
  14996. (if table (mapcar 'list vals) vals)))
  14997. (defun org-property-previous-allowed-value (&optional previous)
  14998. "Switch to the next allowed value for this property."
  14999. (interactive)
  15000. (org-property-next-allowed-value t))
  15001. (defun org-property-next-allowed-value (&optional previous)
  15002. "Switch to the next allowed value for this property."
  15003. (interactive)
  15004. (unless (org-at-property-p)
  15005. (error "Not at a property"))
  15006. (let* ((key (match-string 2))
  15007. (value (match-string 3))
  15008. (allowed (or (org-property-get-allowed-values (point) key)
  15009. (and (member value '("[ ]" "[-]" "[X]"))
  15010. '("[ ]" "[X]"))))
  15011. nval)
  15012. (unless allowed
  15013. (error "Allowed values for this property have not been defined"))
  15014. (if previous (setq allowed (reverse allowed)))
  15015. (if (member value allowed)
  15016. (setq nval (car (cdr (member value allowed)))))
  15017. (setq nval (or nval (car allowed)))
  15018. (if (equal nval value)
  15019. (error "Only one allowed value for this property"))
  15020. (org-at-property-p)
  15021. (replace-match (concat " :" key ": " nval) t t)
  15022. (org-indent-line-function)
  15023. (beginning-of-line 1)
  15024. (skip-chars-forward " \t")))
  15025. (defun org-find-entry-with-id (ident)
  15026. "Locate the entry that contains the ID property with exact value IDENT.
  15027. IDENT can be a string, a symbol or a number, this function will search for
  15028. the string representation of it.
  15029. Return the position where this entry starts, or nil if there is no such entry."
  15030. (let ((id (cond
  15031. ((stringp ident) ident)
  15032. ((symbol-name ident) (symbol-name ident))
  15033. ((numberp ident) (number-to-string ident))
  15034. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15035. (case-fold-search nil))
  15036. (save-excursion
  15037. (save-restriction
  15038. (goto-char (point-min))
  15039. (when (re-search-forward
  15040. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15041. nil t)
  15042. (org-back-to-heading)
  15043. (point))))))
  15044. ;;; Column View
  15045. (defvar org-columns-overlays nil
  15046. "Holds the list of current column overlays.")
  15047. (defvar org-columns-current-fmt nil
  15048. "Local variable, holds the currently active column format.")
  15049. (defvar org-columns-current-fmt-compiled nil
  15050. "Local variable, holds the currently active column format.
  15051. This is the compiled version of the format.")
  15052. (defvar org-columns-current-widths nil
  15053. "Loval variable, holds the currently widths of fields.")
  15054. (defvar org-columns-current-maxwidths nil
  15055. "Loval variable, holds the currently active maximum column widths.")
  15056. (defvar org-columns-begin-marker (make-marker)
  15057. "Points to the position where last a column creation command was called.")
  15058. (defvar org-columns-top-level-marker (make-marker)
  15059. "Points to the position where current columns region starts.")
  15060. (defvar org-columns-map (make-sparse-keymap)
  15061. "The keymap valid in column display.")
  15062. (defun org-columns-content ()
  15063. "Switch to contents view while in columns view."
  15064. (interactive)
  15065. (org-overview)
  15066. (org-content))
  15067. (org-defkey org-columns-map "c" 'org-columns-content)
  15068. (org-defkey org-columns-map "o" 'org-overview)
  15069. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15070. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15071. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15072. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15073. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15074. (org-defkey org-columns-map "q" 'org-columns-quit)
  15075. (org-defkey org-columns-map "r" 'org-columns-redo)
  15076. (org-defkey org-columns-map "g" 'org-columns-redo)
  15077. (org-defkey org-columns-map [left] 'backward-char)
  15078. (org-defkey org-columns-map "\M-b" 'backward-char)
  15079. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15080. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15081. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15082. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15083. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15084. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15085. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15086. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15087. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15088. (org-defkey org-columns-map ">" 'org-columns-widen)
  15089. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15090. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15091. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15092. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15093. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15094. '("Column"
  15095. ["Edit property" org-columns-edit-value t]
  15096. ["Next allowed value" org-columns-next-allowed-value t]
  15097. ["Previous allowed value" org-columns-previous-allowed-value t]
  15098. ["Show full value" org-columns-show-value t]
  15099. ["Edit allowed values" org-columns-edit-allowed t]
  15100. "--"
  15101. ["Edit column attributes" org-columns-edit-attributes t]
  15102. ["Increase column width" org-columns-widen t]
  15103. ["Decrease column width" org-columns-narrow t]
  15104. "--"
  15105. ["Move column right" org-columns-move-right t]
  15106. ["Move column left" org-columns-move-left t]
  15107. ["Add column" org-columns-new t]
  15108. ["Delete column" org-columns-delete t]
  15109. "--"
  15110. ["CONTENTS" org-columns-content t]
  15111. ["OVERVIEW" org-overview t]
  15112. ["Refresh columns display" org-columns-redo t]
  15113. "--"
  15114. ["Open link" org-columns-open-link t]
  15115. "--"
  15116. ["Quit" org-columns-quit t]))
  15117. (defun org-columns-new-overlay (beg end &optional string face)
  15118. "Create a new column overlay and add it to the list."
  15119. (let ((ov (org-make-overlay beg end)))
  15120. (org-overlay-put ov 'face (or face 'secondary-selection))
  15121. (org-overlay-display ov string face)
  15122. (push ov org-columns-overlays)
  15123. ov))
  15124. (defun org-columns-display-here (&optional props)
  15125. "Overlay the current line with column display."
  15126. (interactive)
  15127. (let* ((fmt org-columns-current-fmt-compiled)
  15128. (beg (point-at-bol))
  15129. (level-face (save-excursion
  15130. (beginning-of-line 1)
  15131. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15132. (org-get-level-face 2))))
  15133. (color (list :foreground
  15134. (face-attribute (or level-face 'default) :foreground)))
  15135. props pom property ass width f string ov column val modval)
  15136. ;; Check if the entry is in another buffer.
  15137. (unless props
  15138. (if (eq major-mode 'org-agenda-mode)
  15139. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15140. (get-text-property (point) 'org-marker))
  15141. props (if pom (org-entry-properties pom) nil))
  15142. (setq props (org-entry-properties nil))))
  15143. ;; Walk the format
  15144. (while (setq column (pop fmt))
  15145. (setq property (car column)
  15146. ass (if (equal property "ITEM")
  15147. (cons "ITEM"
  15148. (save-match-data
  15149. (org-no-properties
  15150. (org-remove-tabs
  15151. (buffer-substring-no-properties
  15152. (point-at-bol) (point-at-eol))))))
  15153. (assoc property props))
  15154. width (or (cdr (assoc property org-columns-current-maxwidths))
  15155. (nth 2 column)
  15156. (length property))
  15157. f (format "%%-%d.%ds | " width width)
  15158. val (or (cdr ass) "")
  15159. modval (if (equal property "ITEM")
  15160. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15161. string (format f (or modval val)))
  15162. ;; Create the overlay
  15163. (org-unmodified
  15164. (setq ov (org-columns-new-overlay
  15165. beg (setq beg (1+ beg)) string
  15166. (list color 'org-column)))
  15167. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15168. (org-overlay-put ov 'keymap org-columns-map)
  15169. (org-overlay-put ov 'org-columns-key property)
  15170. (org-overlay-put ov 'org-columns-value (cdr ass))
  15171. (org-overlay-put ov 'org-columns-value-modified modval)
  15172. (org-overlay-put ov 'org-columns-pom pom)
  15173. (org-overlay-put ov 'org-columns-format f))
  15174. (if (or (not (char-after beg))
  15175. (equal (char-after beg) ?\n))
  15176. (let ((inhibit-read-only t))
  15177. (save-excursion
  15178. (goto-char beg)
  15179. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15180. ;; Make the rest of the line disappear.
  15181. (org-unmodified
  15182. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15183. (org-overlay-put ov 'invisible t)
  15184. (org-overlay-put ov 'keymap org-columns-map)
  15185. (org-overlay-put ov 'intangible t)
  15186. (push ov org-columns-overlays)
  15187. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15188. (org-overlay-put ov 'keymap org-columns-map)
  15189. (push ov org-columns-overlays)
  15190. (let ((inhibit-read-only t))
  15191. (put-text-property (max (point-min) (1- (point-at-bol)))
  15192. (min (point-max) (1+ (point-at-eol)))
  15193. 'read-only "Type `e' to edit property")))))
  15194. (defvar org-previous-header-line-format nil
  15195. "The header line format before column view was turned on.")
  15196. (defvar org-columns-inhibit-recalculation nil
  15197. "Inhibit recomputing of columns on column view startup.")
  15198. (defvar header-line-format)
  15199. (defun org-columns-display-here-title ()
  15200. "Overlay the newline before the current line with the table title."
  15201. (interactive)
  15202. (let ((fmt org-columns-current-fmt-compiled)
  15203. string (title "")
  15204. property width f column str widths)
  15205. (while (setq column (pop fmt))
  15206. (setq property (car column)
  15207. str (or (nth 1 column) property)
  15208. width (or (cdr (assoc property org-columns-current-maxwidths))
  15209. (nth 2 column)
  15210. (length str))
  15211. widths (push width widths)
  15212. f (format "%%-%d.%ds | " width width)
  15213. string (format f str)
  15214. title (concat title string)))
  15215. (setq title (concat
  15216. (org-add-props " " nil 'display '(space :align-to 0))
  15217. (org-add-props title nil 'face '(:weight bold :underline t))))
  15218. (org-set-local 'org-previous-header-line-format header-line-format)
  15219. (org-set-local 'org-columns-current-widths (nreverse widths))
  15220. (setq header-line-format title)))
  15221. (defun org-columns-remove-overlays ()
  15222. "Remove all currently active column overlays."
  15223. (interactive)
  15224. (when (marker-buffer org-columns-begin-marker)
  15225. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15226. (when (local-variable-p 'org-previous-header-line-format)
  15227. (setq header-line-format org-previous-header-line-format)
  15228. (kill-local-variable 'org-previous-header-line-format))
  15229. (move-marker org-columns-begin-marker nil)
  15230. (move-marker org-columns-top-level-marker nil)
  15231. (org-unmodified
  15232. (mapc 'org-delete-overlay org-columns-overlays)
  15233. (setq org-columns-overlays nil)
  15234. (let ((inhibit-read-only t))
  15235. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15236. (defun org-columns-cleanup-item (item fmt)
  15237. "Remove from ITEM what is a column in the format FMT."
  15238. (if (not org-complex-heading-regexp)
  15239. item
  15240. (when (string-match org-complex-heading-regexp item)
  15241. (concat
  15242. (org-add-props (concat (match-string 1 item) " ") nil
  15243. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15244. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15245. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15246. " " (match-string 4 item)
  15247. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15248. (defun org-columns-show-value ()
  15249. "Show the full value of the property."
  15250. (interactive)
  15251. (let ((value (get-char-property (point) 'org-columns-value)))
  15252. (message "Value is: %s" (or value ""))))
  15253. (defun org-columns-quit ()
  15254. "Remove the column overlays and in this way exit column editing."
  15255. (interactive)
  15256. (org-unmodified
  15257. (org-columns-remove-overlays)
  15258. (let ((inhibit-read-only t))
  15259. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15260. (when (eq major-mode 'org-agenda-mode)
  15261. (message
  15262. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15263. (defun org-columns-check-computed ()
  15264. "Check if this column value is computed.
  15265. If yes, throw an error indicating that changing it does not make sense."
  15266. (let ((val (get-char-property (point) 'org-columns-value)))
  15267. (when (and (stringp val)
  15268. (get-char-property 0 'org-computed val))
  15269. (error "This value is computed from the entry's children"))))
  15270. (defun org-columns-todo (&optional arg)
  15271. "Change the TODO state during column view."
  15272. (interactive "P")
  15273. (org-columns-edit-value "TODO"))
  15274. (defun org-columns-set-tags-or-toggle (&optional arg)
  15275. "Toggle checkbox at point, or set tags for current headline."
  15276. (interactive "P")
  15277. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15278. (get-char-property (point) 'org-columns-value))
  15279. (org-columns-next-allowed-value)
  15280. (org-columns-edit-value "TAGS")))
  15281. (defun org-columns-edit-value (&optional key)
  15282. "Edit the value of the property at point in column view.
  15283. Where possible, use the standard interface for changing this line."
  15284. (interactive)
  15285. (org-columns-check-computed)
  15286. (let* ((external-key key)
  15287. (col (current-column))
  15288. (key (or key (get-char-property (point) 'org-columns-key)))
  15289. (value (get-char-property (point) 'org-columns-value))
  15290. (bol (point-at-bol)) (eol (point-at-eol))
  15291. (pom (or (get-text-property bol 'org-hd-marker)
  15292. (point))) ; keep despite of compiler waring
  15293. (line-overlays
  15294. (delq nil (mapcar (lambda (x)
  15295. (and (eq (overlay-buffer x) (current-buffer))
  15296. (>= (overlay-start x) bol)
  15297. (<= (overlay-start x) eol)
  15298. x))
  15299. org-columns-overlays)))
  15300. nval eval allowed)
  15301. (cond
  15302. ((equal key "CLOCKSUM")
  15303. (error "This special column cannot be edited"))
  15304. ((equal key "ITEM")
  15305. (setq eval '(org-with-point-at pom
  15306. (org-edit-headline))))
  15307. ((equal key "TODO")
  15308. (setq eval '(org-with-point-at pom
  15309. (let ((current-prefix-arg
  15310. (if external-key current-prefix-arg '(4))))
  15311. (call-interactively 'org-todo)))))
  15312. ((equal key "PRIORITY")
  15313. (setq eval '(org-with-point-at pom
  15314. (call-interactively 'org-priority))))
  15315. ((equal key "TAGS")
  15316. (setq eval '(org-with-point-at pom
  15317. (let ((org-fast-tag-selection-single-key
  15318. (if (eq org-fast-tag-selection-single-key 'expert)
  15319. t org-fast-tag-selection-single-key)))
  15320. (call-interactively 'org-set-tags)))))
  15321. ((equal key "DEADLINE")
  15322. (setq eval '(org-with-point-at pom
  15323. (call-interactively 'org-deadline))))
  15324. ((equal key "SCHEDULED")
  15325. (setq eval '(org-with-point-at pom
  15326. (call-interactively 'org-schedule))))
  15327. (t
  15328. (setq allowed (org-property-get-allowed-values pom key 'table))
  15329. (if allowed
  15330. (setq nval (completing-read "Value: " allowed nil t))
  15331. (setq nval (read-string "Edit: " value)))
  15332. (setq nval (org-trim nval))
  15333. (when (not (equal nval value))
  15334. (setq eval '(org-entry-put pom key nval)))))
  15335. (when eval
  15336. (let ((inhibit-read-only t))
  15337. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15338. (unwind-protect
  15339. (progn
  15340. (setq org-columns-overlays
  15341. (org-delete-all line-overlays org-columns-overlays))
  15342. (mapc 'org-delete-overlay line-overlays)
  15343. (org-columns-eval eval))
  15344. (org-columns-display-here))))
  15345. (move-to-column col)
  15346. (if (and (org-mode-p)
  15347. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15348. (org-columns-update key))))
  15349. (defun org-edit-headline () ; FIXME: this is not columns specific
  15350. "Edit the current headline, the part without TODO keyword, TAGS."
  15351. (org-back-to-heading)
  15352. (when (looking-at org-todo-line-regexp)
  15353. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15354. (txt (match-string 3))
  15355. (post "")
  15356. txt2)
  15357. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15358. (setq post (match-string 0 txt)
  15359. txt (substring txt 0 (match-beginning 0))))
  15360. (setq txt2 (read-string "Edit: " txt))
  15361. (when (not (equal txt txt2))
  15362. (beginning-of-line 1)
  15363. (insert pre txt2 post)
  15364. (delete-region (point) (point-at-eol))
  15365. (org-set-tags nil t)))))
  15366. (defun org-columns-edit-allowed ()
  15367. "Edit the list of allowed values for the current property."
  15368. (interactive)
  15369. (let* ((key (get-char-property (point) 'org-columns-key))
  15370. (key1 (concat key "_ALL"))
  15371. (allowed (org-entry-get (point) key1 t))
  15372. nval)
  15373. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15374. (setq nval (read-string "Allowed: " allowed))
  15375. (org-entry-put
  15376. (cond ((marker-position org-entry-property-inherited-from)
  15377. org-entry-property-inherited-from)
  15378. ((marker-position org-columns-top-level-marker)
  15379. org-columns-top-level-marker))
  15380. key1 nval)))
  15381. (defmacro org-no-warnings (&rest body)
  15382. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15383. (defun org-columns-eval (form)
  15384. (let (hidep)
  15385. (save-excursion
  15386. (beginning-of-line 1)
  15387. ;; `next-line' is needed here, because it skips invisible line.
  15388. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15389. (setq hidep (org-on-heading-p 1)))
  15390. (eval form)
  15391. (and hidep (hide-entry))))
  15392. (defun org-columns-previous-allowed-value ()
  15393. "Switch to the previous allowed value for this column."
  15394. (interactive)
  15395. (org-columns-next-allowed-value t))
  15396. (defun org-columns-next-allowed-value (&optional previous)
  15397. "Switch to the next allowed value for this column."
  15398. (interactive)
  15399. (org-columns-check-computed)
  15400. (let* ((col (current-column))
  15401. (key (get-char-property (point) 'org-columns-key))
  15402. (value (get-char-property (point) 'org-columns-value))
  15403. (bol (point-at-bol)) (eol (point-at-eol))
  15404. (pom (or (get-text-property bol 'org-hd-marker)
  15405. (point))) ; keep despite of compiler waring
  15406. (line-overlays
  15407. (delq nil (mapcar (lambda (x)
  15408. (and (eq (overlay-buffer x) (current-buffer))
  15409. (>= (overlay-start x) bol)
  15410. (<= (overlay-start x) eol)
  15411. x))
  15412. org-columns-overlays)))
  15413. (allowed (or (org-property-get-allowed-values pom key)
  15414. (and (memq
  15415. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15416. '(checkbox checkbox-n-of-m checkbox-percent))
  15417. '("[ ]" "[X]"))))
  15418. nval)
  15419. (when (equal key "ITEM")
  15420. (error "Cannot edit item headline from here"))
  15421. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15422. (error "Allowed values for this property have not been defined"))
  15423. (if (member key '("SCHEDULED" "DEADLINE"))
  15424. (setq nval (if previous 'earlier 'later))
  15425. (if previous (setq allowed (reverse allowed)))
  15426. (if (member value allowed)
  15427. (setq nval (car (cdr (member value allowed)))))
  15428. (setq nval (or nval (car allowed)))
  15429. (if (equal nval value)
  15430. (error "Only one allowed value for this property")))
  15431. (let ((inhibit-read-only t))
  15432. (remove-text-properties (1- bol) eol '(read-only t))
  15433. (unwind-protect
  15434. (progn
  15435. (setq org-columns-overlays
  15436. (org-delete-all line-overlays org-columns-overlays))
  15437. (mapc 'org-delete-overlay line-overlays)
  15438. (org-columns-eval '(org-entry-put pom key nval)))
  15439. (org-columns-display-here)))
  15440. (move-to-column col)
  15441. (if (and (org-mode-p)
  15442. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15443. (org-columns-update key))))
  15444. (defun org-verify-version (task)
  15445. (cond
  15446. ((eq task 'columns)
  15447. (if (or (featurep 'xemacs)
  15448. (< emacs-major-version 22))
  15449. (error "Emacs 22 is required for the columns feature")))))
  15450. (defun org-columns-open-link (&optional arg)
  15451. (interactive "P")
  15452. (let ((value (get-char-property (point) 'org-columns-value)))
  15453. (org-open-link-from-string value arg)))
  15454. (defun org-open-link-from-string (s &optional arg)
  15455. "Open a link in the string S, as if it was in Org-mode."
  15456. (interactive)
  15457. (with-temp-buffer
  15458. (let ((org-inhibit-startup t))
  15459. (org-mode)
  15460. (insert s)
  15461. (goto-char (point-min))
  15462. (org-open-at-point arg))))
  15463. (defun org-columns-get-format-and-top-level ()
  15464. (let (fmt)
  15465. (when (condition-case nil (org-back-to-heading) (error nil))
  15466. (move-marker org-entry-property-inherited-from nil)
  15467. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15468. (setq fmt (or fmt org-columns-default-format))
  15469. (org-set-local 'org-columns-current-fmt fmt)
  15470. (org-columns-compile-format fmt)
  15471. (if (marker-position org-entry-property-inherited-from)
  15472. (move-marker org-columns-top-level-marker
  15473. org-entry-property-inherited-from)
  15474. (move-marker org-columns-top-level-marker (point)))
  15475. fmt))
  15476. (defun org-columns ()
  15477. "Turn on column view on an org-mode file."
  15478. (interactive)
  15479. (org-verify-version 'columns)
  15480. (org-columns-remove-overlays)
  15481. (move-marker org-columns-begin-marker (point))
  15482. (let (beg end fmt cache maxwidths)
  15483. (setq fmt (org-columns-get-format-and-top-level))
  15484. (save-excursion
  15485. (goto-char org-columns-top-level-marker)
  15486. (setq beg (point))
  15487. (unless org-columns-inhibit-recalculation
  15488. (org-columns-compute-all))
  15489. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15490. (point-max)))
  15491. ;; Get and cache the properties
  15492. (goto-char beg)
  15493. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15494. (save-excursion
  15495. (save-restriction
  15496. (narrow-to-region beg end)
  15497. (org-clock-sum))))
  15498. (while (re-search-forward (concat "^" outline-regexp) end t)
  15499. (push (cons (org-current-line) (org-entry-properties)) cache))
  15500. (when cache
  15501. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15502. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15503. (org-columns-display-here-title)
  15504. (mapc (lambda (x)
  15505. (goto-line (car x))
  15506. (org-columns-display-here (cdr x)))
  15507. cache)))))
  15508. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15509. "Insert a new column, to the leeft o the current column."
  15510. (interactive)
  15511. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15512. cell)
  15513. (setq prop (completing-read
  15514. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15515. nil nil prop))
  15516. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15517. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15518. (if (string-match "\\S-" width)
  15519. (setq width (string-to-number width))
  15520. (setq width nil))
  15521. (setq fmt (completing-read "Summary [none]: "
  15522. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15523. nil t))
  15524. (if (string-match "\\S-" fmt)
  15525. (setq fmt (intern fmt))
  15526. (setq fmt nil))
  15527. (if (eq fmt 'none) (setq fmt nil))
  15528. (if editp
  15529. (progn
  15530. (setcar editp prop)
  15531. (setcdr editp (list title width nil fmt)))
  15532. (setq cell (nthcdr (1- (current-column))
  15533. org-columns-current-fmt-compiled))
  15534. (setcdr cell (cons (list prop title width nil fmt)
  15535. (cdr cell))))
  15536. (org-columns-store-format)
  15537. (org-columns-redo)))
  15538. (defun org-columns-delete ()
  15539. "Delete the column at point from columns view."
  15540. (interactive)
  15541. (let* ((n (current-column))
  15542. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15543. (when (y-or-n-p
  15544. (format "Are you sure you want to remove column \"%s\"? " title))
  15545. (setq org-columns-current-fmt-compiled
  15546. (delq (nth n org-columns-current-fmt-compiled)
  15547. org-columns-current-fmt-compiled))
  15548. (org-columns-store-format)
  15549. (org-columns-redo)
  15550. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15551. (backward-char 1)))))
  15552. (defun org-columns-edit-attributes ()
  15553. "Edit the attributes of the current column."
  15554. (interactive)
  15555. (let* ((n (current-column))
  15556. (info (nth n org-columns-current-fmt-compiled)))
  15557. (apply 'org-columns-new info)))
  15558. (defun org-columns-widen (arg)
  15559. "Make the column wider by ARG characters."
  15560. (interactive "p")
  15561. (let* ((n (current-column))
  15562. (entry (nth n org-columns-current-fmt-compiled))
  15563. (width (or (nth 2 entry)
  15564. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15565. (setq width (max 1 (+ width arg)))
  15566. (setcar (nthcdr 2 entry) width)
  15567. (org-columns-store-format)
  15568. (org-columns-redo)))
  15569. (defun org-columns-narrow (arg)
  15570. "Make the column nrrower by ARG characters."
  15571. (interactive "p")
  15572. (org-columns-widen (- arg)))
  15573. (defun org-columns-move-right ()
  15574. "Swap this column with the one to the right."
  15575. (interactive)
  15576. (let* ((n (current-column))
  15577. (cell (nthcdr n org-columns-current-fmt-compiled))
  15578. e)
  15579. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15580. (error "Cannot shift this column further to the right"))
  15581. (setq e (car cell))
  15582. (setcar cell (car (cdr cell)))
  15583. (setcdr cell (cons e (cdr (cdr cell))))
  15584. (org-columns-store-format)
  15585. (org-columns-redo)
  15586. (forward-char 1)))
  15587. (defun org-columns-move-left ()
  15588. "Swap this column with the one to the left."
  15589. (interactive)
  15590. (let* ((n (current-column)))
  15591. (when (= n 0)
  15592. (error "Cannot shift this column further to the left"))
  15593. (backward-char 1)
  15594. (org-columns-move-right)
  15595. (backward-char 1)))
  15596. (defun org-columns-store-format ()
  15597. "Store the text version of the current columns format in appropriate place.
  15598. This is either in the COLUMNS property of the node starting the current column
  15599. display, or in the #+COLUMNS line of the current buffer."
  15600. (let (fmt (cnt 0))
  15601. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15602. (org-set-local 'org-columns-current-fmt fmt)
  15603. (if (marker-position org-columns-top-level-marker)
  15604. (save-excursion
  15605. (goto-char org-columns-top-level-marker)
  15606. (if (and (org-at-heading-p)
  15607. (org-entry-get nil "COLUMNS"))
  15608. (org-entry-put nil "COLUMNS" fmt)
  15609. (goto-char (point-min))
  15610. ;; Overwrite all #+COLUMNS lines....
  15611. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15612. (setq cnt (1+ cnt))
  15613. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15614. (unless (> cnt 0)
  15615. (goto-char (point-min))
  15616. (or (org-on-heading-p t) (outline-next-heading))
  15617. (let ((inhibit-read-only t))
  15618. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15619. (org-set-local 'org-columns-default-format fmt))))))
  15620. (defvar org-overriding-columns-format nil
  15621. "When set, overrides any other definition.")
  15622. (defvar org-agenda-view-columns-initially nil
  15623. "When set, switch to columns view immediately after creating the agenda.")
  15624. (defun org-agenda-columns ()
  15625. "Turn on column view in the agenda."
  15626. (interactive)
  15627. (org-verify-version 'columns)
  15628. (org-columns-remove-overlays)
  15629. (move-marker org-columns-begin-marker (point))
  15630. (let (fmt cache maxwidths m)
  15631. (cond
  15632. ((and (local-variable-p 'org-overriding-columns-format)
  15633. org-overriding-columns-format)
  15634. (setq fmt org-overriding-columns-format))
  15635. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15636. (setq fmt (org-entry-get m "COLUMNS" t)))
  15637. ((and (boundp 'org-columns-current-fmt)
  15638. (local-variable-p 'org-columns-current-fmt)
  15639. org-columns-current-fmt)
  15640. (setq fmt org-columns-current-fmt))
  15641. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15642. (setq m (get-text-property m 'org-hd-marker))
  15643. (setq fmt (org-entry-get m "COLUMNS" t))))
  15644. (setq fmt (or fmt org-columns-default-format))
  15645. (org-set-local 'org-columns-current-fmt fmt)
  15646. (org-columns-compile-format fmt)
  15647. (save-excursion
  15648. ;; Get and cache the properties
  15649. (goto-char (point-min))
  15650. (while (not (eobp))
  15651. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15652. (get-text-property (point) 'org-marker)))
  15653. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15654. (beginning-of-line 2))
  15655. (when cache
  15656. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15657. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15658. (org-columns-display-here-title)
  15659. (mapc (lambda (x)
  15660. (goto-line (car x))
  15661. (org-columns-display-here (cdr x)))
  15662. cache)))))
  15663. (defun org-columns-get-autowidth-alist (s cache)
  15664. "Derive the maximum column widths from the format and the cache."
  15665. (let ((start 0) rtn)
  15666. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15667. (push (cons (match-string 1 s) 1) rtn)
  15668. (setq start (match-end 0)))
  15669. (mapc (lambda (x)
  15670. (setcdr x (apply 'max
  15671. (mapcar
  15672. (lambda (y)
  15673. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15674. cache))))
  15675. rtn)
  15676. rtn))
  15677. (defun org-columns-compute-all ()
  15678. "Compute all columns that have operators defined."
  15679. (org-unmodified
  15680. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15681. (let ((columns org-columns-current-fmt-compiled) col)
  15682. (while (setq col (pop columns))
  15683. (when (nth 3 col)
  15684. (save-excursion
  15685. (org-columns-compute (car col)))))))
  15686. (defun org-columns-update (property)
  15687. "Recompute PROPERTY, and update the columns display for it."
  15688. (org-columns-compute property)
  15689. (let (fmt val pos)
  15690. (save-excursion
  15691. (mapc (lambda (ov)
  15692. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15693. (setq pos (org-overlay-start ov))
  15694. (goto-char pos)
  15695. (when (setq val (cdr (assoc property
  15696. (get-text-property
  15697. (point-at-bol) 'org-summaries))))
  15698. (setq fmt (org-overlay-get ov 'org-columns-format))
  15699. (org-overlay-put ov 'org-columns-value val)
  15700. (org-overlay-put ov 'display (format fmt val)))))
  15701. org-columns-overlays))))
  15702. (defun org-columns-compute (property)
  15703. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15704. (interactive)
  15705. (let* ((re (concat "^" outline-regexp))
  15706. (lmax 30) ; Does anyone use deeper levels???
  15707. (lsum (make-vector lmax 0))
  15708. (lflag (make-vector lmax nil))
  15709. (level 0)
  15710. (ass (assoc property org-columns-current-fmt-compiled))
  15711. (format (nth 4 ass))
  15712. (printf (nth 5 ass))
  15713. (beg org-columns-top-level-marker)
  15714. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15715. (save-excursion
  15716. ;; Find the region to compute
  15717. (goto-char beg)
  15718. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15719. (goto-char end)
  15720. ;; Walk the tree from the back and do the computations
  15721. (while (re-search-backward re beg t)
  15722. (setq sumpos (match-beginning 0)
  15723. last-level level
  15724. level (org-outline-level)
  15725. val (org-entry-get nil property)
  15726. valflag (and val (string-match "\\S-" val)))
  15727. (cond
  15728. ((< level last-level)
  15729. ;; put the sum of lower levels here as a property
  15730. (setq sum (aref lsum last-level) ; current sum
  15731. flag (aref lflag last-level) ; any valid entries from children?
  15732. str (org-column-number-to-string sum format printf)
  15733. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15734. useval (if flag str1 (if valflag val ""))
  15735. sum-alist (get-text-property sumpos 'org-summaries))
  15736. (if (assoc property sum-alist)
  15737. (setcdr (assoc property sum-alist) useval)
  15738. (push (cons property useval) sum-alist)
  15739. (org-unmodified
  15740. (add-text-properties sumpos (1+ sumpos)
  15741. (list 'org-summaries sum-alist))))
  15742. (when val
  15743. (org-entry-put nil property (if flag str val)))
  15744. ;; add current to current level accumulator
  15745. (when (or flag valflag)
  15746. (aset lsum level (+ (aref lsum level)
  15747. (if flag sum (org-column-string-to-number
  15748. (if flag str val) format))))
  15749. (aset lflag level t))
  15750. ;; clear accumulators for deeper levels
  15751. (loop for l from (1+ level) to (1- lmax) do
  15752. (aset lsum l 0)
  15753. (aset lflag l nil)))
  15754. ((>= level last-level)
  15755. ;; add what we have here to the accumulator for this level
  15756. (aset lsum level (+ (aref lsum level)
  15757. (org-column-string-to-number (or val "0") format)))
  15758. (and valflag (aset lflag level t)))
  15759. (t (error "This should not happen")))))))
  15760. (defun org-columns-redo ()
  15761. "Construct the column display again."
  15762. (interactive)
  15763. (message "Recomputing columns...")
  15764. (save-excursion
  15765. (if (marker-position org-columns-begin-marker)
  15766. (goto-char org-columns-begin-marker))
  15767. (org-columns-remove-overlays)
  15768. (if (org-mode-p)
  15769. (call-interactively 'org-columns)
  15770. (call-interactively 'org-agenda-columns)))
  15771. (message "Recomputing columns...done"))
  15772. (defun org-columns-not-in-agenda ()
  15773. (if (eq major-mode 'org-agenda-mode)
  15774. (error "This command is only allowed in Org-mode buffers")))
  15775. (defun org-string-to-number (s)
  15776. "Convert string to number, and interpret hh:mm:ss."
  15777. (if (not (string-match ":" s))
  15778. (string-to-number s)
  15779. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15780. (while l
  15781. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15782. sum)))
  15783. (defun org-column-number-to-string (n fmt &optional printf)
  15784. "Convert a computed column number to a string value, according to FMT."
  15785. (cond
  15786. ((eq fmt 'add_times)
  15787. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15788. (format "%d:%02d" h m)))
  15789. ((eq fmt 'checkbox)
  15790. (cond ((= n (floor n)) "[X]")
  15791. ((> n 1.) "[-]")
  15792. (t "[ ]")))
  15793. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15794. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15795. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15796. (printf (format printf n))
  15797. ((eq fmt 'currency)
  15798. (format "%.2f" n))
  15799. (t (number-to-string n))))
  15800. (defun org-nofm-to-completion (n m &optional percent)
  15801. (if (not percent)
  15802. (format "[%d/%d]" n m)
  15803. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15804. (defun org-column-string-to-number (s fmt)
  15805. "Convert a column value to a number that can be used for column computing."
  15806. (cond
  15807. ((string-match ":" s)
  15808. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15809. (while l
  15810. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15811. sum))
  15812. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15813. (if (equal s "[X]") 1. 0.000001))
  15814. (t (string-to-number s))))
  15815. (defun org-columns-uncompile-format (cfmt)
  15816. "Turn the compiled columns format back into a string representation."
  15817. (let ((rtn "") e s prop title op width fmt printf)
  15818. (while (setq e (pop cfmt))
  15819. (setq prop (car e)
  15820. title (nth 1 e)
  15821. width (nth 2 e)
  15822. op (nth 3 e)
  15823. fmt (nth 4 e)
  15824. printf (nth 5 e))
  15825. (cond
  15826. ((eq fmt 'add_times) (setq op ":"))
  15827. ((eq fmt 'checkbox) (setq op "X"))
  15828. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15829. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15830. ((eq fmt 'add_numbers) (setq op "+"))
  15831. ((eq fmt 'currency) (setq op "$")))
  15832. (if (and op printf) (setq op (concat op ";" printf)))
  15833. (if (equal title prop) (setq title nil))
  15834. (setq s (concat "%" (if width (number-to-string width))
  15835. prop
  15836. (if title (concat "(" title ")"))
  15837. (if op (concat "{" op "}"))))
  15838. (setq rtn (concat rtn " " s)))
  15839. (org-trim rtn)))
  15840. (defun org-columns-compile-format (fmt)
  15841. "Turn a column format string into an alist of specifications.
  15842. The alist has one entry for each column in the format. The elements of
  15843. that list are:
  15844. property the property
  15845. title the title field for the columns
  15846. width the column width in characters, can be nil for automatic
  15847. operator the operator if any
  15848. format the output format for computed results, derived from operator
  15849. printf a printf format for computed values"
  15850. (let ((start 0) width prop title op f printf)
  15851. (setq org-columns-current-fmt-compiled nil)
  15852. (while (string-match
  15853. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15854. fmt start)
  15855. (setq start (match-end 0)
  15856. width (match-string 1 fmt)
  15857. prop (match-string 2 fmt)
  15858. title (or (match-string 3 fmt) prop)
  15859. op (match-string 4 fmt)
  15860. f nil
  15861. printf nil)
  15862. (if width (setq width (string-to-number width)))
  15863. (when (and op (string-match ";" op))
  15864. (setq printf (substring op (match-end 0))
  15865. op (substring op 0 (match-beginning 0))))
  15866. (cond
  15867. ((equal op "+") (setq f 'add_numbers))
  15868. ((equal op "$") (setq f 'currency))
  15869. ((equal op ":") (setq f 'add_times))
  15870. ((equal op "X") (setq f 'checkbox))
  15871. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15872. ((equal op "X%") (setq f 'checkbox-percent))
  15873. )
  15874. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15875. (setq org-columns-current-fmt-compiled
  15876. (nreverse org-columns-current-fmt-compiled))))
  15877. ;;; Dynamic block for Column view
  15878. (defun org-columns-capture-view ()
  15879. "Get the column view of the current buffer and return it as a list.
  15880. The list will contains the title row and all other rows. Each row is
  15881. a list of fields."
  15882. (save-excursion
  15883. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15884. (n (length title)) row tbl)
  15885. (goto-char (point-min))
  15886. (while (re-search-forward "^\\*+ " nil t)
  15887. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15888. (setq row nil)
  15889. (loop for i from 0 to (1- n) do
  15890. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15891. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15892. "")
  15893. row))
  15894. (setq row (nreverse row))
  15895. (push row tbl)))
  15896. (append (list title 'hline) (nreverse tbl)))))
  15897. (defun org-dblock-write:columnview (params)
  15898. "Write the column view table.
  15899. PARAMS is a property list of parameters:
  15900. :width enforce same column widths with <N> specifiers.
  15901. :id the :ID: property of the entry where the columns view
  15902. should be built, as a string. When `local', call locally.
  15903. When `global' call column view with the cursor at the beginning
  15904. of the buffer (usually this means that the whole buffer switches
  15905. to column view).
  15906. :hlines When t, insert a hline before each item. When a number, insert
  15907. a hline before each level <= that number.
  15908. :vlines When t, make each column a colgroup to enforce vertical lines."
  15909. (let ((pos (move-marker (make-marker) (point)))
  15910. (hlines (plist-get params :hlines))
  15911. (vlines (plist-get params :vlines))
  15912. tbl id idpos nfields tmp)
  15913. (save-excursion
  15914. (save-restriction
  15915. (when (setq id (plist-get params :id))
  15916. (cond ((not id) nil)
  15917. ((eq id 'global) (goto-char (point-min)))
  15918. ((eq id 'local) nil)
  15919. ((setq idpos (org-find-entry-with-id id))
  15920. (goto-char idpos))
  15921. (t (error "Cannot find entry with :ID: %s" id))))
  15922. (org-columns)
  15923. (setq tbl (org-columns-capture-view))
  15924. (setq nfields (length (car tbl)))
  15925. (org-columns-quit)))
  15926. (goto-char pos)
  15927. (move-marker pos nil)
  15928. (when tbl
  15929. (when (plist-get params :hlines)
  15930. (setq tmp nil)
  15931. (while tbl
  15932. (if (eq (car tbl) 'hline)
  15933. (push (pop tbl) tmp)
  15934. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15935. (if (and (not (eq (car tmp) 'hline))
  15936. (or (eq hlines t)
  15937. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15938. (push 'hline tmp)))
  15939. (push (pop tbl) tmp)))
  15940. (setq tbl (nreverse tmp)))
  15941. (when vlines
  15942. (setq tbl (mapcar (lambda (x)
  15943. (if (eq 'hline x) x (cons "" x)))
  15944. tbl))
  15945. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15946. (setq pos (point))
  15947. (insert (org-listtable-to-string tbl))
  15948. (when (plist-get params :width)
  15949. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15950. org-columns-current-widths "|")))
  15951. (goto-char pos)
  15952. (org-table-align))))
  15953. (defun org-listtable-to-string (tbl)
  15954. "Convert a listtable TBL to a string that contains the Org-mode table.
  15955. The table still need to be alligned. The resulting string has no leading
  15956. and tailing newline characters."
  15957. (mapconcat
  15958. (lambda (x)
  15959. (cond
  15960. ((listp x)
  15961. (concat "|" (mapconcat 'identity x "|") "|"))
  15962. ((eq x 'hline) "|-|")
  15963. (t (error "Garbage in listtable: %s" x))))
  15964. tbl "\n"))
  15965. (defun org-insert-columns-dblock ()
  15966. "Create a dynamic block capturing a column view table."
  15967. (interactive)
  15968. (let ((defaults '(:name "columnview" :hlines 1))
  15969. (id (completing-read
  15970. "Capture columns (local, global, entry with :ID: property) [local]: "
  15971. (append '(("global") ("local"))
  15972. (mapcar 'list (org-property-values "ID"))))))
  15973. (if (equal id "") (setq id 'local))
  15974. (if (equal id "global") (setq id 'global))
  15975. (setq defaults (append defaults (list :id id)))
  15976. (org-create-dblock defaults)
  15977. (org-update-dblock)))
  15978. ;;;; Timestamps
  15979. (defvar org-last-changed-timestamp nil)
  15980. (defvar org-time-was-given) ; dynamically scoped parameter
  15981. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15982. (defvar org-ts-what) ; dynamically scoped parameter
  15983. (defun org-time-stamp (arg)
  15984. "Prompt for a date/time and insert a time stamp.
  15985. If the user specifies a time like HH:MM, or if this command is called
  15986. with a prefix argument, the time stamp will contain date and time.
  15987. Otherwise, only the date will be included. All parts of a date not
  15988. specified by the user will be filled in from the current date/time.
  15989. So if you press just return without typing anything, the time stamp
  15990. will represent the current date/time. If there is already a timestamp
  15991. at the cursor, it will be modified."
  15992. (interactive "P")
  15993. (let* ((ts nil)
  15994. (default-time
  15995. ;; Default time is either today, or, when entering a range,
  15996. ;; the range start.
  15997. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15998. (save-excursion
  15999. (re-search-backward
  16000. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16001. (- (point) 20) t)))
  16002. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16003. (current-time)))
  16004. (default-input (and ts (org-get-compact-tod ts)))
  16005. org-time-was-given org-end-time-was-given time)
  16006. (cond
  16007. ((and (org-at-timestamp-p)
  16008. (eq last-command 'org-time-stamp)
  16009. (eq this-command 'org-time-stamp))
  16010. (insert "--")
  16011. (setq time (let ((this-command this-command))
  16012. (org-read-date arg 'totime nil nil default-time default-input)))
  16013. (org-insert-time-stamp time (or org-time-was-given arg)))
  16014. ((org-at-timestamp-p)
  16015. (setq time (let ((this-command this-command))
  16016. (org-read-date arg 'totime nil nil default-time default-input)))
  16017. (when (org-at-timestamp-p) ; just to get the match data
  16018. (replace-match "")
  16019. (setq org-last-changed-timestamp
  16020. (org-insert-time-stamp
  16021. time (or org-time-was-given arg)
  16022. nil nil nil (list org-end-time-was-given))))
  16023. (message "Timestamp updated"))
  16024. (t
  16025. (setq time (let ((this-command this-command))
  16026. (org-read-date arg 'totime nil nil default-time default-input)))
  16027. (org-insert-time-stamp time (or org-time-was-given arg)
  16028. nil nil nil (list org-end-time-was-given))))))
  16029. ;; FIXME: can we use this for something else????
  16030. ;; like computing time differences?????
  16031. (defun org-get-compact-tod (s)
  16032. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16033. (let* ((t1 (match-string 1 s))
  16034. (h1 (string-to-number (match-string 2 s)))
  16035. (m1 (string-to-number (match-string 3 s)))
  16036. (t2 (and (match-end 4) (match-string 5 s)))
  16037. (h2 (and t2 (string-to-number (match-string 6 s))))
  16038. (m2 (and t2 (string-to-number (match-string 7 s))))
  16039. dh dm)
  16040. (if (not t2)
  16041. t1
  16042. (setq dh (- h2 h1) dm (- m2 m1))
  16043. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16044. (concat t1 "+" (number-to-string dh)
  16045. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16046. (defun org-time-stamp-inactive (&optional arg)
  16047. "Insert an inactive time stamp.
  16048. An inactive time stamp is enclosed in square brackets instead of angle
  16049. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16050. does not link to the calendar and cannot be changed with the S-cursor keys.
  16051. So these are more for recording a certain time/date."
  16052. (interactive "P")
  16053. (let (org-time-was-given org-end-time-was-given time)
  16054. (setq time (org-read-date arg 'totime))
  16055. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16056. nil nil (list org-end-time-was-given))))
  16057. (defvar org-date-ovl (org-make-overlay 1 1))
  16058. (org-overlay-put org-date-ovl 'face 'org-warning)
  16059. (org-detach-overlay org-date-ovl)
  16060. (defvar org-ans1) ; dynamically scoped parameter
  16061. (defvar org-ans2) ; dynamically scoped parameter
  16062. (defvar org-plain-time-of-day-regexp) ; defined below
  16063. (defvar org-read-date-overlay nil)
  16064. (defvar org-dcst nil) ; dynamically scoped
  16065. (defun org-read-date (&optional with-time to-time from-string prompt
  16066. default-time default-input)
  16067. "Read a date, possibly a time, and make things smooth for the user.
  16068. The prompt will suggest to enter an ISO date, but you can also enter anything
  16069. which will at least partially be understood by `parse-time-string'.
  16070. Unrecognized parts of the date will default to the current day, month, year,
  16071. hour and minute. If this command is called to replace a timestamp at point,
  16072. of to enter the second timestamp of a range, the default time is taken from the
  16073. existing stamp. For example,
  16074. 3-2-5 --> 2003-02-05
  16075. feb 15 --> currentyear-02-15
  16076. sep 12 9 --> 2009-09-12
  16077. 12:45 --> today 12:45
  16078. 22 sept 0:34 --> currentyear-09-22 0:34
  16079. 12 --> currentyear-currentmonth-12
  16080. Fri --> nearest Friday (today or later)
  16081. etc.
  16082. Furthermore you can specify a relative date by giving, as the *first* thing
  16083. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16084. change in days weeks, months, years.
  16085. With a single plus or minus, the date is relative to today. With a double
  16086. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16087. +4d --> four days from today
  16088. +4 --> same as above
  16089. +2w --> two weeks from today
  16090. ++5 --> five days from default date
  16091. The function understands only English month and weekday abbreviations,
  16092. but this can be configured with the variables `parse-time-months' and
  16093. `parse-time-weekdays'.
  16094. While prompting, a calendar is popped up - you can also select the
  16095. date with the mouse (button 1). The calendar shows a period of three
  16096. months. To scroll it to other months, use the keys `>' and `<'.
  16097. If you don't like the calendar, turn it off with
  16098. \(setq org-read-date-popup-calendar nil)
  16099. With optional argument TO-TIME, the date will immediately be converted
  16100. to an internal time.
  16101. With an optional argument WITH-TIME, the prompt will suggest to also
  16102. insert a time. Note that when WITH-TIME is not set, you can still
  16103. enter a time, and this function will inform the calling routine about
  16104. this change. The calling routine may then choose to change the format
  16105. used to insert the time stamp into the buffer to include the time.
  16106. With optional argument FROM-STRING, read from this string instead from
  16107. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16108. the time/date that is used for everything that is not specified by the
  16109. user."
  16110. (require 'parse-time)
  16111. (let* ((org-time-stamp-rounding-minutes
  16112. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16113. (org-dcst org-display-custom-times)
  16114. (ct (org-current-time))
  16115. (def (or default-time ct))
  16116. (defdecode (decode-time def))
  16117. (dummy (progn
  16118. (when (< (nth 2 defdecode) org-extend-today-until)
  16119. (setcar (nthcdr 2 defdecode) -1)
  16120. (setcar (nthcdr 1 defdecode) 59)
  16121. (setq def (apply 'encode-time defdecode)
  16122. defdecode (decode-time def)))))
  16123. (calendar-move-hook nil)
  16124. (view-diary-entries-initially nil)
  16125. (view-calendar-holidays-initially nil)
  16126. (timestr (format-time-string
  16127. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16128. (prompt (concat (if prompt (concat prompt " ") "")
  16129. (format "Date+time [%s]: " timestr)))
  16130. ans (org-ans0 "") org-ans1 org-ans2 final)
  16131. (cond
  16132. (from-string (setq ans from-string))
  16133. (org-read-date-popup-calendar
  16134. (save-excursion
  16135. (save-window-excursion
  16136. (calendar)
  16137. (calendar-forward-day (- (time-to-days def)
  16138. (calendar-absolute-from-gregorian
  16139. (calendar-current-date))))
  16140. (org-eval-in-calendar nil t)
  16141. (let* ((old-map (current-local-map))
  16142. (map (copy-keymap calendar-mode-map))
  16143. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16144. (org-defkey map (kbd "RET") 'org-calendar-select)
  16145. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16146. 'org-calendar-select-mouse)
  16147. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16148. 'org-calendar-select-mouse)
  16149. (org-defkey minibuffer-local-map [(meta shift left)]
  16150. (lambda () (interactive)
  16151. (org-eval-in-calendar '(calendar-backward-month 1))))
  16152. (org-defkey minibuffer-local-map [(meta shift right)]
  16153. (lambda () (interactive)
  16154. (org-eval-in-calendar '(calendar-forward-month 1))))
  16155. (org-defkey minibuffer-local-map [(meta shift up)]
  16156. (lambda () (interactive)
  16157. (org-eval-in-calendar '(calendar-backward-year 1))))
  16158. (org-defkey minibuffer-local-map [(meta shift down)]
  16159. (lambda () (interactive)
  16160. (org-eval-in-calendar '(calendar-forward-year 1))))
  16161. (org-defkey minibuffer-local-map [(shift up)]
  16162. (lambda () (interactive)
  16163. (org-eval-in-calendar '(calendar-backward-week 1))))
  16164. (org-defkey minibuffer-local-map [(shift down)]
  16165. (lambda () (interactive)
  16166. (org-eval-in-calendar '(calendar-forward-week 1))))
  16167. (org-defkey minibuffer-local-map [(shift left)]
  16168. (lambda () (interactive)
  16169. (org-eval-in-calendar '(calendar-backward-day 1))))
  16170. (org-defkey minibuffer-local-map [(shift right)]
  16171. (lambda () (interactive)
  16172. (org-eval-in-calendar '(calendar-forward-day 1))))
  16173. (org-defkey minibuffer-local-map ">"
  16174. (lambda () (interactive)
  16175. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16176. (org-defkey minibuffer-local-map "<"
  16177. (lambda () (interactive)
  16178. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16179. (unwind-protect
  16180. (progn
  16181. (use-local-map map)
  16182. (add-hook 'post-command-hook 'org-read-date-display)
  16183. (setq org-ans0 (read-string prompt default-input nil nil))
  16184. ;; org-ans0: from prompt
  16185. ;; org-ans1: from mouse click
  16186. ;; org-ans2: from calendar motion
  16187. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16188. (remove-hook 'post-command-hook 'org-read-date-display)
  16189. (use-local-map old-map)
  16190. (when org-read-date-overlay
  16191. (org-delete-overlay org-read-date-overlay)
  16192. (setq org-read-date-overlay nil)))))))
  16193. (t ; Naked prompt only
  16194. (unwind-protect
  16195. (setq ans (read-string prompt default-input nil timestr))
  16196. (when org-read-date-overlay
  16197. (org-delete-overlay org-read-date-overlay)
  16198. (setq org-read-date-overlay nil)))))
  16199. (setq final (org-read-date-analyze ans def defdecode))
  16200. (if to-time
  16201. (apply 'encode-time final)
  16202. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16203. (format "%04d-%02d-%02d %02d:%02d"
  16204. (nth 5 final) (nth 4 final) (nth 3 final)
  16205. (nth 2 final) (nth 1 final))
  16206. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16207. (defvar def)
  16208. (defvar defdecode)
  16209. (defvar with-time)
  16210. (defun org-read-date-display ()
  16211. "Display the currrent date prompt interpretation in the minibuffer."
  16212. (when org-read-date-display-live
  16213. (when org-read-date-overlay
  16214. (org-delete-overlay org-read-date-overlay))
  16215. (let ((p (point)))
  16216. (end-of-line 1)
  16217. (while (not (equal (buffer-substring
  16218. (max (point-min) (- (point) 4)) (point))
  16219. " "))
  16220. (insert " "))
  16221. (goto-char p))
  16222. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16223. " " (or org-ans1 org-ans2)))
  16224. (org-end-time-was-given nil)
  16225. (f (org-read-date-analyze ans def defdecode))
  16226. (fmts (if org-dcst
  16227. org-time-stamp-custom-formats
  16228. org-time-stamp-formats))
  16229. (fmt (if (or with-time
  16230. (and (boundp 'org-time-was-given) org-time-was-given))
  16231. (cdr fmts)
  16232. (car fmts)))
  16233. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16234. (when (and org-end-time-was-given
  16235. (string-match org-plain-time-of-day-regexp txt))
  16236. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16237. org-end-time-was-given
  16238. (substring txt (match-end 0)))))
  16239. (setq org-read-date-overlay
  16240. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16241. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16242. (defun org-read-date-analyze (ans def defdecode)
  16243. "Analyze the combined answer of the date prompt."
  16244. ;; FIXME: cleanup and comment
  16245. (let (delta deltan deltaw deltadef year month day
  16246. hour minute second wday pm h2 m2 tl wday1)
  16247. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16248. (setq ans (replace-match "" t t ans)
  16249. deltan (car delta)
  16250. deltaw (nth 1 delta)
  16251. deltadef (nth 2 delta)))
  16252. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16253. (when (string-match
  16254. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16255. (setq year (if (match-end 2)
  16256. (string-to-number (match-string 2 ans))
  16257. (string-to-number (format-time-string "%Y")))
  16258. month (string-to-number (match-string 3 ans))
  16259. day (string-to-number (match-string 4 ans)))
  16260. (if (< year 100) (setq year (+ 2000 year)))
  16261. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16262. t nil ans)))
  16263. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16264. ;; If there is a time with am/pm, and *no* time without it, we convert
  16265. ;; so that matching will be successful.
  16266. (loop for i from 1 to 2 do ; twice, for end time as well
  16267. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16268. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16269. (setq hour (string-to-number (match-string 1 ans))
  16270. minute (if (match-end 3)
  16271. (string-to-number (match-string 3 ans))
  16272. 0)
  16273. pm (equal ?p
  16274. (string-to-char (downcase (match-string 4 ans)))))
  16275. (if (and (= hour 12) (not pm))
  16276. (setq hour 0)
  16277. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16278. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16279. t t ans))))
  16280. ;; Check if a time range is given as a duration
  16281. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16282. (setq hour (string-to-number (match-string 1 ans))
  16283. h2 (+ hour (string-to-number (match-string 3 ans)))
  16284. minute (string-to-number (match-string 2 ans))
  16285. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16286. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16287. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16288. ;; Check if there is a time range
  16289. (when (boundp 'org-end-time-was-given)
  16290. (setq org-time-was-given nil)
  16291. (when (and (string-match org-plain-time-of-day-regexp ans)
  16292. (match-end 8))
  16293. (setq org-end-time-was-given (match-string 8 ans))
  16294. (setq ans (concat (substring ans 0 (match-beginning 7))
  16295. (substring ans (match-end 7))))))
  16296. (setq tl (parse-time-string ans)
  16297. day (or (nth 3 tl) (nth 3 defdecode))
  16298. month (or (nth 4 tl)
  16299. (if (and org-read-date-prefer-future
  16300. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16301. (1+ (nth 4 defdecode))
  16302. (nth 4 defdecode)))
  16303. year (or (nth 5 tl)
  16304. (if (and org-read-date-prefer-future
  16305. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16306. (1+ (nth 5 defdecode))
  16307. (nth 5 defdecode)))
  16308. hour (or (nth 2 tl) (nth 2 defdecode))
  16309. minute (or (nth 1 tl) (nth 1 defdecode))
  16310. second (or (nth 0 tl) 0)
  16311. wday (nth 6 tl))
  16312. (when deltan
  16313. (unless deltadef
  16314. (let ((now (decode-time (current-time))))
  16315. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16316. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16317. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16318. ((equal deltaw "m") (setq month (+ month deltan)))
  16319. ((equal deltaw "y") (setq year (+ year deltan)))))
  16320. (when (and wday (not (nth 3 tl)))
  16321. ;; Weekday was given, but no day, so pick that day in the week
  16322. ;; on or after the derived date.
  16323. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16324. (unless (equal wday wday1)
  16325. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16326. (if (and (boundp 'org-time-was-given)
  16327. (nth 2 tl))
  16328. (setq org-time-was-given t))
  16329. (if (< year 100) (setq year (+ 2000 year)))
  16330. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16331. (list second minute hour day month year)))
  16332. (defvar parse-time-weekdays)
  16333. (defun org-read-date-get-relative (s today default)
  16334. "Check string S for special relative date string.
  16335. TODAY and DEFAULT are internal times, for today and for a default.
  16336. Return shift list (N what def-flag)
  16337. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16338. N is the number of WHATs to shift.
  16339. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16340. the DEFAULT date rather than TODAY."
  16341. (when (string-match
  16342. (concat
  16343. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16344. "\\([0-9]+\\)?"
  16345. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16346. "\\([ \t]\\|$\\)") s)
  16347. (let* ((dir (if (match-end 1)
  16348. (string-to-char (substring (match-string 1 s) -1))
  16349. ?+))
  16350. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16351. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16352. (what (if (match-end 3) (match-string 3 s) "d"))
  16353. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16354. (date (if rel default today))
  16355. (wday (nth 6 (decode-time date)))
  16356. delta)
  16357. (if wday1
  16358. (progn
  16359. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16360. (if (= dir ?-) (setq delta (- delta 7)))
  16361. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16362. (list delta "d" rel))
  16363. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16364. (defun org-eval-in-calendar (form &optional keepdate)
  16365. "Eval FORM in the calendar window and return to current window.
  16366. Also, store the cursor date in variable org-ans2."
  16367. (let ((sw (selected-window)))
  16368. (select-window (get-buffer-window "*Calendar*"))
  16369. (eval form)
  16370. (when (and (not keepdate) (calendar-cursor-to-date))
  16371. (let* ((date (calendar-cursor-to-date))
  16372. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16373. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16374. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16375. (select-window sw)))
  16376. ; ;; Update the prompt to show new default date
  16377. ; (save-excursion
  16378. ; (goto-char (point-min))
  16379. ; (when (and org-ans2
  16380. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16381. ; (get-text-property (match-end 0) 'field))
  16382. ; (let ((inhibit-read-only t))
  16383. ; (replace-match (concat "[" org-ans2 "]") t t)
  16384. ; (add-text-properties (point-min) (1+ (match-end 0))
  16385. ; (text-properties-at (1+ (point-min)))))))))
  16386. (defun org-calendar-select ()
  16387. "Return to `org-read-date' with the date currently selected.
  16388. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16389. (interactive)
  16390. (when (calendar-cursor-to-date)
  16391. (let* ((date (calendar-cursor-to-date))
  16392. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16393. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16394. (if (active-minibuffer-window) (exit-minibuffer))))
  16395. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16396. "Insert a date stamp for the date given by the internal TIME.
  16397. WITH-HM means, use the stamp format that includes the time of the day.
  16398. INACTIVE means use square brackets instead of angular ones, so that the
  16399. stamp will not contribute to the agenda.
  16400. PRE and POST are optional strings to be inserted before and after the
  16401. stamp.
  16402. The command returns the inserted time stamp."
  16403. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16404. stamp)
  16405. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16406. (insert-before-markers (or pre ""))
  16407. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16408. (when (listp extra)
  16409. (setq extra (car extra))
  16410. (if (and (stringp extra)
  16411. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16412. (setq extra (format "-%02d:%02d"
  16413. (string-to-number (match-string 1 extra))
  16414. (string-to-number (match-string 2 extra))))
  16415. (setq extra nil)))
  16416. (when extra
  16417. (backward-char 1)
  16418. (insert-before-markers extra)
  16419. (forward-char 1))
  16420. (insert-before-markers (or post ""))
  16421. stamp))
  16422. (defun org-toggle-time-stamp-overlays ()
  16423. "Toggle the use of custom time stamp formats."
  16424. (interactive)
  16425. (setq org-display-custom-times (not org-display-custom-times))
  16426. (unless org-display-custom-times
  16427. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16428. (while (setq p (next-single-property-change p 'display))
  16429. (if (and (get-text-property p 'display)
  16430. (eq (get-text-property p 'face) 'org-date))
  16431. (remove-text-properties
  16432. p (setq p (next-single-property-change p 'display))
  16433. '(display t))))
  16434. (set-buffer-modified-p bmp)))
  16435. (if (featurep 'xemacs)
  16436. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16437. (org-restart-font-lock)
  16438. (setq org-table-may-need-update t)
  16439. (if org-display-custom-times
  16440. (message "Time stamps are overlayed with custom format")
  16441. (message "Time stamp overlays removed")))
  16442. (defun org-display-custom-time (beg end)
  16443. "Overlay modified time stamp format over timestamp between BED and END."
  16444. (let* ((ts (buffer-substring beg end))
  16445. t1 w1 with-hm tf time str w2 (off 0))
  16446. (save-match-data
  16447. (setq t1 (org-parse-time-string ts t))
  16448. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16449. (setq off (- (match-end 0) (match-beginning 0)))))
  16450. (setq end (- end off))
  16451. (setq w1 (- end beg)
  16452. with-hm (and (nth 1 t1) (nth 2 t1))
  16453. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16454. time (org-fix-decoded-time t1)
  16455. str (org-add-props
  16456. (format-time-string
  16457. (substring tf 1 -1) (apply 'encode-time time))
  16458. nil 'mouse-face 'highlight)
  16459. w2 (length str))
  16460. (if (not (= w2 w1))
  16461. (add-text-properties (1+ beg) (+ 2 beg)
  16462. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16463. (if (featurep 'xemacs)
  16464. (progn
  16465. (put-text-property beg end 'invisible t)
  16466. (put-text-property beg end 'end-glyph (make-glyph str)))
  16467. (put-text-property beg end 'display str))))
  16468. (defun org-translate-time (string)
  16469. "Translate all timestamps in STRING to custom format.
  16470. But do this only if the variable `org-display-custom-times' is set."
  16471. (when org-display-custom-times
  16472. (save-match-data
  16473. (let* ((start 0)
  16474. (re org-ts-regexp-both)
  16475. t1 with-hm inactive tf time str beg end)
  16476. (while (setq start (string-match re string start))
  16477. (setq beg (match-beginning 0)
  16478. end (match-end 0)
  16479. t1 (save-match-data
  16480. (org-parse-time-string (substring string beg end) t))
  16481. with-hm (and (nth 1 t1) (nth 2 t1))
  16482. inactive (equal (substring string beg (1+ beg)) "[")
  16483. tf (funcall (if with-hm 'cdr 'car)
  16484. org-time-stamp-custom-formats)
  16485. time (org-fix-decoded-time t1)
  16486. str (format-time-string
  16487. (concat
  16488. (if inactive "[" "<") (substring tf 1 -1)
  16489. (if inactive "]" ">"))
  16490. (apply 'encode-time time))
  16491. string (replace-match str t t string)
  16492. start (+ start (length str)))))))
  16493. string)
  16494. (defun org-fix-decoded-time (time)
  16495. "Set 0 instead of nil for the first 6 elements of time.
  16496. Don't touch the rest."
  16497. (let ((n 0))
  16498. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16499. (defun org-days-to-time (timestamp-string)
  16500. "Difference between TIMESTAMP-STRING and now in days."
  16501. (- (time-to-days (org-time-string-to-time timestamp-string))
  16502. (time-to-days (current-time))))
  16503. (defun org-deadline-close (timestamp-string &optional ndays)
  16504. "Is the time in TIMESTAMP-STRING close to the current date?"
  16505. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16506. (and (< (org-days-to-time timestamp-string) ndays)
  16507. (not (org-entry-is-done-p))))
  16508. (defun org-get-wdays (ts)
  16509. "Get the deadline lead time appropriate for timestring TS."
  16510. (cond
  16511. ((<= org-deadline-warning-days 0)
  16512. ;; 0 or negative, enforce this value no matter what
  16513. (- org-deadline-warning-days))
  16514. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16515. ;; lead time is specified.
  16516. (floor (* (string-to-number (match-string 1 ts))
  16517. (cdr (assoc (match-string 2 ts)
  16518. '(("d" . 1) ("w" . 7)
  16519. ("m" . 30.4) ("y" . 365.25)))))))
  16520. ;; go for the default.
  16521. (t org-deadline-warning-days)))
  16522. (defun org-calendar-select-mouse (ev)
  16523. "Return to `org-read-date' with the date currently selected.
  16524. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16525. (interactive "e")
  16526. (mouse-set-point ev)
  16527. (when (calendar-cursor-to-date)
  16528. (let* ((date (calendar-cursor-to-date))
  16529. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16530. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16531. (if (active-minibuffer-window) (exit-minibuffer))))
  16532. (defun org-check-deadlines (ndays)
  16533. "Check if there are any deadlines due or past due.
  16534. A deadline is considered due if it happens within `org-deadline-warning-days'
  16535. days from today's date. If the deadline appears in an entry marked DONE,
  16536. it is not shown. The prefix arg NDAYS can be used to test that many
  16537. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16538. (interactive "P")
  16539. (let* ((org-warn-days
  16540. (cond
  16541. ((equal ndays '(4)) 100000)
  16542. (ndays (prefix-numeric-value ndays))
  16543. (t (abs org-deadline-warning-days))))
  16544. (case-fold-search nil)
  16545. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16546. (callback
  16547. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16548. (message "%d deadlines past-due or due within %d days"
  16549. (org-occur regexp nil callback)
  16550. org-warn-days)))
  16551. (defun org-check-before-date (date)
  16552. "Check if there are deadlines or scheduled entries before DATE."
  16553. (interactive (list (org-read-date)))
  16554. (let ((case-fold-search nil)
  16555. (regexp (concat "\\<\\(" org-deadline-string
  16556. "\\|" org-scheduled-string
  16557. "\\) *<\\([^>]+\\)>"))
  16558. (callback
  16559. (lambda () (time-less-p
  16560. (org-time-string-to-time (match-string 2))
  16561. (org-time-string-to-time date)))))
  16562. (message "%d entries before %s"
  16563. (org-occur regexp nil callback) date)))
  16564. (defun org-evaluate-time-range (&optional to-buffer)
  16565. "Evaluate a time range by computing the difference between start and end.
  16566. Normally the result is just printed in the echo area, but with prefix arg
  16567. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16568. If the time range is actually in a table, the result is inserted into the
  16569. next column.
  16570. For time difference computation, a year is assumed to be exactly 365
  16571. days in order to avoid rounding problems."
  16572. (interactive "P")
  16573. (or
  16574. (org-clock-update-time-maybe)
  16575. (save-excursion
  16576. (unless (org-at-date-range-p t)
  16577. (goto-char (point-at-bol))
  16578. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16579. (if (not (org-at-date-range-p t))
  16580. (error "Not at a time-stamp range, and none found in current line")))
  16581. (let* ((ts1 (match-string 1))
  16582. (ts2 (match-string 2))
  16583. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16584. (match-end (match-end 0))
  16585. (time1 (org-time-string-to-time ts1))
  16586. (time2 (org-time-string-to-time ts2))
  16587. (t1 (time-to-seconds time1))
  16588. (t2 (time-to-seconds time2))
  16589. (diff (abs (- t2 t1)))
  16590. (negative (< (- t2 t1) 0))
  16591. ;; (ys (floor (* 365 24 60 60)))
  16592. (ds (* 24 60 60))
  16593. (hs (* 60 60))
  16594. (fy "%dy %dd %02d:%02d")
  16595. (fy1 "%dy %dd")
  16596. (fd "%dd %02d:%02d")
  16597. (fd1 "%dd")
  16598. (fh "%02d:%02d")
  16599. y d h m align)
  16600. (if havetime
  16601. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16602. y 0
  16603. d (floor (/ diff ds)) diff (mod diff ds)
  16604. h (floor (/ diff hs)) diff (mod diff hs)
  16605. m (floor (/ diff 60)))
  16606. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16607. y 0
  16608. d (floor (+ (/ diff ds) 0.5))
  16609. h 0 m 0))
  16610. (if (not to-buffer)
  16611. (message "%s" (org-make-tdiff-string y d h m))
  16612. (if (org-at-table-p)
  16613. (progn
  16614. (goto-char match-end)
  16615. (setq align t)
  16616. (and (looking-at " *|") (goto-char (match-end 0))))
  16617. (goto-char match-end))
  16618. (if (looking-at
  16619. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16620. (replace-match ""))
  16621. (if negative (insert " -"))
  16622. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16623. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16624. (insert " " (format fh h m))))
  16625. (if align (org-table-align))
  16626. (message "Time difference inserted")))))
  16627. (defun org-make-tdiff-string (y d h m)
  16628. (let ((fmt "")
  16629. (l nil))
  16630. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16631. l (push y l)))
  16632. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16633. l (push d l)))
  16634. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16635. l (push h l)))
  16636. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16637. l (push m l)))
  16638. (apply 'format fmt (nreverse l))))
  16639. (defun org-time-string-to-time (s)
  16640. (apply 'encode-time (org-parse-time-string s)))
  16641. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16642. "Convert a time stamp to an absolute day number.
  16643. If there is a specifyer for a cyclic time stamp, get the closest date to
  16644. DAYNR."
  16645. (cond
  16646. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16647. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16648. daynr
  16649. (+ daynr 1000)))
  16650. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16651. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16652. (time-to-days (current-time))) (match-string 0 s)
  16653. prefer))
  16654. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16655. (defun org-time-from-absolute (d)
  16656. "Return the time corresponding to date D.
  16657. D may be an absolute day number, or a calendar-type list (month day year)."
  16658. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16659. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16660. (defun org-calendar-holiday ()
  16661. "List of holidays, for Diary display in Org-mode."
  16662. (require 'holidays)
  16663. (let ((hl (funcall
  16664. (if (fboundp 'calendar-check-holidays)
  16665. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16666. (if hl (mapconcat 'identity hl "; "))))
  16667. (defun org-diary-sexp-entry (sexp entry date)
  16668. "Process a SEXP diary ENTRY for DATE."
  16669. (require 'diary-lib)
  16670. (let ((result (if calendar-debug-sexp
  16671. (let ((stack-trace-on-error t))
  16672. (eval (car (read-from-string sexp))))
  16673. (condition-case nil
  16674. (eval (car (read-from-string sexp)))
  16675. (error
  16676. (beep)
  16677. (message "Bad sexp at line %d in %s: %s"
  16678. (org-current-line)
  16679. (buffer-file-name) sexp)
  16680. (sleep-for 2))))))
  16681. (cond ((stringp result) result)
  16682. ((and (consp result)
  16683. (stringp (cdr result))) (cdr result))
  16684. (result entry)
  16685. (t nil))))
  16686. (defun org-diary-to-ical-string (frombuf)
  16687. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16688. This uses the icalendar.el library."
  16689. (let* ((tmpdir (if (featurep 'xemacs)
  16690. (temp-directory)
  16691. temporary-file-directory))
  16692. (tmpfile (make-temp-name
  16693. (expand-file-name "orgics" tmpdir)))
  16694. buf rtn b e)
  16695. (save-excursion
  16696. (set-buffer frombuf)
  16697. (icalendar-export-region (point-min) (point-max) tmpfile)
  16698. (setq buf (find-buffer-visiting tmpfile))
  16699. (set-buffer buf)
  16700. (goto-char (point-min))
  16701. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16702. (setq b (match-beginning 0)))
  16703. (goto-char (point-max))
  16704. (if (re-search-backward "^END:VEVENT" nil t)
  16705. (setq e (match-end 0)))
  16706. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16707. (kill-buffer buf)
  16708. (kill-buffer frombuf)
  16709. (delete-file tmpfile)
  16710. rtn))
  16711. (defun org-closest-date (start current change prefer)
  16712. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16713. When PREFER is `past' return a date that is either CURRENT or past.
  16714. When PREFER is `future', return a date that is either CURRENT or future."
  16715. ;; Make the proper lists from the dates
  16716. (catch 'exit
  16717. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16718. dn dw sday cday n1 n2
  16719. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16720. (setq start (org-date-to-gregorian start)
  16721. current (org-date-to-gregorian
  16722. (if org-agenda-repeating-timestamp-show-all
  16723. current
  16724. (time-to-days (current-time))))
  16725. sday (calendar-absolute-from-gregorian start)
  16726. cday (calendar-absolute-from-gregorian current))
  16727. (if (<= cday sday) (throw 'exit sday))
  16728. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16729. (setq dn (string-to-number (match-string 1 change))
  16730. dw (cdr (assoc (match-string 2 change) a1)))
  16731. (error "Invalid change specifyer: %s" change))
  16732. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16733. (cond
  16734. ((eq dw 'day)
  16735. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16736. n2 (+ n1 dn)))
  16737. ((eq dw 'year)
  16738. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16739. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16740. (setq date1 (list m d y1)
  16741. n1 (calendar-absolute-from-gregorian date1)
  16742. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16743. n2 (calendar-absolute-from-gregorian date2)))
  16744. ((eq dw 'month)
  16745. ;; approx number of month between the tow dates
  16746. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16747. ;; How often does dn fit in there?
  16748. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16749. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16750. m (+ m nm)
  16751. ny (floor (/ m 12))
  16752. y (+ y ny)
  16753. m (- m (* ny 12)))
  16754. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16755. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16756. (setq m2 (+ m dn) y2 y)
  16757. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16758. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16759. (while (< n2 cday)
  16760. (setq n1 n2 m m2 y y2)
  16761. (setq m2 (+ m dn) y2 y)
  16762. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16763. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16764. (if org-agenda-repeating-timestamp-show-all
  16765. (cond
  16766. ((eq prefer 'past) n1)
  16767. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16768. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16769. (cond
  16770. ((eq prefer 'past) n1)
  16771. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16772. (t (if (= cday n1) n1 n2)))))))
  16773. (defun org-date-to-gregorian (date)
  16774. "Turn any specification of DATE into a gregorian date for the calendar."
  16775. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16776. ((and (listp date) (= (length date) 3)) date)
  16777. ((stringp date)
  16778. (setq date (org-parse-time-string date))
  16779. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16780. ((listp date)
  16781. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16782. (defun org-parse-time-string (s &optional nodefault)
  16783. "Parse the standard Org-mode time string.
  16784. This should be a lot faster than the normal `parse-time-string'.
  16785. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16786. hour and minute fields will be nil if not given."
  16787. (if (string-match org-ts-regexp0 s)
  16788. (list 0
  16789. (if (or (match-beginning 8) (not nodefault))
  16790. (string-to-number (or (match-string 8 s) "0")))
  16791. (if (or (match-beginning 7) (not nodefault))
  16792. (string-to-number (or (match-string 7 s) "0")))
  16793. (string-to-number (match-string 4 s))
  16794. (string-to-number (match-string 3 s))
  16795. (string-to-number (match-string 2 s))
  16796. nil nil nil)
  16797. (make-list 9 0)))
  16798. (defun org-timestamp-up (&optional arg)
  16799. "Increase the date item at the cursor by one.
  16800. If the cursor is on the year, change the year. If it is on the month or
  16801. the day, change that.
  16802. With prefix ARG, change by that many units."
  16803. (interactive "p")
  16804. (org-timestamp-change (prefix-numeric-value arg)))
  16805. (defun org-timestamp-down (&optional arg)
  16806. "Decrease the date item at the cursor by one.
  16807. If the cursor is on the year, change the year. If it is on the month or
  16808. the day, change that.
  16809. With prefix ARG, change by that many units."
  16810. (interactive "p")
  16811. (org-timestamp-change (- (prefix-numeric-value arg))))
  16812. (defun org-timestamp-up-day (&optional arg)
  16813. "Increase the date in the time stamp by one day.
  16814. With prefix ARG, change that many days."
  16815. (interactive "p")
  16816. (if (and (not (org-at-timestamp-p t))
  16817. (org-on-heading-p))
  16818. (org-todo 'up)
  16819. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16820. (defun org-timestamp-down-day (&optional arg)
  16821. "Decrease the date in the time stamp by one day.
  16822. With prefix ARG, change that many days."
  16823. (interactive "p")
  16824. (if (and (not (org-at-timestamp-p t))
  16825. (org-on-heading-p))
  16826. (org-todo 'down)
  16827. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16828. (defsubst org-pos-in-match-range (pos n)
  16829. (and (match-beginning n)
  16830. (<= (match-beginning n) pos)
  16831. (>= (match-end n) pos)))
  16832. (defun org-at-timestamp-p (&optional inactive-ok)
  16833. "Determine if the cursor is in or at a timestamp."
  16834. (interactive)
  16835. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16836. (pos (point))
  16837. (ans (or (looking-at tsr)
  16838. (save-excursion
  16839. (skip-chars-backward "^[<\n\r\t")
  16840. (if (> (point) (point-min)) (backward-char 1))
  16841. (and (looking-at tsr)
  16842. (> (- (match-end 0) pos) -1))))))
  16843. (and ans
  16844. (boundp 'org-ts-what)
  16845. (setq org-ts-what
  16846. (cond
  16847. ((= pos (match-beginning 0)) 'bracket)
  16848. ((= pos (1- (match-end 0))) 'bracket)
  16849. ((org-pos-in-match-range pos 2) 'year)
  16850. ((org-pos-in-match-range pos 3) 'month)
  16851. ((org-pos-in-match-range pos 7) 'hour)
  16852. ((org-pos-in-match-range pos 8) 'minute)
  16853. ((or (org-pos-in-match-range pos 4)
  16854. (org-pos-in-match-range pos 5)) 'day)
  16855. ((and (> pos (or (match-end 8) (match-end 5)))
  16856. (< pos (match-end 0)))
  16857. (- pos (or (match-end 8) (match-end 5))))
  16858. (t 'day))))
  16859. ans))
  16860. (defun org-toggle-timestamp-type ()
  16861. ""
  16862. (interactive)
  16863. (when (org-at-timestamp-p t)
  16864. (save-excursion
  16865. (goto-char (match-beginning 0))
  16866. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16867. (goto-char (1- (match-end 0)))
  16868. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16869. (message "Timestamp is now %sactive"
  16870. (if (equal (char-before) ?>) "in" ""))))
  16871. (defun org-timestamp-change (n &optional what)
  16872. "Change the date in the time stamp at point.
  16873. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16874. `year', `minute', `second'. If WHAT is not given, the cursor position
  16875. in the timestamp determines what will be changed."
  16876. (let ((pos (point))
  16877. with-hm inactive
  16878. org-ts-what
  16879. extra
  16880. ts time time0)
  16881. (if (not (org-at-timestamp-p t))
  16882. (error "Not at a timestamp"))
  16883. (if (and (not what) (eq org-ts-what 'bracket))
  16884. (org-toggle-timestamp-type)
  16885. (if (and (not what) (not (eq org-ts-what 'day))
  16886. org-display-custom-times
  16887. (get-text-property (point) 'display)
  16888. (not (get-text-property (1- (point)) 'display)))
  16889. (setq org-ts-what 'day))
  16890. (setq org-ts-what (or what org-ts-what)
  16891. inactive (= (char-after (match-beginning 0)) ?\[)
  16892. ts (match-string 0))
  16893. (replace-match "")
  16894. (if (string-match
  16895. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16896. ts)
  16897. (setq extra (match-string 1 ts)))
  16898. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16899. (setq with-hm t))
  16900. (setq time0 (org-parse-time-string ts))
  16901. (setq time
  16902. (encode-time (or (car time0) 0)
  16903. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16904. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16905. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16906. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16907. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16908. (nthcdr 6 time0)))
  16909. (when (integerp org-ts-what)
  16910. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16911. (if (eq what 'calendar)
  16912. (let ((cal-date (org-get-date-from-calendar)))
  16913. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16914. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16915. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16916. (setcar time0 (or (car time0) 0))
  16917. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16918. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16919. (setq time (apply 'encode-time time0))))
  16920. (setq org-last-changed-timestamp
  16921. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16922. (org-clock-update-time-maybe)
  16923. (goto-char pos)
  16924. ;; Try to recenter the calendar window, if any
  16925. (if (and org-calendar-follow-timestamp-change
  16926. (get-buffer-window "*Calendar*" t)
  16927. (memq org-ts-what '(day month year)))
  16928. (org-recenter-calendar (time-to-days time))))))
  16929. ;; FIXME: does not yet work for lead times
  16930. (defun org-modify-ts-extra (s pos n)
  16931. "Change the different parts of the lead-time and repeat fields in timestamp."
  16932. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16933. ng h m new)
  16934. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16935. (cond
  16936. ((or (org-pos-in-match-range pos 2)
  16937. (org-pos-in-match-range pos 3))
  16938. (setq m (string-to-number (match-string 3 s))
  16939. h (string-to-number (match-string 2 s)))
  16940. (if (org-pos-in-match-range pos 2)
  16941. (setq h (+ h n))
  16942. (setq m (+ m n)))
  16943. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16944. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16945. (setq h (min 24 (max 0 h)))
  16946. (setq ng 1 new (format "-%02d:%02d" h m)))
  16947. ((org-pos-in-match-range pos 6)
  16948. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16949. ((org-pos-in-match-range pos 5)
  16950. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16951. (when ng
  16952. (setq s (concat
  16953. (substring s 0 (match-beginning ng))
  16954. new
  16955. (substring s (match-end ng))))))
  16956. s))
  16957. (defun org-recenter-calendar (date)
  16958. "If the calendar is visible, recenter it to DATE."
  16959. (let* ((win (selected-window))
  16960. (cwin (get-buffer-window "*Calendar*" t))
  16961. (calendar-move-hook nil))
  16962. (when cwin
  16963. (select-window cwin)
  16964. (calendar-goto-date (if (listp date) date
  16965. (calendar-gregorian-from-absolute date)))
  16966. (select-window win))))
  16967. (defun org-goto-calendar (&optional arg)
  16968. "Go to the Emacs calendar at the current date.
  16969. If there is a time stamp in the current line, go to that date.
  16970. A prefix ARG can be used to force the current date."
  16971. (interactive "P")
  16972. (let ((tsr org-ts-regexp) diff
  16973. (calendar-move-hook nil)
  16974. (view-calendar-holidays-initially nil)
  16975. (view-diary-entries-initially nil))
  16976. (if (or (org-at-timestamp-p)
  16977. (save-excursion
  16978. (beginning-of-line 1)
  16979. (looking-at (concat ".*" tsr))))
  16980. (let ((d1 (time-to-days (current-time)))
  16981. (d2 (time-to-days
  16982. (org-time-string-to-time (match-string 1)))))
  16983. (setq diff (- d2 d1))))
  16984. (calendar)
  16985. (calendar-goto-today)
  16986. (if (and diff (not arg)) (calendar-forward-day diff))))
  16987. (defun org-get-date-from-calendar ()
  16988. "Return a list (month day year) of date at point in calendar."
  16989. (with-current-buffer "*Calendar*"
  16990. (save-match-data
  16991. (calendar-cursor-to-date))))
  16992. (defun org-date-from-calendar ()
  16993. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16994. If there is already a time stamp at the cursor position, update it."
  16995. (interactive)
  16996. (if (org-at-timestamp-p t)
  16997. (org-timestamp-change 0 'calendar)
  16998. (let ((cal-date (org-get-date-from-calendar)))
  16999. (org-insert-time-stamp
  17000. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17001. ;; Make appt aware of appointments from the agenda
  17002. ;;;###autoload
  17003. (defun org-agenda-to-appt (&optional filter)
  17004. "Activate appointments found in `org-agenda-files'.
  17005. When prefixed, prompt for a regular expression and use it as a
  17006. filter: only add entries if they match this regular expression.
  17007. FILTER can be a string. In this case, use this string as a
  17008. regular expression to filter results.
  17009. FILTER can also be an alist, with the car of each cell being
  17010. either 'headline or 'category. For example:
  17011. '((headline \"IMPORTANT\")
  17012. (category \"Work\"))
  17013. will only add headlines containing IMPORTANT or headlines
  17014. belonging to the category \"Work\"."
  17015. (interactive "P")
  17016. (require 'calendar)
  17017. (if (equal filter '(4))
  17018. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17019. (let* ((cnt 0) ; count added events
  17020. (org-agenda-new-buffers nil)
  17021. (today (org-date-to-gregorian
  17022. (time-to-days (current-time))))
  17023. (files (org-agenda-files)) entries file)
  17024. ;; Get all entries which may contain an appt
  17025. (while (setq file (pop files))
  17026. (setq entries
  17027. (append entries
  17028. (org-agenda-get-day-entries
  17029. file today
  17030. :timestamp :scheduled :deadline))))
  17031. (setq entries (delq nil entries))
  17032. ;; Map thru entries and find if they pass thru the filter
  17033. (mapc
  17034. (lambda(x)
  17035. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17036. (cat (get-text-property 1 'org-category x))
  17037. (tod (get-text-property 1 'time-of-day x))
  17038. (ok (or (null filter)
  17039. (and (stringp filter) (string-match filter evt))
  17040. (and (listp filter)
  17041. (or (string-match
  17042. (cadr (assoc 'category filter)) cat)
  17043. (string-match
  17044. (cadr (assoc 'headline filter)) evt))))))
  17045. ;; FIXME: Shall we remove text-properties for the appt text?
  17046. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17047. (when (and ok tod)
  17048. (setq tod (number-to-string tod)
  17049. tod (when (string-match
  17050. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17051. (concat (match-string 1 tod) ":"
  17052. (match-string 2 tod))))
  17053. (appt-add tod evt)
  17054. (setq cnt (1+ cnt))))) entries)
  17055. (org-release-buffers org-agenda-new-buffers)
  17056. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  17057. ;;; The clock for measuring work time.
  17058. (defvar org-mode-line-string "")
  17059. (put 'org-mode-line-string 'risky-local-variable t)
  17060. (defvar org-mode-line-timer nil)
  17061. (defvar org-clock-heading "")
  17062. (defvar org-clock-start-time "")
  17063. (defun org-update-mode-line ()
  17064. (let* ((delta (- (time-to-seconds (current-time))
  17065. (time-to-seconds org-clock-start-time)))
  17066. (h (floor delta 3600))
  17067. (m (floor (- delta (* 3600 h)) 60)))
  17068. (setq org-mode-line-string
  17069. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17070. 'help-echo "Org-mode clock is running"))
  17071. (force-mode-line-update)))
  17072. (defvar org-clock-marker (make-marker)
  17073. "Marker recording the last clock-in.")
  17074. (defvar org-clock-mode-line-entry nil
  17075. "Information for the modeline about the running clock.")
  17076. (defun org-clock-in ()
  17077. "Start the clock on the current item.
  17078. If necessary, clock-out of the currently active clock."
  17079. (interactive)
  17080. (org-clock-out t)
  17081. (let (ts)
  17082. (save-excursion
  17083. (org-back-to-heading t)
  17084. (when (and org-clock-in-switch-to-state
  17085. (not (looking-at (concat outline-regexp "[ \t]*"
  17086. org-clock-in-switch-to-state
  17087. "\\>"))))
  17088. (org-todo org-clock-in-switch-to-state))
  17089. (if (and org-clock-heading-function
  17090. (functionp org-clock-heading-function))
  17091. (setq org-clock-heading (funcall org-clock-heading-function))
  17092. (if (looking-at org-complex-heading-regexp)
  17093. (setq org-clock-heading (match-string 4))
  17094. (setq org-clock-heading "???")))
  17095. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17096. (org-clock-find-position)
  17097. (insert "\n") (backward-char 1)
  17098. (indent-relative)
  17099. (insert org-clock-string " ")
  17100. (setq org-clock-start-time (current-time))
  17101. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17102. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17103. (or global-mode-string (setq global-mode-string '("")))
  17104. (or (memq 'org-mode-line-string global-mode-string)
  17105. (setq global-mode-string
  17106. (append global-mode-string '(org-mode-line-string))))
  17107. (org-update-mode-line)
  17108. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17109. (message "Clock started at %s" ts))))
  17110. (defun org-clock-find-position ()
  17111. "Find the location where the next clock line should be inserted."
  17112. (org-back-to-heading t)
  17113. (catch 'exit
  17114. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17115. (re (concat "^[ \t]*" org-clock-string))
  17116. (cnt 0)
  17117. first last)
  17118. (goto-char beg)
  17119. (when (eobp) (newline) (setq end (max (point) end)))
  17120. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17121. ;; we seem to have a CLOCK drawer, so go there.
  17122. (beginning-of-line 2)
  17123. (throw 'exit t))
  17124. ;; Lets count the CLOCK lines
  17125. (goto-char beg)
  17126. (while (re-search-forward re end t)
  17127. (setq first (or first (match-beginning 0))
  17128. last (match-beginning 0)
  17129. cnt (1+ cnt)))
  17130. (when (and (integerp org-clock-into-drawer)
  17131. (>= (1+ cnt) org-clock-into-drawer))
  17132. ;; Wrap current entries into a new drawer
  17133. (goto-char last)
  17134. (beginning-of-line 2)
  17135. (if (org-at-item-p) (org-end-of-item))
  17136. (insert ":END:\n")
  17137. (beginning-of-line 0)
  17138. (org-indent-line-function)
  17139. (goto-char first)
  17140. (insert ":CLOCK:\n")
  17141. (beginning-of-line 0)
  17142. (org-indent-line-function)
  17143. (org-flag-drawer t)
  17144. (beginning-of-line 2)
  17145. (throw 'exit nil))
  17146. (goto-char beg)
  17147. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17148. (not (equal (match-string 1) org-clock-string)))
  17149. ;; Planning info, skip to after it
  17150. (beginning-of-line 2)
  17151. (or (bolp) (newline)))
  17152. (when (eq t org-clock-into-drawer)
  17153. (insert ":CLOCK:\n:END:\n")
  17154. (beginning-of-line -1)
  17155. (org-indent-line-function)
  17156. (org-flag-drawer t)
  17157. (beginning-of-line 2)
  17158. (org-indent-line-function)))))
  17159. (defun org-clock-out (&optional fail-quietly)
  17160. "Stop the currently running clock.
  17161. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17162. (interactive)
  17163. (catch 'exit
  17164. (if (not (marker-buffer org-clock-marker))
  17165. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17166. (let (ts te s h m)
  17167. (save-excursion
  17168. (set-buffer (marker-buffer org-clock-marker))
  17169. (goto-char org-clock-marker)
  17170. (beginning-of-line 1)
  17171. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17172. (equal (match-string 1) org-clock-string))
  17173. (setq ts (match-string 2))
  17174. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17175. (goto-char (match-end 0))
  17176. (delete-region (point) (point-at-eol))
  17177. (insert "--")
  17178. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17179. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17180. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17181. h (floor (/ s 3600))
  17182. s (- s (* 3600 h))
  17183. m (floor (/ s 60))
  17184. s (- s (* 60 s)))
  17185. (insert " => " (format "%2d:%02d" h m))
  17186. (move-marker org-clock-marker nil)
  17187. (when org-log-note-clock-out
  17188. (org-add-log-maybe 'clock-out))
  17189. (when org-mode-line-timer
  17190. (cancel-timer org-mode-line-timer)
  17191. (setq org-mode-line-timer nil))
  17192. (setq global-mode-string
  17193. (delq 'org-mode-line-string global-mode-string))
  17194. (force-mode-line-update)
  17195. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17196. (defun org-clock-cancel ()
  17197. "Cancel the running clock be removing the start timestamp."
  17198. (interactive)
  17199. (if (not (marker-buffer org-clock-marker))
  17200. (error "No active clock"))
  17201. (save-excursion
  17202. (set-buffer (marker-buffer org-clock-marker))
  17203. (goto-char org-clock-marker)
  17204. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17205. (setq global-mode-string
  17206. (delq 'org-mode-line-string global-mode-string))
  17207. (force-mode-line-update)
  17208. (message "Clock canceled"))
  17209. (defun org-clock-goto (&optional delete-windows)
  17210. "Go to the currently clocked-in entry."
  17211. (interactive "P")
  17212. (if (not (marker-buffer org-clock-marker))
  17213. (error "No active clock"))
  17214. (switch-to-buffer-other-window
  17215. (marker-buffer org-clock-marker))
  17216. (if delete-windows (delete-other-windows))
  17217. (goto-char org-clock-marker)
  17218. (org-show-entry)
  17219. (org-back-to-heading)
  17220. (recenter))
  17221. (defvar org-clock-file-total-minutes nil
  17222. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17223. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17224. (defun org-clock-sum (&optional tstart tend)
  17225. "Sum the times for each subtree.
  17226. Puts the resulting times in minutes as a text property on each headline."
  17227. (interactive)
  17228. (let* ((bmp (buffer-modified-p))
  17229. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17230. org-clock-string
  17231. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17232. (lmax 30)
  17233. (ltimes (make-vector lmax 0))
  17234. (t1 0)
  17235. (level 0)
  17236. ts te dt
  17237. time)
  17238. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17239. (save-excursion
  17240. (goto-char (point-max))
  17241. (while (re-search-backward re nil t)
  17242. (cond
  17243. ((match-end 2)
  17244. ;; Two time stamps
  17245. (setq ts (match-string 2)
  17246. te (match-string 3)
  17247. ts (time-to-seconds
  17248. (apply 'encode-time (org-parse-time-string ts)))
  17249. te (time-to-seconds
  17250. (apply 'encode-time (org-parse-time-string te)))
  17251. ts (if tstart (max ts tstart) ts)
  17252. te (if tend (min te tend) te)
  17253. dt (- te ts)
  17254. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17255. ((match-end 4)
  17256. ;; A naket time
  17257. (setq t1 (+ t1 (string-to-number (match-string 5))
  17258. (* 60 (string-to-number (match-string 4))))))
  17259. (t ;; A headline
  17260. (setq level (- (match-end 1) (match-beginning 1)))
  17261. (when (or (> t1 0) (> (aref ltimes level) 0))
  17262. (loop for l from 0 to level do
  17263. (aset ltimes l (+ (aref ltimes l) t1)))
  17264. (setq t1 0 time (aref ltimes level))
  17265. (loop for l from level to (1- lmax) do
  17266. (aset ltimes l 0))
  17267. (goto-char (match-beginning 0))
  17268. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17269. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17270. (set-buffer-modified-p bmp)))
  17271. (defun org-clock-display (&optional total-only)
  17272. "Show subtree times in the entire buffer.
  17273. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17274. in the echo area."
  17275. (interactive)
  17276. (org-remove-clock-overlays)
  17277. (let (time h m p)
  17278. (org-clock-sum)
  17279. (unless total-only
  17280. (save-excursion
  17281. (goto-char (point-min))
  17282. (while (or (and (equal (setq p (point)) (point-min))
  17283. (get-text-property p :org-clock-minutes))
  17284. (setq p (next-single-property-change
  17285. (point) :org-clock-minutes)))
  17286. (goto-char p)
  17287. (when (setq time (get-text-property p :org-clock-minutes))
  17288. (org-put-clock-overlay time (funcall outline-level))))
  17289. (setq h (/ org-clock-file-total-minutes 60)
  17290. m (- org-clock-file-total-minutes (* 60 h)))
  17291. ;; Arrange to remove the overlays upon next change.
  17292. (when org-remove-highlights-with-change
  17293. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17294. nil 'local))))
  17295. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17296. (defvar org-clock-overlays nil)
  17297. (make-variable-buffer-local 'org-clock-overlays)
  17298. (defun org-put-clock-overlay (time &optional level)
  17299. "Put an overlays on the current line, displaying TIME.
  17300. If LEVEL is given, prefix time with a corresponding number of stars.
  17301. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17302. will be easy to remove."
  17303. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17304. (l (if level (org-get-legal-level level 0) 0))
  17305. (off 0)
  17306. ov tx)
  17307. (move-to-column c)
  17308. (unless (eolp) (skip-chars-backward "^ \t"))
  17309. (skip-chars-backward " \t")
  17310. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17311. tx (concat (buffer-substring (1- (point)) (point))
  17312. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17313. (org-add-props (format "%s %2d:%02d%s"
  17314. (make-string l ?*) h m
  17315. (make-string (- 10 l) ?\ ))
  17316. '(face secondary-selection))
  17317. ""))
  17318. (if (not (featurep 'xemacs))
  17319. (org-overlay-put ov 'display tx)
  17320. (org-overlay-put ov 'invisible t)
  17321. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17322. (push ov org-clock-overlays)))
  17323. (defun org-remove-clock-overlays (&optional beg end noremove)
  17324. "Remove the occur highlights from the buffer.
  17325. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17326. from the `before-change-functions' in the current buffer."
  17327. (interactive)
  17328. (unless org-inhibit-highlight-removal
  17329. (mapc 'org-delete-overlay org-clock-overlays)
  17330. (setq org-clock-overlays nil)
  17331. (unless noremove
  17332. (remove-hook 'before-change-functions
  17333. 'org-remove-clock-overlays 'local))))
  17334. (defun org-clock-out-if-current ()
  17335. "Clock out if the current entry contains the running clock.
  17336. This is used to stop the clock after a TODO entry is marked DONE,
  17337. and is only done if the variable `org-clock-out-when-done' is not nil."
  17338. (when (and org-clock-out-when-done
  17339. (member state org-done-keywords)
  17340. (equal (marker-buffer org-clock-marker) (current-buffer))
  17341. (< (point) org-clock-marker)
  17342. (> (save-excursion (outline-next-heading) (point))
  17343. org-clock-marker))
  17344. ;; Clock out, but don't accept a logging message for this.
  17345. (let ((org-log-note-clock-out nil))
  17346. (org-clock-out))))
  17347. (add-hook 'org-after-todo-state-change-hook
  17348. 'org-clock-out-if-current)
  17349. (defun org-check-running-clock ()
  17350. "Check if the current buffer contains the running clock.
  17351. If yes, offer to stop it and to save the buffer with the changes."
  17352. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17353. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17354. (buffer-name))))
  17355. (org-clock-out)
  17356. (when (y-or-n-p "Save changed buffer?")
  17357. (save-buffer))))
  17358. (defun org-clock-report (&optional arg)
  17359. "Create a table containing a report about clocked time.
  17360. If the cursor is inside an existing clocktable block, then the table
  17361. will be updated. If not, a new clocktable will be inserted.
  17362. When called with a prefix argument, move to the first clock table in the
  17363. buffer and update it."
  17364. (interactive "P")
  17365. (org-remove-clock-overlays)
  17366. (when arg
  17367. (org-find-dblock "clocktable")
  17368. (org-show-entry))
  17369. (if (org-in-clocktable-p)
  17370. (goto-char (org-in-clocktable-p))
  17371. (org-create-dblock (list :name "clocktable"
  17372. :maxlevel 2 :scope 'file)))
  17373. (org-update-dblock))
  17374. (defun org-in-clocktable-p ()
  17375. "Check if the cursor is in a clocktable."
  17376. (let ((pos (point)) start)
  17377. (save-excursion
  17378. (end-of-line 1)
  17379. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17380. (setq start (match-beginning 0))
  17381. (re-search-forward "^#\\+END:.*" nil t)
  17382. (>= (match-end 0) pos)
  17383. start))))
  17384. (defun org-clock-update-time-maybe ()
  17385. "If this is a CLOCK line, update it and return t.
  17386. Otherwise, return nil."
  17387. (interactive)
  17388. (save-excursion
  17389. (beginning-of-line 1)
  17390. (skip-chars-forward " \t")
  17391. (when (looking-at org-clock-string)
  17392. (let ((re (concat "[ \t]*" org-clock-string
  17393. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17394. "\\([ \t]*=>.*\\)?"))
  17395. ts te h m s)
  17396. (if (not (looking-at re))
  17397. nil
  17398. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17399. (end-of-line 1)
  17400. (setq ts (match-string 1)
  17401. te (match-string 2))
  17402. (setq s (- (time-to-seconds
  17403. (apply 'encode-time (org-parse-time-string te)))
  17404. (time-to-seconds
  17405. (apply 'encode-time (org-parse-time-string ts))))
  17406. h (floor (/ s 3600))
  17407. s (- s (* 3600 h))
  17408. m (floor (/ s 60))
  17409. s (- s (* 60 s)))
  17410. (insert " => " (format "%2d:%02d" h m))
  17411. t)))))
  17412. (defun org-clock-special-range (key &optional time as-strings)
  17413. "Return two times bordering a special time range.
  17414. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17415. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17416. A week starts Monday 0:00 and ends Sunday 24:00.
  17417. The range is determined relative to TIME. TIME defaults to the current time.
  17418. The return value is a cons cell with two internal times like the ones
  17419. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17420. the returned times will be formatted strings."
  17421. (let* ((tm (decode-time (or time (current-time))))
  17422. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17423. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17424. (dow (nth 6 tm))
  17425. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17426. (cond
  17427. ((eq key 'today)
  17428. (setq h 0 m 0 h1 24 m1 0))
  17429. ((eq key 'yesterday)
  17430. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17431. ((eq key 'thisweek)
  17432. (setq diff (if (= dow 0) 6 (1- dow))
  17433. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17434. ((eq key 'lastweek)
  17435. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17436. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17437. ((eq key 'thismonth)
  17438. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17439. ((eq key 'lastmonth)
  17440. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17441. ((eq key 'thisyear)
  17442. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17443. ((eq key 'lastyear)
  17444. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17445. (t (error "No such time block %s" key)))
  17446. (setq ts (encode-time s m h d month y)
  17447. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17448. (or d1 d) (or month1 month) (or y1 y)))
  17449. (setq fm (cdr org-time-stamp-formats))
  17450. (if as-strings
  17451. (cons (format-time-string fm ts) (format-time-string fm te))
  17452. (cons ts te))))
  17453. (defun org-dblock-write:clocktable (params)
  17454. "Write the standard clocktable."
  17455. (catch 'exit
  17456. (let* ((hlchars '((1 . "*") (2 . "/")))
  17457. (ins (make-marker))
  17458. (total-time nil)
  17459. (scope (plist-get params :scope))
  17460. (tostring (plist-get params :tostring))
  17461. (multifile (plist-get params :multifile))
  17462. (header (plist-get params :header))
  17463. (maxlevel (or (plist-get params :maxlevel) 3))
  17464. (step (plist-get params :step))
  17465. (emph (plist-get params :emphasize))
  17466. (ts (plist-get params :tstart))
  17467. (te (plist-get params :tend))
  17468. (block (plist-get params :block))
  17469. ipos time h m p level hlc hdl
  17470. cc beg end pos tbl)
  17471. (when step
  17472. (org-clocktable-steps params)
  17473. (throw 'exit nil))
  17474. (when block
  17475. (setq cc (org-clock-special-range block nil t)
  17476. ts (car cc) te (cdr cc)))
  17477. (if ts (setq ts (time-to-seconds
  17478. (apply 'encode-time (org-parse-time-string ts)))))
  17479. (if te (setq te (time-to-seconds
  17480. (apply 'encode-time (org-parse-time-string te)))))
  17481. (move-marker ins (point))
  17482. (setq ipos (point))
  17483. ;; Get the right scope
  17484. (setq pos (point))
  17485. (save-restriction
  17486. (cond
  17487. ((not scope))
  17488. ((eq scope 'file) (widen))
  17489. ((eq scope 'subtree) (org-narrow-to-subtree))
  17490. ((eq scope 'tree)
  17491. (while (org-up-heading-safe))
  17492. (org-narrow-to-subtree))
  17493. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17494. (symbol-name scope)))
  17495. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17496. (catch 'exit
  17497. (while (org-up-heading-safe)
  17498. (looking-at outline-regexp)
  17499. (if (<= (org-reduced-level (funcall outline-level)) level)
  17500. (throw 'exit nil))))
  17501. (org-narrow-to-subtree))
  17502. ((or (listp scope) (eq scope 'agenda))
  17503. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17504. (scope 'agenda)
  17505. (p1 (copy-sequence params))
  17506. file)
  17507. (plist-put p1 :tostring t)
  17508. (plist-put p1 :multifile t)
  17509. (plist-put p1 :scope 'file)
  17510. (org-prepare-agenda-buffers files)
  17511. (while (setq file (pop files))
  17512. (with-current-buffer (find-buffer-visiting file)
  17513. (push (org-clocktable-add-file
  17514. file (org-dblock-write:clocktable p1)) tbl)
  17515. (setq total-time (+ (or total-time 0)
  17516. org-clock-file-total-minutes)))))))
  17517. (goto-char pos)
  17518. (unless (eq scope 'agenda)
  17519. (org-clock-sum ts te)
  17520. (goto-char (point-min))
  17521. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17522. (goto-char p)
  17523. (when (setq time (get-text-property p :org-clock-minutes))
  17524. (save-excursion
  17525. (beginning-of-line 1)
  17526. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17527. (setq level (org-reduced-level
  17528. (- (match-end 1) (match-beginning 1))))
  17529. (<= level maxlevel))
  17530. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17531. hdl (match-string 2)
  17532. h (/ time 60)
  17533. m (- time (* 60 h)))
  17534. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17535. (push (concat
  17536. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17537. (make-string (1- level) ?|)
  17538. hlc (format "%d:%02d" h m) hlc
  17539. " |") tbl))))))
  17540. (setq tbl (nreverse tbl))
  17541. (if tostring
  17542. (if tbl (mapconcat 'identity tbl "\n") nil)
  17543. (goto-char ins)
  17544. (insert-before-markers
  17545. (or header
  17546. (concat
  17547. "Clock summary at ["
  17548. (substring
  17549. (format-time-string (cdr org-time-stamp-formats))
  17550. 1 -1)
  17551. "]."
  17552. (if block
  17553. (format " Considered range is /%s/." block)
  17554. "")
  17555. "\n\n"))
  17556. (if (eq scope 'agenda) "|File" "")
  17557. "|L|Headline|Time|\n")
  17558. (setq total-time (or total-time org-clock-file-total-minutes)
  17559. h (/ total-time 60)
  17560. m (- total-time (* 60 h)))
  17561. (insert-before-markers
  17562. "|-\n|"
  17563. (if (eq scope 'agenda) "|" "")
  17564. "|"
  17565. "*Total time*| "
  17566. (format "*%d:%02d*" h m)
  17567. "|\n|-\n")
  17568. (setq tbl (delq nil tbl))
  17569. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17570. (equal (substring (car tbl) 0 2) "|-"))
  17571. (pop tbl))
  17572. (insert-before-markers (mapconcat
  17573. 'identity (delq nil tbl)
  17574. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17575. (backward-delete-char 1)
  17576. (goto-char ipos)
  17577. (skip-chars-forward "^|")
  17578. (org-table-align))))))
  17579. (defun org-clocktable-steps (params)
  17580. (let* ((p1 (copy-sequence params))
  17581. (ts (plist-get p1 :tstart))
  17582. (te (plist-get p1 :tend))
  17583. (step0 (plist-get p1 :step))
  17584. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17585. (block (plist-get p1 :block))
  17586. cc)
  17587. (when block
  17588. (setq cc (org-clock-special-range block nil t)
  17589. ts (car cc) te (cdr cc)))
  17590. (if ts (setq ts (time-to-seconds
  17591. (apply 'encode-time (org-parse-time-string ts)))))
  17592. (if te (setq te (time-to-seconds
  17593. (apply 'encode-time (org-parse-time-string te)))))
  17594. (plist-put p1 :header "")
  17595. (plist-put p1 :step nil)
  17596. (plist-put p1 :block nil)
  17597. (while (< ts te)
  17598. (or (bolp) (insert "\n"))
  17599. (plist-put p1 :tstart (format-time-string
  17600. (car org-time-stamp-formats)
  17601. (seconds-to-time ts)))
  17602. (plist-put p1 :tend (format-time-string
  17603. (car org-time-stamp-formats)
  17604. (seconds-to-time (setq ts (+ ts step)))))
  17605. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17606. (plist-get p1 :tstart) "\n")
  17607. (org-dblock-write:clocktable p1)
  17608. (re-search-forward "#\\+END:")
  17609. (end-of-line 0))))
  17610. (defun org-clocktable-add-file (file table)
  17611. (if table
  17612. (let ((lines (org-split-string table "\n"))
  17613. (ff (file-name-nondirectory file)))
  17614. (mapconcat 'identity
  17615. (mapcar (lambda (x)
  17616. (if (string-match org-table-dataline-regexp x)
  17617. (concat "|" ff x)
  17618. x))
  17619. lines)
  17620. "\n"))))
  17621. ;; FIXME: I don't think anybody uses this, ask David
  17622. (defun org-collect-clock-time-entries ()
  17623. "Return an internal list with clocking information.
  17624. This list has one entry for each CLOCK interval.
  17625. FIXME: describe the elements."
  17626. (interactive)
  17627. (let ((re (concat "^[ \t]*" org-clock-string
  17628. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17629. rtn beg end next cont level title total closedp leafp
  17630. clockpos titlepos h m donep)
  17631. (save-excursion
  17632. (org-clock-sum)
  17633. (goto-char (point-min))
  17634. (while (re-search-forward re nil t)
  17635. (setq clockpos (match-beginning 0)
  17636. beg (match-string 1) end (match-string 2)
  17637. cont (match-end 0))
  17638. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17639. end (apply 'encode-time (org-parse-time-string end)))
  17640. (org-back-to-heading t)
  17641. (setq donep (org-entry-is-done-p))
  17642. (setq titlepos (point)
  17643. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17644. h (/ total 60) m (- total (* 60 h))
  17645. total (cons h m))
  17646. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17647. (setq level (- (match-end 1) (match-beginning 1))
  17648. title (org-match-string-no-properties 2))
  17649. (save-excursion (outline-next-heading) (setq next (point)))
  17650. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17651. (goto-char next)
  17652. (setq leafp (and (looking-at "^\\*+ ")
  17653. (<= (- (match-end 0) (point)) level)))
  17654. (push (list beg end clockpos closedp donep
  17655. total title titlepos level leafp)
  17656. rtn)
  17657. (goto-char cont)))
  17658. (nreverse rtn)))
  17659. ;;;; Agenda, and Diary Integration
  17660. ;;; Define the Org-agenda-mode
  17661. (defvar org-agenda-mode-map (make-sparse-keymap)
  17662. "Keymap for `org-agenda-mode'.")
  17663. (defvar org-agenda-menu) ; defined later in this file.
  17664. (defvar org-agenda-follow-mode nil)
  17665. (defvar org-agenda-show-log nil)
  17666. (defvar org-agenda-redo-command nil)
  17667. (defvar org-agenda-mode-hook nil)
  17668. (defvar org-agenda-type nil)
  17669. (defvar org-agenda-force-single-file nil)
  17670. (defun org-agenda-mode ()
  17671. "Mode for time-sorted view on action items in Org-mode files.
  17672. The following commands are available:
  17673. \\{org-agenda-mode-map}"
  17674. (interactive)
  17675. (kill-all-local-variables)
  17676. (setq org-agenda-undo-list nil
  17677. org-agenda-pending-undo-list nil)
  17678. (setq major-mode 'org-agenda-mode)
  17679. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17680. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17681. (setq mode-name "Org-Agenda")
  17682. (use-local-map org-agenda-mode-map)
  17683. (easy-menu-add org-agenda-menu)
  17684. (if org-startup-truncated (setq truncate-lines t))
  17685. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17686. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17687. ;; Make sure properties are removed when copying text
  17688. (when (boundp 'buffer-substring-filters)
  17689. (org-set-local 'buffer-substring-filters
  17690. (cons (lambda (x)
  17691. (set-text-properties 0 (length x) nil x) x)
  17692. buffer-substring-filters)))
  17693. (unless org-agenda-keep-modes
  17694. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17695. org-agenda-show-log nil))
  17696. (easy-menu-change
  17697. '("Agenda") "Agenda Files"
  17698. (append
  17699. (list
  17700. (vector
  17701. (if (get 'org-agenda-files 'org-restrict)
  17702. "Restricted to single file"
  17703. "Edit File List")
  17704. '(org-edit-agenda-file-list)
  17705. (not (get 'org-agenda-files 'org-restrict)))
  17706. "--")
  17707. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17708. (org-agenda-set-mode-name)
  17709. (apply
  17710. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17711. (list 'org-agenda-mode-hook)))
  17712. (substitute-key-definition 'undo 'org-agenda-undo
  17713. org-agenda-mode-map global-map)
  17714. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17715. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17716. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17717. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17718. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17719. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17720. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17721. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17722. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17723. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17724. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17725. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17726. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17727. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17728. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17729. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17730. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17731. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17732. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17733. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17734. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17735. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17736. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17737. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17738. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17739. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17740. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17741. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17742. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17743. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17744. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17745. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17746. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17747. (while l (org-defkey org-agenda-mode-map
  17748. (int-to-string (pop l)) 'digit-argument)))
  17749. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17750. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17751. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17752. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17753. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17754. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17755. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17756. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17757. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17758. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17759. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17760. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17761. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17762. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17763. (org-defkey org-agenda-mode-map "n" 'next-line)
  17764. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17765. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17766. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17767. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17768. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17769. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17770. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17771. (eval-after-load "calendar"
  17772. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17773. 'org-calendar-goto-agenda))
  17774. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17775. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17776. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17777. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17778. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17779. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17780. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17781. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17782. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17783. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17784. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17785. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17786. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17787. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17788. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17789. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17790. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17791. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17792. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17793. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17794. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17795. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17796. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17797. "Local keymap for agenda entries from Org-mode.")
  17798. (org-defkey org-agenda-keymap
  17799. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17800. (org-defkey org-agenda-keymap
  17801. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17802. (when org-agenda-mouse-1-follows-link
  17803. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17804. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17805. '("Agenda"
  17806. ("Agenda Files")
  17807. "--"
  17808. ["Show" org-agenda-show t]
  17809. ["Go To (other window)" org-agenda-goto t]
  17810. ["Go To (this window)" org-agenda-switch-to t]
  17811. ["Follow Mode" org-agenda-follow-mode
  17812. :style toggle :selected org-agenda-follow-mode :active t]
  17813. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17814. "--"
  17815. ["Cycle TODO" org-agenda-todo t]
  17816. ["Archive subtree" org-agenda-archive t]
  17817. ["Delete subtree" org-agenda-kill t]
  17818. "--"
  17819. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17820. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17821. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17822. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17823. "--"
  17824. ("Tags and Properties"
  17825. ["Show all Tags" org-agenda-show-tags t]
  17826. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17827. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17828. "--"
  17829. ["Column View" org-columns t])
  17830. ("Date/Schedule"
  17831. ["Schedule" org-agenda-schedule t]
  17832. ["Set Deadline" org-agenda-deadline t]
  17833. "--"
  17834. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17835. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17836. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17837. ("Clock"
  17838. ["Clock in" org-agenda-clock-in t]
  17839. ["Clock out" org-agenda-clock-out t]
  17840. ["Clock cancel" org-agenda-clock-cancel t]
  17841. ["Goto running clock" org-clock-goto t])
  17842. ("Priority"
  17843. ["Set Priority" org-agenda-priority t]
  17844. ["Increase Priority" org-agenda-priority-up t]
  17845. ["Decrease Priority" org-agenda-priority-down t]
  17846. ["Show Priority" org-agenda-show-priority t])
  17847. ("Calendar/Diary"
  17848. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17849. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17850. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17851. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17852. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17853. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17854. "--"
  17855. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17856. "--"
  17857. ("View"
  17858. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17859. :style radio :selected (equal org-agenda-ndays 1)]
  17860. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17861. :style radio :selected (equal org-agenda-ndays 7)]
  17862. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17863. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17864. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17865. :style radio :selected (member org-agenda-ndays '(365 366))]
  17866. "--"
  17867. ["Show Logbook entries" org-agenda-log-mode
  17868. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17869. ["Include Diary" org-agenda-toggle-diary
  17870. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17871. ["Use Time Grid" org-agenda-toggle-time-grid
  17872. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17873. ["Write view to file" org-write-agenda t]
  17874. ["Rebuild buffer" org-agenda-redo t]
  17875. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17876. "--"
  17877. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17878. "--"
  17879. ["Quit" org-agenda-quit t]
  17880. ["Exit and Release Buffers" org-agenda-exit t]
  17881. ))
  17882. ;;; Agenda undo
  17883. (defvar org-agenda-allow-remote-undo t
  17884. "Non-nil means, allow remote undo from the agenda buffer.")
  17885. (defvar org-agenda-undo-list nil
  17886. "List of undoable operations in the agenda since last refresh.")
  17887. (defvar org-agenda-undo-has-started-in nil
  17888. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17889. (defvar org-agenda-pending-undo-list nil
  17890. "In a series of undo commands, this is the list of remaning undo items.")
  17891. (defmacro org-if-unprotected (&rest body)
  17892. "Execute BODY if there is no `org-protected' text property at point."
  17893. (declare (debug t))
  17894. `(unless (get-text-property (point) 'org-protected)
  17895. ,@body))
  17896. (defmacro org-with-remote-undo (_buffer &rest _body)
  17897. "Execute BODY while recording undo information in two buffers."
  17898. (declare (indent 1) (debug t))
  17899. `(let ((_cline (org-current-line))
  17900. (_cmd this-command)
  17901. (_buf1 (current-buffer))
  17902. (_buf2 ,_buffer)
  17903. (_undo1 buffer-undo-list)
  17904. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17905. _c1 _c2)
  17906. ,@_body
  17907. (when org-agenda-allow-remote-undo
  17908. (setq _c1 (org-verify-change-for-undo
  17909. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17910. _c2 (org-verify-change-for-undo
  17911. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17912. (when (or _c1 _c2)
  17913. ;; make sure there are undo boundaries
  17914. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17915. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17916. ;; remember which buffer to undo
  17917. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17918. org-agenda-undo-list)))))
  17919. (defun org-agenda-undo ()
  17920. "Undo a remote editing step in the agenda.
  17921. This undoes changes both in the agenda buffer and in the remote buffer
  17922. that have been changed along."
  17923. (interactive)
  17924. (or org-agenda-allow-remote-undo
  17925. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17926. (if (not (eq this-command last-command))
  17927. (setq org-agenda-undo-has-started-in nil
  17928. org-agenda-pending-undo-list org-agenda-undo-list))
  17929. (if (not org-agenda-pending-undo-list)
  17930. (error "No further undo information"))
  17931. (let* ((entry (pop org-agenda-pending-undo-list))
  17932. buf line cmd rembuf)
  17933. (setq cmd (pop entry) line (pop entry))
  17934. (setq rembuf (nth 2 entry))
  17935. (org-with-remote-undo rembuf
  17936. (while (bufferp (setq buf (pop entry)))
  17937. (if (pop entry)
  17938. (with-current-buffer buf
  17939. (let ((last-undo-buffer buf)
  17940. (inhibit-read-only t))
  17941. (unless (memq buf org-agenda-undo-has-started-in)
  17942. (push buf org-agenda-undo-has-started-in)
  17943. (make-local-variable 'pending-undo-list)
  17944. (undo-start))
  17945. (while (and pending-undo-list
  17946. (listp pending-undo-list)
  17947. (not (car pending-undo-list)))
  17948. (pop pending-undo-list))
  17949. (undo-more 1))))))
  17950. (goto-line line)
  17951. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17952. (defun org-verify-change-for-undo (l1 l2)
  17953. "Verify that a real change occurred between the undo lists L1 and L2."
  17954. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17955. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17956. (not (eq l1 l2)))
  17957. ;;; Agenda dispatch
  17958. (defvar org-agenda-restrict nil)
  17959. (defvar org-agenda-restrict-begin (make-marker))
  17960. (defvar org-agenda-restrict-end (make-marker))
  17961. (defvar org-agenda-last-dispatch-buffer nil)
  17962. (defvar org-agenda-overriding-restriction nil)
  17963. ;;;###autoload
  17964. (defun org-agenda (arg &optional keys restriction)
  17965. "Dispatch agenda commands to collect entries to the agenda buffer.
  17966. Prompts for a command to execute. Any prefix arg will be passed
  17967. on to the selected command. The default selections are:
  17968. a Call `org-agenda-list' to display the agenda for current day or week.
  17969. t Call `org-todo-list' to display the global todo list.
  17970. T Call `org-todo-list' to display the global todo list, select only
  17971. entries with a specific TODO keyword (the user gets a prompt).
  17972. m Call `org-tags-view' to display headlines with tags matching
  17973. a condition (the user is prompted for the condition).
  17974. M Like `m', but select only TODO entries, no ordinary headlines.
  17975. L Create a timeline for the current buffer.
  17976. e Export views to associated files.
  17977. More commands can be added by configuring the variable
  17978. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17979. searches can be pre-defined in this way.
  17980. If the current buffer is in Org-mode and visiting a file, you can also
  17981. first press `<' once to indicate that the agenda should be temporarily
  17982. \(until the next use of \\[org-agenda]) restricted to the current file.
  17983. Pressing `<' twice means to restrict to the current subtree or region
  17984. \(if active)."
  17985. (interactive "P")
  17986. (catch 'exit
  17987. (let* ((prefix-descriptions nil)
  17988. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17989. (org-agenda-custom-commands
  17990. ;; normalize different versions
  17991. (delq nil
  17992. (mapcar
  17993. (lambda (x)
  17994. (cond ((stringp (cdr x))
  17995. (push x prefix-descriptions)
  17996. nil)
  17997. ((stringp (nth 1 x)) x)
  17998. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17999. (t (cons (car x) (cons "" (cdr x))))))
  18000. org-agenda-custom-commands)))
  18001. (buf (current-buffer))
  18002. (bfn (buffer-file-name (buffer-base-buffer)))
  18003. entry key type match lprops ans)
  18004. ;; Turn off restriction unless there is an overriding one
  18005. (unless org-agenda-overriding-restriction
  18006. (put 'org-agenda-files 'org-restrict nil)
  18007. (setq org-agenda-restrict nil)
  18008. (move-marker org-agenda-restrict-begin nil)
  18009. (move-marker org-agenda-restrict-end nil))
  18010. ;; Delete old local properties
  18011. (put 'org-agenda-redo-command 'org-lprops nil)
  18012. ;; Remember where this call originated
  18013. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18014. (unless keys
  18015. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18016. keys (car ans)
  18017. restriction (cdr ans)))
  18018. ;; Estabish the restriction, if any
  18019. (when (and (not org-agenda-overriding-restriction) restriction)
  18020. (put 'org-agenda-files 'org-restrict (list bfn))
  18021. (cond
  18022. ((eq restriction 'region)
  18023. (setq org-agenda-restrict t)
  18024. (move-marker org-agenda-restrict-begin (region-beginning))
  18025. (move-marker org-agenda-restrict-end (region-end)))
  18026. ((eq restriction 'subtree)
  18027. (save-excursion
  18028. (setq org-agenda-restrict t)
  18029. (org-back-to-heading t)
  18030. (move-marker org-agenda-restrict-begin (point))
  18031. (move-marker org-agenda-restrict-end
  18032. (progn (org-end-of-subtree t)))))))
  18033. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18034. ;; For example the todo list should not need it (but does...)
  18035. (cond
  18036. ((setq entry (assoc keys org-agenda-custom-commands))
  18037. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18038. (progn
  18039. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18040. (put 'org-agenda-redo-command 'org-lprops lprops)
  18041. (cond
  18042. ((eq type 'agenda)
  18043. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18044. ((eq type 'alltodo)
  18045. (org-let lprops '(org-todo-list current-prefix-arg)))
  18046. ((eq type 'stuck)
  18047. (org-let lprops '(org-agenda-list-stuck-projects
  18048. current-prefix-arg)))
  18049. ((eq type 'tags)
  18050. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18051. ((eq type 'tags-todo)
  18052. (org-let lprops '(org-tags-view '(4) match)))
  18053. ((eq type 'todo)
  18054. (org-let lprops '(org-todo-list match)))
  18055. ((eq type 'tags-tree)
  18056. (org-check-for-org-mode)
  18057. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18058. ((eq type 'todo-tree)
  18059. (org-check-for-org-mode)
  18060. (org-let lprops
  18061. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18062. (regexp-quote match) "\\>"))))
  18063. ((eq type 'occur-tree)
  18064. (org-check-for-org-mode)
  18065. (org-let lprops '(org-occur match)))
  18066. ((functionp type)
  18067. (org-let lprops '(funcall type match)))
  18068. ((fboundp type)
  18069. (org-let lprops '(funcall type match)))
  18070. (t (error "Invalid custom agenda command type %s" type))))
  18071. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18072. ((equal keys "C")
  18073. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18074. (customize-variable 'org-agenda-custom-commands))
  18075. ((equal keys "a") (call-interactively 'org-agenda-list))
  18076. ((equal keys "t") (call-interactively 'org-todo-list))
  18077. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18078. ((equal keys "m") (call-interactively 'org-tags-view))
  18079. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18080. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18081. ((equal keys "L")
  18082. (unless (org-mode-p)
  18083. (error "This is not an Org-mode file"))
  18084. (unless restriction
  18085. (put 'org-agenda-files 'org-restrict (list bfn))
  18086. (org-call-with-arg 'org-timeline arg)))
  18087. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18088. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18089. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18090. (t (error "Invalid agenda key"))))))
  18091. (defun org-agenda-normalize-custom-commands (cmds)
  18092. (delq nil
  18093. (mapcar
  18094. (lambda (x)
  18095. (cond ((stringp (cdr x)) nil)
  18096. ((stringp (nth 1 x)) x)
  18097. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18098. (t (cons (car x) (cons "" (cdr x))))))
  18099. cmds)))
  18100. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18101. "The user interface for selecting an agenda command."
  18102. (catch 'exit
  18103. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18104. (restrict-ok (and bfn (org-mode-p)))
  18105. (region-p (org-region-active-p))
  18106. (custom org-agenda-custom-commands)
  18107. (selstring "")
  18108. restriction second-time
  18109. c entry key type match prefixes rmheader header-end custom1 desc)
  18110. (save-window-excursion
  18111. (delete-other-windows)
  18112. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18113. (erase-buffer)
  18114. (insert (eval-when-compile
  18115. (let ((header
  18116. "
  18117. Press key for an agenda command: < Buffer,subtree/region restriction
  18118. -------------------------------- > Remove restriction
  18119. a Agenda for current week or day e Export agenda views
  18120. t List of all TODO entries T Entries with special TODO kwd
  18121. m Match a TAGS query M Like m, but only TODO entries
  18122. L Timeline for current buffer # List stuck projects (!=configure)
  18123. / Multi-occur C Configure custom agenda commands
  18124. ")
  18125. (start 0))
  18126. (while (string-match
  18127. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18128. header start)
  18129. (setq start (match-end 0))
  18130. (add-text-properties (match-beginning 2) (match-end 2)
  18131. '(face bold) header))
  18132. header)))
  18133. (setq header-end (move-marker (make-marker) (point)))
  18134. (while t
  18135. (setq custom1 custom)
  18136. (when (eq rmheader t)
  18137. (goto-line 1)
  18138. (re-search-forward ":" nil t)
  18139. (delete-region (match-end 0) (point-at-eol))
  18140. (forward-char 1)
  18141. (looking-at "-+")
  18142. (delete-region (match-end 0) (point-at-eol))
  18143. (move-marker header-end (match-end 0)))
  18144. (goto-char header-end)
  18145. (delete-region (point) (point-max))
  18146. (while (setq entry (pop custom1))
  18147. (setq key (car entry) desc (nth 1 entry)
  18148. type (nth 2 entry) match (nth 3 entry))
  18149. (if (> (length key) 1)
  18150. (add-to-list 'prefixes (string-to-char key))
  18151. (insert
  18152. (format
  18153. "\n%-4s%-14s: %s"
  18154. (org-add-props (copy-sequence key)
  18155. '(face bold))
  18156. (cond
  18157. ((string-match "\\S-" desc) desc)
  18158. ((eq type 'agenda) "Agenda for current week or day")
  18159. ((eq type 'alltodo) "List of all TODO entries")
  18160. ((eq type 'stuck) "List of stuck projects")
  18161. ((eq type 'todo) "TODO keyword")
  18162. ((eq type 'tags) "Tags query")
  18163. ((eq type 'tags-todo) "Tags (TODO)")
  18164. ((eq type 'tags-tree) "Tags tree")
  18165. ((eq type 'todo-tree) "TODO kwd tree")
  18166. ((eq type 'occur-tree) "Occur tree")
  18167. ((functionp type) (if (symbolp type)
  18168. (symbol-name type)
  18169. "Lambda expression"))
  18170. (t "???"))
  18171. (cond
  18172. ((stringp match)
  18173. (org-add-props match nil 'face 'org-warning))
  18174. (match
  18175. (format "set of %d commands" (length match)))
  18176. (t ""))))))
  18177. (when prefixes
  18178. (mapc (lambda (x)
  18179. (insert
  18180. (format "\n%s %s"
  18181. (org-add-props (char-to-string x)
  18182. nil 'face 'bold)
  18183. (or (cdr (assoc (concat selstring (char-to-string x))
  18184. prefix-descriptions))
  18185. "Prefix key"))))
  18186. prefixes))
  18187. (goto-char (point-min))
  18188. (when (fboundp 'fit-window-to-buffer)
  18189. (if second-time
  18190. (if (not (pos-visible-in-window-p (point-max)))
  18191. (fit-window-to-buffer))
  18192. (setq second-time t)
  18193. (fit-window-to-buffer)))
  18194. (message "Press key for agenda command%s:"
  18195. (if (or restrict-ok org-agenda-overriding-restriction)
  18196. (if org-agenda-overriding-restriction
  18197. " (restriction lock active)"
  18198. (if restriction
  18199. (format " (restricted to %s)" restriction)
  18200. " (unrestricted)"))
  18201. ""))
  18202. (setq c (read-char-exclusive))
  18203. (message "")
  18204. (cond
  18205. ((assoc (char-to-string c) custom)
  18206. (setq selstring (concat selstring (char-to-string c)))
  18207. (throw 'exit (cons selstring restriction)))
  18208. ((memq c prefixes)
  18209. (setq selstring (concat selstring (char-to-string c))
  18210. prefixes nil
  18211. rmheader (or rmheader t)
  18212. custom (delq nil (mapcar
  18213. (lambda (x)
  18214. (if (or (= (length (car x)) 1)
  18215. (/= (string-to-char (car x)) c))
  18216. nil
  18217. (cons (substring (car x) 1) (cdr x))))
  18218. custom))))
  18219. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18220. (message "Restriction is only possible in Org-mode buffers")
  18221. (ding) (sit-for 1))
  18222. ((eq c ?1)
  18223. (org-agenda-remove-restriction-lock 'noupdate)
  18224. (setq restriction 'buffer))
  18225. ((eq c ?0)
  18226. (org-agenda-remove-restriction-lock 'noupdate)
  18227. (setq restriction (if region-p 'region 'subtree)))
  18228. ((eq c ?<)
  18229. (org-agenda-remove-restriction-lock 'noupdate)
  18230. (setq restriction
  18231. (cond
  18232. ((eq restriction 'buffer)
  18233. (if region-p 'region 'subtree))
  18234. ((memq restriction '(subtree region))
  18235. nil)
  18236. (t 'buffer))))
  18237. ((eq c ?>)
  18238. (org-agenda-remove-restriction-lock 'noupdate)
  18239. (setq restriction nil))
  18240. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18241. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18242. ((and (> (length selstring) 0) (eq c ?\d))
  18243. (delete-window)
  18244. (org-agenda-get-restriction-and-command prefix-descriptions))
  18245. ((equal c ?q) (error "Abort"))
  18246. (t (error "Invalid key %c" c))))))))
  18247. (defun org-run-agenda-series (name series)
  18248. (org-prepare-agenda name)
  18249. (let* ((org-agenda-multi t)
  18250. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18251. (cmds (car series))
  18252. (gprops (nth 1 series))
  18253. match ;; The byte compiler incorrectly complains about this. Keep it!
  18254. cmd type lprops)
  18255. (while (setq cmd (pop cmds))
  18256. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18257. (cond
  18258. ((eq type 'agenda)
  18259. (org-let2 gprops lprops
  18260. '(call-interactively 'org-agenda-list)))
  18261. ((eq type 'alltodo)
  18262. (org-let2 gprops lprops
  18263. '(call-interactively 'org-todo-list)))
  18264. ((eq type 'stuck)
  18265. (org-let2 gprops lprops
  18266. '(call-interactively 'org-agenda-list-stuck-projects)))
  18267. ((eq type 'tags)
  18268. (org-let2 gprops lprops
  18269. '(org-tags-view current-prefix-arg match)))
  18270. ((eq type 'tags-todo)
  18271. (org-let2 gprops lprops
  18272. '(org-tags-view '(4) match)))
  18273. ((eq type 'todo)
  18274. (org-let2 gprops lprops
  18275. '(org-todo-list match)))
  18276. ((fboundp type)
  18277. (org-let2 gprops lprops
  18278. '(funcall type match)))
  18279. (t (error "Invalid type in command series"))))
  18280. (widen)
  18281. (setq org-agenda-redo-command redo)
  18282. (goto-char (point-min)))
  18283. (org-finalize-agenda))
  18284. ;;;###autoload
  18285. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18286. "Run an agenda command in batch mode and send the result to STDOUT.
  18287. If CMD-KEY is a string of length 1, it is used as a key in
  18288. `org-agenda-custom-commands' and triggers this command. If it is a
  18289. longer string it is used as a tags/todo match string.
  18290. Paramters are alternating variable names and values that will be bound
  18291. before running the agenda command."
  18292. (let (pars)
  18293. (while parameters
  18294. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18295. (if (> (length cmd-key) 2)
  18296. (eval (list 'let (nreverse pars)
  18297. (list 'org-tags-view nil cmd-key)))
  18298. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18299. (set-buffer org-agenda-buffer-name)
  18300. (princ (org-encode-for-stdout (buffer-string)))))
  18301. (defun org-encode-for-stdout (string)
  18302. (if (fboundp 'encode-coding-string)
  18303. (encode-coding-string string buffer-file-coding-system)
  18304. string))
  18305. (defvar org-agenda-info nil)
  18306. ;;;###autoload
  18307. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18308. "Run an agenda command in batch mode and send the result to STDOUT.
  18309. If CMD-KEY is a string of length 1, it is used as a key in
  18310. `org-agenda-custom-commands' and triggers this command. If it is a
  18311. longer string it is used as a tags/todo match string.
  18312. Paramters are alternating variable names and values that will be bound
  18313. before running the agenda command.
  18314. The output gives a line for each selected agenda item. Each
  18315. item is a list of comma-separated values, like this:
  18316. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18317. category The category of the item
  18318. head The headline, without TODO kwd, TAGS and PRIORITY
  18319. type The type of the agenda entry, can be
  18320. todo selected in TODO match
  18321. tagsmatch selected in tags match
  18322. diary imported from diary
  18323. deadline a deadline on given date
  18324. scheduled scheduled on given date
  18325. timestamp entry has timestamp on given date
  18326. closed entry was closed on given date
  18327. upcoming-deadline warning about deadline
  18328. past-scheduled forwarded scheduled item
  18329. block entry has date block including g. date
  18330. todo The todo keyword, if any
  18331. tags All tags including inherited ones, separated by colons
  18332. date The relevant date, like 2007-2-14
  18333. time The time, like 15:00-16:50
  18334. extra Sting with extra planning info
  18335. priority-l The priority letter if any was given
  18336. priority-n The computed numerical priority
  18337. agenda-day The day in the agenda where this is listed"
  18338. (let (pars)
  18339. (while parameters
  18340. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18341. (push (list 'org-agenda-remove-tags t) pars)
  18342. (if (> (length cmd-key) 2)
  18343. (eval (list 'let (nreverse pars)
  18344. (list 'org-tags-view nil cmd-key)))
  18345. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18346. (set-buffer org-agenda-buffer-name)
  18347. (let* ((lines (org-split-string (buffer-string) "\n"))
  18348. line)
  18349. (while (setq line (pop lines))
  18350. (catch 'next
  18351. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18352. (setq org-agenda-info
  18353. (org-fix-agenda-info (text-properties-at 0 line)))
  18354. (princ
  18355. (org-encode-for-stdout
  18356. (mapconcat 'org-agenda-export-csv-mapper
  18357. '(org-category txt type todo tags date time-of-day extra
  18358. priority-letter priority agenda-day)
  18359. ",")))
  18360. (princ "\n"))))))
  18361. (defun org-fix-agenda-info (props)
  18362. "Make sure all properties on an agenda item have a canonical form,
  18363. so the export commands can easily use it."
  18364. (let (tmp re)
  18365. (when (setq tmp (plist-get props 'tags))
  18366. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18367. (when (setq tmp (plist-get props 'date))
  18368. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18369. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18370. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18371. (setq tmp (calendar-date-string tmp)))
  18372. (setq props (plist-put props 'date tmp)))
  18373. (when (setq tmp (plist-get props 'day))
  18374. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18375. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18376. (setq tmp (calendar-date-string tmp)))
  18377. (setq props (plist-put props 'day tmp))
  18378. (setq props (plist-put props 'agenda-day tmp)))
  18379. (when (setq tmp (plist-get props 'txt))
  18380. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18381. (plist-put props 'priority-letter (match-string 1 tmp))
  18382. (setq tmp (replace-match "" t t tmp)))
  18383. (when (and (setq re (plist-get props 'org-todo-regexp))
  18384. (setq re (concat "\\`\\.*" re " ?"))
  18385. (string-match re tmp))
  18386. (plist-put props 'todo (match-string 1 tmp))
  18387. (setq tmp (replace-match "" t t tmp)))
  18388. (plist-put props 'txt tmp)))
  18389. props)
  18390. (defun org-agenda-export-csv-mapper (prop)
  18391. (let ((res (plist-get org-agenda-info prop)))
  18392. (setq res
  18393. (cond
  18394. ((not res) "")
  18395. ((stringp res) res)
  18396. (t (prin1-to-string res))))
  18397. (while (string-match "," res)
  18398. (setq res (replace-match ";" t t res)))
  18399. (org-trim res)))
  18400. ;;;###autoload
  18401. (defun org-store-agenda-views (&rest parameters)
  18402. (interactive)
  18403. (eval (list 'org-batch-store-agenda-views)))
  18404. ;; FIXME, why is this a macro?????
  18405. ;;;###autoload
  18406. (defmacro org-batch-store-agenda-views (&rest parameters)
  18407. "Run all custom agenda commands that have a file argument."
  18408. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18409. (pop-up-frames nil)
  18410. (dir default-directory)
  18411. pars cmd thiscmdkey files opts)
  18412. (while parameters
  18413. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18414. (setq pars (reverse pars))
  18415. (save-window-excursion
  18416. (while cmds
  18417. (setq cmd (pop cmds)
  18418. thiscmdkey (car cmd)
  18419. opts (nth 4 cmd)
  18420. files (nth 5 cmd))
  18421. (if (stringp files) (setq files (list files)))
  18422. (when files
  18423. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18424. (list 'org-agenda nil thiscmdkey)))
  18425. (set-buffer org-agenda-buffer-name)
  18426. (while files
  18427. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18428. (list 'org-write-agenda
  18429. (expand-file-name (pop files) dir) t))))
  18430. (and (get-buffer org-agenda-buffer-name)
  18431. (kill-buffer org-agenda-buffer-name)))))))
  18432. (defun org-write-agenda (file &optional nosettings)
  18433. "Write the current buffer (an agenda view) as a file.
  18434. Depending on the extension of the file name, plain text (.txt),
  18435. HTML (.html or .htm) or Postscript (.ps) is produced.
  18436. If NOSETTINGS is given, do not scope the settings of
  18437. `org-agenda-exporter-settings' into the export commands. This is used when
  18438. the settings have already been scoped and we do not wish to overrule other,
  18439. higher priority settings."
  18440. (interactive "FWrite agenda to file: ")
  18441. (if (not (file-writable-p file))
  18442. (error "Cannot write agenda to file %s" file))
  18443. (cond
  18444. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18445. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18446. (org-let (if nosettings nil org-agenda-exporter-settings)
  18447. '(save-excursion
  18448. (save-window-excursion
  18449. (cond
  18450. ((string-match "\\.html?\\'" file)
  18451. (set-buffer (htmlize-buffer (current-buffer)))
  18452. (when (and org-agenda-export-html-style
  18453. (string-match "<style>" org-agenda-export-html-style))
  18454. ;; replace <style> section with org-agenda-export-html-style
  18455. (goto-char (point-min))
  18456. (kill-region (- (search-forward "<style") 6)
  18457. (search-forward "</style>"))
  18458. (insert org-agenda-export-html-style))
  18459. (write-file file)
  18460. (kill-buffer (current-buffer))
  18461. (message "HTML written to %s" file))
  18462. ((string-match "\\.ps\\'" file)
  18463. (ps-print-buffer-with-faces file)
  18464. (message "Postscript written to %s" file))
  18465. (t
  18466. (let ((bs (buffer-string)))
  18467. (find-file file)
  18468. (insert bs)
  18469. (save-buffer 0)
  18470. (kill-buffer (current-buffer))
  18471. (message "Plain text written to %s" file))))))
  18472. (set-buffer org-agenda-buffer-name)))
  18473. (defmacro org-no-read-only (&rest body)
  18474. "Inhibit read-only for BODY."
  18475. `(let ((inhibit-read-only t)) ,@body))
  18476. (defun org-check-for-org-mode ()
  18477. "Make sure current buffer is in org-mode. Error if not."
  18478. (or (org-mode-p)
  18479. (error "Cannot execute org-mode agenda command on buffer in %s."
  18480. major-mode)))
  18481. (defun org-fit-agenda-window ()
  18482. "Fit the window to the buffer size."
  18483. (and (memq org-agenda-window-setup '(reorganize-frame))
  18484. (fboundp 'fit-window-to-buffer)
  18485. (fit-window-to-buffer
  18486. nil
  18487. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18488. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18489. ;;; Agenda file list
  18490. (defun org-agenda-files (&optional unrestricted)
  18491. "Get the list of agenda files.
  18492. Optional UNRESTRICTED means return the full list even if a restriction
  18493. is currently in place."
  18494. (let ((files
  18495. (cond
  18496. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18497. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18498. ((listp org-agenda-files) org-agenda-files)
  18499. (t (error "Invalid value of `org-agenda-files'")))))
  18500. (setq files (apply 'append
  18501. (mapcar (lambda (f)
  18502. (if (file-directory-p f)
  18503. (directory-files f t
  18504. org-agenda-file-regexp)
  18505. (list f)))
  18506. files)))
  18507. (if org-agenda-skip-unavailable-files
  18508. (delq nil
  18509. (mapcar (function
  18510. (lambda (file)
  18511. (and (file-readable-p file) file)))
  18512. files))
  18513. files))) ; `org-check-agenda-file' will remove them from the list
  18514. (defun org-edit-agenda-file-list ()
  18515. "Edit the list of agenda files.
  18516. Depending on setup, this either uses customize to edit the variable
  18517. `org-agenda-files', or it visits the file that is holding the list. In the
  18518. latter case, the buffer is set up in a way that saving it automatically kills
  18519. the buffer and restores the previous window configuration."
  18520. (interactive)
  18521. (if (stringp org-agenda-files)
  18522. (let ((cw (current-window-configuration)))
  18523. (find-file org-agenda-files)
  18524. (org-set-local 'org-window-configuration cw)
  18525. (org-add-hook 'after-save-hook
  18526. (lambda ()
  18527. (set-window-configuration
  18528. (prog1 org-window-configuration
  18529. (kill-buffer (current-buffer))))
  18530. (org-install-agenda-files-menu)
  18531. (message "New agenda file list installed"))
  18532. nil 'local)
  18533. (message "%s" (substitute-command-keys
  18534. "Edit list and finish with \\[save-buffer]")))
  18535. (customize-variable 'org-agenda-files)))
  18536. (defun org-store-new-agenda-file-list (list)
  18537. "Set new value for the agenda file list and save it correcly."
  18538. (if (stringp org-agenda-files)
  18539. (let ((f org-agenda-files) b)
  18540. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18541. (with-temp-file f
  18542. (insert (mapconcat 'identity list "\n") "\n")))
  18543. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18544. (setq org-agenda-files list)
  18545. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18546. (defun org-read-agenda-file-list ()
  18547. "Read the list of agenda files from a file."
  18548. (when (stringp org-agenda-files)
  18549. (with-temp-buffer
  18550. (insert-file-contents org-agenda-files)
  18551. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18552. ;;;###autoload
  18553. (defun org-cycle-agenda-files ()
  18554. "Cycle through the files in `org-agenda-files'.
  18555. If the current buffer visits an agenda file, find the next one in the list.
  18556. If the current buffer does not, find the first agenda file."
  18557. (interactive)
  18558. (let* ((fs (org-agenda-files t))
  18559. (files (append fs (list (car fs))))
  18560. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18561. file)
  18562. (unless files (error "No agenda files"))
  18563. (catch 'exit
  18564. (while (setq file (pop files))
  18565. (if (equal (file-truename file) tcf)
  18566. (when (car files)
  18567. (find-file (car files))
  18568. (throw 'exit t))))
  18569. (find-file (car fs)))
  18570. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18571. (defun org-agenda-file-to-front (&optional to-end)
  18572. "Move/add the current file to the top of the agenda file list.
  18573. If the file is not present in the list, it is added to the front. If it is
  18574. present, it is moved there. With optional argument TO-END, add/move to the
  18575. end of the list."
  18576. (interactive "P")
  18577. (let ((org-agenda-skip-unavailable-files nil)
  18578. (file-alist (mapcar (lambda (x)
  18579. (cons (file-truename x) x))
  18580. (org-agenda-files t)))
  18581. (ctf (file-truename buffer-file-name))
  18582. x had)
  18583. (setq x (assoc ctf file-alist) had x)
  18584. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18585. (if to-end
  18586. (setq file-alist (append (delq x file-alist) (list x)))
  18587. (setq file-alist (cons x (delq x file-alist))))
  18588. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18589. (org-install-agenda-files-menu)
  18590. (message "File %s to %s of agenda file list"
  18591. (if had "moved" "added") (if to-end "end" "front"))))
  18592. (defun org-remove-file (&optional file)
  18593. "Remove current file from the list of files in variable `org-agenda-files'.
  18594. These are the files which are being checked for agenda entries.
  18595. Optional argument FILE means, use this file instead of the current."
  18596. (interactive)
  18597. (let* ((org-agenda-skip-unavailable-files nil)
  18598. (file (or file buffer-file-name))
  18599. (true-file (file-truename file))
  18600. (afile (abbreviate-file-name file))
  18601. (files (delq nil (mapcar
  18602. (lambda (x)
  18603. (if (equal true-file
  18604. (file-truename x))
  18605. nil x))
  18606. (org-agenda-files t)))))
  18607. (if (not (= (length files) (length (org-agenda-files t))))
  18608. (progn
  18609. (org-store-new-agenda-file-list files)
  18610. (org-install-agenda-files-menu)
  18611. (message "Removed file: %s" afile))
  18612. (message "File was not in list: %s (not removed)" afile))))
  18613. (defun org-file-menu-entry (file)
  18614. (vector file (list 'find-file file) t))
  18615. (defun org-check-agenda-file (file)
  18616. "Make sure FILE exists. If not, ask user what to do."
  18617. (when (not (file-exists-p file))
  18618. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18619. (abbreviate-file-name file))
  18620. (let ((r (downcase (read-char-exclusive))))
  18621. (cond
  18622. ((equal r ?r)
  18623. (org-remove-file file)
  18624. (throw 'nextfile t))
  18625. (t (error "Abort"))))))
  18626. ;;; Agenda prepare and finalize
  18627. (defvar org-agenda-multi nil) ; dynammically scoped
  18628. (defvar org-agenda-buffer-name "*Org Agenda*")
  18629. (defvar org-pre-agenda-window-conf nil)
  18630. (defvar org-agenda-name nil)
  18631. (defun org-prepare-agenda (&optional name)
  18632. (setq org-todo-keywords-for-agenda nil)
  18633. (setq org-done-keywords-for-agenda nil)
  18634. (if org-agenda-multi
  18635. (progn
  18636. (setq buffer-read-only nil)
  18637. (goto-char (point-max))
  18638. (unless (or (bobp) org-agenda-compact-blocks)
  18639. (insert "\n" (make-string (window-width) ?=) "\n"))
  18640. (narrow-to-region (point) (point-max)))
  18641. (org-agenda-reset-markers)
  18642. (org-prepare-agenda-buffers (org-agenda-files))
  18643. (setq org-todo-keywords-for-agenda
  18644. (org-uniquify org-todo-keywords-for-agenda))
  18645. (setq org-done-keywords-for-agenda
  18646. (org-uniquify org-done-keywords-for-agenda))
  18647. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18648. (awin (get-buffer-window abuf)))
  18649. (cond
  18650. ((equal (current-buffer) abuf) nil)
  18651. (awin (select-window awin))
  18652. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18653. ((equal org-agenda-window-setup 'current-window)
  18654. (switch-to-buffer abuf))
  18655. ((equal org-agenda-window-setup 'other-window)
  18656. (org-switch-to-buffer-other-window abuf))
  18657. ((equal org-agenda-window-setup 'other-frame)
  18658. (switch-to-buffer-other-frame abuf))
  18659. ((equal org-agenda-window-setup 'reorganize-frame)
  18660. (delete-other-windows)
  18661. (org-switch-to-buffer-other-window abuf))))
  18662. (setq buffer-read-only nil)
  18663. (erase-buffer)
  18664. (org-agenda-mode)
  18665. (and name (not org-agenda-name)
  18666. (org-set-local 'org-agenda-name name)))
  18667. (setq buffer-read-only nil))
  18668. (defun org-finalize-agenda ()
  18669. "Finishing touch for the agenda buffer, called just before displaying it."
  18670. (unless org-agenda-multi
  18671. (save-excursion
  18672. (let ((inhibit-read-only t))
  18673. (goto-char (point-min))
  18674. (while (org-activate-bracket-links (point-max))
  18675. (add-text-properties (match-beginning 0) (match-end 0)
  18676. '(face org-link)))
  18677. (org-agenda-align-tags)
  18678. (unless org-agenda-with-colors
  18679. (remove-text-properties (point-min) (point-max) '(face nil))))
  18680. (if (and (boundp 'org-overriding-columns-format)
  18681. org-overriding-columns-format)
  18682. (org-set-local 'org-overriding-columns-format
  18683. org-overriding-columns-format))
  18684. (if (and (boundp 'org-agenda-view-columns-initially)
  18685. org-agenda-view-columns-initially)
  18686. (org-agenda-columns))
  18687. (when org-agenda-fontify-priorities
  18688. (org-fontify-priorities))
  18689. (run-hooks 'org-finalize-agenda-hook)
  18690. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18691. )))
  18692. (defun org-fontify-priorities ()
  18693. "Make highest priority lines bold, and lowest italic."
  18694. (interactive)
  18695. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18696. (org-delete-overlay o)))
  18697. (org-overlays-in (point-min) (point-max)))
  18698. (save-excursion
  18699. (let ((inhibit-read-only t)
  18700. b e p ov h l)
  18701. (goto-char (point-min))
  18702. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18703. (setq h (or (get-char-property (point) 'org-highest-priority)
  18704. org-highest-priority)
  18705. l (or (get-char-property (point) 'org-lowest-priority)
  18706. org-lowest-priority)
  18707. p (string-to-char (match-string 1))
  18708. b (match-beginning 0) e (point-at-eol)
  18709. ov (org-make-overlay b e))
  18710. (org-overlay-put
  18711. ov 'face
  18712. (cond ((listp org-agenda-fontify-priorities)
  18713. (cdr (assoc p org-agenda-fontify-priorities)))
  18714. ((equal p l) 'italic)
  18715. ((equal p h) 'bold)))
  18716. (org-overlay-put ov 'org-type 'org-priority)))))
  18717. (defun org-prepare-agenda-buffers (files)
  18718. "Create buffers for all agenda files, protect archived trees and comments."
  18719. (interactive)
  18720. (let ((pa '(:org-archived t))
  18721. (pc '(:org-comment t))
  18722. (pall '(:org-archived t :org-comment t))
  18723. (inhibit-read-only t)
  18724. (rea (concat ":" org-archive-tag ":"))
  18725. bmp file re)
  18726. (save-excursion
  18727. (save-restriction
  18728. (while (setq file (pop files))
  18729. (if (bufferp file)
  18730. (set-buffer file)
  18731. (org-check-agenda-file file)
  18732. (set-buffer (org-get-agenda-file-buffer file)))
  18733. (widen)
  18734. (setq bmp (buffer-modified-p))
  18735. (org-refresh-category-properties)
  18736. (setq org-todo-keywords-for-agenda
  18737. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18738. (setq org-done-keywords-for-agenda
  18739. (append org-done-keywords-for-agenda org-done-keywords))
  18740. (save-excursion
  18741. (remove-text-properties (point-min) (point-max) pall)
  18742. (when org-agenda-skip-archived-trees
  18743. (goto-char (point-min))
  18744. (while (re-search-forward rea nil t)
  18745. (if (org-on-heading-p t)
  18746. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18747. (goto-char (point-min))
  18748. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18749. (while (re-search-forward re nil t)
  18750. (add-text-properties
  18751. (match-beginning 0) (org-end-of-subtree t) pc)))
  18752. (set-buffer-modified-p bmp))))))
  18753. (defvar org-agenda-skip-function nil
  18754. "Function to be called at each match during agenda construction.
  18755. If this function returns nil, the current match should not be skipped.
  18756. Otherwise, the function must return a position from where the search
  18757. should be continued.
  18758. This may also be a Lisp form, it will be evaluated.
  18759. Never set this variable using `setq' or so, because then it will apply
  18760. to all future agenda commands. Instead, bind it with `let' to scope
  18761. it dynamically into the agenda-constructing command. A good way to set
  18762. it is through options in org-agenda-custom-commands.")
  18763. (defun org-agenda-skip ()
  18764. "Throw to `:skip' in places that should be skipped.
  18765. Also moves point to the end of the skipped region, so that search can
  18766. continue from there."
  18767. (let ((p (point-at-bol)) to fp)
  18768. (and org-agenda-skip-archived-trees
  18769. (get-text-property p :org-archived)
  18770. (org-end-of-subtree t)
  18771. (throw :skip t))
  18772. (and (get-text-property p :org-comment)
  18773. (org-end-of-subtree t)
  18774. (throw :skip t))
  18775. (if (equal (char-after p) ?#) (throw :skip t))
  18776. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18777. (consp org-agenda-skip-function))
  18778. (setq to (save-excursion
  18779. (save-match-data
  18780. (if fp
  18781. (funcall org-agenda-skip-function)
  18782. (eval org-agenda-skip-function))))))
  18783. (goto-char to)
  18784. (throw :skip t))))
  18785. (defvar org-agenda-markers nil
  18786. "List of all currently active markers created by `org-agenda'.")
  18787. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18788. "Creation time of the last agenda marker.")
  18789. (defun org-agenda-new-marker (&optional pos)
  18790. "Return a new agenda marker.
  18791. Org-mode keeps a list of these markers and resets them when they are
  18792. no longer in use."
  18793. (let ((m (copy-marker (or pos (point)))))
  18794. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18795. (push m org-agenda-markers)
  18796. m))
  18797. (defun org-agenda-reset-markers ()
  18798. "Reset markers created by `org-agenda'."
  18799. (while org-agenda-markers
  18800. (move-marker (pop org-agenda-markers) nil)))
  18801. (defun org-get-agenda-file-buffer (file)
  18802. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18803. it to the list of buffers which might be released later."
  18804. (let ((buf (org-find-base-buffer-visiting file)))
  18805. (if buf
  18806. buf ; just return it
  18807. ;; Make a new buffer and remember it
  18808. (setq buf (find-file-noselect file))
  18809. (if buf (push buf org-agenda-new-buffers))
  18810. buf)))
  18811. (defun org-release-buffers (blist)
  18812. "Release all buffers in list, asking the user for confirmation when needed.
  18813. When a buffer is unmodified, it is just killed. When modified, it is saved
  18814. \(if the user agrees) and then killed."
  18815. (let (buf file)
  18816. (while (setq buf (pop blist))
  18817. (setq file (buffer-file-name buf))
  18818. (when (and (buffer-modified-p buf)
  18819. file
  18820. (y-or-n-p (format "Save file %s? " file)))
  18821. (with-current-buffer buf (save-buffer)))
  18822. (kill-buffer buf))))
  18823. (defun org-get-category (&optional pos)
  18824. "Get the category applying to position POS."
  18825. (get-text-property (or pos (point)) 'org-category))
  18826. ;;; Agenda timeline
  18827. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18828. (defun org-timeline (&optional include-all)
  18829. "Show a time-sorted view of the entries in the current org file.
  18830. Only entries with a time stamp of today or later will be listed. With
  18831. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18832. under the current date.
  18833. If the buffer contains an active region, only check the region for
  18834. dates."
  18835. (interactive "P")
  18836. (require 'calendar)
  18837. (org-compile-prefix-format 'timeline)
  18838. (org-set-sorting-strategy 'timeline)
  18839. (let* ((dopast t)
  18840. (dotodo include-all)
  18841. (doclosed org-agenda-show-log)
  18842. (entry buffer-file-name)
  18843. (date (calendar-current-date))
  18844. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18845. (end (if (org-region-active-p) (region-end) (point-max)))
  18846. (day-numbers (org-get-all-dates beg end 'no-ranges
  18847. t doclosed ; always include today
  18848. org-timeline-show-empty-dates))
  18849. (org-deadline-warning-days 0)
  18850. (org-agenda-only-exact-dates t)
  18851. (today (time-to-days (current-time)))
  18852. (past t)
  18853. args
  18854. s e rtn d emptyp)
  18855. (setq org-agenda-redo-command
  18856. (list 'progn
  18857. (list 'org-switch-to-buffer-other-window (current-buffer))
  18858. (list 'org-timeline (list 'quote include-all))))
  18859. (if (not dopast)
  18860. ;; Remove past dates from the list of dates.
  18861. (setq day-numbers (delq nil (mapcar (lambda(x)
  18862. (if (>= x today) x nil))
  18863. day-numbers))))
  18864. (org-prepare-agenda (concat "Timeline "
  18865. (file-name-nondirectory buffer-file-name)))
  18866. (if doclosed (push :closed args))
  18867. (push :timestamp args)
  18868. (push :deadline args)
  18869. (push :scheduled args)
  18870. (push :sexp args)
  18871. (if dotodo (push :todo args))
  18872. (while (setq d (pop day-numbers))
  18873. (if (and (listp d) (eq (car d) :omitted))
  18874. (progn
  18875. (setq s (point))
  18876. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18877. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18878. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18879. (if (and (>= d today)
  18880. dopast
  18881. past)
  18882. (progn
  18883. (setq past nil)
  18884. (insert (make-string 79 ?-) "\n")))
  18885. (setq date (calendar-gregorian-from-absolute d))
  18886. (setq s (point))
  18887. (setq rtn (and (not emptyp)
  18888. (apply 'org-agenda-get-day-entries entry
  18889. date args)))
  18890. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18891. (progn
  18892. (insert
  18893. (if (stringp org-agenda-format-date)
  18894. (format-time-string org-agenda-format-date
  18895. (org-time-from-absolute date))
  18896. (funcall org-agenda-format-date date))
  18897. "\n")
  18898. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18899. (put-text-property s (1- (point)) 'org-date-line t)
  18900. (if (equal d today)
  18901. (put-text-property s (1- (point)) 'org-today t))
  18902. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18903. (put-text-property s (1- (point)) 'day d)))))
  18904. (goto-char (point-min))
  18905. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18906. (point-min)))
  18907. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18908. (org-finalize-agenda)
  18909. (setq buffer-read-only t)))
  18910. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18911. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18912. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18913. not every single day in the range. If FORCE-TODAY is non-nil, make
  18914. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18915. inactive time stamps (those in square brackets) are included.
  18916. When EMPTY is non-nil, also include days without any entries."
  18917. (let ((re (concat
  18918. (if pre-re pre-re "")
  18919. (if inactive org-ts-regexp-both org-ts-regexp)))
  18920. dates dates1 date day day1 day2 ts1 ts2)
  18921. (if force-today
  18922. (setq dates (list (time-to-days (current-time)))))
  18923. (save-excursion
  18924. (goto-char beg)
  18925. (while (re-search-forward re end t)
  18926. (setq day (time-to-days (org-time-string-to-time
  18927. (substring (match-string 1) 0 10))))
  18928. (or (memq day dates) (push day dates)))
  18929. (unless no-ranges
  18930. (goto-char beg)
  18931. (while (re-search-forward org-tr-regexp end t)
  18932. (setq ts1 (substring (match-string 1) 0 10)
  18933. ts2 (substring (match-string 2) 0 10)
  18934. day1 (time-to-days (org-time-string-to-time ts1))
  18935. day2 (time-to-days (org-time-string-to-time ts2)))
  18936. (while (< (setq day1 (1+ day1)) day2)
  18937. (or (memq day1 dates) (push day1 dates)))))
  18938. (setq dates (sort dates '<))
  18939. (when empty
  18940. (while (setq day (pop dates))
  18941. (setq day2 (car dates))
  18942. (push day dates1)
  18943. (when (and day2 empty)
  18944. (if (or (eq empty t)
  18945. (and (numberp empty) (<= (- day2 day) empty)))
  18946. (while (< (setq day (1+ day)) day2)
  18947. (push (list day) dates1))
  18948. (push (cons :omitted (- day2 day)) dates1))))
  18949. (setq dates (nreverse dates1)))
  18950. dates)))
  18951. ;;; Agenda Daily/Weekly
  18952. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18953. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18954. (defvar org-agenda-last-arguments nil
  18955. "The arguments of the previous call to org-agenda")
  18956. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18957. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18958. (defvar org-include-all-loc nil) ; local variable
  18959. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18960. ;;;###autoload
  18961. (defun org-agenda-list (&optional include-all start-day ndays)
  18962. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18963. The view will be for the current day or week, but from the overview buffer
  18964. you will be able to go to other days/weeks.
  18965. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18966. all unfinished TODO items will also be shown, before the agenda.
  18967. This feature is considered obsolete, please use the TODO list or a block
  18968. agenda instead.
  18969. With a numeric prefix argument in an interactive call, the agenda will
  18970. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18971. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18972. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18973. given in `org-agenda-start-on-weekday'."
  18974. (interactive "P")
  18975. (if (and (integerp include-all) (> include-all 0))
  18976. (setq ndays include-all include-all nil))
  18977. (setq ndays (or ndays org-agenda-ndays)
  18978. start-day (or start-day org-agenda-start-day))
  18979. (if org-agenda-overriding-arguments
  18980. (setq include-all (car org-agenda-overriding-arguments)
  18981. start-day (nth 1 org-agenda-overriding-arguments)
  18982. ndays (nth 2 org-agenda-overriding-arguments)))
  18983. (if (stringp start-day)
  18984. ;; Convert to an absolute day number
  18985. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18986. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18987. (org-compile-prefix-format 'agenda)
  18988. (org-set-sorting-strategy 'agenda)
  18989. (require 'calendar)
  18990. (let* ((org-agenda-start-on-weekday
  18991. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18992. org-agenda-start-on-weekday nil))
  18993. (thefiles (org-agenda-files))
  18994. (files thefiles)
  18995. (today (time-to-days
  18996. (time-subtract (current-time)
  18997. (list 0 (* 3600 org-extend-today-until) 0))))
  18998. (sd (or start-day today))
  18999. (start (if (or (null org-agenda-start-on-weekday)
  19000. (< org-agenda-ndays 7))
  19001. sd
  19002. (let* ((nt (calendar-day-of-week
  19003. (calendar-gregorian-from-absolute sd)))
  19004. (n1 org-agenda-start-on-weekday)
  19005. (d (- nt n1)))
  19006. (- sd (+ (if (< d 0) 7 0) d)))))
  19007. (day-numbers (list start))
  19008. (day-cnt 0)
  19009. (inhibit-redisplay (not debug-on-error))
  19010. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19011. (setq org-agenda-redo-command
  19012. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19013. ;; Make the list of days
  19014. (setq ndays (or ndays org-agenda-ndays)
  19015. nd ndays)
  19016. (while (> ndays 1)
  19017. (push (1+ (car day-numbers)) day-numbers)
  19018. (setq ndays (1- ndays)))
  19019. (setq day-numbers (nreverse day-numbers))
  19020. (org-prepare-agenda "Day/Week")
  19021. (org-set-local 'org-starting-day (car day-numbers))
  19022. (org-set-local 'org-include-all-loc include-all)
  19023. (org-set-local 'org-agenda-span
  19024. (org-agenda-ndays-to-span nd))
  19025. (when (and (or include-all org-agenda-include-all-todo)
  19026. (member today day-numbers))
  19027. (setq files thefiles
  19028. rtnall nil)
  19029. (while (setq file (pop files))
  19030. (catch 'nextfile
  19031. (org-check-agenda-file file)
  19032. (setq date (calendar-gregorian-from-absolute today)
  19033. rtn (org-agenda-get-day-entries
  19034. file date :todo))
  19035. (setq rtnall (append rtnall rtn))))
  19036. (when rtnall
  19037. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19038. (add-text-properties (point-min) (1- (point))
  19039. (list 'face 'org-agenda-structure))
  19040. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19041. (unless org-agenda-compact-blocks
  19042. (setq s (point))
  19043. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19044. "-agenda:\n")
  19045. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19046. 'org-date-line t)))
  19047. (while (setq d (pop day-numbers))
  19048. (setq date (calendar-gregorian-from-absolute d)
  19049. s (point))
  19050. (if (or (setq todayp (= d today))
  19051. (and (not start-pos) (= d sd)))
  19052. (setq start-pos (point))
  19053. (if (and start-pos (not end-pos))
  19054. (setq end-pos (point))))
  19055. (setq files thefiles
  19056. rtnall nil)
  19057. (while (setq file (pop files))
  19058. (catch 'nextfile
  19059. (org-check-agenda-file file)
  19060. (if org-agenda-show-log
  19061. (setq rtn (org-agenda-get-day-entries
  19062. file date
  19063. :deadline :scheduled :timestamp :sexp :closed))
  19064. (setq rtn (org-agenda-get-day-entries
  19065. file date
  19066. :deadline :scheduled :sexp :timestamp)))
  19067. (setq rtnall (append rtnall rtn))))
  19068. (if org-agenda-include-diary
  19069. (progn
  19070. (require 'diary-lib)
  19071. (setq rtn (org-get-entries-from-diary date))
  19072. (setq rtnall (append rtnall rtn))))
  19073. (if (or rtnall org-agenda-show-all-dates)
  19074. (progn
  19075. (setq day-cnt (1+ day-cnt))
  19076. (insert
  19077. (if (stringp org-agenda-format-date)
  19078. (format-time-string org-agenda-format-date
  19079. (org-time-from-absolute date))
  19080. (funcall org-agenda-format-date date))
  19081. "\n")
  19082. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19083. (put-text-property s (1- (point)) 'org-date-line t)
  19084. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19085. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19086. (if rtnall (insert
  19087. (org-finalize-agenda-entries
  19088. (org-agenda-add-time-grid-maybe
  19089. rtnall nd todayp))
  19090. "\n"))
  19091. (put-text-property s (1- (point)) 'day d)
  19092. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19093. (goto-char (point-min))
  19094. (org-fit-agenda-window)
  19095. (unless (and (pos-visible-in-window-p (point-min))
  19096. (pos-visible-in-window-p (point-max)))
  19097. (goto-char (1- (point-max)))
  19098. (recenter -1)
  19099. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19100. (progn
  19101. (goto-char (or start-pos 1))
  19102. (recenter 1))))
  19103. (goto-char (or start-pos 1))
  19104. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19105. (org-finalize-agenda)
  19106. (setq buffer-read-only t)
  19107. (message "")))
  19108. (defun org-agenda-ndays-to-span (n)
  19109. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19110. ;;; Agenda TODO list
  19111. (defvar org-select-this-todo-keyword nil)
  19112. (defvar org-last-arg nil)
  19113. ;;;###autoload
  19114. (defun org-todo-list (arg)
  19115. "Show all TODO entries from all agenda file in a single list.
  19116. The prefix arg can be used to select a specific TODO keyword and limit
  19117. the list to these. When using \\[universal-argument], you will be prompted
  19118. for a keyword. A numeric prefix directly selects the Nth keyword in
  19119. `org-todo-keywords-1'."
  19120. (interactive "P")
  19121. (require 'calendar)
  19122. (org-compile-prefix-format 'todo)
  19123. (org-set-sorting-strategy 'todo)
  19124. (org-prepare-agenda "TODO")
  19125. (let* ((today (time-to-days (current-time)))
  19126. (date (calendar-gregorian-from-absolute today))
  19127. (kwds org-todo-keywords-for-agenda)
  19128. (completion-ignore-case t)
  19129. (org-select-this-todo-keyword
  19130. (if (stringp arg) arg
  19131. (and arg (integerp arg) (> arg 0)
  19132. (nth (1- arg) kwds))))
  19133. rtn rtnall files file pos)
  19134. (when (equal arg '(4))
  19135. (setq org-select-this-todo-keyword
  19136. (completing-read "Keyword (or KWD1|K2D2|...): "
  19137. (mapcar 'list kwds) nil nil)))
  19138. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19139. (org-set-local 'org-last-arg arg)
  19140. (setq org-agenda-redo-command
  19141. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19142. (setq files (org-agenda-files)
  19143. rtnall nil)
  19144. (while (setq file (pop files))
  19145. (catch 'nextfile
  19146. (org-check-agenda-file file)
  19147. (setq rtn (org-agenda-get-day-entries file date :todo))
  19148. (setq rtnall (append rtnall rtn))))
  19149. (if org-agenda-overriding-header
  19150. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19151. nil 'face 'org-agenda-structure) "\n")
  19152. (insert "Global list of TODO items of type: ")
  19153. (add-text-properties (point-min) (1- (point))
  19154. (list 'face 'org-agenda-structure))
  19155. (setq pos (point))
  19156. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19157. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19158. (setq pos (point))
  19159. (unless org-agenda-multi
  19160. (insert "Available with `N r': (0)ALL")
  19161. (let ((n 0) s)
  19162. (mapc (lambda (x)
  19163. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19164. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19165. (insert "\n "))
  19166. (insert " " s))
  19167. kwds))
  19168. (insert "\n"))
  19169. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19170. (when rtnall
  19171. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19172. (goto-char (point-min))
  19173. (org-fit-agenda-window)
  19174. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19175. (org-finalize-agenda)
  19176. (setq buffer-read-only t)))
  19177. ;;; Agenda tags match
  19178. ;;;###autoload
  19179. (defun org-tags-view (&optional todo-only match)
  19180. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19181. The prefix arg TODO-ONLY limits the search to TODO entries."
  19182. (interactive "P")
  19183. (org-compile-prefix-format 'tags)
  19184. (org-set-sorting-strategy 'tags)
  19185. (let* ((org-tags-match-list-sublevels
  19186. (if todo-only t org-tags-match-list-sublevels))
  19187. (completion-ignore-case t)
  19188. rtn rtnall files file pos matcher
  19189. buffer)
  19190. (setq matcher (org-make-tags-matcher match)
  19191. match (car matcher) matcher (cdr matcher))
  19192. (org-prepare-agenda (concat "TAGS " match))
  19193. (setq org-agenda-redo-command
  19194. (list 'org-tags-view (list 'quote todo-only)
  19195. (list 'if 'current-prefix-arg nil match)))
  19196. (setq files (org-agenda-files)
  19197. rtnall nil)
  19198. (while (setq file (pop files))
  19199. (catch 'nextfile
  19200. (org-check-agenda-file file)
  19201. (setq buffer (if (file-exists-p file)
  19202. (org-get-agenda-file-buffer file)
  19203. (error "No such file %s" file)))
  19204. (if (not buffer)
  19205. ;; If file does not exist, merror message to agenda
  19206. (setq rtn (list
  19207. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19208. rtnall (append rtnall rtn))
  19209. (with-current-buffer buffer
  19210. (unless (org-mode-p)
  19211. (error "Agenda file %s is not in `org-mode'" file))
  19212. (save-excursion
  19213. (save-restriction
  19214. (if org-agenda-restrict
  19215. (narrow-to-region org-agenda-restrict-begin
  19216. org-agenda-restrict-end)
  19217. (widen))
  19218. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19219. (setq rtnall (append rtnall rtn))))))))
  19220. (if org-agenda-overriding-header
  19221. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19222. nil 'face 'org-agenda-structure) "\n")
  19223. (insert "Headlines with TAGS match: ")
  19224. (add-text-properties (point-min) (1- (point))
  19225. (list 'face 'org-agenda-structure))
  19226. (setq pos (point))
  19227. (insert match "\n")
  19228. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19229. (setq pos (point))
  19230. (unless org-agenda-multi
  19231. (insert "Press `C-u r' to search again with new search string\n"))
  19232. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19233. (when rtnall
  19234. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19235. (goto-char (point-min))
  19236. (org-fit-agenda-window)
  19237. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19238. (org-finalize-agenda)
  19239. (setq buffer-read-only t)))
  19240. ;;; Agenda Finding stuck projects
  19241. (defvar org-agenda-skip-regexp nil
  19242. "Regular expression used in skipping subtrees for the agenda.
  19243. This is basically a temporary global variable that can be set and then
  19244. used by user-defined selections using `org-agenda-skip-function'.")
  19245. (defvar org-agenda-overriding-header nil
  19246. "When this is set during todo and tags searches, will replace header.")
  19247. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19248. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19249. If yes, it returns the end position of this tree, causing agenda commands
  19250. to skip this subtree. This is a function that can be put into
  19251. `org-agenda-skip-function' for the duration of a command."
  19252. (let ((end (save-excursion (org-end-of-subtree t)))
  19253. skip)
  19254. (save-excursion
  19255. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19256. (and skip end)))
  19257. (defun org-agenda-skip-entry-if (&rest conditions)
  19258. "Skip entry if any of CONDITIONS is true.
  19259. See `org-agenda-skip-if' for details."
  19260. (org-agenda-skip-if nil conditions))
  19261. (defun org-agenda-skip-subtree-if (&rest conditions)
  19262. "Skip entry if any of CONDITIONS is true.
  19263. See `org-agenda-skip-if' for details."
  19264. (org-agenda-skip-if t conditions))
  19265. (defun org-agenda-skip-if (subtree conditions)
  19266. "Checks current entity for CONDITIONS.
  19267. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19268. the entry, i.e. the text before the next heading is checked.
  19269. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19270. from different tests. Valid conditions are:
  19271. scheduled Check if there is a scheduled cookie
  19272. notscheduled Check if there is no scheduled cookie
  19273. deadline Check if there is a deadline
  19274. notdeadline Check if there is no deadline
  19275. regexp Check if regexp matches
  19276. notregexp Check if regexp does not match.
  19277. The regexp is taken from the conditions list, it must come right after
  19278. the `regexp' or `notregexp' element.
  19279. If any of these conditions is met, this function returns the end point of
  19280. the entity, causing the search to continue from there. This is a function
  19281. that can be put into `org-agenda-skip-function' for the duration of a command."
  19282. (let (beg end m)
  19283. (org-back-to-heading t)
  19284. (setq beg (point)
  19285. end (if subtree
  19286. (progn (org-end-of-subtree t) (point))
  19287. (progn (outline-next-heading) (1- (point)))))
  19288. (goto-char beg)
  19289. (and
  19290. (or
  19291. (and (memq 'scheduled conditions)
  19292. (re-search-forward org-scheduled-time-regexp end t))
  19293. (and (memq 'notscheduled conditions)
  19294. (not (re-search-forward org-scheduled-time-regexp end t)))
  19295. (and (memq 'deadline conditions)
  19296. (re-search-forward org-deadline-time-regexp end t))
  19297. (and (memq 'notdeadline conditions)
  19298. (not (re-search-forward org-deadline-time-regexp end t)))
  19299. (and (setq m (memq 'regexp conditions))
  19300. (stringp (nth 1 m))
  19301. (re-search-forward (nth 1 m) end t))
  19302. (and (setq m (memq 'notregexp conditions))
  19303. (stringp (nth 1 m))
  19304. (not (re-search-forward (nth 1 m) end t))))
  19305. end)))
  19306. ;;;###autoload
  19307. (defun org-agenda-list-stuck-projects (&rest ignore)
  19308. "Create agenda view for projects that are stuck.
  19309. Stuck projects are project that have no next actions. For the definitions
  19310. of what a project is and how to check if it stuck, customize the variable
  19311. `org-stuck-projects'.
  19312. MATCH is being ignored."
  19313. (interactive)
  19314. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19315. ;; FIXME: we could have used org-agenda-skip-if here.
  19316. (org-agenda-overriding-header "List of stuck projects: ")
  19317. (matcher (nth 0 org-stuck-projects))
  19318. (todo (nth 1 org-stuck-projects))
  19319. (todo-wds (if (member "*" todo)
  19320. (progn
  19321. (org-prepare-agenda-buffers (org-agenda-files))
  19322. (org-delete-all
  19323. org-done-keywords-for-agenda
  19324. (copy-sequence org-todo-keywords-for-agenda)))
  19325. todo))
  19326. (todo-re (concat "^\\*+[ \t]+\\("
  19327. (mapconcat 'identity todo-wds "\\|")
  19328. "\\)\\>"))
  19329. (tags (nth 2 org-stuck-projects))
  19330. (tags-re (if (member "*" tags)
  19331. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19332. (concat "^\\*+ .*:\\("
  19333. (mapconcat 'identity tags "\\|")
  19334. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19335. (gen-re (nth 3 org-stuck-projects))
  19336. (re-list
  19337. (delq nil
  19338. (list
  19339. (if todo todo-re)
  19340. (if tags tags-re)
  19341. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19342. gen-re)))))
  19343. (setq org-agenda-skip-regexp
  19344. (if re-list
  19345. (mapconcat 'identity re-list "\\|")
  19346. (error "No information how to identify unstuck projects")))
  19347. (org-tags-view nil matcher)
  19348. (with-current-buffer org-agenda-buffer-name
  19349. (setq org-agenda-redo-command
  19350. '(org-agenda-list-stuck-projects
  19351. (or current-prefix-arg org-last-arg))))))
  19352. ;;; Diary integration
  19353. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19354. (defun org-get-entries-from-diary (date)
  19355. "Get the (Emacs Calendar) diary entries for DATE."
  19356. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19357. (diary-display-hook '(fancy-diary-display))
  19358. (pop-up-frames nil)
  19359. (list-diary-entries-hook
  19360. (cons 'org-diary-default-entry list-diary-entries-hook))
  19361. (diary-file-name-prefix-function nil) ; turn this feature off
  19362. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19363. entries
  19364. (org-disable-agenda-to-diary t))
  19365. (save-excursion
  19366. (save-window-excursion
  19367. (funcall (if (fboundp 'diary-list-entries)
  19368. 'diary-list-entries 'list-diary-entries)
  19369. date 1)))
  19370. (if (not (get-buffer fancy-diary-buffer))
  19371. (setq entries nil)
  19372. (with-current-buffer fancy-diary-buffer
  19373. (setq buffer-read-only nil)
  19374. (if (zerop (buffer-size))
  19375. ;; No entries
  19376. (setq entries nil)
  19377. ;; Omit the date and other unnecessary stuff
  19378. (org-agenda-cleanup-fancy-diary)
  19379. ;; Add prefix to each line and extend the text properties
  19380. (if (zerop (buffer-size))
  19381. (setq entries nil)
  19382. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19383. (set-buffer-modified-p nil)
  19384. (kill-buffer fancy-diary-buffer)))
  19385. (when entries
  19386. (setq entries (org-split-string entries "\n"))
  19387. (setq entries
  19388. (mapcar
  19389. (lambda (x)
  19390. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19391. ;; Extend the text properties to the beginning of the line
  19392. (org-add-props x (text-properties-at (1- (length x)) x)
  19393. 'type "diary" 'date date))
  19394. entries)))))
  19395. (defun org-agenda-cleanup-fancy-diary ()
  19396. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19397. This gets rid of the date, the underline under the date, and
  19398. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19399. date. It also removes lines that contain only whitespace."
  19400. (goto-char (point-min))
  19401. (if (looking-at ".*?:[ \t]*")
  19402. (progn
  19403. (replace-match "")
  19404. (re-search-forward "\n=+$" nil t)
  19405. (replace-match "")
  19406. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19407. (re-search-forward "\n=+$" nil t)
  19408. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19409. (goto-char (point-min))
  19410. (while (re-search-forward "^ +\n" nil t)
  19411. (replace-match ""))
  19412. (goto-char (point-min))
  19413. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19414. (replace-match "")))
  19415. ;; Make sure entries from the diary have the right text properties.
  19416. (eval-after-load "diary-lib"
  19417. '(if (boundp 'diary-modify-entry-list-string-function)
  19418. ;; We can rely on the hook, nothing to do
  19419. nil
  19420. ;; Hook not avaiable, must use advice to make this work
  19421. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19422. "Make the position visible."
  19423. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19424. (stringp string)
  19425. buffer-file-name)
  19426. (setq string (org-modify-diary-entry-string string))))))
  19427. (defun org-modify-diary-entry-string (string)
  19428. "Add text properties to string, allowing org-mode to act on it."
  19429. (org-add-props string nil
  19430. 'mouse-face 'highlight
  19431. 'keymap org-agenda-keymap
  19432. 'help-echo (if buffer-file-name
  19433. (format "mouse-2 or RET jump to diary file %s"
  19434. (abbreviate-file-name buffer-file-name))
  19435. "")
  19436. 'org-agenda-diary-link t
  19437. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19438. (defun org-diary-default-entry ()
  19439. "Add a dummy entry to the diary.
  19440. Needed to avoid empty dates which mess up holiday display."
  19441. ;; Catch the error if dealing with the new add-to-diary-alist
  19442. (when org-disable-agenda-to-diary
  19443. (condition-case nil
  19444. (add-to-diary-list original-date "Org-mode dummy" "")
  19445. (error
  19446. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19447. ;;;###autoload
  19448. (defun org-diary (&rest args)
  19449. "Return diary information from org-files.
  19450. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19451. It accesses org files and extracts information from those files to be
  19452. listed in the diary. The function accepts arguments specifying what
  19453. items should be listed. The following arguments are allowed:
  19454. :timestamp List the headlines of items containing a date stamp or
  19455. date range matching the selected date. Deadlines will
  19456. also be listed, on the expiration day.
  19457. :sexp List entries resulting from diary-like sexps.
  19458. :deadline List any deadlines past due, or due within
  19459. `org-deadline-warning-days'. The listing occurs only
  19460. in the diary for *today*, not at any other date. If
  19461. an entry is marked DONE, it is no longer listed.
  19462. :scheduled List all items which are scheduled for the given date.
  19463. The diary for *today* also contains items which were
  19464. scheduled earlier and are not yet marked DONE.
  19465. :todo List all TODO items from the org-file. This may be a
  19466. long list - so this is not turned on by default.
  19467. Like deadlines, these entries only show up in the
  19468. diary for *today*, not at any other date.
  19469. The call in the diary file should look like this:
  19470. &%%(org-diary) ~/path/to/some/orgfile.org
  19471. Use a separate line for each org file to check. Or, if you omit the file name,
  19472. all files listed in `org-agenda-files' will be checked automatically:
  19473. &%%(org-diary)
  19474. If you don't give any arguments (as in the example above), the default
  19475. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19476. So the example above may also be written as
  19477. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19478. The function expects the lisp variables `entry' and `date' to be provided
  19479. by the caller, because this is how the calendar works. Don't use this
  19480. function from a program - use `org-agenda-get-day-entries' instead."
  19481. (when (> (- (time-to-seconds (current-time))
  19482. org-agenda-last-marker-time)
  19483. 5)
  19484. (org-agenda-reset-markers))
  19485. (org-compile-prefix-format 'agenda)
  19486. (org-set-sorting-strategy 'agenda)
  19487. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19488. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19489. (list entry)
  19490. (org-agenda-files t)))
  19491. file rtn results)
  19492. (org-prepare-agenda-buffers files)
  19493. ;; If this is called during org-agenda, don't return any entries to
  19494. ;; the calendar. Org Agenda will list these entries itself.
  19495. (if org-disable-agenda-to-diary (setq files nil))
  19496. (while (setq file (pop files))
  19497. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19498. (setq results (append results rtn)))
  19499. (if results
  19500. (concat (org-finalize-agenda-entries results) "\n"))))
  19501. ;;; Agenda entry finders
  19502. (defun org-agenda-get-day-entries (file date &rest args)
  19503. "Does the work for `org-diary' and `org-agenda'.
  19504. FILE is the path to a file to be checked for entries. DATE is date like
  19505. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19506. which kind of entries should be extracted. For details about these, see
  19507. the documentation of `org-diary'."
  19508. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19509. (let* ((org-startup-folded nil)
  19510. (org-startup-align-all-tables nil)
  19511. (buffer (if (file-exists-p file)
  19512. (org-get-agenda-file-buffer file)
  19513. (error "No such file %s" file)))
  19514. arg results rtn)
  19515. (if (not buffer)
  19516. ;; If file does not exist, make sure an error message ends up in diary
  19517. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19518. (with-current-buffer buffer
  19519. (unless (org-mode-p)
  19520. (error "Agenda file %s is not in `org-mode'" file))
  19521. (let ((case-fold-search nil))
  19522. (save-excursion
  19523. (save-restriction
  19524. (if org-agenda-restrict
  19525. (narrow-to-region org-agenda-restrict-begin
  19526. org-agenda-restrict-end)
  19527. (widen))
  19528. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19529. (while (setq arg (pop args))
  19530. (cond
  19531. ((and (eq arg :todo)
  19532. (equal date (calendar-current-date)))
  19533. (setq rtn (org-agenda-get-todos))
  19534. (setq results (append results rtn)))
  19535. ((eq arg :timestamp)
  19536. (setq rtn (org-agenda-get-blocks))
  19537. (setq results (append results rtn))
  19538. (setq rtn (org-agenda-get-timestamps))
  19539. (setq results (append results rtn)))
  19540. ((eq arg :sexp)
  19541. (setq rtn (org-agenda-get-sexps))
  19542. (setq results (append results rtn)))
  19543. ((eq arg :scheduled)
  19544. (setq rtn (org-agenda-get-scheduled))
  19545. (setq results (append results rtn)))
  19546. ((eq arg :closed)
  19547. (setq rtn (org-agenda-get-closed))
  19548. (setq results (append results rtn)))
  19549. ((eq arg :deadline)
  19550. (setq rtn (org-agenda-get-deadlines))
  19551. (setq results (append results rtn))))))))
  19552. results))))
  19553. (defun org-entry-is-todo-p ()
  19554. (member (org-get-todo-state) org-not-done-keywords))
  19555. (defun org-entry-is-done-p ()
  19556. (member (org-get-todo-state) org-done-keywords))
  19557. (defun org-get-todo-state ()
  19558. (save-excursion
  19559. (org-back-to-heading t)
  19560. (and (looking-at org-todo-line-regexp)
  19561. (match-end 2)
  19562. (match-string 2))))
  19563. (defun org-at-date-range-p (&optional inactive-ok)
  19564. "Is the cursor inside a date range?"
  19565. (interactive)
  19566. (save-excursion
  19567. (catch 'exit
  19568. (let ((pos (point)))
  19569. (skip-chars-backward "^[<\r\n")
  19570. (skip-chars-backward "<[")
  19571. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19572. (>= (match-end 0) pos)
  19573. (throw 'exit t))
  19574. (skip-chars-backward "^<[\r\n")
  19575. (skip-chars-backward "<[")
  19576. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19577. (>= (match-end 0) pos)
  19578. (throw 'exit t)))
  19579. nil)))
  19580. (defun org-agenda-get-todos ()
  19581. "Return the TODO information for agenda display."
  19582. (let* ((props (list 'face nil
  19583. 'done-face 'org-done
  19584. 'org-not-done-regexp org-not-done-regexp
  19585. 'org-todo-regexp org-todo-regexp
  19586. 'mouse-face 'highlight
  19587. 'keymap org-agenda-keymap
  19588. 'help-echo
  19589. (format "mouse-2 or RET jump to org file %s"
  19590. (abbreviate-file-name buffer-file-name))))
  19591. ;; FIXME: get rid of the \n at some point but watch out
  19592. (regexp (concat "^\\*+[ \t]+\\("
  19593. (if org-select-this-todo-keyword
  19594. (if (equal org-select-this-todo-keyword "*")
  19595. org-todo-regexp
  19596. (concat "\\<\\("
  19597. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19598. "\\)\\>"))
  19599. org-not-done-regexp)
  19600. "[^\n\r]*\\)"))
  19601. marker priority category tags
  19602. ee txt beg end)
  19603. (goto-char (point-min))
  19604. (while (re-search-forward regexp nil t)
  19605. (catch :skip
  19606. (save-match-data
  19607. (beginning-of-line)
  19608. (setq beg (point) end (progn (outline-next-heading) (point)))
  19609. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19610. (re-search-forward org-ts-regexp end t))
  19611. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19612. (re-search-forward org-scheduled-time-regexp end t))
  19613. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19614. (re-search-forward org-deadline-time-regexp end t)
  19615. (org-deadline-close (match-string 1))))
  19616. (goto-char (1+ beg))
  19617. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19618. (throw :skip nil)))
  19619. (goto-char beg)
  19620. (org-agenda-skip)
  19621. (goto-char (match-beginning 1))
  19622. (setq marker (org-agenda-new-marker (match-beginning 0))
  19623. category (org-get-category)
  19624. tags (org-get-tags-at (point))
  19625. txt (org-format-agenda-item "" (match-string 1) category tags)
  19626. priority (1+ (org-get-priority txt)))
  19627. (org-add-props txt props
  19628. 'org-marker marker 'org-hd-marker marker
  19629. 'priority priority 'org-category category
  19630. 'type "todo")
  19631. (push txt ee)
  19632. (if org-agenda-todo-list-sublevels
  19633. (goto-char (match-end 1))
  19634. (org-end-of-subtree 'invisible))))
  19635. (nreverse ee)))
  19636. (defconst org-agenda-no-heading-message
  19637. "No heading for this item in buffer or region.")
  19638. (defun org-agenda-get-timestamps ()
  19639. "Return the date stamp information for agenda display."
  19640. (let* ((props (list 'face nil
  19641. 'org-not-done-regexp org-not-done-regexp
  19642. 'org-todo-regexp org-todo-regexp
  19643. 'mouse-face 'highlight
  19644. 'keymap org-agenda-keymap
  19645. 'help-echo
  19646. (format "mouse-2 or RET jump to org file %s"
  19647. (abbreviate-file-name buffer-file-name))))
  19648. (d1 (calendar-absolute-from-gregorian date))
  19649. (remove-re
  19650. (concat
  19651. (regexp-quote
  19652. (format-time-string
  19653. "<%Y-%m-%d"
  19654. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19655. ".*?>"))
  19656. (regexp
  19657. (concat
  19658. (regexp-quote
  19659. (substring
  19660. (format-time-string
  19661. (car org-time-stamp-formats)
  19662. (apply 'encode-time ; DATE bound by calendar
  19663. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19664. 0 11))
  19665. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19666. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19667. marker hdmarker deadlinep scheduledp donep tmp priority category
  19668. ee txt timestr tags b0 b3 e3 head)
  19669. (goto-char (point-min))
  19670. (while (re-search-forward regexp nil t)
  19671. (setq b0 (match-beginning 0)
  19672. b3 (match-beginning 3) e3 (match-end 3))
  19673. (catch :skip
  19674. (and (org-at-date-range-p) (throw :skip nil))
  19675. (org-agenda-skip)
  19676. (if (and (match-end 1)
  19677. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19678. (throw :skip nil))
  19679. (if (and e3
  19680. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19681. (throw :skip nil))
  19682. (setq marker (org-agenda-new-marker b0)
  19683. category (org-get-category b0)
  19684. tmp (buffer-substring (max (point-min)
  19685. (- b0 org-ds-keyword-length))
  19686. b0)
  19687. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19688. deadlinep (string-match org-deadline-regexp tmp)
  19689. scheduledp (string-match org-scheduled-regexp tmp)
  19690. donep (org-entry-is-done-p))
  19691. (if (or scheduledp deadlinep) (throw :skip t))
  19692. (if (string-match ">" timestr)
  19693. ;; substring should only run to end of time stamp
  19694. (setq timestr (substring timestr 0 (match-end 0))))
  19695. (save-excursion
  19696. (if (re-search-backward "^\\*+ " nil t)
  19697. (progn
  19698. (goto-char (match-beginning 0))
  19699. (setq hdmarker (org-agenda-new-marker)
  19700. tags (org-get-tags-at))
  19701. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19702. (setq head (match-string 1))
  19703. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19704. (setq txt (org-format-agenda-item
  19705. nil head category tags timestr nil
  19706. remove-re)))
  19707. (setq txt org-agenda-no-heading-message))
  19708. (setq priority (org-get-priority txt))
  19709. (org-add-props txt props
  19710. 'org-marker marker 'org-hd-marker hdmarker)
  19711. (org-add-props txt nil 'priority priority
  19712. 'org-category category 'date date
  19713. 'type "timestamp")
  19714. (push txt ee))
  19715. (outline-next-heading)))
  19716. (nreverse ee)))
  19717. (defun org-agenda-get-sexps ()
  19718. "Return the sexp information for agenda display."
  19719. (require 'diary-lib)
  19720. (let* ((props (list 'face nil
  19721. 'mouse-face 'highlight
  19722. 'keymap org-agenda-keymap
  19723. 'help-echo
  19724. (format "mouse-2 or RET jump to org file %s"
  19725. (abbreviate-file-name buffer-file-name))))
  19726. (regexp "^&?%%(")
  19727. marker category ee txt tags entry result beg b sexp sexp-entry)
  19728. (goto-char (point-min))
  19729. (while (re-search-forward regexp nil t)
  19730. (catch :skip
  19731. (org-agenda-skip)
  19732. (setq beg (match-beginning 0))
  19733. (goto-char (1- (match-end 0)))
  19734. (setq b (point))
  19735. (forward-sexp 1)
  19736. (setq sexp (buffer-substring b (point)))
  19737. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19738. (org-trim (match-string 1))
  19739. ""))
  19740. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19741. (when result
  19742. (setq marker (org-agenda-new-marker beg)
  19743. category (org-get-category beg))
  19744. (if (string-match "\\S-" result)
  19745. (setq txt result)
  19746. (setq txt "SEXP entry returned empty string"))
  19747. (setq txt (org-format-agenda-item
  19748. "" txt category tags 'time))
  19749. (org-add-props txt props 'org-marker marker)
  19750. (org-add-props txt nil
  19751. 'org-category category 'date date
  19752. 'type "sexp")
  19753. (push txt ee))))
  19754. (nreverse ee)))
  19755. (defun org-agenda-get-closed ()
  19756. "Return the logged TODO entries for agenda display."
  19757. (let* ((props (list 'mouse-face 'highlight
  19758. 'org-not-done-regexp org-not-done-regexp
  19759. 'org-todo-regexp org-todo-regexp
  19760. 'keymap org-agenda-keymap
  19761. 'help-echo
  19762. (format "mouse-2 or RET jump to org file %s"
  19763. (abbreviate-file-name buffer-file-name))))
  19764. (regexp (concat
  19765. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19766. (regexp-quote
  19767. (substring
  19768. (format-time-string
  19769. (car org-time-stamp-formats)
  19770. (apply 'encode-time ; DATE bound by calendar
  19771. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19772. 1 11))))
  19773. marker hdmarker priority category tags closedp
  19774. ee txt timestr)
  19775. (goto-char (point-min))
  19776. (while (re-search-forward regexp nil t)
  19777. (catch :skip
  19778. (org-agenda-skip)
  19779. (setq marker (org-agenda-new-marker (match-beginning 0))
  19780. closedp (equal (match-string 1) org-closed-string)
  19781. category (org-get-category (match-beginning 0))
  19782. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19783. ;; donep (org-entry-is-done-p)
  19784. )
  19785. (if (string-match "\\]" timestr)
  19786. ;; substring should only run to end of time stamp
  19787. (setq timestr (substring timestr 0 (match-end 0))))
  19788. (save-excursion
  19789. (if (re-search-backward "^\\*+ " nil t)
  19790. (progn
  19791. (goto-char (match-beginning 0))
  19792. (setq hdmarker (org-agenda-new-marker)
  19793. tags (org-get-tags-at))
  19794. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19795. (setq txt (org-format-agenda-item
  19796. (if closedp "Closed: " "Clocked: ")
  19797. (match-string 1) category tags timestr)))
  19798. (setq txt org-agenda-no-heading-message))
  19799. (setq priority 100000)
  19800. (org-add-props txt props
  19801. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19802. 'priority priority 'org-category category
  19803. 'type "closed" 'date date
  19804. 'undone-face 'org-warning 'done-face 'org-done)
  19805. (push txt ee))
  19806. (goto-char (point-at-eol))))
  19807. (nreverse ee)))
  19808. (defun org-agenda-get-deadlines ()
  19809. "Return the deadline information for agenda display."
  19810. (let* ((props (list 'mouse-face 'highlight
  19811. 'org-not-done-regexp org-not-done-regexp
  19812. 'org-todo-regexp org-todo-regexp
  19813. 'keymap org-agenda-keymap
  19814. 'help-echo
  19815. (format "mouse-2 or RET jump to org file %s"
  19816. (abbreviate-file-name buffer-file-name))))
  19817. (regexp org-deadline-time-regexp)
  19818. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19819. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19820. d2 diff dfrac wdays pos pos1 category tags
  19821. ee txt head face s upcomingp donep timestr)
  19822. (goto-char (point-min))
  19823. (while (re-search-forward regexp nil t)
  19824. (catch :skip
  19825. (org-agenda-skip)
  19826. (setq s (match-string 1)
  19827. pos (1- (match-beginning 1))
  19828. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19829. diff (- d2 d1)
  19830. wdays (org-get-wdays s)
  19831. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19832. upcomingp (and todayp (> diff 0)))
  19833. ;; When to show a deadline in the calendar:
  19834. ;; If the expiration is within wdays warning time.
  19835. ;; Past-due deadlines are only shown on the current date
  19836. (if (or (and (<= diff wdays)
  19837. (and todayp (not org-agenda-only-exact-dates)))
  19838. (= diff 0))
  19839. (save-excursion
  19840. (setq category (org-get-category))
  19841. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19842. (progn
  19843. (goto-char (match-end 0))
  19844. (setq pos1 (match-beginning 0))
  19845. (setq tags (org-get-tags-at pos1))
  19846. (setq head (buffer-substring-no-properties
  19847. (point)
  19848. (progn (skip-chars-forward "^\r\n")
  19849. (point))))
  19850. (setq donep (string-match org-looking-at-done-regexp head))
  19851. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19852. (setq timestr
  19853. (concat (substring s (match-beginning 1)) " "))
  19854. (setq timestr 'time))
  19855. (if (and donep
  19856. (or org-agenda-skip-deadline-if-done
  19857. (not (= diff 0))))
  19858. (setq txt nil)
  19859. (setq txt (org-format-agenda-item
  19860. (if (= diff 0)
  19861. (car org-agenda-deadline-leaders)
  19862. (format (nth 1 org-agenda-deadline-leaders)
  19863. diff))
  19864. head category tags timestr))))
  19865. (setq txt org-agenda-no-heading-message))
  19866. (when txt
  19867. (setq face (org-agenda-deadline-face dfrac))
  19868. (org-add-props txt props
  19869. 'org-marker (org-agenda-new-marker pos)
  19870. 'org-hd-marker (org-agenda-new-marker pos1)
  19871. 'priority (+ (- diff)
  19872. (org-get-priority txt))
  19873. 'org-category category
  19874. 'type (if upcomingp "upcoming-deadline" "deadline")
  19875. 'date (if upcomingp date d2)
  19876. 'face (if donep 'org-done face)
  19877. 'undone-face face 'done-face 'org-done)
  19878. (push txt ee))))))
  19879. (nreverse ee)))
  19880. (defun org-agenda-deadline-face (fraction)
  19881. "Return the face to displaying a deadline item.
  19882. FRACTION is what fraction of the head-warning time has passed."
  19883. (let ((faces org-agenda-deadline-faces) f)
  19884. (catch 'exit
  19885. (while (setq f (pop faces))
  19886. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19887. (defun org-agenda-get-scheduled ()
  19888. "Return the scheduled information for agenda display."
  19889. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19890. 'org-todo-regexp org-todo-regexp
  19891. 'done-face 'org-done
  19892. 'mouse-face 'highlight
  19893. 'keymap org-agenda-keymap
  19894. 'help-echo
  19895. (format "mouse-2 or RET jump to org file %s"
  19896. (abbreviate-file-name buffer-file-name))))
  19897. (regexp org-scheduled-time-regexp)
  19898. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19899. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19900. d2 diff pos pos1 category tags
  19901. ee txt head pastschedp donep face timestr s)
  19902. (goto-char (point-min))
  19903. (while (re-search-forward regexp nil t)
  19904. (catch :skip
  19905. (org-agenda-skip)
  19906. (setq s (match-string 1)
  19907. pos (1- (match-beginning 1))
  19908. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19909. ;;; is this right?
  19910. ;;; do we need to do this for deadleine too????
  19911. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19912. diff (- d2 d1))
  19913. (setq pastschedp (and todayp (< diff 0)))
  19914. ;; When to show a scheduled item in the calendar:
  19915. ;; If it is on or past the date.
  19916. (if (or (and (< diff 0)
  19917. (and todayp (not org-agenda-only-exact-dates)))
  19918. (= diff 0))
  19919. (save-excursion
  19920. (setq category (org-get-category))
  19921. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19922. (progn
  19923. (goto-char (match-end 0))
  19924. (setq pos1 (match-beginning 0))
  19925. (setq tags (org-get-tags-at))
  19926. (setq head (buffer-substring-no-properties
  19927. (point)
  19928. (progn (skip-chars-forward "^\r\n") (point))))
  19929. (setq donep (string-match org-looking-at-done-regexp head))
  19930. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19931. (setq timestr
  19932. (concat (substring s (match-beginning 1)) " "))
  19933. (setq timestr 'time))
  19934. (if (and donep
  19935. (or org-agenda-skip-scheduled-if-done
  19936. (not (= diff 0))))
  19937. (setq txt nil)
  19938. (setq txt (org-format-agenda-item
  19939. (if (= diff 0)
  19940. (car org-agenda-scheduled-leaders)
  19941. (format (nth 1 org-agenda-scheduled-leaders)
  19942. (- 1 diff)))
  19943. head category tags timestr))))
  19944. (setq txt org-agenda-no-heading-message))
  19945. (when txt
  19946. (setq face (if pastschedp
  19947. 'org-scheduled-previously
  19948. 'org-scheduled-today))
  19949. (org-add-props txt props
  19950. 'undone-face face
  19951. 'face (if donep 'org-done face)
  19952. 'org-marker (org-agenda-new-marker pos)
  19953. 'org-hd-marker (org-agenda-new-marker pos1)
  19954. 'type (if pastschedp "past-scheduled" "scheduled")
  19955. 'date (if pastschedp d2 date)
  19956. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19957. 'org-category category)
  19958. (push txt ee))))))
  19959. (nreverse ee)))
  19960. (defun org-agenda-get-blocks ()
  19961. "Return the date-range information for agenda display."
  19962. (let* ((props (list 'face nil
  19963. 'org-not-done-regexp org-not-done-regexp
  19964. 'org-todo-regexp org-todo-regexp
  19965. 'mouse-face 'highlight
  19966. 'keymap org-agenda-keymap
  19967. 'help-echo
  19968. (format "mouse-2 or RET jump to org file %s"
  19969. (abbreviate-file-name buffer-file-name))))
  19970. (regexp org-tr-regexp)
  19971. (d0 (calendar-absolute-from-gregorian date))
  19972. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19973. donep head)
  19974. (goto-char (point-min))
  19975. (while (re-search-forward regexp nil t)
  19976. (catch :skip
  19977. (org-agenda-skip)
  19978. (setq pos (point))
  19979. (setq timestr (match-string 0)
  19980. s1 (match-string 1)
  19981. s2 (match-string 2)
  19982. d1 (time-to-days (org-time-string-to-time s1))
  19983. d2 (time-to-days (org-time-string-to-time s2)))
  19984. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19985. ;; Only allow days between the limits, because the normal
  19986. ;; date stamps will catch the limits.
  19987. (save-excursion
  19988. (setq marker (org-agenda-new-marker (point)))
  19989. (setq category (org-get-category))
  19990. (if (re-search-backward "^\\*+ " nil t)
  19991. (progn
  19992. (goto-char (match-beginning 0))
  19993. (setq hdmarker (org-agenda-new-marker (point)))
  19994. (setq tags (org-get-tags-at))
  19995. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19996. (setq head (match-string 1))
  19997. (and org-agenda-skip-timestamp-if-done
  19998. (org-entry-is-done-p)
  19999. (throw :skip t))
  20000. (setq txt (org-format-agenda-item
  20001. (format (if (= d1 d2) "" "(%d/%d): ")
  20002. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20003. head category tags
  20004. (if (= d0 d1) timestr))))
  20005. (setq txt org-agenda-no-heading-message))
  20006. (org-add-props txt props
  20007. 'org-marker marker 'org-hd-marker hdmarker
  20008. 'type "block" 'date date
  20009. 'priority (org-get-priority txt) 'org-category category)
  20010. (push txt ee)))
  20011. (goto-char pos)))
  20012. ;; Sort the entries by expiration date.
  20013. (nreverse ee)))
  20014. ;;; Agenda presentation and sorting
  20015. (defconst org-plain-time-of-day-regexp
  20016. (concat
  20017. "\\(\\<[012]?[0-9]"
  20018. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20019. "\\(--?"
  20020. "\\(\\<[012]?[0-9]"
  20021. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20022. "\\)?")
  20023. "Regular expression to match a plain time or time range.
  20024. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20025. groups carry important information:
  20026. 0 the full match
  20027. 1 the first time, range or not
  20028. 8 the second time, if it is a range.")
  20029. (defconst org-plain-time-extension-regexp
  20030. (concat
  20031. "\\(\\<[012]?[0-9]"
  20032. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20033. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20034. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20035. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20036. groups carry important information:
  20037. 0 the full match
  20038. 7 hours of duration
  20039. 9 minutes of duration")
  20040. (defconst org-stamp-time-of-day-regexp
  20041. (concat
  20042. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20043. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20044. "\\(--?"
  20045. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20046. "Regular expression to match a timestamp time or time range.
  20047. After a match, the following groups carry important information:
  20048. 0 the full match
  20049. 1 date plus weekday, for backreferencing to make sure both times on same day
  20050. 2 the first time, range or not
  20051. 4 the second time, if it is a range.")
  20052. (defvar org-prefix-has-time nil
  20053. "A flag, set by `org-compile-prefix-format'.
  20054. The flag is set if the currently compiled format contains a `%t'.")
  20055. (defvar org-prefix-has-tag nil
  20056. "A flag, set by `org-compile-prefix-format'.
  20057. The flag is set if the currently compiled format contains a `%T'.")
  20058. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20059. noprefix remove-re)
  20060. "Format TXT to be inserted into the agenda buffer.
  20061. In particular, it adds the prefix and corresponding text properties. EXTRA
  20062. must be a string and replaces the `%s' specifier in the prefix format.
  20063. CATEGORY (string, symbol or nil) may be used to overrule the default
  20064. category taken from local variable or file name. It will replace the `%c'
  20065. specifier in the format. DOTIME, when non-nil, indicates that a
  20066. time-of-day should be extracted from TXT for sorting of this entry, and for
  20067. the `%t' specifier in the format. When DOTIME is a string, this string is
  20068. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20069. only the correctly processes TXT should be returned - this is used by
  20070. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20071. Any match of REMOVE-RE will be removed from TXT."
  20072. (save-match-data
  20073. ;; Diary entries sometimes have extra whitespace at the beginning
  20074. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20075. (let* ((category (or category
  20076. org-category
  20077. (if buffer-file-name
  20078. (file-name-sans-extension
  20079. (file-name-nondirectory buffer-file-name))
  20080. "")))
  20081. (tag (if tags (nth (1- (length tags)) tags) ""))
  20082. time ; time and tag are needed for the eval of the prefix format
  20083. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20084. (time-of-day (and dotime (org-get-time-of-day ts)))
  20085. stamp plain s0 s1 s2 rtn srp)
  20086. (when (and dotime time-of-day org-prefix-has-time)
  20087. ;; Extract starting and ending time and move them to prefix
  20088. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20089. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20090. (setq s0 (match-string 0 ts)
  20091. srp (and stamp (match-end 3))
  20092. s1 (match-string (if plain 1 2) ts)
  20093. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20094. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20095. ;; them, we might want to remove them there to avoid duplication.
  20096. ;; The user can turn this off with a variable.
  20097. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20098. (string-match (concat (regexp-quote s0) " *") txt)
  20099. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20100. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20101. (= (match-beginning 0) 0)
  20102. t))
  20103. (setq txt (replace-match "" nil nil txt))))
  20104. ;; Normalize the time(s) to 24 hour
  20105. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20106. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20107. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20108. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20109. (let ((m (+ (string-to-number (match-string 2 s1))
  20110. (* 60 (string-to-number (match-string 1 s1)))
  20111. org-agenda-default-appointment-duration))
  20112. h)
  20113. (setq h (/ m 60) m (- m (* h 60)))
  20114. (setq s2 (format "%02d:%02d" h m))))
  20115. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20116. txt)
  20117. ;; Tags are in the string
  20118. (if (or (eq org-agenda-remove-tags t)
  20119. (and org-agenda-remove-tags
  20120. org-prefix-has-tag))
  20121. (setq txt (replace-match "" t t txt))
  20122. (setq txt (replace-match
  20123. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20124. (match-string 2 txt))
  20125. t t txt))))
  20126. (when remove-re
  20127. (while (string-match remove-re txt)
  20128. (setq txt (replace-match "" t t txt))))
  20129. ;; Create the final string
  20130. (if noprefix
  20131. (setq rtn txt)
  20132. ;; Prepare the variables needed in the eval of the compiled format
  20133. (setq time (cond (s2 (concat s1 "-" s2))
  20134. (s1 (concat s1 "......"))
  20135. (t ""))
  20136. extra (or extra "")
  20137. category (if (symbolp category) (symbol-name category) category))
  20138. ;; Evaluate the compiled format
  20139. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20140. ;; And finally add the text properties
  20141. (org-add-props rtn nil
  20142. 'org-category (downcase category) 'tags tags
  20143. 'org-highest-priority org-highest-priority
  20144. 'org-lowest-priority org-lowest-priority
  20145. 'prefix-length (- (length rtn) (length txt))
  20146. 'time-of-day time-of-day
  20147. 'txt txt
  20148. 'time time
  20149. 'extra extra
  20150. 'dotime dotime))))
  20151. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20152. (defvar org-agenda-sorting-strategy-selected nil)
  20153. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20154. (catch 'exit
  20155. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20156. ((and todayp (member 'today (car org-agenda-time-grid))))
  20157. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20158. ((member 'weekly (car org-agenda-time-grid)))
  20159. (t (throw 'exit list)))
  20160. (let* ((have (delq nil (mapcar
  20161. (lambda (x) (get-text-property 1 'time-of-day x))
  20162. list)))
  20163. (string (nth 1 org-agenda-time-grid))
  20164. (gridtimes (nth 2 org-agenda-time-grid))
  20165. (req (car org-agenda-time-grid))
  20166. (remove (member 'remove-match req))
  20167. new time)
  20168. (if (and (member 'require-timed req) (not have))
  20169. ;; don't show empty grid
  20170. (throw 'exit list))
  20171. (while (setq time (pop gridtimes))
  20172. (unless (and remove (member time have))
  20173. (setq time (int-to-string time))
  20174. (push (org-format-agenda-item
  20175. nil string "" nil
  20176. (concat (substring time 0 -2) ":" (substring time -2)))
  20177. new)
  20178. (put-text-property
  20179. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20180. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20181. (append new list)
  20182. (append list new)))))
  20183. (defun org-compile-prefix-format (key)
  20184. "Compile the prefix format into a Lisp form that can be evaluated.
  20185. The resulting form is returned and stored in the variable
  20186. `org-prefix-format-compiled'."
  20187. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20188. (let ((s (cond
  20189. ((stringp org-agenda-prefix-format)
  20190. org-agenda-prefix-format)
  20191. ((assq key org-agenda-prefix-format)
  20192. (cdr (assq key org-agenda-prefix-format)))
  20193. (t " %-12:c%?-12t% s")))
  20194. (start 0)
  20195. varform vars var e c f opt)
  20196. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20197. s start)
  20198. (setq var (cdr (assoc (match-string 4 s)
  20199. '(("c" . category) ("t" . time) ("s" . extra)
  20200. ("T" . tag))))
  20201. c (or (match-string 3 s) "")
  20202. opt (match-beginning 1)
  20203. start (1+ (match-beginning 0)))
  20204. (if (equal var 'time) (setq org-prefix-has-time t))
  20205. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20206. (setq f (concat "%" (match-string 2 s) "s"))
  20207. (if opt
  20208. (setq varform
  20209. `(if (equal "" ,var)
  20210. ""
  20211. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20212. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20213. (setq s (replace-match "%s" t nil s))
  20214. (push varform vars))
  20215. (setq vars (nreverse vars))
  20216. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20217. (defun org-set-sorting-strategy (key)
  20218. (if (symbolp (car org-agenda-sorting-strategy))
  20219. ;; the old format
  20220. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20221. (setq org-agenda-sorting-strategy-selected
  20222. (or (cdr (assq key org-agenda-sorting-strategy))
  20223. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20224. '(time-up category-keep priority-down)))))
  20225. (defun org-get-time-of-day (s &optional string mod24)
  20226. "Check string S for a time of day.
  20227. If found, return it as a military time number between 0 and 2400.
  20228. If not found, return nil.
  20229. The optional STRING argument forces conversion into a 5 character wide string
  20230. HH:MM."
  20231. (save-match-data
  20232. (when
  20233. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20234. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20235. (let* ((h (string-to-number (match-string 1 s)))
  20236. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20237. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20238. (am-p (equal ampm "am"))
  20239. (h1 (cond ((not ampm) h)
  20240. ((= h 12) (if am-p 0 12))
  20241. (t (+ h (if am-p 0 12)))))
  20242. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20243. (mod h1 24) h1))
  20244. (t0 (+ (* 100 h2) m))
  20245. (t1 (concat (if (>= h1 24) "+" " ")
  20246. (if (< t0 100) "0" "")
  20247. (if (< t0 10) "0" "")
  20248. (int-to-string t0))))
  20249. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20250. (defun org-finalize-agenda-entries (list &optional nosort)
  20251. "Sort and concatenate the agenda items."
  20252. (setq list (mapcar 'org-agenda-highlight-todo list))
  20253. (if nosort
  20254. list
  20255. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20256. (defun org-agenda-highlight-todo (x)
  20257. (let (re pl)
  20258. (if (eq x 'line)
  20259. (save-excursion
  20260. (beginning-of-line 1)
  20261. (setq re (get-text-property (point) 'org-todo-regexp))
  20262. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20263. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20264. (add-text-properties (match-beginning 0) (match-end 0)
  20265. (list 'face (org-get-todo-face 0)))
  20266. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20267. (delete-region (match-beginning 1) (1- (match-end 0)))
  20268. (goto-char (match-beginning 1))
  20269. (insert (format org-agenda-todo-keyword-format s)))))
  20270. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20271. pl (get-text-property 0 'prefix-length x))
  20272. ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  20273. ; (add-text-properties
  20274. ; (or (match-end 1) (match-end 0)) (match-end 0)
  20275. ; (list 'face (org-get-todo-face (match-string 2 x)))
  20276. ; x))
  20277. (when (and re
  20278. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20279. x (or pl 0)) pl))
  20280. (add-text-properties
  20281. (or (match-end 1) (match-end 0)) (match-end 0)
  20282. (list 'face (org-get-todo-face (match-string 2 x)))
  20283. x)
  20284. (setq x (concat (substring x 0 (match-end 1))
  20285. (format org-agenda-todo-keyword-format
  20286. (match-string 2 x))
  20287. " "
  20288. (substring x (match-end 3)))))
  20289. x)))
  20290. (defsubst org-cmp-priority (a b)
  20291. "Compare the priorities of string A and B."
  20292. (let ((pa (or (get-text-property 1 'priority a) 0))
  20293. (pb (or (get-text-property 1 'priority b) 0)))
  20294. (cond ((> pa pb) +1)
  20295. ((< pa pb) -1)
  20296. (t nil))))
  20297. (defsubst org-cmp-category (a b)
  20298. "Compare the string values of categories of strings A and B."
  20299. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20300. (cb (or (get-text-property 1 'org-category b) "")))
  20301. (cond ((string-lessp ca cb) -1)
  20302. ((string-lessp cb ca) +1)
  20303. (t nil))))
  20304. (defsubst org-cmp-tag (a b)
  20305. "Compare the string values of categories of strings A and B."
  20306. (let ((ta (car (last (get-text-property 1 'tags a))))
  20307. (tb (car (last (get-text-property 1 'tags b)))))
  20308. (cond ((not ta) +1)
  20309. ((not tb) -1)
  20310. ((string-lessp ta tb) -1)
  20311. ((string-lessp tb ta) +1)
  20312. (t nil))))
  20313. (defsubst org-cmp-time (a b)
  20314. "Compare the time-of-day values of strings A and B."
  20315. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20316. (ta (or (get-text-property 1 'time-of-day a) def))
  20317. (tb (or (get-text-property 1 'time-of-day b) def)))
  20318. (cond ((< ta tb) -1)
  20319. ((< tb ta) +1)
  20320. (t nil))))
  20321. (defun org-entries-lessp (a b)
  20322. "Predicate for sorting agenda entries."
  20323. ;; The following variables will be used when the form is evaluated.
  20324. ;; So even though the compiler complains, keep them.
  20325. (let* ((time-up (org-cmp-time a b))
  20326. (time-down (if time-up (- time-up) nil))
  20327. (priority-up (org-cmp-priority a b))
  20328. (priority-down (if priority-up (- priority-up) nil))
  20329. (category-up (org-cmp-category a b))
  20330. (category-down (if category-up (- category-up) nil))
  20331. (category-keep (if category-up +1 nil))
  20332. (tag-up (org-cmp-tag a b))
  20333. (tag-down (if tag-up (- tag-up) nil)))
  20334. (cdr (assoc
  20335. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20336. '((-1 . t) (1 . nil) (nil . nil))))))
  20337. ;;; Agenda restriction lock
  20338. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20339. "Overlay to mark the headline to which arenda commands are restricted.")
  20340. (org-overlay-put org-agenda-restriction-lock-overlay
  20341. 'face 'org-agenda-restriction-lock)
  20342. (org-overlay-put org-agenda-restriction-lock-overlay
  20343. 'help-echo "Agendas are currently limited to this subtree.")
  20344. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20345. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20346. "Overlay marking the agenda restriction line in speedbar.")
  20347. (org-overlay-put org-speedbar-restriction-lock-overlay
  20348. 'face 'org-agenda-restriction-lock)
  20349. (org-overlay-put org-speedbar-restriction-lock-overlay
  20350. 'help-echo "Agendas are currently limited to this item.")
  20351. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20352. (defun org-agenda-set-restriction-lock (&optional type)
  20353. "Set restriction lock for agenda, to current subtree or file.
  20354. Restriction will be the file if TYPE is `file', or if type is the
  20355. universal prefix '(4), or if the cursor is before the first headline
  20356. in the file. Otherwise, restriction will be to the current subtree."
  20357. (interactive "P")
  20358. (and (equal type '(4)) (setq type 'file))
  20359. (setq type (cond
  20360. (type type)
  20361. ((org-at-heading-p) 'subtree)
  20362. ((condition-case nil (org-back-to-heading t) (error nil))
  20363. 'subtree)
  20364. (t 'file)))
  20365. (if (eq type 'subtree)
  20366. (progn
  20367. (setq org-agenda-restrict t)
  20368. (setq org-agenda-overriding-restriction 'subtree)
  20369. (put 'org-agenda-files 'org-restrict
  20370. (list (buffer-file-name (buffer-base-buffer))))
  20371. (org-back-to-heading t)
  20372. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20373. (move-marker org-agenda-restrict-begin (point))
  20374. (move-marker org-agenda-restrict-end
  20375. (save-excursion (org-end-of-subtree t)))
  20376. (message "Locking agenda restriction to subtree"))
  20377. (put 'org-agenda-files 'org-restrict
  20378. (list (buffer-file-name (buffer-base-buffer))))
  20379. (setq org-agenda-restrict nil)
  20380. (setq org-agenda-overriding-restriction 'file)
  20381. (move-marker org-agenda-restrict-begin nil)
  20382. (move-marker org-agenda-restrict-end nil)
  20383. (message "Locking agenda restriction to file"))
  20384. (setq current-prefix-arg nil)
  20385. (org-agenda-maybe-redo))
  20386. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20387. "Remove the agenda restriction lock."
  20388. (interactive "P")
  20389. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20390. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20391. (setq org-agenda-overriding-restriction nil)
  20392. (setq org-agenda-restrict nil)
  20393. (put 'org-agenda-files 'org-restrict nil)
  20394. (move-marker org-agenda-restrict-begin nil)
  20395. (move-marker org-agenda-restrict-end nil)
  20396. (setq current-prefix-arg nil)
  20397. (message "Agenda restriction lock removed")
  20398. (or noupdate (org-agenda-maybe-redo)))
  20399. (defun org-agenda-maybe-redo ()
  20400. "If there is any window showing the agenda view, update it."
  20401. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20402. (w0 (selected-window)))
  20403. (when w
  20404. (select-window w)
  20405. (org-agenda-redo)
  20406. (select-window w0)
  20407. (if org-agenda-overriding-restriction
  20408. (message "Agenda view shifted to new %s restriction"
  20409. org-agenda-overriding-restriction)
  20410. (message "Agenda restriction lock removed")))))
  20411. ;;; Agenda commands
  20412. (defun org-agenda-check-type (error &rest types)
  20413. "Check if agenda buffer is of allowed type.
  20414. If ERROR is non-nil, throw an error, otherwise just return nil."
  20415. (if (memq org-agenda-type types)
  20416. t
  20417. (if error
  20418. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20419. nil)))
  20420. (defun org-agenda-quit ()
  20421. "Exit agenda by removing the window or the buffer."
  20422. (interactive)
  20423. (let ((buf (current-buffer)))
  20424. (if (not (one-window-p)) (delete-window))
  20425. (kill-buffer buf)
  20426. (org-agenda-reset-markers)
  20427. (org-columns-remove-overlays))
  20428. ;; Maybe restore the pre-agenda window configuration.
  20429. (and org-agenda-restore-windows-after-quit
  20430. (not (eq org-agenda-window-setup 'other-frame))
  20431. org-pre-agenda-window-conf
  20432. (set-window-configuration org-pre-agenda-window-conf)))
  20433. (defun org-agenda-exit ()
  20434. "Exit agenda by removing the window or the buffer.
  20435. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20436. Org-mode buffers visited directly by the user will not be touched."
  20437. (interactive)
  20438. (org-release-buffers org-agenda-new-buffers)
  20439. (setq org-agenda-new-buffers nil)
  20440. (org-agenda-quit))
  20441. (defun org-agenda-execute (arg)
  20442. "Execute another agenda command, keeping same window.\\<global-map>
  20443. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20444. (interactive "P")
  20445. (let ((org-agenda-window-setup 'current-window))
  20446. (org-agenda arg)))
  20447. (defun org-save-all-org-buffers ()
  20448. "Save all Org-mode buffers without user confirmation."
  20449. (interactive)
  20450. (message "Saving all Org-mode buffers...")
  20451. (save-some-buffers t 'org-mode-p)
  20452. (message "Saving all Org-mode buffers... done"))
  20453. (defun org-agenda-redo ()
  20454. "Rebuild Agenda.
  20455. When this is the global TODO list, a prefix argument will be interpreted."
  20456. (interactive)
  20457. (let* ((org-agenda-keep-modes t)
  20458. (line (org-current-line))
  20459. (window-line (- line (org-current-line (window-start))))
  20460. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20461. (message "Rebuilding agenda buffer...")
  20462. (org-let lprops '(eval org-agenda-redo-command))
  20463. (setq org-agenda-undo-list nil
  20464. org-agenda-pending-undo-list nil)
  20465. (message "Rebuilding agenda buffer...done")
  20466. (goto-line line)
  20467. (recenter window-line)))
  20468. (defun org-agenda-goto-date (date)
  20469. "Jump to DATE in agenda."
  20470. (interactive (list (org-read-date)))
  20471. (org-agenda-list nil date))
  20472. (defun org-agenda-goto-today ()
  20473. "Go to today."
  20474. (interactive)
  20475. (org-agenda-check-type t 'timeline 'agenda)
  20476. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20477. (cond
  20478. (tdpos (goto-char tdpos))
  20479. ((eq org-agenda-type 'agenda)
  20480. (let* ((sd (time-to-days
  20481. (time-subtract (current-time)
  20482. (list 0 (* 3600 org-extend-today-until) 0))))
  20483. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20484. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20485. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20486. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20487. (org-agenda-redo)
  20488. (org-agenda-find-same-or-today-or-agenda)))
  20489. (t (error "Cannot find today")))))
  20490. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20491. (goto-char
  20492. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20493. (text-property-any (point-min) (point-max) 'org-today t)
  20494. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20495. (point-min))))
  20496. (defun org-agenda-later (arg)
  20497. "Go forward in time by thee current span.
  20498. With prefix ARG, go forward that many times the current span."
  20499. (interactive "p")
  20500. (org-agenda-check-type t 'agenda)
  20501. (let* ((span org-agenda-span)
  20502. (sd org-starting-day)
  20503. (greg (calendar-gregorian-from-absolute sd))
  20504. (cnt (get-text-property (point) 'org-day-cnt))
  20505. greg2 nd)
  20506. (cond
  20507. ((eq span 'day)
  20508. (setq sd (+ arg sd) nd 1))
  20509. ((eq span 'week)
  20510. (setq sd (+ (* 7 arg) sd) nd 7))
  20511. ((eq span 'month)
  20512. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20513. sd (calendar-absolute-from-gregorian greg2))
  20514. (setcar greg2 (1+ (car greg2)))
  20515. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20516. ((eq span 'year)
  20517. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20518. sd (calendar-absolute-from-gregorian greg2))
  20519. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20520. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20521. (let ((org-agenda-overriding-arguments
  20522. (list (car org-agenda-last-arguments) sd nd t)))
  20523. (org-agenda-redo)
  20524. (org-agenda-find-same-or-today-or-agenda cnt))))
  20525. (defun org-agenda-earlier (arg)
  20526. "Go backward in time by the current span.
  20527. With prefix ARG, go backward that many times the current span."
  20528. (interactive "p")
  20529. (org-agenda-later (- arg)))
  20530. (defun org-agenda-day-view ()
  20531. "Switch to daily view for agenda."
  20532. (interactive)
  20533. (setq org-agenda-ndays 1)
  20534. (org-agenda-change-time-span 'day))
  20535. (defun org-agenda-week-view ()
  20536. "Switch to daily view for agenda."
  20537. (interactive)
  20538. (setq org-agenda-ndays 7)
  20539. (org-agenda-change-time-span 'week))
  20540. (defun org-agenda-month-view ()
  20541. "Switch to daily view for agenda."
  20542. (interactive)
  20543. (org-agenda-change-time-span 'month))
  20544. (defun org-agenda-year-view ()
  20545. "Switch to daily view for agenda."
  20546. (interactive)
  20547. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20548. (org-agenda-change-time-span 'year)
  20549. (error "Abort")))
  20550. (defun org-agenda-change-time-span (span)
  20551. "Change the agenda view to SPAN.
  20552. SPAN may be `day', `week', `month', `year'."
  20553. (org-agenda-check-type t 'agenda)
  20554. (if (equal org-agenda-span span)
  20555. (error "Viewing span is already \"%s\"" span))
  20556. (let* ((sd (or (get-text-property (point) 'day)
  20557. org-starting-day))
  20558. (computed (org-agenda-compute-time-span sd span))
  20559. (org-agenda-overriding-arguments
  20560. (list (car org-agenda-last-arguments)
  20561. (car computed) (cdr computed) t)))
  20562. (org-agenda-redo)
  20563. (org-agenda-find-same-or-today-or-agenda))
  20564. (org-agenda-set-mode-name)
  20565. (message "Switched to %s view" span))
  20566. (defun org-agenda-compute-time-span (sd span)
  20567. "Compute starting date and number of days for agenda.
  20568. SPAN may be `day', `week', `month', `year'. The return value
  20569. is a cons cell with the starting date and the number of days,
  20570. so that the date SD will be in that range."
  20571. (let* ((greg (calendar-gregorian-from-absolute sd))
  20572. nd)
  20573. (cond
  20574. ((eq span 'day)
  20575. (setq nd 1))
  20576. ((eq span 'week)
  20577. (let* ((nt (calendar-day-of-week
  20578. (calendar-gregorian-from-absolute sd)))
  20579. (d (if org-agenda-start-on-weekday
  20580. (- nt org-agenda-start-on-weekday)
  20581. 0)))
  20582. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20583. (setq nd 7)))
  20584. ((eq span 'month)
  20585. (setq sd (calendar-absolute-from-gregorian
  20586. (list (car greg) 1 (nth 2 greg)))
  20587. nd (- (calendar-absolute-from-gregorian
  20588. (list (1+ (car greg)) 1 (nth 2 greg)))
  20589. sd)))
  20590. ((eq span 'year)
  20591. (setq sd (calendar-absolute-from-gregorian
  20592. (list 1 1 (nth 2 greg)))
  20593. nd (- (calendar-absolute-from-gregorian
  20594. (list 1 1 (1+ (nth 2 greg))))
  20595. sd))))
  20596. (cons sd nd)))
  20597. ;; FIXME: does not work if user makes date format that starts with a blank
  20598. (defun org-agenda-next-date-line (&optional arg)
  20599. "Jump to the next line indicating a date in agenda buffer."
  20600. (interactive "p")
  20601. (org-agenda-check-type t 'agenda 'timeline)
  20602. (beginning-of-line 1)
  20603. (if (looking-at "^\\S-") (forward-char 1))
  20604. (if (not (re-search-forward "^\\S-" nil t arg))
  20605. (progn
  20606. (backward-char 1)
  20607. (error "No next date after this line in this buffer")))
  20608. (goto-char (match-beginning 0)))
  20609. (defun org-agenda-previous-date-line (&optional arg)
  20610. "Jump to the previous line indicating a date in agenda buffer."
  20611. (interactive "p")
  20612. (org-agenda-check-type t 'agenda 'timeline)
  20613. (beginning-of-line 1)
  20614. (if (not (re-search-backward "^\\S-" nil t arg))
  20615. (error "No previous date before this line in this buffer")))
  20616. ;; Initialize the highlight
  20617. (defvar org-hl (org-make-overlay 1 1))
  20618. (org-overlay-put org-hl 'face 'highlight)
  20619. (defun org-highlight (begin end &optional buffer)
  20620. "Highlight a region with overlay."
  20621. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20622. org-hl begin end (or buffer (current-buffer))))
  20623. (defun org-unhighlight ()
  20624. "Detach overlay INDEX."
  20625. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20626. ;; FIXME this is currently not used.
  20627. (defun org-highlight-until-next-command (beg end &optional buffer)
  20628. (org-highlight beg end buffer)
  20629. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20630. (defun org-unhighlight-once ()
  20631. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20632. (org-unhighlight))
  20633. (defun org-agenda-follow-mode ()
  20634. "Toggle follow mode in an agenda buffer."
  20635. (interactive)
  20636. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20637. (org-agenda-set-mode-name)
  20638. (message "Follow mode is %s"
  20639. (if org-agenda-follow-mode "on" "off")))
  20640. (defun org-agenda-log-mode ()
  20641. "Toggle log mode in an agenda buffer."
  20642. (interactive)
  20643. (org-agenda-check-type t 'agenda 'timeline)
  20644. (setq org-agenda-show-log (not org-agenda-show-log))
  20645. (org-agenda-set-mode-name)
  20646. (org-agenda-redo)
  20647. (message "Log mode is %s"
  20648. (if org-agenda-show-log "on" "off")))
  20649. (defun org-agenda-toggle-diary ()
  20650. "Toggle diary inclusion in an agenda buffer."
  20651. (interactive)
  20652. (org-agenda-check-type t 'agenda)
  20653. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20654. (org-agenda-redo)
  20655. (org-agenda-set-mode-name)
  20656. (message "Diary inclusion turned %s"
  20657. (if org-agenda-include-diary "on" "off")))
  20658. (defun org-agenda-toggle-time-grid ()
  20659. "Toggle time grid in an agenda buffer."
  20660. (interactive)
  20661. (org-agenda-check-type t 'agenda)
  20662. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20663. (org-agenda-redo)
  20664. (org-agenda-set-mode-name)
  20665. (message "Time-grid turned %s"
  20666. (if org-agenda-use-time-grid "on" "off")))
  20667. (defun org-agenda-set-mode-name ()
  20668. "Set the mode name to indicate all the small mode settings."
  20669. (setq mode-name
  20670. (concat "Org-Agenda"
  20671. (if (equal org-agenda-ndays 1) " Day" "")
  20672. (if (equal org-agenda-ndays 7) " Week" "")
  20673. (if org-agenda-follow-mode " Follow" "")
  20674. (if org-agenda-include-diary " Diary" "")
  20675. (if org-agenda-use-time-grid " Grid" "")
  20676. (if org-agenda-show-log " Log" "")))
  20677. (force-mode-line-update))
  20678. (defun org-agenda-post-command-hook ()
  20679. (and (eolp) (not (bolp)) (backward-char 1))
  20680. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20681. (if (and org-agenda-follow-mode
  20682. (get-text-property (point) 'org-marker))
  20683. (org-agenda-show)))
  20684. (defun org-agenda-show-priority ()
  20685. "Show the priority of the current item.
  20686. This priority is composed of the main priority given with the [#A] cookies,
  20687. and by additional input from the age of a schedules or deadline entry."
  20688. (interactive)
  20689. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20690. (message "Priority is %d" (if pri pri -1000))))
  20691. (defun org-agenda-show-tags ()
  20692. "Show the tags applicable to the current item."
  20693. (interactive)
  20694. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20695. (if tags
  20696. (message "Tags are :%s:"
  20697. (org-no-properties (mapconcat 'identity tags ":")))
  20698. (message "No tags associated with this line"))))
  20699. (defun org-agenda-goto (&optional highlight)
  20700. "Go to the Org-mode file which contains the item at point."
  20701. (interactive)
  20702. (let* ((marker (or (get-text-property (point) 'org-marker)
  20703. (org-agenda-error)))
  20704. (buffer (marker-buffer marker))
  20705. (pos (marker-position marker)))
  20706. (switch-to-buffer-other-window buffer)
  20707. (widen)
  20708. (goto-char pos)
  20709. (when (org-mode-p)
  20710. (org-show-context 'agenda)
  20711. (save-excursion
  20712. (and (outline-next-heading)
  20713. (org-flag-heading nil)))) ; show the next heading
  20714. (recenter (/ (window-height) 2))
  20715. (run-hooks 'org-agenda-after-show-hook)
  20716. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20717. (defvar org-agenda-after-show-hook nil
  20718. "Normal hook run after an item has been shown from the agenda.
  20719. Point is in the buffer where the item originated.")
  20720. (defun org-agenda-kill ()
  20721. "Kill the entry or subtree belonging to the current agenda entry."
  20722. (interactive)
  20723. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20724. (let* ((marker (or (get-text-property (point) 'org-marker)
  20725. (org-agenda-error)))
  20726. (buffer (marker-buffer marker))
  20727. (pos (marker-position marker))
  20728. (type (get-text-property (point) 'type))
  20729. dbeg dend (n 0) conf)
  20730. (org-with-remote-undo buffer
  20731. (with-current-buffer buffer
  20732. (save-excursion
  20733. (goto-char pos)
  20734. (if (and (org-mode-p) (not (member type '("sexp"))))
  20735. (setq dbeg (progn (org-back-to-heading t) (point))
  20736. dend (org-end-of-subtree t t))
  20737. (setq dbeg (point-at-bol)
  20738. dend (min (point-max) (1+ (point-at-eol)))))
  20739. (goto-char dbeg)
  20740. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20741. (setq conf (or (eq t org-agenda-confirm-kill)
  20742. (and (numberp org-agenda-confirm-kill)
  20743. (> n org-agenda-confirm-kill))))
  20744. (and conf
  20745. (not (y-or-n-p
  20746. (format "Delete entry with %d lines in buffer \"%s\"? "
  20747. n (buffer-name buffer))))
  20748. (error "Abort"))
  20749. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20750. (with-current-buffer buffer (delete-region dbeg dend))
  20751. (message "Agenda item and source killed"))))
  20752. (defun org-agenda-archive ()
  20753. "Kill the entry or subtree belonging to the current agenda entry."
  20754. (interactive)
  20755. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20756. (let* ((marker (or (get-text-property (point) 'org-marker)
  20757. (org-agenda-error)))
  20758. (buffer (marker-buffer marker))
  20759. (pos (marker-position marker)))
  20760. (org-with-remote-undo buffer
  20761. (with-current-buffer buffer
  20762. (if (org-mode-p)
  20763. (save-excursion
  20764. (goto-char pos)
  20765. (org-remove-subtree-entries-from-agenda)
  20766. (org-back-to-heading t)
  20767. (org-archive-subtree))
  20768. (error "Archiving works only in Org-mode files"))))))
  20769. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20770. "Remove all lines in the agenda that correspond to a given subtree.
  20771. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20772. If this information is not given, the function uses the tree at point."
  20773. (let ((buf (or buf (current-buffer))) m p)
  20774. (save-excursion
  20775. (unless (and beg end)
  20776. (org-back-to-heading t)
  20777. (setq beg (point))
  20778. (org-end-of-subtree t)
  20779. (setq end (point)))
  20780. (set-buffer (get-buffer org-agenda-buffer-name))
  20781. (save-excursion
  20782. (goto-char (point-max))
  20783. (beginning-of-line 1)
  20784. (while (not (bobp))
  20785. (when (and (setq m (get-text-property (point) 'org-marker))
  20786. (equal buf (marker-buffer m))
  20787. (setq p (marker-position m))
  20788. (>= p beg)
  20789. (<= p end))
  20790. (let ((inhibit-read-only t))
  20791. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20792. (beginning-of-line 0))))))
  20793. (defun org-agenda-open-link ()
  20794. "Follow the link in the current line, if any."
  20795. (interactive)
  20796. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20797. (save-excursion
  20798. (save-restriction
  20799. (narrow-to-region (point-at-bol) (point-at-eol))
  20800. (org-open-at-point))))
  20801. (defun org-agenda-copy-local-variable (var)
  20802. "Get a variable from a referenced buffer and install it here."
  20803. (let ((m (get-text-property (point) 'org-marker)))
  20804. (when (and m (buffer-live-p (marker-buffer m)))
  20805. (org-set-local var (with-current-buffer (marker-buffer m)
  20806. (symbol-value var))))))
  20807. (defun org-agenda-switch-to (&optional delete-other-windows)
  20808. "Go to the Org-mode file which contains the item at point."
  20809. (interactive)
  20810. (let* ((marker (or (get-text-property (point) 'org-marker)
  20811. (org-agenda-error)))
  20812. (buffer (marker-buffer marker))
  20813. (pos (marker-position marker)))
  20814. (switch-to-buffer buffer)
  20815. (and delete-other-windows (delete-other-windows))
  20816. (widen)
  20817. (goto-char pos)
  20818. (when (org-mode-p)
  20819. (org-show-context 'agenda)
  20820. (save-excursion
  20821. (and (outline-next-heading)
  20822. (org-flag-heading nil)))))) ; show the next heading
  20823. (defun org-agenda-goto-mouse (ev)
  20824. "Go to the Org-mode file which contains the item at the mouse click."
  20825. (interactive "e")
  20826. (mouse-set-point ev)
  20827. (org-agenda-goto))
  20828. (defun org-agenda-show ()
  20829. "Display the Org-mode file which contains the item at point."
  20830. (interactive)
  20831. (let ((win (selected-window)))
  20832. (org-agenda-goto t)
  20833. (select-window win)))
  20834. (defun org-agenda-recenter (arg)
  20835. "Display the Org-mode file which contains the item at point and recenter."
  20836. (interactive "P")
  20837. (let ((win (selected-window)))
  20838. (org-agenda-goto t)
  20839. (recenter arg)
  20840. (select-window win)))
  20841. (defun org-agenda-show-mouse (ev)
  20842. "Display the Org-mode file which contains the item at the mouse click."
  20843. (interactive "e")
  20844. (mouse-set-point ev)
  20845. (org-agenda-show))
  20846. (defun org-agenda-check-no-diary ()
  20847. "Check if the entry is a diary link and abort if yes."
  20848. (if (get-text-property (point) 'org-agenda-diary-link)
  20849. (org-agenda-error)))
  20850. (defun org-agenda-error ()
  20851. (error "Command not allowed in this line"))
  20852. (defun org-agenda-tree-to-indirect-buffer ()
  20853. "Show the subtree corresponding to the current entry in an indirect buffer.
  20854. This calls the command `org-tree-to-indirect-buffer' from the original
  20855. Org-mode buffer.
  20856. With numerical prefix arg ARG, go up to this level and then take that tree.
  20857. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20858. dedicated frame)."
  20859. (interactive)
  20860. (org-agenda-check-no-diary)
  20861. (let* ((marker (or (get-text-property (point) 'org-marker)
  20862. (org-agenda-error)))
  20863. (buffer (marker-buffer marker))
  20864. (pos (marker-position marker)))
  20865. (with-current-buffer buffer
  20866. (save-excursion
  20867. (goto-char pos)
  20868. (call-interactively 'org-tree-to-indirect-buffer)))))
  20869. (defvar org-last-heading-marker (make-marker)
  20870. "Marker pointing to the headline that last changed its TODO state
  20871. by a remote command from the agenda.")
  20872. (defun org-agenda-todo-nextset ()
  20873. "Switch TODO entry to next sequence."
  20874. (interactive)
  20875. (org-agenda-todo 'nextset))
  20876. (defun org-agenda-todo-previousset ()
  20877. "Switch TODO entry to previous sequence."
  20878. (interactive)
  20879. (org-agenda-todo 'previousset))
  20880. (defun org-agenda-todo (&optional arg)
  20881. "Cycle TODO state of line at point, also in Org-mode file.
  20882. This changes the line at point, all other lines in the agenda referring to
  20883. the same tree node, and the headline of the tree node in the Org-mode file."
  20884. (interactive "P")
  20885. (org-agenda-check-no-diary)
  20886. (let* ((col (current-column))
  20887. (marker (or (get-text-property (point) 'org-marker)
  20888. (org-agenda-error)))
  20889. (buffer (marker-buffer marker))
  20890. (pos (marker-position marker))
  20891. (hdmarker (get-text-property (point) 'org-hd-marker))
  20892. (inhibit-read-only t)
  20893. newhead)
  20894. (org-with-remote-undo buffer
  20895. (with-current-buffer buffer
  20896. (widen)
  20897. (goto-char pos)
  20898. (org-show-context 'agenda)
  20899. (save-excursion
  20900. (and (outline-next-heading)
  20901. (org-flag-heading nil))) ; show the next heading
  20902. (org-todo arg)
  20903. (and (bolp) (forward-char 1))
  20904. (setq newhead (org-get-heading))
  20905. (save-excursion
  20906. (org-back-to-heading)
  20907. (move-marker org-last-heading-marker (point))))
  20908. (beginning-of-line 1)
  20909. (save-excursion
  20910. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20911. (move-to-column col))))
  20912. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20913. "Change all lines in the agenda buffer which match HDMARKER.
  20914. The new content of the line will be NEWHEAD (as modified by
  20915. `org-format-agenda-item'). HDMARKER is checked with
  20916. `equal' against all `org-hd-marker' text properties in the file.
  20917. If FIXFACE is non-nil, the face of each item is modified acording to
  20918. the new TODO state."
  20919. (let* ((inhibit-read-only t)
  20920. props m pl undone-face done-face finish new dotime cat tags)
  20921. (save-excursion
  20922. (goto-char (point-max))
  20923. (beginning-of-line 1)
  20924. (while (not finish)
  20925. (setq finish (bobp))
  20926. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20927. (equal m hdmarker))
  20928. (setq props (text-properties-at (point))
  20929. dotime (get-text-property (point) 'dotime)
  20930. cat (get-text-property (point) 'org-category)
  20931. tags (get-text-property (point) 'tags)
  20932. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20933. pl (get-text-property (point) 'prefix-length)
  20934. undone-face (get-text-property (point) 'undone-face)
  20935. done-face (get-text-property (point) 'done-face))
  20936. (move-to-column pl)
  20937. (cond
  20938. ((equal new "")
  20939. (beginning-of-line 1)
  20940. (and (looking-at ".*\n?") (replace-match "")))
  20941. ((looking-at ".*")
  20942. (replace-match new t t)
  20943. (beginning-of-line 1)
  20944. (add-text-properties (point-at-bol) (point-at-eol) props)
  20945. (when fixface
  20946. (add-text-properties
  20947. (point-at-bol) (point-at-eol)
  20948. (list 'face
  20949. (if org-last-todo-state-is-todo
  20950. undone-face done-face))))
  20951. (org-agenda-highlight-todo 'line)
  20952. (beginning-of-line 1))
  20953. (t (error "Line update did not work"))))
  20954. (beginning-of-line 0)))
  20955. (org-finalize-agenda)))
  20956. (defun org-agenda-align-tags (&optional line)
  20957. "Align all tags in agenda items to `org-agenda-tags-column'."
  20958. (let ((inhibit-read-only t) l c)
  20959. (save-excursion
  20960. (goto-char (if line (point-at-bol) (point-min)))
  20961. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20962. (if line (point-at-eol) nil) t)
  20963. (add-text-properties
  20964. (match-beginning 2) (match-end 2)
  20965. (list 'face (list 'org-tag (get-text-property
  20966. (match-beginning 2) 'face))))
  20967. (setq l (- (match-end 2) (match-beginning 2))
  20968. c (if (< org-agenda-tags-column 0)
  20969. (- (abs org-agenda-tags-column) l)
  20970. org-agenda-tags-column))
  20971. (delete-region (match-beginning 1) (match-end 1))
  20972. (goto-char (match-beginning 1))
  20973. (insert (org-add-props
  20974. (make-string (max 1 (- c (current-column))) ?\ )
  20975. (text-properties-at (point))))))))
  20976. (defun org-agenda-priority-up ()
  20977. "Increase the priority of line at point, also in Org-mode file."
  20978. (interactive)
  20979. (org-agenda-priority 'up))
  20980. (defun org-agenda-priority-down ()
  20981. "Decrease the priority of line at point, also in Org-mode file."
  20982. (interactive)
  20983. (org-agenda-priority 'down))
  20984. (defun org-agenda-priority (&optional force-direction)
  20985. "Set the priority of line at point, also in Org-mode file.
  20986. This changes the line at point, all other lines in the agenda referring to
  20987. the same tree node, and the headline of the tree node in the Org-mode file."
  20988. (interactive)
  20989. (org-agenda-check-no-diary)
  20990. (let* ((marker (or (get-text-property (point) 'org-marker)
  20991. (org-agenda-error)))
  20992. (hdmarker (get-text-property (point) 'org-hd-marker))
  20993. (buffer (marker-buffer hdmarker))
  20994. (pos (marker-position hdmarker))
  20995. (inhibit-read-only t)
  20996. newhead)
  20997. (org-with-remote-undo buffer
  20998. (with-current-buffer buffer
  20999. (widen)
  21000. (goto-char pos)
  21001. (org-show-context 'agenda)
  21002. (save-excursion
  21003. (and (outline-next-heading)
  21004. (org-flag-heading nil))) ; show the next heading
  21005. (funcall 'org-priority force-direction)
  21006. (end-of-line 1)
  21007. (setq newhead (org-get-heading)))
  21008. (org-agenda-change-all-lines newhead hdmarker)
  21009. (beginning-of-line 1))))
  21010. (defun org-get-tags-at (&optional pos)
  21011. "Get a list of all headline tags applicable at POS.
  21012. POS defaults to point. If tags are inherited, the list contains
  21013. the targets in the same sequence as the headlines appear, i.e.
  21014. the tags of the current headline come last."
  21015. (interactive)
  21016. (let (tags lastpos)
  21017. (save-excursion
  21018. (save-restriction
  21019. (widen)
  21020. (goto-char (or pos (point)))
  21021. (save-match-data
  21022. (org-back-to-heading t)
  21023. (condition-case nil
  21024. (while (not (equal lastpos (point)))
  21025. (setq lastpos (point))
  21026. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21027. (setq tags (append (org-split-string
  21028. (org-match-string-no-properties 1) ":")
  21029. tags)))
  21030. (or org-use-tag-inheritance (error ""))
  21031. (org-up-heading-all 1))
  21032. (error nil))))
  21033. tags)))
  21034. ;; FIXME: should fix the tags property of the agenda line.
  21035. (defun org-agenda-set-tags ()
  21036. "Set tags for the current headline."
  21037. (interactive)
  21038. (org-agenda-check-no-diary)
  21039. (if (and (org-region-active-p) (interactive-p))
  21040. (call-interactively 'org-change-tag-in-region)
  21041. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21042. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21043. (org-agenda-error)))
  21044. (buffer (marker-buffer hdmarker))
  21045. (pos (marker-position hdmarker))
  21046. (inhibit-read-only t)
  21047. newhead)
  21048. (org-with-remote-undo buffer
  21049. (with-current-buffer buffer
  21050. (widen)
  21051. (goto-char pos)
  21052. (save-excursion
  21053. (org-show-context 'agenda))
  21054. (save-excursion
  21055. (and (outline-next-heading)
  21056. (org-flag-heading nil))) ; show the next heading
  21057. (goto-char pos)
  21058. (call-interactively 'org-set-tags)
  21059. (end-of-line 1)
  21060. (setq newhead (org-get-heading)))
  21061. (org-agenda-change-all-lines newhead hdmarker)
  21062. (beginning-of-line 1)))))
  21063. (defun org-agenda-toggle-archive-tag ()
  21064. "Toggle the archive tag for the current entry."
  21065. (interactive)
  21066. (org-agenda-check-no-diary)
  21067. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21068. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21069. (org-agenda-error)))
  21070. (buffer (marker-buffer hdmarker))
  21071. (pos (marker-position hdmarker))
  21072. (inhibit-read-only t)
  21073. newhead)
  21074. (org-with-remote-undo buffer
  21075. (with-current-buffer buffer
  21076. (widen)
  21077. (goto-char pos)
  21078. (org-show-context 'agenda)
  21079. (save-excursion
  21080. (and (outline-next-heading)
  21081. (org-flag-heading nil))) ; show the next heading
  21082. (call-interactively 'org-toggle-archive-tag)
  21083. (end-of-line 1)
  21084. (setq newhead (org-get-heading)))
  21085. (org-agenda-change-all-lines newhead hdmarker)
  21086. (beginning-of-line 1))))
  21087. (defun org-agenda-date-later (arg &optional what)
  21088. "Change the date of this item to one day later."
  21089. (interactive "p")
  21090. (org-agenda-check-type t 'agenda 'timeline)
  21091. (org-agenda-check-no-diary)
  21092. (let* ((marker (or (get-text-property (point) 'org-marker)
  21093. (org-agenda-error)))
  21094. (buffer (marker-buffer marker))
  21095. (pos (marker-position marker)))
  21096. (org-with-remote-undo buffer
  21097. (with-current-buffer buffer
  21098. (widen)
  21099. (goto-char pos)
  21100. (if (not (org-at-timestamp-p))
  21101. (error "Cannot find time stamp"))
  21102. (org-timestamp-change arg (or what 'day)))
  21103. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21104. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21105. (defun org-agenda-date-earlier (arg &optional what)
  21106. "Change the date of this item to one day earlier."
  21107. (interactive "p")
  21108. (org-agenda-date-later (- arg) what))
  21109. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21110. "Show new date stamp via text properties."
  21111. ;; We use text properties to make this undoable
  21112. (let ((inhibit-read-only t))
  21113. (setq stamp (concat " " prefix " => " stamp))
  21114. (save-excursion
  21115. (goto-char (point-max))
  21116. (while (not (bobp))
  21117. (when (equal marker (get-text-property (point) 'org-marker))
  21118. (move-to-column (- (window-width) (length stamp)) t)
  21119. (if (featurep 'xemacs)
  21120. ;; Use `duplicable' property to trigger undo recording
  21121. (let ((ex (make-extent nil nil))
  21122. (gl (make-glyph stamp)))
  21123. (set-glyph-face gl 'secondary-selection)
  21124. (set-extent-properties
  21125. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21126. (insert-extent ex (1- (point)) (point-at-eol)))
  21127. (add-text-properties
  21128. (1- (point)) (point-at-eol)
  21129. (list 'display (org-add-props stamp nil
  21130. 'face 'secondary-selection))))
  21131. (beginning-of-line 1))
  21132. (beginning-of-line 0)))))
  21133. (defun org-agenda-date-prompt (arg)
  21134. "Change the date of this item. Date is prompted for, with default today.
  21135. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21136. be used to request time specification in the time stamp."
  21137. (interactive "P")
  21138. (org-agenda-check-type t 'agenda 'timeline)
  21139. (org-agenda-check-no-diary)
  21140. (let* ((marker (or (get-text-property (point) 'org-marker)
  21141. (org-agenda-error)))
  21142. (buffer (marker-buffer marker))
  21143. (pos (marker-position marker)))
  21144. (org-with-remote-undo buffer
  21145. (with-current-buffer buffer
  21146. (widen)
  21147. (goto-char pos)
  21148. (if (not (org-at-timestamp-p))
  21149. (error "Cannot find time stamp"))
  21150. (org-time-stamp arg)
  21151. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21152. (defun org-agenda-schedule (arg)
  21153. "Schedule the item at point."
  21154. (interactive "P")
  21155. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21156. (org-agenda-check-no-diary)
  21157. (let* ((marker (or (get-text-property (point) 'org-marker)
  21158. (org-agenda-error)))
  21159. (buffer (marker-buffer marker))
  21160. (pos (marker-position marker))
  21161. (org-insert-labeled-timestamps-at-point nil)
  21162. ts)
  21163. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21164. (set-marker-insertion-type marker t)
  21165. (org-with-remote-undo buffer
  21166. (with-current-buffer buffer
  21167. (widen)
  21168. (goto-char pos)
  21169. (setq ts (org-schedule arg)))
  21170. (org-agenda-show-new-time marker ts "S"))
  21171. (message "Item scheduled for %s" ts)))
  21172. (defun org-agenda-deadline (arg)
  21173. "Schedule the item at point."
  21174. (interactive "P")
  21175. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21176. (org-agenda-check-no-diary)
  21177. (let* ((marker (or (get-text-property (point) 'org-marker)
  21178. (org-agenda-error)))
  21179. (buffer (marker-buffer marker))
  21180. (pos (marker-position marker))
  21181. (org-insert-labeled-timestamps-at-point nil)
  21182. ts)
  21183. (org-with-remote-undo buffer
  21184. (with-current-buffer buffer
  21185. (widen)
  21186. (goto-char pos)
  21187. (setq ts (org-deadline arg)))
  21188. (org-agenda-show-new-time marker ts "S"))
  21189. (message "Deadline for this item set to %s" ts)))
  21190. (defun org-get-heading (&optional no-tags)
  21191. "Return the heading of the current entry, without the stars."
  21192. (save-excursion
  21193. (org-back-to-heading t)
  21194. (if (looking-at
  21195. (if no-tags
  21196. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21197. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21198. (match-string 1) "")))
  21199. (defun org-agenda-clock-in (&optional arg)
  21200. "Start the clock on the currently selected item."
  21201. (interactive "P")
  21202. (org-agenda-check-no-diary)
  21203. (let* ((marker (or (get-text-property (point) 'org-marker)
  21204. (org-agenda-error)))
  21205. (pos (marker-position marker)))
  21206. (org-with-remote-undo (marker-buffer marker)
  21207. (with-current-buffer (marker-buffer marker)
  21208. (widen)
  21209. (goto-char pos)
  21210. (org-clock-in)))))
  21211. (defun org-agenda-clock-out (&optional arg)
  21212. "Stop the currently running clock."
  21213. (interactive "P")
  21214. (unless (marker-buffer org-clock-marker)
  21215. (error "No running clock"))
  21216. (org-with-remote-undo (marker-buffer org-clock-marker)
  21217. (org-clock-out)))
  21218. (defun org-agenda-clock-cancel (&optional arg)
  21219. "Cancel the currently running clock."
  21220. (interactive "P")
  21221. (unless (marker-buffer org-clock-marker)
  21222. (error "No running clock"))
  21223. (org-with-remote-undo (marker-buffer org-clock-marker)
  21224. (org-clock-cancel)))
  21225. (defun org-agenda-diary-entry ()
  21226. "Make a diary entry, like the `i' command from the calendar.
  21227. All the standard commands work: block, weekly etc."
  21228. (interactive)
  21229. (org-agenda-check-type t 'agenda 'timeline)
  21230. (require 'diary-lib)
  21231. (let* ((char (progn
  21232. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21233. (read-char-exclusive)))
  21234. (cmd (cdr (assoc char
  21235. '((?d . insert-diary-entry)
  21236. (?w . insert-weekly-diary-entry)
  21237. (?m . insert-monthly-diary-entry)
  21238. (?y . insert-yearly-diary-entry)
  21239. (?a . insert-anniversary-diary-entry)
  21240. (?b . insert-block-diary-entry)
  21241. (?c . insert-cyclic-diary-entry)))))
  21242. (oldf (symbol-function 'calendar-cursor-to-date))
  21243. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21244. (point (point))
  21245. (mark (or (mark t) (point))))
  21246. (unless cmd
  21247. (error "No command associated with <%c>" char))
  21248. (unless (and (get-text-property point 'day)
  21249. (or (not (equal ?b char))
  21250. (get-text-property mark 'day)))
  21251. (error "Don't know which date to use for diary entry"))
  21252. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21253. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21254. (let ((calendar-mark-ring
  21255. (list (calendar-gregorian-from-absolute
  21256. (or (get-text-property mark 'day)
  21257. (get-text-property point 'day))))))
  21258. (unwind-protect
  21259. (progn
  21260. (fset 'calendar-cursor-to-date
  21261. (lambda (&optional error)
  21262. (calendar-gregorian-from-absolute
  21263. (get-text-property point 'day))))
  21264. (call-interactively cmd))
  21265. (fset 'calendar-cursor-to-date oldf)))))
  21266. (defun org-agenda-execute-calendar-command (cmd)
  21267. "Execute a calendar command from the agenda, with the date associated to
  21268. the cursor position."
  21269. (org-agenda-check-type t 'agenda 'timeline)
  21270. (require 'diary-lib)
  21271. (unless (get-text-property (point) 'day)
  21272. (error "Don't know which date to use for calendar command"))
  21273. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21274. (point (point))
  21275. (date (calendar-gregorian-from-absolute
  21276. (get-text-property point 'day)))
  21277. ;; the following 3 vars are needed in the calendar
  21278. (displayed-day (extract-calendar-day date))
  21279. (displayed-month (extract-calendar-month date))
  21280. (displayed-year (extract-calendar-year date)))
  21281. (unwind-protect
  21282. (progn
  21283. (fset 'calendar-cursor-to-date
  21284. (lambda (&optional error)
  21285. (calendar-gregorian-from-absolute
  21286. (get-text-property point 'day))))
  21287. (call-interactively cmd))
  21288. (fset 'calendar-cursor-to-date oldf))))
  21289. (defun org-agenda-phases-of-moon ()
  21290. "Display the phases of the moon for the 3 months around the cursor date."
  21291. (interactive)
  21292. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21293. (defun org-agenda-holidays ()
  21294. "Display the holidays for the 3 months around the cursor date."
  21295. (interactive)
  21296. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21297. (defun org-agenda-sunrise-sunset (arg)
  21298. "Display sunrise and sunset for the cursor date.
  21299. Latitude and longitude can be specified with the variables
  21300. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21301. argument, latitude and longitude will be prompted for."
  21302. (interactive "P")
  21303. (let ((calendar-longitude (if arg nil calendar-longitude))
  21304. (calendar-latitude (if arg nil calendar-latitude))
  21305. (calendar-location-name
  21306. (if arg "the given coordinates" calendar-location-name)))
  21307. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21308. (defun org-agenda-goto-calendar ()
  21309. "Open the Emacs calendar with the date at the cursor."
  21310. (interactive)
  21311. (org-agenda-check-type t 'agenda 'timeline)
  21312. (let* ((day (or (get-text-property (point) 'day)
  21313. (error "Don't know which date to open in calendar")))
  21314. (date (calendar-gregorian-from-absolute day))
  21315. (calendar-move-hook nil)
  21316. (view-calendar-holidays-initially nil)
  21317. (view-diary-entries-initially nil))
  21318. (calendar)
  21319. (calendar-goto-date date)))
  21320. (defun org-calendar-goto-agenda ()
  21321. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21322. This is a command that has to be installed in `calendar-mode-map'."
  21323. (interactive)
  21324. (org-agenda-list nil (calendar-absolute-from-gregorian
  21325. (calendar-cursor-to-date))
  21326. nil))
  21327. (defun org-agenda-convert-date ()
  21328. (interactive)
  21329. (org-agenda-check-type t 'agenda 'timeline)
  21330. (let ((day (get-text-property (point) 'day))
  21331. date s)
  21332. (unless day
  21333. (error "Don't know which date to convert"))
  21334. (setq date (calendar-gregorian-from-absolute day))
  21335. (setq s (concat
  21336. "Gregorian: " (calendar-date-string date) "\n"
  21337. "ISO: " (calendar-iso-date-string date) "\n"
  21338. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21339. "Julian: " (calendar-julian-date-string date) "\n"
  21340. "Astron. JD: " (calendar-astro-date-string date)
  21341. " (Julian date number at noon UTC)\n"
  21342. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21343. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21344. "French: " (calendar-french-date-string date) "\n"
  21345. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21346. "Mayan: " (calendar-mayan-date-string date) "\n"
  21347. "Coptic: " (calendar-coptic-date-string date) "\n"
  21348. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21349. "Persian: " (calendar-persian-date-string date) "\n"
  21350. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21351. (with-output-to-temp-buffer "*Dates*"
  21352. (princ s))
  21353. (if (fboundp 'fit-window-to-buffer)
  21354. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21355. ;;;; Embedded LaTeX
  21356. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21357. "Keymap for the minor `org-cdlatex-mode'.")
  21358. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21359. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21360. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21361. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21362. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21363. (defvar org-cdlatex-texmathp-advice-is-done nil
  21364. "Flag remembering if we have applied the advice to texmathp already.")
  21365. (define-minor-mode org-cdlatex-mode
  21366. "Toggle the minor `org-cdlatex-mode'.
  21367. This mode supports entering LaTeX environment and math in LaTeX fragments
  21368. in Org-mode.
  21369. \\{org-cdlatex-mode-map}"
  21370. nil " OCDL" nil
  21371. (when org-cdlatex-mode (require 'cdlatex))
  21372. (unless org-cdlatex-texmathp-advice-is-done
  21373. (setq org-cdlatex-texmathp-advice-is-done t)
  21374. (defadvice texmathp (around org-math-always-on activate)
  21375. "Always return t in org-mode buffers.
  21376. This is because we want to insert math symbols without dollars even outside
  21377. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21378. en embedded LaTeX fragement, let texmathp do its job.
  21379. \\[org-cdlatex-mode-map]"
  21380. (interactive)
  21381. (let (p)
  21382. (cond
  21383. ((not (org-mode-p)) ad-do-it)
  21384. ((eq this-command 'cdlatex-math-symbol)
  21385. (setq ad-return-value t
  21386. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21387. (t
  21388. (let ((p (org-inside-LaTeX-fragment-p)))
  21389. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21390. (setq ad-return-value t
  21391. texmathp-why '("Org-mode embedded math" . 0))
  21392. (if p ad-do-it)))))))))
  21393. (defun turn-on-org-cdlatex ()
  21394. "Unconditionally turn on `org-cdlatex-mode'."
  21395. (org-cdlatex-mode 1))
  21396. (defun org-inside-LaTeX-fragment-p ()
  21397. "Test if point is inside a LaTeX fragment.
  21398. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21399. sequence appearing also before point.
  21400. Even though the matchers for math are configurable, this function assumes
  21401. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21402. delimiters are skipped when they have been removed by customization.
  21403. The return value is nil, or a cons cell with the delimiter and
  21404. and the position of this delimiter.
  21405. This function does a reasonably good job, but can locally be fooled by
  21406. for example currency specifications. For example it will assume being in
  21407. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21408. fragments that are properly closed, but during editing, we have to live
  21409. with the uncertainty caused by missing closing delimiters. This function
  21410. looks only before point, not after."
  21411. (catch 'exit
  21412. (let ((pos (point))
  21413. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21414. (lim (progn
  21415. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21416. (point)))
  21417. dd-on str (start 0) m re)
  21418. (goto-char pos)
  21419. (when dodollar
  21420. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21421. re (nth 1 (assoc "$" org-latex-regexps)))
  21422. (while (string-match re str start)
  21423. (cond
  21424. ((= (match-end 0) (length str))
  21425. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21426. ((= (match-end 0) (- (length str) 5))
  21427. (throw 'exit nil))
  21428. (t (setq start (match-end 0))))))
  21429. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21430. (goto-char pos)
  21431. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21432. (and (match-beginning 2) (throw 'exit nil))
  21433. ;; count $$
  21434. (while (re-search-backward "\\$\\$" lim t)
  21435. (setq dd-on (not dd-on)))
  21436. (goto-char pos)
  21437. (if dd-on (cons "$$" m))))))
  21438. (defun org-try-cdlatex-tab ()
  21439. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21440. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21441. - inside a LaTeX fragment, or
  21442. - after the first word in a line, where an abbreviation expansion could
  21443. insert a LaTeX environment."
  21444. (when org-cdlatex-mode
  21445. (cond
  21446. ((save-excursion
  21447. (skip-chars-backward "a-zA-Z0-9*")
  21448. (skip-chars-backward " \t")
  21449. (bolp))
  21450. (cdlatex-tab) t)
  21451. ((org-inside-LaTeX-fragment-p)
  21452. (cdlatex-tab) t)
  21453. (t nil))))
  21454. (defun org-cdlatex-underscore-caret (&optional arg)
  21455. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21456. Revert to the normal definition outside of these fragments."
  21457. (interactive "P")
  21458. (if (org-inside-LaTeX-fragment-p)
  21459. (call-interactively 'cdlatex-sub-superscript)
  21460. (let (org-cdlatex-mode)
  21461. (call-interactively (key-binding (vector last-input-event))))))
  21462. (defun org-cdlatex-math-modify (&optional arg)
  21463. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21464. Revert to the normal definition outside of these fragments."
  21465. (interactive "P")
  21466. (if (org-inside-LaTeX-fragment-p)
  21467. (call-interactively 'cdlatex-math-modify)
  21468. (let (org-cdlatex-mode)
  21469. (call-interactively (key-binding (vector last-input-event))))))
  21470. (defvar org-latex-fragment-image-overlays nil
  21471. "List of overlays carrying the images of latex fragments.")
  21472. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21473. (defun org-remove-latex-fragment-image-overlays ()
  21474. "Remove all overlays with LaTeX fragment images in current buffer."
  21475. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21476. (setq org-latex-fragment-image-overlays nil))
  21477. (defun org-preview-latex-fragment (&optional subtree)
  21478. "Preview the LaTeX fragment at point, or all locally or globally.
  21479. If the cursor is in a LaTeX fragment, create the image and overlay
  21480. it over the source code. If there is no fragment at point, display
  21481. all fragments in the current text, from one headline to the next. With
  21482. prefix SUBTREE, display all fragments in the current subtree. With a
  21483. double prefix `C-u C-u', or when the cursor is before the first headline,
  21484. display all fragments in the buffer.
  21485. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21486. (interactive "P")
  21487. (org-remove-latex-fragment-image-overlays)
  21488. (save-excursion
  21489. (save-restriction
  21490. (let (beg end at msg)
  21491. (cond
  21492. ((or (equal subtree '(16))
  21493. (not (save-excursion
  21494. (re-search-backward (concat "^" outline-regexp) nil t))))
  21495. (setq beg (point-min) end (point-max)
  21496. msg "Creating images for buffer...%s"))
  21497. ((equal subtree '(4))
  21498. (org-back-to-heading)
  21499. (setq beg (point) end (org-end-of-subtree t)
  21500. msg "Creating images for subtree...%s"))
  21501. (t
  21502. (if (setq at (org-inside-LaTeX-fragment-p))
  21503. (goto-char (max (point-min) (- (cdr at) 2)))
  21504. (org-back-to-heading))
  21505. (setq beg (point) end (progn (outline-next-heading) (point))
  21506. msg (if at "Creating image...%s"
  21507. "Creating images for entry...%s"))))
  21508. (message msg "")
  21509. (narrow-to-region beg end)
  21510. (goto-char beg)
  21511. (org-format-latex
  21512. (concat "ltxpng/" (file-name-sans-extension
  21513. (file-name-nondirectory
  21514. buffer-file-name)))
  21515. default-directory 'overlays msg at 'forbuffer)
  21516. (message msg "done. Use `C-c C-c' to remove images.")))))
  21517. (defvar org-latex-regexps
  21518. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21519. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21520. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21521. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21522. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21523. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21524. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21525. "Regular expressions for matching embedded LaTeX.")
  21526. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21527. "Replace LaTeX fragments with links to an image, and produce images."
  21528. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21529. (let* ((prefixnodir (file-name-nondirectory prefix))
  21530. (absprefix (expand-file-name prefix dir))
  21531. (todir (file-name-directory absprefix))
  21532. (opt org-format-latex-options)
  21533. (matchers (plist-get opt :matchers))
  21534. (re-list org-latex-regexps)
  21535. (cnt 0) txt link beg end re e checkdir
  21536. m n block linkfile movefile ov)
  21537. ;; Check if there are old images files with this prefix, and remove them
  21538. (when (file-directory-p todir)
  21539. (mapc 'delete-file
  21540. (directory-files
  21541. todir 'full
  21542. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21543. ;; Check the different regular expressions
  21544. (while (setq e (pop re-list))
  21545. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21546. block (if (nth 3 e) "\n\n" ""))
  21547. (when (member m matchers)
  21548. (goto-char (point-min))
  21549. (while (re-search-forward re nil t)
  21550. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21551. (setq txt (match-string n)
  21552. beg (match-beginning n) end (match-end n)
  21553. cnt (1+ cnt)
  21554. linkfile (format "%s_%04d.png" prefix cnt)
  21555. movefile (format "%s_%04d.png" absprefix cnt)
  21556. link (concat block "[[file:" linkfile "]]" block))
  21557. (if msg (message msg cnt))
  21558. (goto-char beg)
  21559. (unless checkdir ; make sure the directory exists
  21560. (setq checkdir t)
  21561. (or (file-directory-p todir) (make-directory todir)))
  21562. (org-create-formula-image
  21563. txt movefile opt forbuffer)
  21564. (if overlays
  21565. (progn
  21566. (setq ov (org-make-overlay beg end))
  21567. (if (featurep 'xemacs)
  21568. (progn
  21569. (org-overlay-put ov 'invisible t)
  21570. (org-overlay-put
  21571. ov 'end-glyph
  21572. (make-glyph (vector 'png :file movefile))))
  21573. (org-overlay-put
  21574. ov 'display
  21575. (list 'image :type 'png :file movefile :ascent 'center)))
  21576. (push ov org-latex-fragment-image-overlays)
  21577. (goto-char end))
  21578. (delete-region beg end)
  21579. (insert link))))))))
  21580. ;; This function borrows from Ganesh Swami's latex2png.el
  21581. (defun org-create-formula-image (string tofile options buffer)
  21582. (let* ((tmpdir (if (featurep 'xemacs)
  21583. (temp-directory)
  21584. temporary-file-directory))
  21585. (texfilebase (make-temp-name
  21586. (expand-file-name "orgtex" tmpdir)))
  21587. (texfile (concat texfilebase ".tex"))
  21588. (dvifile (concat texfilebase ".dvi"))
  21589. (pngfile (concat texfilebase ".png"))
  21590. (fnh (face-attribute 'default :height nil))
  21591. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21592. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21593. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21594. "Black"))
  21595. (bg (or (plist-get options (if buffer :background :html-background))
  21596. "Transparent")))
  21597. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21598. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21599. (with-temp-file texfile
  21600. (insert org-format-latex-header
  21601. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21602. (let ((dir default-directory))
  21603. (condition-case nil
  21604. (progn
  21605. (cd tmpdir)
  21606. (call-process "latex" nil nil nil texfile))
  21607. (error nil))
  21608. (cd dir))
  21609. (if (not (file-exists-p dvifile))
  21610. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21611. (call-process "dvipng" nil nil nil
  21612. "-E" "-fg" fg "-bg" bg
  21613. "-D" dpi
  21614. ;;"-x" scale "-y" scale
  21615. "-T" "tight"
  21616. "-o" pngfile
  21617. dvifile)
  21618. (if (not (file-exists-p pngfile))
  21619. (progn (message "Failed to create png file from %s" texfile) nil)
  21620. ;; Use the requested file name and clean up
  21621. (copy-file pngfile tofile 'replace)
  21622. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21623. (delete-file (concat texfilebase e)))
  21624. pngfile))))
  21625. (defun org-dvipng-color (attr)
  21626. "Return an rgb color specification for dvipng."
  21627. (apply 'format "rgb %s %s %s"
  21628. (mapcar 'org-normalize-color
  21629. (color-values (face-attribute 'default attr nil)))))
  21630. (defun org-normalize-color (value)
  21631. "Return string to be used as color value for an RGB component."
  21632. (format "%g" (/ value 65535.0)))
  21633. ;;;; Exporting
  21634. ;;; Variables, constants, and parameter plists
  21635. (defconst org-level-max 20)
  21636. (defvar org-export-html-preamble nil
  21637. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21638. (defvar org-export-html-postamble nil
  21639. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21640. (defvar org-export-html-auto-preamble t
  21641. "Should default preamble be inserted? Set by publishing functions.")
  21642. (defvar org-export-html-auto-postamble t
  21643. "Should default postamble be inserted? Set by publishing functions.")
  21644. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21645. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21646. (defconst org-export-plist-vars
  21647. '((:language . org-export-default-language)
  21648. (:customtime . org-display-custom-times)
  21649. (:headline-levels . org-export-headline-levels)
  21650. (:section-numbers . org-export-with-section-numbers)
  21651. (:table-of-contents . org-export-with-toc)
  21652. (:preserve-breaks . org-export-preserve-breaks)
  21653. (:archived-trees . org-export-with-archived-trees)
  21654. (:emphasize . org-export-with-emphasize)
  21655. (:sub-superscript . org-export-with-sub-superscripts)
  21656. (:special-strings . org-export-with-special-strings)
  21657. (:footnotes . org-export-with-footnotes)
  21658. (:drawers . org-export-with-drawers)
  21659. (:tags . org-export-with-tags)
  21660. (:TeX-macros . org-export-with-TeX-macros)
  21661. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21662. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21663. (:fixed-width . org-export-with-fixed-width)
  21664. (:timestamps . org-export-with-timestamps)
  21665. (:author-info . org-export-author-info)
  21666. (:time-stamp-file . org-export-time-stamp-file)
  21667. (:tables . org-export-with-tables)
  21668. (:table-auto-headline . org-export-highlight-first-table-line)
  21669. (:style . org-export-html-style)
  21670. (:agenda-style . org-agenda-export-html-style)
  21671. (:convert-org-links . org-export-html-link-org-files-as-html)
  21672. (:inline-images . org-export-html-inline-images)
  21673. (:html-extension . org-export-html-extension)
  21674. (:html-table-tag . org-export-html-table-tag)
  21675. (:expand-quoted-html . org-export-html-expand)
  21676. (:timestamp . org-export-html-with-timestamp)
  21677. (:publishing-directory . org-export-publishing-directory)
  21678. (:preamble . org-export-html-preamble)
  21679. (:postamble . org-export-html-postamble)
  21680. (:auto-preamble . org-export-html-auto-preamble)
  21681. (:auto-postamble . org-export-html-auto-postamble)
  21682. (:author . user-full-name)
  21683. (:email . user-mail-address)))
  21684. (defun org-default-export-plist ()
  21685. "Return the property list with default settings for the export variables."
  21686. (let ((l org-export-plist-vars) rtn e)
  21687. (while (setq e (pop l))
  21688. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21689. rtn))
  21690. (defun org-infile-export-plist ()
  21691. "Return the property list with file-local settings for export."
  21692. (save-excursion
  21693. (save-restriction
  21694. (widen)
  21695. (goto-char 0)
  21696. (let ((re (org-make-options-regexp
  21697. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21698. p key val text options)
  21699. (while (re-search-forward re nil t)
  21700. (setq key (org-match-string-no-properties 1)
  21701. val (org-match-string-no-properties 2))
  21702. (cond
  21703. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21704. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21705. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21706. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21707. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21708. ((string-equal key "TEXT")
  21709. (setq text (if text (concat text "\n" val) val)))
  21710. ((string-equal key "OPTIONS") (setq options val))))
  21711. (setq p (plist-put p :text text))
  21712. (when options
  21713. (let ((op '(("H" . :headline-levels)
  21714. ("num" . :section-numbers)
  21715. ("toc" . :table-of-contents)
  21716. ("\\n" . :preserve-breaks)
  21717. ("@" . :expand-quoted-html)
  21718. (":" . :fixed-width)
  21719. ("|" . :tables)
  21720. ("^" . :sub-superscript)
  21721. ("-" . :special-strings)
  21722. ("f" . :footnotes)
  21723. ("d" . :drawers)
  21724. ("tags" . :tags)
  21725. ("*" . :emphasize)
  21726. ("TeX" . :TeX-macros)
  21727. ("LaTeX" . :LaTeX-fragments)
  21728. ("skip" . :skip-before-1st-heading)
  21729. ("author" . :author-info)
  21730. ("timestamp" . :time-stamp-file)))
  21731. o)
  21732. (while (setq o (pop op))
  21733. (if (string-match (concat (regexp-quote (car o))
  21734. ":\\([^ \t\n\r;,.]*\\)")
  21735. options)
  21736. (setq p (plist-put p (cdr o)
  21737. (car (read-from-string
  21738. (match-string 1 options)))))))))
  21739. p))))
  21740. (defun org-export-directory (type plist)
  21741. (let* ((val (plist-get plist :publishing-directory))
  21742. (dir (if (listp val)
  21743. (or (cdr (assoc type val)) ".")
  21744. val)))
  21745. dir))
  21746. (defun org-skip-comments (lines)
  21747. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21748. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21749. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21750. (case-fold-search nil)
  21751. rtn line level)
  21752. (while (setq line (pop lines))
  21753. (cond
  21754. ((and (string-match re1 line)
  21755. (setq level (- (match-end 1) (match-beginning 1))))
  21756. ;; Beginning of a COMMENT subtree. Skip it.
  21757. (while (and (setq line (pop lines))
  21758. (or (not (string-match re2 line))
  21759. (> (- (match-end 1) (match-beginning 1)) level))))
  21760. (setq lines (cons line lines)))
  21761. ((string-match "^#" line)
  21762. ;; an ordinary comment line
  21763. )
  21764. ((and org-export-table-remove-special-lines
  21765. (string-match "^[ \t]*|" line)
  21766. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21767. (and (string-match "| *<[0-9]+> *|" line)
  21768. (not (string-match "| *[^ <|]" line)))))
  21769. ;; a special table line that should be removed
  21770. )
  21771. (t (setq rtn (cons line rtn)))))
  21772. (nreverse rtn)))
  21773. (defun org-export (&optional arg)
  21774. (interactive)
  21775. (let ((help "[t] insert the export option template
  21776. \[v] limit export to visible part of outline tree
  21777. \[a] export as ASCII
  21778. \[h] export as HTML
  21779. \[H] export as HTML to temporary buffer
  21780. \[R] export region as HTML
  21781. \[b] export as HTML and browse immediately
  21782. \[x] export as XOXO
  21783. \[l] export as LaTeX
  21784. \[L] export as LaTeX to temporary buffer
  21785. \[i] export current file as iCalendar file
  21786. \[I] export all agenda files as iCalendar files
  21787. \[c] export agenda files into combined iCalendar file
  21788. \[F] publish current file
  21789. \[P] publish current project
  21790. \[X] publish... (project will be prompted for)
  21791. \[A] publish all projects")
  21792. (cmds
  21793. '((?t . org-insert-export-options-template)
  21794. (?v . org-export-visible)
  21795. (?a . org-export-as-ascii)
  21796. (?h . org-export-as-html)
  21797. (?b . org-export-as-html-and-open)
  21798. (?H . org-export-as-html-to-buffer)
  21799. (?R . org-export-region-as-html)
  21800. (?x . org-export-as-xoxo)
  21801. (?l . org-export-as-latex)
  21802. (?L . org-export-as-latex-to-buffer)
  21803. (?i . org-export-icalendar-this-file)
  21804. (?I . org-export-icalendar-all-agenda-files)
  21805. (?c . org-export-icalendar-combine-agenda-files)
  21806. (?F . org-publish-current-file)
  21807. (?P . org-publish-current-project)
  21808. (?X . org-publish)
  21809. (?A . org-publish-all)))
  21810. r1 r2 ass)
  21811. (save-window-excursion
  21812. (delete-other-windows)
  21813. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21814. (princ help))
  21815. (message "Select command: ")
  21816. (setq r1 (read-char-exclusive)))
  21817. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21818. (if (setq ass (assq r2 cmds))
  21819. (call-interactively (cdr ass))
  21820. (error "No command associated with key %c" r1))))
  21821. (defconst org-html-entities
  21822. '(("nbsp")
  21823. ("iexcl")
  21824. ("cent")
  21825. ("pound")
  21826. ("curren")
  21827. ("yen")
  21828. ("brvbar")
  21829. ("vert" . "&#124;")
  21830. ("sect")
  21831. ("uml")
  21832. ("copy")
  21833. ("ordf")
  21834. ("laquo")
  21835. ("not")
  21836. ("shy")
  21837. ("reg")
  21838. ("macr")
  21839. ("deg")
  21840. ("plusmn")
  21841. ("sup2")
  21842. ("sup3")
  21843. ("acute")
  21844. ("micro")
  21845. ("para")
  21846. ("middot")
  21847. ("odot"."o")
  21848. ("star"."*")
  21849. ("cedil")
  21850. ("sup1")
  21851. ("ordm")
  21852. ("raquo")
  21853. ("frac14")
  21854. ("frac12")
  21855. ("frac34")
  21856. ("iquest")
  21857. ("Agrave")
  21858. ("Aacute")
  21859. ("Acirc")
  21860. ("Atilde")
  21861. ("Auml")
  21862. ("Aring") ("AA"."&Aring;")
  21863. ("AElig")
  21864. ("Ccedil")
  21865. ("Egrave")
  21866. ("Eacute")
  21867. ("Ecirc")
  21868. ("Euml")
  21869. ("Igrave")
  21870. ("Iacute")
  21871. ("Icirc")
  21872. ("Iuml")
  21873. ("ETH")
  21874. ("Ntilde")
  21875. ("Ograve")
  21876. ("Oacute")
  21877. ("Ocirc")
  21878. ("Otilde")
  21879. ("Ouml")
  21880. ("times")
  21881. ("Oslash")
  21882. ("Ugrave")
  21883. ("Uacute")
  21884. ("Ucirc")
  21885. ("Uuml")
  21886. ("Yacute")
  21887. ("THORN")
  21888. ("szlig")
  21889. ("agrave")
  21890. ("aacute")
  21891. ("acirc")
  21892. ("atilde")
  21893. ("auml")
  21894. ("aring")
  21895. ("aelig")
  21896. ("ccedil")
  21897. ("egrave")
  21898. ("eacute")
  21899. ("ecirc")
  21900. ("euml")
  21901. ("igrave")
  21902. ("iacute")
  21903. ("icirc")
  21904. ("iuml")
  21905. ("eth")
  21906. ("ntilde")
  21907. ("ograve")
  21908. ("oacute")
  21909. ("ocirc")
  21910. ("otilde")
  21911. ("ouml")
  21912. ("divide")
  21913. ("oslash")
  21914. ("ugrave")
  21915. ("uacute")
  21916. ("ucirc")
  21917. ("uuml")
  21918. ("yacute")
  21919. ("thorn")
  21920. ("yuml")
  21921. ("fnof")
  21922. ("Alpha")
  21923. ("Beta")
  21924. ("Gamma")
  21925. ("Delta")
  21926. ("Epsilon")
  21927. ("Zeta")
  21928. ("Eta")
  21929. ("Theta")
  21930. ("Iota")
  21931. ("Kappa")
  21932. ("Lambda")
  21933. ("Mu")
  21934. ("Nu")
  21935. ("Xi")
  21936. ("Omicron")
  21937. ("Pi")
  21938. ("Rho")
  21939. ("Sigma")
  21940. ("Tau")
  21941. ("Upsilon")
  21942. ("Phi")
  21943. ("Chi")
  21944. ("Psi")
  21945. ("Omega")
  21946. ("alpha")
  21947. ("beta")
  21948. ("gamma")
  21949. ("delta")
  21950. ("epsilon")
  21951. ("varepsilon"."&epsilon;")
  21952. ("zeta")
  21953. ("eta")
  21954. ("theta")
  21955. ("iota")
  21956. ("kappa")
  21957. ("lambda")
  21958. ("mu")
  21959. ("nu")
  21960. ("xi")
  21961. ("omicron")
  21962. ("pi")
  21963. ("rho")
  21964. ("sigmaf") ("varsigma"."&sigmaf;")
  21965. ("sigma")
  21966. ("tau")
  21967. ("upsilon")
  21968. ("phi")
  21969. ("chi")
  21970. ("psi")
  21971. ("omega")
  21972. ("thetasym") ("vartheta"."&thetasym;")
  21973. ("upsih")
  21974. ("piv")
  21975. ("bull") ("bullet"."&bull;")
  21976. ("hellip") ("dots"."&hellip;")
  21977. ("prime")
  21978. ("Prime")
  21979. ("oline")
  21980. ("frasl")
  21981. ("weierp")
  21982. ("image")
  21983. ("real")
  21984. ("trade")
  21985. ("alefsym")
  21986. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21987. ("uarr") ("uparrow"."&uarr;")
  21988. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21989. ("darr")("downarrow"."&darr;")
  21990. ("harr") ("leftrightarrow"."&harr;")
  21991. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21992. ("lArr") ("Leftarrow"."&lArr;")
  21993. ("uArr") ("Uparrow"."&uArr;")
  21994. ("rArr") ("Rightarrow"."&rArr;")
  21995. ("dArr") ("Downarrow"."&dArr;")
  21996. ("hArr") ("Leftrightarrow"."&hArr;")
  21997. ("forall")
  21998. ("part") ("partial"."&part;")
  21999. ("exist") ("exists"."&exist;")
  22000. ("empty") ("emptyset"."&empty;")
  22001. ("nabla")
  22002. ("isin") ("in"."&isin;")
  22003. ("notin")
  22004. ("ni")
  22005. ("prod")
  22006. ("sum")
  22007. ("minus")
  22008. ("lowast") ("ast"."&lowast;")
  22009. ("radic")
  22010. ("prop") ("proptp"."&prop;")
  22011. ("infin") ("infty"."&infin;")
  22012. ("ang") ("angle"."&ang;")
  22013. ("and") ("wedge"."&and;")
  22014. ("or") ("vee"."&or;")
  22015. ("cap")
  22016. ("cup")
  22017. ("int")
  22018. ("there4")
  22019. ("sim")
  22020. ("cong") ("simeq"."&cong;")
  22021. ("asymp")("approx"."&asymp;")
  22022. ("ne") ("neq"."&ne;")
  22023. ("equiv")
  22024. ("le")
  22025. ("ge")
  22026. ("sub") ("subset"."&sub;")
  22027. ("sup") ("supset"."&sup;")
  22028. ("nsub")
  22029. ("sube")
  22030. ("supe")
  22031. ("oplus")
  22032. ("otimes")
  22033. ("perp")
  22034. ("sdot") ("cdot"."&sdot;")
  22035. ("lceil")
  22036. ("rceil")
  22037. ("lfloor")
  22038. ("rfloor")
  22039. ("lang")
  22040. ("rang")
  22041. ("loz") ("Diamond"."&loz;")
  22042. ("spades") ("spadesuit"."&spades;")
  22043. ("clubs") ("clubsuit"."&clubs;")
  22044. ("hearts") ("diamondsuit"."&hearts;")
  22045. ("diams") ("diamondsuit"."&diams;")
  22046. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22047. ("quot")
  22048. ("amp")
  22049. ("lt")
  22050. ("gt")
  22051. ("OElig")
  22052. ("oelig")
  22053. ("Scaron")
  22054. ("scaron")
  22055. ("Yuml")
  22056. ("circ")
  22057. ("tilde")
  22058. ("ensp")
  22059. ("emsp")
  22060. ("thinsp")
  22061. ("zwnj")
  22062. ("zwj")
  22063. ("lrm")
  22064. ("rlm")
  22065. ("ndash")
  22066. ("mdash")
  22067. ("lsquo")
  22068. ("rsquo")
  22069. ("sbquo")
  22070. ("ldquo")
  22071. ("rdquo")
  22072. ("bdquo")
  22073. ("dagger")
  22074. ("Dagger")
  22075. ("permil")
  22076. ("lsaquo")
  22077. ("rsaquo")
  22078. ("euro")
  22079. ("arccos"."arccos")
  22080. ("arcsin"."arcsin")
  22081. ("arctan"."arctan")
  22082. ("arg"."arg")
  22083. ("cos"."cos")
  22084. ("cosh"."cosh")
  22085. ("cot"."cot")
  22086. ("coth"."coth")
  22087. ("csc"."csc")
  22088. ("deg"."deg")
  22089. ("det"."det")
  22090. ("dim"."dim")
  22091. ("exp"."exp")
  22092. ("gcd"."gcd")
  22093. ("hom"."hom")
  22094. ("inf"."inf")
  22095. ("ker"."ker")
  22096. ("lg"."lg")
  22097. ("lim"."lim")
  22098. ("liminf"."liminf")
  22099. ("limsup"."limsup")
  22100. ("ln"."ln")
  22101. ("log"."log")
  22102. ("max"."max")
  22103. ("min"."min")
  22104. ("Pr"."Pr")
  22105. ("sec"."sec")
  22106. ("sin"."sin")
  22107. ("sinh"."sinh")
  22108. ("sup"."sup")
  22109. ("tan"."tan")
  22110. ("tanh"."tanh")
  22111. )
  22112. "Entities for TeX->HTML translation.
  22113. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22114. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22115. In that case, \"\\ent\" will be translated to \"&other;\".
  22116. The list contains HTML entities for Latin-1, Greek and other symbols.
  22117. It is supplemented by a number of commonly used TeX macros with appropriate
  22118. translations. There is currently no way for users to extend this.")
  22119. ;;; General functions for all backends
  22120. (defun org-cleaned-string-for-export (string &rest parameters)
  22121. "Cleanup a buffer STRING so that links can be created safely."
  22122. (interactive)
  22123. (let* ((re-radio (and org-target-link-regexp
  22124. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22125. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22126. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22127. (re-archive (concat ":" org-archive-tag ":"))
  22128. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22129. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22130. (htmlp (plist-get parameters :for-html))
  22131. (asciip (plist-get parameters :for-ascii))
  22132. (latexp (plist-get parameters :for-LaTeX))
  22133. (commentsp (plist-get parameters :comments))
  22134. (archived-trees (plist-get parameters :archived-trees))
  22135. (inhibit-read-only t)
  22136. (drawers org-drawers)
  22137. (exp-drawers (plist-get parameters :drawers))
  22138. (outline-regexp "\\*+ ")
  22139. a b xx
  22140. rtn p)
  22141. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22142. (erase-buffer)
  22143. (insert string)
  22144. ;; Remove license-to-kill stuff
  22145. (while (setq p (text-property-any (point-min) (point-max)
  22146. :org-license-to-kill t))
  22147. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22148. (let ((org-inhibit-startup t)) (org-mode))
  22149. (untabify (point-min) (point-max))
  22150. ;; Get rid of drawers
  22151. (unless (eq t exp-drawers)
  22152. (goto-char (point-min))
  22153. (let ((re (concat "^[ \t]*:\\("
  22154. (mapconcat
  22155. 'identity
  22156. (org-delete-all exp-drawers
  22157. (copy-sequence drawers))
  22158. "\\|")
  22159. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22160. (while (re-search-forward re nil t)
  22161. (replace-match ""))))
  22162. ;; Get the correct stuff before the first headline
  22163. (when (plist-get parameters :skip-before-1st-heading)
  22164. (goto-char (point-min))
  22165. (when (re-search-forward "^\\*+[ \t]" nil t)
  22166. (delete-region (point-min) (match-beginning 0))
  22167. (goto-char (point-min))
  22168. (insert "\n")))
  22169. (when (plist-get parameters :add-text)
  22170. (goto-char (point-min))
  22171. (insert (plist-get parameters :add-text) "\n"))
  22172. ;; Get rid of archived trees
  22173. (when (not (eq archived-trees t))
  22174. (goto-char (point-min))
  22175. (while (re-search-forward re-archive nil t)
  22176. (if (not (org-on-heading-p t))
  22177. (org-end-of-subtree t)
  22178. (beginning-of-line 1)
  22179. (setq a (if archived-trees
  22180. (1+ (point-at-eol)) (point))
  22181. b (org-end-of-subtree t))
  22182. (if (> b a) (delete-region a b)))))
  22183. ;; Find targets in comments and move them out of comments,
  22184. ;; but mark them as targets that should be invisible
  22185. (goto-char (point-min))
  22186. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22187. (replace-match "\\1(INVISIBLE)"))
  22188. ;; Protect backend specific stuff, throw away the others.
  22189. (let ((formatters
  22190. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22191. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22192. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22193. fmt)
  22194. (goto-char (point-min))
  22195. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22196. (goto-char (match-end 0))
  22197. (while (not (looking-at "#\\+END_EXAMPLE"))
  22198. (insert ": ")
  22199. (beginning-of-line 2)))
  22200. (goto-char (point-min))
  22201. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22202. (add-text-properties (match-beginning 0) (match-end 0)
  22203. '(org-protected t)))
  22204. (while formatters
  22205. (setq fmt (pop formatters))
  22206. (when (car fmt)
  22207. (goto-char (point-min))
  22208. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22209. ":[ \t]*\\(.*\\)") nil t)
  22210. (replace-match "\\1" t)
  22211. (add-text-properties
  22212. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22213. '(org-protected t))))
  22214. (goto-char (point-min))
  22215. (while (re-search-forward
  22216. (concat "^#\\+"
  22217. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22218. (cadddr fmt) "\\>.*\n?") nil t)
  22219. (if (car fmt)
  22220. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22221. '(org-protected t))
  22222. (delete-region (match-beginning 0) (match-end 0))))))
  22223. ;; Protect quoted subtrees
  22224. (goto-char (point-min))
  22225. (while (re-search-forward re-quote nil t)
  22226. (goto-char (match-beginning 0))
  22227. (end-of-line 1)
  22228. (add-text-properties (point) (org-end-of-subtree t)
  22229. '(org-protected t)))
  22230. ;; Protect verbatim elements
  22231. (goto-char (point-min))
  22232. (while (re-search-forward org-verbatim-re nil t)
  22233. (add-text-properties (match-beginning 4) (match-end 4)
  22234. '(org-protected t))
  22235. (goto-char (1+ (match-end 4))))
  22236. ;; Remove subtrees that are commented
  22237. (goto-char (point-min))
  22238. (while (re-search-forward re-commented nil t)
  22239. (goto-char (match-beginning 0))
  22240. (delete-region (point) (org-end-of-subtree t)))
  22241. ;; Remove special table lines
  22242. (when org-export-table-remove-special-lines
  22243. (goto-char (point-min))
  22244. (while (re-search-forward "^[ \t]*|" nil t)
  22245. (beginning-of-line 1)
  22246. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22247. (and (looking-at ".*?| *<[0-9]+> *|")
  22248. (not (looking-at ".*?| *[^ <|]"))))
  22249. (delete-region (max (point-min) (1- (point-at-bol)))
  22250. (point-at-eol))
  22251. (end-of-line 1))))
  22252. ;; Specific LaTeX stuff
  22253. (when latexp
  22254. (require 'org-export-latex nil)
  22255. (org-export-latex-cleaned-string))
  22256. (when asciip
  22257. (org-export-ascii-clean-string))
  22258. ;; Specific HTML stuff
  22259. (when htmlp
  22260. ;; Convert LaTeX fragments to images
  22261. (when (plist-get parameters :LaTeX-fragments)
  22262. (org-format-latex
  22263. (concat "ltxpng/" (file-name-sans-extension
  22264. (file-name-nondirectory
  22265. org-current-export-file)))
  22266. org-current-export-dir nil "Creating LaTeX image %s"))
  22267. (message "Exporting..."))
  22268. ;; Remove or replace comments
  22269. (goto-char (point-min))
  22270. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22271. (if commentsp
  22272. (progn (add-text-properties
  22273. (match-beginning 0) (match-end 0) '(org-protected t))
  22274. (replace-match (format commentsp (match-string 1)) t t))
  22275. (replace-match "")))
  22276. ;; Find matches for radio targets and turn them into internal links
  22277. (goto-char (point-min))
  22278. (when re-radio
  22279. (while (re-search-forward re-radio nil t)
  22280. (org-if-unprotected
  22281. (replace-match "\\1[[\\2]]"))))
  22282. ;; Find all links that contain a newline and put them into a single line
  22283. (goto-char (point-min))
  22284. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22285. (org-if-unprotected
  22286. (replace-match "\\1 \\3")
  22287. (goto-char (match-beginning 0))))
  22288. ;; Normalize links: Convert angle and plain links into bracket links
  22289. ;; Expand link abbreviations
  22290. (goto-char (point-min))
  22291. (while (re-search-forward re-plain-link nil t)
  22292. (goto-char (1- (match-end 0)))
  22293. (org-if-unprotected
  22294. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22295. ":" (match-string 3) "]]")))
  22296. ;; added 'org-link face to links
  22297. (put-text-property 0 (length s) 'face 'org-link s)
  22298. (replace-match s t t))))
  22299. (goto-char (point-min))
  22300. (while (re-search-forward re-angle-link nil t)
  22301. (goto-char (1- (match-end 0)))
  22302. (org-if-unprotected
  22303. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22304. ":" (match-string 3) "]]")))
  22305. (put-text-property 0 (length s) 'face 'org-link s)
  22306. (replace-match s t t))))
  22307. (goto-char (point-min))
  22308. (while (re-search-forward org-bracket-link-regexp nil t)
  22309. (org-if-unprotected
  22310. (let* ((s (concat "[[" (setq xx (save-match-data
  22311. (org-link-expand-abbrev (match-string 1))))
  22312. "]"
  22313. (if (match-end 3)
  22314. (match-string 2)
  22315. (concat "[" xx "]"))
  22316. "]")))
  22317. (put-text-property 0 (length s) 'face 'org-link s)
  22318. (replace-match s t t))))
  22319. ;; Find multiline emphasis and put them into single line
  22320. (when (plist-get parameters :emph-multiline)
  22321. (goto-char (point-min))
  22322. (while (re-search-forward org-emph-re nil t)
  22323. (if (not (= (char-after (match-beginning 3))
  22324. (char-after (match-beginning 4))))
  22325. (org-if-unprotected
  22326. (subst-char-in-region (match-beginning 0) (match-end 0)
  22327. ?\n ?\ t)
  22328. (goto-char (1- (match-end 0))))
  22329. (goto-char (1+ (match-beginning 0))))))
  22330. (setq rtn (buffer-string)))
  22331. (kill-buffer " org-mode-tmp")
  22332. rtn))
  22333. (defun org-export-grab-title-from-buffer ()
  22334. "Get a title for the current document, from looking at the buffer."
  22335. (let ((inhibit-read-only t))
  22336. (save-excursion
  22337. (goto-char (point-min))
  22338. (let ((end (save-excursion (outline-next-heading) (point))))
  22339. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22340. ;; Mark the line so that it will not be exported as normal text.
  22341. (org-unmodified
  22342. (add-text-properties (match-beginning 0) (match-end 0)
  22343. (list :org-license-to-kill t)))
  22344. ;; Return the title string
  22345. (org-trim (match-string 0)))))))
  22346. (defun org-export-get-title-from-subtree ()
  22347. "Return subtree title and exclude it from export."
  22348. (let (title (m (mark)))
  22349. (save-excursion
  22350. (goto-char (region-beginning))
  22351. (when (and (org-at-heading-p)
  22352. (>= (org-end-of-subtree t t) (region-end)))
  22353. ;; This is a subtree, we take the title from the first heading
  22354. (goto-char (region-beginning))
  22355. (looking-at org-todo-line-regexp)
  22356. (setq title (match-string 3))
  22357. (org-unmodified
  22358. (add-text-properties (point) (1+ (point-at-eol))
  22359. (list :org-license-to-kill t)))))
  22360. title))
  22361. (defun org-solidify-link-text (s &optional alist)
  22362. "Take link text and make a safe target out of it."
  22363. (save-match-data
  22364. (let* ((rtn
  22365. (mapconcat
  22366. 'identity
  22367. (org-split-string s "[ \t\r\n]+") "--"))
  22368. (a (assoc rtn alist)))
  22369. (or (cdr a) rtn))))
  22370. (defun org-get-min-level (lines)
  22371. "Get the minimum level in LINES."
  22372. (let ((re "^\\(\\*+\\) ") l min)
  22373. (catch 'exit
  22374. (while (setq l (pop lines))
  22375. (if (string-match re l)
  22376. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22377. 1)))
  22378. ;; Variable holding the vector with section numbers
  22379. (defvar org-section-numbers (make-vector org-level-max 0))
  22380. (defun org-init-section-numbers ()
  22381. "Initialize the vector for the section numbers."
  22382. (let* ((level -1)
  22383. (numbers (nreverse (org-split-string "" "\\.")))
  22384. (depth (1- (length org-section-numbers)))
  22385. (i depth) number-string)
  22386. (while (>= i 0)
  22387. (if (> i level)
  22388. (aset org-section-numbers i 0)
  22389. (setq number-string (or (car numbers) "0"))
  22390. (if (string-match "\\`[A-Z]\\'" number-string)
  22391. (aset org-section-numbers i
  22392. (- (string-to-char number-string) ?A -1))
  22393. (aset org-section-numbers i (string-to-number number-string)))
  22394. (pop numbers))
  22395. (setq i (1- i)))))
  22396. (defun org-section-number (&optional level)
  22397. "Return a string with the current section number.
  22398. When LEVEL is non-nil, increase section numbers on that level."
  22399. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22400. (when level
  22401. (when (> level -1)
  22402. (aset org-section-numbers
  22403. level (1+ (aref org-section-numbers level))))
  22404. (setq idx (1+ level))
  22405. (while (<= idx depth)
  22406. (if (not (= idx 1))
  22407. (aset org-section-numbers idx 0))
  22408. (setq idx (1+ idx))))
  22409. (setq idx 0)
  22410. (while (<= idx depth)
  22411. (setq n (aref org-section-numbers idx))
  22412. (setq string (concat string (if (not (string= string "")) "." "")
  22413. (int-to-string n)))
  22414. (setq idx (1+ idx)))
  22415. (save-match-data
  22416. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22417. (setq string (replace-match "" t nil string)))
  22418. (if (string-match "\\(\\.0\\)+\\'" string)
  22419. (setq string (replace-match "" t nil string))))
  22420. string))
  22421. ;;; ASCII export
  22422. (defvar org-last-level nil) ; dynamically scoped variable
  22423. (defvar org-min-level nil) ; dynamically scoped variable
  22424. (defvar org-levels-open nil) ; dynamically scoped parameter
  22425. (defvar org-ascii-current-indentation nil) ; For communication
  22426. (defun org-export-as-ascii (arg)
  22427. "Export the outline as a pretty ASCII file.
  22428. If there is an active region, export only the region.
  22429. The prefix ARG specifies how many levels of the outline should become
  22430. underlined headlines. The default is 3."
  22431. (interactive "P")
  22432. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22433. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22434. (org-infile-export-plist)))
  22435. (region-p (org-region-active-p))
  22436. (subtree-p
  22437. (when region-p
  22438. (save-excursion
  22439. (goto-char (region-beginning))
  22440. (and (org-at-heading-p)
  22441. (>= (org-end-of-subtree t t) (region-end))))))
  22442. (custom-times org-display-custom-times)
  22443. (org-ascii-current-indentation '(0 . 0))
  22444. (level 0) line txt
  22445. (umax nil)
  22446. (umax-toc nil)
  22447. (case-fold-search nil)
  22448. (filename (concat (file-name-as-directory
  22449. (org-export-directory :ascii opt-plist))
  22450. (file-name-sans-extension
  22451. (or (and subtree-p
  22452. (org-entry-get (region-beginning)
  22453. "EXPORT_FILE_NAME" t))
  22454. (file-name-nondirectory buffer-file-name)))
  22455. ".txt"))
  22456. (filename (if (equal (file-truename filename)
  22457. (file-truename buffer-file-name))
  22458. (concat filename ".txt")
  22459. filename))
  22460. (buffer (find-file-noselect filename))
  22461. (org-levels-open (make-vector org-level-max nil))
  22462. (odd org-odd-levels-only)
  22463. (date (plist-get opt-plist :date))
  22464. (author (plist-get opt-plist :author))
  22465. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22466. (plist-get opt-plist :title)
  22467. (and (not
  22468. (plist-get opt-plist :skip-before-1st-heading))
  22469. (org-export-grab-title-from-buffer))
  22470. (file-name-sans-extension
  22471. (file-name-nondirectory buffer-file-name))))
  22472. (email (plist-get opt-plist :email))
  22473. (language (plist-get opt-plist :language))
  22474. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22475. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22476. (todo nil)
  22477. (lang-words nil)
  22478. (region
  22479. (buffer-substring
  22480. (if (org-region-active-p) (region-beginning) (point-min))
  22481. (if (org-region-active-p) (region-end) (point-max))))
  22482. (lines (org-split-string
  22483. (org-cleaned-string-for-export
  22484. region
  22485. :for-ascii t
  22486. :skip-before-1st-heading
  22487. (plist-get opt-plist :skip-before-1st-heading)
  22488. :drawers (plist-get opt-plist :drawers)
  22489. :verbatim-multiline t
  22490. :archived-trees
  22491. (plist-get opt-plist :archived-trees)
  22492. :add-text (plist-get opt-plist :text))
  22493. "\n"))
  22494. thetoc have-headings first-heading-pos
  22495. table-open table-buffer)
  22496. (let ((inhibit-read-only t))
  22497. (org-unmodified
  22498. (remove-text-properties (point-min) (point-max)
  22499. '(:org-license-to-kill t))))
  22500. (setq org-min-level (org-get-min-level lines))
  22501. (setq org-last-level org-min-level)
  22502. (org-init-section-numbers)
  22503. (find-file-noselect filename)
  22504. (setq lang-words (or (assoc language org-export-language-setup)
  22505. (assoc "en" org-export-language-setup)))
  22506. (switch-to-buffer-other-window buffer)
  22507. (erase-buffer)
  22508. (fundamental-mode)
  22509. ;; create local variables for all options, to make sure all called
  22510. ;; functions get the correct information
  22511. (mapc (lambda (x)
  22512. (set (make-local-variable (cdr x))
  22513. (plist-get opt-plist (car x))))
  22514. org-export-plist-vars)
  22515. (org-set-local 'org-odd-levels-only odd)
  22516. (setq umax (if arg (prefix-numeric-value arg)
  22517. org-export-headline-levels))
  22518. (setq umax-toc (if (integerp org-export-with-toc)
  22519. (min org-export-with-toc umax)
  22520. umax))
  22521. ;; File header
  22522. (if title (org-insert-centered title ?=))
  22523. (insert "\n")
  22524. (if (and (or author email)
  22525. org-export-author-info)
  22526. (insert (concat (nth 1 lang-words) ": " (or author "")
  22527. (if email (concat " <" email ">") "")
  22528. "\n")))
  22529. (cond
  22530. ((and date (string-match "%" date))
  22531. (setq date (format-time-string date (current-time))))
  22532. (date)
  22533. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22534. (if (and date org-export-time-stamp-file)
  22535. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22536. (insert "\n\n")
  22537. (if org-export-with-toc
  22538. (progn
  22539. (push (concat (nth 3 lang-words) "\n") thetoc)
  22540. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22541. (mapc '(lambda (line)
  22542. (if (string-match org-todo-line-regexp
  22543. line)
  22544. ;; This is a headline
  22545. (progn
  22546. (setq have-headings t)
  22547. (setq level (- (match-end 1) (match-beginning 1))
  22548. level (org-tr-level level)
  22549. txt (match-string 3 line)
  22550. todo
  22551. (or (and org-export-mark-todo-in-toc
  22552. (match-beginning 2)
  22553. (not (member (match-string 2 line)
  22554. org-done-keywords)))
  22555. ; TODO, not DONE
  22556. (and org-export-mark-todo-in-toc
  22557. (= level umax-toc)
  22558. (org-search-todo-below
  22559. line lines level))))
  22560. (setq txt (org-html-expand-for-ascii txt))
  22561. (while (string-match org-bracket-link-regexp txt)
  22562. (setq txt
  22563. (replace-match
  22564. (match-string (if (match-end 2) 3 1) txt)
  22565. t t txt)))
  22566. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22567. (string-match
  22568. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22569. txt))
  22570. (setq txt (replace-match "" t t txt)))
  22571. (if (string-match quote-re0 txt)
  22572. (setq txt (replace-match "" t t txt)))
  22573. (if org-export-with-section-numbers
  22574. (setq txt (concat (org-section-number level)
  22575. " " txt)))
  22576. (if (<= level umax-toc)
  22577. (progn
  22578. (push
  22579. (concat
  22580. (make-string
  22581. (* (max 0 (- level org-min-level)) 4) ?\ )
  22582. (format (if todo "%s (*)\n" "%s\n") txt))
  22583. thetoc)
  22584. (setq org-last-level level))
  22585. ))))
  22586. lines)
  22587. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22588. (org-init-section-numbers)
  22589. (while (setq line (pop lines))
  22590. ;; Remove the quoted HTML tags.
  22591. (setq line (org-html-expand-for-ascii line))
  22592. ;; Remove targets
  22593. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22594. (setq line (replace-match "" t t line)))
  22595. ;; Replace internal links
  22596. (while (string-match org-bracket-link-regexp line)
  22597. (setq line (replace-match
  22598. (if (match-end 3) "[\\3]" "[\\1]")
  22599. t nil line)))
  22600. (when custom-times
  22601. (setq line (org-translate-time line)))
  22602. (cond
  22603. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22604. ;; a Headline
  22605. (setq first-heading-pos (or first-heading-pos (point)))
  22606. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22607. txt (match-string 2 line))
  22608. (org-ascii-level-start level txt umax lines))
  22609. ((and org-export-with-tables
  22610. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22611. (if (not table-open)
  22612. ;; New table starts
  22613. (setq table-open t table-buffer nil))
  22614. ;; Accumulate lines
  22615. (setq table-buffer (cons line table-buffer))
  22616. (when (or (not lines)
  22617. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22618. (car lines))))
  22619. (setq table-open nil
  22620. table-buffer (nreverse table-buffer))
  22621. (insert (mapconcat
  22622. (lambda (x)
  22623. (org-fix-indentation x org-ascii-current-indentation))
  22624. (org-format-table-ascii table-buffer)
  22625. "\n") "\n")))
  22626. (t
  22627. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22628. (if (and org-export-with-fixed-width
  22629. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22630. (setq line (replace-match "\\1" nil nil line)))
  22631. (insert line "\n"))))
  22632. (normal-mode)
  22633. ;; insert the table of contents
  22634. (when thetoc
  22635. (goto-char (point-min))
  22636. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22637. (progn
  22638. (goto-char (match-beginning 0))
  22639. (replace-match ""))
  22640. (goto-char first-heading-pos))
  22641. (mapc 'insert thetoc)
  22642. (or (looking-at "[ \t]*\n[ \t]*\n")
  22643. (insert "\n\n")))
  22644. ;; Convert whitespace place holders
  22645. (goto-char (point-min))
  22646. (let (beg end)
  22647. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22648. (setq end (next-single-property-change beg 'org-whitespace))
  22649. (goto-char beg)
  22650. (delete-region beg end)
  22651. (insert (make-string (- end beg) ?\ ))))
  22652. (save-buffer)
  22653. ;; remove display and invisible chars
  22654. (let (beg end)
  22655. (goto-char (point-min))
  22656. (while (setq beg (next-single-property-change (point) 'display))
  22657. (setq end (next-single-property-change beg 'display))
  22658. (delete-region beg end)
  22659. (goto-char beg)
  22660. (insert "=>"))
  22661. (goto-char (point-min))
  22662. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22663. (setq end (next-single-property-change beg 'org-cwidth))
  22664. (delete-region beg end)
  22665. (goto-char beg)))
  22666. (goto-char (point-min))))
  22667. (defun org-export-ascii-clean-string ()
  22668. "Do extra work for ASCII export"
  22669. (goto-char (point-min))
  22670. (while (re-search-forward org-verbatim-re nil t)
  22671. (goto-char (match-end 2))
  22672. (backward-delete-char 1) (insert "'")
  22673. (goto-char (match-beginning 2))
  22674. (delete-char 1) (insert "`")
  22675. (goto-char (match-end 2))))
  22676. (defun org-search-todo-below (line lines level)
  22677. "Search the subtree below LINE for any TODO entries."
  22678. (let ((rest (cdr (memq line lines)))
  22679. (re org-todo-line-regexp)
  22680. line lv todo)
  22681. (catch 'exit
  22682. (while (setq line (pop rest))
  22683. (if (string-match re line)
  22684. (progn
  22685. (setq lv (- (match-end 1) (match-beginning 1))
  22686. todo (and (match-beginning 2)
  22687. (not (member (match-string 2 line)
  22688. org-done-keywords))))
  22689. ; TODO, not DONE
  22690. (if (<= lv level) (throw 'exit nil))
  22691. (if todo (throw 'exit t))))))))
  22692. (defun org-html-expand-for-ascii (line)
  22693. "Handle quoted HTML for ASCII export."
  22694. (if org-export-html-expand
  22695. (while (string-match "@<[^<>\n]*>" line)
  22696. ;; We just remove the tags for now.
  22697. (setq line (replace-match "" nil nil line))))
  22698. line)
  22699. (defun org-insert-centered (s &optional underline)
  22700. "Insert the string S centered and underline it with character UNDERLINE."
  22701. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22702. (insert (make-string ind ?\ ) s "\n")
  22703. (if underline
  22704. (insert (make-string ind ?\ )
  22705. (make-string (string-width s) underline)
  22706. "\n"))))
  22707. (defun org-ascii-level-start (level title umax &optional lines)
  22708. "Insert a new level in ASCII export."
  22709. (let (char (n (- level umax 1)) (ind 0))
  22710. (if (> level umax)
  22711. (progn
  22712. (insert (make-string (* 2 n) ?\ )
  22713. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22714. org-export-ascii-bullets))
  22715. " " title "\n")
  22716. ;; find the indentation of the next non-empty line
  22717. (catch 'stop
  22718. (while lines
  22719. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22720. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22721. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22722. (pop lines)))
  22723. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22724. (if (or (not (equal (char-before) ?\n))
  22725. (not (equal (char-before (1- (point))) ?\n)))
  22726. (insert "\n"))
  22727. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22728. (unless org-export-with-tags
  22729. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22730. (setq title (replace-match "" t t title))))
  22731. (if org-export-with-section-numbers
  22732. (setq title (concat (org-section-number level) " " title)))
  22733. (insert title "\n" (make-string (string-width title) char) "\n")
  22734. (setq org-ascii-current-indentation '(0 . 0)))))
  22735. (defun org-export-visible (type arg)
  22736. "Create a copy of the visible part of the current buffer, and export it.
  22737. The copy is created in a temporary buffer and removed after use.
  22738. TYPE is the final key (as a string) that also select the export command in
  22739. the `C-c C-e' export dispatcher.
  22740. As a special case, if the you type SPC at the prompt, the temporary
  22741. org-mode file will not be removed but presented to you so that you can
  22742. continue to use it. The prefix arg ARG is passed through to the exporting
  22743. command."
  22744. (interactive
  22745. (list (progn
  22746. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22747. (read-char-exclusive))
  22748. current-prefix-arg))
  22749. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22750. (error "Invalid export key"))
  22751. (let* ((binding (cdr (assoc type
  22752. '((?a . org-export-as-ascii)
  22753. (?\C-a . org-export-as-ascii)
  22754. (?b . org-export-as-html-and-open)
  22755. (?\C-b . org-export-as-html-and-open)
  22756. (?h . org-export-as-html)
  22757. (?H . org-export-as-html-to-buffer)
  22758. (?R . org-export-region-as-html)
  22759. (?x . org-export-as-xoxo)))))
  22760. (keepp (equal type ?\ ))
  22761. (file buffer-file-name)
  22762. (buffer (get-buffer-create "*Org Export Visible*"))
  22763. s e)
  22764. ;; Need to hack the drawers here.
  22765. (save-excursion
  22766. (goto-char (point-min))
  22767. (while (re-search-forward org-drawer-regexp nil t)
  22768. (goto-char (match-beginning 1))
  22769. (or (org-invisible-p) (org-flag-drawer nil))))
  22770. (with-current-buffer buffer (erase-buffer))
  22771. (save-excursion
  22772. (setq s (goto-char (point-min)))
  22773. (while (not (= (point) (point-max)))
  22774. (goto-char (org-find-invisible))
  22775. (append-to-buffer buffer s (point))
  22776. (setq s (goto-char (org-find-visible))))
  22777. (org-cycle-hide-drawers 'all)
  22778. (goto-char (point-min))
  22779. (unless keepp
  22780. ;; Copy all comment lines to the end, to make sure #+ settings are
  22781. ;; still available for the second export step. Kind of a hack, but
  22782. ;; does do the trick.
  22783. (if (looking-at "#[^\r\n]*")
  22784. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22785. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22786. (append-to-buffer buffer (1+ (match-beginning 0))
  22787. (min (point-max) (1+ (match-end 0))))))
  22788. (set-buffer buffer)
  22789. (let ((buffer-file-name file)
  22790. (org-inhibit-startup t))
  22791. (org-mode)
  22792. (show-all)
  22793. (unless keepp (funcall binding arg))))
  22794. (if (not keepp)
  22795. (kill-buffer buffer)
  22796. (switch-to-buffer-other-window buffer)
  22797. (goto-char (point-min)))))
  22798. (defun org-find-visible ()
  22799. (let ((s (point)))
  22800. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22801. (get-char-property s 'invisible)))
  22802. s))
  22803. (defun org-find-invisible ()
  22804. (let ((s (point)))
  22805. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22806. (not (get-char-property s 'invisible))))
  22807. s))
  22808. ;;; HTML export
  22809. (defun org-get-current-options ()
  22810. "Return a string with current options as keyword options.
  22811. Does include HTML export options as well as TODO and CATEGORY stuff."
  22812. (format
  22813. "#+TITLE: %s
  22814. #+AUTHOR: %s
  22815. #+EMAIL: %s
  22816. #+LANGUAGE: %s
  22817. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22818. #+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
  22819. #+CATEGORY: %s
  22820. #+SEQ_TODO: %s
  22821. #+TYP_TODO: %s
  22822. #+PRIORITIES: %c %c %c
  22823. #+DRAWERS: %s
  22824. #+STARTUP: %s %s %s %s %s
  22825. #+TAGS: %s
  22826. #+ARCHIVE: %s
  22827. #+LINK: %s
  22828. "
  22829. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22830. org-export-headline-levels
  22831. org-export-with-section-numbers
  22832. org-export-with-toc
  22833. org-export-preserve-breaks
  22834. org-export-html-expand
  22835. org-export-with-fixed-width
  22836. org-export-with-tables
  22837. org-export-with-sub-superscripts
  22838. org-export-with-special-strings
  22839. org-export-with-footnotes
  22840. org-export-with-emphasize
  22841. org-export-with-TeX-macros
  22842. org-export-with-LaTeX-fragments
  22843. org-export-skip-text-before-1st-heading
  22844. org-export-with-drawers
  22845. org-export-with-tags
  22846. (file-name-nondirectory buffer-file-name)
  22847. "TODO FEEDBACK VERIFY DONE"
  22848. "Me Jason Marie DONE"
  22849. org-highest-priority org-lowest-priority org-default-priority
  22850. (mapconcat 'identity org-drawers " ")
  22851. (cdr (assoc org-startup-folded
  22852. '((nil . "showall") (t . "overview") (content . "content"))))
  22853. (if org-odd-levels-only "odd" "oddeven")
  22854. (if org-hide-leading-stars "hidestars" "showstars")
  22855. (if org-startup-align-all-tables "align" "noalign")
  22856. (cond ((eq org-log-done t) "logdone")
  22857. ((equal org-log-done 'note) "lognotedone")
  22858. ((not org-log-done) "nologdone"))
  22859. (or (mapconcat (lambda (x)
  22860. (cond
  22861. ((equal '(:startgroup) x) "{")
  22862. ((equal '(:endgroup) x) "}")
  22863. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22864. (t (car x))))
  22865. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22866. org-archive-location
  22867. "org file:~/org/%s.org"
  22868. ))
  22869. (defun org-insert-export-options-template ()
  22870. "Insert into the buffer a template with information for exporting."
  22871. (interactive)
  22872. (if (not (bolp)) (newline))
  22873. (let ((s (org-get-current-options)))
  22874. (and (string-match "#\\+CATEGORY" s)
  22875. (setq s (substring s 0 (match-beginning 0))))
  22876. (insert s)))
  22877. (defun org-toggle-fixed-width-section (arg)
  22878. "Toggle the fixed-width export.
  22879. If there is no active region, the QUOTE keyword at the current headline is
  22880. inserted or removed. When present, it causes the text between this headline
  22881. and the next to be exported as fixed-width text, and unmodified.
  22882. If there is an active region, this command adds or removes a colon as the
  22883. first character of this line. If the first character of a line is a colon,
  22884. this line is also exported in fixed-width font."
  22885. (interactive "P")
  22886. (let* ((cc 0)
  22887. (regionp (org-region-active-p))
  22888. (beg (if regionp (region-beginning) (point)))
  22889. (end (if regionp (region-end)))
  22890. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22891. (case-fold-search nil)
  22892. (re "[ \t]*\\(:\\)")
  22893. off)
  22894. (if regionp
  22895. (save-excursion
  22896. (goto-char beg)
  22897. (setq cc (current-column))
  22898. (beginning-of-line 1)
  22899. (setq off (looking-at re))
  22900. (while (> nlines 0)
  22901. (setq nlines (1- nlines))
  22902. (beginning-of-line 1)
  22903. (cond
  22904. (arg
  22905. (move-to-column cc t)
  22906. (insert ":\n")
  22907. (forward-line -1))
  22908. ((and off (looking-at re))
  22909. (replace-match "" t t nil 1))
  22910. ((not off) (move-to-column cc t) (insert ":")))
  22911. (forward-line 1)))
  22912. (save-excursion
  22913. (org-back-to-heading)
  22914. (if (looking-at (concat outline-regexp
  22915. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22916. (replace-match "" t t nil 1)
  22917. (if (looking-at outline-regexp)
  22918. (progn
  22919. (goto-char (match-end 0))
  22920. (insert org-quote-string " "))))))))
  22921. (defun org-export-as-html-and-open (arg)
  22922. "Export the outline as HTML and immediately open it with a browser.
  22923. If there is an active region, export only the region.
  22924. The prefix ARG specifies how many levels of the outline should become
  22925. headlines. The default is 3. Lower levels will become bulleted lists."
  22926. (interactive "P")
  22927. (org-export-as-html arg 'hidden)
  22928. (org-open-file buffer-file-name))
  22929. (defun org-export-as-html-batch ()
  22930. "Call `org-export-as-html', may be used in batch processing as
  22931. emacs --batch
  22932. --load=$HOME/lib/emacs/org.el
  22933. --eval \"(setq org-export-headline-levels 2)\"
  22934. --visit=MyFile --funcall org-export-as-html-batch"
  22935. (org-export-as-html org-export-headline-levels 'hidden))
  22936. (defun org-export-as-html-to-buffer (arg)
  22937. "Call `org-exort-as-html` with output to a temporary buffer.
  22938. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22939. (interactive "P")
  22940. (org-export-as-html arg nil nil "*Org HTML Export*")
  22941. (switch-to-buffer-other-window "*Org HTML Export*"))
  22942. (defun org-replace-region-by-html (beg end)
  22943. "Assume the current region has org-mode syntax, and convert it to HTML.
  22944. This can be used in any buffer. For example, you could write an
  22945. itemized list in org-mode syntax in an HTML buffer and then use this
  22946. command to convert it."
  22947. (interactive "r")
  22948. (let (reg html buf pop-up-frames)
  22949. (save-window-excursion
  22950. (if (org-mode-p)
  22951. (setq html (org-export-region-as-html
  22952. beg end t 'string))
  22953. (setq reg (buffer-substring beg end)
  22954. buf (get-buffer-create "*Org tmp*"))
  22955. (with-current-buffer buf
  22956. (erase-buffer)
  22957. (insert reg)
  22958. (org-mode)
  22959. (setq html (org-export-region-as-html
  22960. (point-min) (point-max) t 'string)))
  22961. (kill-buffer buf)))
  22962. (delete-region beg end)
  22963. (insert html)))
  22964. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22965. "Convert region from BEG to END in org-mode buffer to HTML.
  22966. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22967. contents, and only produce the region of converted text, useful for
  22968. cut-and-paste operations.
  22969. If BUFFER is a buffer or a string, use/create that buffer as a target
  22970. of the converted HTML. If BUFFER is the symbol `string', return the
  22971. produced HTML as a string and leave not buffer behind. For example,
  22972. a Lisp program could call this function in the following way:
  22973. (setq html (org-export-region-as-html beg end t 'string))
  22974. When called interactively, the output buffer is selected, and shown
  22975. in a window. A non-interactive call will only retunr the buffer."
  22976. (interactive "r\nP")
  22977. (when (interactive-p)
  22978. (setq buffer "*Org HTML Export*"))
  22979. (let ((transient-mark-mode t) (zmacs-regions t)
  22980. rtn)
  22981. (goto-char end)
  22982. (set-mark (point)) ;; to activate the region
  22983. (goto-char beg)
  22984. (setq rtn (org-export-as-html
  22985. nil nil nil
  22986. buffer body-only))
  22987. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22988. (if (and (interactive-p) (bufferp rtn))
  22989. (switch-to-buffer-other-window rtn)
  22990. rtn)))
  22991. (defvar html-table-tag nil) ; dynamically scoped into this.
  22992. (defun org-export-as-html (arg &optional hidden ext-plist
  22993. to-buffer body-only)
  22994. "Export the outline as a pretty HTML file.
  22995. If there is an active region, export only the region. The prefix
  22996. ARG specifies how many levels of the outline should become
  22997. headlines. The default is 3. Lower levels will become bulleted
  22998. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22999. EXT-PLIST is a property list with external parameters overriding
  23000. org-mode's default settings, but still inferior to file-local
  23001. settings. When TO-BUFFER is non-nil, create a buffer with that
  23002. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23003. don't leave any buffer behind but just return the resulting HTML as
  23004. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23005. simply return the content of <body>...</body>, without even
  23006. the body tags themselves."
  23007. (interactive "P")
  23008. ;; Make sure we have a file name when we need it.
  23009. (when (and (not (or to-buffer body-only))
  23010. (not buffer-file-name))
  23011. (if (buffer-base-buffer)
  23012. (org-set-local 'buffer-file-name
  23013. (with-current-buffer (buffer-base-buffer)
  23014. buffer-file-name))
  23015. (error "Need a file name to be able to export.")))
  23016. (message "Exporting...")
  23017. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23018. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23019. (setq-default org-done-keywords org-done-keywords)
  23020. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23021. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23022. ext-plist
  23023. (org-infile-export-plist)))
  23024. (style (plist-get opt-plist :style))
  23025. (html-extension (plist-get opt-plist :html-extension))
  23026. (link-validate (plist-get opt-plist :link-validation-function))
  23027. valid thetoc have-headings first-heading-pos
  23028. (odd org-odd-levels-only)
  23029. (region-p (org-region-active-p))
  23030. (subtree-p
  23031. (when region-p
  23032. (save-excursion
  23033. (goto-char (region-beginning))
  23034. (and (org-at-heading-p)
  23035. (>= (org-end-of-subtree t t) (region-end))))))
  23036. ;; The following two are dynamically scoped into other
  23037. ;; routines below.
  23038. (org-current-export-dir (org-export-directory :html opt-plist))
  23039. (org-current-export-file buffer-file-name)
  23040. (level 0) (line "") (origline "") txt todo
  23041. (umax nil)
  23042. (umax-toc nil)
  23043. (filename (if to-buffer nil
  23044. (expand-file-name
  23045. (concat
  23046. (file-name-sans-extension
  23047. (or (and subtree-p
  23048. (org-entry-get (region-beginning)
  23049. "EXPORT_FILE_NAME" t))
  23050. (file-name-nondirectory buffer-file-name)))
  23051. "." html-extension)
  23052. (file-name-as-directory
  23053. (org-export-directory :html opt-plist)))))
  23054. (current-dir (if buffer-file-name
  23055. (file-name-directory buffer-file-name)
  23056. default-directory))
  23057. (buffer (if to-buffer
  23058. (cond
  23059. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23060. (t (get-buffer-create to-buffer)))
  23061. (find-file-noselect filename)))
  23062. (org-levels-open (make-vector org-level-max nil))
  23063. (date (plist-get opt-plist :date))
  23064. (author (plist-get opt-plist :author))
  23065. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23066. (plist-get opt-plist :title)
  23067. (and (not
  23068. (plist-get opt-plist :skip-before-1st-heading))
  23069. (org-export-grab-title-from-buffer))
  23070. (and buffer-file-name
  23071. (file-name-sans-extension
  23072. (file-name-nondirectory buffer-file-name)))
  23073. "UNTITLED"))
  23074. (html-table-tag (plist-get opt-plist :html-table-tag))
  23075. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23076. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23077. (inquote nil)
  23078. (infixed nil)
  23079. (in-local-list nil)
  23080. (local-list-num nil)
  23081. (local-list-indent nil)
  23082. (llt org-plain-list-ordered-item-terminator)
  23083. (email (plist-get opt-plist :email))
  23084. (language (plist-get opt-plist :language))
  23085. (lang-words nil)
  23086. (target-alist nil) tg
  23087. (head-count 0) cnt
  23088. (start 0)
  23089. (coding-system (and (boundp 'buffer-file-coding-system)
  23090. buffer-file-coding-system))
  23091. (coding-system-for-write (or org-export-html-coding-system
  23092. coding-system))
  23093. (save-buffer-coding-system (or org-export-html-coding-system
  23094. coding-system))
  23095. (charset (and coding-system-for-write
  23096. (fboundp 'coding-system-get)
  23097. (coding-system-get coding-system-for-write
  23098. 'mime-charset)))
  23099. (region
  23100. (buffer-substring
  23101. (if region-p (region-beginning) (point-min))
  23102. (if region-p (region-end) (point-max))))
  23103. (lines
  23104. (org-split-string
  23105. (org-cleaned-string-for-export
  23106. region
  23107. :emph-multiline t
  23108. :for-html t
  23109. :skip-before-1st-heading
  23110. (plist-get opt-plist :skip-before-1st-heading)
  23111. :drawers (plist-get opt-plist :drawers)
  23112. :archived-trees
  23113. (plist-get opt-plist :archived-trees)
  23114. :add-text
  23115. (plist-get opt-plist :text)
  23116. :LaTeX-fragments
  23117. (plist-get opt-plist :LaTeX-fragments))
  23118. "[\r\n]"))
  23119. table-open type
  23120. table-buffer table-orig-buffer
  23121. ind start-is-num starter didclose
  23122. rpl path desc descp desc1 desc2 link
  23123. )
  23124. (let ((inhibit-read-only t))
  23125. (org-unmodified
  23126. (remove-text-properties (point-min) (point-max)
  23127. '(:org-license-to-kill t))))
  23128. (message "Exporting...")
  23129. (setq org-min-level (org-get-min-level lines))
  23130. (setq org-last-level org-min-level)
  23131. (org-init-section-numbers)
  23132. (cond
  23133. ((and date (string-match "%" date))
  23134. (setq date (format-time-string date (current-time))))
  23135. (date)
  23136. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23137. ;; Get the language-dependent settings
  23138. (setq lang-words (or (assoc language org-export-language-setup)
  23139. (assoc "en" org-export-language-setup)))
  23140. ;; Switch to the output buffer
  23141. (set-buffer buffer)
  23142. (let ((inhibit-read-only t)) (erase-buffer))
  23143. (fundamental-mode)
  23144. (and (fboundp 'set-buffer-file-coding-system)
  23145. (set-buffer-file-coding-system coding-system-for-write))
  23146. (let ((case-fold-search nil)
  23147. (org-odd-levels-only odd))
  23148. ;; create local variables for all options, to make sure all called
  23149. ;; functions get the correct information
  23150. (mapc (lambda (x)
  23151. (set (make-local-variable (cdr x))
  23152. (plist-get opt-plist (car x))))
  23153. org-export-plist-vars)
  23154. (setq umax (if arg (prefix-numeric-value arg)
  23155. org-export-headline-levels))
  23156. (setq umax-toc (if (integerp org-export-with-toc)
  23157. (min org-export-with-toc umax)
  23158. umax))
  23159. (unless body-only
  23160. ;; File header
  23161. (insert (format
  23162. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23163. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23164. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23165. lang=\"%s\" xml:lang=\"%s\">
  23166. <head>
  23167. <title>%s</title>
  23168. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23169. <meta name=\"generator\" content=\"Org-mode\"/>
  23170. <meta name=\"generated\" content=\"%s\"/>
  23171. <meta name=\"author\" content=\"%s\"/>
  23172. %s
  23173. </head><body>
  23174. "
  23175. language language (org-html-expand title)
  23176. (or charset "iso-8859-1") date author style))
  23177. (insert (or (plist-get opt-plist :preamble) ""))
  23178. (when (plist-get opt-plist :auto-preamble)
  23179. (if title (insert (format org-export-html-title-format
  23180. (org-html-expand title))))))
  23181. (if (and org-export-with-toc (not body-only))
  23182. (progn
  23183. (push (format "<h%d>%s</h%d>\n"
  23184. org-export-html-toplevel-hlevel
  23185. (nth 3 lang-words)
  23186. org-export-html-toplevel-hlevel)
  23187. thetoc)
  23188. (push "<ul>\n<li>" thetoc)
  23189. (setq lines
  23190. (mapcar '(lambda (line)
  23191. (if (string-match org-todo-line-regexp line)
  23192. ;; This is a headline
  23193. (progn
  23194. (setq have-headings t)
  23195. (setq level (- (match-end 1) (match-beginning 1))
  23196. level (org-tr-level level)
  23197. txt (save-match-data
  23198. (org-html-expand
  23199. (org-export-cleanup-toc-line
  23200. (match-string 3 line))))
  23201. todo
  23202. (or (and org-export-mark-todo-in-toc
  23203. (match-beginning 2)
  23204. (not (member (match-string 2 line)
  23205. org-done-keywords)))
  23206. ; TODO, not DONE
  23207. (and org-export-mark-todo-in-toc
  23208. (= level umax-toc)
  23209. (org-search-todo-below
  23210. line lines level))))
  23211. (if (string-match
  23212. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23213. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23214. (if (string-match quote-re0 txt)
  23215. (setq txt (replace-match "" t t txt)))
  23216. (if org-export-with-section-numbers
  23217. (setq txt (concat (org-section-number level)
  23218. " " txt)))
  23219. (if (<= level (max umax umax-toc))
  23220. (setq head-count (+ head-count 1)))
  23221. (if (<= level umax-toc)
  23222. (progn
  23223. (if (> level org-last-level)
  23224. (progn
  23225. (setq cnt (- level org-last-level))
  23226. (while (>= (setq cnt (1- cnt)) 0)
  23227. (push "\n<ul>\n<li>" thetoc))
  23228. (push "\n" thetoc)))
  23229. (if (< level org-last-level)
  23230. (progn
  23231. (setq cnt (- org-last-level level))
  23232. (while (>= (setq cnt (1- cnt)) 0)
  23233. (push "</li>\n</ul>" thetoc))
  23234. (push "\n" thetoc)))
  23235. ;; Check for targets
  23236. (while (string-match org-target-regexp line)
  23237. (setq tg (match-string 1 line)
  23238. line (replace-match
  23239. (concat "@<span class=\"target\">" tg "@</span> ")
  23240. t t line))
  23241. (push (cons (org-solidify-link-text tg)
  23242. (format "sec-%d" head-count))
  23243. target-alist))
  23244. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23245. (setq txt (replace-match "" t t txt)))
  23246. (push
  23247. (format
  23248. (if todo
  23249. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23250. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23251. head-count txt) thetoc)
  23252. (setq org-last-level level))
  23253. )))
  23254. line)
  23255. lines))
  23256. (while (> org-last-level (1- org-min-level))
  23257. (setq org-last-level (1- org-last-level))
  23258. (push "</li>\n</ul>\n" thetoc))
  23259. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23260. (setq head-count 0)
  23261. (org-init-section-numbers)
  23262. (while (setq line (pop lines) origline line)
  23263. (catch 'nextline
  23264. ;; end of quote section?
  23265. (when (and inquote (string-match "^\\*+ " line))
  23266. (insert "</pre>\n")
  23267. (setq inquote nil))
  23268. ;; inside a quote section?
  23269. (when inquote
  23270. (insert (org-html-protect line) "\n")
  23271. (throw 'nextline nil))
  23272. ;; verbatim lines
  23273. (when (and org-export-with-fixed-width
  23274. (string-match "^[ \t]*:\\(.*\\)" line))
  23275. (when (not infixed)
  23276. (setq infixed t)
  23277. (insert "<pre>\n"))
  23278. (insert (org-html-protect (match-string 1 line)) "\n")
  23279. (when (and lines
  23280. (not (string-match "^[ \t]*\\(:.*\\)"
  23281. (car lines))))
  23282. (setq infixed nil)
  23283. (insert "</pre>\n"))
  23284. (throw 'nextline nil))
  23285. ;; Protected HTML
  23286. (when (get-text-property 0 'org-protected line)
  23287. (let (par)
  23288. (when (re-search-backward
  23289. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23290. (setq par (match-string 1))
  23291. (replace-match "\\2\n"))
  23292. (insert line "\n")
  23293. (while (and lines
  23294. (or (= (length (car lines)) 0)
  23295. (get-text-property 0 'org-protected (car lines))))
  23296. (insert (pop lines) "\n"))
  23297. (and par (insert "<p>\n")))
  23298. (throw 'nextline nil))
  23299. ;; Horizontal line
  23300. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23301. (insert "\n<hr/>\n")
  23302. (throw 'nextline nil))
  23303. ;; make targets to anchors
  23304. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23305. (cond
  23306. ((match-end 2)
  23307. (setq line (replace-match
  23308. (concat "@<a name=\""
  23309. (org-solidify-link-text (match-string 1 line))
  23310. "\">\\nbsp@</a>")
  23311. t t line)))
  23312. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23313. (setq line (replace-match
  23314. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23315. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23316. t t line)))
  23317. (t
  23318. (setq line (replace-match
  23319. (concat "@<a name=\""
  23320. (org-solidify-link-text (match-string 1 line))
  23321. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23322. t t line)))))
  23323. (setq line (org-html-handle-time-stamps line))
  23324. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23325. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23326. ;; Also handle sub_superscripts and checkboxes
  23327. (or (string-match org-table-hline-regexp line)
  23328. (setq line (org-html-expand line)))
  23329. ;; Format the links
  23330. (setq start 0)
  23331. (while (string-match org-bracket-link-analytic-regexp line start)
  23332. (setq start (match-beginning 0))
  23333. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23334. (setq path (match-string 3 line))
  23335. (setq desc1 (if (match-end 5) (match-string 5 line))
  23336. desc2 (if (match-end 2) (concat type ":" path) path)
  23337. descp (and desc1 (not (equal desc1 desc2)))
  23338. desc (or desc1 desc2))
  23339. ;; Make an image out of the description if that is so wanted
  23340. (when (and descp (org-file-image-p desc))
  23341. (save-match-data
  23342. (if (string-match "^file:" desc)
  23343. (setq desc (substring desc (match-end 0)))))
  23344. (setq desc (concat "<img src=\"" desc "\"/>")))
  23345. ;; FIXME: do we need to unescape here somewhere?
  23346. (cond
  23347. ((equal type "internal")
  23348. (setq rpl
  23349. (concat
  23350. "<a href=\"#"
  23351. (org-solidify-link-text
  23352. (save-match-data (org-link-unescape path)) target-alist)
  23353. "\">" desc "</a>")))
  23354. ((member type '("http" "https"))
  23355. ;; standard URL, just check if we need to inline an image
  23356. (if (and (or (eq t org-export-html-inline-images)
  23357. (and org-export-html-inline-images (not descp)))
  23358. (org-file-image-p path))
  23359. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23360. (setq link (concat type ":" path))
  23361. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23362. ((member type '("ftp" "mailto" "news"))
  23363. ;; standard URL
  23364. (setq link (concat type ":" path))
  23365. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23366. ((string= type "file")
  23367. ;; FILE link
  23368. (let* ((filename path)
  23369. (abs-p (file-name-absolute-p filename))
  23370. thefile file-is-image-p search)
  23371. (save-match-data
  23372. (if (string-match "::\\(.*\\)" filename)
  23373. (setq search (match-string 1 filename)
  23374. filename (replace-match "" t nil filename)))
  23375. (setq valid
  23376. (if (functionp link-validate)
  23377. (funcall link-validate filename current-dir)
  23378. t))
  23379. (setq file-is-image-p (org-file-image-p filename))
  23380. (setq thefile (if abs-p (expand-file-name filename) filename))
  23381. (when (and org-export-html-link-org-files-as-html
  23382. (string-match "\\.org$" thefile))
  23383. (setq thefile (concat (substring thefile 0
  23384. (match-beginning 0))
  23385. "." html-extension))
  23386. (if (and search
  23387. ;; make sure this is can be used as target search
  23388. (not (string-match "^[0-9]*$" search))
  23389. (not (string-match "^\\*" search))
  23390. (not (string-match "^/.*/$" search)))
  23391. (setq thefile (concat thefile "#"
  23392. (org-solidify-link-text
  23393. (org-link-unescape search)))))
  23394. (when (string-match "^file:" desc)
  23395. (setq desc (replace-match "" t t desc))
  23396. (if (string-match "\\.org$" desc)
  23397. (setq desc (replace-match "" t t desc))))))
  23398. (setq rpl (if (and file-is-image-p
  23399. (or (eq t org-export-html-inline-images)
  23400. (and org-export-html-inline-images
  23401. (not descp))))
  23402. (concat "<img src=\"" thefile "\"/>")
  23403. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23404. (if (not valid) (setq rpl desc))))
  23405. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23406. (setq rpl (concat "<i>&lt;" type ":"
  23407. (save-match-data (org-link-unescape path))
  23408. "&gt;</i>"))))
  23409. (setq line (replace-match rpl t t line)
  23410. start (+ start (length rpl))))
  23411. ;; TODO items
  23412. (if (and (string-match org-todo-line-regexp line)
  23413. (match-beginning 2))
  23414. (setq line
  23415. (concat (substring line 0 (match-beginning 2))
  23416. "<span class=\""
  23417. (if (member (match-string 2 line)
  23418. org-done-keywords)
  23419. "done" "todo")
  23420. "\">" (match-string 2 line)
  23421. "</span>" (substring line (match-end 2)))))
  23422. ;; Does this contain a reference to a footnote?
  23423. (when org-export-with-footnotes
  23424. (setq start 0)
  23425. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23426. (if (get-text-property (match-beginning 2) 'org-protected line)
  23427. (setq start (match-end 2))
  23428. (let ((n (match-string 2 line)))
  23429. (setq line
  23430. (replace-match
  23431. (format
  23432. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23433. (match-string 1 line) n n n)
  23434. t t line))))))
  23435. (cond
  23436. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23437. ;; This is a headline
  23438. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23439. txt (match-string 2 line))
  23440. (if (string-match quote-re0 txt)
  23441. (setq txt (replace-match "" t t txt)))
  23442. (if (<= level (max umax umax-toc))
  23443. (setq head-count (+ head-count 1)))
  23444. (when in-local-list
  23445. ;; Close any local lists before inserting a new header line
  23446. (while local-list-num
  23447. (org-close-li)
  23448. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23449. (pop local-list-num))
  23450. (setq local-list-indent nil
  23451. in-local-list nil))
  23452. (setq first-heading-pos (or first-heading-pos (point)))
  23453. (org-html-level-start level txt umax
  23454. (and org-export-with-toc (<= level umax))
  23455. head-count)
  23456. ;; QUOTES
  23457. (when (string-match quote-re line)
  23458. (insert "<pre>")
  23459. (setq inquote t)))
  23460. ((and org-export-with-tables
  23461. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23462. (if (not table-open)
  23463. ;; New table starts
  23464. (setq table-open t table-buffer nil table-orig-buffer nil))
  23465. ;; Accumulate lines
  23466. (setq table-buffer (cons line table-buffer)
  23467. table-orig-buffer (cons origline table-orig-buffer))
  23468. (when (or (not lines)
  23469. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23470. (car lines))))
  23471. (setq table-open nil
  23472. table-buffer (nreverse table-buffer)
  23473. table-orig-buffer (nreverse table-orig-buffer))
  23474. (org-close-par-maybe)
  23475. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23476. (t
  23477. ;; Normal lines
  23478. (when (string-match
  23479. (cond
  23480. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23481. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23482. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23483. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23484. line)
  23485. (setq ind (org-get-string-indentation line)
  23486. start-is-num (match-beginning 4)
  23487. starter (if (match-beginning 2)
  23488. (substring (match-string 2 line) 0 -1))
  23489. line (substring line (match-beginning 5)))
  23490. (unless (string-match "[^ \t]" line)
  23491. ;; empty line. Pretend indentation is large.
  23492. (setq ind (if org-empty-line-terminates-plain-lists
  23493. 0
  23494. (1+ (or (car local-list-indent) 1)))))
  23495. (setq didclose nil)
  23496. (while (and in-local-list
  23497. (or (and (= ind (car local-list-indent))
  23498. (not starter))
  23499. (< ind (car local-list-indent))))
  23500. (setq didclose t)
  23501. (org-close-li)
  23502. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23503. (pop local-list-num) (pop local-list-indent)
  23504. (setq in-local-list local-list-indent))
  23505. (cond
  23506. ((and starter
  23507. (or (not in-local-list)
  23508. (> ind (car local-list-indent))))
  23509. ;; Start new (level of) list
  23510. (org-close-par-maybe)
  23511. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23512. (push start-is-num local-list-num)
  23513. (push ind local-list-indent)
  23514. (setq in-local-list t))
  23515. (starter
  23516. ;; continue current list
  23517. (org-close-li)
  23518. (insert "<li>\n"))
  23519. (didclose
  23520. ;; we did close a list, normal text follows: need <p>
  23521. (org-open-par)))
  23522. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23523. (setq line
  23524. (replace-match
  23525. (if (equal (match-string 1 line) "X")
  23526. "<b>[X]</b>"
  23527. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23528. t t line))))
  23529. ;; Empty lines start a new paragraph. If hand-formatted lists
  23530. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23531. ;; also start a new paragraph.
  23532. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23533. ;; Is this the start of a footnote?
  23534. (when org-export-with-footnotes
  23535. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23536. (org-close-par-maybe)
  23537. (let ((n (match-string 1 line)))
  23538. (setq line (replace-match
  23539. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23540. ;; Check if the line break needs to be conserved
  23541. (cond
  23542. ((string-match "\\\\\\\\[ \t]*$" line)
  23543. (setq line (replace-match "<br/>" t t line)))
  23544. (org-export-preserve-breaks
  23545. (setq line (concat line "<br/>"))))
  23546. (insert line "\n")))))
  23547. ;; Properly close all local lists and other lists
  23548. (when inquote (insert "</pre>\n"))
  23549. (when in-local-list
  23550. ;; Close any local lists before inserting a new header line
  23551. (while local-list-num
  23552. (org-close-li)
  23553. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23554. (pop local-list-num))
  23555. (setq local-list-indent nil
  23556. in-local-list nil))
  23557. (org-html-level-start 1 nil umax
  23558. (and org-export-with-toc (<= level umax))
  23559. head-count)
  23560. (unless body-only
  23561. (when (plist-get opt-plist :auto-postamble)
  23562. (insert "<div id=\"postamble\">")
  23563. (when (and org-export-author-info author)
  23564. (insert "<p class=\"author\"> "
  23565. (nth 1 lang-words) ": " author "\n")
  23566. (when email
  23567. (if (listp (split-string email ",+ *"))
  23568. (mapc (lambda(e)
  23569. (insert "<a href=\"mailto:" e "\">&lt;"
  23570. e "&gt;</a>\n"))
  23571. (split-string email ",+ *"))
  23572. (insert "<a href=\"mailto:" email "\">&lt;"
  23573. email "&gt;</a>\n")))
  23574. (insert "</p>\n"))
  23575. (when (and date org-export-time-stamp-file)
  23576. (insert "<p class=\"date\"> "
  23577. (nth 2 lang-words) ": "
  23578. date "</p>\n"))
  23579. (insert "</div>"))
  23580. (if org-export-html-with-timestamp
  23581. (insert org-export-html-html-helper-timestamp))
  23582. (insert (or (plist-get opt-plist :postamble) ""))
  23583. (insert "</body>\n</html>\n"))
  23584. (normal-mode)
  23585. (if (eq major-mode default-major-mode) (html-mode))
  23586. ;; insert the table of contents
  23587. (goto-char (point-min))
  23588. (when thetoc
  23589. (if (or (re-search-forward
  23590. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23591. (re-search-forward
  23592. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23593. (progn
  23594. (goto-char (match-beginning 0))
  23595. (replace-match ""))
  23596. (goto-char first-heading-pos)
  23597. (when (looking-at "\\s-*</p>")
  23598. (goto-char (match-end 0))
  23599. (insert "\n")))
  23600. (insert "<div id=\"table-of-contents\">\n")
  23601. (mapc 'insert thetoc)
  23602. (insert "</div>\n"))
  23603. ;; remove empty paragraphs and lists
  23604. (goto-char (point-min))
  23605. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23606. (replace-match ""))
  23607. (goto-char (point-min))
  23608. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23609. (replace-match ""))
  23610. (goto-char (point-min))
  23611. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23612. (replace-match ""))
  23613. ;; Convert whitespace place holders
  23614. (goto-char (point-min))
  23615. (let (beg end n)
  23616. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23617. (setq n (get-text-property beg 'org-whitespace)
  23618. end (next-single-property-change beg 'org-whitespace))
  23619. (goto-char beg)
  23620. (delete-region beg end)
  23621. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23622. (make-string n ?x)))))
  23623. (or to-buffer (save-buffer))
  23624. (goto-char (point-min))
  23625. (message "Exporting... done")
  23626. (if (eq to-buffer 'string)
  23627. (prog1 (buffer-substring (point-min) (point-max))
  23628. (kill-buffer (current-buffer)))
  23629. (current-buffer)))))
  23630. (defvar org-table-colgroup-info nil)
  23631. (defun org-format-table-ascii (lines)
  23632. "Format a table for ascii export."
  23633. (if (stringp lines)
  23634. (setq lines (org-split-string lines "\n")))
  23635. (if (not (string-match "^[ \t]*|" (car lines)))
  23636. ;; Table made by table.el - test for spanning
  23637. lines
  23638. ;; A normal org table
  23639. ;; Get rid of hlines at beginning and end
  23640. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23641. (setq lines (nreverse lines))
  23642. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23643. (setq lines (nreverse lines))
  23644. (when org-export-table-remove-special-lines
  23645. ;; Check if the table has a marking column. If yes remove the
  23646. ;; column and the special lines
  23647. (setq lines (org-table-clean-before-export lines)))
  23648. ;; Get rid of the vertical lines except for grouping
  23649. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23650. rtn line vl1 start)
  23651. (while (setq line (pop lines))
  23652. (if (string-match org-table-hline-regexp line)
  23653. (and (string-match "|\\(.*\\)|" line)
  23654. (setq line (replace-match " \\1" t nil line)))
  23655. (setq start 0 vl1 vl)
  23656. (while (string-match "|" line start)
  23657. (setq start (match-end 0))
  23658. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23659. (push line rtn))
  23660. (nreverse rtn))))
  23661. (defun org-colgroup-info-to-vline-list (info)
  23662. (let (vl new last)
  23663. (while info
  23664. (setq last new new (pop info))
  23665. (if (or (memq last '(:end :startend))
  23666. (memq new '(:start :startend)))
  23667. (push t vl)
  23668. (push nil vl)))
  23669. (setq vl (nreverse vl))
  23670. (and vl (setcar vl nil))
  23671. vl))
  23672. (defun org-format-table-html (lines olines)
  23673. "Find out which HTML converter to use and return the HTML code."
  23674. (if (stringp lines)
  23675. (setq lines (org-split-string lines "\n")))
  23676. (if (string-match "^[ \t]*|" (car lines))
  23677. ;; A normal org table
  23678. (org-format-org-table-html lines)
  23679. ;; Table made by table.el - test for spanning
  23680. (let* ((hlines (delq nil (mapcar
  23681. (lambda (x)
  23682. (if (string-match "^[ \t]*\\+-" x) x
  23683. nil))
  23684. lines)))
  23685. (first (car hlines))
  23686. (ll (and (string-match "\\S-+" first)
  23687. (match-string 0 first)))
  23688. (re (concat "^[ \t]*" (regexp-quote ll)))
  23689. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23690. hlines))))
  23691. (if (and (not spanning)
  23692. (not org-export-prefer-native-exporter-for-tables))
  23693. ;; We can use my own converter with HTML conversions
  23694. (org-format-table-table-html lines)
  23695. ;; Need to use the code generator in table.el, with the original text.
  23696. (org-format-table-table-html-using-table-generate-source olines)))))
  23697. (defun org-format-org-table-html (lines &optional splice)
  23698. "Format a table into HTML."
  23699. ;; Get rid of hlines at beginning and end
  23700. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23701. (setq lines (nreverse lines))
  23702. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23703. (setq lines (nreverse lines))
  23704. (when org-export-table-remove-special-lines
  23705. ;; Check if the table has a marking column. If yes remove the
  23706. ;; column and the special lines
  23707. (setq lines (org-table-clean-before-export lines)))
  23708. (let ((head (and org-export-highlight-first-table-line
  23709. (delq nil (mapcar
  23710. (lambda (x) (string-match "^[ \t]*|-" x))
  23711. (cdr lines)))))
  23712. (nlines 0) fnum i
  23713. tbopen line fields html gr colgropen)
  23714. (if splice (setq head nil))
  23715. (unless splice (push (if head "<thead>" "<tbody>") html))
  23716. (setq tbopen t)
  23717. (while (setq line (pop lines))
  23718. (catch 'next-line
  23719. (if (string-match "^[ \t]*|-" line)
  23720. (progn
  23721. (unless splice
  23722. (push (if head "</thead>" "</tbody>") html)
  23723. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23724. (setq head nil) ;; head ends here, first time around
  23725. ;; ignore this line
  23726. (throw 'next-line t)))
  23727. ;; Break the line into fields
  23728. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23729. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23730. (setq nlines (1+ nlines) i -1)
  23731. (push (concat "<tr>"
  23732. (mapconcat
  23733. (lambda (x)
  23734. (setq i (1+ i))
  23735. (if (and (< i nlines)
  23736. (string-match org-table-number-regexp x))
  23737. (incf (aref fnum i)))
  23738. (if head
  23739. (concat (car org-export-table-header-tags) x
  23740. (cdr org-export-table-header-tags))
  23741. (concat (car org-export-table-data-tags) x
  23742. (cdr org-export-table-data-tags))))
  23743. fields "")
  23744. "</tr>")
  23745. html)))
  23746. (unless splice (if tbopen (push "</tbody>" html)))
  23747. (unless splice (push "</table>\n" html))
  23748. (setq html (nreverse html))
  23749. (unless splice
  23750. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23751. (push (mapconcat
  23752. (lambda (x)
  23753. (setq gr (pop org-table-colgroup-info))
  23754. (format "%s<col align=\"%s\"></col>%s"
  23755. (if (memq gr '(:start :startend))
  23756. (prog1
  23757. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23758. (setq colgropen t))
  23759. "")
  23760. (if (> (/ (float x) nlines) org-table-number-fraction)
  23761. "right" "left")
  23762. (if (memq gr '(:end :startend))
  23763. (progn (setq colgropen nil) "</colgroup>")
  23764. "")))
  23765. fnum "")
  23766. html)
  23767. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23768. (push html-table-tag html))
  23769. (concat (mapconcat 'identity html "\n") "\n")))
  23770. (defun org-table-clean-before-export (lines)
  23771. "Check if the table has a marking column.
  23772. If yes remove the column and the special lines."
  23773. (setq org-table-colgroup-info nil)
  23774. (if (memq nil
  23775. (mapcar
  23776. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23777. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23778. lines))
  23779. (progn
  23780. (setq org-table-clean-did-remove-column nil)
  23781. (delq nil
  23782. (mapcar
  23783. (lambda (x)
  23784. (cond
  23785. ((string-match "^[ \t]*| */ *|" x)
  23786. (setq org-table-colgroup-info
  23787. (mapcar (lambda (x)
  23788. (cond ((member x '("<" "&lt;")) :start)
  23789. ((member x '(">" "&gt;")) :end)
  23790. ((member x '("<>" "&lt;&gt;")) :startend)
  23791. (t nil)))
  23792. (org-split-string x "[ \t]*|[ \t]*")))
  23793. nil)
  23794. (t x)))
  23795. lines)))
  23796. (setq org-table-clean-did-remove-column t)
  23797. (delq nil
  23798. (mapcar
  23799. (lambda (x)
  23800. (cond
  23801. ((string-match "^[ \t]*| */ *|" x)
  23802. (setq org-table-colgroup-info
  23803. (mapcar (lambda (x)
  23804. (cond ((member x '("<" "&lt;")) :start)
  23805. ((member x '(">" "&gt;")) :end)
  23806. ((member x '("<>" "&lt;&gt;")) :startend)
  23807. (t nil)))
  23808. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23809. nil)
  23810. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23811. nil) ; ignore this line
  23812. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23813. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23814. ;; remove the first column
  23815. (replace-match "\\1|" t nil x))))
  23816. lines))))
  23817. (defun org-format-table-table-html (lines)
  23818. "Format a table generated by table.el into HTML.
  23819. This conversion does *not* use `table-generate-source' from table.el.
  23820. This has the advantage that Org-mode's HTML conversions can be used.
  23821. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23822. (let (line field-buffer
  23823. (head org-export-highlight-first-table-line)
  23824. fields html empty)
  23825. (setq html (concat html-table-tag "\n"))
  23826. (while (setq line (pop lines))
  23827. (setq empty "&nbsp;")
  23828. (catch 'next-line
  23829. (if (string-match "^[ \t]*\\+-" line)
  23830. (progn
  23831. (if field-buffer
  23832. (progn
  23833. (setq
  23834. html
  23835. (concat
  23836. html
  23837. "<tr>"
  23838. (mapconcat
  23839. (lambda (x)
  23840. (if (equal x "") (setq x empty))
  23841. (if head
  23842. (concat (car org-export-table-header-tags) x
  23843. (cdr org-export-table-header-tags))
  23844. (concat (car org-export-table-data-tags) x
  23845. (cdr org-export-table-data-tags))))
  23846. field-buffer "\n")
  23847. "</tr>\n"))
  23848. (setq head nil)
  23849. (setq field-buffer nil)))
  23850. ;; Ignore this line
  23851. (throw 'next-line t)))
  23852. ;; Break the line into fields and store the fields
  23853. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23854. (if field-buffer
  23855. (setq field-buffer (mapcar
  23856. (lambda (x)
  23857. (concat x "<br/>" (pop fields)))
  23858. field-buffer))
  23859. (setq field-buffer fields))))
  23860. (setq html (concat html "</table>\n"))
  23861. html))
  23862. (defun org-format-table-table-html-using-table-generate-source (lines)
  23863. "Format a table into html, using `table-generate-source' from table.el.
  23864. This has the advantage that cell- or row-spanning is allowed.
  23865. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23866. (require 'table)
  23867. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23868. (erase-buffer)
  23869. (insert (mapconcat 'identity lines "\n"))
  23870. (goto-char (point-min))
  23871. (if (not (re-search-forward "|[^+]" nil t))
  23872. (error "Error processing table"))
  23873. (table-recognize-table)
  23874. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23875. (table-generate-source 'html " org-tmp2 ")
  23876. (set-buffer " org-tmp2 ")
  23877. (buffer-substring (point-min) (point-max))))
  23878. (defun org-html-handle-time-stamps (s)
  23879. "Format time stamps in string S, or remove them."
  23880. (catch 'exit
  23881. (let (r b)
  23882. (while (string-match org-maybe-keyword-time-regexp s)
  23883. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23884. ;; never export CLOCK
  23885. (throw 'exit ""))
  23886. (or b (setq b (substring s 0 (match-beginning 0))))
  23887. (if (not org-export-with-timestamps)
  23888. (setq r (concat r (substring s 0 (match-beginning 0)))
  23889. s (substring s (match-end 0)))
  23890. (setq r (concat
  23891. r (substring s 0 (match-beginning 0))
  23892. (if (match-end 1)
  23893. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23894. (match-string 1 s)))
  23895. (format " @<span class=\"timestamp\">%s@</span>"
  23896. (substring
  23897. (org-translate-time (match-string 3 s)) 1 -1)))
  23898. s (substring s (match-end 0)))))
  23899. ;; Line break if line started and ended with time stamp stuff
  23900. (if (not r)
  23901. s
  23902. (setq r (concat r s))
  23903. (unless (string-match "\\S-" (concat b s))
  23904. (setq r (concat r "@<br/>")))
  23905. r))))
  23906. (defun org-html-protect (s)
  23907. ;; convert & to &amp;, < to &lt; and > to &gt;
  23908. (let ((start 0))
  23909. (while (string-match "&" s start)
  23910. (setq s (replace-match "&amp;" t t s)
  23911. start (1+ (match-beginning 0))))
  23912. (while (string-match "<" s)
  23913. (setq s (replace-match "&lt;" t t s)))
  23914. (while (string-match ">" s)
  23915. (setq s (replace-match "&gt;" t t s))))
  23916. s)
  23917. (defun org-export-cleanup-toc-line (s)
  23918. "Remove tags and time staps from lines going into the toc."
  23919. (when (memq org-export-with-tags '(not-in-toc nil))
  23920. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23921. (setq s (replace-match "" t t s))))
  23922. (when org-export-remove-timestamps-from-toc
  23923. (while (string-match org-maybe-keyword-time-regexp s)
  23924. (setq s (replace-match "" t t s))))
  23925. (while (string-match org-bracket-link-regexp s)
  23926. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23927. t t s)))
  23928. s)
  23929. (defun org-html-expand (string)
  23930. "Prepare STRING for HTML export. Applies all active conversions.
  23931. If there are links in the string, don't modify these."
  23932. (let* ((re (concat org-bracket-link-regexp "\\|"
  23933. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23934. m s l res)
  23935. (while (setq m (string-match re string))
  23936. (setq s (substring string 0 m)
  23937. l (match-string 0 string)
  23938. string (substring string (match-end 0)))
  23939. (push (org-html-do-expand s) res)
  23940. (push l res))
  23941. (push (org-html-do-expand string) res)
  23942. (apply 'concat (nreverse res))))
  23943. (defun org-html-do-expand (s)
  23944. "Apply all active conversions to translate special ASCII to HTML."
  23945. (setq s (org-html-protect s))
  23946. (if org-export-html-expand
  23947. (let ((start 0))
  23948. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23949. (setq s (replace-match "<\\1>" t nil s)))))
  23950. (if org-export-with-emphasize
  23951. (setq s (org-export-html-convert-emphasize s)))
  23952. (if org-export-with-special-strings
  23953. (setq s (org-export-html-convert-special-strings s)))
  23954. (if org-export-with-sub-superscripts
  23955. (setq s (org-export-html-convert-sub-super s)))
  23956. (if org-export-with-TeX-macros
  23957. (let ((start 0) wd ass)
  23958. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23959. (if (get-text-property (match-beginning 0) 'org-protected s)
  23960. (setq start (match-end 0))
  23961. (setq wd (match-string 1 s))
  23962. (if (setq ass (assoc wd org-html-entities))
  23963. (setq s (replace-match (or (cdr ass)
  23964. (concat "&" (car ass) ";"))
  23965. t t s))
  23966. (setq start (+ start (length wd))))))))
  23967. s)
  23968. (defun org-create-multibrace-regexp (left right n)
  23969. "Create a regular expression which will match a balanced sexp.
  23970. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23971. as single character strings.
  23972. The regexp returned will match the entire expression including the
  23973. delimiters. It will also define a single group which contains the
  23974. match except for the outermost delimiters. The maximum depth of
  23975. stacked delimiters is N. Escaping delimiters is not possible."
  23976. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23977. (or "\\|")
  23978. (re nothing)
  23979. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23980. (while (> n 1)
  23981. (setq n (1- n)
  23982. re (concat re or next)
  23983. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23984. (concat left "\\(" re "\\)" right)))
  23985. (defvar org-match-substring-regexp
  23986. (concat
  23987. "\\([^\\]\\)\\([_^]\\)\\("
  23988. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23989. "\\|"
  23990. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23991. "\\|"
  23992. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23993. "The regular expression matching a sub- or superscript.")
  23994. (defvar org-match-substring-with-braces-regexp
  23995. (concat
  23996. "\\([^\\]\\)\\([_^]\\)\\("
  23997. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23998. "\\)")
  23999. "The regular expression matching a sub- or superscript, forcing braces.")
  24000. (defconst org-export-html-special-string-regexps
  24001. '(("\\\\-" . "&shy;")
  24002. ("---\\([^-]\\)" . "&mdash;\\1")
  24003. ("--\\([^-]\\)" . "&ndash;\\1")
  24004. ("\\.\\.\\." . "&hellip;"))
  24005. "Regular expressions for special string conversion.")
  24006. (defun org-export-html-convert-special-strings (string)
  24007. "Convert special characters in STRING to HTML."
  24008. (let ((all org-export-html-special-string-regexps)
  24009. e a re rpl start)
  24010. (while (setq a (pop all))
  24011. (setq re (car a) rpl (cdr a) start 0)
  24012. (while (string-match re string start)
  24013. (if (get-text-property (match-beginning 0) 'org-protected string)
  24014. (setq start (match-end 0))
  24015. (setq string (replace-match rpl t nil string)))))
  24016. string))
  24017. (defun org-export-html-convert-sub-super (string)
  24018. "Convert sub- and superscripts in STRING to HTML."
  24019. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24020. (while (string-match org-match-substring-regexp string s)
  24021. (cond
  24022. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24023. ((get-text-property (match-beginning 2) 'org-protected string)
  24024. (setq s (match-end 2)))
  24025. (t
  24026. (setq s (match-end 1)
  24027. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24028. c (or (match-string 8 string)
  24029. (match-string 6 string)
  24030. (match-string 5 string))
  24031. string (replace-match
  24032. (concat (match-string 1 string)
  24033. "<" key ">" c "</" key ">")
  24034. t t string)))))
  24035. (while (string-match "\\\\\\([_^]\\)" string)
  24036. (setq string (replace-match (match-string 1 string) t t string)))
  24037. string))
  24038. (defun org-export-html-convert-emphasize (string)
  24039. "Apply emphasis."
  24040. (let ((s 0) rpl)
  24041. (while (string-match org-emph-re string s)
  24042. (if (not (equal
  24043. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24044. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24045. (setq s (match-beginning 0)
  24046. rpl
  24047. (concat
  24048. (match-string 1 string)
  24049. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24050. (match-string 4 string)
  24051. (nth 3 (assoc (match-string 3 string)
  24052. org-emphasis-alist))
  24053. (match-string 5 string))
  24054. string (replace-match rpl t t string)
  24055. s (+ s (- (length rpl) 2)))
  24056. (setq s (1+ s))))
  24057. string))
  24058. (defvar org-par-open nil)
  24059. (defun org-open-par ()
  24060. "Insert <p>, but first close previous paragraph if any."
  24061. (org-close-par-maybe)
  24062. (insert "\n<p>")
  24063. (setq org-par-open t))
  24064. (defun org-close-par-maybe ()
  24065. "Close paragraph if there is one open."
  24066. (when org-par-open
  24067. (insert "</p>")
  24068. (setq org-par-open nil)))
  24069. (defun org-close-li ()
  24070. "Close <li> if necessary."
  24071. (org-close-par-maybe)
  24072. (insert "</li>\n"))
  24073. (defvar body-only) ; dynamically scoped into this.
  24074. (defun org-html-level-start (level title umax with-toc head-count)
  24075. "Insert a new level in HTML export.
  24076. When TITLE is nil, just close all open levels."
  24077. (org-close-par-maybe)
  24078. (let ((l org-level-max))
  24079. (while (>= l level)
  24080. (if (aref org-levels-open (1- l))
  24081. (progn
  24082. (org-html-level-close l umax)
  24083. (aset org-levels-open (1- l) nil)))
  24084. (setq l (1- l)))
  24085. (when title
  24086. ;; If title is nil, this means this function is called to close
  24087. ;; all levels, so the rest is done only if title is given
  24088. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24089. (setq title (replace-match
  24090. (if org-export-with-tags
  24091. (save-match-data
  24092. (concat
  24093. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24094. (mapconcat 'identity (org-split-string
  24095. (match-string 1 title) ":")
  24096. "&nbsp;")
  24097. "</span>"))
  24098. "")
  24099. t t title)))
  24100. (if (> level umax)
  24101. (progn
  24102. (if (aref org-levels-open (1- level))
  24103. (progn
  24104. (org-close-li)
  24105. (insert "<li>" title "<br/>\n"))
  24106. (aset org-levels-open (1- level) t)
  24107. (org-close-par-maybe)
  24108. (insert "<ul>\n<li>" title "<br/>\n")))
  24109. (aset org-levels-open (1- level) t)
  24110. (if (and org-export-with-section-numbers (not body-only))
  24111. (setq title (concat (org-section-number level) " " title)))
  24112. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24113. (if with-toc
  24114. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24115. level level head-count title level))
  24116. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24117. (org-open-par)))))
  24118. (defun org-html-level-close (level max-outline-level)
  24119. "Terminate one level in HTML export."
  24120. (if (<= level max-outline-level)
  24121. (insert "</div>\n")
  24122. (org-close-li)
  24123. (insert "</ul>\n")))
  24124. ;;; iCalendar export
  24125. ;;;###autoload
  24126. (defun org-export-icalendar-this-file ()
  24127. "Export current file as an iCalendar file.
  24128. The iCalendar file will be located in the same directory as the Org-mode
  24129. file, but with extension `.ics'."
  24130. (interactive)
  24131. (org-export-icalendar nil buffer-file-name))
  24132. ;;;###autoload
  24133. (defun org-export-icalendar-all-agenda-files ()
  24134. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24135. Each iCalendar file will be located in the same directory as the Org-mode
  24136. file, but with extension `.ics'."
  24137. (interactive)
  24138. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24139. ;;;###autoload
  24140. (defun org-export-icalendar-combine-agenda-files ()
  24141. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24142. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24143. (interactive)
  24144. (apply 'org-export-icalendar t (org-agenda-files t)))
  24145. (defun org-export-icalendar (combine &rest files)
  24146. "Create iCalendar files for all elements of FILES.
  24147. If COMBINE is non-nil, combine all calendar entries into a single large
  24148. file and store it under the name `org-combined-agenda-icalendar-file'."
  24149. (save-excursion
  24150. (org-prepare-agenda-buffers files)
  24151. (let* ((dir (org-export-directory
  24152. :ical (list :publishing-directory
  24153. org-export-publishing-directory)))
  24154. file ical-file ical-buffer category started org-agenda-new-buffers)
  24155. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24156. (when combine
  24157. (setq ical-file
  24158. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24159. org-combined-agenda-icalendar-file
  24160. (expand-file-name org-combined-agenda-icalendar-file dir))
  24161. ical-buffer (org-get-agenda-file-buffer ical-file))
  24162. (set-buffer ical-buffer) (erase-buffer))
  24163. (while (setq file (pop files))
  24164. (catch 'nextfile
  24165. (org-check-agenda-file file)
  24166. (set-buffer (org-get-agenda-file-buffer file))
  24167. (unless combine
  24168. (setq ical-file (concat (file-name-as-directory dir)
  24169. (file-name-sans-extension
  24170. (file-name-nondirectory buffer-file-name))
  24171. ".ics"))
  24172. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24173. (with-current-buffer ical-buffer (erase-buffer)))
  24174. (setq category (or org-category
  24175. (file-name-sans-extension
  24176. (file-name-nondirectory buffer-file-name))))
  24177. (if (symbolp category) (setq category (symbol-name category)))
  24178. (let ((standard-output ical-buffer))
  24179. (if combine
  24180. (and (not started) (setq started t)
  24181. (org-start-icalendar-file org-icalendar-combined-name))
  24182. (org-start-icalendar-file category))
  24183. (org-print-icalendar-entries combine)
  24184. (when (or (and combine (not files)) (not combine))
  24185. (org-finish-icalendar-file)
  24186. (set-buffer ical-buffer)
  24187. (save-buffer)
  24188. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24189. (org-release-buffers org-agenda-new-buffers))))
  24190. (defvar org-after-save-iCalendar-file-hook nil
  24191. "Hook run after an iCalendar file has been saved.
  24192. The iCalendar buffer is still current when this hook is run.
  24193. A good way to use this is to tell a desktop calenndar application to re-read
  24194. the iCalendar file.")
  24195. (defun org-print-icalendar-entries (&optional combine)
  24196. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24197. When COMBINE is non nil, add the category to each line."
  24198. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24199. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24200. (dts (org-ical-ts-to-string
  24201. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24202. "DTSTART"))
  24203. hd ts ts2 state status (inc t) pos b sexp rrule
  24204. scheduledp deadlinep tmp pri category entry location summary desc
  24205. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24206. (org-refresh-category-properties)
  24207. (save-excursion
  24208. (goto-char (point-min))
  24209. (while (re-search-forward re1 nil t)
  24210. (catch :skip
  24211. (org-agenda-skip)
  24212. (setq pos (match-beginning 0)
  24213. ts (match-string 0)
  24214. inc t
  24215. hd (org-get-heading)
  24216. summary (org-icalendar-cleanup-string
  24217. (org-entry-get nil "SUMMARY"))
  24218. desc (org-icalendar-cleanup-string
  24219. (or (org-entry-get nil "DESCRIPTION")
  24220. (and org-icalendar-include-body (org-get-entry)))
  24221. t org-icalendar-include-body)
  24222. location (org-icalendar-cleanup-string
  24223. (org-entry-get nil "LOCATION"))
  24224. category (org-get-category))
  24225. (if (looking-at re2)
  24226. (progn
  24227. (goto-char (match-end 0))
  24228. (setq ts2 (match-string 1) inc nil))
  24229. (setq tmp (buffer-substring (max (point-min)
  24230. (- pos org-ds-keyword-length))
  24231. pos)
  24232. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24233. (progn
  24234. (setq inc nil)
  24235. (replace-match "\\1" t nil ts))
  24236. ts)
  24237. deadlinep (string-match org-deadline-regexp tmp)
  24238. scheduledp (string-match org-scheduled-regexp tmp)
  24239. ;; donep (org-entry-is-done-p)
  24240. ))
  24241. (if (or (string-match org-tr-regexp hd)
  24242. (string-match org-ts-regexp hd))
  24243. (setq hd (replace-match "" t t hd)))
  24244. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24245. (setq rrule
  24246. (concat "\nRRULE:FREQ="
  24247. (cdr (assoc
  24248. (match-string 2 ts)
  24249. '(("d" . "DAILY")("w" . "WEEKLY")
  24250. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24251. ";INTERVAL=" (match-string 1 ts)))
  24252. (setq rrule ""))
  24253. (setq summary (or summary hd))
  24254. (if (string-match org-bracket-link-regexp summary)
  24255. (setq summary
  24256. (replace-match (if (match-end 3)
  24257. (match-string 3 summary)
  24258. (match-string 1 summary))
  24259. t t summary)))
  24260. (if deadlinep (setq summary (concat "DL: " summary)))
  24261. (if scheduledp (setq summary (concat "S: " summary)))
  24262. (if (string-match "\\`<%%" ts)
  24263. (with-current-buffer sexp-buffer
  24264. (insert (substring ts 1 -1) " " summary "\n"))
  24265. (princ (format "BEGIN:VEVENT
  24266. %s
  24267. %s%s
  24268. SUMMARY:%s%s%s
  24269. CATEGORIES:%s
  24270. END:VEVENT\n"
  24271. (org-ical-ts-to-string ts "DTSTART")
  24272. (org-ical-ts-to-string ts2 "DTEND" inc)
  24273. rrule summary
  24274. (if (and desc (string-match "\\S-" desc))
  24275. (concat "\nDESCRIPTION: " desc) "")
  24276. (if (and location (string-match "\\S-" location))
  24277. (concat "\nLOCATION: " location) "")
  24278. category)))))
  24279. (when (and org-icalendar-include-sexps
  24280. (condition-case nil (require 'icalendar) (error nil))
  24281. (fboundp 'icalendar-export-region))
  24282. ;; Get all the literal sexps
  24283. (goto-char (point-min))
  24284. (while (re-search-forward "^&?%%(" nil t)
  24285. (catch :skip
  24286. (org-agenda-skip)
  24287. (setq b (match-beginning 0))
  24288. (goto-char (1- (match-end 0)))
  24289. (forward-sexp 1)
  24290. (end-of-line 1)
  24291. (setq sexp (buffer-substring b (point)))
  24292. (with-current-buffer sexp-buffer
  24293. (insert sexp "\n"))
  24294. (princ (org-diary-to-ical-string sexp-buffer)))))
  24295. (when org-icalendar-include-todo
  24296. (goto-char (point-min))
  24297. (while (re-search-forward org-todo-line-regexp nil t)
  24298. (catch :skip
  24299. (org-agenda-skip)
  24300. (setq state (match-string 2))
  24301. (setq status (if (member state org-done-keywords)
  24302. "COMPLETED" "NEEDS-ACTION"))
  24303. (when (and state
  24304. (or (not (member state org-done-keywords))
  24305. (eq org-icalendar-include-todo 'all))
  24306. (not (member org-archive-tag (org-get-tags-at)))
  24307. )
  24308. (setq hd (match-string 3)
  24309. summary (org-icalendar-cleanup-string
  24310. (org-entry-get nil "SUMMARY"))
  24311. desc (org-icalendar-cleanup-string
  24312. (or (org-entry-get nil "DESCRIPTION")
  24313. (and org-icalendar-include-body (org-get-entry)))
  24314. t org-icalendar-include-body)
  24315. location (org-icalendar-cleanup-string
  24316. (org-entry-get nil "LOCATION")))
  24317. (if (string-match org-bracket-link-regexp hd)
  24318. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24319. (match-string 1 hd))
  24320. t t hd)))
  24321. (if (string-match org-priority-regexp hd)
  24322. (setq pri (string-to-char (match-string 2 hd))
  24323. hd (concat (substring hd 0 (match-beginning 1))
  24324. (substring hd (match-end 1))))
  24325. (setq pri org-default-priority))
  24326. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24327. (- org-lowest-priority org-highest-priority))))))
  24328. (princ (format "BEGIN:VTODO
  24329. %s
  24330. SUMMARY:%s%s%s
  24331. CATEGORIES:%s
  24332. SEQUENCE:1
  24333. PRIORITY:%d
  24334. STATUS:%s
  24335. END:VTODO\n"
  24336. dts
  24337. (or summary hd)
  24338. (if (and location (string-match "\\S-" location))
  24339. (concat "\nLOCATION: " location) "")
  24340. (if (and desc (string-match "\\S-" desc))
  24341. (concat "\nDESCRIPTION: " desc) "")
  24342. category pri status)))))))))
  24343. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24344. "Take out stuff and quote what needs to be quoted.
  24345. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24346. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24347. characters."
  24348. (if (not s)
  24349. nil
  24350. (when is-body
  24351. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24352. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24353. (while (string-match re s) (setq s (replace-match "" t t s)))
  24354. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24355. (let ((start 0))
  24356. (while (string-match "\\([,;\\]\\)" s start)
  24357. (setq start (+ (match-beginning 0) 2)
  24358. s (replace-match "\\\\\\1" nil nil s))))
  24359. (when is-body
  24360. (while (string-match "[ \t]*\n[ \t]*" s)
  24361. (setq s (replace-match "\\n" t t s))))
  24362. (setq s (org-trim s))
  24363. (if is-body
  24364. (if maxlength
  24365. (if (and (numberp maxlength)
  24366. (> (length s) maxlength))
  24367. (setq s (substring s 0 maxlength)))))
  24368. s))
  24369. (defun org-get-entry ()
  24370. "Clean-up description string."
  24371. (save-excursion
  24372. (org-back-to-heading t)
  24373. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24374. (defun org-start-icalendar-file (name)
  24375. "Start an iCalendar file by inserting the header."
  24376. (let ((user user-full-name)
  24377. (name (or name "unknown"))
  24378. (timezone (cadr (current-time-zone))))
  24379. (princ
  24380. (format "BEGIN:VCALENDAR
  24381. VERSION:2.0
  24382. X-WR-CALNAME:%s
  24383. PRODID:-//%s//Emacs with Org-mode//EN
  24384. X-WR-TIMEZONE:%s
  24385. CALSCALE:GREGORIAN\n" name user timezone))))
  24386. (defun org-finish-icalendar-file ()
  24387. "Finish an iCalendar file by inserting the END statement."
  24388. (princ "END:VCALENDAR\n"))
  24389. (defun org-ical-ts-to-string (s keyword &optional inc)
  24390. "Take a time string S and convert it to iCalendar format.
  24391. KEYWORD is added in front, to make a complete line like DTSTART....
  24392. When INC is non-nil, increase the hour by two (if time string contains
  24393. a time), or the day by one (if it does not contain a time)."
  24394. (let ((t1 (org-parse-time-string s 'nodefault))
  24395. t2 fmt have-time time)
  24396. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24397. (setq t2 t1 have-time t)
  24398. (setq t2 (org-parse-time-string s)))
  24399. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24400. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24401. (when inc
  24402. (if have-time
  24403. (if org-agenda-default-appointment-duration
  24404. (setq mi (+ org-agenda-default-appointment-duration mi))
  24405. (setq h (+ 2 h)))
  24406. (setq d (1+ d))))
  24407. (setq time (encode-time s mi h d m y)))
  24408. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24409. (concat keyword (format-time-string fmt time))))
  24410. ;;; XOXO export
  24411. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24412. (with-current-buffer buffer
  24413. (apply 'insert output)))
  24414. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24415. (defun org-export-as-xoxo (&optional buffer)
  24416. "Export the org buffer as XOXO.
  24417. The XOXO buffer is named *xoxo-<source buffer name>*"
  24418. (interactive (list (current-buffer)))
  24419. ;; A quickie abstraction
  24420. ;; Output everything as XOXO
  24421. (with-current-buffer (get-buffer buffer)
  24422. (let* ((pos (point))
  24423. (opt-plist (org-combine-plists (org-default-export-plist)
  24424. (org-infile-export-plist)))
  24425. (filename (concat (file-name-as-directory
  24426. (org-export-directory :xoxo opt-plist))
  24427. (file-name-sans-extension
  24428. (file-name-nondirectory buffer-file-name))
  24429. ".html"))
  24430. (out (find-file-noselect filename))
  24431. (last-level 1)
  24432. (hanging-li nil))
  24433. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24434. ;; Check the output buffer is empty.
  24435. (with-current-buffer out (erase-buffer))
  24436. ;; Kick off the output
  24437. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24438. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24439. (let* ((hd (match-string-no-properties 1))
  24440. (level (length hd))
  24441. (text (concat
  24442. (match-string-no-properties 2)
  24443. (save-excursion
  24444. (goto-char (match-end 0))
  24445. (let ((str ""))
  24446. (catch 'loop
  24447. (while 't
  24448. (forward-line)
  24449. (if (looking-at "^[ \t]\\(.*\\)")
  24450. (setq str (concat str (match-string-no-properties 1)))
  24451. (throw 'loop str)))))))))
  24452. ;; Handle level rendering
  24453. (cond
  24454. ((> level last-level)
  24455. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24456. ((< level last-level)
  24457. (dotimes (- (- last-level level) 1)
  24458. (if hanging-li
  24459. (org-export-as-xoxo-insert-into out "</li>\n"))
  24460. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24461. (when hanging-li
  24462. (org-export-as-xoxo-insert-into out "</li>\n")
  24463. (setq hanging-li nil)))
  24464. ((equal level last-level)
  24465. (if hanging-li
  24466. (org-export-as-xoxo-insert-into out "</li>\n")))
  24467. )
  24468. (setq last-level level)
  24469. ;; And output the new li
  24470. (setq hanging-li 't)
  24471. (if (equal ?+ (elt text 0))
  24472. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24473. (org-export-as-xoxo-insert-into out "<li>" text))))
  24474. ;; Finally finish off the ol
  24475. (dotimes (- last-level 1)
  24476. (if hanging-li
  24477. (org-export-as-xoxo-insert-into out "</li>\n"))
  24478. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24479. (goto-char pos)
  24480. ;; Finish the buffer off and clean it up.
  24481. (switch-to-buffer-other-window out)
  24482. (indent-region (point-min) (point-max) nil)
  24483. (save-buffer)
  24484. (goto-char (point-min))
  24485. )))
  24486. ;;;; Key bindings
  24487. ;; Make `C-c C-x' a prefix key
  24488. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24489. ;; TAB key with modifiers
  24490. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24491. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24492. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24493. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24494. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24495. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24496. ;; The following line is necessary under Suse GNU/Linux
  24497. (unless (featurep 'xemacs)
  24498. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24499. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24500. (define-key org-mode-map [backtab] 'org-shifttab)
  24501. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24502. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24503. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24504. ;; Cursor keys with modifiers
  24505. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24506. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24507. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24508. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24509. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24510. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24511. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24512. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24513. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24514. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24515. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24516. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24517. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24518. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24519. ;;; Extra keys for tty access.
  24520. ;; We only set them when really needed because otherwise the
  24521. ;; menus don't show the simple keys
  24522. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24523. (not window-system))
  24524. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24525. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24526. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24527. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24528. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24529. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24530. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24531. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24532. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24533. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24534. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24535. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24536. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24537. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24538. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24539. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24540. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24541. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24542. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24543. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24544. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24545. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24546. ;; All the other keys
  24547. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24548. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24549. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24550. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24551. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24552. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24553. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24554. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24555. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24556. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24557. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24558. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24559. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24560. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24561. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24562. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24563. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24564. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24565. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24566. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24567. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24568. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24569. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24570. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24571. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24572. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24573. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24574. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24575. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24576. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24577. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24578. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24579. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24580. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24581. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24582. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24583. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24584. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24585. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24586. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24587. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24588. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24589. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24590. (org-defkey org-mode-map "\C-m" 'org-return)
  24591. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24592. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24593. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24594. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24595. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24596. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24597. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24598. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24599. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24600. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24601. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24602. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24603. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24604. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24605. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24606. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24607. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24608. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24609. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24610. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24611. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24612. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24613. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24614. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24615. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24616. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24617. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24618. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24619. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24620. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24621. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24622. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24623. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24624. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24625. (when (featurep 'xemacs)
  24626. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24627. (defsubst org-table-p () (org-at-table-p))
  24628. (defun org-self-insert-command (N)
  24629. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24630. If the cursor is in a table looking at whitespace, the whitespace is
  24631. overwritten, and the table is not marked as requiring realignment."
  24632. (interactive "p")
  24633. (if (and (org-table-p)
  24634. (progn
  24635. ;; check if we blank the field, and if that triggers align
  24636. (and org-table-auto-blank-field
  24637. (member last-command
  24638. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24639. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24640. ;; got extra space, this field does not determine column width
  24641. (let (org-table-may-need-update) (org-table-blank-field))
  24642. ;; no extra space, this field may determine column width
  24643. (org-table-blank-field)))
  24644. t)
  24645. (eq N 1)
  24646. (looking-at "[^|\n]* |"))
  24647. (let (org-table-may-need-update)
  24648. (goto-char (1- (match-end 0)))
  24649. (delete-backward-char 1)
  24650. (goto-char (match-beginning 0))
  24651. (self-insert-command N))
  24652. (setq org-table-may-need-update t)
  24653. (self-insert-command N)
  24654. (org-fix-tags-on-the-fly)))
  24655. (defun org-fix-tags-on-the-fly ()
  24656. (when (and (equal (char-after (point-at-bol)) ?*)
  24657. (org-on-heading-p))
  24658. (org-align-tags-here org-tags-column)))
  24659. (defun org-delete-backward-char (N)
  24660. "Like `delete-backward-char', insert whitespace at field end in tables.
  24661. When deleting backwards, in tables this function will insert whitespace in
  24662. front of the next \"|\" separator, to keep the table aligned. The table will
  24663. still be marked for re-alignment if the field did fill the entire column,
  24664. because, in this case the deletion might narrow the column."
  24665. (interactive "p")
  24666. (if (and (org-table-p)
  24667. (eq N 1)
  24668. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24669. (looking-at ".*?|"))
  24670. (let ((pos (point))
  24671. (noalign (looking-at "[^|\n\r]* |"))
  24672. (c org-table-may-need-update))
  24673. (backward-delete-char N)
  24674. (skip-chars-forward "^|")
  24675. (insert " ")
  24676. (goto-char (1- pos))
  24677. ;; noalign: if there were two spaces at the end, this field
  24678. ;; does not determine the width of the column.
  24679. (if noalign (setq org-table-may-need-update c)))
  24680. (backward-delete-char N)
  24681. (org-fix-tags-on-the-fly)))
  24682. (defun org-delete-char (N)
  24683. "Like `delete-char', but insert whitespace at field end in tables.
  24684. When deleting characters, in tables this function will insert whitespace in
  24685. front of the next \"|\" separator, to keep the table aligned. The table will
  24686. still be marked for re-alignment if the field did fill the entire column,
  24687. because, in this case the deletion might narrow the column."
  24688. (interactive "p")
  24689. (if (and (org-table-p)
  24690. (not (bolp))
  24691. (not (= (char-after) ?|))
  24692. (eq N 1))
  24693. (if (looking-at ".*?|")
  24694. (let ((pos (point))
  24695. (noalign (looking-at "[^|\n\r]* |"))
  24696. (c org-table-may-need-update))
  24697. (replace-match (concat
  24698. (substring (match-string 0) 1 -1)
  24699. " |"))
  24700. (goto-char pos)
  24701. ;; noalign: if there were two spaces at the end, this field
  24702. ;; does not determine the width of the column.
  24703. (if noalign (setq org-table-may-need-update c)))
  24704. (delete-char N))
  24705. (delete-char N)
  24706. (org-fix-tags-on-the-fly)))
  24707. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24708. (put 'org-self-insert-command 'delete-selection t)
  24709. (put 'orgtbl-self-insert-command 'delete-selection t)
  24710. (put 'org-delete-char 'delete-selection 'supersede)
  24711. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24712. ;; Make `flyspell-mode' delay after some commands
  24713. (put 'org-self-insert-command 'flyspell-delayed t)
  24714. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24715. (put 'org-delete-char 'flyspell-delayed t)
  24716. (put 'org-delete-backward-char 'flyspell-delayed t)
  24717. ;; Make pabbrev-mode expand after org-mode commands
  24718. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24719. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24720. ;; How to do this: Measure non-white length of current string
  24721. ;; If equal to column width, we should realign.
  24722. (defun org-remap (map &rest commands)
  24723. "In MAP, remap the functions given in COMMANDS.
  24724. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24725. (let (new old)
  24726. (while commands
  24727. (setq old (pop commands) new (pop commands))
  24728. (if (fboundp 'command-remapping)
  24729. (org-defkey map (vector 'remap old) new)
  24730. (substitute-key-definition old new map global-map)))))
  24731. (when (eq org-enable-table-editor 'optimized)
  24732. ;; If the user wants maximum table support, we need to hijack
  24733. ;; some standard editing functions
  24734. (org-remap org-mode-map
  24735. 'self-insert-command 'org-self-insert-command
  24736. 'delete-char 'org-delete-char
  24737. 'delete-backward-char 'org-delete-backward-char)
  24738. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24739. (defun org-shiftcursor-error ()
  24740. "Throw an error because Shift-Cursor command was applied in wrong context."
  24741. (error "This command is active in special context like tables, headlines or timestamps"))
  24742. (defun org-shifttab (&optional arg)
  24743. "Global visibility cycling or move to previous table field.
  24744. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24745. on context.
  24746. See the individual commands for more information."
  24747. (interactive "P")
  24748. (cond
  24749. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24750. (arg (message "Content view to level: ")
  24751. (org-content (prefix-numeric-value arg))
  24752. (setq org-cycle-global-status 'overview))
  24753. (t (call-interactively 'org-global-cycle))))
  24754. (defun org-shiftmetaleft ()
  24755. "Promote subtree or delete table column.
  24756. Calls `org-promote-subtree', `org-outdent-item',
  24757. or `org-table-delete-column', depending on context.
  24758. See the individual commands for more information."
  24759. (interactive)
  24760. (cond
  24761. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24762. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24763. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24764. (t (org-shiftcursor-error))))
  24765. (defun org-shiftmetaright ()
  24766. "Demote subtree or insert table column.
  24767. Calls `org-demote-subtree', `org-indent-item',
  24768. or `org-table-insert-column', depending on context.
  24769. See the individual commands for more information."
  24770. (interactive)
  24771. (cond
  24772. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24773. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24774. ((org-at-item-p) (call-interactively 'org-indent-item))
  24775. (t (org-shiftcursor-error))))
  24776. (defun org-shiftmetaup (&optional arg)
  24777. "Move subtree up or kill table row.
  24778. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24779. `org-move-item-up' depending on context. See the individual commands
  24780. for more information."
  24781. (interactive "P")
  24782. (cond
  24783. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24784. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24785. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24786. (t (org-shiftcursor-error))))
  24787. (defun org-shiftmetadown (&optional arg)
  24788. "Move subtree down or insert table row.
  24789. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24790. `org-move-item-down', depending on context. See the individual
  24791. commands for more information."
  24792. (interactive "P")
  24793. (cond
  24794. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24795. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24796. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24797. (t (org-shiftcursor-error))))
  24798. (defun org-metaleft (&optional arg)
  24799. "Promote heading or move table column to left.
  24800. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24801. With no specific context, calls the Emacs default `backward-word'.
  24802. See the individual commands for more information."
  24803. (interactive "P")
  24804. (cond
  24805. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24806. ((or (org-on-heading-p) (org-region-active-p))
  24807. (call-interactively 'org-do-promote))
  24808. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24809. (t (call-interactively 'backward-word))))
  24810. (defun org-metaright (&optional arg)
  24811. "Demote subtree or move table column to right.
  24812. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24813. With no specific context, calls the Emacs default `forward-word'.
  24814. See the individual commands for more information."
  24815. (interactive "P")
  24816. (cond
  24817. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24818. ((or (org-on-heading-p) (org-region-active-p))
  24819. (call-interactively 'org-do-demote))
  24820. ((org-at-item-p) (call-interactively 'org-indent-item))
  24821. (t (call-interactively 'forward-word))))
  24822. (defun org-metaup (&optional arg)
  24823. "Move subtree up or move table row up.
  24824. Calls `org-move-subtree-up' or `org-table-move-row' or
  24825. `org-move-item-up', depending on context. See the individual commands
  24826. for more information."
  24827. (interactive "P")
  24828. (cond
  24829. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24830. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24831. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24832. (t (transpose-lines 1) (beginning-of-line -1))))
  24833. (defun org-metadown (&optional arg)
  24834. "Move subtree down or move table row down.
  24835. Calls `org-move-subtree-down' or `org-table-move-row' or
  24836. `org-move-item-down', depending on context. See the individual
  24837. commands for more information."
  24838. (interactive "P")
  24839. (cond
  24840. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24841. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24842. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24843. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24844. (defun org-shiftup (&optional arg)
  24845. "Increase item in timestamp or increase priority of current headline.
  24846. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24847. depending on context. See the individual commands for more information."
  24848. (interactive "P")
  24849. (cond
  24850. ((org-at-timestamp-p t)
  24851. (call-interactively (if org-edit-timestamp-down-means-later
  24852. 'org-timestamp-down 'org-timestamp-up)))
  24853. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24854. ((org-at-item-p) (call-interactively 'org-previous-item))
  24855. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24856. (defun org-shiftdown (&optional arg)
  24857. "Decrease item in timestamp or decrease priority of current headline.
  24858. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24859. depending on context. See the individual commands for more information."
  24860. (interactive "P")
  24861. (cond
  24862. ((org-at-timestamp-p t)
  24863. (call-interactively (if org-edit-timestamp-down-means-later
  24864. 'org-timestamp-up 'org-timestamp-down)))
  24865. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24866. (t (call-interactively 'org-next-item))))
  24867. (defun org-shiftright ()
  24868. "Next TODO keyword or timestamp one day later, depending on context."
  24869. (interactive)
  24870. (cond
  24871. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24872. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24873. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24874. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24875. (t (org-shiftcursor-error))))
  24876. (defun org-shiftleft ()
  24877. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24878. (interactive)
  24879. (cond
  24880. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24881. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24882. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24883. ((org-at-property-p)
  24884. (call-interactively 'org-property-previous-allowed-value))
  24885. (t (org-shiftcursor-error))))
  24886. (defun org-shiftcontrolright ()
  24887. "Switch to next TODO set."
  24888. (interactive)
  24889. (cond
  24890. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24891. (t (org-shiftcursor-error))))
  24892. (defun org-shiftcontrolleft ()
  24893. "Switch to previous TODO set."
  24894. (interactive)
  24895. (cond
  24896. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24897. (t (org-shiftcursor-error))))
  24898. (defun org-ctrl-c-ret ()
  24899. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24900. (interactive)
  24901. (cond
  24902. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24903. (t (call-interactively 'org-insert-heading))))
  24904. (defun org-copy-special ()
  24905. "Copy region in table or copy current subtree.
  24906. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24907. See the individual commands for more information."
  24908. (interactive)
  24909. (call-interactively
  24910. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24911. (defun org-cut-special ()
  24912. "Cut region in table or cut current subtree.
  24913. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24914. See the individual commands for more information."
  24915. (interactive)
  24916. (call-interactively
  24917. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24918. (defun org-paste-special (arg)
  24919. "Paste rectangular region into table, or past subtree relative to level.
  24920. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24921. See the individual commands for more information."
  24922. (interactive "P")
  24923. (if (org-at-table-p)
  24924. (org-table-paste-rectangle)
  24925. (org-paste-subtree arg)))
  24926. (defun org-ctrl-c-ctrl-c (&optional arg)
  24927. "Set tags in headline, or update according to changed information at point.
  24928. This command does many different things, depending on context:
  24929. - If the cursor is in a headline, prompt for tags and insert them
  24930. into the current line, aligned to `org-tags-column'. When called
  24931. with prefix arg, realign all tags in the current buffer.
  24932. - If the cursor is in one of the special #+KEYWORD lines, this
  24933. triggers scanning the buffer for these lines and updating the
  24934. information.
  24935. - If the cursor is inside a table, realign the table. This command
  24936. works even if the automatic table editor has been turned off.
  24937. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24938. the entire table.
  24939. - If the cursor is a the beginning of a dynamic block, update it.
  24940. - If the cursor is inside a table created by the table.el package,
  24941. activate that table.
  24942. - If the current buffer is a remember buffer, close note and file it.
  24943. with a prefix argument, file it without further interaction to the default
  24944. location.
  24945. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24946. links in this buffer.
  24947. - If the cursor is on a numbered item in a plain list, renumber the
  24948. ordered list.
  24949. - If the cursor is on a checkbox, toggle it."
  24950. (interactive "P")
  24951. (let ((org-enable-table-editor t))
  24952. (cond
  24953. ((or org-clock-overlays
  24954. org-occur-highlights
  24955. org-latex-fragment-image-overlays)
  24956. (org-remove-clock-overlays)
  24957. (org-remove-occur-highlights)
  24958. (org-remove-latex-fragment-image-overlays)
  24959. (message "Temporary highlights/overlays removed from current buffer"))
  24960. ((and (local-variable-p 'org-finish-function (current-buffer))
  24961. (fboundp org-finish-function))
  24962. (funcall org-finish-function))
  24963. ((org-at-property-p)
  24964. (call-interactively 'org-property-action))
  24965. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24966. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24967. ((org-at-table.el-p)
  24968. (require 'table)
  24969. (beginning-of-line 1)
  24970. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24971. (call-interactively 'table-recognize-table))
  24972. ((org-at-table-p)
  24973. (org-table-maybe-eval-formula)
  24974. (if arg
  24975. (call-interactively 'org-table-recalculate)
  24976. (org-table-maybe-recalculate-line))
  24977. (call-interactively 'org-table-align))
  24978. ((org-at-item-checkbox-p)
  24979. (call-interactively 'org-toggle-checkbox))
  24980. ((org-at-item-p)
  24981. (call-interactively 'org-maybe-renumber-ordered-list))
  24982. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24983. ;; Dynamic block
  24984. (beginning-of-line 1)
  24985. (org-update-dblock))
  24986. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24987. (cond
  24988. ((equal (match-string 1) "TBLFM")
  24989. ;; Recalculate the table before this line
  24990. (save-excursion
  24991. (beginning-of-line 1)
  24992. (skip-chars-backward " \r\n\t")
  24993. (if (org-at-table-p)
  24994. (org-call-with-arg 'org-table-recalculate t))))
  24995. (t
  24996. (call-interactively 'org-mode-restart))))
  24997. (t (error "C-c C-c can do nothing useful at this location.")))))
  24998. (defun org-mode-restart ()
  24999. "Restart Org-mode, to scan again for special lines.
  25000. Also updates the keyword regular expressions."
  25001. (interactive)
  25002. (let ((org-inhibit-startup t)) (org-mode))
  25003. (message "Org-mode restarted to refresh keyword and special line setup"))
  25004. (defun org-kill-note-or-show-branches ()
  25005. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25006. (interactive)
  25007. (if (not org-finish-function)
  25008. (call-interactively 'show-branches)
  25009. (let ((org-note-abort t))
  25010. (funcall org-finish-function))))
  25011. (defun org-return (&optional indent)
  25012. "Goto next table row or insert a newline.
  25013. Calls `org-table-next-row' or `newline', depending on context.
  25014. See the individual commands for more information."
  25015. (interactive)
  25016. (cond
  25017. ((bobp) (if indent (newline-and-indent) (newline)))
  25018. ((and (org-at-heading-p)
  25019. (looking-at
  25020. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25021. (org-show-entry)
  25022. (end-of-line 1)
  25023. (newline))
  25024. ((org-at-table-p)
  25025. (org-table-justify-field-maybe)
  25026. (call-interactively 'org-table-next-row))
  25027. (t (if indent (newline-and-indent) (newline)))))
  25028. (defun org-return-indent ()
  25029. (interactive)
  25030. "Goto next table row or insert a newline and indent.
  25031. Calls `org-table-next-row' or `newline-and-indent', depending on
  25032. context. See the individual commands for more information."
  25033. (org-return t))
  25034. (defun org-ctrl-c-minus ()
  25035. "Insert separator line in table or modify bullet type in list.
  25036. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25037. depending on context."
  25038. (interactive)
  25039. (cond
  25040. ((org-at-table-p)
  25041. (call-interactively 'org-table-insert-hline))
  25042. ((org-on-heading-p)
  25043. ;; Convert to item
  25044. (save-excursion
  25045. (beginning-of-line 1)
  25046. (if (looking-at "\\*+ ")
  25047. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25048. ((org-in-item-p)
  25049. (call-interactively 'org-cycle-list-bullet))
  25050. (t (error "`C-c -' does have no function here."))))
  25051. (defun org-meta-return (&optional arg)
  25052. "Insert a new heading or wrap a region in a table.
  25053. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25054. See the individual commands for more information."
  25055. (interactive "P")
  25056. (cond
  25057. ((org-at-table-p)
  25058. (call-interactively 'org-table-wrap-region))
  25059. (t (call-interactively 'org-insert-heading))))
  25060. ;;; Menu entries
  25061. ;; Define the Org-mode menus
  25062. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25063. '("Tbl"
  25064. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25065. ["Next Field" org-cycle (org-at-table-p)]
  25066. ["Previous Field" org-shifttab (org-at-table-p)]
  25067. ["Next Row" org-return (org-at-table-p)]
  25068. "--"
  25069. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25070. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25071. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25072. "--"
  25073. ("Column"
  25074. ["Move Column Left" org-metaleft (org-at-table-p)]
  25075. ["Move Column Right" org-metaright (org-at-table-p)]
  25076. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25077. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25078. ("Row"
  25079. ["Move Row Up" org-metaup (org-at-table-p)]
  25080. ["Move Row Down" org-metadown (org-at-table-p)]
  25081. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25082. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25083. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25084. "--"
  25085. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25086. ("Rectangle"
  25087. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25088. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25089. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25090. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25091. "--"
  25092. ("Calculate"
  25093. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25094. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25095. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25096. "--"
  25097. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25098. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25099. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25100. "--"
  25101. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25102. "--"
  25103. ["Sum Column/Rectangle" org-table-sum
  25104. (or (org-at-table-p) (org-region-active-p))]
  25105. ["Which Column?" org-table-current-column (org-at-table-p)])
  25106. ["Debug Formulas"
  25107. org-table-toggle-formula-debugger
  25108. :style toggle :selected org-table-formula-debug]
  25109. ["Show Col/Row Numbers"
  25110. org-table-toggle-coordinate-overlays
  25111. :style toggle :selected org-table-overlay-coordinates]
  25112. "--"
  25113. ["Create" org-table-create (and (not (org-at-table-p))
  25114. org-enable-table-editor)]
  25115. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25116. ["Import from File" org-table-import (not (org-at-table-p))]
  25117. ["Export to File" org-table-export (org-at-table-p)]
  25118. "--"
  25119. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25120. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25121. '("Org"
  25122. ("Show/Hide"
  25123. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25124. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25125. ["Sparse Tree" org-occur t]
  25126. ["Reveal Context" org-reveal t]
  25127. ["Show All" show-all t]
  25128. "--"
  25129. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25130. "--"
  25131. ["New Heading" org-insert-heading t]
  25132. ("Navigate Headings"
  25133. ["Up" outline-up-heading t]
  25134. ["Next" outline-next-visible-heading t]
  25135. ["Previous" outline-previous-visible-heading t]
  25136. ["Next Same Level" outline-forward-same-level t]
  25137. ["Previous Same Level" outline-backward-same-level t]
  25138. "--"
  25139. ["Jump" org-goto t])
  25140. ("Edit Structure"
  25141. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25142. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25143. "--"
  25144. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25145. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25146. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25147. "--"
  25148. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25149. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25150. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25151. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25152. "--"
  25153. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25154. "--"
  25155. ["Convert to odd levels" org-convert-to-odd-levels t]
  25156. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25157. ("Editing"
  25158. ["Emphasis..." org-emphasize t])
  25159. ("Archive"
  25160. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25161. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25162. ; :active t :keys "C-u C-c C-x C-a"]
  25163. ["Sparse trees open ARCHIVE trees"
  25164. (setq org-sparse-tree-open-archived-trees
  25165. (not org-sparse-tree-open-archived-trees))
  25166. :style toggle :selected org-sparse-tree-open-archived-trees]
  25167. ["Cycling opens ARCHIVE trees"
  25168. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25169. :style toggle :selected org-cycle-open-archived-trees]
  25170. ["Agenda includes ARCHIVE trees"
  25171. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25172. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25173. "--"
  25174. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25175. ; ["Check and Move Children" (org-archive-subtree '(4))
  25176. ; :active t :keys "C-u C-c C-x C-s"]
  25177. )
  25178. "--"
  25179. ("TODO Lists"
  25180. ["TODO/DONE/-" org-todo t]
  25181. ("Select keyword"
  25182. ["Next keyword" org-shiftright (org-on-heading-p)]
  25183. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25184. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25185. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25186. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25187. ["Show TODO Tree" org-show-todo-tree t]
  25188. ["Global TODO list" org-todo-list t]
  25189. "--"
  25190. ["Set Priority" org-priority t]
  25191. ["Priority Up" org-shiftup t]
  25192. ["Priority Down" org-shiftdown t])
  25193. ("TAGS and Properties"
  25194. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25195. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25196. "--"
  25197. ["Set property" 'org-set-property t]
  25198. ["Column view of properties" org-columns t]
  25199. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25200. ("Dates and Scheduling"
  25201. ["Timestamp" org-time-stamp t]
  25202. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25203. ("Change Date"
  25204. ["1 Day Later" org-shiftright t]
  25205. ["1 Day Earlier" org-shiftleft t]
  25206. ["1 ... Later" org-shiftup t]
  25207. ["1 ... Earlier" org-shiftdown t])
  25208. ["Compute Time Range" org-evaluate-time-range t]
  25209. ["Schedule Item" org-schedule t]
  25210. ["Deadline" org-deadline t]
  25211. "--"
  25212. ["Custom time format" org-toggle-time-stamp-overlays
  25213. :style radio :selected org-display-custom-times]
  25214. "--"
  25215. ["Goto Calendar" org-goto-calendar t]
  25216. ["Date from Calendar" org-date-from-calendar t])
  25217. ("Logging work"
  25218. ["Clock in" org-clock-in t]
  25219. ["Clock out" org-clock-out t]
  25220. ["Clock cancel" org-clock-cancel t]
  25221. ["Goto running clock" org-clock-goto t]
  25222. ["Display times" org-clock-display t]
  25223. ["Create clock table" org-clock-report t]
  25224. "--"
  25225. ["Record DONE time"
  25226. (progn (setq org-log-done (not org-log-done))
  25227. (message "Switching to %s will %s record a timestamp"
  25228. (car org-done-keywords)
  25229. (if org-log-done "automatically" "not")))
  25230. :style toggle :selected org-log-done])
  25231. "--"
  25232. ["Agenda Command..." org-agenda t]
  25233. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25234. ("File List for Agenda")
  25235. ("Special views current file"
  25236. ["TODO Tree" org-show-todo-tree t]
  25237. ["Check Deadlines" org-check-deadlines t]
  25238. ["Timeline" org-timeline t]
  25239. ["Tags Tree" org-tags-sparse-tree t])
  25240. "--"
  25241. ("Hyperlinks"
  25242. ["Store Link (Global)" org-store-link t]
  25243. ["Insert Link" org-insert-link t]
  25244. ["Follow Link" org-open-at-point t]
  25245. "--"
  25246. ["Next link" org-next-link t]
  25247. ["Previous link" org-previous-link t]
  25248. "--"
  25249. ["Descriptive Links"
  25250. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25251. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25252. ["Literal Links"
  25253. (progn
  25254. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25255. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25256. "--"
  25257. ["Export/Publish..." org-export t]
  25258. ("LaTeX"
  25259. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25260. :selected org-cdlatex-mode]
  25261. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25262. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25263. ["Modify math symbol" org-cdlatex-math-modify
  25264. (org-inside-LaTeX-fragment-p)]
  25265. ["Export LaTeX fragments as images"
  25266. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25267. :style toggle :selected org-export-with-LaTeX-fragments])
  25268. "--"
  25269. ("Documentation"
  25270. ["Show Version" org-version t]
  25271. ["Info Documentation" org-info t])
  25272. ("Customize"
  25273. ["Browse Org Group" org-customize t]
  25274. "--"
  25275. ["Expand This Menu" org-create-customize-menu
  25276. (fboundp 'customize-menu-create)])
  25277. "--"
  25278. ["Refresh setup" org-mode-restart t]
  25279. ))
  25280. (defun org-info (&optional node)
  25281. "Read documentation for Org-mode in the info system.
  25282. With optional NODE, go directly to that node."
  25283. (interactive)
  25284. (require 'info)
  25285. (Info-goto-node (format "(org)%s" (or node ""))))
  25286. (defun org-install-agenda-files-menu ()
  25287. (let ((bl (buffer-list)))
  25288. (save-excursion
  25289. (while bl
  25290. (set-buffer (pop bl))
  25291. (if (org-mode-p) (setq bl nil)))
  25292. (when (org-mode-p)
  25293. (easy-menu-change
  25294. '("Org") "File List for Agenda"
  25295. (append
  25296. (list
  25297. ["Edit File List" (org-edit-agenda-file-list) t]
  25298. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25299. ["Remove Current File from List" org-remove-file t]
  25300. ["Cycle through agenda files" org-cycle-agenda-files t]
  25301. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25302. "--")
  25303. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25304. ;;;; Documentation
  25305. (defun org-customize ()
  25306. "Call the customize function with org as argument."
  25307. (interactive)
  25308. (customize-browse 'org))
  25309. (defun org-create-customize-menu ()
  25310. "Create a full customization menu for Org-mode, insert it into the menu."
  25311. (interactive)
  25312. (if (fboundp 'customize-menu-create)
  25313. (progn
  25314. (easy-menu-change
  25315. '("Org") "Customize"
  25316. `(["Browse Org group" org-customize t]
  25317. "--"
  25318. ,(customize-menu-create 'org)
  25319. ["Set" Custom-set t]
  25320. ["Save" Custom-save t]
  25321. ["Reset to Current" Custom-reset-current t]
  25322. ["Reset to Saved" Custom-reset-saved t]
  25323. ["Reset to Standard Settings" Custom-reset-standard t]))
  25324. (message "\"Org\"-menu now contains full customization menu"))
  25325. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25326. ;;;; Miscellaneous stuff
  25327. ;;; Generally useful functions
  25328. (defun org-context ()
  25329. "Return a list of contexts of the current cursor position.
  25330. If several contexts apply, all are returned.
  25331. Each context entry is a list with a symbol naming the context, and
  25332. two positions indicating start and end of the context. Possible
  25333. contexts are:
  25334. :headline anywhere in a headline
  25335. :headline-stars on the leading stars in a headline
  25336. :todo-keyword on a TODO keyword (including DONE) in a headline
  25337. :tags on the TAGS in a headline
  25338. :priority on the priority cookie in a headline
  25339. :item on the first line of a plain list item
  25340. :item-bullet on the bullet/number of a plain list item
  25341. :checkbox on the checkbox in a plain list item
  25342. :table in an org-mode table
  25343. :table-special on a special filed in a table
  25344. :table-table in a table.el table
  25345. :link on a hyperlink
  25346. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25347. :target on a <<target>>
  25348. :radio-target on a <<<radio-target>>>
  25349. :latex-fragment on a LaTeX fragment
  25350. :latex-preview on a LaTeX fragment with overlayed preview image
  25351. This function expects the position to be visible because it uses font-lock
  25352. faces as a help to recognize the following contexts: :table-special, :link,
  25353. and :keyword."
  25354. (let* ((f (get-text-property (point) 'face))
  25355. (faces (if (listp f) f (list f)))
  25356. (p (point)) clist o)
  25357. ;; First the large context
  25358. (cond
  25359. ((org-on-heading-p t)
  25360. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25361. (when (progn
  25362. (beginning-of-line 1)
  25363. (looking-at org-todo-line-tags-regexp))
  25364. (push (org-point-in-group p 1 :headline-stars) clist)
  25365. (push (org-point-in-group p 2 :todo-keyword) clist)
  25366. (push (org-point-in-group p 4 :tags) clist))
  25367. (goto-char p)
  25368. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25369. (if (looking-at "\\[#[A-Z0-9]\\]")
  25370. (push (org-point-in-group p 0 :priority) clist)))
  25371. ((org-at-item-p)
  25372. (push (org-point-in-group p 2 :item-bullet) clist)
  25373. (push (list :item (point-at-bol)
  25374. (save-excursion (org-end-of-item) (point)))
  25375. clist)
  25376. (and (org-at-item-checkbox-p)
  25377. (push (org-point-in-group p 0 :checkbox) clist)))
  25378. ((org-at-table-p)
  25379. (push (list :table (org-table-begin) (org-table-end)) clist)
  25380. (if (memq 'org-formula faces)
  25381. (push (list :table-special
  25382. (previous-single-property-change p 'face)
  25383. (next-single-property-change p 'face)) clist)))
  25384. ((org-at-table-p 'any)
  25385. (push (list :table-table) clist)))
  25386. (goto-char p)
  25387. ;; Now the small context
  25388. (cond
  25389. ((org-at-timestamp-p)
  25390. (push (org-point-in-group p 0 :timestamp) clist))
  25391. ((memq 'org-link faces)
  25392. (push (list :link
  25393. (previous-single-property-change p 'face)
  25394. (next-single-property-change p 'face)) clist))
  25395. ((memq 'org-special-keyword faces)
  25396. (push (list :keyword
  25397. (previous-single-property-change p 'face)
  25398. (next-single-property-change p 'face)) clist))
  25399. ((org-on-target-p)
  25400. (push (org-point-in-group p 0 :target) clist)
  25401. (goto-char (1- (match-beginning 0)))
  25402. (if (looking-at org-radio-target-regexp)
  25403. (push (org-point-in-group p 0 :radio-target) clist))
  25404. (goto-char p))
  25405. ((setq o (car (delq nil
  25406. (mapcar
  25407. (lambda (x)
  25408. (if (memq x org-latex-fragment-image-overlays) x))
  25409. (org-overlays-at (point))))))
  25410. (push (list :latex-fragment
  25411. (org-overlay-start o) (org-overlay-end o)) clist)
  25412. (push (list :latex-preview
  25413. (org-overlay-start o) (org-overlay-end o)) clist))
  25414. ((org-inside-LaTeX-fragment-p)
  25415. ;; FIXME: positions wrong.
  25416. (push (list :latex-fragment (point) (point)) clist)))
  25417. (setq clist (nreverse (delq nil clist)))
  25418. clist))
  25419. ;; FIXME: Compare with at-regexp-p Do we need both?
  25420. (defun org-in-regexp (re &optional nlines visually)
  25421. "Check if point is inside a match of regexp.
  25422. Normally only the current line is checked, but you can include NLINES extra
  25423. lines both before and after point into the search.
  25424. If VISUALLY is set, require that the cursor is not after the match but
  25425. really on, so that the block visually is on the match."
  25426. (catch 'exit
  25427. (let ((pos (point))
  25428. (eol (point-at-eol (+ 1 (or nlines 0))))
  25429. (inc (if visually 1 0)))
  25430. (save-excursion
  25431. (beginning-of-line (- 1 (or nlines 0)))
  25432. (while (re-search-forward re eol t)
  25433. (if (and (<= (match-beginning 0) pos)
  25434. (>= (+ inc (match-end 0)) pos))
  25435. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25436. (defun org-at-regexp-p (regexp)
  25437. "Is point inside a match of REGEXP in the current line?"
  25438. (catch 'exit
  25439. (save-excursion
  25440. (let ((pos (point)) (end (point-at-eol)))
  25441. (beginning-of-line 1)
  25442. (while (re-search-forward regexp end t)
  25443. (if (and (<= (match-beginning 0) pos)
  25444. (>= (match-end 0) pos))
  25445. (throw 'exit t)))
  25446. nil))))
  25447. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25448. "Call `multi-occur' with buffers for all agenda files."
  25449. (interactive "sOrg-files matching: \np")
  25450. (let* ((files (org-agenda-files))
  25451. (tnames (mapcar 'file-truename files))
  25452. (extra org-agenda-multi-occur-extra-files)
  25453. f)
  25454. (while (setq f (pop extra))
  25455. (unless (member (file-truename f) tnames)
  25456. (add-to-list 'files f 'append)
  25457. (add-to-list 'tnames (file-truename f) 'append)))
  25458. (multi-occur
  25459. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25460. regexp)))
  25461. (if (boundp 'occur-mode-find-occurrence-hook)
  25462. ;; Emacs 23
  25463. (add-hook 'occur-mode-find-occurrence-hook
  25464. (lambda ()
  25465. (when (org-mode-p)
  25466. (org-reveal))))
  25467. ;; Emacs 22
  25468. (defadvice occur-mode-goto-occurrence
  25469. (after org-occur-reveal activate)
  25470. (and (org-mode-p) (org-reveal)))
  25471. (defadvice occur-mode-goto-occurrence-other-window
  25472. (after org-occur-reveal activate)
  25473. (and (org-mode-p) (org-reveal)))
  25474. (defadvice occur-mode-display-occurrence
  25475. (after org-occur-reveal activate)
  25476. (when (org-mode-p)
  25477. (let ((pos (occur-mode-find-occurrence)))
  25478. (with-current-buffer (marker-buffer pos)
  25479. (save-excursion
  25480. (goto-char pos)
  25481. (org-reveal)))))))
  25482. (defun org-uniquify (list)
  25483. "Remove duplicate elements from LIST."
  25484. (let (res)
  25485. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25486. res))
  25487. (defun org-delete-all (elts list)
  25488. "Remove all elements in ELTS from LIST."
  25489. (while elts
  25490. (setq list (delete (pop elts) list)))
  25491. list)
  25492. (defun org-back-over-empty-lines ()
  25493. "Move backwards over witespace, to the beginning of the first empty line.
  25494. Returns the number o empty lines passed."
  25495. (let ((pos (point)))
  25496. (skip-chars-backward " \t\n\r")
  25497. (beginning-of-line 2)
  25498. (goto-char (min (point) pos))
  25499. (count-lines (point) pos)))
  25500. (defun org-skip-whitespace ()
  25501. (skip-chars-forward " \t\n\r"))
  25502. (defun org-point-in-group (point group &optional context)
  25503. "Check if POINT is in match-group GROUP.
  25504. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25505. match. If the match group does ot exist or point is not inside it,
  25506. return nil."
  25507. (and (match-beginning group)
  25508. (>= point (match-beginning group))
  25509. (<= point (match-end group))
  25510. (if context
  25511. (list context (match-beginning group) (match-end group))
  25512. t)))
  25513. (defun org-switch-to-buffer-other-window (&rest args)
  25514. "Switch to buffer in a second window on the current frame.
  25515. In particular, do not allow pop-up frames."
  25516. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25517. special-display-function)
  25518. (apply 'switch-to-buffer-other-window args)))
  25519. (defun org-combine-plists (&rest plists)
  25520. "Create a single property list from all plists in PLISTS.
  25521. The process starts by copying the first list, and then setting properties
  25522. from the other lists. Settings in the last list are the most significant
  25523. ones and overrule settings in the other lists."
  25524. (let ((rtn (copy-sequence (pop plists)))
  25525. p v ls)
  25526. (while plists
  25527. (setq ls (pop plists))
  25528. (while ls
  25529. (setq p (pop ls) v (pop ls))
  25530. (setq rtn (plist-put rtn p v))))
  25531. rtn))
  25532. (defun org-move-line-down (arg)
  25533. "Move the current line down. With prefix argument, move it past ARG lines."
  25534. (interactive "p")
  25535. (let ((col (current-column))
  25536. beg end pos)
  25537. (beginning-of-line 1) (setq beg (point))
  25538. (beginning-of-line 2) (setq end (point))
  25539. (beginning-of-line (+ 1 arg))
  25540. (setq pos (move-marker (make-marker) (point)))
  25541. (insert (delete-and-extract-region beg end))
  25542. (goto-char pos)
  25543. (move-to-column col)))
  25544. (defun org-move-line-up (arg)
  25545. "Move the current line up. With prefix argument, move it past ARG lines."
  25546. (interactive "p")
  25547. (let ((col (current-column))
  25548. beg end pos)
  25549. (beginning-of-line 1) (setq beg (point))
  25550. (beginning-of-line 2) (setq end (point))
  25551. (beginning-of-line (- arg))
  25552. (setq pos (move-marker (make-marker) (point)))
  25553. (insert (delete-and-extract-region beg end))
  25554. (goto-char pos)
  25555. (move-to-column col)))
  25556. (defun org-replace-escapes (string table)
  25557. "Replace %-escapes in STRING with values in TABLE.
  25558. TABLE is an association list with keys like \"%a\" and string values.
  25559. The sequences in STRING may contain normal field width and padding information,
  25560. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25561. so values can contain further %-escapes if they are define later in TABLE."
  25562. (let ((case-fold-search nil)
  25563. e re rpl)
  25564. (while (setq e (pop table))
  25565. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25566. (while (string-match re string)
  25567. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25568. (cdr e)))
  25569. (setq string (replace-match rpl t t string))))
  25570. string))
  25571. (defun org-sublist (list start end)
  25572. "Return a section of LIST, from START to END.
  25573. Counting starts at 1."
  25574. (let (rtn (c start))
  25575. (setq list (nthcdr (1- start) list))
  25576. (while (and list (<= c end))
  25577. (push (pop list) rtn)
  25578. (setq c (1+ c)))
  25579. (nreverse rtn)))
  25580. (defun org-find-base-buffer-visiting (file)
  25581. "Like `find-buffer-visiting' but alway return the base buffer and
  25582. not an indirect buffer"
  25583. (let ((buf (find-buffer-visiting file)))
  25584. (if buf
  25585. (or (buffer-base-buffer buf) buf)
  25586. nil)))
  25587. (defun org-image-file-name-regexp ()
  25588. "Return regexp matching the file names of images."
  25589. (if (fboundp 'image-file-name-regexp)
  25590. (image-file-name-regexp)
  25591. (let ((image-file-name-extensions
  25592. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25593. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25594. (concat "\\."
  25595. (regexp-opt (nconc (mapcar 'upcase
  25596. image-file-name-extensions)
  25597. image-file-name-extensions)
  25598. t)
  25599. "\\'"))))
  25600. (defun org-file-image-p (file)
  25601. "Return non-nil if FILE is an image."
  25602. (save-match-data
  25603. (string-match (org-image-file-name-regexp) file)))
  25604. ;;; Paragraph filling stuff.
  25605. ;; We want this to be just right, so use the full arsenal.
  25606. (defun org-indent-line-function ()
  25607. "Indent line like previous, but further if previous was headline or item."
  25608. (interactive)
  25609. (let* ((pos (point))
  25610. (itemp (org-at-item-p))
  25611. column bpos bcol tpos tcol bullet btype bullet-type)
  25612. ;; Find the previous relevant line
  25613. (beginning-of-line 1)
  25614. (cond
  25615. ((looking-at "#") (setq column 0))
  25616. ((looking-at "\\*+ ") (setq column 0))
  25617. (t
  25618. (beginning-of-line 0)
  25619. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25620. (beginning-of-line 0))
  25621. (cond
  25622. ((looking-at "\\*+[ \t]+")
  25623. (goto-char (match-end 0))
  25624. (setq column (current-column)))
  25625. ((org-in-item-p)
  25626. (org-beginning-of-item)
  25627. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25628. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25629. (setq bpos (match-beginning 1) tpos (match-end 0)
  25630. bcol (progn (goto-char bpos) (current-column))
  25631. tcol (progn (goto-char tpos) (current-column))
  25632. bullet (match-string 1)
  25633. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25634. (if (not itemp)
  25635. (setq column tcol)
  25636. (goto-char pos)
  25637. (beginning-of-line 1)
  25638. (if (looking-at "\\S-")
  25639. (progn
  25640. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25641. (setq bullet (match-string 1)
  25642. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25643. (setq column (if (equal btype bullet-type) bcol tcol)))
  25644. (setq column (org-get-indentation)))))
  25645. (t (setq column (org-get-indentation))))))
  25646. (goto-char pos)
  25647. (if (<= (current-column) (current-indentation))
  25648. (indent-line-to column)
  25649. (save-excursion (indent-line-to column)))
  25650. (setq column (current-column))
  25651. (beginning-of-line 1)
  25652. (if (looking-at
  25653. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25654. (replace-match (concat "\\1" (format org-property-format
  25655. (match-string 2) (match-string 3)))
  25656. t nil))
  25657. (move-to-column column)))
  25658. (defun org-set-autofill-regexps ()
  25659. (interactive)
  25660. ;; In the paragraph separator we include headlines, because filling
  25661. ;; text in a line directly attached to a headline would otherwise
  25662. ;; fill the headline as well.
  25663. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25664. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25665. ;; The paragraph starter includes hand-formatted lists.
  25666. (org-set-local 'paragraph-start
  25667. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25668. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25669. ;; But only if the user has not turned off tables or fixed-width regions
  25670. (org-set-local
  25671. 'auto-fill-inhibit-regexp
  25672. (concat "\\*+ \\|#\\+"
  25673. "\\|[ \t]*" org-keyword-time-regexp
  25674. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25675. (concat
  25676. "\\|[ \t]*["
  25677. (if org-enable-table-editor "|" "")
  25678. (if org-enable-fixed-width-editor ":" "")
  25679. "]"))))
  25680. ;; We use our own fill-paragraph function, to make sure that tables
  25681. ;; and fixed-width regions are not wrapped. That function will pass
  25682. ;; through to `fill-paragraph' when appropriate.
  25683. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25684. ; Adaptive filling: To get full control, first make sure that
  25685. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25686. (org-set-local 'adaptive-fill-regexp "\000")
  25687. (org-set-local 'adaptive-fill-function
  25688. 'org-adaptive-fill-function)
  25689. (org-set-local
  25690. 'align-mode-rules-list
  25691. '((org-in-buffer-settings
  25692. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25693. (modes . '(org-mode))))))
  25694. (defun org-fill-paragraph (&optional justify)
  25695. "Re-align a table, pass through to fill-paragraph if no table."
  25696. (let ((table-p (org-at-table-p))
  25697. (table.el-p (org-at-table.el-p)))
  25698. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25699. (save-excursion (goto-char (point-at-bol))
  25700. (looking-at outline-regexp)))
  25701. t) ; skip headlines
  25702. (table.el-p t) ; skip table.el tables
  25703. (table-p (org-table-align) t) ; align org-mode tables
  25704. (t nil)))) ; call paragraph-fill
  25705. ;; For reference, this is the default value of adaptive-fill-regexp
  25706. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25707. (defun org-adaptive-fill-function ()
  25708. "Return a fill prefix for org-mode files.
  25709. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25710. work correctly."
  25711. (cond ((looking-at "#[ \t]+")
  25712. (match-string 0))
  25713. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25714. (save-excursion
  25715. (goto-char (match-end 0))
  25716. (make-string (current-column) ?\ )))
  25717. (t nil)))
  25718. ;;;; Functions extending outline functionality
  25719. (defun org-beginning-of-line (&optional arg)
  25720. "Go to the beginning of the current line. If that is invisible, continue
  25721. to a visible line beginning. This makes the function of C-a more intuitive.
  25722. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25723. first attempt, and only move to after the tags when the cursor is already
  25724. beyond the end of the headline."
  25725. (interactive "P")
  25726. (let ((pos (point)))
  25727. (beginning-of-line 1)
  25728. (if (bobp)
  25729. nil
  25730. (backward-char 1)
  25731. (if (org-invisible-p)
  25732. (while (and (not (bobp)) (org-invisible-p))
  25733. (backward-char 1)
  25734. (beginning-of-line 1))
  25735. (forward-char 1)))
  25736. (when org-special-ctrl-a/e
  25737. (cond
  25738. ((and (looking-at org-todo-line-regexp)
  25739. (= (char-after (match-end 1)) ?\ ))
  25740. (goto-char
  25741. (if (eq org-special-ctrl-a/e t)
  25742. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25743. ((= pos (point)) (match-beginning 3))
  25744. (t (point)))
  25745. (cond ((> pos (point)) (point))
  25746. ((not (eq last-command this-command)) (point))
  25747. (t (match-beginning 3))))))
  25748. ((org-at-item-p)
  25749. (goto-char
  25750. (if (eq org-special-ctrl-a/e t)
  25751. (cond ((> pos (match-end 4)) (match-end 4))
  25752. ((= pos (point)) (match-end 4))
  25753. (t (point)))
  25754. (cond ((> pos (point)) (point))
  25755. ((not (eq last-command this-command)) (point))
  25756. (t (match-end 4))))))))))
  25757. (defun org-end-of-line (&optional arg)
  25758. "Go to the end of the line.
  25759. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25760. first attempt, and only move to after the tags when the cursor is already
  25761. beyond the end of the headline."
  25762. (interactive "P")
  25763. (if (or (not org-special-ctrl-a/e)
  25764. (not (org-on-heading-p)))
  25765. (end-of-line arg)
  25766. (let ((pos (point)))
  25767. (beginning-of-line 1)
  25768. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25769. (if (eq org-special-ctrl-a/e t)
  25770. (if (or (< pos (match-beginning 1))
  25771. (= pos (match-end 0)))
  25772. (goto-char (match-beginning 1))
  25773. (goto-char (match-end 0)))
  25774. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25775. (goto-char (match-end 0))
  25776. (goto-char (match-beginning 1))))
  25777. (end-of-line arg)))))
  25778. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25779. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25780. (defun org-kill-line (&optional arg)
  25781. "Kill line, to tags or end of line."
  25782. (interactive "P")
  25783. (cond
  25784. ((or (not org-special-ctrl-k)
  25785. (bolp)
  25786. (not (org-on-heading-p)))
  25787. (call-interactively 'kill-line))
  25788. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25789. (kill-region (point) (match-beginning 1))
  25790. (org-set-tags nil t))
  25791. (t (kill-region (point) (point-at-eol)))))
  25792. (define-key org-mode-map "\C-k" 'org-kill-line)
  25793. (defun org-invisible-p ()
  25794. "Check if point is at a character currently not visible."
  25795. ;; Early versions of noutline don't have `outline-invisible-p'.
  25796. (if (fboundp 'outline-invisible-p)
  25797. (outline-invisible-p)
  25798. (get-char-property (point) 'invisible)))
  25799. (defun org-invisible-p2 ()
  25800. "Check if point is at a character currently not visible."
  25801. (save-excursion
  25802. (if (and (eolp) (not (bobp))) (backward-char 1))
  25803. ;; Early versions of noutline don't have `outline-invisible-p'.
  25804. (if (fboundp 'outline-invisible-p)
  25805. (outline-invisible-p)
  25806. (get-char-property (point) 'invisible))))
  25807. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25808. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25809. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25810. (defun org-at-heading-or-item-p ()
  25811. (or (org-on-heading-p) (org-at-item-p)))
  25812. (defun org-on-target-p ()
  25813. (or (org-in-regexp org-radio-target-regexp)
  25814. (org-in-regexp org-target-regexp)))
  25815. (defun org-up-heading-all (arg)
  25816. "Move to the heading line of which the present line is a subheading.
  25817. This function considers both visible and invisible heading lines.
  25818. With argument, move up ARG levels."
  25819. (if (fboundp 'outline-up-heading-all)
  25820. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25821. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25822. (defun org-up-heading-safe ()
  25823. "Move to the heading line of which the present line is a subheading.
  25824. This version will not throw an error. It will return the level of the
  25825. headline found, or nil if no higher level is found."
  25826. (let ((pos (point)) start-level level
  25827. (re (concat "^" outline-regexp)))
  25828. (catch 'exit
  25829. (outline-back-to-heading t)
  25830. (setq start-level (funcall outline-level))
  25831. (if (equal start-level 1) (throw 'exit nil))
  25832. (while (re-search-backward re nil t)
  25833. (setq level (funcall outline-level))
  25834. (if (< level start-level) (throw 'exit level)))
  25835. nil)))
  25836. (defun org-first-sibling-p ()
  25837. "Is this heading the first child of its parents?"
  25838. (interactive)
  25839. (let ((re (concat "^" outline-regexp))
  25840. level l)
  25841. (unless (org-at-heading-p t)
  25842. (error "Not at a heading"))
  25843. (setq level (funcall outline-level))
  25844. (save-excursion
  25845. (if (not (re-search-backward re nil t))
  25846. t
  25847. (setq l (funcall outline-level))
  25848. (< l level)))))
  25849. (defun org-goto-sibling (&optional previous)
  25850. "Goto the next sibling, even if it is invisible.
  25851. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25852. when a sibling was found. When none is found, return nil and don't
  25853. move point."
  25854. (let ((fun (if previous 're-search-backward 're-search-forward))
  25855. (pos (point))
  25856. (re (concat "^" outline-regexp))
  25857. level l)
  25858. (when (condition-case nil (org-back-to-heading t) (error nil))
  25859. (setq level (funcall outline-level))
  25860. (catch 'exit
  25861. (or previous (forward-char 1))
  25862. (while (funcall fun re nil t)
  25863. (setq l (funcall outline-level))
  25864. (when (< l level) (goto-char pos) (throw 'exit nil))
  25865. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25866. (goto-char pos)
  25867. nil))))
  25868. (defun org-show-siblings ()
  25869. "Show all siblings of the current headline."
  25870. (save-excursion
  25871. (while (org-goto-sibling) (org-flag-heading nil)))
  25872. (save-excursion
  25873. (while (org-goto-sibling 'previous)
  25874. (org-flag-heading nil))))
  25875. (defun org-show-hidden-entry ()
  25876. "Show an entry where even the heading is hidden."
  25877. (save-excursion
  25878. (org-show-entry)))
  25879. (defun org-flag-heading (flag &optional entry)
  25880. "Flag the current heading. FLAG non-nil means make invisible.
  25881. When ENTRY is non-nil, show the entire entry."
  25882. (save-excursion
  25883. (org-back-to-heading t)
  25884. ;; Check if we should show the entire entry
  25885. (if entry
  25886. (progn
  25887. (org-show-entry)
  25888. (save-excursion
  25889. (and (outline-next-heading)
  25890. (org-flag-heading nil))))
  25891. (outline-flag-region (max (point-min) (1- (point)))
  25892. (save-excursion (outline-end-of-heading) (point))
  25893. flag))))
  25894. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25895. ;; This is an exact copy of the original function, but it uses
  25896. ;; `org-back-to-heading', to make it work also in invisible
  25897. ;; trees. And is uses an invisible-OK argument.
  25898. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25899. (org-back-to-heading invisible-OK)
  25900. (let ((first t)
  25901. (level (funcall outline-level)))
  25902. (while (and (not (eobp))
  25903. (or first (> (funcall outline-level) level)))
  25904. (setq first nil)
  25905. (outline-next-heading))
  25906. (unless to-heading
  25907. (if (memq (preceding-char) '(?\n ?\^M))
  25908. (progn
  25909. ;; Go to end of line before heading
  25910. (forward-char -1)
  25911. (if (memq (preceding-char) '(?\n ?\^M))
  25912. ;; leave blank line before heading
  25913. (forward-char -1))))))
  25914. (point))
  25915. (defun org-show-subtree ()
  25916. "Show everything after this heading at deeper levels."
  25917. (outline-flag-region
  25918. (point)
  25919. (save-excursion
  25920. (outline-end-of-subtree) (outline-next-heading) (point))
  25921. nil))
  25922. (defun org-show-entry ()
  25923. "Show the body directly following this heading.
  25924. Show the heading too, if it is currently invisible."
  25925. (interactive)
  25926. (save-excursion
  25927. (condition-case nil
  25928. (progn
  25929. (org-back-to-heading t)
  25930. (outline-flag-region
  25931. (max (point-min) (1- (point)))
  25932. (save-excursion
  25933. (re-search-forward
  25934. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25935. (or (match-beginning 1) (point-max)))
  25936. nil))
  25937. (error nil))))
  25938. (defun org-make-options-regexp (kwds)
  25939. "Make a regular expression for keyword lines."
  25940. (concat
  25941. "^"
  25942. "#?[ \t]*\\+\\("
  25943. (mapconcat 'regexp-quote kwds "\\|")
  25944. "\\):[ \t]*"
  25945. "\\(.+\\)"))
  25946. ;; Make isearch reveal the necessary context
  25947. (defun org-isearch-end ()
  25948. "Reveal context after isearch exits."
  25949. (when isearch-success ; only if search was successful
  25950. (if (featurep 'xemacs)
  25951. ;; Under XEmacs, the hook is run in the correct place,
  25952. ;; we directly show the context.
  25953. (org-show-context 'isearch)
  25954. ;; In Emacs the hook runs *before* restoring the overlays.
  25955. ;; So we have to use a one-time post-command-hook to do this.
  25956. ;; (Emacs 22 has a special variable, see function `org-mode')
  25957. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25958. isearch-mode-end-hook-quit)
  25959. ;; Only when the isearch was not quitted.
  25960. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25961. 'append 'local)))))
  25962. (defun org-isearch-post-command ()
  25963. "Remove self from hook, and show context."
  25964. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25965. (org-show-context 'isearch))
  25966. ;;;; Integration with and fixes for other packages
  25967. ;;; Imenu support
  25968. (defvar org-imenu-markers nil
  25969. "All markers currently used by Imenu.")
  25970. (make-variable-buffer-local 'org-imenu-markers)
  25971. (defun org-imenu-new-marker (&optional pos)
  25972. "Return a new marker for use by Imenu, and remember the marker."
  25973. (let ((m (make-marker)))
  25974. (move-marker m (or pos (point)))
  25975. (push m org-imenu-markers)
  25976. m))
  25977. (defun org-imenu-get-tree ()
  25978. "Produce the index for Imenu."
  25979. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25980. (setq org-imenu-markers nil)
  25981. (let* ((n org-imenu-depth)
  25982. (re (concat "^" outline-regexp))
  25983. (subs (make-vector (1+ n) nil))
  25984. (last-level 0)
  25985. m tree level head)
  25986. (save-excursion
  25987. (save-restriction
  25988. (widen)
  25989. (goto-char (point-max))
  25990. (while (re-search-backward re nil t)
  25991. (setq level (org-reduced-level (funcall outline-level)))
  25992. (when (<= level n)
  25993. (looking-at org-complex-heading-regexp)
  25994. (setq head (org-match-string-no-properties 4)
  25995. m (org-imenu-new-marker))
  25996. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25997. (if (>= level last-level)
  25998. (push (cons head m) (aref subs level))
  25999. (push (cons head (aref subs (1+ level))) (aref subs level))
  26000. (loop for i from (1+ level) to n do (aset subs i nil)))
  26001. (setq last-level level)))))
  26002. (aref subs 1)))
  26003. (eval-after-load "imenu"
  26004. '(progn
  26005. (add-hook 'imenu-after-jump-hook
  26006. (lambda () (org-show-context 'org-goto)))))
  26007. ;; Speedbar support
  26008. (defun org-speedbar-set-agenda-restriction ()
  26009. "Restrict future agenda commands to the location at point in speedbar.
  26010. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26011. (interactive)
  26012. (let (p m tp np dir txt w)
  26013. (cond
  26014. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26015. 'org-imenu t))
  26016. (setq m (get-text-property p 'org-imenu-marker))
  26017. (save-excursion
  26018. (save-restriction
  26019. (set-buffer (marker-buffer m))
  26020. (goto-char m)
  26021. (org-agenda-set-restriction-lock 'subtree))))
  26022. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26023. 'speedbar-function 'speedbar-find-file))
  26024. (setq tp (previous-single-property-change
  26025. (1+ p) 'speedbar-function)
  26026. np (next-single-property-change
  26027. tp 'speedbar-function)
  26028. dir (speedbar-line-directory)
  26029. txt (buffer-substring-no-properties (or tp (point-min))
  26030. (or np (point-max))))
  26031. (save-excursion
  26032. (save-restriction
  26033. (set-buffer (find-file-noselect
  26034. (let ((default-directory dir))
  26035. (expand-file-name txt))))
  26036. (unless (org-mode-p)
  26037. (error "Cannot restrict to non-Org-mode file"))
  26038. (org-agenda-set-restriction-lock 'file))))
  26039. (t (error "Don't know how to restrict Org-mode's agenda")))
  26040. (org-move-overlay org-speedbar-restriction-lock-overlay
  26041. (point-at-bol) (point-at-eol))
  26042. (setq current-prefix-arg nil)
  26043. (org-agenda-maybe-redo)))
  26044. (eval-after-load "speedbar"
  26045. '(progn
  26046. (speedbar-add-supported-extension ".org")
  26047. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26048. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26049. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26050. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26051. (add-hook 'speedbar-visiting-tag-hook
  26052. (lambda () (org-show-context 'org-goto)))))
  26053. ;;; Fixes and Hacks
  26054. ;; Make flyspell not check words in links, to not mess up our keymap
  26055. (defun org-mode-flyspell-verify ()
  26056. "Don't let flyspell put overlays at active buttons."
  26057. (not (get-text-property (point) 'keymap)))
  26058. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26059. (eval-after-load "bookmark"
  26060. '(if (boundp 'bookmark-after-jump-hook)
  26061. ;; We can use the hook
  26062. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26063. ;; Hook not available, use advice
  26064. (defadvice bookmark-jump (after org-make-visible activate)
  26065. "Make the position visible."
  26066. (org-bookmark-jump-unhide))))
  26067. (defun org-bookmark-jump-unhide ()
  26068. "Unhide the current position, to show the bookmark location."
  26069. (and (org-mode-p)
  26070. (or (org-invisible-p)
  26071. (save-excursion (goto-char (max (point-min) (1- (point))))
  26072. (org-invisible-p)))
  26073. (org-show-context 'bookmark-jump)))
  26074. ;; Fix a bug in htmlize where there are text properties (face nil)
  26075. (eval-after-load "htmlize"
  26076. '(progn
  26077. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26078. "Make sure there are no nil faces"
  26079. (setq ad-return-value (delq nil ad-return-value)))))
  26080. ;; Make session.el ignore our circular variable
  26081. (eval-after-load "session"
  26082. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26083. ;;;; Experimental code
  26084. (defun org-closed-in-range ()
  26085. "Sparse tree of items closed in a certain time range.
  26086. Still experimental, may disappear in the future."
  26087. (interactive)
  26088. ;; Get the time interval from the user.
  26089. (let* ((time1 (time-to-seconds
  26090. (org-read-date nil 'to-time nil "Starting date: ")))
  26091. (time2 (time-to-seconds
  26092. (org-read-date nil 'to-time nil "End date:")))
  26093. ;; callback function
  26094. (callback (lambda ()
  26095. (let ((time
  26096. (time-to-seconds
  26097. (apply 'encode-time
  26098. (org-parse-time-string
  26099. (match-string 1))))))
  26100. ;; check if time in interval
  26101. (and (>= time time1) (<= time time2))))))
  26102. ;; make tree, check each match with the callback
  26103. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26104. ;;;; Finish up
  26105. (provide 'org)
  26106. (run-hooks 'org-load-hook)
  26107. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26108. ;;; org.el ends here