org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.22a+
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.22a+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. (defcustom org-load-hook '(org-load-default-extensions)
  138. "Hook that is run after org.el has been loaded.
  139. This happens also after `org' has been provided, so
  140. requiring something in this hook that does a (require 'org) is ok."
  141. :group 'org
  142. :type 'hook)
  143. (defcustom org-default-extensions '(org-irc)
  144. "Extensions that should always be loaded together with org.el
  145. If the description starts with <A>, this means the extension
  146. will be autoloaded when needed, to preloading is not necessary."
  147. :group 'org
  148. :type
  149. '(set :greedy t
  150. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  151. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  152. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  153. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  154. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  155. (defun org-load-default-extensions ()
  156. "Load all extensions that are listed in `org-default-extensions'."
  157. (mapc 'require org-default-extensions))
  158. ;; FIXME: Needs a separate group...
  159. (defcustom org-completion-fallback-command 'hippie-expand
  160. "The expansion command called by \\[org-complete] in normal context.
  161. Normal means, no org-mode-specific context."
  162. :group 'org
  163. :type 'function)
  164. (defgroup org-startup nil
  165. "Options concerning startup of Org-mode."
  166. :tag "Org Startup"
  167. :group 'org)
  168. (defcustom org-startup-folded t
  169. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  170. This can also be configured on a per-file basis by adding one of
  171. the following lines anywhere in the buffer:
  172. #+STARTUP: fold
  173. #+STARTUP: nofold
  174. #+STARTUP: content"
  175. :group 'org-startup
  176. :type '(choice
  177. (const :tag "nofold: show all" nil)
  178. (const :tag "fold: overview" t)
  179. (const :tag "content: all headlines" content)))
  180. (defcustom org-startup-truncated t
  181. "Non-nil means, entering Org-mode will set `truncate-lines'.
  182. This is useful since some lines containing links can be very long and
  183. uninteresting. Also tables look terrible when wrapped."
  184. :group 'org-startup
  185. :type 'boolean)
  186. (defcustom org-startup-align-all-tables nil
  187. "Non-nil means, align all tables when visiting a file.
  188. This is useful when the column width in tables is forced with <N> cookies
  189. in table fields. Such tables will look correct only after the first re-align.
  190. This can also be configured on a per-file basis by adding one of
  191. the following lines anywhere in the buffer:
  192. #+STARTUP: align
  193. #+STARTUP: noalign"
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-insert-mode-line-in-empty-file nil
  197. "Non-nil means insert the first line setting Org-mode in empty files.
  198. When the function `org-mode' is called interactively in an empty file, this
  199. normally means that the file name does not automatically trigger Org-mode.
  200. To ensure that the file will always be in Org-mode in the future, a
  201. line enforcing Org-mode will be inserted into the buffer, if this option
  202. has been set."
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-replace-disputed-keys nil
  206. "Non-nil means use alternative key bindings for some keys.
  207. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  208. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  209. If you want to use Org-mode together with one of these other modes,
  210. or more generally if you would like to move some Org-mode commands to
  211. other keys, set this variable and configure the keys with the variable
  212. `org-disputed-keys'.
  213. This option is only relevant at load-time of Org-mode, and must be set
  214. *before* org.el is loaded. Changing it requires a restart of Emacs to
  215. become effective."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (if (fboundp 'defvaralias)
  219. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  220. (defcustom org-disputed-keys
  221. '(([(shift up)] . [(meta p)])
  222. ([(shift down)] . [(meta n)])
  223. ([(shift left)] . [(meta -)])
  224. ([(shift right)] . [(meta +)])
  225. ([(control shift right)] . [(meta shift +)])
  226. ([(control shift left)] . [(meta shift -)]))
  227. "Keys for which Org-mode and other modes compete.
  228. This is an alist, cars are the default keys, second element specifies
  229. the alternative to use when `org-replace-disputed-keys' is t.
  230. Keys can be specified in any syntax supported by `define-key'.
  231. The value of this option takes effect only at Org-mode's startup,
  232. therefore you'll have to restart Emacs to apply it after changing."
  233. :group 'org-startup
  234. :type 'alist)
  235. (defun org-key (key)
  236. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  237. Or return the original if not disputed."
  238. (if org-replace-disputed-keys
  239. (let* ((nkey (key-description key))
  240. (x (org-find-if (lambda (x)
  241. (equal (key-description (car x)) nkey))
  242. org-disputed-keys)))
  243. (if x (cdr x) key))
  244. key))
  245. (defun org-find-if (predicate seq)
  246. (catch 'exit
  247. (while seq
  248. (if (funcall predicate (car seq))
  249. (throw 'exit (car seq))
  250. (pop seq)))))
  251. (defun org-defkey (keymap key def)
  252. "Define a key, possibly translated, as returned by `org-key'."
  253. (define-key keymap (org-key key) def))
  254. (defcustom org-ellipsis nil
  255. "The ellipsis to use in the Org-mode outline.
  256. When nil, just use the standard three dots. When a string, use that instead,
  257. When a face, use the standart 3 dots, but with the specified face.
  258. The change affects only Org-mode (which will then use its own display table).
  259. Changing this requires executing `M-x org-mode' in a buffer to become
  260. effective."
  261. :group 'org-startup
  262. :type '(choice (const :tag "Default" nil)
  263. (face :tag "Face" :value org-warning)
  264. (string :tag "String" :value "...#")))
  265. (defvar org-display-table nil
  266. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  267. (defgroup org-keywords nil
  268. "Keywords in Org-mode."
  269. :tag "Org Keywords"
  270. :group 'org)
  271. (defcustom org-deadline-string "DEADLINE:"
  272. "String to mark deadline entries.
  273. A deadline is this string, followed by a time stamp. Should be a word,
  274. terminated by a colon. You can insert a schedule keyword and
  275. a timestamp with \\[org-deadline].
  276. Changes become only effective after restarting Emacs."
  277. :group 'org-keywords
  278. :type 'string)
  279. (defcustom org-scheduled-string "SCHEDULED:"
  280. "String to mark scheduled TODO entries.
  281. A schedule is this string, followed by a time stamp. Should be a word,
  282. terminated by a colon. You can insert a schedule keyword and
  283. a timestamp with \\[org-schedule].
  284. Changes become only effective after restarting Emacs."
  285. :group 'org-keywords
  286. :type 'string)
  287. (defcustom org-closed-string "CLOSED:"
  288. "String used as the prefix for timestamps logging closing a TODO entry."
  289. :group 'org-keywords
  290. :type 'string)
  291. (defcustom org-clock-string "CLOCK:"
  292. "String used as prefix for timestamps clocking work hours on an item."
  293. :group 'org-keywords
  294. :type 'string)
  295. (defcustom org-comment-string "COMMENT"
  296. "Entries starting with this keyword will never be exported.
  297. An entry can be toggled between COMMENT and normal with
  298. \\[org-toggle-comment].
  299. Changes become only effective after restarting Emacs."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-quote-string "QUOTE"
  303. "Entries starting with this keyword will be exported in fixed-width font.
  304. Quoting applies only to the text in the entry following the headline, and does
  305. not extend beyond the next headline, even if that is lower level.
  306. An entry can be toggled between QUOTE and normal with
  307. \\[org-toggle-fixed-width-section]."
  308. :group 'org-keywords
  309. :type 'string)
  310. (defconst org-repeat-re
  311. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  312. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  313. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  314. "Regular expression for specifying repeated events.
  315. After a match, group 1 contains the repeat expression.")
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-reveal-location nil
  321. "Options about how to make context of a location visible."
  322. :tag "Org Reveal Location"
  323. :group 'org-structure)
  324. (defconst org-context-choice
  325. '(choice
  326. (const :tag "Always" t)
  327. (const :tag "Never" nil)
  328. (repeat :greedy t :tag "Individual contexts"
  329. (cons
  330. (choice :tag "Context"
  331. (const agenda)
  332. (const org-goto)
  333. (const occur-tree)
  334. (const tags-tree)
  335. (const link-search)
  336. (const mark-goto)
  337. (const bookmark-jump)
  338. (const isearch)
  339. (const default))
  340. (boolean))))
  341. "Contexts for the reveal options.")
  342. (defcustom org-show-hierarchy-above '((default . t))
  343. "Non-nil means, show full hierarchy when revealing a location.
  344. Org-mode often shows locations in an org-mode file which might have
  345. been invisible before. When this is set, the hierarchy of headings
  346. above the exposed location is shown.
  347. Turning this off for example for sparse trees makes them very compact.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. Valid contexts are
  350. agenda when exposing an entry from the agenda
  351. org-goto when using the command `org-goto' on key C-c C-j
  352. occur-tree when using the command `org-occur' on key C-c /
  353. tags-tree when constructing a sparse tree based on tags matches
  354. link-search when exposing search matches associated with a link
  355. mark-goto when exposing the jump goal of a mark
  356. bookmark-jump when exposing a bookmark location
  357. isearch when exiting from an incremental search
  358. default default for all contexts not set explicitly"
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-following-heading '((default . nil))
  362. "Non-nil means, show following heading when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the heading following the
  365. match is shown.
  366. Turning this off for example for sparse trees makes them very compact,
  367. but makes it harder to edit the location of the match. In such a case,
  368. use the command \\[org-reveal] to show more context.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. See `org-show-hierarchy-above' for valid contexts."
  371. :group 'org-reveal-location
  372. :type org-context-choice)
  373. (defcustom org-show-siblings '((default . nil) (isearch t))
  374. "Non-nil means, show all sibling heading when revealing a location.
  375. Org-mode often shows locations in an org-mode file which might have
  376. been invisible before. When this is set, the sibling of the current entry
  377. heading are all made visible. If `org-show-hierarchy-above' is t,
  378. the same happens on each level of the hierarchy above the current entry.
  379. By default this is on for the isearch context, off for all other contexts.
  380. Turning this off for example for sparse trees makes them very compact,
  381. but makes it harder to edit the location of the match. In such a case,
  382. use the command \\[org-reveal] to show more context.
  383. Instead of t, this can also be an alist specifying this option for different
  384. contexts. See `org-show-hierarchy-above' for valid contexts."
  385. :group 'org-reveal-location
  386. :type org-context-choice)
  387. (defcustom org-show-entry-below '((default . nil))
  388. "Non-nil means, show the entry below a headline when revealing a location.
  389. Org-mode often shows locations in an org-mode file which might have
  390. been invisible before. When this is set, the text below the headline that is
  391. exposed is also shown.
  392. By default this is off for all contexts.
  393. Instead of t, this can also be an alist specifying this option for different
  394. contexts. See `org-show-hierarchy-above' for valid contexts."
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defgroup org-cycle nil
  398. "Options concerning visibility cycling in Org-mode."
  399. :tag "Org Cycle"
  400. :group 'org-structure)
  401. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  402. "Names of drawers. Drawers are not opened by cycling on the headline above.
  403. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  404. this:
  405. :DRAWERNAME:
  406. .....
  407. :END:
  408. The drawer \"PROPERTIES\" is special for capturing properties through
  409. the property API.
  410. Drawers can be defined on the per-file basis with a line like:
  411. #+DRAWERS: HIDDEN STATE PROPERTIES"
  412. :group 'org-structure
  413. :type '(repeat (string :tag "Drawer Name")))
  414. (defcustom org-cycle-global-at-bob nil
  415. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  416. This makes it possible to do global cycling without having to use S-TAB or
  417. C-u TAB. For this special case to work, the first line of the buffer
  418. must not be a headline - it may be empty ot some other text. When used in
  419. this way, `org-cycle-hook' is disables temporarily, to make sure the
  420. cursor stays at the beginning of the buffer.
  421. When this option is nil, don't do anything special at the beginning
  422. of the buffer."
  423. :group 'org-cycle
  424. :type 'boolean)
  425. (defcustom org-cycle-emulate-tab t
  426. "Where should `org-cycle' emulate TAB.
  427. nil Never
  428. white Only in completely white lines
  429. whitestart Only at the beginning of lines, before the first non-white char
  430. t Everywhere except in headlines
  431. exc-hl-bol Everywhere except at the start of a headline
  432. If TAB is used in a place where it does not emulate TAB, the current subtree
  433. visibility is cycled."
  434. :group 'org-cycle
  435. :type '(choice (const :tag "Never" nil)
  436. (const :tag "Only in completely white lines" white)
  437. (const :tag "Before first char in a line" whitestart)
  438. (const :tag "Everywhere except in headlines" t)
  439. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  440. ))
  441. (defcustom org-cycle-separator-lines 2
  442. "Number of empty lines needed to keep an empty line between collapsed trees.
  443. If you leave an empty line between the end of a subtree and the following
  444. headline, this empty line is hidden when the subtree is folded.
  445. Org-mode will leave (exactly) one empty line visible if the number of
  446. empty lines is equal or larger to the number given in this variable.
  447. So the default 2 means, at least 2 empty lines after the end of a subtree
  448. are needed to produce free space between a collapsed subtree and the
  449. following headline.
  450. Special case: when 0, never leave empty lines in collapsed view."
  451. :group 'org-cycle
  452. :type 'integer)
  453. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  454. org-cycle-hide-drawers
  455. org-cycle-show-empty-lines
  456. org-optimize-window-after-visibility-change)
  457. "Hook that is run after `org-cycle' has changed the buffer visibility.
  458. The function(s) in this hook must accept a single argument which indicates
  459. the new state that was set by the most recent `org-cycle' command. The
  460. argument is a symbol. After a global state change, it can have the values
  461. `overview', `content', or `all'. After a local state change, it can have
  462. the values `folded', `children', or `subtree'."
  463. :group 'org-cycle
  464. :type 'hook)
  465. (defgroup org-edit-structure nil
  466. "Options concerning structure editing in Org-mode."
  467. :tag "Org Edit Structure"
  468. :group 'org-structure)
  469. (defcustom org-odd-levels-only nil
  470. "Non-nil means, skip even levels and only use odd levels for the outline.
  471. This has the effect that two stars are being added/taken away in
  472. promotion/demotion commands. It also influences how levels are
  473. handled by the exporters.
  474. Changing it requires restart of `font-lock-mode' to become effective
  475. for fontification also in regions already fontified.
  476. You may also set this on a per-file basis by adding one of the following
  477. lines to the buffer:
  478. #+STARTUP: odd
  479. #+STARTUP: oddeven"
  480. :group 'org-edit-structure
  481. :group 'org-font-lock
  482. :type 'boolean)
  483. (defcustom org-adapt-indentation t
  484. "Non-nil means, adapt indentation when promoting and demoting.
  485. When this is set and the *entire* text in an entry is indented, the
  486. indentation is increased by one space in a demotion command, and
  487. decreased by one in a promotion command. If any line in the entry
  488. body starts at column 0, indentation is not changed at all."
  489. :group 'org-edit-structure
  490. :type 'boolean)
  491. (defcustom org-special-ctrl-a/e nil
  492. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  493. When t, `C-a' will bring back the cursor to the beginning of the
  494. headline text, i.e. after the stars and after a possible TODO keyword.
  495. In an item, this will be the position after the bullet.
  496. When the cursor is already at that position, another `C-a' will bring
  497. it to the beginning of the line.
  498. `C-e' will jump to the end of the headline, ignoring the presence of tags
  499. in the headline. A second `C-e' will then jump to the true end of the
  500. line, after any tags.
  501. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  502. and only a directly following, identical keypress will bring the cursor
  503. to the special positions."
  504. :group 'org-edit-structure
  505. :type '(choice
  506. (const :tag "off" nil)
  507. (const :tag "after bullet first" t)
  508. (const :tag "border first" reversed)))
  509. (if (fboundp 'defvaralias)
  510. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  511. (defcustom org-special-ctrl-k nil
  512. "Non-nil means `C-k' will behave specially in headlines.
  513. When nil, `C-k' will call the default `kill-line' command.
  514. When t, the following will happen while the cursor is in the headline:
  515. - When the cursor is at the beginning of a headline, kill the entire
  516. line and possible the folded subtree below the line.
  517. - When in the middle of the headline text, kill the headline up to the tags.
  518. - When after the headline text, kill the tags."
  519. :group 'org-edit-structure
  520. :type 'boolean)
  521. (defcustom org-M-RET-may-split-line '((default . t))
  522. "Non-nil means, M-RET will split the line at the cursor position.
  523. When nil, it will go to the end of the line before making a
  524. new line.
  525. You may also set this option in a different way for different
  526. contexts. Valid contexts are:
  527. headline when creating a new headline
  528. item when creating a new item
  529. table in a table field
  530. default the value to be used for all contexts not explicitly
  531. customized"
  532. :group 'org-structure
  533. :group 'org-table
  534. :type '(choice
  535. (const :tag "Always" t)
  536. (const :tag "Never" nil)
  537. (repeat :greedy t :tag "Individual contexts"
  538. (cons
  539. (choice :tag "Context"
  540. (const headline)
  541. (const item)
  542. (const table)
  543. (const default))
  544. (boolean)))))
  545. (defcustom org-blank-before-new-entry '((heading . nil)
  546. (plain-list-item . nil))
  547. "Should `org-insert-heading' leave a blank line before new heading/item?
  548. The value is an alist, with `heading' and `plain-list-item' as car,
  549. and a boolean flag as cdr."
  550. :group 'org-edit-structure
  551. :type '(list
  552. (cons (const heading) (boolean))
  553. (cons (const plain-list-item) (boolean))))
  554. (defcustom org-insert-heading-hook nil
  555. "Hook being run after inserting a new heading."
  556. :group 'org-edit-structure
  557. :type 'hook)
  558. (defcustom org-enable-fixed-width-editor t
  559. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  560. This currently only means, they are never auto-wrapped.
  561. When nil, such lines will be treated like ordinary lines.
  562. See also the QUOTE keyword."
  563. :group 'org-edit-structure
  564. :type 'boolean)
  565. (defcustom org-goto-auto-isearch t
  566. "Non-nil means, typing characters in org-goto starts incremental search."
  567. :group 'org-edit-structure
  568. :type 'boolean)
  569. (defgroup org-sparse-trees nil
  570. "Options concerning sparse trees in Org-mode."
  571. :tag "Org Sparse Trees"
  572. :group 'org-structure)
  573. (defcustom org-highlight-sparse-tree-matches t
  574. "Non-nil means, highlight all matches that define a sparse tree.
  575. The highlights will automatically disappear the next time the buffer is
  576. changed by an edit command."
  577. :group 'org-sparse-trees
  578. :type 'boolean)
  579. (defcustom org-remove-highlights-with-change t
  580. "Non-nil means, any change to the buffer will remove temporary highlights.
  581. Such highlights are created by `org-occur' and `org-clock-display'.
  582. When nil, `C-c C-c needs to be used to get rid of the highlights.
  583. The highlights created by `org-preview-latex-fragment' always need
  584. `C-c C-c' to be removed."
  585. :group 'org-sparse-trees
  586. :group 'org-time
  587. :type 'boolean)
  588. (defcustom org-occur-hook '(org-first-headline-recenter)
  589. "Hook that is run after `org-occur' has constructed a sparse tree.
  590. This can be used to recenter the window to show as much of the structure
  591. as possible."
  592. :group 'org-sparse-trees
  593. :type 'hook)
  594. (defgroup org-plain-lists nil
  595. "Options concerning plain lists in Org-mode."
  596. :tag "Org Plain lists"
  597. :group 'org-structure)
  598. (defcustom org-cycle-include-plain-lists nil
  599. "Non-nil means, include plain lists into visibility cycling.
  600. This means that during cycling, plain list items will *temporarily* be
  601. interpreted as outline headlines with a level given by 1000+i where i is the
  602. indentation of the bullet. In all other operations, plain list items are
  603. not seen as headlines. For example, you cannot assign a TODO keyword to
  604. such an item."
  605. :group 'org-plain-lists
  606. :type 'boolean)
  607. (defcustom org-plain-list-ordered-item-terminator t
  608. "The character that makes a line with leading number an ordered list item.
  609. Valid values are ?. and ?\). To get both terminators, use t. While
  610. ?. may look nicer, it creates the danger that a line with leading
  611. number may be incorrectly interpreted as an item. ?\) therefore is
  612. the safe choice."
  613. :group 'org-plain-lists
  614. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  615. (const :tag "paren like in \"2)\"" ?\))
  616. (const :tab "both" t)))
  617. (defcustom org-auto-renumber-ordered-lists t
  618. "Non-nil means, automatically renumber ordered plain lists.
  619. Renumbering happens when the sequence have been changed with
  620. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  621. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  622. :group 'org-plain-lists
  623. :type 'boolean)
  624. (defcustom org-provide-checkbox-statistics t
  625. "Non-nil means, update checkbox statistics after insert and toggle.
  626. When this is set, checkbox statistics is updated each time you either insert
  627. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  628. with \\[org-ctrl-c-ctrl-c\\]."
  629. :group 'org-plain-lists
  630. :type 'boolean)
  631. (defgroup org-archive nil
  632. "Options concerning archiving in Org-mode."
  633. :tag "Org Archive"
  634. :group 'org-structure)
  635. (defcustom org-archive-tag "ARCHIVE"
  636. "The tag that marks a subtree as archived.
  637. An archived subtree does not open during visibility cycling, and does
  638. not contribute to the agenda listings.
  639. After changing this, font-lock must be restarted in the relevant buffers to
  640. get the proper fontification."
  641. :group 'org-archive
  642. :group 'org-keywords
  643. :type 'string)
  644. (defcustom org-agenda-skip-archived-trees t
  645. "Non-nil means, the agenda will skip any items located in archived trees.
  646. An archived tree is a tree marked with the tag ARCHIVE."
  647. :group 'org-archive
  648. :group 'org-agenda-skip
  649. :type 'boolean)
  650. (defcustom org-cycle-open-archived-trees nil
  651. "Non-nil means, `org-cycle' will open archived trees.
  652. An archived tree is a tree marked with the tag ARCHIVE.
  653. When nil, archived trees will stay folded. You can still open them with
  654. normal outline commands like `show-all', but not with the cycling commands."
  655. :group 'org-archive
  656. :group 'org-cycle
  657. :type 'boolean)
  658. (defcustom org-sparse-tree-open-archived-trees nil
  659. "Non-nil means sparse tree construction shows matches in archived trees.
  660. When nil, matches in these trees are highlighted, but the trees are kept in
  661. collapsed state."
  662. :group 'org-archive
  663. :group 'org-sparse-trees
  664. :type 'boolean)
  665. (defcustom org-archive-location "%s_archive::"
  666. "The location where subtrees should be archived.
  667. This string consists of two parts, separated by a double-colon.
  668. The first part is a file name - when omitted, archiving happens in the same
  669. file. %s will be replaced by the current file name (without directory part).
  670. Archiving to a different file is useful to keep archived entries from
  671. contributing to the Org-mode Agenda.
  672. The part after the double colon is a headline. The archived entries will be
  673. filed under that headline. When omitted, the subtrees are simply filed away
  674. at the end of the file, as top-level entries.
  675. Here are a few examples:
  676. \"%s_archive::\"
  677. If the current file is Projects.org, archive in file
  678. Projects.org_archive, as top-level trees. This is the default.
  679. \"::* Archived Tasks\"
  680. Archive in the current file, under the top-level headline
  681. \"* Archived Tasks\".
  682. \"~/org/archive.org::\"
  683. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  684. \"basement::** Finished Tasks\"
  685. Archive in file ./basement (relative path), as level 3 trees
  686. below the level 2 heading \"** Finished Tasks\".
  687. You may set this option on a per-file basis by adding to the buffer a
  688. line like
  689. #+ARCHIVE: basement::** Finished Tasks"
  690. :group 'org-archive
  691. :type 'string)
  692. (defcustom org-archive-mark-done t
  693. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  694. This can be a string to set the keyword to use. When t, Org-mode will
  695. use the first keyword in its list that means done."
  696. :group 'org-archive
  697. :type '(choice
  698. (const :tag "No" nil)
  699. (const :tag "Yes" t)
  700. (string :tag "Use this keyword")))
  701. (defcustom org-archive-stamp-time t
  702. "Non-nil means, add a time stamp to entries moved to an archive file.
  703. This variable is obsolete and has no effect anymore, instead add ot remove
  704. `time' from the variablle `org-archive-save-context-info'."
  705. :group 'org-archive
  706. :type 'boolean)
  707. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  708. "Parts of context info that should be stored as properties when archiving.
  709. When a subtree is moved to an archive file, it looses information given by
  710. context, like inherited tags, the category, and possibly also the TODO
  711. state (depending on the variable `org-archive-mark-done').
  712. This variable can be a list of any of the following symbols:
  713. time The time of archiving.
  714. file The file where the entry originates.
  715. itags The local tags, in the headline of the subtree.
  716. ltags The tags the subtree inherits from further up the hierarchy.
  717. todo The pre-archive TODO state.
  718. category The category, taken from file name or #+CATEGORY lines.
  719. olpath The outline path to the item. These are all headlines above
  720. the current item, separated by /, like a file path.
  721. For each symbol present in the list, a property will be created in
  722. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  723. information."
  724. :group 'org-archive
  725. :type '(set :greedy t
  726. (const :tag "Time" time)
  727. (const :tag "File" file)
  728. (const :tag "Category" category)
  729. (const :tag "TODO state" todo)
  730. (const :tag "TODO state" priority)
  731. (const :tag "Inherited tags" itags)
  732. (const :tag "Outline path" olpath)
  733. (const :tag "Local tags" ltags)))
  734. (defgroup org-imenu-and-speedbar nil
  735. "Options concerning imenu and speedbar in Org-mode."
  736. :tag "Org Imenu and Speedbar"
  737. :group 'org-structure)
  738. (defcustom org-imenu-depth 2
  739. "The maximum level for Imenu access to Org-mode headlines.
  740. This also applied for speedbar access."
  741. :group 'org-imenu-and-speedbar
  742. :type 'number)
  743. (defgroup org-table nil
  744. "Options concerning tables in Org-mode."
  745. :tag "Org Table"
  746. :group 'org)
  747. (defcustom org-enable-table-editor 'optimized
  748. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  749. When nil, such lines will be treated like ordinary lines.
  750. When equal to the symbol `optimized', the table editor will be optimized to
  751. do the following:
  752. - Automatic overwrite mode in front of whitespace in table fields.
  753. This makes the structure of the table stay in tact as long as the edited
  754. field does not exceed the column width.
  755. - Minimize the number of realigns. Normally, the table is aligned each time
  756. TAB or RET are pressed to move to another field. With optimization this
  757. happens only if changes to a field might have changed the column width.
  758. Optimization requires replacing the functions `self-insert-command',
  759. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  760. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  761. very good at guessing when a re-align will be necessary, but you can always
  762. force one with \\[org-ctrl-c-ctrl-c].
  763. If you would like to use the optimized version in Org-mode, but the
  764. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  765. This variable can be used to turn on and off the table editor during a session,
  766. but in order to toggle optimization, a restart is required.
  767. See also the variable `org-table-auto-blank-field'."
  768. :group 'org-table
  769. :type '(choice
  770. (const :tag "off" nil)
  771. (const :tag "on" t)
  772. (const :tag "on, optimized" optimized)))
  773. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  774. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  775. In the optimized version, the table editor takes over all simple keys that
  776. normally just insert a character. In tables, the characters are inserted
  777. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  778. for empty fields). Outside tables, the correct binding of the keys is
  779. restored.
  780. The default for this option is t if the optimized version is also used in
  781. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  782. this variable requires a restart of Emacs to become effective."
  783. :group 'org-table
  784. :type 'boolean)
  785. (defcustom orgtbl-radio-table-templates
  786. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  787. % END RECEIVE ORGTBL %n
  788. \\begin{comment}
  789. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  790. | | |
  791. \\end{comment}\n")
  792. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  793. @c END RECEIVE ORGTBL %n
  794. @ignore
  795. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  796. | | |
  797. @end ignore\n")
  798. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  799. <!-- END RECEIVE ORGTBL %n -->
  800. <!--
  801. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  802. | | |
  803. -->\n"))
  804. "Templates for radio tables in different major modes.
  805. All occurrences of %n in a template will be replaced with the name of the
  806. table, obtained by prompting the user."
  807. :group 'org-table
  808. :type '(repeat
  809. (list (symbol :tag "Major mode")
  810. (string :tag "Format"))))
  811. (defgroup org-table-settings nil
  812. "Settings for tables in Org-mode."
  813. :tag "Org Table Settings"
  814. :group 'org-table)
  815. (defcustom org-table-default-size "5x2"
  816. "The default size for newly created tables, Columns x Rows."
  817. :group 'org-table-settings
  818. :type 'string)
  819. (defcustom org-table-number-regexp
  820. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  821. "Regular expression for recognizing numbers in table columns.
  822. If a table column contains mostly numbers, it will be aligned to the
  823. right. If not, it will be aligned to the left.
  824. The default value of this option is a regular expression which allows
  825. anything which looks remotely like a number as used in scientific
  826. context. For example, all of the following will be considered a
  827. number:
  828. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  829. Other options offered by the customize interface are more restrictive."
  830. :group 'org-table-settings
  831. :type '(choice
  832. (const :tag "Positive Integers"
  833. "^[0-9]+$")
  834. (const :tag "Integers"
  835. "^[-+]?[0-9]+$")
  836. (const :tag "Floating Point Numbers"
  837. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  838. (const :tag "Floating Point Number or Integer"
  839. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  840. (const :tag "Exponential, Floating point, Integer"
  841. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  842. (const :tag "Very General Number-Like, including hex"
  843. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  844. (string :tag "Regexp:")))
  845. (defcustom org-table-number-fraction 0.5
  846. "Fraction of numbers in a column required to make the column align right.
  847. In a column all non-white fields are considered. If at least this
  848. fraction of fields is matched by `org-table-number-fraction',
  849. alignment to the right border applies."
  850. :group 'org-table-settings
  851. :type 'number)
  852. (defgroup org-table-editing nil
  853. "Behavior of tables during editing in Org-mode."
  854. :tag "Org Table Editing"
  855. :group 'org-table)
  856. (defcustom org-table-automatic-realign t
  857. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  858. When nil, aligning is only done with \\[org-table-align], or after column
  859. removal/insertion."
  860. :group 'org-table-editing
  861. :type 'boolean)
  862. (defcustom org-table-auto-blank-field t
  863. "Non-nil means, automatically blank table field when starting to type into it.
  864. This only happens when typing immediately after a field motion
  865. command (TAB, S-TAB or RET).
  866. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  867. :group 'org-table-editing
  868. :type 'boolean)
  869. (defcustom org-table-tab-jumps-over-hlines t
  870. "Non-nil means, tab in the last column of a table with jump over a hline.
  871. If a horizontal separator line is following the current line,
  872. `org-table-next-field' can either create a new row before that line, or jump
  873. over the line. When this option is nil, a new line will be created before
  874. this line."
  875. :group 'org-table-editing
  876. :type 'boolean)
  877. (defcustom org-table-tab-recognizes-table.el t
  878. "Non-nil means, TAB will automatically notice a table.el table.
  879. When it sees such a table, it moves point into it and - if necessary -
  880. calls `table-recognize-table'."
  881. :group 'org-table-editing
  882. :type 'boolean)
  883. (defgroup org-table-calculation nil
  884. "Options concerning tables in Org-mode."
  885. :tag "Org Table Calculation"
  886. :group 'org-table)
  887. (defcustom org-table-use-standard-references t
  888. "Should org-mode work with table refrences like B3 instead of @3$2?
  889. Possible values are:
  890. nil never use them
  891. from accept as input, do not present for editing
  892. t: accept as input and present for editing"
  893. :group 'org-table-calculation
  894. :type '(choice
  895. (const :tag "Never, don't even check unser input for them" nil)
  896. (const :tag "Always, both as user input, and when editing" t)
  897. (const :tag "Convert user input, don't offer during editing" 'from)))
  898. (defcustom org-table-copy-increment t
  899. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  900. :group 'org-table-calculation
  901. :type 'boolean)
  902. (defcustom org-calc-default-modes
  903. '(calc-internal-prec 12
  904. calc-float-format (float 5)
  905. calc-angle-mode deg
  906. calc-prefer-frac nil
  907. calc-symbolic-mode nil
  908. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  909. calc-display-working-message t
  910. )
  911. "List with Calc mode settings for use in calc-eval for table formulas.
  912. The list must contain alternating symbols (Calc modes variables and values).
  913. Don't remove any of the default settings, just change the values. Org-mode
  914. relies on the variables to be present in the list."
  915. :group 'org-table-calculation
  916. :type 'plist)
  917. (defcustom org-table-formula-evaluate-inline t
  918. "Non-nil means, TAB and RET evaluate a formula in current table field.
  919. If the current field starts with an equal sign, it is assumed to be a formula
  920. which should be evaluated as described in the manual and in the documentation
  921. string of the command `org-table-eval-formula'. This feature requires the
  922. Emacs calc package.
  923. When this variable is nil, formula calculation is only available through
  924. the command \\[org-table-eval-formula]."
  925. :group 'org-table-calculation
  926. :type 'boolean)
  927. (defcustom org-table-formula-use-constants t
  928. "Non-nil means, interpret constants in formulas in tables.
  929. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  930. by the value given in `org-table-formula-constants', or by a value obtained
  931. from the `constants.el' package."
  932. :group 'org-table-calculation
  933. :type 'boolean)
  934. (defcustom org-table-formula-constants nil
  935. "Alist with constant names and values, for use in table formulas.
  936. The car of each element is a name of a constant, without the `$' before it.
  937. The cdr is the value as a string. For example, if you'd like to use the
  938. speed of light in a formula, you would configure
  939. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  940. and then use it in an equation like `$1*$c'.
  941. Constants can also be defined on a per-file basis using a line like
  942. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  943. :group 'org-table-calculation
  944. :type '(repeat
  945. (cons (string :tag "name")
  946. (string :tag "value"))))
  947. (defvar org-table-formula-constants-local nil
  948. "Local version of `org-table-formula-constants'.")
  949. (make-variable-buffer-local 'org-table-formula-constants-local)
  950. (defcustom org-table-allow-automatic-line-recalculation t
  951. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  952. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  953. :group 'org-table-calculation
  954. :type 'boolean)
  955. (defgroup org-link nil
  956. "Options concerning links in Org-mode."
  957. :tag "Org Link"
  958. :group 'org)
  959. (defvar org-link-abbrev-alist-local nil
  960. "Buffer-local version of `org-link-abbrev-alist', which see.
  961. The value of this is taken from the #+LINK lines.")
  962. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  963. (defcustom org-link-abbrev-alist nil
  964. "Alist of link abbreviations.
  965. The car of each element is a string, to be replaced at the start of a link.
  966. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  967. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  968. [[linkkey:tag][description]]
  969. If REPLACE is a string, the tag will simply be appended to create the link.
  970. If the string contains \"%s\", the tag will be inserted there.
  971. REPLACE may also be a function that will be called with the tag as the
  972. only argument to create the link, which should be returned as a string.
  973. See the manual for examples."
  974. :group 'org-link
  975. :type 'alist)
  976. (defcustom org-descriptive-links t
  977. "Non-nil means, hide link part and only show description of bracket links.
  978. Bracket links are like [[link][descritpion]]. This variable sets the initial
  979. state in new org-mode buffers. The setting can then be toggled on a
  980. per-buffer basis from the Org->Hyperlinks menu."
  981. :group 'org-link
  982. :type 'boolean)
  983. (defcustom org-link-file-path-type 'adaptive
  984. "How the path name in file links should be stored.
  985. Valid values are:
  986. relative Relative to the current directory, i.e. the directory of the file
  987. into which the link is being inserted.
  988. absolute Absolute path, if possible with ~ for home directory.
  989. noabbrev Absolute path, no abbreviation of home directory.
  990. adaptive Use relative path for files in the current directory and sub-
  991. directories of it. For other files, use an absolute path."
  992. :group 'org-link
  993. :type '(choice
  994. (const relative)
  995. (const absolute)
  996. (const noabbrev)
  997. (const adaptive)))
  998. (defcustom org-activate-links '(bracket angle plain radio tag date)
  999. "Types of links that should be activated in Org-mode files.
  1000. This is a list of symbols, each leading to the activation of a certain link
  1001. type. In principle, it does not hurt to turn on most link types - there may
  1002. be a small gain when turning off unused link types. The types are:
  1003. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1004. angular Links in angular brackes that may contain whitespace like
  1005. <bbdb:Carsten Dominik>.
  1006. plain Plain links in normal text, no whitespace, like http://google.com.
  1007. radio Text that is matched by a radio target, see manual for details.
  1008. tag Tag settings in a headline (link to tag search).
  1009. date Time stamps (link to calendar).
  1010. Changing this variable requires a restart of Emacs to become effective."
  1011. :group 'org-link
  1012. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1013. (const :tag "Angular bracket links (old style)" angular)
  1014. (const :tag "plain text links" plain)
  1015. (const :tag "Radio target matches" radio)
  1016. (const :tag "Tags" tag)
  1017. (const :tag "Tags" target)
  1018. (const :tag "Timestamps" date)))
  1019. (defgroup org-link-store nil
  1020. "Options concerning storing links in Org-mode"
  1021. :tag "Org Store Link"
  1022. :group 'org-link)
  1023. (defcustom org-email-link-description-format "Email %c: %.30s"
  1024. "Format of the description part of a link to an email or usenet message.
  1025. The following %-excapes will be replaced by corresponding information:
  1026. %F full \"From\" field
  1027. %f name, taken from \"From\" field, address if no name
  1028. %T full \"To\" field
  1029. %t first name in \"To\" field, address if no name
  1030. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1031. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1032. %s subject
  1033. %m message-id.
  1034. You may use normal field width specification between the % and the letter.
  1035. This is for example useful to limit the length of the subject.
  1036. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1037. :group 'org-link-store
  1038. :type 'string)
  1039. (defcustom org-from-is-user-regexp
  1040. (let (r1 r2)
  1041. (when (and user-mail-address (not (string= user-mail-address "")))
  1042. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1043. (when (and user-full-name (not (string= user-full-name "")))
  1044. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1045. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1046. "Regexp mached against the \"From:\" header of an email or usenet message.
  1047. It should match if the message is from the user him/herself."
  1048. :group 'org-link-store
  1049. :type 'regexp)
  1050. (defcustom org-context-in-file-links t
  1051. "Non-nil means, file links from `org-store-link' contain context.
  1052. A search string will be added to the file name with :: as separator and
  1053. used to find the context when the link is activated by the command
  1054. `org-open-at-point'.
  1055. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1056. negates this setting for the duration of the command."
  1057. :group 'org-link-store
  1058. :type 'boolean)
  1059. (defcustom org-keep-stored-link-after-insertion nil
  1060. "Non-nil means, keep link in list for entire session.
  1061. The command `org-store-link' adds a link pointing to the current
  1062. location to an internal list. These links accumulate during a session.
  1063. The command `org-insert-link' can be used to insert links into any
  1064. Org-mode file (offering completion for all stored links). When this
  1065. option is nil, every link which has been inserted once using \\[org-insert-link]
  1066. will be removed from the list, to make completing the unused links
  1067. more efficient."
  1068. :group 'org-link-store
  1069. :type 'boolean)
  1070. (defcustom org-usenet-links-prefer-google nil
  1071. "Non-nil means, `org-store-link' will create web links to Google groups.
  1072. When nil, Gnus will be used for such links.
  1073. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1074. negates this setting for the duration of the command."
  1075. :group 'org-link-store
  1076. :type 'boolean)
  1077. (defgroup org-link-follow nil
  1078. "Options concerning following links in Org-mode"
  1079. :tag "Org Follow Link"
  1080. :group 'org-link)
  1081. (defcustom org-tab-follows-link nil
  1082. "Non-nil means, on links TAB will follow the link.
  1083. Needs to be set before org.el is loaded."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-return-follows-link nil
  1087. "Non-nil means, on links RET will follow the link.
  1088. Needs to be set before org.el is loaded."
  1089. :group 'org-link-follow
  1090. :type 'boolean)
  1091. (defcustom org-mouse-1-follows-link t
  1092. "Non-nil means, mouse-1 on a link will follow the link.
  1093. A longer mouse click will still set point. Does not wortk on XEmacs.
  1094. Needs to be set before org.el is loaded."
  1095. :group 'org-link-follow
  1096. :type 'boolean)
  1097. (defcustom org-mark-ring-length 4
  1098. "Number of different positions to be recorded in the ring
  1099. Changing this requires a restart of Emacs to work correctly."
  1100. :group 'org-link-follow
  1101. :type 'interger)
  1102. (defcustom org-link-frame-setup
  1103. '((vm . vm-visit-folder-other-frame)
  1104. (gnus . gnus-other-frame)
  1105. (file . find-file-other-window))
  1106. "Setup the frame configuration for following links.
  1107. When following a link with Emacs, it may often be useful to display
  1108. this link in another window or frame. This variable can be used to
  1109. set this up for the different types of links.
  1110. For VM, use any of
  1111. `vm-visit-folder'
  1112. `vm-visit-folder-other-frame'
  1113. For Gnus, use any of
  1114. `gnus'
  1115. `gnus-other-frame'
  1116. For FILE, use any of
  1117. `find-file'
  1118. `find-file-other-window'
  1119. `find-file-other-frame'
  1120. For the calendar, use the variable `calendar-setup'.
  1121. For BBDB, it is currently only possible to display the matches in
  1122. another window."
  1123. :group 'org-link-follow
  1124. :type '(list
  1125. (cons (const vm)
  1126. (choice
  1127. (const vm-visit-folder)
  1128. (const vm-visit-folder-other-window)
  1129. (const vm-visit-folder-other-frame)))
  1130. (cons (const gnus)
  1131. (choice
  1132. (const gnus)
  1133. (const gnus-other-frame)))
  1134. (cons (const file)
  1135. (choice
  1136. (const find-file)
  1137. (const find-file-other-window)
  1138. (const find-file-other-frame)))))
  1139. (defcustom org-display-internal-link-with-indirect-buffer nil
  1140. "Non-nil means, use indirect buffer to display infile links.
  1141. Activating internal links (from one location in a file to another location
  1142. in the same file) normally just jumps to the location. When the link is
  1143. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1144. another window. When this option is set, the other window actually displays
  1145. an indirect buffer clone of the current buffer, to avoid any visibility
  1146. changes to the current buffer."
  1147. :group 'org-link-follow
  1148. :type 'boolean)
  1149. (defcustom org-open-non-existing-files nil
  1150. "Non-nil means, `org-open-file' will open non-existing files.
  1151. When nil, an error will be generated."
  1152. :group 'org-link-follow
  1153. :type 'boolean)
  1154. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1155. "Function and arguments to call for following mailto links.
  1156. This is a list with the first element being a lisp function, and the
  1157. remaining elements being arguments to the function. In string arguments,
  1158. %a will be replaced by the address, and %s will be replaced by the subject
  1159. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1160. :group 'org-link-follow
  1161. :type '(choice
  1162. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1163. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1164. (const :tag "message-mail" (message-mail "%a" "%s"))
  1165. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1166. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1167. "Non-nil means, ask for confirmation before executing shell links.
  1168. Shell links can be dangerous: just think about a link
  1169. [[shell:rm -rf ~/*][Google Search]]
  1170. This link would show up in your Org-mode document as \"Google Search\",
  1171. but really it would remove your entire home directory.
  1172. Therefore we advise against setting this variable to nil.
  1173. Just change it to `y-or-n-p' of you want to confirm with a
  1174. single keystroke rather than having to type \"yes\"."
  1175. :group 'org-link-follow
  1176. :type '(choice
  1177. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1178. (const :tag "with y-or-n (faster)" y-or-n-p)
  1179. (const :tag "no confirmation (dangerous)" nil)))
  1180. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1181. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1182. Elisp links can be dangerous: just think about a link
  1183. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1184. This link would show up in your Org-mode document as \"Google Search\",
  1185. but really it would remove your entire home directory.
  1186. Therefore we advise against setting this variable to nil.
  1187. Just change it to `y-or-n-p' of you want to confirm with a
  1188. single keystroke rather than having to type \"yes\"."
  1189. :group 'org-link-follow
  1190. :type '(choice
  1191. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1192. (const :tag "with y-or-n (faster)" y-or-n-p)
  1193. (const :tag "no confirmation (dangerous)" nil)))
  1194. (defconst org-file-apps-defaults-gnu
  1195. '((remote . emacs)
  1196. (t . mailcap))
  1197. "Default file applications on a UNIX or GNU/Linux system.
  1198. See `org-file-apps'.")
  1199. (defconst org-file-apps-defaults-macosx
  1200. '((remote . emacs)
  1201. (t . "open %s")
  1202. ("ps" . "gv %s")
  1203. ("ps.gz" . "gv %s")
  1204. ("eps" . "gv %s")
  1205. ("eps.gz" . "gv %s")
  1206. ("dvi" . "xdvi %s")
  1207. ("fig" . "xfig %s"))
  1208. "Default file applications on a MacOS X system.
  1209. The system \"open\" is known as a default, but we use X11 applications
  1210. for some files for which the OS does not have a good default.
  1211. See `org-file-apps'.")
  1212. (defconst org-file-apps-defaults-windowsnt
  1213. (list
  1214. '(remote . emacs)
  1215. (cons t
  1216. (list (if (featurep 'xemacs)
  1217. 'mswindows-shell-execute
  1218. 'w32-shell-execute)
  1219. "open" 'file)))
  1220. "Default file applications on a Windows NT system.
  1221. The system \"open\" is used for most files.
  1222. See `org-file-apps'.")
  1223. (defcustom org-file-apps
  1224. '(
  1225. ("txt" . emacs)
  1226. ("tex" . emacs)
  1227. ("ltx" . emacs)
  1228. ("org" . emacs)
  1229. ("el" . emacs)
  1230. ("bib" . emacs)
  1231. )
  1232. "External applications for opening `file:path' items in a document.
  1233. Org-mode uses system defaults for different file types, but
  1234. you can use this variable to set the application for a given file
  1235. extension. The entries in this list are cons cells where the car identifies
  1236. files and the cdr the corresponding command. Possible values for the
  1237. file identifier are
  1238. \"ext\" A string identifying an extension
  1239. `directory' Matches a directory
  1240. `remote' Matches a remote file, accessible through tramp or efs.
  1241. Remote files most likely should be visited through Emacs
  1242. because external applications cannot handle such paths.
  1243. t Default for all remaining files
  1244. Possible values for the command are:
  1245. `emacs' The file will be visited by the current Emacs process.
  1246. `default' Use the default application for this file type.
  1247. string A command to be executed by a shell; %s will be replaced
  1248. by the path to the file.
  1249. sexp A Lisp form which will be evaluated. The file path will
  1250. be available in the Lisp variable `file'.
  1251. For more examples, see the system specific constants
  1252. `org-file-apps-defaults-macosx'
  1253. `org-file-apps-defaults-windowsnt'
  1254. `org-file-apps-defaults-gnu'."
  1255. :group 'org-link-follow
  1256. :type '(repeat
  1257. (cons (choice :value ""
  1258. (string :tag "Extension")
  1259. (const :tag "Default for unrecognized files" t)
  1260. (const :tag "Remote file" remote)
  1261. (const :tag "Links to a directory" directory))
  1262. (choice :value ""
  1263. (const :tag "Visit with Emacs" emacs)
  1264. (const :tag "Use system default" default)
  1265. (string :tag "Command")
  1266. (sexp :tag "Lisp form")))))
  1267. (defcustom org-mhe-search-all-folders nil
  1268. "Non-nil means, that the search for the mh-message will be extended to
  1269. all folders if the message cannot be found in the folder given in the link.
  1270. Searching all folders is very efficient with one of the search engines
  1271. supported by MH-E, but will be slow with pick."
  1272. :group 'org-link-follow
  1273. :type 'boolean)
  1274. (defgroup org-remember nil
  1275. "Options concerning interaction with remember.el."
  1276. :tag "Org Remember"
  1277. :group 'org)
  1278. (defcustom org-directory "~/org"
  1279. "Directory with org files.
  1280. This directory will be used as default to prompt for org files.
  1281. Used by the hooks for remember.el."
  1282. :group 'org-remember
  1283. :type 'directory)
  1284. (defcustom org-default-notes-file "~/.notes"
  1285. "Default target for storing notes.
  1286. Used by the hooks for remember.el. This can be a string, or nil to mean
  1287. the value of `remember-data-file'.
  1288. You can set this on a per-template basis with the variable
  1289. `org-remember-templates'."
  1290. :group 'org-remember
  1291. :type '(choice
  1292. (const :tag "Default from remember-data-file" nil)
  1293. file))
  1294. (defcustom org-remember-store-without-prompt t
  1295. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1296. In this case, you need `C-u C-c C-c' to get the prompts for
  1297. note file and headline.
  1298. When this variable is nil, `C-c C-c' give you the prompts, and
  1299. `C-u C-c C-c' trigger the fasttrack."
  1300. :group 'org-remember
  1301. :type 'boolean)
  1302. (defcustom org-remember-interactive-interface 'refile
  1303. "The interface to be used for interactive filing of remember notes.
  1304. This is only used when the interactive mode for selecting a filing
  1305. location is used (see the variable `org-remember-store-without-prompt').
  1306. Allowed vaues are:
  1307. outline The interface shows an outline of the relevant file
  1308. and the correct heading is found by moving through
  1309. the outline or by searching with incremental search.
  1310. outline-path-completion Headlines in the current buffer are offered via
  1311. completion.
  1312. refile Use the refile interface, and offer headlines,
  1313. possibly from different buffers."
  1314. :group 'org-remember
  1315. :type '(choice
  1316. (const :tag "Refile" refile)
  1317. (const :tag "Outline" outline)
  1318. (const :tag "Outline-path-completion" outline-path-completion)))
  1319. (defcustom org-goto-interface 'outline
  1320. "The default interface to be used for `org-goto'.
  1321. Allowed vaues are:
  1322. outline The interface shows an outline of the relevant file
  1323. and the correct heading is found by moving through
  1324. the outline or by searching with incremental search.
  1325. outline-path-completion Headlines in the current buffer are offered via
  1326. completion."
  1327. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1328. :type '(choice
  1329. (const :tag "Outline" outline)
  1330. (const :tag "Outline-path-completion" outline-path-completion)))
  1331. (defcustom org-remember-default-headline ""
  1332. "The headline that should be the default location in the notes file.
  1333. When filing remember notes, the cursor will start at that position.
  1334. You can set this on a per-template basis with the variable
  1335. `org-remember-templates'."
  1336. :group 'org-remember
  1337. :type 'string)
  1338. (defcustom org-remember-templates nil
  1339. "Templates for the creation of remember buffers.
  1340. When nil, just let remember make the buffer.
  1341. When not nil, this is a list of 5-element lists. In each entry, the first
  1342. element is the name of the template, which should be a single short word.
  1343. The second element is a character, a unique key to select this template.
  1344. The third element is the template. The fourth element is optional and can
  1345. specify a destination file for remember items created with this template.
  1346. The default file is given by `org-default-notes-file'. An optional fifth
  1347. element can specify the headline in that file that should be offered
  1348. first when the user is asked to file the entry. The default headline is
  1349. given in the variable `org-remember-default-headline'.
  1350. The template specifies the structure of the remember buffer. It should have
  1351. a first line starting with a star, to act as the org-mode headline.
  1352. Furthermore, the following %-escapes will be replaced with content:
  1353. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1354. A default value and a completion table ca be specified like this:
  1355. %^{prompt|default|completion2|completion3|...}
  1356. %t time stamp, date only
  1357. %T time stamp with date and time
  1358. %u, %U like the above, but inactive time stamps
  1359. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1360. You may define a prompt like %^{Please specify birthday}t
  1361. %n user name (taken from `user-full-name')
  1362. %a annotation, normally the link created with org-store-link
  1363. %i initial content, the region active. If %i is indented,
  1364. the entire inserted text will be indented as well.
  1365. %c content of the clipboard, or current kill ring head
  1366. %^g prompt for tags, with completion on tags in target file
  1367. %^G prompt for tags, with completion all tags in all agenda files
  1368. %:keyword specific information for certain link types, see below
  1369. %[pathname] insert the contents of the file given by `pathname'
  1370. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1371. %! Store this note immediately after filling the template
  1372. %? After completing the template, position cursor here.
  1373. Apart from these general escapes, you can access information specific to the
  1374. link type that is created. For example, calling `remember' in emails or gnus
  1375. will record the author and the subject of the message, which you can access
  1376. with %:author and %:subject, respectively. Here is a complete list of what
  1377. is recorded for each link type.
  1378. Link type | Available information
  1379. -------------------+------------------------------------------------------
  1380. bbdb | %:type %:name %:company
  1381. vm, wl, mh, rmail | %:type %:subject %:message-id
  1382. | %:from %:fromname %:fromaddress
  1383. | %:to %:toname %:toaddress
  1384. | %:fromto (either \"to NAME\" or \"from NAME\")
  1385. gnus | %:group, for messages also all email fields
  1386. w3, w3m | %:type %:url
  1387. info | %:type %:file %:node
  1388. calendar | %:type %:date"
  1389. :group 'org-remember
  1390. :get (lambda (var) ; Make sure all entries have 5 elements
  1391. (mapcar (lambda (x)
  1392. (if (not (stringp (car x))) (setq x (cons "" x)))
  1393. (cond ((= (length x) 4) (append x '("")))
  1394. ((= (length x) 3) (append x '("" "")))
  1395. (t x)))
  1396. (default-value var)))
  1397. :type '(repeat
  1398. :tag "enabled"
  1399. (list :value ("" ?a "\n" nil nil)
  1400. (string :tag "Name")
  1401. (character :tag "Selection Key")
  1402. (string :tag "Template")
  1403. (choice
  1404. (file :tag "Destination file")
  1405. (const :tag "Prompt for file" nil))
  1406. (choice
  1407. (string :tag "Destination headline")
  1408. (const :tag "Selection interface for heading")))))
  1409. (defcustom org-reverse-note-order nil
  1410. "Non-nil means, store new notes at the beginning of a file or entry.
  1411. When nil, new notes will be filed to the end of a file or entry.
  1412. This can also be a list with cons cells of regular expressions that
  1413. are matched against file names, and values."
  1414. :group 'org-remember
  1415. :type '(choice
  1416. (const :tag "Reverse always" t)
  1417. (const :tag "Reverse never" nil)
  1418. (repeat :tag "By file name regexp"
  1419. (cons regexp boolean))))
  1420. (defcustom org-refile-targets nil
  1421. "Targets for refiling entries with \\[org-refile].
  1422. This is list of cons cells. Each cell contains:
  1423. - a specification of the files to be considered, either a list of files,
  1424. or a symbol whose function or value fields will be used to retrieve
  1425. a file name or a list of file names. Nil means, refile to a different
  1426. heading in the current buffer.
  1427. - A specification of how to find candidate refile targets. This may be
  1428. any of
  1429. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1430. This tag has to be present in all target headlines, inheritance will
  1431. not be considered.
  1432. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1433. todo keyword.
  1434. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1435. headlines that are refiling targets.
  1436. - a cons cell (:level . N). Any headline of level N is considered a target.
  1437. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1438. ;; FIXME: what if there are a var and func with same name???
  1439. :group 'org-remember
  1440. :type '(repeat
  1441. (cons
  1442. (choice :value org-agenda-files
  1443. (const :tag "All agenda files" org-agenda-files)
  1444. (const :tag "Current buffer" nil)
  1445. (function) (variable) (file))
  1446. (choice :tag "Identify target headline by"
  1447. (cons :tag "Specific tag" (const :tag) (string))
  1448. (cons :tag "TODO keyword" (const :todo) (string))
  1449. (cons :tag "Regular expression" (const :regexp) (regexp))
  1450. (cons :tag "Level number" (const :level) (integer))
  1451. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1452. (defcustom org-refile-use-outline-path nil
  1453. "Non-nil means, provide refile targets as paths.
  1454. So a level 3 headline will be available as level1/level2/level3.
  1455. When the value is `file', also include the file name (without directory)
  1456. into the path. When `full-file-path', include the full file path."
  1457. :group 'org-remember
  1458. :type '(choice
  1459. (const :tag "Not" nil)
  1460. (const :tag "Yes" t)
  1461. (const :tag "Start with file name" file)
  1462. (const :tag "Start with full file path" full-file-path)))
  1463. (defgroup org-todo nil
  1464. "Options concerning TODO items in Org-mode."
  1465. :tag "Org TODO"
  1466. :group 'org)
  1467. (defgroup org-progress nil
  1468. "Options concerning Progress logging in Org-mode."
  1469. :tag "Org Progress"
  1470. :group 'org-time)
  1471. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1472. "List of TODO entry keyword sequences and their interpretation.
  1473. \\<org-mode-map>This is a list of sequences.
  1474. Each sequence starts with a symbol, either `sequence' or `type',
  1475. indicating if the keywords should be interpreted as a sequence of
  1476. action steps, or as different types of TODO items. The first
  1477. keywords are states requiring action - these states will select a headline
  1478. for inclusion into the global TODO list Org-mode produces. If one of
  1479. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1480. signify that no further action is necessary. If \"|\" is not found,
  1481. the last keyword is treated as the only DONE state of the sequence.
  1482. The command \\[org-todo] cycles an entry through these states, and one
  1483. additional state where no keyword is present. For details about this
  1484. cycling, see the manual.
  1485. TODO keywords and interpretation can also be set on a per-file basis with
  1486. the special #+SEQ_TODO and #+TYP_TODO lines.
  1487. Each keyword can optionally specify a character for fast state selection
  1488. \(in combination with the variable `org-use-fast-todo-selection')
  1489. and specifiers for state change logging, using the same syntax
  1490. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1491. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1492. indicates to record a time stamp each time this state is selected.
  1493. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1494. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1495. entering and when leaving this state.
  1496. For backward compatibility, this variable may also be just a list
  1497. of keywords - in this case the interptetation (sequence or type) will be
  1498. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1499. :group 'org-todo
  1500. :group 'org-keywords
  1501. :type '(choice
  1502. (repeat :tag "Old syntax, just keywords"
  1503. (string :tag "Keyword"))
  1504. (repeat :tag "New syntax"
  1505. (cons
  1506. (choice
  1507. :tag "Interpretation"
  1508. (const :tag "Sequence (cycling hits every state)" sequence)
  1509. (const :tag "Type (cycling directly to DONE)" type))
  1510. (repeat
  1511. (string :tag "Keyword"))))))
  1512. (defvar org-todo-keywords-1 nil
  1513. "All TODO and DONE keywords active in a buffer.")
  1514. (make-variable-buffer-local 'org-todo-keywords-1)
  1515. (defvar org-todo-keywords-for-agenda nil)
  1516. (defvar org-done-keywords-for-agenda nil)
  1517. (defvar org-not-done-keywords nil)
  1518. (make-variable-buffer-local 'org-not-done-keywords)
  1519. (defvar org-done-keywords nil)
  1520. (make-variable-buffer-local 'org-done-keywords)
  1521. (defvar org-todo-heads nil)
  1522. (make-variable-buffer-local 'org-todo-heads)
  1523. (defvar org-todo-sets nil)
  1524. (make-variable-buffer-local 'org-todo-sets)
  1525. (defvar org-todo-log-states nil)
  1526. (make-variable-buffer-local 'org-todo-log-states)
  1527. (defvar org-todo-kwd-alist nil)
  1528. (make-variable-buffer-local 'org-todo-kwd-alist)
  1529. (defvar org-todo-key-alist nil)
  1530. (make-variable-buffer-local 'org-todo-key-alist)
  1531. (defvar org-todo-key-trigger nil)
  1532. (make-variable-buffer-local 'org-todo-key-trigger)
  1533. (defcustom org-todo-interpretation 'sequence
  1534. "Controls how TODO keywords are interpreted.
  1535. This variable is in principle obsolete and is only used for
  1536. backward compatibility, if the interpretation of todo keywords is
  1537. not given already in `org-todo-keywords'. See that variable for
  1538. more information."
  1539. :group 'org-todo
  1540. :group 'org-keywords
  1541. :type '(choice (const sequence)
  1542. (const type)))
  1543. (defcustom org-use-fast-todo-selection 'prefix
  1544. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1545. This variable describes if and under what circumstances the cycling
  1546. mechanism for TODO keywords will be replaced by a single-key, direct
  1547. selection scheme.
  1548. When nil, fast selection is never used.
  1549. When the symbol `prefix', it will be used when `org-todo' is called with
  1550. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1551. in an agenda buffer.
  1552. When t, fast selection is used by default. In this case, the prefix
  1553. argument forces cycling instead.
  1554. In all cases, the special interface is only used if access keys have actually
  1555. been assigned by the user, i.e. if keywords in the configuration are followed
  1556. by a letter in parenthesis, like TODO(t)."
  1557. :group 'org-todo
  1558. :type '(choice
  1559. (const :tag "Never" nil)
  1560. (const :tag "By default" t)
  1561. (const :tag "Only with C-u C-c C-t" prefix)))
  1562. (defcustom org-after-todo-state-change-hook nil
  1563. "Hook which is run after the state of a TODO item was changed.
  1564. The new state (a string with a TODO keyword, or nil) is available in the
  1565. Lisp variable `state'."
  1566. :group 'org-todo
  1567. :type 'hook)
  1568. (defcustom org-log-done nil
  1569. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1570. When equal to the list (done), also prompt for a closing note.
  1571. This can also be configured on a per-file basis by adding one of
  1572. the following lines anywhere in the buffer:
  1573. #+STARTUP: logdone
  1574. #+STARTUP: lognotedone
  1575. #+STARTUP: nologdone"
  1576. :group 'org-todo
  1577. :group 'org-progress
  1578. :type '(choice
  1579. (const :tag "No logging" nil)
  1580. (const :tag "Record CLOSED timestamp" time)
  1581. (const :tag "Record CLOSED timestamp with closing note." note)))
  1582. ;; Normalize old uses of org-log-done.
  1583. (cond
  1584. ((eq org-log-done t) (setq org-log-done 'time))
  1585. ((and (listp org-log-done) (memq 'done org-log-done))
  1586. (setq org-log-done 'note)))
  1587. ;; FIXME: document
  1588. (defcustom org-log-note-clock-out nil
  1589. "Non-nil means, recored a note when clocking out of an item.
  1590. This can also be configured on a per-file basis by adding one of
  1591. the following lines anywhere in the buffer:
  1592. #+STARTUP: lognoteclock-out
  1593. #+STARTUP: nolognoteclock-out"
  1594. :group 'org-todo
  1595. :group 'org-progress
  1596. :type 'boolean)
  1597. (defcustom org-log-done-with-time t
  1598. "Non-nil means, the CLOSED time stamp will contain date and time.
  1599. When nil, only the date will be recorded."
  1600. :group 'org-progress
  1601. :type 'boolean)
  1602. (defcustom org-log-note-headings
  1603. '((done . "CLOSING NOTE %t")
  1604. (state . "State %-12s %t")
  1605. (clock-out . ""))
  1606. "Headings for notes added when clocking out or closing TODO items.
  1607. The value is an alist, with the car being a symbol indicating the note
  1608. context, and the cdr is the heading to be used. The heading may also be the
  1609. empty string.
  1610. %t in the heading will be replaced by a time stamp.
  1611. %s will be replaced by the new TODO state, in double quotes.
  1612. %u will be replaced by the user name.
  1613. %U will be replaced by the full user name."
  1614. :group 'org-todo
  1615. :group 'org-progress
  1616. :type '(list :greedy t
  1617. (cons (const :tag "Heading when closing an item" done) string)
  1618. (cons (const :tag
  1619. "Heading when changing todo state (todo sequence only)"
  1620. state) string)
  1621. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1622. (defcustom org-log-states-order-reversed t
  1623. "Non-nil means, the latest state change note will be directly after heading.
  1624. When nil, the notes will be orderer according to time."
  1625. :group 'org-todo
  1626. :group 'org-progress
  1627. :type 'boolean)
  1628. (defcustom org-log-repeat 'time
  1629. "Non-nil means, record moving through the DONE state when triggering repeat.
  1630. An auto-repeating tasks is immediately switched back to TODO when marked
  1631. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1632. the TODO keyword definition, or recording a cloing note by setting
  1633. `org-log-done', there will be no record of the task moving trhough DONE.
  1634. This variable forces taking a note anyway. Possible values are:
  1635. nil Don't force a record
  1636. time Record a time stamp
  1637. note Record a note
  1638. This option can also be set with on a per-file-basis with
  1639. #+STARTUP: logrepeat
  1640. #+STARTUP: lognoterepeat
  1641. #+STARTUP: nologrepeat
  1642. You can have local logging settings for a subtree by setting the LOGGING
  1643. property to one or more of these keywords."
  1644. :group 'org-todo
  1645. :group 'org-progress
  1646. :type '(choice
  1647. (const :tag "Don't force a record" nil)
  1648. (const :tag "Force recording the DONE state" time)
  1649. (const :tag "Force recording a note with the DONE state" note)))
  1650. (defcustom org-clock-into-drawer 2
  1651. "Should clocking info be wrapped into a drawer?
  1652. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1653. If necessary, the drawer will be created.
  1654. When nil, the drawer will not be created, but used when present.
  1655. When an integer and the number of clocking entries in an item
  1656. reaches or exceeds this number, a drawer will be created."
  1657. :group 'org-todo
  1658. :group 'org-progress
  1659. :type '(choice
  1660. (const :tag "Always" t)
  1661. (const :tag "Only when drawer exists" nil)
  1662. (integer :tag "When at least N clock entries")))
  1663. (defcustom org-clock-out-when-done t
  1664. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1665. Nil means, clock will keep running until stopped explicitly with
  1666. `C-c C-x C-o', or until the clock is started in a different item."
  1667. :group 'org-progress
  1668. :type 'boolean)
  1669. (defcustom org-clock-in-switch-to-state nil
  1670. "Set task to a special todo state while clocking it.
  1671. The value should be the state to which the entry should be switched."
  1672. :group 'org-progress
  1673. :group 'org-todo
  1674. :type '(choice
  1675. (const :tag "Don't force a state" nil)
  1676. (string :tag "State")))
  1677. (defgroup org-priorities nil
  1678. "Priorities in Org-mode."
  1679. :tag "Org Priorities"
  1680. :group 'org-todo)
  1681. (defcustom org-highest-priority ?A
  1682. "The highest priority of TODO items. A character like ?A, ?B etc.
  1683. Must have a smaller ASCII number than `org-lowest-priority'."
  1684. :group 'org-priorities
  1685. :type 'character)
  1686. (defcustom org-lowest-priority ?C
  1687. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1688. Must have a larger ASCII number than `org-highest-priority'."
  1689. :group 'org-priorities
  1690. :type 'character)
  1691. (defcustom org-default-priority ?B
  1692. "The default priority of TODO items.
  1693. This is the priority an item get if no explicit priority is given."
  1694. :group 'org-priorities
  1695. :type 'character)
  1696. (defcustom org-priority-start-cycle-with-default t
  1697. "Non-nil means, start with default priority when starting to cycle.
  1698. When this is nil, the first step in the cycle will be (depending on the
  1699. command used) one higher or lower that the default priority."
  1700. :group 'org-priorities
  1701. :type 'boolean)
  1702. (defgroup org-time nil
  1703. "Options concerning time stamps and deadlines in Org-mode."
  1704. :tag "Org Time"
  1705. :group 'org)
  1706. (defcustom org-insert-labeled-timestamps-at-point nil
  1707. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1708. When nil, these labeled time stamps are forces into the second line of an
  1709. entry, just after the headline. When scheduling from the global TODO list,
  1710. the time stamp will always be forced into the second line."
  1711. :group 'org-time
  1712. :type 'boolean)
  1713. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1714. "Formats for `format-time-string' which are used for time stamps.
  1715. It is not recommended to change this constant.")
  1716. (defcustom org-time-stamp-rounding-minutes 0
  1717. "Number of minutes to round time stamps to upon insertion.
  1718. When zero, insert the time unmodified. Useful rounding numbers
  1719. should be factors of 60, so for example 5, 10, 15.
  1720. When this is not zero, you can still force an exact time-stamp by using
  1721. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1722. :group 'org-time
  1723. :type 'integer)
  1724. (defcustom org-display-custom-times nil
  1725. "Non-nil means, overlay custom formats over all time stamps.
  1726. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1727. To turn this on on a per-file basis, insert anywhere in the file:
  1728. #+STARTUP: customtime"
  1729. :group 'org-time
  1730. :set 'set-default
  1731. :type 'sexp)
  1732. (make-variable-buffer-local 'org-display-custom-times)
  1733. (defcustom org-time-stamp-custom-formats
  1734. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1735. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1736. These are overlayed over the default ISO format if the variable
  1737. `org-display-custom-times' is set. Time like %H:%M should be at the
  1738. end of the second format."
  1739. :group 'org-time
  1740. :type 'sexp)
  1741. (defun org-time-stamp-format (&optional long inactive)
  1742. "Get the right format for a time string."
  1743. (let ((f (if long (cdr org-time-stamp-formats)
  1744. (car org-time-stamp-formats))))
  1745. (if inactive
  1746. (concat "[" (substring f 1 -1) "]")
  1747. f)))
  1748. (defcustom org-read-date-prefer-future t
  1749. "Non-nil means, assume future for incomplete date input from user.
  1750. This affects the following situations:
  1751. 1. The user gives a day, but no month.
  1752. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1753. read this as the third of *next* month. However, if you enter \"17\",
  1754. it will be considered as *this* month.
  1755. 2. The user gives a month but not a year.
  1756. For example, if it is april and you enter \"feb 2\", this will be read
  1757. as feb 2, *next* year. \"May 5\", however, will be this year.
  1758. When this option is nil, the current month and year will always be used
  1759. as defaults."
  1760. :group 'org-time
  1761. :type 'boolean)
  1762. (defcustom org-read-date-display-live t
  1763. "Non-nil means, display current interpretation of date prompt live.
  1764. This display will be in an overlay, in the minibuffer."
  1765. :group 'org-time
  1766. :type 'boolean)
  1767. (defcustom org-read-date-popup-calendar t
  1768. "Non-nil means, pop up a calendar when prompting for a date.
  1769. In the calendar, the date can be selected with mouse-1. However, the
  1770. minibuffer will also be active, and you can simply enter the date as well.
  1771. When nil, only the minibuffer will be available."
  1772. :group 'org-time
  1773. :type 'boolean)
  1774. (if (fboundp 'defvaralias)
  1775. (defvaralias 'org-popup-calendar-for-date-prompt
  1776. 'org-read-date-popup-calendar))
  1777. (defcustom org-extend-today-until 0
  1778. "The hour when your day really ends.
  1779. This has influence for the following applications:
  1780. - When switching the agenda to \"today\". It it is still earlier than
  1781. the time given here, the day recognized as TODAY is actually yesterday.
  1782. - When a date is read from the user and it is still before the time given
  1783. here, the current date and time will be assumed to be yesterday, 23:59.
  1784. FIXME:
  1785. IMPORTANT: This is still a very experimental feature, it may disappear
  1786. again or it may be extended to mean more things."
  1787. :group 'org-time
  1788. :type 'number)
  1789. (defcustom org-edit-timestamp-down-means-later nil
  1790. "Non-nil means, S-down will increase the time in a time stamp.
  1791. When nil, S-up will increase."
  1792. :group 'org-time
  1793. :type 'boolean)
  1794. (defcustom org-calendar-follow-timestamp-change t
  1795. "Non-nil means, make the calendar window follow timestamp changes.
  1796. When a timestamp is modified and the calendar window is visible, it will be
  1797. moved to the new date."
  1798. :group 'org-time
  1799. :type 'boolean)
  1800. (defcustom org-clock-heading-function nil
  1801. "When non-nil, should be a function to create `org-clock-heading'.
  1802. This is the string shown in the mode line when a clock is running.
  1803. The function is called with point at the beginning of the headline."
  1804. :group 'org-time ; FIXME: Should we have a separate group????
  1805. :type 'function)
  1806. (defgroup org-tags nil
  1807. "Options concerning tags in Org-mode."
  1808. :tag "Org Tags"
  1809. :group 'org)
  1810. (defcustom org-tag-alist nil
  1811. "List of tags allowed in Org-mode files.
  1812. When this list is nil, Org-mode will base TAG input on what is already in the
  1813. buffer.
  1814. The value of this variable is an alist, the car of each entry must be a
  1815. keyword as a string, the cdr may be a character that is used to select
  1816. that tag through the fast-tag-selection interface.
  1817. See the manual for details."
  1818. :group 'org-tags
  1819. :type '(repeat
  1820. (choice
  1821. (cons (string :tag "Tag name")
  1822. (character :tag "Access char"))
  1823. (const :tag "Start radio group" (:startgroup))
  1824. (const :tag "End radio group" (:endgroup)))))
  1825. (defcustom org-use-fast-tag-selection 'auto
  1826. "Non-nil means, use fast tag selection scheme.
  1827. This is a special interface to select and deselect tags with single keys.
  1828. When nil, fast selection is never used.
  1829. When the symbol `auto', fast selection is used if and only if selection
  1830. characters for tags have been configured, either through the variable
  1831. `org-tag-alist' or through a #+TAGS line in the buffer.
  1832. When t, fast selection is always used and selection keys are assigned
  1833. automatically if necessary."
  1834. :group 'org-tags
  1835. :type '(choice
  1836. (const :tag "Always" t)
  1837. (const :tag "Never" nil)
  1838. (const :tag "When selection characters are configured" 'auto)))
  1839. (defcustom org-fast-tag-selection-single-key nil
  1840. "Non-nil means, fast tag selection exits after first change.
  1841. When nil, you have to press RET to exit it.
  1842. During fast tag selection, you can toggle this flag with `C-c'.
  1843. This variable can also have the value `expert'. In this case, the window
  1844. displaying the tags menu is not even shown, until you press C-c again."
  1845. :group 'org-tags
  1846. :type '(choice
  1847. (const :tag "No" nil)
  1848. (const :tag "Yes" t)
  1849. (const :tag "Expert" expert)))
  1850. (defvar org-fast-tag-selection-include-todo nil
  1851. "Non-nil means, fast tags selection interface will also offer TODO states.
  1852. This is an undocumented feature, you should not rely on it.")
  1853. (defcustom org-tags-column -80
  1854. "The column to which tags should be indented in a headline.
  1855. If this number is positive, it specifies the column. If it is negative,
  1856. it means that the tags should be flushright to that column. For example,
  1857. -80 works well for a normal 80 character screen."
  1858. :group 'org-tags
  1859. :type 'integer)
  1860. (defcustom org-auto-align-tags t
  1861. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1862. These operations change the length of a headline and therefore shift
  1863. the tags around. With this options turned on, after each such operation
  1864. the tags are again aligned to `org-tags-column'."
  1865. :group 'org-tags
  1866. :type 'boolean)
  1867. (defcustom org-use-tag-inheritance t
  1868. "Non-nil means, tags in levels apply also for sublevels.
  1869. When nil, only the tags directly given in a specific line apply there.
  1870. If you turn off this option, you very likely want to turn on the
  1871. companion option `org-tags-match-list-sublevels'."
  1872. :group 'org-tags
  1873. :type 'boolean)
  1874. (defcustom org-tags-match-list-sublevels nil
  1875. "Non-nil means list also sublevels of headlines matching tag search.
  1876. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1877. the sublevels of a headline matching a tag search often also match
  1878. the same search. Listing all of them can create very long lists.
  1879. Setting this variable to nil causes subtrees of a match to be skipped.
  1880. This option is off by default, because inheritance in on. If you turn
  1881. inheritance off, you very likely want to turn this option on.
  1882. As a special case, if the tag search is restricted to TODO items, the
  1883. value of this variable is ignored and sublevels are always checked, to
  1884. make sure all corresponding TODO items find their way into the list."
  1885. :group 'org-tags
  1886. :type 'boolean)
  1887. (defvar org-tags-history nil
  1888. "History of minibuffer reads for tags.")
  1889. (defvar org-last-tags-completion-table nil
  1890. "The last used completion table for tags.")
  1891. (defvar org-after-tags-change-hook nil
  1892. "Hook that is run after the tags in a line have changed.")
  1893. (defgroup org-properties nil
  1894. "Options concerning properties in Org-mode."
  1895. :tag "Org Properties"
  1896. :group 'org)
  1897. (defcustom org-property-format "%-10s %s"
  1898. "How property key/value pairs should be formatted by `indent-line'.
  1899. When `indent-line' hits a property definition, it will format the line
  1900. according to this format, mainly to make sure that the values are
  1901. lined-up with respect to each other."
  1902. :group 'org-properties
  1903. :type 'string)
  1904. (defcustom org-use-property-inheritance nil
  1905. "Non-nil means, properties apply also for sublevels.
  1906. This setting is only relevant during property searches, not when querying
  1907. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1908. you need to call `org-entry-get' with the inheritance flag.
  1909. Turning this on can cause significant overhead when doing a search, so
  1910. this is turned off by default.
  1911. When nil, only the properties directly given in the current entry count.
  1912. The value may also be a list of properties that shouldhave inheritance.
  1913. However, note that some special properties use inheritance under special
  1914. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1915. and the properties ending in \"_ALL\" when they are used as descriptor
  1916. for valid values of a property."
  1917. :group 'org-properties
  1918. :type '(choice
  1919. (const :tag "Not" nil)
  1920. (const :tag "Always" nil)
  1921. (repeat :tag "Specific properties" (string :tag "Property"))))
  1922. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1923. "The default column format, if no other format has been defined.
  1924. This variable can be set on the per-file basis by inserting a line
  1925. #+COLUMNS: %25ITEM ....."
  1926. :group 'org-properties
  1927. :type 'string)
  1928. (defcustom org-global-properties nil
  1929. "List of property/value pairs that can be inherited by any entry.
  1930. You can set buffer-local values for this by adding lines like
  1931. #+PROPERTY: NAME VALUE"
  1932. :group 'org-properties
  1933. :type '(repeat
  1934. (cons (string :tag "Property")
  1935. (string :tag "Value"))))
  1936. (defvar org-local-properties nil
  1937. "List of property/value pairs that can be inherited by any entry.
  1938. Valid for the current buffer.
  1939. This variable is populated from #+PROPERTY lines.")
  1940. (defgroup org-agenda nil
  1941. "Options concerning agenda views in Org-mode."
  1942. :tag "Org Agenda"
  1943. :group 'org)
  1944. (defvar org-category nil
  1945. "Variable used by org files to set a category for agenda display.
  1946. Such files should use a file variable to set it, for example
  1947. # -*- mode: org; org-category: \"ELisp\"
  1948. or contain a special line
  1949. #+CATEGORY: ELisp
  1950. If the file does not specify a category, then file's base name
  1951. is used instead.")
  1952. (make-variable-buffer-local 'org-category)
  1953. (defcustom org-agenda-files nil
  1954. "The files to be used for agenda display.
  1955. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1956. \\[org-remove-file]. You can also use customize to edit the list.
  1957. If an entry is a directory, all files in that directory that are matched by
  1958. `org-agenda-file-regexp' will be part of the file list.
  1959. If the value of the variable is not a list but a single file name, then
  1960. the list of agenda files is actually stored and maintained in that file, one
  1961. agenda file per line."
  1962. :group 'org-agenda
  1963. :type '(choice
  1964. (repeat :tag "List of files and directories" file)
  1965. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1966. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1967. "Regular expression to match files for `org-agenda-files'.
  1968. If any element in the list in that variable contains a directory instead
  1969. of a normal file, all files in that directory that are matched by this
  1970. regular expression will be included."
  1971. :group 'org-agenda
  1972. :type 'regexp)
  1973. (defcustom org-agenda-skip-unavailable-files nil
  1974. "t means to just skip non-reachable files in `org-agenda-files'.
  1975. Nil means to remove them, after a query, from the list."
  1976. :group 'org-agenda
  1977. :type 'boolean)
  1978. (defcustom org-agenda-multi-occur-extra-files nil
  1979. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1980. The files in `org-agenda-files' are always searched."
  1981. :group 'org-agenda
  1982. :type '(repeat file))
  1983. (defcustom org-agenda-confirm-kill 1
  1984. "When set, remote killing from the agenda buffer needs confirmation.
  1985. When t, a confirmation is always needed. When a number N, confirmation is
  1986. only needed when the text to be killed contains more than N non-white lines."
  1987. :group 'org-agenda
  1988. :type '(choice
  1989. (const :tag "Never" nil)
  1990. (const :tag "Always" t)
  1991. (number :tag "When more than N lines")))
  1992. (defcustom org-calendar-to-agenda-key [?c]
  1993. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1994. The command `org-calendar-goto-agenda' will be bound to this key. The
  1995. default is the character `c' because then `c' can be used to switch back and
  1996. forth between agenda and calendar."
  1997. :group 'org-agenda
  1998. :type 'sexp)
  1999. (defcustom org-agenda-compact-blocks nil
  2000. "Non-nil means, make the block agenda more compact.
  2001. This is done by leaving out unnecessary lines."
  2002. :group 'org-agenda
  2003. :type nil)
  2004. (defgroup org-agenda-export nil
  2005. "Options concerning exporting agenda views in Org-mode."
  2006. :tag "Org Agenda Export"
  2007. :group 'org-agenda)
  2008. (defcustom org-agenda-with-colors t
  2009. "Non-nil means, use colors in agenda views."
  2010. :group 'org-agenda-export
  2011. :type 'boolean)
  2012. (defcustom org-agenda-exporter-settings nil
  2013. "Alist of variable/value pairs that should be active during agenda export.
  2014. This is a good place to set uptions for ps-print and for htmlize."
  2015. :group 'org-agenda-export
  2016. :type '(repeat
  2017. (list
  2018. (variable)
  2019. (sexp :tag "Value"))))
  2020. (defcustom org-agenda-export-html-style ""
  2021. "The style specification for exported HTML Agenda files.
  2022. If this variable contains a string, it will replace the default <style>
  2023. section as produced by `htmlize'.
  2024. Since there are different ways of setting style information, this variable
  2025. needs to contain the full HTML structure to provide a style, including the
  2026. surrounding HTML tags. The style specifications should include definitions
  2027. the fonts used by the agenda, here is an example:
  2028. <style type=\"text/css\">
  2029. p { font-weight: normal; color: gray; }
  2030. .org-agenda-structure {
  2031. font-size: 110%;
  2032. color: #003399;
  2033. font-weight: 600;
  2034. }
  2035. .org-todo {
  2036. color: #cc6666;Week-agenda:
  2037. font-weight: bold;
  2038. }
  2039. .org-done {
  2040. color: #339933;
  2041. }
  2042. .title { text-align: center; }
  2043. .todo, .deadline { color: red; }
  2044. .done { color: green; }
  2045. </style>
  2046. or, if you want to keep the style in a file,
  2047. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2048. As the value of this option simply gets inserted into the HTML <head> header,
  2049. you can \"misuse\" it to also add other text to the header. However,
  2050. <style>...</style> is required, if not present the variable will be ignored."
  2051. :group 'org-agenda-export
  2052. :group 'org-export-html
  2053. :type 'string)
  2054. (defgroup org-agenda-custom-commands nil
  2055. "Options concerning agenda views in Org-mode."
  2056. :tag "Org Agenda Custom Commands"
  2057. :group 'org-agenda)
  2058. (defcustom org-agenda-custom-commands nil
  2059. "Custom commands for the agenda.
  2060. These commands will be offered on the splash screen displayed by the
  2061. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2062. (key desc type match options files)
  2063. key The key (one or more characters as a string) to be associated
  2064. with the command.
  2065. desc A description of the commend, when omitted or nil, a default
  2066. description is built using MATCH.
  2067. type The command type, any of the following symbols:
  2068. todo Entries with a specific TODO keyword, in all agenda files.
  2069. tags Tags match in all agenda files.
  2070. tags-todo Tags match in all agenda files, TODO entries only.
  2071. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2072. tags-tree Sparse tree with all tags matches in *current* file.
  2073. occur-tree Occur sparse tree for *current* file.
  2074. ... A user-defined function.
  2075. match What to search for:
  2076. - a single keyword for TODO keyword searches
  2077. - a tags match expression for tags searches
  2078. - a regular expression for occur searches
  2079. options A list of option settings, similar to that in a let form, so like
  2080. this: ((opt1 val1) (opt2 val2) ...)
  2081. files A list of files file to write the produced agenda buffer to
  2082. with the command `org-store-agenda-views'.
  2083. If a file name ends in \".html\", an HTML version of the buffer
  2084. is written out. If it ends in \".ps\", a postscript version is
  2085. produced. Otherwide, only the plain text is written to the file.
  2086. You can also define a set of commands, to create a composite agenda buffer.
  2087. In this case, an entry looks like this:
  2088. (key desc (cmd1 cmd2 ...) general-options file)
  2089. where
  2090. desc A description string to be displayed in the dispatcher menu.
  2091. cmd An agenda command, similar to the above. However, tree commands
  2092. are no allowed, but instead you can get agenda and global todo list.
  2093. So valid commands for a set are:
  2094. (agenda)
  2095. (alltodo)
  2096. (stuck)
  2097. (todo \"match\" options files)
  2098. (tags \"match\" options files)
  2099. (tags-todo \"match\" options files)
  2100. Each command can carry a list of options, and another set of options can be
  2101. given for the whole set of commands. Individual command options take
  2102. precedence over the general options.
  2103. When using several characters as key to a command, the first characters
  2104. are prefix commands. For the dispatcher to display useful information, you
  2105. should provide a description for the prefix, like
  2106. (setq org-agenda-custom-commands
  2107. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2108. (\"hl\" tags \"+HOME+Lisa\")
  2109. (\"hp\" tags \"+HOME+Peter\")
  2110. (\"hk\" tags \"+HOME+Kim\")))"
  2111. :group 'org-agenda-custom-commands
  2112. :type '(repeat
  2113. (choice :value ("a" "" tags "" nil)
  2114. (list :tag "Single command"
  2115. (string :tag "Access Key(s) ")
  2116. (option (string :tag "Description"))
  2117. (choice
  2118. (const :tag "Agenda" agenda)
  2119. (const :tag "TODO list" alltodo)
  2120. (const :tag "Stuck projects" stuck)
  2121. (const :tag "Tags search (all agenda files)" tags)
  2122. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2123. (const :tag "TODO keyword search (all agenda files)" todo)
  2124. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2125. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2126. (const :tag "Occur tree (current buffer)" occur-tree)
  2127. (sexp :tag "Other, user-defined function"))
  2128. (string :tag "Match")
  2129. (repeat :tag "Local options"
  2130. (list (variable :tag "Option") (sexp :tag "Value")))
  2131. (option (repeat :tag "Export" (file :tag "Export to"))))
  2132. (list :tag "Command series, all agenda files"
  2133. (string :tag "Access Key(s)")
  2134. (string :tag "Description ")
  2135. (repeat
  2136. (choice
  2137. (const :tag "Agenda" (agenda))
  2138. (const :tag "TODO list" (alltodo))
  2139. (const :tag "Stuck projects" (stuck))
  2140. (list :tag "Tags search"
  2141. (const :format "" tags)
  2142. (string :tag "Match")
  2143. (repeat :tag "Local options"
  2144. (list (variable :tag "Option")
  2145. (sexp :tag "Value"))))
  2146. (list :tag "Tags search, TODO entries only"
  2147. (const :format "" tags-todo)
  2148. (string :tag "Match")
  2149. (repeat :tag "Local options"
  2150. (list (variable :tag "Option")
  2151. (sexp :tag "Value"))))
  2152. (list :tag "TODO keyword search"
  2153. (const :format "" todo)
  2154. (string :tag "Match")
  2155. (repeat :tag "Local options"
  2156. (list (variable :tag "Option")
  2157. (sexp :tag "Value"))))
  2158. (list :tag "Other, user-defined function"
  2159. (symbol :tag "function")
  2160. (string :tag "Match")
  2161. (repeat :tag "Local options"
  2162. (list (variable :tag "Option")
  2163. (sexp :tag "Value"))))))
  2164. (repeat :tag "General options"
  2165. (list (variable :tag "Option")
  2166. (sexp :tag "Value")))
  2167. (option (repeat :tag "Export" (file :tag "Export to"))))
  2168. (cons :tag "Prefix key documentation"
  2169. (string :tag "Access Key(s)")
  2170. (string :tag "Description ")))))
  2171. (defcustom org-stuck-projects
  2172. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2173. "How to identify stuck projects.
  2174. This is a list of four items:
  2175. 1. A tags/todo matcher string that is used to identify a project.
  2176. The entire tree below a headline matched by this is considered one project.
  2177. 2. A list of TODO keywords identifying non-stuck projects.
  2178. If the project subtree contains any headline with one of these todo
  2179. keywords, the project is considered to be not stuck. If you specify
  2180. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2181. 3. A list of tags identifying non-stuck projects.
  2182. If the project subtree contains any headline with one of these tags,
  2183. the project is considered to be not stuck. If you specify \"*\" as
  2184. a tag, any tag will mark the project unstuck.
  2185. 4. An arbitrary regular expression matching non-stuck projects.
  2186. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2187. or `C-c a #' to produce the list."
  2188. :group 'org-agenda-custom-commands
  2189. :type '(list
  2190. (string :tag "Tags/TODO match to identify a project")
  2191. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2192. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2193. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2194. (defgroup org-agenda-skip nil
  2195. "Options concerning skipping parts of agenda files."
  2196. :tag "Org Agenda Skip"
  2197. :group 'org-agenda)
  2198. (defcustom org-agenda-todo-list-sublevels t
  2199. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2200. When nil, the sublevels of a TODO entry are not checked, resulting in
  2201. potentially much shorter TODO lists."
  2202. :group 'org-agenda-skip
  2203. :group 'org-todo
  2204. :type 'boolean)
  2205. (defcustom org-agenda-todo-ignore-with-date nil
  2206. "Non-nil means, don't show entries with a date in the global todo list.
  2207. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2208. but don't want them to show up in the TODO list.
  2209. When this is set, it also covers deadlines and scheduled items, the settings
  2210. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2211. will be ignored."
  2212. :group 'org-agenda-skip
  2213. :group 'org-todo
  2214. :type 'boolean)
  2215. (defcustom org-agenda-todo-ignore-scheduled nil
  2216. "Non-nil means, don't show scheduled entries in the global todo list.
  2217. The idea behind this is that by scheduling it, you have already taken care
  2218. of this item.
  2219. See also `org-agenda-todo-ignore-with-date'."
  2220. :group 'org-agenda-skip
  2221. :group 'org-todo
  2222. :type 'boolean)
  2223. (defcustom org-agenda-todo-ignore-deadlines nil
  2224. "Non-nil means, don't show near deadline entries in the global todo list.
  2225. Near means closer than `org-deadline-warning-days' days.
  2226. The idea behind this is that such items will appear in the agenda anyway.
  2227. See also `org-agenda-todo-ignore-with-date'."
  2228. :group 'org-agenda-skip
  2229. :group 'org-todo
  2230. :type 'boolean)
  2231. (defcustom org-agenda-skip-scheduled-if-done nil
  2232. "Non-nil means don't show scheduled items in agenda when they are done.
  2233. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2234. it applies only to the actual date of the scheduling. Warnings about
  2235. an item with a past scheduling dates are always turned off when the item
  2236. is DONE."
  2237. :group 'org-agenda-skip
  2238. :type 'boolean)
  2239. (defcustom org-agenda-skip-deadline-if-done nil
  2240. "Non-nil means don't show deadines when the corresponding item is done.
  2241. When nil, the deadline is still shown and should give you a happy feeling.
  2242. This is relevant for the daily/weekly agenda. And it applied only to the
  2243. actualy date of the deadline. Warnings about approching and past-due
  2244. deadlines are always turned off when the item is DONE."
  2245. :group 'org-agenda-skip
  2246. :type 'boolean)
  2247. (defcustom org-agenda-skip-timestamp-if-done nil
  2248. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2249. :group 'org-agenda-skip
  2250. :type 'boolean)
  2251. (defcustom org-timeline-show-empty-dates 3
  2252. "Non-nil means, `org-timeline' also shows dates without an entry.
  2253. When nil, only the days which actually have entries are shown.
  2254. When t, all days between the first and the last date are shown.
  2255. When an integer, show also empty dates, but if there is a gap of more than
  2256. N days, just insert a special line indicating the size of the gap."
  2257. :group 'org-agenda-skip
  2258. :type '(choice
  2259. (const :tag "None" nil)
  2260. (const :tag "All" t)
  2261. (number :tag "at most")))
  2262. (defgroup org-agenda-startup nil
  2263. "Options concerning initial settings in the Agenda in Org Mode."
  2264. :tag "Org Agenda Startup"
  2265. :group 'org-agenda)
  2266. (defcustom org-finalize-agenda-hook nil
  2267. "Hook run just before displaying an agenda buffer."
  2268. :group 'org-agenda-startup
  2269. :type 'hook)
  2270. (defcustom org-agenda-mouse-1-follows-link nil
  2271. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2272. A longer mouse click will still set point. Does not wortk on XEmacs.
  2273. Needs to be set before org.el is loaded."
  2274. :group 'org-agenda-startup
  2275. :type 'boolean)
  2276. (defcustom org-agenda-start-with-follow-mode nil
  2277. "The initial value of follow-mode in a newly created agenda window."
  2278. :group 'org-agenda-startup
  2279. :type 'boolean)
  2280. (defgroup org-agenda-windows nil
  2281. "Options concerning the windows used by the Agenda in Org Mode."
  2282. :tag "Org Agenda Windows"
  2283. :group 'org-agenda)
  2284. (defcustom org-agenda-window-setup 'reorganize-frame
  2285. "How the agenda buffer should be displayed.
  2286. Possible values for this option are:
  2287. current-window Show agenda in the current window, keeping all other windows.
  2288. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2289. other-window Use `switch-to-buffer-other-window' to display agenda.
  2290. reorganize-frame Show only two windows on the current frame, the current
  2291. window and the agenda.
  2292. See also the variable `org-agenda-restore-windows-after-quit'."
  2293. :group 'org-agenda-windows
  2294. :type '(choice
  2295. (const current-window)
  2296. (const other-frame)
  2297. (const other-window)
  2298. (const reorganize-frame)))
  2299. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2300. "The min and max height of the agenda window as a fraction of frame height.
  2301. The value of the variable is a cons cell with two numbers between 0 and 1.
  2302. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2303. :group 'org-agenda-windows
  2304. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2305. (defcustom org-agenda-restore-windows-after-quit nil
  2306. "Non-nil means, restore window configuration open exiting agenda.
  2307. Before the window configuration is changed for displaying the agenda,
  2308. the current status is recorded. When the agenda is exited with
  2309. `q' or `x' and this option is set, the old state is restored. If
  2310. `org-agenda-window-setup' is `other-frame', the value of this
  2311. option will be ignored.."
  2312. :group 'org-agenda-windows
  2313. :type 'boolean)
  2314. (defcustom org-indirect-buffer-display 'other-window
  2315. "How should indirect tree buffers be displayed?
  2316. This applies to indirect buffers created with the commands
  2317. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2318. Valid values are:
  2319. current-window Display in the current window
  2320. other-window Just display in another window.
  2321. dedicated-frame Create one new frame, and re-use it each time.
  2322. new-frame Make a new frame each time. Note that in this case
  2323. previously-made indirect buffers are kept, and you need to
  2324. kill these buffers yourself."
  2325. :group 'org-structure
  2326. :group 'org-agenda-windows
  2327. :type '(choice
  2328. (const :tag "In current window" current-window)
  2329. (const :tag "In current frame, other window" other-window)
  2330. (const :tag "Each time a new frame" new-frame)
  2331. (const :tag "One dedicated frame" dedicated-frame)))
  2332. (defgroup org-agenda-daily/weekly nil
  2333. "Options concerning the daily/weekly agenda."
  2334. :tag "Org Agenda Daily/Weekly"
  2335. :group 'org-agenda)
  2336. (defcustom org-agenda-ndays 7
  2337. "Number of days to include in overview display.
  2338. Should be 1 or 7."
  2339. :group 'org-agenda-daily/weekly
  2340. :type 'number)
  2341. (defcustom org-agenda-start-on-weekday 1
  2342. "Non-nil means, start the overview always on the specified weekday.
  2343. 0 denotes Sunday, 1 denotes Monday etc.
  2344. When nil, always start on the current day."
  2345. :group 'org-agenda-daily/weekly
  2346. :type '(choice (const :tag "Today" nil)
  2347. (number :tag "Weekday No.")))
  2348. (defcustom org-agenda-show-all-dates t
  2349. "Non-nil means, `org-agenda' shows every day in the selected range.
  2350. When nil, only the days which actually have entries are shown."
  2351. :group 'org-agenda-daily/weekly
  2352. :type 'boolean)
  2353. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2354. "Format string for displaying dates in the agenda.
  2355. Used by the daily/weekly agenda and by the timeline. This should be
  2356. a format string understood by `format-time-string', or a function returning
  2357. the formatted date as a string. The function must take a single argument,
  2358. a calendar-style date list like (month day year)."
  2359. :group 'org-agenda-daily/weekly
  2360. :type '(choice
  2361. (string :tag "Format string")
  2362. (function :tag "Function")))
  2363. (defun org-agenda-format-date-aligned (date)
  2364. "Format a date string for display in the daily/weekly agenda, or timeline.
  2365. This function makes sure that dates are aligned for easy reading."
  2366. (format "%-9s %2d %s %4d"
  2367. (calendar-day-name date)
  2368. (extract-calendar-day date)
  2369. (calendar-month-name (extract-calendar-month date))
  2370. (extract-calendar-year date)))
  2371. (defcustom org-agenda-include-diary nil
  2372. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2373. :group 'org-agenda-daily/weekly
  2374. :type 'boolean)
  2375. (defcustom org-agenda-include-all-todo nil
  2376. "Set means weekly/daily agenda will always contain all TODO entries.
  2377. The TODO entries will be listed at the top of the agenda, before
  2378. the entries for specific days."
  2379. :group 'org-agenda-daily/weekly
  2380. :type 'boolean)
  2381. (defcustom org-agenda-repeating-timestamp-show-all t
  2382. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2383. When nil, only one occurence is shown, either today or the
  2384. nearest into the future."
  2385. :group 'org-agenda-daily/weekly
  2386. :type 'boolean)
  2387. (defcustom org-deadline-warning-days 14
  2388. "No. of days before expiration during which a deadline becomes active.
  2389. This variable governs the display in sparse trees and in the agenda.
  2390. When 0 or negative, it means use this number (the absolute value of it)
  2391. even if a deadline has a different individual lead time specified."
  2392. :group 'org-time
  2393. :group 'org-agenda-daily/weekly
  2394. :type 'number)
  2395. (defcustom org-scheduled-past-days 10000
  2396. "No. of days to continue listing scheduled items that are not marked DONE.
  2397. When an item is scheduled on a date, it shows up in the agenda on this
  2398. day and will be listed until it is marked done for the number of days
  2399. given here."
  2400. :group 'org-agenda-daily/weekly
  2401. :type 'number)
  2402. (defgroup org-agenda-time-grid nil
  2403. "Options concerning the time grid in the Org-mode Agenda."
  2404. :tag "Org Agenda Time Grid"
  2405. :group 'org-agenda)
  2406. (defcustom org-agenda-use-time-grid t
  2407. "Non-nil means, show a time grid in the agenda schedule.
  2408. A time grid is a set of lines for specific times (like every two hours between
  2409. 8:00 and 20:00). The items scheduled for a day at specific times are
  2410. sorted in between these lines.
  2411. For details about when the grid will be shown, and what it will look like, see
  2412. the variable `org-agenda-time-grid'."
  2413. :group 'org-agenda-time-grid
  2414. :type 'boolean)
  2415. (defcustom org-agenda-time-grid
  2416. '((daily today require-timed)
  2417. "----------------"
  2418. (800 1000 1200 1400 1600 1800 2000))
  2419. "The settings for time grid for agenda display.
  2420. This is a list of three items. The first item is again a list. It contains
  2421. symbols specifying conditions when the grid should be displayed:
  2422. daily if the agenda shows a single day
  2423. weekly if the agenda shows an entire week
  2424. today show grid on current date, independent of daily/weekly display
  2425. require-timed show grid only if at least one item has a time specification
  2426. The second item is a string which will be places behing the grid time.
  2427. The third item is a list of integers, indicating the times that should have
  2428. a grid line."
  2429. :group 'org-agenda-time-grid
  2430. :type
  2431. '(list
  2432. (set :greedy t :tag "Grid Display Options"
  2433. (const :tag "Show grid in single day agenda display" daily)
  2434. (const :tag "Show grid in weekly agenda display" weekly)
  2435. (const :tag "Always show grid for today" today)
  2436. (const :tag "Show grid only if any timed entries are present"
  2437. require-timed)
  2438. (const :tag "Skip grid times already present in an entry"
  2439. remove-match))
  2440. (string :tag "Grid String")
  2441. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2442. (defgroup org-agenda-sorting nil
  2443. "Options concerning sorting in the Org-mode Agenda."
  2444. :tag "Org Agenda Sorting"
  2445. :group 'org-agenda)
  2446. (defconst org-sorting-choice
  2447. '(choice
  2448. (const time-up) (const time-down)
  2449. (const category-keep) (const category-up) (const category-down)
  2450. (const tag-down) (const tag-up)
  2451. (const priority-up) (const priority-down))
  2452. "Sorting choices.")
  2453. (defcustom org-agenda-sorting-strategy
  2454. '((agenda time-up category-keep priority-down)
  2455. (todo category-keep priority-down)
  2456. (tags category-keep priority-down))
  2457. "Sorting structure for the agenda items of a single day.
  2458. This is a list of symbols which will be used in sequence to determine
  2459. if an entry should be listed before another entry. The following
  2460. symbols are recognized:
  2461. time-up Put entries with time-of-day indications first, early first
  2462. time-down Put entries with time-of-day indications first, late first
  2463. category-keep Keep the default order of categories, corresponding to the
  2464. sequence in `org-agenda-files'.
  2465. category-up Sort alphabetically by category, A-Z.
  2466. category-down Sort alphabetically by category, Z-A.
  2467. tag-up Sort alphabetically by last tag, A-Z.
  2468. tag-down Sort alphabetically by last tag, Z-A.
  2469. priority-up Sort numerically by priority, high priority last.
  2470. priority-down Sort numerically by priority, high priority first.
  2471. The different possibilities will be tried in sequence, and testing stops
  2472. if one comparison returns a \"not-equal\". For example, the default
  2473. '(time-up category-keep priority-down)
  2474. means: Pull out all entries having a specified time of day and sort them,
  2475. in order to make a time schedule for the current day the first thing in the
  2476. agenda listing for the day. Of the entries without a time indication, keep
  2477. the grouped in categories, don't sort the categories, but keep them in
  2478. the sequence given in `org-agenda-files'. Within each category sort by
  2479. priority.
  2480. Leaving out `category-keep' would mean that items will be sorted across
  2481. categories by priority.
  2482. Instead of a single list, this can also be a set of list for specific
  2483. contents, with a context symbol in the car of the list, any of
  2484. `agenda', `todo', `tags' for the corresponding agenda views."
  2485. :group 'org-agenda-sorting
  2486. :type `(choice
  2487. (repeat :tag "General" ,org-sorting-choice)
  2488. (list :tag "Individually"
  2489. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2490. (repeat ,org-sorting-choice))
  2491. (cons (const :tag "Strategy for TODO lists" todo)
  2492. (repeat ,org-sorting-choice))
  2493. (cons (const :tag "Strategy for Tags matches" tags)
  2494. (repeat ,org-sorting-choice)))))
  2495. (defcustom org-sort-agenda-notime-is-late t
  2496. "Non-nil means, items without time are considered late.
  2497. This is only relevant for sorting. When t, items which have no explicit
  2498. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2499. do have a time. When nil, the default time is before 0:00. You can use this
  2500. option to decide if the schedule for today should come before or after timeless
  2501. agenda entries."
  2502. :group 'org-agenda-sorting
  2503. :type 'boolean)
  2504. (defgroup org-agenda-line-format nil
  2505. "Options concerning the entry prefix in the Org-mode agenda display."
  2506. :tag "Org Agenda Line Format"
  2507. :group 'org-agenda)
  2508. (defcustom org-agenda-prefix-format
  2509. '((agenda . " %-12:c%?-12t% s")
  2510. (timeline . " % s")
  2511. (todo . " %-12:c")
  2512. (tags . " %-12:c"))
  2513. "Format specifications for the prefix of items in the agenda views.
  2514. An alist with four entries, for the different agenda types. The keys to the
  2515. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2516. are format strings.
  2517. This format works similar to a printf format, with the following meaning:
  2518. %c the category of the item, \"Diary\" for entries from the diary, or
  2519. as given by the CATEGORY keyword or derived from the file name.
  2520. %T the *last* tag of the item. Last because inherited tags come
  2521. first in the list.
  2522. %t the time-of-day specification if one applies to the entry, in the
  2523. format HH:MM
  2524. %s Scheduling/Deadline information, a short string
  2525. All specifiers work basically like the standard `%s' of printf, but may
  2526. contain two additional characters: A question mark just after the `%' and
  2527. a whitespace/punctuation character just before the final letter.
  2528. If the first character after `%' is a question mark, the entire field
  2529. will only be included if the corresponding value applies to the
  2530. current entry. This is useful for fields which should have fixed
  2531. width when present, but zero width when absent. For example,
  2532. \"%?-12t\" will result in a 12 character time field if a time of the
  2533. day is specified, but will completely disappear in entries which do
  2534. not contain a time.
  2535. If there is punctuation or whitespace character just before the final
  2536. format letter, this character will be appended to the field value if
  2537. the value is not empty. For example, the format \"%-12:c\" leads to
  2538. \"Diary: \" if the category is \"Diary\". If the category were be
  2539. empty, no additional colon would be interted.
  2540. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2541. - Indent the line with two space characters
  2542. - Give the category in a 12 chars wide field, padded with whitespace on
  2543. the right (because of `-'). Append a colon if there is a category
  2544. (because of `:').
  2545. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2546. time, don't put in an empty field, just skip it (because of '?').
  2547. - Finally, put the scheduling information and append a whitespace.
  2548. As another example, if you don't want the time-of-day of entries in
  2549. the prefix, you could use:
  2550. (setq org-agenda-prefix-format \" %-11:c% s\")
  2551. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2552. `org-agenda-remove-tags'."
  2553. :type '(choice
  2554. (string :tag "General format")
  2555. (list :greedy t :tag "View dependent"
  2556. (cons (const agenda) (string :tag "Format"))
  2557. (cons (const timeline) (string :tag "Format"))
  2558. (cons (const todo) (string :tag "Format"))
  2559. (cons (const tags) (string :tag "Format"))))
  2560. :group 'org-agenda-line-format)
  2561. (defvar org-prefix-format-compiled nil
  2562. "The compiled version of the most recently used prefix format.
  2563. See the variable `org-agenda-prefix-format'.")
  2564. (defcustom org-agenda-todo-keyword-format "%-1s"
  2565. "Format for the TODO keyword in agenda lines.
  2566. Set this to something like \"%-12s\" if you want all TODO keywords
  2567. to occupy a fixed space in the agenda display."
  2568. :group 'org-agenda-line-format
  2569. :type 'string)
  2570. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2571. "Text preceeding scheduled items in the agenda view.
  2572. This is a list with two strings. The first applies when the item is
  2573. scheduled on the current day. The second applies when it has been scheduled
  2574. previously, it may contain a %d to capture how many days ago the item was
  2575. scheduled."
  2576. :group 'org-agenda-line-format
  2577. :type '(list
  2578. (string :tag "Scheduled today ")
  2579. (string :tag "Scheduled previously")))
  2580. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2581. "Text preceeding deadline items in the agenda view.
  2582. This is a list with two strings. The first applies when the item has its
  2583. deadline on the current day. The second applies when it is in the past or
  2584. in the future, it may contain %d to capture how many days away the deadline
  2585. is (was)."
  2586. :group 'org-agenda-line-format
  2587. :type '(list
  2588. (string :tag "Deadline today ")
  2589. (string :tag "Deadline relative")))
  2590. (defcustom org-agenda-remove-times-when-in-prefix t
  2591. "Non-nil means, remove duplicate time specifications in agenda items.
  2592. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2593. time-of-day specification in a headline or diary entry is extracted and
  2594. placed into the prefix. If this option is non-nil, the original specification
  2595. \(a timestamp or -range, or just a plain time(range) specification like
  2596. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2597. cluttered.
  2598. The option can be t or nil. It may also be the symbol `beg', indicating
  2599. that the time should only be removed what it is located at the beginning of
  2600. the headline/diary entry."
  2601. :group 'org-agenda-line-format
  2602. :type '(choice
  2603. (const :tag "Always" t)
  2604. (const :tag "Never" nil)
  2605. (const :tag "When at beginning of entry" beg)))
  2606. (defcustom org-agenda-default-appointment-duration nil
  2607. "Default duration for appointments that only have a starting time.
  2608. When nil, no duration is specified in such cases.
  2609. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2610. :group 'org-agenda-line-format
  2611. :type '(choice
  2612. (integer :tag "Minutes")
  2613. (const :tag "No default duration")))
  2614. (defcustom org-agenda-remove-tags nil
  2615. "Non-nil means, remove the tags from the headline copy in the agenda.
  2616. When this is the symbol `prefix', only remove tags when
  2617. `org-agenda-prefix-format' contains a `%T' specifier."
  2618. :group 'org-agenda-line-format
  2619. :type '(choice
  2620. (const :tag "Always" t)
  2621. (const :tag "Never" nil)
  2622. (const :tag "When prefix format contains %T" prefix)))
  2623. (if (fboundp 'defvaralias)
  2624. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2625. 'org-agenda-remove-tags))
  2626. (defcustom org-agenda-tags-column -80
  2627. "Shift tags in agenda items to this column.
  2628. If this number is positive, it specifies the column. If it is negative,
  2629. it means that the tags should be flushright to that column. For example,
  2630. -80 works well for a normal 80 character screen."
  2631. :group 'org-agenda-line-format
  2632. :type 'integer)
  2633. (if (fboundp 'defvaralias)
  2634. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2635. (defcustom org-agenda-fontify-priorities t
  2636. "Non-nil means, highlight low and high priorities in agenda.
  2637. When t, the highest priority entries are bold, lowest priority italic.
  2638. This may also be an association list of priority faces. The face may be
  2639. a names face, or a list like `(:background \"Red\")'."
  2640. :group 'org-agenda-line-format
  2641. :type '(choice
  2642. (const :tag "Never" nil)
  2643. (const :tag "Defaults" t)
  2644. (repeat :tag "Specify"
  2645. (list (character :tag "Priority" :value ?A)
  2646. (sexp :tag "face")))))
  2647. (defgroup org-latex nil
  2648. "Options for embedding LaTeX code into Org-mode"
  2649. :tag "Org LaTeX"
  2650. :group 'org)
  2651. (defcustom org-format-latex-options
  2652. '(:foreground default :background default :scale 1.0
  2653. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2654. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2655. "Options for creating images from LaTeX fragments.
  2656. This is a property list with the following properties:
  2657. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2658. `default' means use the forground of the default face.
  2659. :background the background color, or \"Transparent\".
  2660. `default' means use the background of the default face.
  2661. :scale a scaling factor for the size of the images
  2662. :html-foreground, :html-background, :html-scale
  2663. The same numbers for HTML export.
  2664. :matchers a list indicating which matchers should be used to
  2665. find LaTeX fragments. Valid members of this list are:
  2666. \"begin\" find environments
  2667. \"$\" find math expressions surrounded by $...$
  2668. \"$$\" find math expressions surrounded by $$....$$
  2669. \"\\(\" find math expressions surrounded by \\(...\\)
  2670. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2671. :group 'org-latex
  2672. :type 'plist)
  2673. (defcustom org-format-latex-header "\\documentclass{article}
  2674. \\usepackage{fullpage} % do not remove
  2675. \\usepackage{amssymb}
  2676. \\usepackage[usenames]{color}
  2677. \\usepackage{amsmath}
  2678. \\usepackage{latexsym}
  2679. \\usepackage[mathscr]{eucal}
  2680. \\pagestyle{empty} % do not remove"
  2681. "The document header used for processing LaTeX fragments."
  2682. :group 'org-latex
  2683. :type 'string)
  2684. (defgroup org-export nil
  2685. "Options for exporting org-listings."
  2686. :tag "Org Export"
  2687. :group 'org)
  2688. (defgroup org-export-general nil
  2689. "General options for exporting Org-mode files."
  2690. :tag "Org Export General"
  2691. :group 'org-export)
  2692. ;; FIXME
  2693. (defvar org-export-publishing-directory nil)
  2694. (defcustom org-export-with-special-strings t
  2695. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2696. When this option is turned on, these strings will be exported as:
  2697. Org HTML LaTeX
  2698. -----+----------+--------
  2699. \\- &shy; \\-
  2700. -- &ndash; --
  2701. --- &mdash; ---
  2702. ... &hellip; \ldots
  2703. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2704. :group 'org-export-translation
  2705. :type 'boolean)
  2706. (defcustom org-export-language-setup
  2707. '(("en" "Author" "Date" "Table of Contents")
  2708. ("cs" "Autor" "Datum" "Obsah")
  2709. ("da" "Ophavsmand" "Dato" "Indhold")
  2710. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2711. ("es" "Autor" "Fecha" "\xcdndice")
  2712. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2713. ("it" "Autore" "Data" "Indice")
  2714. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2715. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2716. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2717. "Terms used in export text, translated to different languages.
  2718. Use the variable `org-export-default-language' to set the language,
  2719. or use the +OPTION lines for a per-file setting."
  2720. :group 'org-export-general
  2721. :type '(repeat
  2722. (list
  2723. (string :tag "HTML language tag")
  2724. (string :tag "Author")
  2725. (string :tag "Date")
  2726. (string :tag "Table of Contents"))))
  2727. (defcustom org-export-default-language "en"
  2728. "The default language of HTML export, as a string.
  2729. This should have an association in `org-export-language-setup'."
  2730. :group 'org-export-general
  2731. :type 'string)
  2732. (defcustom org-export-skip-text-before-1st-heading t
  2733. "Non-nil means, skip all text before the first headline when exporting.
  2734. When nil, that text is exported as well."
  2735. :group 'org-export-general
  2736. :type 'boolean)
  2737. (defcustom org-export-headline-levels 3
  2738. "The last level which is still exported as a headline.
  2739. Inferior levels will produce itemize lists when exported.
  2740. Note that a numeric prefix argument to an exporter function overrides
  2741. this setting.
  2742. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2743. :group 'org-export-general
  2744. :type 'number)
  2745. (defcustom org-export-with-section-numbers t
  2746. "Non-nil means, add section numbers to headlines when exporting.
  2747. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2748. :group 'org-export-general
  2749. :type 'boolean)
  2750. (defcustom org-export-with-toc t
  2751. "Non-nil means, create a table of contents in exported files.
  2752. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2753. When an integer, include levels up to N in the toc, this may then be
  2754. different from `org-export-headline-levels', but it will not be allowed
  2755. to be larger than the number of headline levels.
  2756. When nil, no table of contents is made.
  2757. Headlines which contain any TODO items will be marked with \"(*)\" in
  2758. ASCII export, and with red color in HTML output, if the option
  2759. `org-export-mark-todo-in-toc' is set.
  2760. In HTML output, the TOC will be clickable.
  2761. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2762. or \"toc:3\"."
  2763. :group 'org-export-general
  2764. :type '(choice
  2765. (const :tag "No Table of Contents" nil)
  2766. (const :tag "Full Table of Contents" t)
  2767. (integer :tag "TOC to level")))
  2768. (defcustom org-export-mark-todo-in-toc nil
  2769. "Non-nil means, mark TOC lines that contain any open TODO items."
  2770. :group 'org-export-general
  2771. :type 'boolean)
  2772. (defcustom org-export-preserve-breaks nil
  2773. "Non-nil means, preserve all line breaks when exporting.
  2774. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2775. export, line breaks will always be preserved, regardless of this variable.
  2776. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2777. :group 'org-export-general
  2778. :type 'boolean)
  2779. (defcustom org-export-with-archived-trees 'headline
  2780. "Whether subtrees with the ARCHIVE tag should be exported.
  2781. This can have three different values
  2782. nil Do not export, pretend this tree is not present
  2783. t Do export the entire tree
  2784. headline Only export the headline, but skip the tree below it."
  2785. :group 'org-export-general
  2786. :group 'org-archive
  2787. :type '(choice
  2788. (const :tag "not at all" nil)
  2789. (const :tag "headline only" 'headline)
  2790. (const :tag "entirely" t)))
  2791. (defcustom org-export-author-info t
  2792. "Non-nil means, insert author name and email into the exported file.
  2793. This option can also be set with the +OPTIONS line,
  2794. e.g. \"author-info:nil\"."
  2795. :group 'org-export-general
  2796. :type 'boolean)
  2797. (defcustom org-export-time-stamp-file t
  2798. "Non-nil means, insert a time stamp into the exported file.
  2799. The time stamp shows when the file was created.
  2800. This option can also be set with the +OPTIONS line,
  2801. e.g. \"timestamp:nil\"."
  2802. :group 'org-export-general
  2803. :type 'boolean)
  2804. (defcustom org-export-with-timestamps t
  2805. "If nil, do not export time stamps and associated keywords."
  2806. :group 'org-export-general
  2807. :type 'boolean)
  2808. (defcustom org-export-remove-timestamps-from-toc t
  2809. "If nil, remove timestamps from the table of contents entries."
  2810. :group 'org-export-general
  2811. :type 'boolean)
  2812. (defcustom org-export-with-tags 'not-in-toc
  2813. "If nil, do not export tags, just remove them from headlines.
  2814. If this is the symbol `not-in-toc', tags will be removed from table of
  2815. contents entries, but still be shown in the headlines of the document.
  2816. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2817. :group 'org-export-general
  2818. :type '(choice
  2819. (const :tag "Off" nil)
  2820. (const :tag "Not in TOC" not-in-toc)
  2821. (const :tag "On" t)))
  2822. (defcustom org-export-with-drawers nil
  2823. "Non-nil means, export with drawers like the property drawer.
  2824. When t, all drawers are exported. This may also be a list of
  2825. drawer names to export."
  2826. :group 'org-export-general
  2827. :type '(choice
  2828. (const :tag "All drawers" t)
  2829. (const :tag "None" nil)
  2830. (repeat :tag "Selected drawers"
  2831. (string :tag "Drawer name"))))
  2832. (defgroup org-export-translation nil
  2833. "Options for translating special ascii sequences for the export backends."
  2834. :tag "Org Export Translation"
  2835. :group 'org-export)
  2836. (defcustom org-export-with-emphasize t
  2837. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2838. If the export target supports emphasizing text, the word will be
  2839. typeset in bold, italic, or underlined, respectively. Works only for
  2840. single words, but you can say: I *really* *mean* *this*.
  2841. Not all export backends support this.
  2842. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2843. :group 'org-export-translation
  2844. :type 'boolean)
  2845. (defcustom org-export-with-footnotes t
  2846. "If nil, export [1] as a footnote marker.
  2847. Lines starting with [1] will be formatted as footnotes.
  2848. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2849. :group 'org-export-translation
  2850. :type 'boolean)
  2851. (defcustom org-export-with-sub-superscripts t
  2852. "Non-nil means, interpret \"_\" and \"^\" for export.
  2853. When this option is turned on, you can use TeX-like syntax for sub- and
  2854. superscripts. Several characters after \"_\" or \"^\" will be
  2855. considered as a single item - so grouping with {} is normally not
  2856. needed. For example, the following things will be parsed as single
  2857. sub- or superscripts.
  2858. 10^24 or 10^tau several digits will be considered 1 item.
  2859. 10^-12 or 10^-tau a leading sign with digits or a word
  2860. x^2-y^3 will be read as x^2 - y^3, because items are
  2861. terminated by almost any nonword/nondigit char.
  2862. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2863. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2864. sub/superscript. If you set this variable to the symbol `{}',
  2865. the braces are *required* in order to trigger interpretations as
  2866. sub/superscript. This can be helpful in documents that need \"_\"
  2867. frequently in plain text.
  2868. Not all export backends support this, but HTML does.
  2869. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2870. :group 'org-export-translation
  2871. :type '(choice
  2872. (const :tag "Always interpret" t)
  2873. (const :tag "Only with braces" {})
  2874. (const :tag "Never interpret" nil)))
  2875. (defcustom org-export-with-special-strings t
  2876. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2877. When this option is turned on, these strings will be exported as:
  2878. \\- : &shy;
  2879. -- : &ndash;
  2880. --- : &mdash;
  2881. Not all export backends support this, but HTML does.
  2882. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2883. :group 'org-export-translation
  2884. :type 'boolean)
  2885. (defcustom org-export-with-TeX-macros t
  2886. "Non-nil means, interpret simple TeX-like macros when exporting.
  2887. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2888. No only real TeX macros will work here, but the standard HTML entities
  2889. for math can be used as macro names as well. For a list of supported
  2890. names in HTML export, see the constant `org-html-entities'.
  2891. Not all export backends support this.
  2892. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2893. :group 'org-export-translation
  2894. :group 'org-export-latex
  2895. :type 'boolean)
  2896. (defcustom org-export-with-LaTeX-fragments nil
  2897. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2898. When set, the exporter will find LaTeX environments if the \\begin line is
  2899. the first non-white thing on a line. It will also find the math delimiters
  2900. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2901. display math.
  2902. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2903. :group 'org-export-translation
  2904. :group 'org-export-latex
  2905. :type 'boolean)
  2906. (defcustom org-export-with-fixed-width t
  2907. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2908. This can be used to have pre-formatted text, fragments of code etc. For
  2909. example:
  2910. : ;; Some Lisp examples
  2911. : (while (defc cnt)
  2912. : (ding))
  2913. will be looking just like this in also HTML. See also the QUOTE keyword.
  2914. Not all export backends support this.
  2915. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2916. :group 'org-export-translation
  2917. :type 'boolean)
  2918. (defcustom org-match-sexp-depth 3
  2919. "Number of stacked braces for sub/superscript matching.
  2920. This has to be set before loading org.el to be effective."
  2921. :group 'org-export-translation
  2922. :type 'integer)
  2923. (defgroup org-export-tables nil
  2924. "Options for exporting tables in Org-mode."
  2925. :tag "Org Export Tables"
  2926. :group 'org-export)
  2927. (defcustom org-export-with-tables t
  2928. "If non-nil, lines starting with \"|\" define a table.
  2929. For example:
  2930. | Name | Address | Birthday |
  2931. |-------------+----------+-----------|
  2932. | Arthur Dent | England | 29.2.2100 |
  2933. Not all export backends support this.
  2934. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2935. :group 'org-export-tables
  2936. :type 'boolean)
  2937. (defcustom org-export-highlight-first-table-line t
  2938. "Non-nil means, highlight the first table line.
  2939. In HTML export, this means use <th> instead of <td>.
  2940. In tables created with table.el, this applies to the first table line.
  2941. In Org-mode tables, all lines before the first horizontal separator
  2942. line will be formatted with <th> tags."
  2943. :group 'org-export-tables
  2944. :type 'boolean)
  2945. (defcustom org-export-table-remove-special-lines t
  2946. "Remove special lines and marking characters in calculating tables.
  2947. This removes the special marking character column from tables that are set
  2948. up for spreadsheet calculations. It also removes the entire lines
  2949. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2950. the values of constants may be useful to have."
  2951. :group 'org-export-tables
  2952. :type 'boolean)
  2953. (defcustom org-export-prefer-native-exporter-for-tables nil
  2954. "Non-nil means, always export tables created with table.el natively.
  2955. Natively means, use the HTML code generator in table.el.
  2956. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2957. the table does not use row- or column-spanning). This has the
  2958. advantage, that the automatic HTML conversions for math symbols and
  2959. sub/superscripts can be applied. Org-mode's HTML generator is also
  2960. much faster."
  2961. :group 'org-export-tables
  2962. :type 'boolean)
  2963. (defgroup org-export-ascii nil
  2964. "Options specific for ASCII export of Org-mode files."
  2965. :tag "Org Export ASCII"
  2966. :group 'org-export)
  2967. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2968. "Characters for underlining headings in ASCII export.
  2969. In the given sequence, these characters will be used for level 1, 2, ..."
  2970. :group 'org-export-ascii
  2971. :type '(repeat character))
  2972. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2973. "Bullet characters for headlines converted to lists in ASCII export.
  2974. The first character is used for the first lest level generated in this
  2975. way, and so on. If there are more levels than characters given here,
  2976. the list will be repeated.
  2977. Note that plain lists will keep the same bullets as the have in the
  2978. Org-mode file."
  2979. :group 'org-export-ascii
  2980. :type '(repeat character))
  2981. (defgroup org-export-xml nil
  2982. "Options specific for XML export of Org-mode files."
  2983. :tag "Org Export XML"
  2984. :group 'org-export)
  2985. (defgroup org-export-html nil
  2986. "Options specific for HTML export of Org-mode files."
  2987. :tag "Org Export HTML"
  2988. :group 'org-export)
  2989. (defcustom org-export-html-coding-system nil
  2990. ""
  2991. :group 'org-export-html
  2992. :type 'coding-system)
  2993. (defcustom org-export-html-extension "html"
  2994. "The extension for exported HTML files."
  2995. :group 'org-export-html
  2996. :type 'string)
  2997. (defcustom org-export-html-style
  2998. "<style type=\"text/css\">
  2999. html {
  3000. font-family: Times, serif;
  3001. font-size: 12pt;
  3002. }
  3003. .title { text-align: center; }
  3004. .todo { color: red; }
  3005. .done { color: green; }
  3006. .timestamp { color: grey }
  3007. .timestamp-kwd { color: CadetBlue }
  3008. .tag { background-color:lightblue; font-weight:normal }
  3009. .target { background-color: lavender; }
  3010. pre {
  3011. border: 1pt solid #AEBDCC;
  3012. background-color: #F3F5F7;
  3013. padding: 5pt;
  3014. font-family: courier, monospace;
  3015. }
  3016. table { border-collapse: collapse; }
  3017. td, th {
  3018. vertical-align: top;
  3019. <!--border: 1pt solid #ADB9CC;-->
  3020. }
  3021. </style>"
  3022. "The default style specification for exported HTML files.
  3023. Since there are different ways of setting style information, this variable
  3024. needs to contain the full HTML structure to provide a style, including the
  3025. surrounding HTML tags. The style specifications should include definitions
  3026. for new classes todo, done, title, and deadline. For example, legal values
  3027. would be:
  3028. <style type=\"text/css\">
  3029. p { font-weight: normal; color: gray; }
  3030. h1 { color: black; }
  3031. .title { text-align: center; }
  3032. .todo, .deadline { color: red; }
  3033. .done { color: green; }
  3034. </style>
  3035. or, if you want to keep the style in a file,
  3036. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3037. As the value of this option simply gets inserted into the HTML <head> header,
  3038. you can \"misuse\" it to add arbitrary text to the header."
  3039. :group 'org-export-html
  3040. :type 'string)
  3041. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3042. "Format for typesetting the document title in HTML export."
  3043. :group 'org-export-html
  3044. :type 'string)
  3045. (defcustom org-export-html-toplevel-hlevel 2
  3046. "The <H> level for level 1 headings in HTML export."
  3047. :group 'org-export-html
  3048. :type 'string)
  3049. (defcustom org-export-html-link-org-files-as-html t
  3050. "Non-nil means, make file links to `file.org' point to `file.html'.
  3051. When org-mode is exporting an org-mode file to HTML, links to
  3052. non-html files are directly put into a href tag in HTML.
  3053. However, links to other Org-mode files (recognized by the
  3054. extension `.org.) should become links to the corresponding html
  3055. file, assuming that the linked org-mode file will also be
  3056. converted to HTML.
  3057. When nil, the links still point to the plain `.org' file."
  3058. :group 'org-export-html
  3059. :type 'boolean)
  3060. (defcustom org-export-html-inline-images 'maybe
  3061. "Non-nil means, inline images into exported HTML pages.
  3062. This is done using an <img> tag. When nil, an anchor with href is used to
  3063. link to the image. If this option is `maybe', then images in links with
  3064. an empty description will be inlined, while images with a description will
  3065. be linked only."
  3066. :group 'org-export-html
  3067. :type '(choice (const :tag "Never" nil)
  3068. (const :tag "Always" t)
  3069. (const :tag "When there is no description" maybe)))
  3070. ;; FIXME: rename
  3071. (defcustom org-export-html-expand t
  3072. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3073. When nil, these tags will be exported as plain text and therefore
  3074. not be interpreted by a browser.
  3075. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3076. :group 'org-export-html
  3077. :type 'boolean)
  3078. (defcustom org-export-html-table-tag
  3079. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3080. "The HTML tag that is used to start a table.
  3081. This must be a <table> tag, but you may change the options like
  3082. borders and spacing."
  3083. :group 'org-export-html
  3084. :type 'string)
  3085. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3086. "The opening tag for table header fields.
  3087. This is customizable so that alignment options can be specified."
  3088. :group 'org-export-tables
  3089. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3090. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3091. "The opening tag for table data fields.
  3092. This is customizable so that alignment options can be specified."
  3093. :group 'org-export-tables
  3094. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3095. (defcustom org-export-html-with-timestamp nil
  3096. "If non-nil, write `org-export-html-html-helper-timestamp'
  3097. into the exported HTML text. Otherwise, the buffer will just be saved
  3098. to a file."
  3099. :group 'org-export-html
  3100. :type 'boolean)
  3101. (defcustom org-export-html-html-helper-timestamp
  3102. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3103. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3104. :group 'org-export-html
  3105. :type 'string)
  3106. (defgroup org-export-icalendar nil
  3107. "Options specific for iCalendar export of Org-mode files."
  3108. :tag "Org Export iCalendar"
  3109. :group 'org-export)
  3110. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3111. "The file name for the iCalendar file covering all agenda files.
  3112. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3113. The file name should be absolute, the file will be overwritten without warning."
  3114. :group 'org-export-icalendar
  3115. :type 'file)
  3116. (defcustom org-icalendar-include-todo nil
  3117. "Non-nil means, export to iCalendar files should also cover TODO items."
  3118. :group 'org-export-icalendar
  3119. :type '(choice
  3120. (const :tag "None" nil)
  3121. (const :tag "Unfinished" t)
  3122. (const :tag "All" all)))
  3123. (defcustom org-icalendar-include-sexps t
  3124. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3125. These are entries like in the diary, but directly in an Org-mode file."
  3126. :group 'org-export-icalendar
  3127. :type 'boolean)
  3128. (defcustom org-icalendar-include-body 100
  3129. "Amount of text below headline to be included in iCalendar export.
  3130. This is a number of characters that should maximally be included.
  3131. Properties, scheduling and clocking lines will always be removed.
  3132. The text will be inserted into the DESCRIPTION field."
  3133. :group 'org-export-icalendar
  3134. :type '(choice
  3135. (const :tag "Nothing" nil)
  3136. (const :tag "Everything" t)
  3137. (integer :tag "Max characters")))
  3138. (defcustom org-icalendar-combined-name "OrgMode"
  3139. "Calendar name for the combined iCalendar representing all agenda files."
  3140. :group 'org-export-icalendar
  3141. :type 'string)
  3142. (defgroup org-font-lock nil
  3143. "Font-lock settings for highlighting in Org-mode."
  3144. :tag "Org Font Lock"
  3145. :group 'org)
  3146. (defcustom org-level-color-stars-only nil
  3147. "Non-nil means fontify only the stars in each headline.
  3148. When nil, the entire headline is fontified.
  3149. Changing it requires restart of `font-lock-mode' to become effective
  3150. also in regions already fontified."
  3151. :group 'org-font-lock
  3152. :type 'boolean)
  3153. (defcustom org-hide-leading-stars nil
  3154. "Non-nil means, hide the first N-1 stars in a headline.
  3155. This works by using the face `org-hide' for these stars. This
  3156. face is white for a light background, and black for a dark
  3157. background. You may have to customize the face `org-hide' to
  3158. make this work.
  3159. Changing it requires restart of `font-lock-mode' to become effective
  3160. also in regions already fontified.
  3161. You may also set this on a per-file basis by adding one of the following
  3162. lines to the buffer:
  3163. #+STARTUP: hidestars
  3164. #+STARTUP: showstars"
  3165. :group 'org-font-lock
  3166. :type 'boolean)
  3167. (defcustom org-fontify-done-headline nil
  3168. "Non-nil means, change the face of a headline if it is marked DONE.
  3169. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3170. When this is non-nil, the headline after the keyword is set to the
  3171. `org-headline-done' as an additional indication."
  3172. :group 'org-font-lock
  3173. :type 'boolean)
  3174. (defcustom org-fontify-emphasized-text t
  3175. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3176. Changing this variable requires a restart of Emacs to take effect."
  3177. :group 'org-font-lock
  3178. :type 'boolean)
  3179. (defcustom org-highlight-latex-fragments-and-specials nil
  3180. "Non-nil means, fontify what is treated specially by the exporters."
  3181. :group 'org-font-lock
  3182. :type 'boolean)
  3183. (defcustom org-hide-emphasis-markers nil
  3184. "Non-nil mean font-lock should hide the emphasis marker characters."
  3185. :group 'org-font-lock
  3186. :type 'boolean)
  3187. (defvar org-emph-re nil
  3188. "Regular expression for matching emphasis.")
  3189. (defvar org-verbatim-re nil
  3190. "Regular expression for matching verbatim text.")
  3191. (defvar org-emphasis-regexp-components) ; defined just below
  3192. (defvar org-emphasis-alist) ; defined just below
  3193. (defun org-set-emph-re (var val)
  3194. "Set variable and compute the emphasis regular expression."
  3195. (set var val)
  3196. (when (and (boundp 'org-emphasis-alist)
  3197. (boundp 'org-emphasis-regexp-components)
  3198. org-emphasis-alist org-emphasis-regexp-components)
  3199. (let* ((e org-emphasis-regexp-components)
  3200. (pre (car e))
  3201. (post (nth 1 e))
  3202. (border (nth 2 e))
  3203. (body (nth 3 e))
  3204. (nl (nth 4 e))
  3205. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3206. (body1 (concat body "*?"))
  3207. (markers (mapconcat 'car org-emphasis-alist ""))
  3208. (vmarkers (mapconcat
  3209. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3210. org-emphasis-alist "")))
  3211. ;; make sure special characters appear at the right position in the class
  3212. (if (string-match "\\^" markers)
  3213. (setq markers (concat (replace-match "" t t markers) "^")))
  3214. (if (string-match "-" markers)
  3215. (setq markers (concat (replace-match "" t t markers) "-")))
  3216. (if (string-match "\\^" vmarkers)
  3217. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3218. (if (string-match "-" vmarkers)
  3219. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3220. (if (> nl 0)
  3221. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3222. (int-to-string nl) "\\}")))
  3223. ;; Make the regexp
  3224. (setq org-emph-re
  3225. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3226. "\\("
  3227. "\\([" markers "]\\)"
  3228. "\\("
  3229. "[^" border "]\\|"
  3230. "[^" border (if (and nil stacked) markers) "]"
  3231. body1
  3232. "[^" border (if (and nil stacked) markers) "]"
  3233. "\\)"
  3234. "\\3\\)"
  3235. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3236. (setq org-verbatim-re
  3237. (concat "\\([" pre "]\\|^\\)"
  3238. "\\("
  3239. "\\([" vmarkers "]\\)"
  3240. "\\("
  3241. "[^" border "]\\|"
  3242. "[^" border "]"
  3243. body1
  3244. "[^" border "]"
  3245. "\\)"
  3246. "\\3\\)"
  3247. "\\([" post "]\\|$\\)")))))
  3248. (defcustom org-emphasis-regexp-components
  3249. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3250. "Components used to build the regular expression for emphasis.
  3251. This is a list with 6 entries. Terminology: In an emphasis string
  3252. like \" *strong word* \", we call the initial space PREMATCH, the final
  3253. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3254. and \"trong wor\" is the body. The different components in this variable
  3255. specify what is allowed/forbidden in each part:
  3256. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3257. post Chars allowed as postmatch. End of line will be allowed too.
  3258. border The chars *forbidden* as border characters.
  3259. body-regexp A regexp like \".\" to match a body character. Don't use
  3260. non-shy groups here, and don't allow newline here.
  3261. newline The maximum number of newlines allowed in an emphasis exp.
  3262. Use customize to modify this, or restart Emacs after changing it."
  3263. :group 'org-font-lock
  3264. :set 'org-set-emph-re
  3265. :type '(list
  3266. (sexp :tag "Allowed chars in pre ")
  3267. (sexp :tag "Allowed chars in post ")
  3268. (sexp :tag "Forbidden chars in border ")
  3269. (sexp :tag "Regexp for body ")
  3270. (integer :tag "number of newlines allowed")
  3271. (option (boolean :tag "Stacking (DISABLED) "))))
  3272. (defcustom org-emphasis-alist
  3273. '(("*" bold "<b>" "</b>")
  3274. ("/" italic "<i>" "</i>")
  3275. ("_" underline "<u>" "</u>")
  3276. ("=" org-code "<code>" "</code>" verbatim)
  3277. ("~" org-verbatim "" "" verbatim)
  3278. ("+" (:strike-through t) "<del>" "</del>")
  3279. )
  3280. "Special syntax for emphasized text.
  3281. Text starting and ending with a special character will be emphasized, for
  3282. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3283. characters, the face to be used by font-lock for highlighting in Org-mode
  3284. Emacs buffers, and the HTML tags to be used for this.
  3285. Use customize to modify this, or restart Emacs after changing it."
  3286. :group 'org-font-lock
  3287. :set 'org-set-emph-re
  3288. :type '(repeat
  3289. (list
  3290. (string :tag "Marker character")
  3291. (choice
  3292. (face :tag "Font-lock-face")
  3293. (plist :tag "Face property list"))
  3294. (string :tag "HTML start tag")
  3295. (string :tag "HTML end tag")
  3296. (option (const verbatim)))))
  3297. ;;; The faces
  3298. (defgroup org-faces nil
  3299. "Faces in Org-mode."
  3300. :tag "Org Faces"
  3301. :group 'org-font-lock)
  3302. (defun org-compatible-face (inherits specs)
  3303. "Make a compatible face specification.
  3304. If INHERITS is an existing face and if the Emacs version supports it,
  3305. just inherit the face. If not, use SPECS to define the face.
  3306. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3307. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3308. to the top of the list. The `min-colors' attribute will be removed from
  3309. any other entries, and any resulting duplicates will be removed entirely."
  3310. (cond
  3311. ((and inherits (facep inherits)
  3312. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3313. ;; In Emacs 23, we use inheritance where possible.
  3314. ;; We only do this in Emacs 23, because only there the outline
  3315. ;; faces have been changed to the original org-mode-level-faces.
  3316. (list (list t :inherit inherits)))
  3317. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3318. ;; These do not understand the `min-colors' attribute.
  3319. (let (r e a)
  3320. (while (setq e (pop specs))
  3321. (cond
  3322. ((memq (car e) '(t default)) (push e r))
  3323. ((setq a (member '(min-colors 8) (car e)))
  3324. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3325. (cdr e)))))
  3326. ((setq a (assq 'min-colors (car e)))
  3327. (setq e (cons (delq a (car e)) (cdr e)))
  3328. (or (assoc (car e) r) (push e r)))
  3329. (t (or (assoc (car e) r) (push e r)))))
  3330. (nreverse r)))
  3331. (t specs)))
  3332. (put 'org-compatible-face 'lisp-indent-function 1)
  3333. (defface org-hide
  3334. '((((background light)) (:foreground "white"))
  3335. (((background dark)) (:foreground "black")))
  3336. "Face used to hide leading stars in headlines.
  3337. The forground color of this face should be equal to the background
  3338. color of the frame."
  3339. :group 'org-faces)
  3340. (defface org-level-1 ;; font-lock-function-name-face
  3341. (org-compatible-face 'outline-1
  3342. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3343. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3344. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3345. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3346. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3347. (t (:bold t))))
  3348. "Face used for level 1 headlines."
  3349. :group 'org-faces)
  3350. (defface org-level-2 ;; font-lock-variable-name-face
  3351. (org-compatible-face 'outline-2
  3352. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3353. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3354. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3355. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3356. (t (:bold t))))
  3357. "Face used for level 2 headlines."
  3358. :group 'org-faces)
  3359. (defface org-level-3 ;; font-lock-keyword-face
  3360. (org-compatible-face 'outline-3
  3361. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3362. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3363. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3364. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3365. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3366. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3367. (t (:bold t))))
  3368. "Face used for level 3 headlines."
  3369. :group 'org-faces)
  3370. (defface org-level-4 ;; font-lock-comment-face
  3371. (org-compatible-face 'outline-4
  3372. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3373. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3374. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3375. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3376. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3377. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3378. (t (:bold t))))
  3379. "Face used for level 4 headlines."
  3380. :group 'org-faces)
  3381. (defface org-level-5 ;; font-lock-type-face
  3382. (org-compatible-face 'outline-5
  3383. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3384. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3385. (((class color) (min-colors 8)) (:foreground "green"))))
  3386. "Face used for level 5 headlines."
  3387. :group 'org-faces)
  3388. (defface org-level-6 ;; font-lock-constant-face
  3389. (org-compatible-face 'outline-6
  3390. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3391. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3392. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3393. "Face used for level 6 headlines."
  3394. :group 'org-faces)
  3395. (defface org-level-7 ;; font-lock-builtin-face
  3396. (org-compatible-face 'outline-7
  3397. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3398. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3399. (((class color) (min-colors 8)) (:foreground "blue"))))
  3400. "Face used for level 7 headlines."
  3401. :group 'org-faces)
  3402. (defface org-level-8 ;; font-lock-string-face
  3403. (org-compatible-face 'outline-8
  3404. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3405. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3406. (((class color) (min-colors 8)) (:foreground "green"))))
  3407. "Face used for level 8 headlines."
  3408. :group 'org-faces)
  3409. (defface org-special-keyword ;; font-lock-string-face
  3410. (org-compatible-face nil
  3411. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3412. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3413. (t (:italic t))))
  3414. "Face used for special keywords."
  3415. :group 'org-faces)
  3416. (defface org-drawer ;; font-lock-function-name-face
  3417. (org-compatible-face nil
  3418. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3419. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3420. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3421. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3422. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3423. (t (:bold t))))
  3424. "Face used for drawers."
  3425. :group 'org-faces)
  3426. (defface org-property-value nil
  3427. "Face used for the value of a property."
  3428. :group 'org-faces)
  3429. (defface org-column
  3430. (org-compatible-face nil
  3431. '((((class color) (min-colors 16) (background light))
  3432. (:background "grey90"))
  3433. (((class color) (min-colors 16) (background dark))
  3434. (:background "grey30"))
  3435. (((class color) (min-colors 8))
  3436. (:background "cyan" :foreground "black"))
  3437. (t (:inverse-video t))))
  3438. "Face for column display of entry properties."
  3439. :group 'org-faces)
  3440. (when (fboundp 'set-face-attribute)
  3441. ;; Make sure that a fixed-width face is used when we have a column table.
  3442. (set-face-attribute 'org-column nil
  3443. :height (face-attribute 'default :height)
  3444. :family (face-attribute 'default :family)))
  3445. (defface org-warning
  3446. (org-compatible-face 'font-lock-warning-face
  3447. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3448. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3449. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3450. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3451. (t (:bold t))))
  3452. "Face for deadlines and TODO keywords."
  3453. :group 'org-faces)
  3454. (defface org-archived ; similar to shadow
  3455. (org-compatible-face 'shadow
  3456. '((((class color grayscale) (min-colors 88) (background light))
  3457. (:foreground "grey50"))
  3458. (((class color grayscale) (min-colors 88) (background dark))
  3459. (:foreground "grey70"))
  3460. (((class color) (min-colors 8) (background light))
  3461. (:foreground "green"))
  3462. (((class color) (min-colors 8) (background dark))
  3463. (:foreground "yellow"))))
  3464. "Face for headline with the ARCHIVE tag."
  3465. :group 'org-faces)
  3466. (defface org-link
  3467. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3468. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3469. (t (:underline t)))
  3470. "Face for links."
  3471. :group 'org-faces)
  3472. (defface org-ellipsis
  3473. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3474. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3475. (t (:strike-through t)))
  3476. "Face for the ellipsis in folded text."
  3477. :group 'org-faces)
  3478. (defface org-target
  3479. '((((class color) (background light)) (:underline t))
  3480. (((class color) (background dark)) (:underline t))
  3481. (t (:underline t)))
  3482. "Face for links."
  3483. :group 'org-faces)
  3484. (defface org-date
  3485. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3486. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3487. (t (:underline t)))
  3488. "Face for links."
  3489. :group 'org-faces)
  3490. (defface org-sexp-date
  3491. '((((class color) (background light)) (:foreground "Purple"))
  3492. (((class color) (background dark)) (:foreground "Cyan"))
  3493. (t (:underline t)))
  3494. "Face for links."
  3495. :group 'org-faces)
  3496. (defface org-tag
  3497. '((t (:bold t)))
  3498. "Face for tags."
  3499. :group 'org-faces)
  3500. (defface org-todo ; font-lock-warning-face
  3501. (org-compatible-face nil
  3502. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3503. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3504. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3505. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3506. (t (:inverse-video t :bold t))))
  3507. "Face for TODO keywords."
  3508. :group 'org-faces)
  3509. (defface org-done ;; font-lock-type-face
  3510. (org-compatible-face nil
  3511. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3512. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3513. (((class color) (min-colors 8)) (:foreground "green"))
  3514. (t (:bold t))))
  3515. "Face used for todo keywords that indicate DONE items."
  3516. :group 'org-faces)
  3517. (defface org-headline-done ;; font-lock-string-face
  3518. (org-compatible-face nil
  3519. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3520. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3521. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3522. "Face used to indicate that a headline is DONE.
  3523. This face is only used if `org-fontify-done-headline' is set. If applies
  3524. to the part of the headline after the DONE keyword."
  3525. :group 'org-faces)
  3526. (defcustom org-todo-keyword-faces nil
  3527. "Faces for specific TODO keywords.
  3528. This is a list of cons cells, with TODO keywords in the car
  3529. and faces in the cdr. The face can be a symbol, or a property
  3530. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3531. :group 'org-faces
  3532. :group 'org-todo
  3533. :type '(repeat
  3534. (cons
  3535. (string :tag "keyword")
  3536. (sexp :tag "face"))))
  3537. (defface org-table ;; 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) (background light)) (:foreground "blue"))
  3544. (((class color) (min-colors 8) (background dark)))))
  3545. "Face used for tables."
  3546. :group 'org-faces)
  3547. (defface org-formula
  3548. (org-compatible-face nil
  3549. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3550. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3551. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3552. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3553. (t (:bold t :italic t))))
  3554. "Face for formulas."
  3555. :group 'org-faces)
  3556. (defface org-code
  3557. (org-compatible-face nil
  3558. '((((class color grayscale) (min-colors 88) (background light))
  3559. (:foreground "grey50"))
  3560. (((class color grayscale) (min-colors 88) (background dark))
  3561. (:foreground "grey70"))
  3562. (((class color) (min-colors 8) (background light))
  3563. (:foreground "green"))
  3564. (((class color) (min-colors 8) (background dark))
  3565. (:foreground "yellow"))))
  3566. "Face for fixed-with text like code snippets."
  3567. :group 'org-faces
  3568. :version "22.1")
  3569. (defface org-verbatim
  3570. (org-compatible-face nil
  3571. '((((class color grayscale) (min-colors 88) (background light))
  3572. (:foreground "grey50" :underline t))
  3573. (((class color grayscale) (min-colors 88) (background dark))
  3574. (:foreground "grey70" :underline t))
  3575. (((class color) (min-colors 8) (background light))
  3576. (:foreground "green" :underline t))
  3577. (((class color) (min-colors 8) (background dark))
  3578. (:foreground "yellow" :underline t))))
  3579. "Face for fixed-with text like code snippets."
  3580. :group 'org-faces
  3581. :version "22.1")
  3582. (defface org-agenda-structure ;; font-lock-function-name-face
  3583. (org-compatible-face nil
  3584. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3585. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3586. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3587. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3588. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3589. (t (:bold t))))
  3590. "Face used in agenda for captions and dates."
  3591. :group 'org-faces)
  3592. (defface org-scheduled-today
  3593. (org-compatible-face nil
  3594. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3595. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3596. (((class color) (min-colors 8)) (:foreground "green"))
  3597. (t (:bold t :italic t))))
  3598. "Face for items scheduled for a certain day."
  3599. :group 'org-faces)
  3600. (defface org-scheduled-previously
  3601. (org-compatible-face nil
  3602. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3603. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3604. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3605. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3606. (t (:bold t))))
  3607. "Face for items scheduled previously, and not yet done."
  3608. :group 'org-faces)
  3609. (defface org-upcoming-deadline
  3610. (org-compatible-face nil
  3611. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3612. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3613. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3614. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3615. (t (:bold t))))
  3616. "Face for items scheduled previously, and not yet done."
  3617. :group 'org-faces)
  3618. (defcustom org-agenda-deadline-faces
  3619. '((1.0 . org-warning)
  3620. (0.5 . org-upcoming-deadline)
  3621. (0.0 . default))
  3622. "Faces for showing deadlines in the agenda.
  3623. This is a list of cons cells. The cdr of each cell is a face to be used,
  3624. and it can also just be like '(:foreground \"yellow\").
  3625. Each car is a fraction of the head-warning time that must have passed for
  3626. this the face in the cdr to be used for display. The numbers must be
  3627. given in descending order. The head-warning time is normally taken
  3628. from `org-deadline-warning-days', but can also be specified in the deadline
  3629. timestamp itself, like this:
  3630. DEADLINE: <2007-08-13 Mon -8d>
  3631. You may use d for days, w for weeks, m for months and y for years. Months
  3632. and years will only be treated in an approximate fashion (30.4 days for a
  3633. month and 365.24 days for a year)."
  3634. :group 'org-faces
  3635. :group 'org-agenda-daily/weekly
  3636. :type '(repeat
  3637. (cons
  3638. (number :tag "Fraction of head-warning time passed")
  3639. (sexp :tag "Face"))))
  3640. ;; FIXME: this is not a good face yet.
  3641. (defface org-agenda-restriction-lock
  3642. (org-compatible-face nil
  3643. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3644. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3645. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3646. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3647. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3648. (t (:inverse-video t))))
  3649. "Face for showing the agenda restriction lock."
  3650. :group 'org-faces)
  3651. (defface org-time-grid ;; font-lock-variable-name-face
  3652. (org-compatible-face nil
  3653. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3654. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3655. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3656. "Face used for time grids."
  3657. :group 'org-faces)
  3658. (defconst org-level-faces
  3659. '(org-level-1 org-level-2 org-level-3 org-level-4
  3660. org-level-5 org-level-6 org-level-7 org-level-8
  3661. ))
  3662. (defcustom org-n-level-faces (length org-level-faces)
  3663. "The number of different faces to be used for headlines.
  3664. Org-mode defines 8 different headline faces, so this can be at most 8.
  3665. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3666. :type 'number
  3667. :group 'org-faces)
  3668. ;;; Functions and variables from ther packages
  3669. ;; Declared here to avoid compiler warnings
  3670. (eval-and-compile
  3671. (unless (fboundp 'declare-function)
  3672. (defmacro declare-function (fn file &optional arglist fileonly))))
  3673. ;; XEmacs only
  3674. (defvar outline-mode-menu-heading)
  3675. (defvar outline-mode-menu-show)
  3676. (defvar outline-mode-menu-hide)
  3677. (defvar zmacs-regions) ; XEmacs regions
  3678. ;; Emacs only
  3679. (defvar mark-active)
  3680. ;; Various packages
  3681. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3682. (declare-function add-to-diary-list "diary-lib"
  3683. (date string specifier &optional marker globcolor literal))
  3684. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3685. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3686. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3687. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3688. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3689. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3690. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3691. (declare-function bbdb-record-name "ext:bbdb" (record))
  3692. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3693. (declare-function bibtex-generate-autokey "bibtex" ())
  3694. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3695. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3696. (defvar calc-embedded-close-formula)
  3697. (defvar calc-embedded-open-formula)
  3698. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3699. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3700. (declare-function calendar-check-holidays "holidays" (date))
  3701. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3702. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3703. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3704. (declare-function calendar-forward-day "cal-move" (arg))
  3705. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3706. (declare-function calendar-goto-date "cal-move" (date))
  3707. (declare-function calendar-goto-today "cal-move" ())
  3708. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3709. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3710. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3711. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3712. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3713. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3714. (defvar calendar-mode-map)
  3715. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3716. (declare-function cdlatex-tab "ext:cdlatex" ())
  3717. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3718. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3719. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3720. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3721. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3722. (defvar font-lock-unfontify-region-function)
  3723. (declare-function gnus-article-show-summary "gnus-art" ())
  3724. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3725. (defvar gnus-other-frame-object)
  3726. (defvar gnus-group-name)
  3727. (defvar gnus-article-current)
  3728. (defvar Info-current-file)
  3729. (defvar Info-current-node)
  3730. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3731. (declare-function mh-find-path "mh-utils" ())
  3732. (declare-function mh-get-header-field "mh-utils" (field))
  3733. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3734. (declare-function mh-header-display "mh-show" ())
  3735. (declare-function mh-index-previous-folder "mh-search" ())
  3736. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3737. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3738. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3739. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3740. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3741. (declare-function mh-show-header-display "mh-show" t t)
  3742. (declare-function mh-show-msg "mh-show" (msg))
  3743. (declare-function mh-show-show "mh-show" t t)
  3744. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3745. (defvar mh-progs)
  3746. (defvar mh-current-folder)
  3747. (defvar mh-show-folder-buffer)
  3748. (defvar mh-index-folder)
  3749. (defvar mh-searcher)
  3750. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3751. (declare-function parse-time-string "parse-time" (string))
  3752. (declare-function remember "remember" (&optional initial))
  3753. (declare-function remember-buffer-desc "remember" ())
  3754. (declare-function remember-finalize "remember" ())
  3755. (defvar remember-save-after-remembering)
  3756. (defvar remember-data-file)
  3757. (defvar remember-register)
  3758. (defvar remember-buffer)
  3759. (defvar remember-handler-functions)
  3760. (defvar remember-annotation-functions)
  3761. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3762. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3763. (declare-function rmail-what-message "rmail" ())
  3764. (defvar rmail-current-message)
  3765. (defvar texmathp-why)
  3766. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3767. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3768. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3769. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3770. (declare-function vm-isearch-update "ext:vm-search" ())
  3771. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3772. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3773. (declare-function vm-su-subject "ext:vm-summary" (m))
  3774. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3775. (defvar vm-message-pointer)
  3776. (defvar vm-folder-directory)
  3777. (defvar w3m-current-url)
  3778. (defvar w3m-current-title)
  3779. ;; backward compatibility to old version of wl
  3780. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3781. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3782. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3783. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3784. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3785. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3786. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3787. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3788. (defvar wl-summary-buffer-elmo-folder)
  3789. (defvar wl-summary-buffer-folder-name)
  3790. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3791. (defvar org-latex-regexps)
  3792. (defvar constants-unit-system)
  3793. ;;; Variables for pre-computed regular expressions, all buffer local
  3794. (defvar org-drawer-regexp nil
  3795. "Matches first line of a hidden block.")
  3796. (make-variable-buffer-local 'org-drawer-regexp)
  3797. (defvar org-todo-regexp nil
  3798. "Matches any of the TODO state keywords.")
  3799. (make-variable-buffer-local 'org-todo-regexp)
  3800. (defvar org-not-done-regexp nil
  3801. "Matches any of the TODO state keywords except the last one.")
  3802. (make-variable-buffer-local 'org-not-done-regexp)
  3803. (defvar org-todo-line-regexp nil
  3804. "Matches a headline and puts TODO state into group 2 if present.")
  3805. (make-variable-buffer-local 'org-todo-line-regexp)
  3806. (defvar org-complex-heading-regexp nil
  3807. "Matches a headline and puts everything into groups:
  3808. group 1: the stars
  3809. group 2: The todo keyword, maybe
  3810. group 3: Priority cookie
  3811. group 4: True headline
  3812. group 5: Tags")
  3813. (make-variable-buffer-local 'org-complex-heading-regexp)
  3814. (defvar org-todo-line-tags-regexp nil
  3815. "Matches a headline and puts TODO state into group 2 if present.
  3816. Also put tags into group 4 if tags are present.")
  3817. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3818. (defvar org-nl-done-regexp nil
  3819. "Matches newline followed by a headline with the DONE keyword.")
  3820. (make-variable-buffer-local 'org-nl-done-regexp)
  3821. (defvar org-looking-at-done-regexp nil
  3822. "Matches the DONE keyword a point.")
  3823. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3824. (defvar org-ds-keyword-length 12
  3825. "Maximum length of the Deadline and SCHEDULED keywords.")
  3826. (make-variable-buffer-local 'org-ds-keyword-length)
  3827. (defvar org-deadline-regexp nil
  3828. "Matches the DEADLINE keyword.")
  3829. (make-variable-buffer-local 'org-deadline-regexp)
  3830. (defvar org-deadline-time-regexp nil
  3831. "Matches the DEADLINE keyword together with a time stamp.")
  3832. (make-variable-buffer-local 'org-deadline-time-regexp)
  3833. (defvar org-deadline-line-regexp nil
  3834. "Matches the DEADLINE keyword and the rest of the line.")
  3835. (make-variable-buffer-local 'org-deadline-line-regexp)
  3836. (defvar org-scheduled-regexp nil
  3837. "Matches the SCHEDULED keyword.")
  3838. (make-variable-buffer-local 'org-scheduled-regexp)
  3839. (defvar org-scheduled-time-regexp nil
  3840. "Matches the SCHEDULED keyword together with a time stamp.")
  3841. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3842. (defvar org-closed-time-regexp nil
  3843. "Matches the CLOSED keyword together with a time stamp.")
  3844. (make-variable-buffer-local 'org-closed-time-regexp)
  3845. (defvar org-keyword-time-regexp nil
  3846. "Matches any of the 4 keywords, together with the time stamp.")
  3847. (make-variable-buffer-local 'org-keyword-time-regexp)
  3848. (defvar org-keyword-time-not-clock-regexp nil
  3849. "Matches any of the 3 keywords, together with the time stamp.")
  3850. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3851. (defvar org-maybe-keyword-time-regexp nil
  3852. "Matches a timestamp, possibly preceeded by a keyword.")
  3853. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3854. (defvar org-planning-or-clock-line-re nil
  3855. "Matches a line with planning or clock info.")
  3856. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3857. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3858. rear-nonsticky t mouse-map t fontified t)
  3859. "Properties to remove when a string without properties is wanted.")
  3860. (defsubst org-match-string-no-properties (num &optional string)
  3861. (if (featurep 'xemacs)
  3862. (let ((s (match-string num string)))
  3863. (remove-text-properties 0 (length s) org-rm-props s)
  3864. s)
  3865. (match-string-no-properties num string)))
  3866. (defsubst org-no-properties (s)
  3867. (if (fboundp 'set-text-properties)
  3868. (set-text-properties 0 (length s) nil s)
  3869. (remove-text-properties 0 (length s) org-rm-props s))
  3870. s)
  3871. (defsubst org-get-alist-option (option key)
  3872. (cond ((eq key t) t)
  3873. ((eq option t) t)
  3874. ((assoc key option) (cdr (assoc key option)))
  3875. (t (cdr (assq 'default option)))))
  3876. (defsubst org-inhibit-invisibility ()
  3877. "Modified `buffer-invisibility-spec' for Emacs 21.
  3878. Some ops with invisible text do not work correctly on Emacs 21. For these
  3879. we turn off invisibility temporarily. Use this in a `let' form."
  3880. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3881. (defsubst org-set-local (var value)
  3882. "Make VAR local in current buffer and set it to VALUE."
  3883. (set (make-variable-buffer-local var) value))
  3884. (defsubst org-mode-p ()
  3885. "Check if the current buffer is in Org-mode."
  3886. (eq major-mode 'org-mode))
  3887. (defsubst org-last (list)
  3888. "Return the last element of LIST."
  3889. (car (last list)))
  3890. (defun org-let (list &rest body)
  3891. (eval (cons 'let (cons list body))))
  3892. (put 'org-let 'lisp-indent-function 1)
  3893. (defun org-let2 (list1 list2 &rest body)
  3894. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3895. (put 'org-let2 'lisp-indent-function 2)
  3896. (defconst org-startup-options
  3897. '(("fold" org-startup-folded t)
  3898. ("overview" org-startup-folded t)
  3899. ("nofold" org-startup-folded nil)
  3900. ("showall" org-startup-folded nil)
  3901. ("content" org-startup-folded content)
  3902. ("hidestars" org-hide-leading-stars t)
  3903. ("showstars" org-hide-leading-stars nil)
  3904. ("odd" org-odd-levels-only t)
  3905. ("oddeven" org-odd-levels-only nil)
  3906. ("align" org-startup-align-all-tables t)
  3907. ("noalign" org-startup-align-all-tables nil)
  3908. ("customtime" org-display-custom-times t)
  3909. ("logdone" org-log-done time)
  3910. ("lognotedone" org-log-done note)
  3911. ("nologdone" org-log-done nil)
  3912. ("lognoteclock-out" org-log-note-clock-out t)
  3913. ("nolognoteclock-out" org-log-note-clock-out nil)
  3914. ("logrepeat" org-log-repeat state)
  3915. ("lognoterepeat" org-log-repeat note)
  3916. ("nologrepeat" org-log-repeat nil)
  3917. ("constcgs" constants-unit-system cgs)
  3918. ("constSI" constants-unit-system SI))
  3919. "Variable associated with STARTUP options for org-mode.
  3920. Each element is a list of three items: The startup options as written
  3921. in the #+STARTUP line, the corresponding variable, and the value to
  3922. set this variable to if the option is found. An optional forth element PUSH
  3923. means to push this value onto the list in the variable.")
  3924. (defun org-set-regexps-and-options ()
  3925. "Precompute regular expressions for current buffer."
  3926. (when (org-mode-p)
  3927. (org-set-local 'org-todo-kwd-alist nil)
  3928. (org-set-local 'org-todo-key-alist nil)
  3929. (org-set-local 'org-todo-key-trigger nil)
  3930. (org-set-local 'org-todo-keywords-1 nil)
  3931. (org-set-local 'org-done-keywords nil)
  3932. (org-set-local 'org-todo-heads nil)
  3933. (org-set-local 'org-todo-sets nil)
  3934. (org-set-local 'org-todo-log-states nil)
  3935. (let ((re (org-make-options-regexp
  3936. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3937. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3938. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3939. (splitre "[ \t]+")
  3940. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3941. tail sep kws1 prio props drawers)
  3942. (save-excursion
  3943. (save-restriction
  3944. (widen)
  3945. (goto-char (point-min))
  3946. (while (re-search-forward re nil t)
  3947. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3948. (cond
  3949. ((equal key "CATEGORY")
  3950. (if (string-match "[ \t]+$" value)
  3951. (setq value (replace-match "" t t value)))
  3952. (setq cat value))
  3953. ((member key '("SEQ_TODO" "TODO"))
  3954. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3955. ((equal key "TYP_TODO")
  3956. (push (cons 'type (org-split-string value splitre)) kwds))
  3957. ((equal key "TAGS")
  3958. (setq tags (append tags (org-split-string value splitre))))
  3959. ((equal key "COLUMNS")
  3960. (org-set-local 'org-columns-default-format value))
  3961. ((equal key "LINK")
  3962. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3963. (push (cons (match-string 1 value)
  3964. (org-trim (match-string 2 value)))
  3965. links)))
  3966. ((equal key "PRIORITIES")
  3967. (setq prio (org-split-string value " +")))
  3968. ((equal key "PROPERTY")
  3969. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3970. (push (cons (match-string 1 value) (match-string 2 value))
  3971. props)))
  3972. ((equal key "DRAWERS")
  3973. (setq drawers (org-split-string value splitre)))
  3974. ((equal key "CONSTANTS")
  3975. (setq const (append const (org-split-string value splitre))))
  3976. ((equal key "STARTUP")
  3977. (let ((opts (org-split-string value splitre))
  3978. l var val)
  3979. (while (setq l (pop opts))
  3980. (when (setq l (assoc l org-startup-options))
  3981. (setq var (nth 1 l) val (nth 2 l))
  3982. (if (not (nth 3 l))
  3983. (set (make-local-variable var) val)
  3984. (if (not (listp (symbol-value var)))
  3985. (set (make-local-variable var) nil))
  3986. (set (make-local-variable var) (symbol-value var))
  3987. (add-to-list var val))))))
  3988. ((equal key "ARCHIVE")
  3989. (string-match " *$" value)
  3990. (setq arch (replace-match "" t t value))
  3991. (remove-text-properties 0 (length arch)
  3992. '(face t fontified t) arch)))
  3993. )))
  3994. (when cat
  3995. (org-set-local 'org-category (intern cat))
  3996. (push (cons "CATEGORY" cat) props))
  3997. (when prio
  3998. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3999. (setq prio (mapcar 'string-to-char prio))
  4000. (org-set-local 'org-highest-priority (nth 0 prio))
  4001. (org-set-local 'org-lowest-priority (nth 1 prio))
  4002. (org-set-local 'org-default-priority (nth 2 prio)))
  4003. (and props (org-set-local 'org-local-properties (nreverse props)))
  4004. (and drawers (org-set-local 'org-drawers drawers))
  4005. (and arch (org-set-local 'org-archive-location arch))
  4006. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4007. ;; Process the TODO keywords
  4008. (unless kwds
  4009. ;; Use the global values as if they had been given locally.
  4010. (setq kwds (default-value 'org-todo-keywords))
  4011. (if (stringp (car kwds))
  4012. (setq kwds (list (cons org-todo-interpretation
  4013. (default-value 'org-todo-keywords)))))
  4014. (setq kwds (reverse kwds)))
  4015. (setq kwds (nreverse kwds))
  4016. (let (inter kws kw)
  4017. (while (setq kws (pop kwds))
  4018. (setq inter (pop kws) sep (member "|" kws)
  4019. kws0 (delete "|" (copy-sequence kws))
  4020. kwsa nil
  4021. kws1 (mapcar
  4022. (lambda (x)
  4023. ;; 1 2
  4024. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4025. (progn
  4026. (setq kw (match-string 1 x)
  4027. key (and (match-end 2) (match-string 2 x))
  4028. log (org-extract-log-state-settings x))
  4029. (push (cons kw (and key (string-to-char key))) kwsa)
  4030. (and log (push log org-todo-log-states))
  4031. kw)
  4032. (error "Invalid TODO keyword %s" x)))
  4033. kws0)
  4034. kwsa (if kwsa (append '((:startgroup))
  4035. (nreverse kwsa)
  4036. '((:endgroup))))
  4037. hw (car kws1)
  4038. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4039. tail (list inter hw (car dws) (org-last dws)))
  4040. (add-to-list 'org-todo-heads hw 'append)
  4041. (push kws1 org-todo-sets)
  4042. (setq org-done-keywords (append org-done-keywords dws nil))
  4043. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4044. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4045. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4046. (setq org-todo-sets (nreverse org-todo-sets)
  4047. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4048. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4049. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4050. ;; Process the constants
  4051. (when const
  4052. (let (e cst)
  4053. (while (setq e (pop const))
  4054. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4055. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4056. (setq org-table-formula-constants-local cst)))
  4057. ;; Process the tags.
  4058. (when tags
  4059. (let (e tgs)
  4060. (while (setq e (pop tags))
  4061. (cond
  4062. ((equal e "{") (push '(:startgroup) tgs))
  4063. ((equal e "}") (push '(:endgroup) tgs))
  4064. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4065. (push (cons (match-string 1 e)
  4066. (string-to-char (match-string 2 e)))
  4067. tgs))
  4068. (t (push (list e) tgs))))
  4069. (org-set-local 'org-tag-alist nil)
  4070. (while (setq e (pop tgs))
  4071. (or (and (stringp (car e))
  4072. (assoc (car e) org-tag-alist))
  4073. (push e org-tag-alist))))))
  4074. ;; Compute the regular expressions and other local variables
  4075. (if (not org-done-keywords)
  4076. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4077. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4078. (length org-scheduled-string)))
  4079. org-drawer-regexp
  4080. (concat "^[ \t]*:\\("
  4081. (mapconcat 'regexp-quote org-drawers "\\|")
  4082. "\\):[ \t]*$")
  4083. org-not-done-keywords
  4084. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4085. org-todo-regexp
  4086. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4087. "\\|") "\\)\\>")
  4088. org-not-done-regexp
  4089. (concat "\\<\\("
  4090. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4091. "\\)\\>")
  4092. org-todo-line-regexp
  4093. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4094. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4095. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4096. org-complex-heading-regexp
  4097. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4098. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4099. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4100. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4101. org-nl-done-regexp
  4102. (concat "\n\\*+[ \t]+"
  4103. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4104. "\\)" "\\>")
  4105. org-todo-line-tags-regexp
  4106. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4107. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4108. (org-re
  4109. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4110. org-looking-at-done-regexp
  4111. (concat "^" "\\(?:"
  4112. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4113. "\\>")
  4114. org-deadline-regexp (concat "\\<" org-deadline-string)
  4115. org-deadline-time-regexp
  4116. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4117. org-deadline-line-regexp
  4118. (concat "\\<\\(" org-deadline-string "\\).*")
  4119. org-scheduled-regexp
  4120. (concat "\\<" org-scheduled-string)
  4121. org-scheduled-time-regexp
  4122. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4123. org-closed-time-regexp
  4124. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4125. org-keyword-time-regexp
  4126. (concat "\\<\\(" org-scheduled-string
  4127. "\\|" org-deadline-string
  4128. "\\|" org-closed-string
  4129. "\\|" org-clock-string "\\)"
  4130. " *[[<]\\([^]>]+\\)[]>]")
  4131. org-keyword-time-not-clock-regexp
  4132. (concat "\\<\\(" org-scheduled-string
  4133. "\\|" org-deadline-string
  4134. "\\|" org-closed-string
  4135. "\\)"
  4136. " *[[<]\\([^]>]+\\)[]>]")
  4137. org-maybe-keyword-time-regexp
  4138. (concat "\\(\\<\\(" org-scheduled-string
  4139. "\\|" org-deadline-string
  4140. "\\|" org-closed-string
  4141. "\\|" org-clock-string "\\)\\)?"
  4142. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4143. org-planning-or-clock-line-re
  4144. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4145. "\\|" org-deadline-string
  4146. "\\|" org-closed-string "\\|" org-clock-string
  4147. "\\)\\>\\)")
  4148. )
  4149. (org-compute-latex-and-specials-regexp)
  4150. (org-set-font-lock-defaults)))
  4151. (defun org-extract-log-state-settings (x)
  4152. "Extract the log state setting from a TODO keyword string.
  4153. This will extract info from a string like \"WAIT(w@/!)\"."
  4154. (let (kw key log1 log2)
  4155. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4156. (setq kw (match-string 1 x)
  4157. key (and (match-end 2) (match-string 2 x))
  4158. log1 (and (match-end 3) (match-string 3 x))
  4159. log2 (and (match-end 4) (match-string 4 x)))
  4160. (and (or log1 log2)
  4161. (list kw
  4162. (and log1 (if (equal log1 "!") 'time 'note))
  4163. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4164. (defun org-remove-keyword-keys (list)
  4165. "Remove a pair of parenthesis at the end of each string in LIST."
  4166. (mapcar (lambda (x)
  4167. (if (string-match "(.*)$" x)
  4168. (substring x 0 (match-beginning 0))
  4169. x))
  4170. list))
  4171. ;; FIXME: this could be done much better, using second characters etc.
  4172. (defun org-assign-fast-keys (alist)
  4173. "Assign fast keys to a keyword-key alist.
  4174. Respect keys that are already there."
  4175. (let (new e k c c1 c2 (char ?a))
  4176. (while (setq e (pop alist))
  4177. (cond
  4178. ((equal e '(:startgroup)) (push e new))
  4179. ((equal e '(:endgroup)) (push e new))
  4180. (t
  4181. (setq k (car e) c2 nil)
  4182. (if (cdr e)
  4183. (setq c (cdr e))
  4184. ;; automatically assign a character.
  4185. (setq c1 (string-to-char
  4186. (downcase (substring
  4187. k (if (= (string-to-char k) ?@) 1 0)))))
  4188. (if (or (rassoc c1 new) (rassoc c1 alist))
  4189. (while (or (rassoc char new) (rassoc char alist))
  4190. (setq char (1+ char)))
  4191. (setq c2 c1))
  4192. (setq c (or c2 char)))
  4193. (push (cons k c) new))))
  4194. (nreverse new)))
  4195. ;;; Some variables ujsed in various places
  4196. (defvar org-window-configuration nil
  4197. "Used in various places to store a window configuration.")
  4198. (defvar org-finish-function nil
  4199. "Function to be called when `C-c C-c' is used.
  4200. This is for getting out of special buffers like remember.")
  4201. ;; FIXME: Occasionally check by commenting these, to make sure
  4202. ;; no other functions uses these, forgetting to let-bind them.
  4203. (defvar entry)
  4204. (defvar state)
  4205. (defvar last-state)
  4206. (defvar date)
  4207. (defvar description)
  4208. ;; Defined somewhere in this file, but used before definition.
  4209. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4210. (defvar org-agenda-buffer-name)
  4211. (defvar org-agenda-undo-list)
  4212. (defvar org-agenda-pending-undo-list)
  4213. (defvar org-agenda-overriding-header)
  4214. (defvar orgtbl-mode)
  4215. (defvar org-html-entities)
  4216. (defvar org-struct-menu)
  4217. (defvar org-org-menu)
  4218. (defvar org-tbl-menu)
  4219. (defvar org-agenda-keymap)
  4220. ;;;; Emacs/XEmacs compatibility
  4221. ;; Overlay compatibility functions
  4222. (defun org-make-overlay (beg end &optional buffer)
  4223. (if (featurep 'xemacs)
  4224. (make-extent beg end buffer)
  4225. (make-overlay beg end buffer)))
  4226. (defun org-delete-overlay (ovl)
  4227. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4228. (defun org-detach-overlay (ovl)
  4229. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4230. (defun org-move-overlay (ovl beg end &optional buffer)
  4231. (if (featurep 'xemacs)
  4232. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4233. (move-overlay ovl beg end buffer)))
  4234. (defun org-overlay-put (ovl prop value)
  4235. (if (featurep 'xemacs)
  4236. (set-extent-property ovl prop value)
  4237. (overlay-put ovl prop value)))
  4238. (defun org-overlay-display (ovl text &optional face evap)
  4239. "Make overlay OVL display TEXT with face FACE."
  4240. (if (featurep 'xemacs)
  4241. (let ((gl (make-glyph text)))
  4242. (and face (set-glyph-face gl face))
  4243. (set-extent-property ovl 'invisible t)
  4244. (set-extent-property ovl 'end-glyph gl))
  4245. (overlay-put ovl 'display text)
  4246. (if face (overlay-put ovl 'face face))
  4247. (if evap (overlay-put ovl 'evaporate t))))
  4248. (defun org-overlay-before-string (ovl text &optional face evap)
  4249. "Make overlay OVL display TEXT with face FACE."
  4250. (if (featurep 'xemacs)
  4251. (let ((gl (make-glyph text)))
  4252. (and face (set-glyph-face gl face))
  4253. (set-extent-property ovl 'begin-glyph gl))
  4254. (if face (org-add-props text nil 'face face))
  4255. (overlay-put ovl 'before-string text)
  4256. (if evap (overlay-put ovl 'evaporate t))))
  4257. (defun org-overlay-get (ovl prop)
  4258. (if (featurep 'xemacs)
  4259. (extent-property ovl prop)
  4260. (overlay-get ovl prop)))
  4261. (defun org-overlays-at (pos)
  4262. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4263. (defun org-overlays-in (&optional start end)
  4264. (if (featurep 'xemacs)
  4265. (extent-list nil start end)
  4266. (overlays-in start end)))
  4267. (defun org-overlay-start (o)
  4268. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4269. (defun org-overlay-end (o)
  4270. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4271. (defun org-find-overlays (prop &optional pos delete)
  4272. "Find all overlays specifying PROP at POS or point.
  4273. If DELETE is non-nil, delete all those overlays."
  4274. (let ((overlays (org-overlays-at (or pos (point))))
  4275. ov found)
  4276. (while (setq ov (pop overlays))
  4277. (if (org-overlay-get ov prop)
  4278. (if delete (org-delete-overlay ov) (push ov found))))
  4279. found))
  4280. ;; Region compatibility
  4281. (defun org-add-hook (hook function &optional append local)
  4282. "Add-hook, compatible with both Emacsen."
  4283. (if (and local (featurep 'xemacs))
  4284. (add-local-hook hook function append)
  4285. (add-hook hook function append local)))
  4286. (defvar org-ignore-region nil
  4287. "To temporarily disable the active region.")
  4288. (defun org-region-active-p ()
  4289. "Is `transient-mark-mode' on and the region active?
  4290. Works on both Emacs and XEmacs."
  4291. (if org-ignore-region
  4292. nil
  4293. (if (featurep 'xemacs)
  4294. (and zmacs-regions (region-active-p))
  4295. (if (fboundp 'use-region-p)
  4296. (use-region-p)
  4297. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4298. ;; Invisibility compatibility
  4299. (defun org-add-to-invisibility-spec (arg)
  4300. "Add elements to `buffer-invisibility-spec'.
  4301. See documentation for `buffer-invisibility-spec' for the kind of elements
  4302. that can be added."
  4303. (cond
  4304. ((fboundp 'add-to-invisibility-spec)
  4305. (add-to-invisibility-spec arg))
  4306. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4307. (setq buffer-invisibility-spec (list arg)))
  4308. (t
  4309. (setq buffer-invisibility-spec
  4310. (cons arg buffer-invisibility-spec)))))
  4311. (defun org-remove-from-invisibility-spec (arg)
  4312. "Remove elements from `buffer-invisibility-spec'."
  4313. (if (fboundp 'remove-from-invisibility-spec)
  4314. (remove-from-invisibility-spec arg)
  4315. (if (consp buffer-invisibility-spec)
  4316. (setq buffer-invisibility-spec
  4317. (delete arg buffer-invisibility-spec)))))
  4318. (defun org-in-invisibility-spec-p (arg)
  4319. "Is ARG a member of `buffer-invisibility-spec'?"
  4320. (if (consp buffer-invisibility-spec)
  4321. (member arg buffer-invisibility-spec)
  4322. nil))
  4323. ;;;; Define the Org-mode
  4324. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4325. (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."))
  4326. ;; We use a before-change function to check if a table might need
  4327. ;; an update.
  4328. (defvar org-table-may-need-update t
  4329. "Indicates that a table might need an update.
  4330. This variable is set by `org-before-change-function'.
  4331. `org-table-align' sets it back to nil.")
  4332. (defvar org-mode-map)
  4333. (defvar org-mode-hook nil)
  4334. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4335. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4336. (defvar org-table-buffer-is-an nil)
  4337. (defconst org-outline-regexp "\\*+ ")
  4338. ;;;###autoload
  4339. (define-derived-mode org-mode outline-mode "Org"
  4340. "Outline-based notes management and organizer, alias
  4341. \"Carsten's outline-mode for keeping track of everything.\"
  4342. Org-mode develops organizational tasks around a NOTES file which
  4343. contains information about projects as plain text. Org-mode is
  4344. implemented on top of outline-mode, which is ideal to keep the content
  4345. of large files well structured. It supports ToDo items, deadlines and
  4346. time stamps, which magically appear in the diary listing of the Emacs
  4347. calendar. Tables are easily created with a built-in table editor.
  4348. Plain text URL-like links connect to websites, emails (VM), Usenet
  4349. messages (Gnus), BBDB entries, and any files related to the project.
  4350. For printing and sharing of notes, an Org-mode file (or a part of it)
  4351. can be exported as a structured ASCII or HTML file.
  4352. The following commands are available:
  4353. \\{org-mode-map}"
  4354. ;; Get rid of Outline menus, they are not needed
  4355. ;; Need to do this here because define-derived-mode sets up
  4356. ;; the keymap so late. Still, it is a waste to call this each time
  4357. ;; we switch another buffer into org-mode.
  4358. (if (featurep 'xemacs)
  4359. (when (boundp 'outline-mode-menu-heading)
  4360. ;; Assume this is Greg's port, it used easymenu
  4361. (easy-menu-remove outline-mode-menu-heading)
  4362. (easy-menu-remove outline-mode-menu-show)
  4363. (easy-menu-remove outline-mode-menu-hide))
  4364. (define-key org-mode-map [menu-bar headings] 'undefined)
  4365. (define-key org-mode-map [menu-bar hide] 'undefined)
  4366. (define-key org-mode-map [menu-bar show] 'undefined))
  4367. (easy-menu-add org-org-menu)
  4368. (easy-menu-add org-tbl-menu)
  4369. (org-install-agenda-files-menu)
  4370. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4371. (org-add-to-invisibility-spec '(org-cwidth))
  4372. (when (featurep 'xemacs)
  4373. (org-set-local 'line-move-ignore-invisible t))
  4374. (org-set-local 'outline-regexp org-outline-regexp)
  4375. (org-set-local 'outline-level 'org-outline-level)
  4376. (when (and org-ellipsis
  4377. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4378. (fboundp 'make-glyph-code))
  4379. (unless org-display-table
  4380. (setq org-display-table (make-display-table)))
  4381. (set-display-table-slot
  4382. org-display-table 4
  4383. (vconcat (mapcar
  4384. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4385. org-ellipsis)))
  4386. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4387. (setq buffer-display-table org-display-table))
  4388. (org-set-regexps-and-options)
  4389. ;; Calc embedded
  4390. (org-set-local 'calc-embedded-open-mode "# ")
  4391. (modify-syntax-entry ?# "<")
  4392. (modify-syntax-entry ?@ "w")
  4393. (if org-startup-truncated (setq truncate-lines t))
  4394. (org-set-local 'font-lock-unfontify-region-function
  4395. 'org-unfontify-region)
  4396. ;; Activate before-change-function
  4397. (org-set-local 'org-table-may-need-update t)
  4398. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4399. 'local)
  4400. ;; Check for running clock before killing a buffer
  4401. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4402. ;; Paragraphs and auto-filling
  4403. (org-set-autofill-regexps)
  4404. (setq indent-line-function 'org-indent-line-function)
  4405. (org-update-radio-target-regexp)
  4406. ;; Comment characters
  4407. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4408. (org-set-local 'comment-padding " ")
  4409. ;; Align options lines
  4410. (org-set-local
  4411. 'align-mode-rules-list
  4412. '((org-in-buffer-settings
  4413. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4414. (modes . '(org-mode)))))
  4415. ;; Imenu
  4416. (org-set-local 'imenu-create-index-function
  4417. 'org-imenu-get-tree)
  4418. ;; Make isearch reveal context
  4419. (if (or (featurep 'xemacs)
  4420. (not (boundp 'outline-isearch-open-invisible-function)))
  4421. ;; Emacs 21 and XEmacs make use of the hook
  4422. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4423. ;; Emacs 22 deals with this through a special variable
  4424. (org-set-local 'outline-isearch-open-invisible-function
  4425. (lambda (&rest ignore) (org-show-context 'isearch))))
  4426. ;; If empty file that did not turn on org-mode automatically, make it to.
  4427. (if (and org-insert-mode-line-in-empty-file
  4428. (interactive-p)
  4429. (= (point-min) (point-max)))
  4430. (insert "# -*- mode: org -*-\n\n"))
  4431. (unless org-inhibit-startup
  4432. (when org-startup-align-all-tables
  4433. (let ((bmp (buffer-modified-p)))
  4434. (org-table-map-tables 'org-table-align)
  4435. (set-buffer-modified-p bmp)))
  4436. (org-cycle-hide-drawers 'all)
  4437. (cond
  4438. ((eq org-startup-folded t)
  4439. (org-cycle '(4)))
  4440. ((eq org-startup-folded 'content)
  4441. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4442. (org-cycle '(4)) (org-cycle '(4)))))))
  4443. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4444. (defsubst org-call-with-arg (command arg)
  4445. "Call COMMAND interactively, but pretend prefix are was ARG."
  4446. (let ((current-prefix-arg arg)) (call-interactively command)))
  4447. (defsubst org-current-line (&optional pos)
  4448. (save-excursion
  4449. (and pos (goto-char pos))
  4450. ;; works also in narrowed buffer, because we start at 1, not point-min
  4451. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4452. (defun org-current-time ()
  4453. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4454. (if (> org-time-stamp-rounding-minutes 0)
  4455. (let ((r org-time-stamp-rounding-minutes)
  4456. (time (decode-time)))
  4457. (apply 'encode-time
  4458. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4459. (nthcdr 2 time))))
  4460. (current-time)))
  4461. (defun org-add-props (string plist &rest props)
  4462. "Add text properties to entire string, from beginning to end.
  4463. PLIST may be a list of properties, PROPS are individual properties and values
  4464. that will be added to PLIST. Returns the string that was modified."
  4465. (add-text-properties
  4466. 0 (length string) (if props (append plist props) plist) string)
  4467. string)
  4468. (put 'org-add-props 'lisp-indent-function 2)
  4469. ;;;; Font-Lock stuff, including the activators
  4470. (defvar org-mouse-map (make-sparse-keymap))
  4471. (org-defkey org-mouse-map
  4472. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4473. (org-defkey org-mouse-map
  4474. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4475. (when org-mouse-1-follows-link
  4476. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4477. (when org-tab-follows-link
  4478. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4479. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4480. (when org-return-follows-link
  4481. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4482. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4483. (require 'font-lock)
  4484. (defconst org-non-link-chars "]\t\n\r<>")
  4485. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4486. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4487. (defvar org-link-re-with-space nil
  4488. "Matches a link with spaces, optional angular brackets around it.")
  4489. (defvar org-link-re-with-space2 nil
  4490. "Matches a link with spaces, optional angular brackets around it.")
  4491. (defvar org-angle-link-re nil
  4492. "Matches link with angular brackets, spaces are allowed.")
  4493. (defvar org-plain-link-re nil
  4494. "Matches plain link, without spaces.")
  4495. (defvar org-bracket-link-regexp nil
  4496. "Matches a link in double brackets.")
  4497. (defvar org-bracket-link-analytic-regexp nil
  4498. "Regular expression used to analyze links.
  4499. Here is what the match groups contain after a match:
  4500. 1: http:
  4501. 2: http
  4502. 3: path
  4503. 4: [desc]
  4504. 5: desc")
  4505. (defvar org-any-link-re nil
  4506. "Regular expression matching any link.")
  4507. (defun org-make-link-regexps ()
  4508. "Update the link regular expressions.
  4509. This should be called after the variable `org-link-types' has changed."
  4510. (setq org-link-re-with-space
  4511. (concat
  4512. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4513. "\\([^" org-non-link-chars " ]"
  4514. "[^" org-non-link-chars "]*"
  4515. "[^" org-non-link-chars " ]\\)>?")
  4516. org-link-re-with-space2
  4517. (concat
  4518. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4519. "\\([^" org-non-link-chars " ]"
  4520. "[^]\t\n\r]*"
  4521. "[^" org-non-link-chars " ]\\)>?")
  4522. org-angle-link-re
  4523. (concat
  4524. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4525. "\\([^" org-non-link-chars " ]"
  4526. "[^" org-non-link-chars "]*"
  4527. "\\)>")
  4528. org-plain-link-re
  4529. (concat
  4530. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4531. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4532. org-bracket-link-regexp
  4533. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4534. org-bracket-link-analytic-regexp
  4535. (concat
  4536. "\\[\\["
  4537. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4538. "\\([^]]+\\)"
  4539. "\\]"
  4540. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4541. "\\]")
  4542. org-any-link-re
  4543. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4544. org-angle-link-re "\\)\\|\\("
  4545. org-plain-link-re "\\)")))
  4546. (org-make-link-regexps)
  4547. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4548. "Regular expression for fast time stamp matching.")
  4549. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4550. "Regular expression for fast time stamp matching.")
  4551. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4552. "Regular expression matching time strings for analysis.
  4553. This one does not require the space after the date.")
  4554. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4555. "Regular expression matching time strings for analysis.")
  4556. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4557. "Regular expression matching time stamps, with groups.")
  4558. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4559. "Regular expression matching time stamps (also [..]), with groups.")
  4560. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4561. "Regular expression matching a time stamp range.")
  4562. (defconst org-tr-regexp-both
  4563. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4564. "Regular expression matching a time stamp range.")
  4565. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4566. org-ts-regexp "\\)?")
  4567. "Regular expression matching a time stamp or time stamp range.")
  4568. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4569. org-ts-regexp-both "\\)?")
  4570. "Regular expression matching a time stamp or time stamp range.
  4571. The time stamps may be either active or inactive.")
  4572. (defvar org-emph-face nil)
  4573. (defun org-do-emphasis-faces (limit)
  4574. "Run through the buffer and add overlays to links."
  4575. (let (rtn)
  4576. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4577. (if (not (= (char-after (match-beginning 3))
  4578. (char-after (match-beginning 4))))
  4579. (progn
  4580. (setq rtn t)
  4581. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4582. 'face
  4583. (nth 1 (assoc (match-string 3)
  4584. org-emphasis-alist)))
  4585. (add-text-properties (match-beginning 2) (match-end 2)
  4586. '(font-lock-multiline t))
  4587. (when org-hide-emphasis-markers
  4588. (add-text-properties (match-end 4) (match-beginning 5)
  4589. '(invisible org-link))
  4590. (add-text-properties (match-beginning 3) (match-end 3)
  4591. '(invisible org-link)))))
  4592. (backward-char 1))
  4593. rtn))
  4594. (defun org-emphasize (&optional char)
  4595. "Insert or change an emphasis, i.e. a font like bold or italic.
  4596. If there is an active region, change that region to a new emphasis.
  4597. If there is no region, just insert the marker characters and position
  4598. the cursor between them.
  4599. CHAR should be either the marker character, or the first character of the
  4600. HTML tag associated with that emphasis. If CHAR is a space, the means
  4601. to remove the emphasis of the selected region.
  4602. If char is not given (for example in an interactive call) it
  4603. will be prompted for."
  4604. (interactive)
  4605. (let ((eal org-emphasis-alist) e det
  4606. (erc org-emphasis-regexp-components)
  4607. (prompt "")
  4608. (string "") beg end move tag c s)
  4609. (if (org-region-active-p)
  4610. (setq beg (region-beginning) end (region-end)
  4611. string (buffer-substring beg end))
  4612. (setq move t))
  4613. (while (setq e (pop eal))
  4614. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4615. c (aref tag 0))
  4616. (push (cons c (string-to-char (car e))) det)
  4617. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4618. (substring tag 1)))))
  4619. (unless char
  4620. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4621. (setq char (read-char-exclusive)))
  4622. (setq char (or (cdr (assoc char det)) char))
  4623. (if (equal char ?\ )
  4624. (setq s "" move nil)
  4625. (unless (assoc (char-to-string char) org-emphasis-alist)
  4626. (error "No such emphasis marker: \"%c\"" char))
  4627. (setq s (char-to-string char)))
  4628. (while (and (> (length string) 1)
  4629. (equal (substring string 0 1) (substring string -1))
  4630. (assoc (substring string 0 1) org-emphasis-alist))
  4631. (setq string (substring string 1 -1)))
  4632. (setq string (concat s string s))
  4633. (if beg (delete-region beg end))
  4634. (unless (or (bolp)
  4635. (string-match (concat "[" (nth 0 erc) "\n]")
  4636. (char-to-string (char-before (point)))))
  4637. (insert " "))
  4638. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4639. (char-to-string (char-after (point))))
  4640. (insert " ") (backward-char 1))
  4641. (insert string)
  4642. (and move (backward-char 1))))
  4643. (defconst org-nonsticky-props
  4644. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4645. (defun org-activate-plain-links (limit)
  4646. "Run through the buffer and add overlays to links."
  4647. (catch 'exit
  4648. (let (f)
  4649. (while (re-search-forward org-plain-link-re limit t)
  4650. (setq f (get-text-property (match-beginning 0) 'face))
  4651. (if (or (eq f 'org-tag)
  4652. (and (listp f) (memq 'org-tag f)))
  4653. nil
  4654. (add-text-properties (match-beginning 0) (match-end 0)
  4655. (list 'mouse-face 'highlight
  4656. 'rear-nonsticky org-nonsticky-props
  4657. 'keymap org-mouse-map
  4658. ))
  4659. (throw 'exit t))))))
  4660. (defun org-activate-code (limit)
  4661. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4662. (unless (get-text-property (match-beginning 1) 'face)
  4663. (remove-text-properties (match-beginning 0) (match-end 0)
  4664. '(display t invisible t intangible t))
  4665. t)))
  4666. (defun org-activate-angle-links (limit)
  4667. "Run through the buffer and add overlays to links."
  4668. (if (re-search-forward org-angle-link-re limit t)
  4669. (progn
  4670. (add-text-properties (match-beginning 0) (match-end 0)
  4671. (list 'mouse-face 'highlight
  4672. 'rear-nonsticky org-nonsticky-props
  4673. 'keymap org-mouse-map
  4674. ))
  4675. t)))
  4676. (defmacro org-maybe-intangible (props)
  4677. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4678. In emacs 21, invisible text is not avoided by the command loop, so the
  4679. intangible property is needed to make sure point skips this text.
  4680. In Emacs 22, this is not necessary. The intangible text property has
  4681. led to problems with flyspell. These problems are fixed in flyspell.el,
  4682. but we still avoid setting the property in Emacs 22 and later.
  4683. We use a macro so that the test can happen at compilation time."
  4684. (if (< emacs-major-version 22)
  4685. `(append '(intangible t) ,props)
  4686. props))
  4687. (defun org-activate-bracket-links (limit)
  4688. "Run through the buffer and add overlays to bracketed links."
  4689. (if (re-search-forward org-bracket-link-regexp limit t)
  4690. (let* ((help (concat "LINK: "
  4691. (org-match-string-no-properties 1)))
  4692. ;; FIXME: above we should remove the escapes.
  4693. ;; but that requires another match, protecting match data,
  4694. ;; a lot of overhead for font-lock.
  4695. (ip (org-maybe-intangible
  4696. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4697. 'keymap org-mouse-map 'mouse-face 'highlight
  4698. 'font-lock-multiline t 'help-echo help)))
  4699. (vp (list 'rear-nonsticky org-nonsticky-props
  4700. 'keymap org-mouse-map 'mouse-face 'highlight
  4701. ' font-lock-multiline t 'help-echo help)))
  4702. ;; We need to remove the invisible property here. Table narrowing
  4703. ;; may have made some of this invisible.
  4704. (remove-text-properties (match-beginning 0) (match-end 0)
  4705. '(invisible nil))
  4706. (if (match-end 3)
  4707. (progn
  4708. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4709. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4710. (add-text-properties (match-end 3) (match-end 0) ip))
  4711. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4712. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4713. (add-text-properties (match-end 1) (match-end 0) ip))
  4714. t)))
  4715. (defun org-activate-dates (limit)
  4716. "Run through the buffer and add overlays to dates."
  4717. (if (re-search-forward org-tsr-regexp-both limit t)
  4718. (progn
  4719. (add-text-properties (match-beginning 0) (match-end 0)
  4720. (list 'mouse-face 'highlight
  4721. 'rear-nonsticky org-nonsticky-props
  4722. 'keymap org-mouse-map))
  4723. (when org-display-custom-times
  4724. (if (match-end 3)
  4725. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4726. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4727. t)))
  4728. (defvar org-target-link-regexp nil
  4729. "Regular expression matching radio targets in plain text.")
  4730. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4731. "Regular expression matching a link target.")
  4732. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4733. "Regular expression matching a radio target.")
  4734. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4735. "Regular expression matching any target.")
  4736. (defun org-activate-target-links (limit)
  4737. "Run through the buffer and add overlays to target matches."
  4738. (when org-target-link-regexp
  4739. (let ((case-fold-search t))
  4740. (if (re-search-forward org-target-link-regexp limit t)
  4741. (progn
  4742. (add-text-properties (match-beginning 0) (match-end 0)
  4743. (list 'mouse-face 'highlight
  4744. 'rear-nonsticky org-nonsticky-props
  4745. 'keymap org-mouse-map
  4746. 'help-echo "Radio target link"
  4747. 'org-linked-text t))
  4748. t)))))
  4749. (defun org-update-radio-target-regexp ()
  4750. "Find all radio targets in this file and update the regular expression."
  4751. (interactive)
  4752. (when (memq 'radio org-activate-links)
  4753. (setq org-target-link-regexp
  4754. (org-make-target-link-regexp (org-all-targets 'radio)))
  4755. (org-restart-font-lock)))
  4756. (defun org-hide-wide-columns (limit)
  4757. (let (s e)
  4758. (setq s (text-property-any (point) (or limit (point-max))
  4759. 'org-cwidth t))
  4760. (when s
  4761. (setq e (next-single-property-change s 'org-cwidth))
  4762. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4763. (goto-char e)
  4764. t)))
  4765. (defvar org-latex-and-specials-regexp nil
  4766. "Regular expression for highlighting export special stuff.")
  4767. (defvar org-match-substring-regexp)
  4768. (defvar org-match-substring-with-braces-regexp)
  4769. (defvar org-export-html-special-string-regexps)
  4770. (defun org-compute-latex-and-specials-regexp ()
  4771. "Compute regular expression for stuff treated specially by exporters."
  4772. (if (not org-highlight-latex-fragments-and-specials)
  4773. (org-set-local 'org-latex-and-specials-regexp nil)
  4774. (let*
  4775. ((matchers (plist-get org-format-latex-options :matchers))
  4776. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4777. org-latex-regexps)))
  4778. (options (org-combine-plists (org-default-export-plist)
  4779. (org-infile-export-plist)))
  4780. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4781. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4782. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4783. (org-export-html-expand (plist-get options :expand-quoted-html))
  4784. (org-export-with-special-strings (plist-get options :special-strings))
  4785. (re-sub
  4786. (cond
  4787. ((equal org-export-with-sub-superscripts '{})
  4788. (list org-match-substring-with-braces-regexp))
  4789. (org-export-with-sub-superscripts
  4790. (list org-match-substring-regexp))
  4791. (t nil)))
  4792. (re-latex
  4793. (if org-export-with-LaTeX-fragments
  4794. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4795. (re-macros
  4796. (if org-export-with-TeX-macros
  4797. (list (concat "\\\\"
  4798. (regexp-opt
  4799. (append (mapcar 'car org-html-entities)
  4800. (if (boundp 'org-latex-entities)
  4801. org-latex-entities nil))
  4802. 'words))) ; FIXME
  4803. ))
  4804. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4805. (re-special (if org-export-with-special-strings
  4806. (mapcar (lambda (x) (car x))
  4807. org-export-html-special-string-regexps)))
  4808. (re-rest
  4809. (delq nil
  4810. (list
  4811. (if org-export-html-expand "@<[^>\n]+>")
  4812. ))))
  4813. (org-set-local
  4814. 'org-latex-and-specials-regexp
  4815. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4816. re-rest) "\\|")))))
  4817. (defface org-latex-and-export-specials
  4818. (let ((font (cond ((assq :inherit custom-face-attributes)
  4819. '(:inherit underline))
  4820. (t '(:underline t)))))
  4821. `((((class grayscale) (background light))
  4822. (:foreground "DimGray" ,@font))
  4823. (((class grayscale) (background dark))
  4824. (:foreground "LightGray" ,@font))
  4825. (((class color) (background light))
  4826. (:foreground "SaddleBrown"))
  4827. (((class color) (background dark))
  4828. (:foreground "burlywood"))
  4829. (t (,@font))))
  4830. "Face used to highlight math latex and other special exporter stuff."
  4831. :group 'org-faces)
  4832. (defun org-do-latex-and-special-faces (limit)
  4833. "Run through the buffer and add overlays to links."
  4834. (when org-latex-and-specials-regexp
  4835. (let (rtn d)
  4836. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4837. limit t))
  4838. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4839. 'face))
  4840. '(org-code org-verbatim underline)))
  4841. (progn
  4842. (setq rtn t
  4843. d (cond ((member (char-after (1+ (match-beginning 0)))
  4844. '(?_ ?^)) 1)
  4845. (t 0)))
  4846. (font-lock-prepend-text-property
  4847. (+ d (match-beginning 0)) (match-end 0)
  4848. 'face 'org-latex-and-export-specials)
  4849. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4850. '(font-lock-multiline t)))))
  4851. rtn)))
  4852. (defun org-restart-font-lock ()
  4853. "Restart font-lock-mode, to force refontification."
  4854. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4855. (font-lock-mode -1)
  4856. (font-lock-mode 1)))
  4857. (defun org-all-targets (&optional radio)
  4858. "Return a list of all targets in this file.
  4859. With optional argument RADIO, only find radio targets."
  4860. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4861. rtn)
  4862. (save-excursion
  4863. (goto-char (point-min))
  4864. (while (re-search-forward re nil t)
  4865. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4866. rtn)))
  4867. (defun org-make-target-link-regexp (targets)
  4868. "Make regular expression matching all strings in TARGETS.
  4869. The regular expression finds the targets also if there is a line break
  4870. between words."
  4871. (and targets
  4872. (concat
  4873. "\\<\\("
  4874. (mapconcat
  4875. (lambda (x)
  4876. (while (string-match " +" x)
  4877. (setq x (replace-match "\\s-+" t t x)))
  4878. x)
  4879. targets
  4880. "\\|")
  4881. "\\)\\>")))
  4882. (defun org-activate-tags (limit)
  4883. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4884. (progn
  4885. (add-text-properties (match-beginning 1) (match-end 1)
  4886. (list 'mouse-face 'highlight
  4887. 'rear-nonsticky org-nonsticky-props
  4888. 'keymap org-mouse-map))
  4889. t)))
  4890. (defun org-outline-level ()
  4891. (save-excursion
  4892. (looking-at outline-regexp)
  4893. (if (match-beginning 1)
  4894. (+ (org-get-string-indentation (match-string 1)) 1000)
  4895. (1- (- (match-end 0) (match-beginning 0))))))
  4896. (defvar org-font-lock-keywords nil)
  4897. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4898. "Regular expression matching a property line.")
  4899. (defun org-set-font-lock-defaults ()
  4900. (let* ((em org-fontify-emphasized-text)
  4901. (lk org-activate-links)
  4902. (org-font-lock-extra-keywords
  4903. (list
  4904. ;; Headlines
  4905. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4906. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4907. ;; Table lines
  4908. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4909. (1 'org-table t))
  4910. ;; Table internals
  4911. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4912. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4913. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4914. ;; Drawers
  4915. (list org-drawer-regexp '(0 'org-special-keyword t))
  4916. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4917. ;; Properties
  4918. (list org-property-re
  4919. '(1 'org-special-keyword t)
  4920. '(3 'org-property-value t))
  4921. (if org-format-transports-properties-p
  4922. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4923. ;; Links
  4924. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4925. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4926. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4927. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4928. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4929. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4930. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4931. '(org-hide-wide-columns (0 nil append))
  4932. ;; TODO lines
  4933. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4934. '(1 (org-get-todo-face 1) t))
  4935. ;; DONE
  4936. (if org-fontify-done-headline
  4937. (list (concat "^[*]+ +\\<\\("
  4938. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4939. "\\)\\(.*\\)")
  4940. '(2 'org-headline-done t))
  4941. nil)
  4942. ;; Priorities
  4943. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4944. ;; Special keywords
  4945. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4946. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4947. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4948. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4949. ;; Emphasis
  4950. (if em
  4951. (if (featurep 'xemacs)
  4952. '(org-do-emphasis-faces (0 nil append))
  4953. '(org-do-emphasis-faces)))
  4954. ;; Checkboxes
  4955. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4956. 2 'bold prepend)
  4957. (if org-provide-checkbox-statistics
  4958. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4959. (0 (org-get-checkbox-statistics-face) t)))
  4960. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4961. '(1 'org-archived prepend))
  4962. ;; Specials
  4963. '(org-do-latex-and-special-faces)
  4964. ;; Code
  4965. '(org-activate-code (1 'org-code t))
  4966. ;; COMMENT
  4967. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4968. "\\|" org-quote-string "\\)\\>")
  4969. '(1 'org-special-keyword t))
  4970. '("^#.*" (0 'font-lock-comment-face t))
  4971. )))
  4972. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4973. ;; Now set the full font-lock-keywords
  4974. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4975. (org-set-local 'font-lock-defaults
  4976. '(org-font-lock-keywords t nil nil backward-paragraph))
  4977. (kill-local-variable 'font-lock-keywords) nil))
  4978. (defvar org-m nil)
  4979. (defvar org-l nil)
  4980. (defvar org-f nil)
  4981. (defun org-get-level-face (n)
  4982. "Get the right face for match N in font-lock matching of healdines."
  4983. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4984. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4985. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4986. (cond
  4987. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4988. ((eq n 2) org-f)
  4989. (t (if org-level-color-stars-only nil org-f))))
  4990. (defun org-get-todo-face (kwd)
  4991. "Get the right face for a TODO keyword KWD.
  4992. If KWD is a number, get the corresponding match group."
  4993. (if (numberp kwd) (setq kwd (match-string kwd)))
  4994. (or (cdr (assoc kwd org-todo-keyword-faces))
  4995. (and (member kwd org-done-keywords) 'org-done)
  4996. 'org-todo))
  4997. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4998. "Remove fontification and activation overlays from links."
  4999. (font-lock-default-unfontify-region beg end)
  5000. (let* ((buffer-undo-list t)
  5001. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5002. (inhibit-modification-hooks t)
  5003. deactivate-mark buffer-file-name buffer-file-truename)
  5004. (remove-text-properties beg end
  5005. '(mouse-face t keymap t org-linked-text t
  5006. invisible t intangible t))))
  5007. ;;;; Visibility cycling, including org-goto and indirect buffer
  5008. ;;; Cycling
  5009. (defvar org-cycle-global-status nil)
  5010. (make-variable-buffer-local 'org-cycle-global-status)
  5011. (defvar org-cycle-subtree-status nil)
  5012. (make-variable-buffer-local 'org-cycle-subtree-status)
  5013. ;;;###autoload
  5014. (defun org-cycle (&optional arg)
  5015. "Visibility cycling for Org-mode.
  5016. - When this function is called with a prefix argument, rotate the entire
  5017. buffer through 3 states (global cycling)
  5018. 1. OVERVIEW: Show only top-level headlines.
  5019. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5020. 3. SHOW ALL: Show everything.
  5021. - When point is at the beginning of a headline, rotate the subtree started
  5022. by this line through 3 different states (local cycling)
  5023. 1. FOLDED: Only the main headline is shown.
  5024. 2. CHILDREN: The main headline and the direct children are shown.
  5025. From this state, you can move to one of the children
  5026. and zoom in further.
  5027. 3. SUBTREE: Show the entire subtree, including body text.
  5028. - When there is a numeric prefix, go up to a heading with level ARG, do
  5029. a `show-subtree' and return to the previous cursor position. If ARG
  5030. is negative, go up that many levels.
  5031. - When point is not at the beginning of a headline, execute
  5032. `indent-relative', like TAB normally does. See the option
  5033. `org-cycle-emulate-tab' for details.
  5034. - Special case: if point is at the beginning of the buffer and there is
  5035. no headline in line 1, this function will act as if called with prefix arg.
  5036. But only if also the variable `org-cycle-global-at-bob' is t."
  5037. (interactive "P")
  5038. (let* ((outline-regexp
  5039. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5040. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5041. outline-regexp))
  5042. (bob-special (and org-cycle-global-at-bob (bobp)
  5043. (not (looking-at outline-regexp))))
  5044. (org-cycle-hook
  5045. (if bob-special
  5046. (delq 'org-optimize-window-after-visibility-change
  5047. (copy-sequence org-cycle-hook))
  5048. org-cycle-hook))
  5049. (pos (point)))
  5050. (if (or bob-special (equal arg '(4)))
  5051. ;; special case: use global cycling
  5052. (setq arg t))
  5053. (cond
  5054. ((org-at-table-p 'any)
  5055. ;; Enter the table or move to the next field in the table
  5056. (or (org-table-recognize-table.el)
  5057. (progn
  5058. (if arg (org-table-edit-field t)
  5059. (org-table-justify-field-maybe)
  5060. (call-interactively 'org-table-next-field)))))
  5061. ((eq arg t) ;; Global cycling
  5062. (cond
  5063. ((and (eq last-command this-command)
  5064. (eq org-cycle-global-status 'overview))
  5065. ;; We just created the overview - now do table of contents
  5066. ;; This can be slow in very large buffers, so indicate action
  5067. (message "CONTENTS...")
  5068. (org-content)
  5069. (message "CONTENTS...done")
  5070. (setq org-cycle-global-status 'contents)
  5071. (run-hook-with-args 'org-cycle-hook 'contents))
  5072. ((and (eq last-command this-command)
  5073. (eq org-cycle-global-status 'contents))
  5074. ;; We just showed the table of contents - now show everything
  5075. (show-all)
  5076. (message "SHOW ALL")
  5077. (setq org-cycle-global-status 'all)
  5078. (run-hook-with-args 'org-cycle-hook 'all))
  5079. (t
  5080. ;; Default action: go to overview
  5081. (org-overview)
  5082. (message "OVERVIEW")
  5083. (setq org-cycle-global-status 'overview)
  5084. (run-hook-with-args 'org-cycle-hook 'overview))))
  5085. ((and org-drawers org-drawer-regexp
  5086. (save-excursion
  5087. (beginning-of-line 1)
  5088. (looking-at org-drawer-regexp)))
  5089. ;; Toggle block visibility
  5090. (org-flag-drawer
  5091. (not (get-char-property (match-end 0) 'invisible))))
  5092. ((integerp arg)
  5093. ;; Show-subtree, ARG levels up from here.
  5094. (save-excursion
  5095. (org-back-to-heading)
  5096. (outline-up-heading (if (< arg 0) (- arg)
  5097. (- (funcall outline-level) arg)))
  5098. (org-show-subtree)))
  5099. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5100. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5101. ;; At a heading: rotate between three different views
  5102. (org-back-to-heading)
  5103. (let ((goal-column 0) eoh eol eos)
  5104. ;; First, some boundaries
  5105. (save-excursion
  5106. (org-back-to-heading)
  5107. (save-excursion
  5108. (beginning-of-line 2)
  5109. (while (and (not (eobp)) ;; this is like `next-line'
  5110. (get-char-property (1- (point)) 'invisible))
  5111. (beginning-of-line 2)) (setq eol (point)))
  5112. (outline-end-of-heading) (setq eoh (point))
  5113. (org-end-of-subtree t)
  5114. (unless (eobp)
  5115. (skip-chars-forward " \t\n")
  5116. (beginning-of-line 1) ; in case this is an item
  5117. )
  5118. (setq eos (1- (point))))
  5119. ;; Find out what to do next and set `this-command'
  5120. (cond
  5121. ((= eos eoh)
  5122. ;; Nothing is hidden behind this heading
  5123. (message "EMPTY ENTRY")
  5124. (setq org-cycle-subtree-status nil)
  5125. (save-excursion
  5126. (goto-char eos)
  5127. (outline-next-heading)
  5128. (if (org-invisible-p) (org-flag-heading nil))))
  5129. ((or (>= eol eos)
  5130. (not (string-match "\\S-" (buffer-substring eol eos))))
  5131. ;; Entire subtree is hidden in one line: open it
  5132. (org-show-entry)
  5133. (show-children)
  5134. (message "CHILDREN")
  5135. (save-excursion
  5136. (goto-char eos)
  5137. (outline-next-heading)
  5138. (if (org-invisible-p) (org-flag-heading nil)))
  5139. (setq org-cycle-subtree-status 'children)
  5140. (run-hook-with-args 'org-cycle-hook 'children))
  5141. ((and (eq last-command this-command)
  5142. (eq org-cycle-subtree-status 'children))
  5143. ;; We just showed the children, now show everything.
  5144. (org-show-subtree)
  5145. (message "SUBTREE")
  5146. (setq org-cycle-subtree-status 'subtree)
  5147. (run-hook-with-args 'org-cycle-hook 'subtree))
  5148. (t
  5149. ;; Default action: hide the subtree.
  5150. (hide-subtree)
  5151. (message "FOLDED")
  5152. (setq org-cycle-subtree-status 'folded)
  5153. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5154. ;; TAB emulation
  5155. (buffer-read-only (org-back-to-heading))
  5156. ((org-try-cdlatex-tab))
  5157. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5158. (or (not (bolp))
  5159. (not (looking-at outline-regexp))))
  5160. (call-interactively (global-key-binding "\t")))
  5161. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5162. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5163. (or (and (eq org-cycle-emulate-tab 'white)
  5164. (= (match-end 0) (point-at-eol)))
  5165. (and (eq org-cycle-emulate-tab 'whitestart)
  5166. (>= (match-end 0) pos))))
  5167. t
  5168. (eq org-cycle-emulate-tab t))
  5169. ; (if (and (looking-at "[ \n\r\t]")
  5170. ; (string-match "^[ \t]*$" (buffer-substring
  5171. ; (point-at-bol) (point))))
  5172. ; (progn
  5173. ; (beginning-of-line 1)
  5174. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5175. (call-interactively (global-key-binding "\t")))
  5176. (t (save-excursion
  5177. (org-back-to-heading)
  5178. (org-cycle))))))
  5179. ;;;###autoload
  5180. (defun org-global-cycle (&optional arg)
  5181. "Cycle the global visibility. For details see `org-cycle'."
  5182. (interactive "P")
  5183. (let ((org-cycle-include-plain-lists
  5184. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5185. (if (integerp arg)
  5186. (progn
  5187. (show-all)
  5188. (hide-sublevels arg)
  5189. (setq org-cycle-global-status 'contents))
  5190. (org-cycle '(4)))))
  5191. (defun org-overview ()
  5192. "Switch to overview mode, shoing only top-level headlines.
  5193. Really, this shows all headlines with level equal or greater than the level
  5194. of the first headline in the buffer. This is important, because if the
  5195. first headline is not level one, then (hide-sublevels 1) gives confusing
  5196. results."
  5197. (interactive)
  5198. (let ((level (save-excursion
  5199. (goto-char (point-min))
  5200. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5201. (progn
  5202. (goto-char (match-beginning 0))
  5203. (funcall outline-level))))))
  5204. (and level (hide-sublevels level))))
  5205. (defun org-content (&optional arg)
  5206. "Show all headlines in the buffer, like a table of contents.
  5207. With numerical argument N, show content up to level N."
  5208. (interactive "P")
  5209. (save-excursion
  5210. ;; Visit all headings and show their offspring
  5211. (and (integerp arg) (org-overview))
  5212. (goto-char (point-max))
  5213. (catch 'exit
  5214. (while (and (progn (condition-case nil
  5215. (outline-previous-visible-heading 1)
  5216. (error (goto-char (point-min))))
  5217. t)
  5218. (looking-at outline-regexp))
  5219. (if (integerp arg)
  5220. (show-children (1- arg))
  5221. (show-branches))
  5222. (if (bobp) (throw 'exit nil))))))
  5223. (defun org-optimize-window-after-visibility-change (state)
  5224. "Adjust the window after a change in outline visibility.
  5225. This function is the default value of the hook `org-cycle-hook'."
  5226. (when (get-buffer-window (current-buffer))
  5227. (cond
  5228. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5229. ; ((eq state 'overview) (org-beginning-of-line))
  5230. ((eq state 'content) nil)
  5231. ((eq state 'all) nil)
  5232. ((eq state 'folded) nil)
  5233. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5234. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5235. (defun org-compact-display-after-subtree-move ()
  5236. (let (beg end)
  5237. (save-excursion
  5238. (if (org-up-heading-safe)
  5239. (progn
  5240. (hide-subtree)
  5241. (show-entry)
  5242. (show-children)
  5243. (org-cycle-show-empty-lines 'children)
  5244. (org-cycle-hide-drawers 'children))
  5245. (org-overview)))))
  5246. (defun org-cycle-show-empty-lines (state)
  5247. "Show empty lines above all visible headlines.
  5248. The region to be covered depends on STATE when called through
  5249. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5250. entire buffer covered. Note that an empty line is only shown if there
  5251. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5252. (when (> org-cycle-separator-lines 0)
  5253. (save-excursion
  5254. (let* ((n org-cycle-separator-lines)
  5255. (re (cond
  5256. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5257. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5258. (t (let ((ns (number-to-string (- n 2))))
  5259. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5260. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5261. beg end)
  5262. (cond
  5263. ((memq state '(overview contents t))
  5264. (setq beg (point-min) end (point-max)))
  5265. ((memq state '(children folded))
  5266. (setq beg (point) end (progn (org-end-of-subtree t t)
  5267. (beginning-of-line 2)
  5268. (point)))))
  5269. (when beg
  5270. (goto-char beg)
  5271. (while (re-search-forward re end t)
  5272. (if (not (get-char-property (match-end 1) 'invisible))
  5273. (outline-flag-region
  5274. (match-beginning 1) (match-end 1) nil)))))))
  5275. ;; Never hide empty lines at the end of the file.
  5276. (save-excursion
  5277. (goto-char (point-max))
  5278. (outline-previous-heading)
  5279. (outline-end-of-heading)
  5280. (if (and (looking-at "[ \t\n]+")
  5281. (= (match-end 0) (point-max)))
  5282. (outline-flag-region (point) (match-end 0) nil))))
  5283. (defun org-subtree-end-visible-p ()
  5284. "Is the end of the current subtree visible?"
  5285. (pos-visible-in-window-p
  5286. (save-excursion (org-end-of-subtree t) (point))))
  5287. (defun org-first-headline-recenter (&optional N)
  5288. "Move cursor to the first headline and recenter the headline.
  5289. Optional argument N means, put the headline into the Nth line of the window."
  5290. (goto-char (point-min))
  5291. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5292. (beginning-of-line)
  5293. (recenter (prefix-numeric-value N))))
  5294. ;;; Org-goto
  5295. (defvar org-goto-window-configuration nil)
  5296. (defvar org-goto-marker nil)
  5297. (defvar org-goto-map
  5298. (let ((map (make-sparse-keymap)))
  5299. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5300. (while (setq cmd (pop cmds))
  5301. (substitute-key-definition cmd cmd map global-map)))
  5302. (suppress-keymap map)
  5303. (org-defkey map "\C-m" 'org-goto-ret)
  5304. (org-defkey map [(return)] 'org-goto-ret)
  5305. (org-defkey map [(left)] 'org-goto-left)
  5306. (org-defkey map [(right)] 'org-goto-right)
  5307. (org-defkey map [(control ?g)] 'org-goto-quit)
  5308. (org-defkey map "\C-i" 'org-cycle)
  5309. (org-defkey map [(tab)] 'org-cycle)
  5310. (org-defkey map [(down)] 'outline-next-visible-heading)
  5311. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5312. (if org-goto-auto-isearch
  5313. (if (fboundp 'define-key-after)
  5314. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5315. nil)
  5316. (org-defkey map "q" 'org-goto-quit)
  5317. (org-defkey map "n" 'outline-next-visible-heading)
  5318. (org-defkey map "p" 'outline-previous-visible-heading)
  5319. (org-defkey map "f" 'outline-forward-same-level)
  5320. (org-defkey map "b" 'outline-backward-same-level)
  5321. (org-defkey map "u" 'outline-up-heading))
  5322. (org-defkey map "/" 'org-occur)
  5323. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5324. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5325. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5326. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5327. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5328. map))
  5329. (defconst org-goto-help
  5330. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5331. RET=jump to location [Q]uit and return to previous location
  5332. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5333. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5334. (defun org-goto (&optional alternative-interface)
  5335. "Look up a different location in the current file, keeping current visibility.
  5336. When you want look-up or go to a different location in a document, the
  5337. fastest way is often to fold the entire buffer and then dive into the tree.
  5338. This method has the disadvantage, that the previous location will be folded,
  5339. which may not be what you want.
  5340. This command works around this by showing a copy of the current buffer
  5341. in an indirect buffer, in overview mode. You can dive into the tree in
  5342. that copy, use org-occur and incremental search to find a location.
  5343. When pressing RET or `Q', the command returns to the original buffer in
  5344. which the visibility is still unchanged. After RET is will also jump to
  5345. the location selected in the indirect buffer and expose the
  5346. the headline hierarchy above."
  5347. (interactive "P")
  5348. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5349. (org-refile-use-outline-path t)
  5350. (interface
  5351. (if (not alternative-interface)
  5352. org-goto-interface
  5353. (if (eq org-goto-interface 'outline)
  5354. 'outline-path-completion
  5355. 'outline)))
  5356. (org-goto-start-pos (point))
  5357. (selected-point
  5358. (if (eq interface 'outline)
  5359. (car (org-get-location (current-buffer) org-goto-help))
  5360. (nth 3 (org-refile-get-location "Goto: ")))))
  5361. (if selected-point
  5362. (progn
  5363. (org-mark-ring-push org-goto-start-pos)
  5364. (goto-char selected-point)
  5365. (if (or (org-invisible-p) (org-invisible-p2))
  5366. (org-show-context 'org-goto)))
  5367. (message "Quit"))))
  5368. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5369. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5370. (defvar org-goto-local-auto-isearch-map) ; defined below
  5371. (defun org-get-location (buf help)
  5372. "Let the user select a location in the Org-mode buffer BUF.
  5373. This function uses a recursive edit. It returns the selected position
  5374. or nil."
  5375. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5376. (isearch-hide-immediately nil)
  5377. (isearch-search-fun-function
  5378. (lambda () 'org-goto-local-search-forward-headings))
  5379. (org-goto-selected-point org-goto-exit-command))
  5380. (save-excursion
  5381. (save-window-excursion
  5382. (delete-other-windows)
  5383. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5384. (switch-to-buffer
  5385. (condition-case nil
  5386. (make-indirect-buffer (current-buffer) "*org-goto*")
  5387. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5388. (with-output-to-temp-buffer "*Help*"
  5389. (princ help))
  5390. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5391. (setq buffer-read-only nil)
  5392. (let ((org-startup-truncated t)
  5393. (org-startup-folded nil)
  5394. (org-startup-align-all-tables nil))
  5395. (org-mode)
  5396. (org-overview))
  5397. (setq buffer-read-only t)
  5398. (if (and (boundp 'org-goto-start-pos)
  5399. (integer-or-marker-p org-goto-start-pos))
  5400. (let ((org-show-hierarchy-above t)
  5401. (org-show-siblings t)
  5402. (org-show-following-heading t))
  5403. (goto-char org-goto-start-pos)
  5404. (and (org-invisible-p) (org-show-context)))
  5405. (goto-char (point-min)))
  5406. (org-beginning-of-line)
  5407. (message "Select location and press RET")
  5408. (use-local-map org-goto-map)
  5409. (recursive-edit)
  5410. ))
  5411. (kill-buffer "*org-goto*")
  5412. (cons org-goto-selected-point org-goto-exit-command)))
  5413. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5414. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5415. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5416. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5417. (defun org-goto-local-search-forward-headings (string bound noerror)
  5418. "Search and make sure that anu matches are in headlines."
  5419. (catch 'return
  5420. (while (search-forward string bound noerror)
  5421. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5422. (and (member :headline context)
  5423. (not (member :tags context))))
  5424. (throw 'return (point))))))
  5425. (defun org-goto-local-auto-isearch ()
  5426. "Start isearch."
  5427. (interactive)
  5428. (goto-char (point-min))
  5429. (let ((keys (this-command-keys)))
  5430. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5431. (isearch-mode t)
  5432. (isearch-process-search-char (string-to-char keys)))))
  5433. (defun org-goto-ret (&optional arg)
  5434. "Finish `org-goto' by going to the new location."
  5435. (interactive "P")
  5436. (setq org-goto-selected-point (point)
  5437. org-goto-exit-command 'return)
  5438. (throw 'exit nil))
  5439. (defun org-goto-left ()
  5440. "Finish `org-goto' by going to the new location."
  5441. (interactive)
  5442. (if (org-on-heading-p)
  5443. (progn
  5444. (beginning-of-line 1)
  5445. (setq org-goto-selected-point (point)
  5446. org-goto-exit-command 'left)
  5447. (throw 'exit nil))
  5448. (error "Not on a heading")))
  5449. (defun org-goto-right ()
  5450. "Finish `org-goto' by going to the new location."
  5451. (interactive)
  5452. (if (org-on-heading-p)
  5453. (progn
  5454. (setq org-goto-selected-point (point)
  5455. org-goto-exit-command 'right)
  5456. (throw 'exit nil))
  5457. (error "Not on a heading")))
  5458. (defun org-goto-quit ()
  5459. "Finish `org-goto' without cursor motion."
  5460. (interactive)
  5461. (setq org-goto-selected-point nil)
  5462. (setq org-goto-exit-command 'quit)
  5463. (throw 'exit nil))
  5464. ;;; Indirect buffer display of subtrees
  5465. (defvar org-indirect-dedicated-frame nil
  5466. "This is the frame being used for indirect tree display.")
  5467. (defvar org-last-indirect-buffer nil)
  5468. (defun org-tree-to-indirect-buffer (&optional arg)
  5469. "Create indirect buffer and narrow it to current subtree.
  5470. With numerical prefix ARG, go up to this level and then take that tree.
  5471. If ARG is negative, go up that many levels.
  5472. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5473. indirect buffer previously made with this command, to avoid proliferation of
  5474. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5475. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5476. is kept so that you can work with several indirect buffers at the same time.
  5477. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5478. requests that a new frame be made for the new buffer, so that the dedicated
  5479. frame is not changed."
  5480. (interactive "P")
  5481. (let ((cbuf (current-buffer))
  5482. (cwin (selected-window))
  5483. (pos (point))
  5484. beg end level heading ibuf)
  5485. (save-excursion
  5486. (org-back-to-heading t)
  5487. (when (numberp arg)
  5488. (setq level (org-outline-level))
  5489. (if (< arg 0) (setq arg (+ level arg)))
  5490. (while (> (setq level (org-outline-level)) arg)
  5491. (outline-up-heading 1 t)))
  5492. (setq beg (point)
  5493. heading (org-get-heading))
  5494. (org-end-of-subtree t) (setq end (point)))
  5495. (if (and (buffer-live-p org-last-indirect-buffer)
  5496. (not (eq org-indirect-buffer-display 'new-frame))
  5497. (not arg))
  5498. (kill-buffer org-last-indirect-buffer))
  5499. (setq ibuf (org-get-indirect-buffer cbuf)
  5500. org-last-indirect-buffer ibuf)
  5501. (cond
  5502. ((or (eq org-indirect-buffer-display 'new-frame)
  5503. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5504. (select-frame (make-frame))
  5505. (delete-other-windows)
  5506. (switch-to-buffer ibuf)
  5507. (org-set-frame-title heading))
  5508. ((eq org-indirect-buffer-display 'dedicated-frame)
  5509. (raise-frame
  5510. (select-frame (or (and org-indirect-dedicated-frame
  5511. (frame-live-p org-indirect-dedicated-frame)
  5512. org-indirect-dedicated-frame)
  5513. (setq org-indirect-dedicated-frame (make-frame)))))
  5514. (delete-other-windows)
  5515. (switch-to-buffer ibuf)
  5516. (org-set-frame-title (concat "Indirect: " heading)))
  5517. ((eq org-indirect-buffer-display 'current-window)
  5518. (switch-to-buffer ibuf))
  5519. ((eq org-indirect-buffer-display 'other-window)
  5520. (pop-to-buffer ibuf))
  5521. (t (error "Invalid value.")))
  5522. (if (featurep 'xemacs)
  5523. (save-excursion (org-mode) (turn-on-font-lock)))
  5524. (narrow-to-region beg end)
  5525. (show-all)
  5526. (goto-char pos)
  5527. (and (window-live-p cwin) (select-window cwin))))
  5528. (defun org-get-indirect-buffer (&optional buffer)
  5529. (setq buffer (or buffer (current-buffer)))
  5530. (let ((n 1) (base (buffer-name buffer)) bname)
  5531. (while (buffer-live-p
  5532. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5533. (setq n (1+ n)))
  5534. (condition-case nil
  5535. (make-indirect-buffer buffer bname 'clone)
  5536. (error (make-indirect-buffer buffer bname)))))
  5537. (defun org-set-frame-title (title)
  5538. "Set the title of the current frame to the string TITLE."
  5539. ;; FIXME: how to name a single frame in XEmacs???
  5540. (unless (featurep 'xemacs)
  5541. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5542. ;;;; Structure editing
  5543. ;;; Inserting headlines
  5544. (defun org-insert-heading (&optional force-heading)
  5545. "Insert a new heading or item with same depth at point.
  5546. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5547. If point is at the beginning of a headline, insert a sibling before the
  5548. current headline. If point is not at the beginning, do not split the line,
  5549. but create the new hedline after the current line."
  5550. (interactive "P")
  5551. (if (= (buffer-size) 0)
  5552. (insert "\n* ")
  5553. (when (or force-heading (not (org-insert-item)))
  5554. (let* ((head (save-excursion
  5555. (condition-case nil
  5556. (progn
  5557. (org-back-to-heading)
  5558. (match-string 0))
  5559. (error "*"))))
  5560. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5561. pos)
  5562. (cond
  5563. ((and (org-on-heading-p) (bolp)
  5564. (or (bobp)
  5565. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5566. ;; insert before the current line
  5567. (open-line (if blank 2 1)))
  5568. ((and (bolp)
  5569. (or (bobp)
  5570. (save-excursion
  5571. (backward-char 1) (not (org-invisible-p)))))
  5572. ;; insert right here
  5573. nil)
  5574. (t
  5575. ; ;; in the middle of the line
  5576. ; (org-show-entry)
  5577. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5578. ; (if (and
  5579. ; (org-on-heading-p)
  5580. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5581. ; ;; protect the tags
  5582. ;; (let ((tags (match-string 2)) pos)
  5583. ; (delete-region (match-beginning 1) (match-end 1))
  5584. ; (setq pos (point-at-bol))
  5585. ; (newline (if blank 2 1))
  5586. ; (save-excursion
  5587. ; (goto-char pos)
  5588. ; (end-of-line 1)
  5589. ; (insert " " tags)
  5590. ; (org-set-tags nil 'align)))
  5591. ; (newline (if blank 2 1)))
  5592. ; (newline (if blank 2 1))))
  5593. ;; in the middle of the line
  5594. (org-show-entry)
  5595. (let ((split
  5596. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5597. tags pos)
  5598. (if (org-on-heading-p)
  5599. (progn
  5600. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5601. (setq tags (and (match-end 2) (match-string 2)))
  5602. (and (match-end 1)
  5603. (delete-region (match-beginning 1) (match-end 1)))
  5604. (setq pos (point-at-bol))
  5605. (or split (end-of-line 1))
  5606. (delete-horizontal-space)
  5607. (newline (if blank 2 1))
  5608. (when tags
  5609. (save-excursion
  5610. (goto-char pos)
  5611. (end-of-line 1)
  5612. (insert " " tags)
  5613. (org-set-tags nil 'align))))
  5614. (or split (end-of-line 1))
  5615. (newline (if blank 2 1))))))
  5616. (insert head) (just-one-space)
  5617. (setq pos (point))
  5618. (end-of-line 1)
  5619. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5620. (run-hooks 'org-insert-heading-hook)))))
  5621. (defun org-insert-heading-after-current ()
  5622. "Insert a new heading with same level as current, after current subtree."
  5623. (interactive)
  5624. (org-back-to-heading)
  5625. (org-insert-heading)
  5626. (org-move-subtree-down)
  5627. (end-of-line 1))
  5628. (defun org-insert-todo-heading (arg)
  5629. "Insert a new heading with the same level and TODO state as current heading.
  5630. If the heading has no TODO state, or if the state is DONE, use the first
  5631. state (TODO by default). Also with prefix arg, force first state."
  5632. (interactive "P")
  5633. (when (not (org-insert-item 'checkbox))
  5634. (org-insert-heading)
  5635. (save-excursion
  5636. (org-back-to-heading)
  5637. (outline-previous-heading)
  5638. (looking-at org-todo-line-regexp))
  5639. (if (or arg
  5640. (not (match-beginning 2))
  5641. (member (match-string 2) org-done-keywords))
  5642. (insert (car org-todo-keywords-1) " ")
  5643. (insert (match-string 2) " "))))
  5644. (defun org-insert-subheading (arg)
  5645. "Insert a new subheading and demote it.
  5646. Works for outline headings and for plain lists alike."
  5647. (interactive "P")
  5648. (org-insert-heading arg)
  5649. (cond
  5650. ((org-on-heading-p) (org-do-demote))
  5651. ((org-at-item-p) (org-indent-item 1))))
  5652. (defun org-insert-todo-subheading (arg)
  5653. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5654. Works for outline headings and for plain lists alike."
  5655. (interactive "P")
  5656. (org-insert-todo-heading arg)
  5657. (cond
  5658. ((org-on-heading-p) (org-do-demote))
  5659. ((org-at-item-p) (org-indent-item 1))))
  5660. ;;; Promotion and Demotion
  5661. (defun org-promote-subtree ()
  5662. "Promote the entire subtree.
  5663. See also `org-promote'."
  5664. (interactive)
  5665. (save-excursion
  5666. (org-map-tree 'org-promote))
  5667. (org-fix-position-after-promote))
  5668. (defun org-demote-subtree ()
  5669. "Demote the entire subtree. See `org-demote'.
  5670. See also `org-promote'."
  5671. (interactive)
  5672. (save-excursion
  5673. (org-map-tree 'org-demote))
  5674. (org-fix-position-after-promote))
  5675. (defun org-do-promote ()
  5676. "Promote the current heading higher up the tree.
  5677. If the region is active in `transient-mark-mode', promote all headings
  5678. in the region."
  5679. (interactive)
  5680. (save-excursion
  5681. (if (org-region-active-p)
  5682. (org-map-region 'org-promote (region-beginning) (region-end))
  5683. (org-promote)))
  5684. (org-fix-position-after-promote))
  5685. (defun org-do-demote ()
  5686. "Demote the current heading lower down the tree.
  5687. If the region is active in `transient-mark-mode', demote all headings
  5688. in the region."
  5689. (interactive)
  5690. (save-excursion
  5691. (if (org-region-active-p)
  5692. (org-map-region 'org-demote (region-beginning) (region-end))
  5693. (org-demote)))
  5694. (org-fix-position-after-promote))
  5695. (defun org-fix-position-after-promote ()
  5696. "Make sure that after pro/demotion cursor position is right."
  5697. (let ((pos (point)))
  5698. (when (save-excursion
  5699. (beginning-of-line 1)
  5700. (looking-at org-todo-line-regexp)
  5701. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5702. (cond ((eobp) (insert " "))
  5703. ((eolp) (insert " "))
  5704. ((equal (char-after) ?\ ) (forward-char 1))))))
  5705. (defun org-reduced-level (l)
  5706. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5707. (defun org-get-legal-level (level &optional change)
  5708. "Rectify a level change under the influence of `org-odd-levels-only'
  5709. LEVEL is a current level, CHANGE is by how much the level should be
  5710. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5711. even level numbers will become the next higher odd number."
  5712. (if org-odd-levels-only
  5713. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5714. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5715. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5716. (max 1 (+ level change))))
  5717. (defun org-promote ()
  5718. "Promote the current heading higher up the tree.
  5719. If the region is active in `transient-mark-mode', promote all headings
  5720. in the region."
  5721. (org-back-to-heading t)
  5722. (let* ((level (save-match-data (funcall outline-level)))
  5723. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5724. (diff (abs (- level (length up-head) -1))))
  5725. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5726. (replace-match up-head nil t)
  5727. ;; Fixup tag positioning
  5728. (and org-auto-align-tags (org-set-tags nil t))
  5729. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5730. (defun org-demote ()
  5731. "Demote the current heading lower down the tree.
  5732. If the region is active in `transient-mark-mode', demote all headings
  5733. in the region."
  5734. (org-back-to-heading t)
  5735. (let* ((level (save-match-data (funcall outline-level)))
  5736. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5737. (diff (abs (- level (length down-head) -1))))
  5738. (replace-match down-head nil t)
  5739. ;; Fixup tag positioning
  5740. (and org-auto-align-tags (org-set-tags nil t))
  5741. (if org-adapt-indentation (org-fixup-indentation diff))))
  5742. (defun org-map-tree (fun)
  5743. "Call FUN for every heading underneath the current one."
  5744. (org-back-to-heading)
  5745. (let ((level (funcall outline-level)))
  5746. (save-excursion
  5747. (funcall fun)
  5748. (while (and (progn
  5749. (outline-next-heading)
  5750. (> (funcall outline-level) level))
  5751. (not (eobp)))
  5752. (funcall fun)))))
  5753. (defun org-map-region (fun beg end)
  5754. "Call FUN for every heading between BEG and END."
  5755. (let ((org-ignore-region t))
  5756. (save-excursion
  5757. (setq end (copy-marker end))
  5758. (goto-char beg)
  5759. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5760. (< (point) end))
  5761. (funcall fun))
  5762. (while (and (progn
  5763. (outline-next-heading)
  5764. (< (point) end))
  5765. (not (eobp)))
  5766. (funcall fun)))))
  5767. (defun org-fixup-indentation (diff)
  5768. "Change the indentation in the current entry by DIFF
  5769. However, if any line in the current entry has no indentation, or if it
  5770. would end up with no indentation after the change, nothing at all is done."
  5771. (save-excursion
  5772. (let ((end (save-excursion (outline-next-heading)
  5773. (point-marker)))
  5774. (prohibit (if (> diff 0)
  5775. "^\\S-"
  5776. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5777. col)
  5778. (unless (save-excursion (end-of-line 1)
  5779. (re-search-forward prohibit end t))
  5780. (while (and (< (point) end)
  5781. (re-search-forward "^[ \t]+" end t))
  5782. (goto-char (match-end 0))
  5783. (setq col (current-column))
  5784. (if (< diff 0) (replace-match ""))
  5785. (indent-to (+ diff col))))
  5786. (move-marker end nil))))
  5787. (defun org-convert-to-odd-levels ()
  5788. "Convert an org-mode file with all levels allowed to one with odd levels.
  5789. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5790. level 5 etc."
  5791. (interactive)
  5792. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5793. (let ((org-odd-levels-only nil) n)
  5794. (save-excursion
  5795. (goto-char (point-min))
  5796. (while (re-search-forward "^\\*\\*+ " nil t)
  5797. (setq n (- (length (match-string 0)) 2))
  5798. (while (>= (setq n (1- n)) 0)
  5799. (org-demote))
  5800. (end-of-line 1))))))
  5801. (defun org-convert-to-oddeven-levels ()
  5802. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5803. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5804. section with an even level, conversion would destroy the structure of the file. An error
  5805. is signaled in this case."
  5806. (interactive)
  5807. (goto-char (point-min))
  5808. ;; First check if there are no even levels
  5809. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5810. (org-show-context t)
  5811. (error "Not all levels are odd in this file. Conversion not possible."))
  5812. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5813. (let ((org-odd-levels-only nil) n)
  5814. (save-excursion
  5815. (goto-char (point-min))
  5816. (while (re-search-forward "^\\*\\*+ " nil t)
  5817. (setq n (/ (1- (length (match-string 0))) 2))
  5818. (while (>= (setq n (1- n)) 0)
  5819. (org-promote))
  5820. (end-of-line 1))))))
  5821. (defun org-tr-level (n)
  5822. "Make N odd if required."
  5823. (if org-odd-levels-only (1+ (/ n 2)) n))
  5824. ;;; Vertical tree motion, cutting and pasting of subtrees
  5825. (defun org-move-subtree-up (&optional arg)
  5826. "Move the current subtree up past ARG headlines of the same level."
  5827. (interactive "p")
  5828. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5829. (defun org-move-subtree-down (&optional arg)
  5830. "Move the current subtree down past ARG headlines of the same level."
  5831. (interactive "p")
  5832. (setq arg (prefix-numeric-value arg))
  5833. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5834. 'outline-get-last-sibling))
  5835. (ins-point (make-marker))
  5836. (cnt (abs arg))
  5837. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5838. ;; Select the tree
  5839. (org-back-to-heading)
  5840. (setq beg0 (point))
  5841. (save-excursion
  5842. (setq ne-beg (org-back-over-empty-lines))
  5843. (setq beg (point)))
  5844. (save-match-data
  5845. (save-excursion (outline-end-of-heading)
  5846. (setq folded (org-invisible-p)))
  5847. (outline-end-of-subtree))
  5848. (outline-next-heading)
  5849. (setq ne-end (org-back-over-empty-lines))
  5850. (setq end (point))
  5851. (goto-char beg0)
  5852. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5853. ;; include less whitespace
  5854. (save-excursion
  5855. (goto-char beg)
  5856. (forward-line (- ne-beg ne-end))
  5857. (setq beg (point))))
  5858. ;; Find insertion point, with error handling
  5859. (while (> cnt 0)
  5860. (or (and (funcall movfunc) (looking-at outline-regexp))
  5861. (progn (goto-char beg0)
  5862. (error "Cannot move past superior level or buffer limit")))
  5863. (setq cnt (1- cnt)))
  5864. (if (> arg 0)
  5865. ;; Moving forward - still need to move over subtree
  5866. (progn (org-end-of-subtree t t)
  5867. (save-excursion
  5868. (org-back-over-empty-lines)
  5869. (or (bolp) (newline)))))
  5870. (setq ne-ins (org-back-over-empty-lines))
  5871. (move-marker ins-point (point))
  5872. (setq txt (buffer-substring beg end))
  5873. (delete-region beg end)
  5874. (outline-flag-region (1- beg) beg nil)
  5875. (outline-flag-region (1- (point)) (point) nil)
  5876. (insert txt)
  5877. (or (bolp) (insert "\n"))
  5878. (setq ins-end (point))
  5879. (goto-char ins-point)
  5880. (org-skip-whitespace)
  5881. (when (and (< arg 0)
  5882. (org-first-sibling-p)
  5883. (> ne-ins ne-beg))
  5884. ;; Move whitespace back to beginning
  5885. (save-excursion
  5886. (goto-char ins-end)
  5887. (let ((kill-whole-line t))
  5888. (kill-line (- ne-ins ne-beg)) (point)))
  5889. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5890. (move-marker ins-point nil)
  5891. (org-compact-display-after-subtree-move)
  5892. (unless folded
  5893. (org-show-entry)
  5894. (show-children)
  5895. (org-cycle-hide-drawers 'children))))
  5896. (defvar org-subtree-clip ""
  5897. "Clipboard for cut and paste of subtrees.
  5898. This is actually only a copy of the kill, because we use the normal kill
  5899. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5900. (defvar org-subtree-clip-folded nil
  5901. "Was the last copied subtree folded?
  5902. This is used to fold the tree back after pasting.")
  5903. (defun org-cut-subtree (&optional n)
  5904. "Cut the current subtree into the clipboard.
  5905. With prefix arg N, cut this many sequential subtrees.
  5906. This is a short-hand for marking the subtree and then cutting it."
  5907. (interactive "p")
  5908. (org-copy-subtree n 'cut))
  5909. (defun org-copy-subtree (&optional n cut)
  5910. "Cut the current subtree into the clipboard.
  5911. With prefix arg N, cut this many sequential subtrees.
  5912. This is a short-hand for marking the subtree and then copying it.
  5913. If CUT is non-nil, actually cut the subtree."
  5914. (interactive "p")
  5915. (let (beg end folded (beg0 (point)))
  5916. (if (interactive-p)
  5917. (org-back-to-heading nil) ; take what looks like a subtree
  5918. (org-back-to-heading t)) ; take what is really there
  5919. (org-back-over-empty-lines)
  5920. (setq beg (point))
  5921. (skip-chars-forward " \t\r\n")
  5922. (save-match-data
  5923. (save-excursion (outline-end-of-heading)
  5924. (setq folded (org-invisible-p)))
  5925. (condition-case nil
  5926. (outline-forward-same-level (1- n))
  5927. (error nil))
  5928. (org-end-of-subtree t t))
  5929. (org-back-over-empty-lines)
  5930. (setq end (point))
  5931. (goto-char beg0)
  5932. (when (> end beg)
  5933. (setq org-subtree-clip-folded folded)
  5934. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5935. (setq org-subtree-clip (current-kill 0))
  5936. (message "%s: Subtree(s) with %d characters"
  5937. (if cut "Cut" "Copied")
  5938. (length org-subtree-clip)))))
  5939. (defun org-paste-subtree (&optional level tree)
  5940. "Paste the clipboard as a subtree, with modification of headline level.
  5941. The entire subtree is promoted or demoted in order to match a new headline
  5942. level. By default, the new level is derived from the visible headings
  5943. before and after the insertion point, and taken to be the inferior headline
  5944. level of the two. So if the previous visible heading is level 3 and the
  5945. next is level 4 (or vice versa), level 4 will be used for insertion.
  5946. This makes sure that the subtree remains an independent subtree and does
  5947. not swallow low level entries.
  5948. You can also force a different level, either by using a numeric prefix
  5949. argument, or by inserting the heading marker by hand. For example, if the
  5950. cursor is after \"*****\", then the tree will be shifted to level 5.
  5951. If you want to insert the tree as is, just use \\[yank].
  5952. If optional TREE is given, use this text instead of the kill ring."
  5953. (interactive "P")
  5954. (unless (org-kill-is-subtree-p tree)
  5955. (error "%s"
  5956. (substitute-command-keys
  5957. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5958. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5959. (^re (concat "^\\(" outline-regexp "\\)"))
  5960. (re (concat "\\(" outline-regexp "\\)"))
  5961. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5962. (old-level (if (string-match ^re txt)
  5963. (- (match-end 0) (match-beginning 0) 1)
  5964. -1))
  5965. (force-level (cond (level (prefix-numeric-value level))
  5966. ((string-match
  5967. ^re_ (buffer-substring (point-at-bol) (point)))
  5968. (- (match-end 1) (match-beginning 1)))
  5969. (t nil)))
  5970. (previous-level (save-excursion
  5971. (condition-case nil
  5972. (progn
  5973. (outline-previous-visible-heading 1)
  5974. (if (looking-at re)
  5975. (- (match-end 0) (match-beginning 0) 1)
  5976. 1))
  5977. (error 1))))
  5978. (next-level (save-excursion
  5979. (condition-case nil
  5980. (progn
  5981. (or (looking-at outline-regexp)
  5982. (outline-next-visible-heading 1))
  5983. (if (looking-at re)
  5984. (- (match-end 0) (match-beginning 0) 1)
  5985. 1))
  5986. (error 1))))
  5987. (new-level (or force-level (max previous-level next-level)))
  5988. (shift (if (or (= old-level -1)
  5989. (= new-level -1)
  5990. (= old-level new-level))
  5991. 0
  5992. (- new-level old-level)))
  5993. (delta (if (> shift 0) -1 1))
  5994. (func (if (> shift 0) 'org-demote 'org-promote))
  5995. (org-odd-levels-only nil)
  5996. beg end)
  5997. ;; Remove the forced level indicator
  5998. (if force-level
  5999. (delete-region (point-at-bol) (point)))
  6000. ;; Paste
  6001. (beginning-of-line 1)
  6002. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6003. (setq beg (point))
  6004. (insert-before-markers txt) ;; FIXME: correct fix????
  6005. (unless (string-match "\n\\'" txt) (insert "\n"))
  6006. (setq end (point))
  6007. (goto-char beg)
  6008. (skip-chars-forward " \t\n\r")
  6009. (setq beg (point))
  6010. ;; Shift if necessary
  6011. (unless (= shift 0)
  6012. (save-restriction
  6013. (narrow-to-region beg end)
  6014. (while (not (= shift 0))
  6015. (org-map-region func (point-min) (point-max))
  6016. (setq shift (+ delta shift)))
  6017. (goto-char (point-min))))
  6018. (when (interactive-p)
  6019. (message "Clipboard pasted as level %d subtree" new-level))
  6020. (if (and kill-ring
  6021. (eq org-subtree-clip (current-kill 0))
  6022. org-subtree-clip-folded)
  6023. ;; The tree was folded before it was killed/copied
  6024. (hide-subtree))))
  6025. (defun org-kill-is-subtree-p (&optional txt)
  6026. "Check if the current kill is an outline subtree, or a set of trees.
  6027. Returns nil if kill does not start with a headline, or if the first
  6028. headline level is not the largest headline level in the tree.
  6029. So this will actually accept several entries of equal levels as well,
  6030. which is OK for `org-paste-subtree'.
  6031. If optional TXT is given, check this string instead of the current kill."
  6032. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6033. (start-level (and kill
  6034. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6035. org-outline-regexp "\\)")
  6036. kill)
  6037. (- (match-end 2) (match-beginning 2) 1)))
  6038. (re (concat "^" org-outline-regexp))
  6039. (start (1+ (match-beginning 2))))
  6040. (if (not start-level)
  6041. (progn
  6042. nil) ;; does not even start with a heading
  6043. (catch 'exit
  6044. (while (setq start (string-match re kill (1+ start)))
  6045. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6046. (throw 'exit nil)))
  6047. t))))
  6048. (defun org-narrow-to-subtree ()
  6049. "Narrow buffer to the current subtree."
  6050. (interactive)
  6051. (save-excursion
  6052. (save-match-data
  6053. (narrow-to-region
  6054. (progn (org-back-to-heading) (point))
  6055. (progn (org-end-of-subtree t t) (point))))))
  6056. ;;; Outline Sorting
  6057. (defun org-sort (with-case)
  6058. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6059. Optional argument WITH-CASE means sort case-sensitively."
  6060. (interactive "P")
  6061. (if (org-at-table-p)
  6062. (org-call-with-arg 'org-table-sort-lines with-case)
  6063. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6064. (defvar org-priority-regexp) ; defined later in the file
  6065. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6066. "Sort entries on a certain level of an outline tree.
  6067. If there is an active region, the entries in the region are sorted.
  6068. Else, if the cursor is before the first entry, sort the top-level items.
  6069. Else, the children of the entry at point are sorted.
  6070. Sorting can be alphabetically, numerically, and by date/time as given by
  6071. the first time stamp in the entry. The command prompts for the sorting
  6072. type unless it has been given to the function through the SORTING-TYPE
  6073. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6074. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6075. called with point at the beginning of the record. It must return either
  6076. a string or a number that should serve as the sorting key for that record.
  6077. Comparing entries ignores case by default. However, with an optional argument
  6078. WITH-CASE, the sorting considers case as well."
  6079. (interactive "P")
  6080. (let ((case-func (if with-case 'identity 'downcase))
  6081. start beg end stars re re2
  6082. txt what tmp plain-list-p)
  6083. ;; Find beginning and end of region to sort
  6084. (cond
  6085. ((org-region-active-p)
  6086. ;; we will sort the region
  6087. (setq end (region-end)
  6088. what "region")
  6089. (goto-char (region-beginning))
  6090. (if (not (org-on-heading-p)) (outline-next-heading))
  6091. (setq start (point)))
  6092. ((org-at-item-p)
  6093. ;; we will sort this plain list
  6094. (org-beginning-of-item-list) (setq start (point))
  6095. (org-end-of-item-list) (setq end (point))
  6096. (goto-char start)
  6097. (setq plain-list-p t
  6098. what "plain list"))
  6099. ((or (org-on-heading-p)
  6100. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6101. ;; we will sort the children of the current headline
  6102. (org-back-to-heading)
  6103. (setq start (point)
  6104. end (progn (org-end-of-subtree t t)
  6105. (org-back-over-empty-lines)
  6106. (point))
  6107. what "children")
  6108. (goto-char start)
  6109. (show-subtree)
  6110. (outline-next-heading))
  6111. (t
  6112. ;; we will sort the top-level entries in this file
  6113. (goto-char (point-min))
  6114. (or (org-on-heading-p) (outline-next-heading))
  6115. (setq start (point) end (point-max) what "top-level")
  6116. (goto-char start)
  6117. (show-all)))
  6118. (setq beg (point))
  6119. (if (>= beg end) (error "Nothing to sort"))
  6120. (unless plain-list-p
  6121. (looking-at "\\(\\*+\\)")
  6122. (setq stars (match-string 1)
  6123. re (concat "^" (regexp-quote stars) " +")
  6124. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6125. txt (buffer-substring beg end))
  6126. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6127. (if (and (not (equal stars "*")) (string-match re2 txt))
  6128. (error "Region to sort contains a level above the first entry")))
  6129. (unless sorting-type
  6130. (message
  6131. (if plain-list-p
  6132. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6133. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6134. what)
  6135. (setq sorting-type (read-char-exclusive))
  6136. (and (= (downcase sorting-type) ?f)
  6137. (setq getkey-func
  6138. (completing-read "Sort using function: "
  6139. obarray 'fboundp t nil nil))
  6140. (setq getkey-func (intern getkey-func)))
  6141. (and (= (downcase sorting-type) ?r)
  6142. (setq property
  6143. (completing-read "Property: "
  6144. (mapcar 'list (org-buffer-property-keys t))
  6145. nil t))))
  6146. (message "Sorting entries...")
  6147. (save-restriction
  6148. (narrow-to-region start end)
  6149. (let ((dcst (downcase sorting-type))
  6150. (now (current-time)))
  6151. (sort-subr
  6152. (/= dcst sorting-type)
  6153. ;; This function moves to the beginning character of the "record" to
  6154. ;; be sorted.
  6155. (if plain-list-p
  6156. (lambda nil
  6157. (if (org-at-item-p) t (goto-char (point-max))))
  6158. (lambda nil
  6159. (if (re-search-forward re nil t)
  6160. (goto-char (match-beginning 0))
  6161. (goto-char (point-max)))))
  6162. ;; This function moves to the last character of the "record" being
  6163. ;; sorted.
  6164. (if plain-list-p
  6165. 'org-end-of-item
  6166. (lambda nil
  6167. (save-match-data
  6168. (condition-case nil
  6169. (outline-forward-same-level 1)
  6170. (error
  6171. (goto-char (point-max)))))))
  6172. ;; This function returns the value that gets sorted against.
  6173. (if plain-list-p
  6174. (lambda nil
  6175. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6176. (cond
  6177. ((= dcst ?n)
  6178. (string-to-number (buffer-substring (match-end 0)
  6179. (point-at-eol))))
  6180. ((= dcst ?a)
  6181. (buffer-substring (match-end 0) (point-at-eol)))
  6182. ((= dcst ?t)
  6183. (if (re-search-forward org-ts-regexp
  6184. (point-at-eol) t)
  6185. (org-time-string-to-time (match-string 0))
  6186. now))
  6187. ((= dcst ?f)
  6188. (if getkey-func
  6189. (progn
  6190. (setq tmp (funcall getkey-func))
  6191. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6192. tmp)
  6193. (error "Invalid key function `%s'" getkey-func)))
  6194. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6195. (lambda nil
  6196. (cond
  6197. ((= dcst ?n)
  6198. (if (looking-at outline-regexp)
  6199. (string-to-number (buffer-substring (match-end 0)
  6200. (point-at-eol)))
  6201. nil))
  6202. ((= dcst ?a)
  6203. (funcall case-func (buffer-substring (point-at-bol)
  6204. (point-at-eol))))
  6205. ((= dcst ?t)
  6206. (if (re-search-forward org-ts-regexp
  6207. (save-excursion
  6208. (forward-line 2)
  6209. (point)) t)
  6210. (org-time-string-to-time (match-string 0))
  6211. now))
  6212. ((= dcst ?p)
  6213. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6214. (string-to-char (match-string 2))
  6215. org-default-priority))
  6216. ((= dcst ?r)
  6217. (or (org-entry-get nil property) ""))
  6218. ((= dcst ?f)
  6219. (if getkey-func
  6220. (progn
  6221. (setq tmp (funcall getkey-func))
  6222. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6223. tmp)
  6224. (error "Invalid key function `%s'" getkey-func)))
  6225. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6226. nil
  6227. (cond
  6228. ((= dcst ?a) 'string<)
  6229. ((= dcst ?t) 'time-less-p)
  6230. (t nil)))))
  6231. (message "Sorting entries...done")))
  6232. (defun org-do-sort (table what &optional with-case sorting-type)
  6233. "Sort TABLE of WHAT according to SORTING-TYPE.
  6234. The user will be prompted for the SORTING-TYPE if the call to this
  6235. function does not specify it. WHAT is only for the prompt, to indicate
  6236. what is being sorted. The sorting key will be extracted from
  6237. the car of the elements of the table.
  6238. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6239. (unless sorting-type
  6240. (message
  6241. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6242. what)
  6243. (setq sorting-type (read-char-exclusive)))
  6244. (let ((dcst (downcase sorting-type))
  6245. extractfun comparefun)
  6246. ;; Define the appropriate functions
  6247. (cond
  6248. ((= dcst ?n)
  6249. (setq extractfun 'string-to-number
  6250. comparefun (if (= dcst sorting-type) '< '>)))
  6251. ((= dcst ?a)
  6252. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6253. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6254. comparefun (if (= dcst sorting-type)
  6255. 'string<
  6256. (lambda (a b) (and (not (string< a b))
  6257. (not (string= a b)))))))
  6258. ((= dcst ?t)
  6259. (setq extractfun
  6260. (lambda (x)
  6261. (if (string-match org-ts-regexp x)
  6262. (time-to-seconds
  6263. (org-time-string-to-time (match-string 0 x)))
  6264. 0))
  6265. comparefun (if (= dcst sorting-type) '< '>)))
  6266. (t (error "Invalid sorting type `%c'" sorting-type)))
  6267. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6268. table)
  6269. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6270. ;;;; Plain list items, including checkboxes
  6271. ;;; Plain list items
  6272. (defun org-at-item-p ()
  6273. "Is point in a line starting a hand-formatted item?"
  6274. (let ((llt org-plain-list-ordered-item-terminator))
  6275. (save-excursion
  6276. (goto-char (point-at-bol))
  6277. (looking-at
  6278. (cond
  6279. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6280. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6281. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6282. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6283. (defun org-in-item-p ()
  6284. "It the cursor inside a plain list item.
  6285. Does not have to be the first line."
  6286. (save-excursion
  6287. (condition-case nil
  6288. (progn
  6289. (org-beginning-of-item)
  6290. (org-at-item-p)
  6291. t)
  6292. (error nil))))
  6293. (defun org-insert-item (&optional checkbox)
  6294. "Insert a new item at the current level.
  6295. Return t when things worked, nil when we are not in an item."
  6296. (when (save-excursion
  6297. (condition-case nil
  6298. (progn
  6299. (org-beginning-of-item)
  6300. (org-at-item-p)
  6301. (if (org-invisible-p) (error "Invisible item"))
  6302. t)
  6303. (error nil)))
  6304. (let* ((bul (match-string 0))
  6305. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6306. (match-end 0)))
  6307. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6308. pos)
  6309. (cond
  6310. ((and (org-at-item-p) (<= (point) eow))
  6311. ;; before the bullet
  6312. (beginning-of-line 1)
  6313. (open-line (if blank 2 1)))
  6314. ((<= (point) eow)
  6315. (beginning-of-line 1))
  6316. (t
  6317. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6318. (end-of-line 1)
  6319. (delete-horizontal-space))
  6320. (newline (if blank 2 1))))
  6321. (insert bul (if checkbox "[ ]" ""))
  6322. (just-one-space)
  6323. (setq pos (point))
  6324. (end-of-line 1)
  6325. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6326. (org-maybe-renumber-ordered-list)
  6327. (and checkbox (org-update-checkbox-count-maybe))
  6328. t))
  6329. ;;; Checkboxes
  6330. (defun org-at-item-checkbox-p ()
  6331. "Is point at a line starting a plain-list item with a checklet?"
  6332. (and (org-at-item-p)
  6333. (save-excursion
  6334. (goto-char (match-end 0))
  6335. (skip-chars-forward " \t")
  6336. (looking-at "\\[[- X]\\]"))))
  6337. (defun org-toggle-checkbox (&optional arg)
  6338. "Toggle the checkbox in the current line."
  6339. (interactive "P")
  6340. (catch 'exit
  6341. (let (beg end status (firstnew 'unknown))
  6342. (cond
  6343. ((org-region-active-p)
  6344. (setq beg (region-beginning) end (region-end)))
  6345. ((org-on-heading-p)
  6346. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6347. ((org-at-item-checkbox-p)
  6348. (let ((pos (point)))
  6349. (replace-match
  6350. (cond (arg "[-]")
  6351. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6352. (t "[ ]"))
  6353. t t)
  6354. (goto-char pos))
  6355. (throw 'exit t))
  6356. (t (error "Not at a checkbox or heading, and no active region")))
  6357. (save-excursion
  6358. (goto-char beg)
  6359. (while (< (point) end)
  6360. (when (org-at-item-checkbox-p)
  6361. (setq status (equal (match-string 0) "[X]"))
  6362. (when (eq firstnew 'unknown)
  6363. (setq firstnew (not status)))
  6364. (replace-match
  6365. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6366. (beginning-of-line 2)))))
  6367. (org-update-checkbox-count-maybe))
  6368. (defun org-update-checkbox-count-maybe ()
  6369. "Update checkbox statistics unless turned off by user."
  6370. (when org-provide-checkbox-statistics
  6371. (org-update-checkbox-count)))
  6372. (defun org-update-checkbox-count (&optional all)
  6373. "Update the checkbox statistics in the current section.
  6374. This will find all statistic cookies like [57%] and [6/12] and update them
  6375. with the current numbers. With optional prefix argument ALL, do this for
  6376. the whole buffer."
  6377. (interactive "P")
  6378. (save-excursion
  6379. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6380. (beg (condition-case nil
  6381. (progn (outline-back-to-heading) (point))
  6382. (error (point-min))))
  6383. (end (move-marker (make-marker)
  6384. (progn (outline-next-heading) (point))))
  6385. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6386. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6387. (re-find (concat re "\\|" re-box))
  6388. beg-cookie end-cookie is-percent c-on c-off lim
  6389. eline curr-ind next-ind continue-from startsearch
  6390. (cstat 0)
  6391. )
  6392. (when all
  6393. (goto-char (point-min))
  6394. (outline-next-heading)
  6395. (setq beg (point) end (point-max)))
  6396. (goto-char end)
  6397. ;; find each statistic cookie
  6398. (while (re-search-backward re-find beg t)
  6399. (setq beg-cookie (match-beginning 1)
  6400. end-cookie (match-end 1)
  6401. cstat (+ cstat (if end-cookie 1 0))
  6402. startsearch (point-at-eol)
  6403. continue-from (point-at-bol)
  6404. is-percent (match-beginning 2)
  6405. lim (cond
  6406. ((org-on-heading-p) (outline-next-heading) (point))
  6407. ((org-at-item-p) (org-end-of-item) (point))
  6408. (t nil))
  6409. c-on 0
  6410. c-off 0)
  6411. (when lim
  6412. ;; find first checkbox for this cookie and gather
  6413. ;; statistics from all that are at this indentation level
  6414. (goto-char startsearch)
  6415. (if (re-search-forward re-box lim t)
  6416. (progn
  6417. (org-beginning-of-item)
  6418. (setq curr-ind (org-get-indentation))
  6419. (setq next-ind curr-ind)
  6420. (while (= curr-ind next-ind)
  6421. (save-excursion (end-of-line) (setq eline (point)))
  6422. (if (re-search-forward re-box eline t)
  6423. (if (member (match-string 2) '("[ ]" "[-]"))
  6424. (setq c-off (1+ c-off))
  6425. (setq c-on (1+ c-on))
  6426. )
  6427. )
  6428. (org-end-of-item)
  6429. (setq next-ind (org-get-indentation))
  6430. )))
  6431. (goto-char continue-from)
  6432. ;; update cookie
  6433. (when end-cookie
  6434. (delete-region beg-cookie end-cookie)
  6435. (goto-char beg-cookie)
  6436. (insert
  6437. (if is-percent
  6438. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6439. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6440. ;; update items checkbox if it has one
  6441. (when (org-at-item-p)
  6442. (org-beginning-of-item)
  6443. (when (and (> (+ c-on c-off) 0)
  6444. (re-search-forward re-box (point-at-eol) t))
  6445. (setq beg-cookie (match-beginning 2)
  6446. end-cookie (match-end 2))
  6447. (delete-region beg-cookie end-cookie)
  6448. (goto-char beg-cookie)
  6449. (cond ((= c-off 0) (insert "[X]"))
  6450. ((= c-on 0) (insert "[ ]"))
  6451. (t (insert "[-]")))
  6452. )))
  6453. (goto-char continue-from))
  6454. (when (interactive-p)
  6455. (message "Checkbox satistics updated %s (%d places)"
  6456. (if all "in entire file" "in current outline entry") cstat)))))
  6457. (defun org-get-checkbox-statistics-face ()
  6458. "Select the face for checkbox statistics.
  6459. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6460. it will be `org-todo'."
  6461. (if (match-end 1)
  6462. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6463. (if (and (> (match-end 2) (match-beginning 2))
  6464. (equal (match-string 2) (match-string 3)))
  6465. 'org-done
  6466. 'org-todo)))
  6467. (defun org-get-indentation (&optional line)
  6468. "Get the indentation of the current line, interpreting tabs.
  6469. When LINE is given, assume it represents a line and compute its indentation."
  6470. (if line
  6471. (if (string-match "^ *" (org-remove-tabs line))
  6472. (match-end 0))
  6473. (save-excursion
  6474. (beginning-of-line 1)
  6475. (skip-chars-forward " \t")
  6476. (current-column))))
  6477. (defun org-remove-tabs (s &optional width)
  6478. "Replace tabulators in S with spaces.
  6479. Assumes that s is a single line, starting in column 0."
  6480. (setq width (or width tab-width))
  6481. (while (string-match "\t" s)
  6482. (setq s (replace-match
  6483. (make-string
  6484. (- (* width (/ (+ (match-beginning 0) width) width))
  6485. (match-beginning 0)) ?\ )
  6486. t t s)))
  6487. s)
  6488. (defun org-fix-indentation (line ind)
  6489. "Fix indentation in LINE.
  6490. IND is a cons cell with target and minimum indentation.
  6491. If the current indenation in LINE is smaller than the minimum,
  6492. leave it alone. If it is larger than ind, set it to the target."
  6493. (let* ((l (org-remove-tabs line))
  6494. (i (org-get-indentation l))
  6495. (i1 (car ind)) (i2 (cdr ind)))
  6496. (if (>= i i2) (setq l (substring line i2)))
  6497. (if (> i1 0)
  6498. (concat (make-string i1 ?\ ) l)
  6499. l)))
  6500. (defcustom org-empty-line-terminates-plain-lists nil
  6501. "Non-nil means, an empty line ends all plain list levels.
  6502. When nil, empty lines are part of the preceeding item."
  6503. :group 'org-plain-lists
  6504. :type 'boolean)
  6505. (defun org-beginning-of-item ()
  6506. "Go to the beginning of the current hand-formatted item.
  6507. If the cursor is not in an item, throw an error."
  6508. (interactive)
  6509. (let ((pos (point))
  6510. (limit (save-excursion
  6511. (condition-case nil
  6512. (progn
  6513. (org-back-to-heading)
  6514. (beginning-of-line 2) (point))
  6515. (error (point-min)))))
  6516. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6517. ind ind1)
  6518. (if (org-at-item-p)
  6519. (beginning-of-line 1)
  6520. (beginning-of-line 1)
  6521. (skip-chars-forward " \t")
  6522. (setq ind (current-column))
  6523. (if (catch 'exit
  6524. (while t
  6525. (beginning-of-line 0)
  6526. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6527. (if (looking-at "[ \t]*$")
  6528. (setq ind1 ind-empty)
  6529. (skip-chars-forward " \t")
  6530. (setq ind1 (current-column)))
  6531. (if (< ind1 ind)
  6532. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6533. nil
  6534. (goto-char pos)
  6535. (error "Not in an item")))))
  6536. (defun org-end-of-item ()
  6537. "Go to the end of the current hand-formatted item.
  6538. If the cursor is not in an item, throw an error."
  6539. (interactive)
  6540. (let* ((pos (point))
  6541. ind1
  6542. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6543. (limit (save-excursion (outline-next-heading) (point)))
  6544. (ind (save-excursion
  6545. (org-beginning-of-item)
  6546. (skip-chars-forward " \t")
  6547. (current-column)))
  6548. (end (catch 'exit
  6549. (while t
  6550. (beginning-of-line 2)
  6551. (if (eobp) (throw 'exit (point)))
  6552. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6553. (if (looking-at "[ \t]*$")
  6554. (setq ind1 ind-empty)
  6555. (skip-chars-forward " \t")
  6556. (setq ind1 (current-column)))
  6557. (if (<= ind1 ind)
  6558. (throw 'exit (point-at-bol)))))))
  6559. (if end
  6560. (goto-char end)
  6561. (goto-char pos)
  6562. (error "Not in an item"))))
  6563. (defun org-next-item ()
  6564. "Move to the beginning of the next item in the current plain list.
  6565. Error if not at a plain list, or if this is the last item in the list."
  6566. (interactive)
  6567. (let (ind ind1 (pos (point)))
  6568. (org-beginning-of-item)
  6569. (setq ind (org-get-indentation))
  6570. (org-end-of-item)
  6571. (setq ind1 (org-get-indentation))
  6572. (unless (and (org-at-item-p) (= ind ind1))
  6573. (goto-char pos)
  6574. (error "On last item"))))
  6575. (defun org-previous-item ()
  6576. "Move to the beginning of the previous item in the current plain list.
  6577. Error if not at a plain list, or if this is the first item in the list."
  6578. (interactive)
  6579. (let (beg ind ind1 (pos (point)))
  6580. (org-beginning-of-item)
  6581. (setq beg (point))
  6582. (setq ind (org-get-indentation))
  6583. (goto-char beg)
  6584. (catch 'exit
  6585. (while t
  6586. (beginning-of-line 0)
  6587. (if (looking-at "[ \t]*$")
  6588. nil
  6589. (if (<= (setq ind1 (org-get-indentation)) ind)
  6590. (throw 'exit t)))))
  6591. (condition-case nil
  6592. (if (or (not (org-at-item-p))
  6593. (< ind1 (1- ind)))
  6594. (error "")
  6595. (org-beginning-of-item))
  6596. (error (goto-char pos)
  6597. (error "On first item")))))
  6598. (defun org-first-list-item-p ()
  6599. "Is this heading the item in a plain list?"
  6600. (unless (org-at-item-p)
  6601. (error "Not at a plain list item"))
  6602. (org-beginning-of-item)
  6603. (= (point) (save-excursion (org-beginning-of-item-list))))
  6604. (defun org-move-item-down ()
  6605. "Move the plain list item at point down, i.e. swap with following item.
  6606. Subitems (items with larger indentation) are considered part of the item,
  6607. so this really moves item trees."
  6608. (interactive)
  6609. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6610. (org-beginning-of-item)
  6611. (setq beg0 (point))
  6612. (save-excursion
  6613. (setq ne-beg (org-back-over-empty-lines))
  6614. (setq beg (point)))
  6615. (goto-char beg0)
  6616. (setq ind (org-get-indentation))
  6617. (org-end-of-item)
  6618. (setq end0 (point))
  6619. (setq ind1 (org-get-indentation))
  6620. (setq ne-end (org-back-over-empty-lines))
  6621. (setq end (point))
  6622. (goto-char beg0)
  6623. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6624. ;; include less whitespace
  6625. (save-excursion
  6626. (goto-char beg)
  6627. (forward-line (- ne-beg ne-end))
  6628. (setq beg (point))))
  6629. (goto-char end0)
  6630. (if (and (org-at-item-p) (= ind ind1))
  6631. (progn
  6632. (org-end-of-item)
  6633. (org-back-over-empty-lines)
  6634. (setq txt (buffer-substring beg end))
  6635. (save-excursion
  6636. (delete-region beg end))
  6637. (setq pos (point))
  6638. (insert txt)
  6639. (goto-char pos) (org-skip-whitespace)
  6640. (org-maybe-renumber-ordered-list))
  6641. (goto-char pos)
  6642. (error "Cannot move this item further down"))))
  6643. (defun org-move-item-up (arg)
  6644. "Move the plain list item at point up, i.e. swap with previous item.
  6645. Subitems (items with larger indentation) are considered part of the item,
  6646. so this really moves item trees."
  6647. (interactive "p")
  6648. (let (beg beg0 end ind ind1 (pos (point)) txt
  6649. ne-beg ne-ins ins-end)
  6650. (org-beginning-of-item)
  6651. (setq beg0 (point))
  6652. (setq ind (org-get-indentation))
  6653. (save-excursion
  6654. (setq ne-beg (org-back-over-empty-lines))
  6655. (setq beg (point)))
  6656. (goto-char beg0)
  6657. (org-end-of-item)
  6658. (setq end (point))
  6659. (goto-char beg0)
  6660. (catch 'exit
  6661. (while t
  6662. (beginning-of-line 0)
  6663. (if (looking-at "[ \t]*$")
  6664. (if org-empty-line-terminates-plain-lists
  6665. (progn
  6666. (goto-char pos)
  6667. (error "Cannot move this item further up"))
  6668. nil)
  6669. (if (<= (setq ind1 (org-get-indentation)) ind)
  6670. (throw 'exit t)))))
  6671. (condition-case nil
  6672. (org-beginning-of-item)
  6673. (error (goto-char beg)
  6674. (error "Cannot move this item further up")))
  6675. (setq ind1 (org-get-indentation))
  6676. (if (and (org-at-item-p) (= ind ind1))
  6677. (progn
  6678. (setq ne-ins (org-back-over-empty-lines))
  6679. (setq txt (buffer-substring beg end))
  6680. (save-excursion
  6681. (delete-region beg end))
  6682. (setq pos (point))
  6683. (insert txt)
  6684. (setq ins-end (point))
  6685. (goto-char pos) (org-skip-whitespace)
  6686. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6687. ;; Move whitespace back to beginning
  6688. (save-excursion
  6689. (goto-char ins-end)
  6690. (let ((kill-whole-line t))
  6691. (kill-line (- ne-ins ne-beg)) (point)))
  6692. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6693. (org-maybe-renumber-ordered-list))
  6694. (goto-char pos)
  6695. (error "Cannot move this item further up"))))
  6696. (defun org-maybe-renumber-ordered-list ()
  6697. "Renumber the ordered list at point if setup allows it.
  6698. This tests the user option `org-auto-renumber-ordered-lists' before
  6699. doing the renumbering."
  6700. (interactive)
  6701. (when (and org-auto-renumber-ordered-lists
  6702. (org-at-item-p))
  6703. (if (match-beginning 3)
  6704. (org-renumber-ordered-list 1)
  6705. (org-fix-bullet-type))))
  6706. (defun org-maybe-renumber-ordered-list-safe ()
  6707. (condition-case nil
  6708. (save-excursion
  6709. (org-maybe-renumber-ordered-list))
  6710. (error nil)))
  6711. (defun org-cycle-list-bullet (&optional which)
  6712. "Cycle through the different itemize/enumerate bullets.
  6713. This cycle the entire list level through the sequence:
  6714. `-' -> `+' -> `*' -> `1.' -> `1)'
  6715. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6716. 0 meand `-', 1 means `+' etc."
  6717. (interactive "P")
  6718. (org-preserve-lc
  6719. (org-beginning-of-item-list)
  6720. (org-at-item-p)
  6721. (beginning-of-line 1)
  6722. (let ((current (match-string 0))
  6723. (prevp (eq which 'previous))
  6724. new)
  6725. (setq new (cond
  6726. ((and (numberp which)
  6727. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6728. ((string-match "-" current) (if prevp "1)" "+"))
  6729. ((string-match "\\+" current)
  6730. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6731. ((string-match "\\*" current) (if prevp "+" "1."))
  6732. ((string-match "\\." current) (if prevp "*" "1)"))
  6733. ((string-match ")" current) (if prevp "1." "-"))
  6734. (t (error "This should not happen"))))
  6735. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6736. (org-fix-bullet-type)
  6737. (org-maybe-renumber-ordered-list))))
  6738. (defun org-get-string-indentation (s)
  6739. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6740. (let ((n -1) (i 0) (w tab-width) c)
  6741. (catch 'exit
  6742. (while (< (setq n (1+ n)) (length s))
  6743. (setq c (aref s n))
  6744. (cond ((= c ?\ ) (setq i (1+ i)))
  6745. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6746. (t (throw 'exit t)))))
  6747. i))
  6748. (defun org-renumber-ordered-list (arg)
  6749. "Renumber an ordered plain list.
  6750. Cursor needs to be in the first line of an item, the line that starts
  6751. with something like \"1.\" or \"2)\"."
  6752. (interactive "p")
  6753. (unless (and (org-at-item-p)
  6754. (match-beginning 3))
  6755. (error "This is not an ordered list"))
  6756. (let ((line (org-current-line))
  6757. (col (current-column))
  6758. (ind (org-get-string-indentation
  6759. (buffer-substring (point-at-bol) (match-beginning 3))))
  6760. ;; (term (substring (match-string 3) -1))
  6761. ind1 (n (1- arg))
  6762. fmt)
  6763. ;; find where this list begins
  6764. (org-beginning-of-item-list)
  6765. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6766. (setq fmt (concat "%d" (match-string 1)))
  6767. (beginning-of-line 0)
  6768. ;; walk forward and replace these numbers
  6769. (catch 'exit
  6770. (while t
  6771. (catch 'next
  6772. (beginning-of-line 2)
  6773. (if (eobp) (throw 'exit nil))
  6774. (if (looking-at "[ \t]*$") (throw 'next nil))
  6775. (skip-chars-forward " \t") (setq ind1 (current-column))
  6776. (if (> ind1 ind) (throw 'next t))
  6777. (if (< ind1 ind) (throw 'exit t))
  6778. (if (not (org-at-item-p)) (throw 'exit nil))
  6779. (delete-region (match-beginning 2) (match-end 2))
  6780. (goto-char (match-beginning 2))
  6781. (insert (format fmt (setq n (1+ n)))))))
  6782. (goto-line line)
  6783. (move-to-column col)))
  6784. (defun org-fix-bullet-type ()
  6785. "Make sure all items in this list have the same bullet as the firsst item."
  6786. (interactive)
  6787. (unless (org-at-item-p) (error "This is not a list"))
  6788. (let ((line (org-current-line))
  6789. (col (current-column))
  6790. (ind (current-indentation))
  6791. ind1 bullet)
  6792. ;; find where this list begins
  6793. (org-beginning-of-item-list)
  6794. (beginning-of-line 1)
  6795. ;; find out what the bullet type is
  6796. (looking-at "[ \t]*\\(\\S-+\\)")
  6797. (setq bullet (match-string 1))
  6798. ;; walk forward and replace these numbers
  6799. (beginning-of-line 0)
  6800. (catch 'exit
  6801. (while t
  6802. (catch 'next
  6803. (beginning-of-line 2)
  6804. (if (eobp) (throw 'exit nil))
  6805. (if (looking-at "[ \t]*$") (throw 'next nil))
  6806. (skip-chars-forward " \t") (setq ind1 (current-column))
  6807. (if (> ind1 ind) (throw 'next t))
  6808. (if (< ind1 ind) (throw 'exit t))
  6809. (if (not (org-at-item-p)) (throw 'exit nil))
  6810. (skip-chars-forward " \t")
  6811. (looking-at "\\S-+")
  6812. (replace-match bullet))))
  6813. (goto-line line)
  6814. (move-to-column col)
  6815. (if (string-match "[0-9]" bullet)
  6816. (org-renumber-ordered-list 1))))
  6817. (defun org-beginning-of-item-list ()
  6818. "Go to the beginning of the current item list.
  6819. I.e. to the first item in this list."
  6820. (interactive)
  6821. (org-beginning-of-item)
  6822. (let ((pos (point-at-bol))
  6823. (ind (org-get-indentation))
  6824. ind1)
  6825. ;; find where this list begins
  6826. (catch 'exit
  6827. (while t
  6828. (catch 'next
  6829. (beginning-of-line 0)
  6830. (if (looking-at "[ \t]*$")
  6831. (throw (if (bobp) 'exit 'next) t))
  6832. (skip-chars-forward " \t") (setq ind1 (current-column))
  6833. (if (or (< ind1 ind)
  6834. (and (= ind1 ind)
  6835. (not (org-at-item-p)))
  6836. (bobp))
  6837. (throw 'exit t)
  6838. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6839. (goto-char pos)))
  6840. (defun org-end-of-item-list ()
  6841. "Go to the end of the current item list.
  6842. I.e. to the text after the last item."
  6843. (interactive)
  6844. (org-beginning-of-item)
  6845. (let ((pos (point-at-bol))
  6846. (ind (org-get-indentation))
  6847. ind1)
  6848. ;; find where this list begins
  6849. (catch 'exit
  6850. (while t
  6851. (catch 'next
  6852. (beginning-of-line 2)
  6853. (if (looking-at "[ \t]*$")
  6854. (throw (if (eobp) 'exit 'next) t))
  6855. (skip-chars-forward " \t") (setq ind1 (current-column))
  6856. (if (or (< ind1 ind)
  6857. (and (= ind1 ind)
  6858. (not (org-at-item-p)))
  6859. (eobp))
  6860. (progn
  6861. (setq pos (point-at-bol))
  6862. (throw 'exit t))))))
  6863. (goto-char pos)))
  6864. (defvar org-last-indent-begin-marker (make-marker))
  6865. (defvar org-last-indent-end-marker (make-marker))
  6866. (defun org-outdent-item (arg)
  6867. "Outdent a local list item."
  6868. (interactive "p")
  6869. (org-indent-item (- arg)))
  6870. (defun org-indent-item (arg)
  6871. "Indent a local list item."
  6872. (interactive "p")
  6873. (unless (org-at-item-p)
  6874. (error "Not on an item"))
  6875. (save-excursion
  6876. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6877. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6878. (setq beg org-last-indent-begin-marker
  6879. end org-last-indent-end-marker)
  6880. (org-beginning-of-item)
  6881. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6882. (org-end-of-item)
  6883. (setq end (move-marker org-last-indent-end-marker (point))))
  6884. (goto-char beg)
  6885. (setq tmp (org-item-indent-positions)
  6886. ind (car tmp)
  6887. ind-down (nth 2 tmp)
  6888. ind-up (nth 1 tmp)
  6889. delta (if (> arg 0)
  6890. (if ind-down (- ind-down ind) 2)
  6891. (if ind-up (- ind-up ind) -2)))
  6892. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6893. (while (< (point) end)
  6894. (beginning-of-line 1)
  6895. (skip-chars-forward " \t") (setq ind1 (current-column))
  6896. (delete-region (point-at-bol) (point))
  6897. (or (eolp) (indent-to-column (+ ind1 delta)))
  6898. (beginning-of-line 2))))
  6899. (org-fix-bullet-type)
  6900. (org-maybe-renumber-ordered-list-safe)
  6901. (save-excursion
  6902. (beginning-of-line 0)
  6903. (condition-case nil (org-beginning-of-item) (error nil))
  6904. (org-maybe-renumber-ordered-list-safe)))
  6905. (defun org-item-indent-positions ()
  6906. "Return indentation for plain list items.
  6907. This returns a list with three values: The current indentation, the
  6908. parent indentation and the indentation a child should habe.
  6909. Assumes cursor in item line."
  6910. (let* ((bolpos (point-at-bol))
  6911. (ind (org-get-indentation))
  6912. ind-down ind-up pos)
  6913. (save-excursion
  6914. (org-beginning-of-item-list)
  6915. (skip-chars-backward "\n\r \t")
  6916. (when (org-in-item-p)
  6917. (org-beginning-of-item)
  6918. (setq ind-up (org-get-indentation))))
  6919. (setq pos (point))
  6920. (save-excursion
  6921. (cond
  6922. ((and (condition-case nil (progn (org-previous-item) t)
  6923. (error nil))
  6924. (or (forward-char 1) t)
  6925. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6926. (setq ind-down (org-get-indentation)))
  6927. ((and (goto-char pos)
  6928. (org-at-item-p))
  6929. (goto-char (match-end 0))
  6930. (skip-chars-forward " \t")
  6931. (setq ind-down (current-column)))))
  6932. (list ind ind-up ind-down)))
  6933. ;;; The orgstruct minor mode
  6934. ;; Define a minor mode which can be used in other modes in order to
  6935. ;; integrate the org-mode structure editing commands.
  6936. ;; This is really a hack, because the org-mode structure commands use
  6937. ;; keys which normally belong to the major mode. Here is how it
  6938. ;; works: The minor mode defines all the keys necessary to operate the
  6939. ;; structure commands, but wraps the commands into a function which
  6940. ;; tests if the cursor is currently at a headline or a plain list
  6941. ;; item. If that is the case, the structure command is used,
  6942. ;; temporarily setting many Org-mode variables like regular
  6943. ;; expressions for filling etc. However, when any of those keys is
  6944. ;; used at a different location, function uses `key-binding' to look
  6945. ;; up if the key has an associated command in another currently active
  6946. ;; keymap (minor modes, major mode, global), and executes that
  6947. ;; command. There might be problems if any of the keys is otherwise
  6948. ;; used as a prefix key.
  6949. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6950. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6951. ;; addresses this by checking explicitly for both bindings.
  6952. (defvar orgstruct-mode-map (make-sparse-keymap)
  6953. "Keymap for the minor `orgstruct-mode'.")
  6954. (defvar org-local-vars nil
  6955. "List of local variables, for use by `orgstruct-mode'")
  6956. ;;;###autoload
  6957. (define-minor-mode orgstruct-mode
  6958. "Toggle the minor more `orgstruct-mode'.
  6959. This mode is for using Org-mode structure commands in other modes.
  6960. The following key behave as if Org-mode was active, if the cursor
  6961. is on a headline, or on a plain list item (both in the definition
  6962. of Org-mode).
  6963. M-up Move entry/item up
  6964. M-down Move entry/item down
  6965. M-left Promote
  6966. M-right Demote
  6967. M-S-up Move entry/item up
  6968. M-S-down Move entry/item down
  6969. M-S-left Promote subtree
  6970. M-S-right Demote subtree
  6971. M-q Fill paragraph and items like in Org-mode
  6972. C-c ^ Sort entries
  6973. C-c - Cycle list bullet
  6974. TAB Cycle item visibility
  6975. M-RET Insert new heading/item
  6976. S-M-RET Insert new TODO heading / Chekbox item
  6977. C-c C-c Set tags / toggle checkbox"
  6978. nil " OrgStruct" nil
  6979. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6980. ;;;###autoload
  6981. (defun turn-on-orgstruct ()
  6982. "Unconditionally turn on `orgstruct-mode'."
  6983. (orgstruct-mode 1))
  6984. ;;;###autoload
  6985. (defun turn-on-orgstruct++ ()
  6986. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6987. In addition to setting orgstruct-mode, this also exports all indentation and
  6988. autofilling variables from org-mode into the buffer. Note that turning
  6989. off orgstruct-mode will *not* remove these additional settings."
  6990. (orgstruct-mode 1)
  6991. (let (var val)
  6992. (mapc
  6993. (lambda (x)
  6994. (when (string-match
  6995. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6996. (symbol-name (car x)))
  6997. (setq var (car x) val (nth 1 x))
  6998. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6999. org-local-vars)))
  7000. (defun orgstruct-error ()
  7001. "Error when there is no default binding for a structure key."
  7002. (interactive)
  7003. (error "This key has no function outside structure elements"))
  7004. (defun orgstruct-setup ()
  7005. "Setup orgstruct keymaps."
  7006. (let ((nfunc 0)
  7007. (bindings
  7008. (list
  7009. '([(meta up)] org-metaup)
  7010. '([(meta down)] org-metadown)
  7011. '([(meta left)] org-metaleft)
  7012. '([(meta right)] org-metaright)
  7013. '([(meta shift up)] org-shiftmetaup)
  7014. '([(meta shift down)] org-shiftmetadown)
  7015. '([(meta shift left)] org-shiftmetaleft)
  7016. '([(meta shift right)] org-shiftmetaright)
  7017. '([(shift up)] org-shiftup)
  7018. '([(shift down)] org-shiftdown)
  7019. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7020. '("\M-q" fill-paragraph)
  7021. '("\C-c^" org-sort)
  7022. '("\C-c-" org-cycle-list-bullet)))
  7023. elt key fun cmd)
  7024. (while (setq elt (pop bindings))
  7025. (setq nfunc (1+ nfunc))
  7026. (setq key (org-key (car elt))
  7027. fun (nth 1 elt)
  7028. cmd (orgstruct-make-binding fun nfunc key))
  7029. (org-defkey orgstruct-mode-map key cmd))
  7030. ;; Special treatment needed for TAB and RET
  7031. (org-defkey orgstruct-mode-map [(tab)]
  7032. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7033. (org-defkey orgstruct-mode-map "\C-i"
  7034. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7035. (org-defkey orgstruct-mode-map "\M-\C-m"
  7036. (orgstruct-make-binding 'org-insert-heading 105
  7037. "\M-\C-m" [(meta return)]))
  7038. (org-defkey orgstruct-mode-map [(meta return)]
  7039. (orgstruct-make-binding 'org-insert-heading 106
  7040. [(meta return)] "\M-\C-m"))
  7041. (org-defkey orgstruct-mode-map [(shift meta return)]
  7042. (orgstruct-make-binding 'org-insert-todo-heading 107
  7043. [(meta return)] "\M-\C-m"))
  7044. (unless org-local-vars
  7045. (setq org-local-vars (org-get-local-variables)))
  7046. t))
  7047. (defun orgstruct-make-binding (fun n &rest keys)
  7048. "Create a function for binding in the structure minor mode.
  7049. FUN is the command to call inside a table. N is used to create a unique
  7050. command name. KEYS are keys that should be checked in for a command
  7051. to execute outside of tables."
  7052. (eval
  7053. (list 'defun
  7054. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7055. '(arg)
  7056. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7057. "Outside of structure, run the binding of `"
  7058. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7059. "'.")
  7060. '(interactive "p")
  7061. (list 'if
  7062. '(org-context-p 'headline 'item)
  7063. (list 'org-run-like-in-org-mode (list 'quote fun))
  7064. (list 'let '(orgstruct-mode)
  7065. (list 'call-interactively
  7066. (append '(or)
  7067. (mapcar (lambda (k)
  7068. (list 'key-binding k))
  7069. keys)
  7070. '('orgstruct-error))))))))
  7071. (defun org-context-p (&rest contexts)
  7072. "Check if local context is and of CONTEXTS.
  7073. Possible values in the list of contexts are `table', `headline', and `item'."
  7074. (let ((pos (point)))
  7075. (goto-char (point-at-bol))
  7076. (prog1 (or (and (memq 'table contexts)
  7077. (looking-at "[ \t]*|"))
  7078. (and (memq 'headline contexts)
  7079. (looking-at "\\*+"))
  7080. (and (memq 'item contexts)
  7081. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7082. (goto-char pos))))
  7083. (defun org-get-local-variables ()
  7084. "Return a list of all local variables in an org-mode buffer."
  7085. (let (varlist)
  7086. (with-current-buffer (get-buffer-create "*Org tmp*")
  7087. (erase-buffer)
  7088. (org-mode)
  7089. (setq varlist (buffer-local-variables)))
  7090. (kill-buffer "*Org tmp*")
  7091. (delq nil
  7092. (mapcar
  7093. (lambda (x)
  7094. (setq x
  7095. (if (symbolp x)
  7096. (list x)
  7097. (list (car x) (list 'quote (cdr x)))))
  7098. (if (string-match
  7099. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7100. (symbol-name (car x)))
  7101. x nil))
  7102. varlist))))
  7103. ;;;###autoload
  7104. (defun org-run-like-in-org-mode (cmd)
  7105. (unless org-local-vars
  7106. (setq org-local-vars (org-get-local-variables)))
  7107. (eval (list 'let org-local-vars
  7108. (list 'call-interactively (list 'quote cmd)))))
  7109. ;;;; Archiving
  7110. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7111. (defun org-archive-subtree (&optional find-done)
  7112. "Move the current subtree to the archive.
  7113. The archive can be a certain top-level heading in the current file, or in
  7114. a different file. The tree will be moved to that location, the subtree
  7115. heading be marked DONE, and the current time will be added.
  7116. When called with prefix argument FIND-DONE, find whole trees without any
  7117. open TODO items and archive them (after getting confirmation from the user).
  7118. If the cursor is not at a headline when this comand is called, try all level
  7119. 1 trees. If the cursor is on a headline, only try the direct children of
  7120. this heading."
  7121. (interactive "P")
  7122. (if find-done
  7123. (org-archive-all-done)
  7124. ;; Save all relevant TODO keyword-relatex variables
  7125. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7126. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7127. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7128. (tr-org-done-keywords org-done-keywords)
  7129. (tr-org-todo-regexp org-todo-regexp)
  7130. (tr-org-todo-line-regexp org-todo-line-regexp)
  7131. (tr-org-odd-levels-only org-odd-levels-only)
  7132. (this-buffer (current-buffer))
  7133. (org-archive-location org-archive-location)
  7134. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7135. ;; start of variables that will be used for saving context
  7136. ;; The compiler complains about them - keep them anyway!
  7137. (file (abbreviate-file-name (buffer-file-name)))
  7138. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7139. (time (format-time-string
  7140. (substring (cdr org-time-stamp-formats) 1 -1)
  7141. (current-time)))
  7142. afile heading buffer level newfile-p
  7143. category todo priority
  7144. ;; start of variables that will be used for savind context
  7145. ltags itags prop)
  7146. ;; Try to find a local archive location
  7147. (save-excursion
  7148. (save-restriction
  7149. (widen)
  7150. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7151. (if (and prop (string-match "\\S-" prop))
  7152. (setq org-archive-location prop)
  7153. (if (or (re-search-backward re nil t)
  7154. (re-search-forward re nil t))
  7155. (setq org-archive-location (match-string 1))))))
  7156. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7157. (progn
  7158. (setq afile (format (match-string 1 org-archive-location)
  7159. (file-name-nondirectory buffer-file-name))
  7160. heading (match-string 2 org-archive-location)))
  7161. (error "Invalid `org-archive-location'"))
  7162. (if (> (length afile) 0)
  7163. (setq newfile-p (not (file-exists-p afile))
  7164. buffer (find-file-noselect afile))
  7165. (setq buffer (current-buffer)))
  7166. (unless buffer
  7167. (error "Cannot access file \"%s\"" afile))
  7168. (if (and (> (length heading) 0)
  7169. (string-match "^\\*+" heading))
  7170. (setq level (match-end 0))
  7171. (setq heading nil level 0))
  7172. (save-excursion
  7173. (org-back-to-heading t)
  7174. ;; Get context information that will be lost by moving the tree
  7175. (org-refresh-category-properties)
  7176. (setq category (org-get-category)
  7177. todo (and (looking-at org-todo-line-regexp)
  7178. (match-string 2))
  7179. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7180. ltags (org-get-tags)
  7181. itags (org-delete-all ltags (org-get-tags-at)))
  7182. (setq ltags (mapconcat 'identity ltags " ")
  7183. itags (mapconcat 'identity itags " "))
  7184. ;; We first only copy, in case something goes wrong
  7185. ;; we need to protect this-command, to avoid kill-region sets it,
  7186. ;; which would lead to duplication of subtrees
  7187. (let (this-command) (org-copy-subtree))
  7188. (set-buffer buffer)
  7189. ;; Enforce org-mode for the archive buffer
  7190. (if (not (org-mode-p))
  7191. ;; Force the mode for future visits.
  7192. (let ((org-insert-mode-line-in-empty-file t)
  7193. (org-inhibit-startup t))
  7194. (call-interactively 'org-mode)))
  7195. (when newfile-p
  7196. (goto-char (point-max))
  7197. (insert (format "\nArchived entries from file %s\n\n"
  7198. (buffer-file-name this-buffer))))
  7199. ;; Force the TODO keywords of the original buffer
  7200. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7201. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7202. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7203. (org-done-keywords tr-org-done-keywords)
  7204. (org-todo-regexp tr-org-todo-regexp)
  7205. (org-todo-line-regexp tr-org-todo-line-regexp)
  7206. (org-odd-levels-only
  7207. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7208. org-odd-levels-only
  7209. tr-org-odd-levels-only)))
  7210. (goto-char (point-min))
  7211. (if heading
  7212. (progn
  7213. (if (re-search-forward
  7214. (concat "^" (regexp-quote heading)
  7215. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7216. nil t)
  7217. (goto-char (match-end 0))
  7218. ;; Heading not found, just insert it at the end
  7219. (goto-char (point-max))
  7220. (or (bolp) (insert "\n"))
  7221. (insert "\n" heading "\n")
  7222. (end-of-line 0))
  7223. ;; Make the subtree visible
  7224. (show-subtree)
  7225. (org-end-of-subtree t)
  7226. (skip-chars-backward " \t\r\n")
  7227. (and (looking-at "[ \t\r\n]*")
  7228. (replace-match "\n\n")))
  7229. ;; No specific heading, just go to end of file.
  7230. (goto-char (point-max)) (insert "\n"))
  7231. ;; Paste
  7232. (org-paste-subtree (org-get-legal-level level 1))
  7233. ;; Mark the entry as done
  7234. (when (and org-archive-mark-done
  7235. (looking-at org-todo-line-regexp)
  7236. (or (not (match-end 2))
  7237. (not (member (match-string 2) org-done-keywords))))
  7238. (let (org-log-done org-todo-log-states)
  7239. (org-todo
  7240. (car (or (member org-archive-mark-done org-done-keywords)
  7241. org-done-keywords)))))
  7242. ;; Add the context info
  7243. (when org-archive-save-context-info
  7244. (let ((l org-archive-save-context-info) e n v)
  7245. (while (setq e (pop l))
  7246. (when (and (setq v (symbol-value e))
  7247. (stringp v) (string-match "\\S-" v))
  7248. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7249. (org-entry-put (point) n v)))))
  7250. ;; Save the buffer, if it is not the same buffer.
  7251. (if (not (eq this-buffer buffer)) (save-buffer))))
  7252. ;; Here we are back in the original buffer. Everything seems to have
  7253. ;; worked. So now cut the tree and finish up.
  7254. (let (this-command) (org-cut-subtree))
  7255. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7256. (message "Subtree archived %s"
  7257. (if (eq this-buffer buffer)
  7258. (concat "under heading: " heading)
  7259. (concat "in file: " (abbreviate-file-name afile)))))))
  7260. (defun org-refresh-category-properties ()
  7261. "Refresh category text properties in teh buffer."
  7262. (let ((def-cat (cond
  7263. ((null org-category)
  7264. (if buffer-file-name
  7265. (file-name-sans-extension
  7266. (file-name-nondirectory buffer-file-name))
  7267. "???"))
  7268. ((symbolp org-category) (symbol-name org-category))
  7269. (t org-category)))
  7270. beg end cat pos optionp)
  7271. (org-unmodified
  7272. (save-excursion
  7273. (save-restriction
  7274. (widen)
  7275. (goto-char (point-min))
  7276. (put-text-property (point) (point-max) 'org-category def-cat)
  7277. (while (re-search-forward
  7278. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7279. (setq pos (match-end 0)
  7280. optionp (equal (char-after (match-beginning 0)) ?#)
  7281. cat (org-trim (match-string 2)))
  7282. (if optionp
  7283. (setq beg (point-at-bol) end (point-max))
  7284. (org-back-to-heading t)
  7285. (setq beg (point) end (org-end-of-subtree t t)))
  7286. (put-text-property beg end 'org-category cat)
  7287. (goto-char pos)))))))
  7288. (defun org-archive-all-done (&optional tag)
  7289. "Archive sublevels of the current tree without open TODO items.
  7290. If the cursor is not on a headline, try all level 1 trees. If
  7291. it is on a headline, try all direct children.
  7292. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7293. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7294. (rea (concat ".*:" org-archive-tag ":"))
  7295. (begm (make-marker))
  7296. (endm (make-marker))
  7297. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7298. "Move subtree to archive (no open TODO items)? "))
  7299. beg end (cntarch 0))
  7300. (if (org-on-heading-p)
  7301. (progn
  7302. (setq re1 (concat "^" (regexp-quote
  7303. (make-string
  7304. (1+ (- (match-end 0) (match-beginning 0) 1))
  7305. ?*))
  7306. " "))
  7307. (move-marker begm (point))
  7308. (move-marker endm (org-end-of-subtree t)))
  7309. (setq re1 "^* ")
  7310. (move-marker begm (point-min))
  7311. (move-marker endm (point-max)))
  7312. (save-excursion
  7313. (goto-char begm)
  7314. (while (re-search-forward re1 endm t)
  7315. (setq beg (match-beginning 0)
  7316. end (save-excursion (org-end-of-subtree t) (point)))
  7317. (goto-char beg)
  7318. (if (re-search-forward re end t)
  7319. (goto-char end)
  7320. (goto-char beg)
  7321. (if (and (or (not tag) (not (looking-at rea)))
  7322. (y-or-n-p question))
  7323. (progn
  7324. (if tag
  7325. (org-toggle-tag org-archive-tag 'on)
  7326. (org-archive-subtree))
  7327. (setq cntarch (1+ cntarch)))
  7328. (goto-char end)))))
  7329. (message "%d trees archived" cntarch)))
  7330. (defun org-cycle-hide-drawers (state)
  7331. "Re-hide all drawers after a visibility state change."
  7332. (when (and (org-mode-p)
  7333. (not (memq state '(overview folded))))
  7334. (save-excursion
  7335. (let* ((globalp (memq state '(contents all)))
  7336. (beg (if globalp (point-min) (point)))
  7337. (end (if globalp (point-max) (org-end-of-subtree t))))
  7338. (goto-char beg)
  7339. (while (re-search-forward org-drawer-regexp end t)
  7340. (org-flag-drawer t))))))
  7341. (defun org-flag-drawer (flag)
  7342. (save-excursion
  7343. (beginning-of-line 1)
  7344. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7345. (let ((b (match-end 0))
  7346. (outline-regexp org-outline-regexp))
  7347. (if (re-search-forward
  7348. "^[ \t]*:END:"
  7349. (save-excursion (outline-next-heading) (point)) t)
  7350. (outline-flag-region b (point-at-eol) flag)
  7351. (error ":END: line missing"))))))
  7352. (defun org-cycle-hide-archived-subtrees (state)
  7353. "Re-hide all archived subtrees after a visibility state change."
  7354. (when (and (not org-cycle-open-archived-trees)
  7355. (not (memq state '(overview folded))))
  7356. (save-excursion
  7357. (let* ((globalp (memq state '(contents all)))
  7358. (beg (if globalp (point-min) (point)))
  7359. (end (if globalp (point-max) (org-end-of-subtree t))))
  7360. (org-hide-archived-subtrees beg end)
  7361. (goto-char beg)
  7362. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7363. (message "%s" (substitute-command-keys
  7364. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7365. (defun org-force-cycle-archived ()
  7366. "Cycle subtree even if it is archived."
  7367. (interactive)
  7368. (setq this-command 'org-cycle)
  7369. (let ((org-cycle-open-archived-trees t))
  7370. (call-interactively 'org-cycle)))
  7371. (defun org-hide-archived-subtrees (beg end)
  7372. "Re-hide all archived subtrees after a visibility state change."
  7373. (save-excursion
  7374. (let* ((re (concat ":" org-archive-tag ":")))
  7375. (goto-char beg)
  7376. (while (re-search-forward re end t)
  7377. (and (org-on-heading-p) (hide-subtree))
  7378. (org-end-of-subtree t)))))
  7379. (defun org-toggle-tag (tag &optional onoff)
  7380. "Toggle the tag TAG for the current line.
  7381. If ONOFF is `on' or `off', don't toggle but set to this state."
  7382. (unless (org-on-heading-p t) (error "Not on headling"))
  7383. (let (res current)
  7384. (save-excursion
  7385. (beginning-of-line)
  7386. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7387. (point-at-eol) t)
  7388. (progn
  7389. (setq current (match-string 1))
  7390. (replace-match ""))
  7391. (setq current ""))
  7392. (setq current (nreverse (org-split-string current ":")))
  7393. (cond
  7394. ((eq onoff 'on)
  7395. (setq res t)
  7396. (or (member tag current) (push tag current)))
  7397. ((eq onoff 'off)
  7398. (or (not (member tag current)) (setq current (delete tag current))))
  7399. (t (if (member tag current)
  7400. (setq current (delete tag current))
  7401. (setq res t)
  7402. (push tag current))))
  7403. (end-of-line 1)
  7404. (if current
  7405. (progn
  7406. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7407. (org-set-tags nil t))
  7408. (delete-horizontal-space))
  7409. (run-hooks 'org-after-tags-change-hook))
  7410. res))
  7411. (defun org-toggle-archive-tag (&optional arg)
  7412. "Toggle the archive tag for the current headline.
  7413. With prefix ARG, check all children of current headline and offer tagging
  7414. the children that do not contain any open TODO items."
  7415. (interactive "P")
  7416. (if arg
  7417. (org-archive-all-done 'tag)
  7418. (let (set)
  7419. (save-excursion
  7420. (org-back-to-heading t)
  7421. (setq set (org-toggle-tag org-archive-tag))
  7422. (when set (hide-subtree)))
  7423. (and set (beginning-of-line 1))
  7424. (message "Subtree %s" (if set "archived" "unarchived")))))
  7425. ;;;; Tables
  7426. ;;; The table editor
  7427. ;; Watch out: Here we are talking about two different kind of tables.
  7428. ;; Most of the code is for the tables created with the Org-mode table editor.
  7429. ;; Sometimes, we talk about tables created and edited with the table.el
  7430. ;; Emacs package. We call the former org-type tables, and the latter
  7431. ;; table.el-type tables.
  7432. (defun org-before-change-function (beg end)
  7433. "Every change indicates that a table might need an update."
  7434. (setq org-table-may-need-update t))
  7435. (defconst org-table-line-regexp "^[ \t]*|"
  7436. "Detects an org-type table line.")
  7437. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7438. "Detects an org-type table line.")
  7439. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7440. "Detects a table line marked for automatic recalculation.")
  7441. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7442. "Detects a table line marked for automatic recalculation.")
  7443. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7444. "Detects a table line marked for automatic recalculation.")
  7445. (defconst org-table-hline-regexp "^[ \t]*|-"
  7446. "Detects an org-type table hline.")
  7447. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7448. "Detects a table-type table hline.")
  7449. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7450. "Detects an org-type or table-type table.")
  7451. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7452. "Searching from within a table (any type) this finds the first line
  7453. outside the table.")
  7454. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7455. "Searching from within a table (any type) this finds the first line
  7456. outside the table.")
  7457. (defvar org-table-last-highlighted-reference nil)
  7458. (defvar org-table-formula-history nil)
  7459. (defvar org-table-column-names nil
  7460. "Alist with column names, derived from the `!' line.")
  7461. (defvar org-table-column-name-regexp nil
  7462. "Regular expression matching the current column names.")
  7463. (defvar org-table-local-parameters nil
  7464. "Alist with parameter names, derived from the `$' line.")
  7465. (defvar org-table-named-field-locations nil
  7466. "Alist with locations of named fields.")
  7467. (defvar org-table-current-line-types nil
  7468. "Table row types, non-nil only for the duration of a comand.")
  7469. (defvar org-table-current-begin-line nil
  7470. "Table begin line, non-nil only for the duration of a comand.")
  7471. (defvar org-table-current-begin-pos nil
  7472. "Table begin position, non-nil only for the duration of a comand.")
  7473. (defvar org-table-dlines nil
  7474. "Vector of data line line numbers in the current table.")
  7475. (defvar org-table-hlines nil
  7476. "Vector of hline line numbers in the current table.")
  7477. (defconst org-table-range-regexp
  7478. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7479. ;; 1 2 3 4 5
  7480. "Regular expression for matching ranges in formulas.")
  7481. (defconst org-table-range-regexp2
  7482. (concat
  7483. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7484. "\\.\\."
  7485. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7486. "Match a range for reference display.")
  7487. (defconst org-table-translate-regexp
  7488. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7489. "Match a reference that needs translation, for reference display.")
  7490. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7491. (defun org-table-create-with-table.el ()
  7492. "Use the table.el package to insert a new table.
  7493. If there is already a table at point, convert between Org-mode tables
  7494. and table.el tables."
  7495. (interactive)
  7496. (require 'table)
  7497. (cond
  7498. ((org-at-table.el-p)
  7499. (if (y-or-n-p "Convert table to Org-mode table? ")
  7500. (org-table-convert)))
  7501. ((org-at-table-p)
  7502. (if (y-or-n-p "Convert table to table.el table? ")
  7503. (org-table-convert)))
  7504. (t (call-interactively 'table-insert))))
  7505. (defun org-table-create-or-convert-from-region (arg)
  7506. "Convert region to table, or create an empty table.
  7507. If there is an active region, convert it to a table, using the function
  7508. `org-table-convert-region'. See the documentation of that function
  7509. to learn how the prefix argument is interpreted to determine the field
  7510. separator.
  7511. If there is no such region, create an empty table with `org-table-create'."
  7512. (interactive "P")
  7513. (if (org-region-active-p)
  7514. (org-table-convert-region (region-beginning) (region-end) arg)
  7515. (org-table-create arg)))
  7516. (defun org-table-create (&optional size)
  7517. "Query for a size and insert a table skeleton.
  7518. SIZE is a string Columns x Rows like for example \"3x2\"."
  7519. (interactive "P")
  7520. (unless size
  7521. (setq size (read-string
  7522. (concat "Table size Columns x Rows [e.g. "
  7523. org-table-default-size "]: ")
  7524. "" nil org-table-default-size)))
  7525. (let* ((pos (point))
  7526. (indent (make-string (current-column) ?\ ))
  7527. (split (org-split-string size " *x *"))
  7528. (rows (string-to-number (nth 1 split)))
  7529. (columns (string-to-number (car split)))
  7530. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7531. "\n")))
  7532. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7533. (point-at-bol) (point)))
  7534. (beginning-of-line 1)
  7535. (newline))
  7536. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7537. (dotimes (i rows) (insert line))
  7538. (goto-char pos)
  7539. (if (> rows 1)
  7540. ;; Insert a hline after the first row.
  7541. (progn
  7542. (end-of-line 1)
  7543. (insert "\n|-")
  7544. (goto-char pos)))
  7545. (org-table-align)))
  7546. (defun org-table-convert-region (beg0 end0 &optional separator)
  7547. "Convert region to a table.
  7548. The region goes from BEG0 to END0, but these borders will be moved
  7549. slightly, to make sure a beginning of line in the first line is included.
  7550. SEPARATOR specifies the field separator in the lines. It can have the
  7551. following values:
  7552. '(4) Use the comma as a field separator
  7553. '(16) Use a TAB as field separator
  7554. integer When a number, use that many spaces as field separator
  7555. nil When nil, the command tries to be smart and figure out the
  7556. separator in the following way:
  7557. - when each line contains a TAB, assume TAB-separated material
  7558. - when each line contains a comme, assume CSV material
  7559. - else, assume one or more SPACE charcters as separator."
  7560. (interactive "rP")
  7561. (let* ((beg (min beg0 end0))
  7562. (end (max beg0 end0))
  7563. re)
  7564. (goto-char beg)
  7565. (beginning-of-line 1)
  7566. (setq beg (move-marker (make-marker) (point)))
  7567. (goto-char end)
  7568. (if (bolp) (backward-char 1) (end-of-line 1))
  7569. (setq end (move-marker (make-marker) (point)))
  7570. ;; Get the right field separator
  7571. (unless separator
  7572. (goto-char beg)
  7573. (setq separator
  7574. (cond
  7575. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7576. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7577. (t 1))))
  7578. (setq re (cond
  7579. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7580. ((equal separator '(16)) "^\\|\t")
  7581. ((integerp separator)
  7582. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7583. (t (error "This should not happen"))))
  7584. (goto-char beg)
  7585. (while (re-search-forward re end t)
  7586. (replace-match "| " t t))
  7587. (goto-char beg)
  7588. (insert " ")
  7589. (org-table-align)))
  7590. (defun org-table-import (file arg)
  7591. "Import FILE as a table.
  7592. The file is assumed to be tab-separated. Such files can be produced by most
  7593. spreadsheet and database applications. If no tabs (at least one per line)
  7594. are found, lines will be split on whitespace into fields."
  7595. (interactive "f\nP")
  7596. (or (bolp) (newline))
  7597. (let ((beg (point))
  7598. (pm (point-max)))
  7599. (insert-file-contents file)
  7600. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7601. (defun org-table-export ()
  7602. "Export table as a tab-separated file.
  7603. Such a file can be imported into a spreadsheet program like Excel."
  7604. (interactive)
  7605. (let* ((beg (org-table-begin))
  7606. (end (org-table-end))
  7607. (table (buffer-substring beg end))
  7608. (file (read-file-name "Export table to: "))
  7609. buf)
  7610. (unless (or (not (file-exists-p file))
  7611. (y-or-n-p (format "Overwrite file %s? " file)))
  7612. (error "Abort"))
  7613. (with-current-buffer (find-file-noselect file)
  7614. (setq buf (current-buffer))
  7615. (erase-buffer)
  7616. (fundamental-mode)
  7617. (insert table)
  7618. (goto-char (point-min))
  7619. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7620. (replace-match "" t t)
  7621. (end-of-line 1))
  7622. (goto-char (point-min))
  7623. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7624. (replace-match "" t t)
  7625. (goto-char (min (1+ (point)) (point-max))))
  7626. (goto-char (point-min))
  7627. (while (re-search-forward "^-[-+]*$" nil t)
  7628. (replace-match "")
  7629. (if (looking-at "\n")
  7630. (delete-char 1)))
  7631. (goto-char (point-min))
  7632. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7633. (replace-match "\t" t t))
  7634. (save-buffer))
  7635. (kill-buffer buf)))
  7636. (defvar org-table-aligned-begin-marker (make-marker)
  7637. "Marker at the beginning of the table last aligned.
  7638. Used to check if cursor still is in that table, to minimize realignment.")
  7639. (defvar org-table-aligned-end-marker (make-marker)
  7640. "Marker at the end of the table last aligned.
  7641. Used to check if cursor still is in that table, to minimize realignment.")
  7642. (defvar org-table-last-alignment nil
  7643. "List of flags for flushright alignment, from the last re-alignment.
  7644. This is being used to correctly align a single field after TAB or RET.")
  7645. (defvar org-table-last-column-widths nil
  7646. "List of max width of fields in each column.
  7647. This is being used to correctly align a single field after TAB or RET.")
  7648. (defvar org-table-overlay-coordinates nil
  7649. "Overlay coordinates after each align of a table.")
  7650. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7651. (defvar org-last-recalc-line nil)
  7652. (defconst org-narrow-column-arrow "=>"
  7653. "Used as display property in narrowed table columns.")
  7654. (defun org-table-align ()
  7655. "Align the table at point by aligning all vertical bars."
  7656. (interactive)
  7657. (let* (
  7658. ;; Limits of table
  7659. (beg (org-table-begin))
  7660. (end (org-table-end))
  7661. ;; Current cursor position
  7662. (linepos (org-current-line))
  7663. (colpos (org-table-current-column))
  7664. (winstart (window-start))
  7665. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7666. lines (new "") lengths l typenums ty fields maxfields i
  7667. column
  7668. (indent "") cnt frac
  7669. rfmt hfmt
  7670. (spaces '(1 . 1))
  7671. (sp1 (car spaces))
  7672. (sp2 (cdr spaces))
  7673. (rfmt1 (concat
  7674. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7675. (hfmt1 (concat
  7676. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7677. emptystrings links dates emph narrow fmax f1 len c e)
  7678. (untabify beg end)
  7679. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7680. ;; Check if we have links or dates
  7681. (goto-char beg)
  7682. (setq links (re-search-forward org-bracket-link-regexp end t))
  7683. (goto-char beg)
  7684. (setq emph (and org-hide-emphasis-markers
  7685. (re-search-forward org-emph-re end t)))
  7686. (goto-char beg)
  7687. (setq dates (and org-display-custom-times
  7688. (re-search-forward org-ts-regexp-both end t)))
  7689. ;; Make sure the link properties are right
  7690. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7691. ;; Make sure the date properties are right
  7692. (when dates (goto-char beg) (while (org-activate-dates end)))
  7693. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7694. ;; Check if we are narrowing any columns
  7695. (goto-char beg)
  7696. (setq narrow (and org-format-transports-properties-p
  7697. (re-search-forward "<[0-9]+>" end t)))
  7698. ;; Get the rows
  7699. (setq lines (org-split-string
  7700. (buffer-substring beg end) "\n"))
  7701. ;; Store the indentation of the first line
  7702. (if (string-match "^ *" (car lines))
  7703. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7704. ;; Mark the hlines by setting the corresponding element to nil
  7705. ;; At the same time, we remove trailing space.
  7706. (setq lines (mapcar (lambda (l)
  7707. (if (string-match "^ *|-" l)
  7708. nil
  7709. (if (string-match "[ \t]+$" l)
  7710. (substring l 0 (match-beginning 0))
  7711. l)))
  7712. lines))
  7713. ;; Get the data fields by splitting the lines.
  7714. (setq fields (mapcar
  7715. (lambda (l)
  7716. (org-split-string l " *| *"))
  7717. (delq nil (copy-sequence lines))))
  7718. ;; How many fields in the longest line?
  7719. (condition-case nil
  7720. (setq maxfields (apply 'max (mapcar 'length fields)))
  7721. (error
  7722. (kill-region beg end)
  7723. (org-table-create org-table-default-size)
  7724. (error "Empty table - created default table")))
  7725. ;; A list of empty strings to fill any short rows on output
  7726. (setq emptystrings (make-list maxfields ""))
  7727. ;; Check for special formatting.
  7728. (setq i -1)
  7729. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7730. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7731. ;; Check if there is an explicit width specified
  7732. (when narrow
  7733. (setq c column fmax nil)
  7734. (while c
  7735. (setq e (pop c))
  7736. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7737. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7738. ;; Find fields that are wider than fmax, and shorten them
  7739. (when fmax
  7740. (loop for xx in column do
  7741. (when (and (stringp xx)
  7742. (> (org-string-width xx) fmax))
  7743. (org-add-props xx nil
  7744. 'help-echo
  7745. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7746. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7747. (unless (> f1 1)
  7748. (error "Cannot narrow field starting with wide link \"%s\""
  7749. (match-string 0 xx)))
  7750. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7751. (add-text-properties (- f1 2) f1
  7752. (list 'display org-narrow-column-arrow)
  7753. xx)))))
  7754. ;; Get the maximum width for each column
  7755. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7756. ;; Get the fraction of numbers, to decide about alignment of the column
  7757. (setq cnt 0 frac 0.0)
  7758. (loop for x in column do
  7759. (if (equal x "")
  7760. nil
  7761. (setq frac ( / (+ (* frac cnt)
  7762. (if (string-match org-table-number-regexp x) 1 0))
  7763. (setq cnt (1+ cnt))))))
  7764. (push (>= frac org-table-number-fraction) typenums))
  7765. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7766. ;; Store the alignment of this table, for later editing of single fields
  7767. (setq org-table-last-alignment typenums
  7768. org-table-last-column-widths lengths)
  7769. ;; With invisible characters, `format' does not get the field width right
  7770. ;; So we need to make these fields wide by hand.
  7771. (when (or links emph)
  7772. (loop for i from 0 upto (1- maxfields) do
  7773. (setq len (nth i lengths))
  7774. (loop for j from 0 upto (1- (length fields)) do
  7775. (setq c (nthcdr i (car (nthcdr j fields))))
  7776. (if (and (stringp (car c))
  7777. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7778. ; (string-match org-bracket-link-regexp (car c))
  7779. (< (org-string-width (car c)) len))
  7780. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7781. ;; Compute the formats needed for output of the table
  7782. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7783. (while (setq l (pop lengths))
  7784. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7785. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7786. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7787. (setq rfmt (concat rfmt "\n")
  7788. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7789. (setq new (mapconcat
  7790. (lambda (l)
  7791. (if l (apply 'format rfmt
  7792. (append (pop fields) emptystrings))
  7793. hfmt))
  7794. lines ""))
  7795. ;; Replace the old one
  7796. (delete-region beg end)
  7797. (move-marker end nil)
  7798. (move-marker org-table-aligned-begin-marker (point))
  7799. (insert new)
  7800. (move-marker org-table-aligned-end-marker (point))
  7801. (when (and orgtbl-mode (not (org-mode-p)))
  7802. (goto-char org-table-aligned-begin-marker)
  7803. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7804. ;; Try to move to the old location
  7805. (goto-line winstartline)
  7806. (setq winstart (point-at-bol))
  7807. (goto-line linepos)
  7808. (set-window-start (selected-window) winstart 'noforce)
  7809. (org-table-goto-column colpos)
  7810. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7811. (setq org-table-may-need-update nil)
  7812. ))
  7813. (defun org-string-width (s)
  7814. "Compute width of string, ignoring invisible characters.
  7815. This ignores character with invisibility property `org-link', and also
  7816. characters with property `org-cwidth', because these will become invisible
  7817. upon the next fontification round."
  7818. (let (b l)
  7819. (when (or (eq t buffer-invisibility-spec)
  7820. (assq 'org-link buffer-invisibility-spec))
  7821. (while (setq b (text-property-any 0 (length s)
  7822. 'invisible 'org-link s))
  7823. (setq s (concat (substring s 0 b)
  7824. (substring s (or (next-single-property-change
  7825. b 'invisible s) (length s)))))))
  7826. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7827. (setq s (concat (substring s 0 b)
  7828. (substring s (or (next-single-property-change
  7829. b 'org-cwidth s) (length s))))))
  7830. (setq l (string-width s) b -1)
  7831. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7832. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7833. l))
  7834. (defun org-table-begin (&optional table-type)
  7835. "Find the beginning of the table and return its position.
  7836. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7837. (save-excursion
  7838. (if (not (re-search-backward
  7839. (if table-type org-table-any-border-regexp
  7840. org-table-border-regexp)
  7841. nil t))
  7842. (progn (goto-char (point-min)) (point))
  7843. (goto-char (match-beginning 0))
  7844. (beginning-of-line 2)
  7845. (point))))
  7846. (defun org-table-end (&optional table-type)
  7847. "Find the end of the table and return its position.
  7848. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7849. (save-excursion
  7850. (if (not (re-search-forward
  7851. (if table-type org-table-any-border-regexp
  7852. org-table-border-regexp)
  7853. nil t))
  7854. (goto-char (point-max))
  7855. (goto-char (match-beginning 0)))
  7856. (point-marker)))
  7857. (defun org-table-justify-field-maybe (&optional new)
  7858. "Justify the current field, text to left, number to right.
  7859. Optional argument NEW may specify text to replace the current field content."
  7860. (cond
  7861. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7862. ((org-at-table-hline-p))
  7863. ((and (not new)
  7864. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7865. (current-buffer)))
  7866. (< (point) org-table-aligned-begin-marker)
  7867. (>= (point) org-table-aligned-end-marker)))
  7868. ;; This is not the same table, force a full re-align
  7869. (setq org-table-may-need-update t))
  7870. (t ;; realign the current field, based on previous full realign
  7871. (let* ((pos (point)) s
  7872. (col (org-table-current-column))
  7873. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7874. l f n o e)
  7875. (when (> col 0)
  7876. (skip-chars-backward "^|\n")
  7877. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7878. (progn
  7879. (setq s (match-string 1)
  7880. o (match-string 0)
  7881. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7882. e (not (= (match-beginning 2) (match-end 2))))
  7883. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7884. l (if e "|" (setq org-table-may-need-update t) ""))
  7885. n (format f s))
  7886. (if new
  7887. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7888. (setq n (format f new))
  7889. (setq n (concat new "|") org-table-may-need-update t)))
  7890. (or (equal n o)
  7891. (let (org-table-may-need-update)
  7892. (replace-match n t t))))
  7893. (setq org-table-may-need-update t))
  7894. (goto-char pos))))))
  7895. (defun org-table-next-field ()
  7896. "Go to the next field in the current table, creating new lines as needed.
  7897. Before doing so, re-align the table if necessary."
  7898. (interactive)
  7899. (org-table-maybe-eval-formula)
  7900. (org-table-maybe-recalculate-line)
  7901. (if (and org-table-automatic-realign
  7902. org-table-may-need-update)
  7903. (org-table-align))
  7904. (let ((end (org-table-end)))
  7905. (if (org-at-table-hline-p)
  7906. (end-of-line 1))
  7907. (condition-case nil
  7908. (progn
  7909. (re-search-forward "|" end)
  7910. (if (looking-at "[ \t]*$")
  7911. (re-search-forward "|" end))
  7912. (if (and (looking-at "-")
  7913. org-table-tab-jumps-over-hlines
  7914. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7915. (goto-char (match-beginning 1)))
  7916. (if (looking-at "-")
  7917. (progn
  7918. (beginning-of-line 0)
  7919. (org-table-insert-row 'below))
  7920. (if (looking-at " ") (forward-char 1))))
  7921. (error
  7922. (org-table-insert-row 'below)))))
  7923. (defun org-table-previous-field ()
  7924. "Go to the previous field in the table.
  7925. Before doing so, re-align the table if necessary."
  7926. (interactive)
  7927. (org-table-justify-field-maybe)
  7928. (org-table-maybe-recalculate-line)
  7929. (if (and org-table-automatic-realign
  7930. org-table-may-need-update)
  7931. (org-table-align))
  7932. (if (org-at-table-hline-p)
  7933. (end-of-line 1))
  7934. (re-search-backward "|" (org-table-begin))
  7935. (re-search-backward "|" (org-table-begin))
  7936. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7937. (re-search-backward "|" (org-table-begin)))
  7938. (if (looking-at "| ?")
  7939. (goto-char (match-end 0))))
  7940. (defun org-table-next-row ()
  7941. "Go to the next row (same column) in the current table.
  7942. Before doing so, re-align the table if necessary."
  7943. (interactive)
  7944. (org-table-maybe-eval-formula)
  7945. (org-table-maybe-recalculate-line)
  7946. (if (or (looking-at "[ \t]*$")
  7947. (save-excursion (skip-chars-backward " \t") (bolp)))
  7948. (newline)
  7949. (if (and org-table-automatic-realign
  7950. org-table-may-need-update)
  7951. (org-table-align))
  7952. (let ((col (org-table-current-column)))
  7953. (beginning-of-line 2)
  7954. (if (or (not (org-at-table-p))
  7955. (org-at-table-hline-p))
  7956. (progn
  7957. (beginning-of-line 0)
  7958. (org-table-insert-row 'below)))
  7959. (org-table-goto-column col)
  7960. (skip-chars-backward "^|\n\r")
  7961. (if (looking-at " ") (forward-char 1)))))
  7962. (defun org-table-copy-down (n)
  7963. "Copy a field down in the current column.
  7964. If the field at the cursor is empty, copy into it the content of the nearest
  7965. non-empty field above. With argument N, use the Nth non-empty field.
  7966. If the current field is not empty, it is copied down to the next row, and
  7967. the cursor is moved with it. Therefore, repeating this command causes the
  7968. column to be filled row-by-row.
  7969. If the variable `org-table-copy-increment' is non-nil and the field is an
  7970. integer or a timestamp, it will be incremented while copying. In the case of
  7971. a timestamp, if the cursor is on the year, change the year. If it is on the
  7972. month or the day, change that. Point will stay on the current date field
  7973. in order to easily repeat the interval."
  7974. (interactive "p")
  7975. (let* ((colpos (org-table-current-column))
  7976. (col (current-column))
  7977. (field (org-table-get-field))
  7978. (non-empty (string-match "[^ \t]" field))
  7979. (beg (org-table-begin))
  7980. txt)
  7981. (org-table-check-inside-data-field)
  7982. (if non-empty
  7983. (progn
  7984. (setq txt (org-trim field))
  7985. (org-table-next-row)
  7986. (org-table-blank-field))
  7987. (save-excursion
  7988. (setq txt
  7989. (catch 'exit
  7990. (while (progn (beginning-of-line 1)
  7991. (re-search-backward org-table-dataline-regexp
  7992. beg t))
  7993. (org-table-goto-column colpos t)
  7994. (if (and (looking-at
  7995. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7996. (= (setq n (1- n)) 0))
  7997. (throw 'exit (match-string 1))))))))
  7998. (if txt
  7999. (progn
  8000. (if (and org-table-copy-increment
  8001. (string-match "^[0-9]+$" txt))
  8002. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8003. (insert txt)
  8004. (move-to-column col)
  8005. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8006. (org-timestamp-up 1)
  8007. (org-table-maybe-recalculate-line))
  8008. (org-table-align)
  8009. (move-to-column col))
  8010. (error "No non-empty field found"))))
  8011. (defun org-table-check-inside-data-field ()
  8012. "Is point inside a table data field?
  8013. I.e. not on a hline or before the first or after the last column?
  8014. This actually throws an error, so it aborts the current command."
  8015. (if (or (not (org-at-table-p))
  8016. (= (org-table-current-column) 0)
  8017. (org-at-table-hline-p)
  8018. (looking-at "[ \t]*$"))
  8019. (error "Not in table data field")))
  8020. (defvar org-table-clip nil
  8021. "Clipboard for table regions.")
  8022. (defun org-table-blank-field ()
  8023. "Blank the current table field or active region."
  8024. (interactive)
  8025. (org-table-check-inside-data-field)
  8026. (if (and (interactive-p) (org-region-active-p))
  8027. (let (org-table-clip)
  8028. (org-table-cut-region (region-beginning) (region-end)))
  8029. (skip-chars-backward "^|")
  8030. (backward-char 1)
  8031. (if (looking-at "|[^|\n]+")
  8032. (let* ((pos (match-beginning 0))
  8033. (match (match-string 0))
  8034. (len (org-string-width match)))
  8035. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8036. (goto-char (+ 2 pos))
  8037. (substring match 1)))))
  8038. (defun org-table-get-field (&optional n replace)
  8039. "Return the value of the field in column N of current row.
  8040. N defaults to current field.
  8041. If REPLACE is a string, replace field with this value. The return value
  8042. is always the old value."
  8043. (and n (org-table-goto-column n))
  8044. (skip-chars-backward "^|\n")
  8045. (backward-char 1)
  8046. (if (looking-at "|[^|\r\n]*")
  8047. (let* ((pos (match-beginning 0))
  8048. (val (buffer-substring (1+ pos) (match-end 0))))
  8049. (if replace
  8050. (replace-match (concat "|" replace) t t))
  8051. (goto-char (min (point-at-eol) (+ 2 pos)))
  8052. val)
  8053. (forward-char 1) ""))
  8054. (defun org-table-field-info (arg)
  8055. "Show info about the current field, and highlight any reference at point."
  8056. (interactive "P")
  8057. (org-table-get-specials)
  8058. (save-excursion
  8059. (let* ((pos (point))
  8060. (col (org-table-current-column))
  8061. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8062. (name (car (rassoc (list (org-current-line) col)
  8063. org-table-named-field-locations)))
  8064. (eql (org-table-get-stored-formulas))
  8065. (dline (org-table-current-dline))
  8066. (ref (format "@%d$%d" dline col))
  8067. (ref1 (org-table-convert-refs-to-an ref))
  8068. (fequation (or (assoc name eql) (assoc ref eql)))
  8069. (cequation (assoc (int-to-string col) eql))
  8070. (eqn (or fequation cequation)))
  8071. (goto-char pos)
  8072. (condition-case nil
  8073. (org-table-show-reference 'local)
  8074. (error nil))
  8075. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8076. dline col
  8077. (if cname (concat " or $" cname) "")
  8078. dline col ref1
  8079. (if name (concat " or $" name) "")
  8080. ;; FIXME: formula info not correct if special table line
  8081. (if eqn
  8082. (concat ", formula: "
  8083. (org-table-formula-to-user
  8084. (concat
  8085. (if (string-match "^[$@]"(car eqn)) "" "$")
  8086. (car eqn) "=" (cdr eqn))))
  8087. "")))))
  8088. (defun org-table-current-column ()
  8089. "Find out which column we are in.
  8090. When called interactively, column is also displayed in echo area."
  8091. (interactive)
  8092. (if (interactive-p) (org-table-check-inside-data-field))
  8093. (save-excursion
  8094. (let ((cnt 0) (pos (point)))
  8095. (beginning-of-line 1)
  8096. (while (search-forward "|" pos t)
  8097. (setq cnt (1+ cnt)))
  8098. (if (interactive-p) (message "This is table column %d" cnt))
  8099. cnt)))
  8100. (defun org-table-current-dline ()
  8101. "Find out what table data line we are in.
  8102. Only datalins count for this."
  8103. (interactive)
  8104. (if (interactive-p) (org-table-check-inside-data-field))
  8105. (save-excursion
  8106. (let ((cnt 0) (pos (point)))
  8107. (goto-char (org-table-begin))
  8108. (while (<= (point) pos)
  8109. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8110. (beginning-of-line 2))
  8111. (if (interactive-p) (message "This is table line %d" cnt))
  8112. cnt)))
  8113. (defun org-table-goto-column (n &optional on-delim force)
  8114. "Move the cursor to the Nth column in the current table line.
  8115. With optional argument ON-DELIM, stop with point before the left delimiter
  8116. of the field.
  8117. If there are less than N fields, just go to after the last delimiter.
  8118. However, when FORCE is non-nil, create new columns if necessary."
  8119. (interactive "p")
  8120. (let ((pos (point-at-eol)))
  8121. (beginning-of-line 1)
  8122. (when (> n 0)
  8123. (while (and (> (setq n (1- n)) -1)
  8124. (or (search-forward "|" pos t)
  8125. (and force
  8126. (progn (end-of-line 1)
  8127. (skip-chars-backward "^|")
  8128. (insert " | "))))))
  8129. ; (backward-char 2) t)))))
  8130. (when (and force (not (looking-at ".*|")))
  8131. (save-excursion (end-of-line 1) (insert " | ")))
  8132. (if on-delim
  8133. (backward-char 1)
  8134. (if (looking-at " ") (forward-char 1))))))
  8135. (defun org-at-table-p (&optional table-type)
  8136. "Return t if the cursor is inside an org-type table.
  8137. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8138. (if org-enable-table-editor
  8139. (save-excursion
  8140. (beginning-of-line 1)
  8141. (looking-at (if table-type org-table-any-line-regexp
  8142. org-table-line-regexp)))
  8143. nil))
  8144. (defun org-at-table.el-p ()
  8145. "Return t if and only if we are at a table.el table."
  8146. (and (org-at-table-p 'any)
  8147. (save-excursion
  8148. (goto-char (org-table-begin 'any))
  8149. (looking-at org-table1-hline-regexp))))
  8150. (defun org-table-recognize-table.el ()
  8151. "If there is a table.el table nearby, recognize it and move into it."
  8152. (if org-table-tab-recognizes-table.el
  8153. (if (org-at-table.el-p)
  8154. (progn
  8155. (beginning-of-line 1)
  8156. (if (looking-at org-table-dataline-regexp)
  8157. nil
  8158. (if (looking-at org-table1-hline-regexp)
  8159. (progn
  8160. (beginning-of-line 2)
  8161. (if (looking-at org-table-any-border-regexp)
  8162. (beginning-of-line -1)))))
  8163. (if (re-search-forward "|" (org-table-end t) t)
  8164. (progn
  8165. (require 'table)
  8166. (if (table--at-cell-p (point))
  8167. t
  8168. (message "recognizing table.el table...")
  8169. (table-recognize-table)
  8170. (message "recognizing table.el table...done")))
  8171. (error "This should not happen..."))
  8172. t)
  8173. nil)
  8174. nil))
  8175. (defun org-at-table-hline-p ()
  8176. "Return t if the cursor is inside a hline in a table."
  8177. (if org-enable-table-editor
  8178. (save-excursion
  8179. (beginning-of-line 1)
  8180. (looking-at org-table-hline-regexp))
  8181. nil))
  8182. (defun org-table-insert-column ()
  8183. "Insert a new column into the table."
  8184. (interactive)
  8185. (if (not (org-at-table-p))
  8186. (error "Not at a table"))
  8187. (org-table-find-dataline)
  8188. (let* ((col (max 1 (org-table-current-column)))
  8189. (beg (org-table-begin))
  8190. (end (org-table-end))
  8191. ;; Current cursor position
  8192. (linepos (org-current-line))
  8193. (colpos col))
  8194. (goto-char beg)
  8195. (while (< (point) end)
  8196. (if (org-at-table-hline-p)
  8197. nil
  8198. (org-table-goto-column col t)
  8199. (insert "| "))
  8200. (beginning-of-line 2))
  8201. (move-marker end nil)
  8202. (goto-line linepos)
  8203. (org-table-goto-column colpos)
  8204. (org-table-align)
  8205. (org-table-fix-formulas "$" nil (1- col) 1)))
  8206. (defun org-table-find-dataline ()
  8207. "Find a dataline in the current table, which is needed for column commands."
  8208. (if (and (org-at-table-p)
  8209. (not (org-at-table-hline-p)))
  8210. t
  8211. (let ((col (current-column))
  8212. (end (org-table-end)))
  8213. (move-to-column col)
  8214. (while (and (< (point) end)
  8215. (or (not (= (current-column) col))
  8216. (org-at-table-hline-p)))
  8217. (beginning-of-line 2)
  8218. (move-to-column col))
  8219. (if (and (org-at-table-p)
  8220. (not (org-at-table-hline-p)))
  8221. t
  8222. (error
  8223. "Please position cursor in a data line for column operations")))))
  8224. (defun org-table-delete-column ()
  8225. "Delete a column from the table."
  8226. (interactive)
  8227. (if (not (org-at-table-p))
  8228. (error "Not at a table"))
  8229. (org-table-find-dataline)
  8230. (org-table-check-inside-data-field)
  8231. (let* ((col (org-table-current-column))
  8232. (beg (org-table-begin))
  8233. (end (org-table-end))
  8234. ;; Current cursor position
  8235. (linepos (org-current-line))
  8236. (colpos col))
  8237. (goto-char beg)
  8238. (while (< (point) end)
  8239. (if (org-at-table-hline-p)
  8240. nil
  8241. (org-table-goto-column col t)
  8242. (and (looking-at "|[^|\n]+|")
  8243. (replace-match "|")))
  8244. (beginning-of-line 2))
  8245. (move-marker end nil)
  8246. (goto-line linepos)
  8247. (org-table-goto-column colpos)
  8248. (org-table-align)
  8249. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8250. col -1 col)))
  8251. (defun org-table-move-column-right ()
  8252. "Move column to the right."
  8253. (interactive)
  8254. (org-table-move-column nil))
  8255. (defun org-table-move-column-left ()
  8256. "Move column to the left."
  8257. (interactive)
  8258. (org-table-move-column 'left))
  8259. (defun org-table-move-column (&optional left)
  8260. "Move the current column to the right. With arg LEFT, move to the left."
  8261. (interactive "P")
  8262. (if (not (org-at-table-p))
  8263. (error "Not at a table"))
  8264. (org-table-find-dataline)
  8265. (org-table-check-inside-data-field)
  8266. (let* ((col (org-table-current-column))
  8267. (col1 (if left (1- col) col))
  8268. (beg (org-table-begin))
  8269. (end (org-table-end))
  8270. ;; Current cursor position
  8271. (linepos (org-current-line))
  8272. (colpos (if left (1- col) (1+ col))))
  8273. (if (and left (= col 1))
  8274. (error "Cannot move column further left"))
  8275. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8276. (error "Cannot move column further right"))
  8277. (goto-char beg)
  8278. (while (< (point) end)
  8279. (if (org-at-table-hline-p)
  8280. nil
  8281. (org-table-goto-column col1 t)
  8282. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8283. (replace-match "|\\2|\\1|")))
  8284. (beginning-of-line 2))
  8285. (move-marker end nil)
  8286. (goto-line linepos)
  8287. (org-table-goto-column colpos)
  8288. (org-table-align)
  8289. (org-table-fix-formulas
  8290. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8291. (cons (number-to-string colpos) (number-to-string col))))))
  8292. (defun org-table-move-row-down ()
  8293. "Move table row down."
  8294. (interactive)
  8295. (org-table-move-row nil))
  8296. (defun org-table-move-row-up ()
  8297. "Move table row up."
  8298. (interactive)
  8299. (org-table-move-row 'up))
  8300. (defun org-table-move-row (&optional up)
  8301. "Move the current table line down. With arg UP, move it up."
  8302. (interactive "P")
  8303. (let* ((col (current-column))
  8304. (pos (point))
  8305. (hline1p (save-excursion (beginning-of-line 1)
  8306. (looking-at org-table-hline-regexp)))
  8307. (dline1 (org-table-current-dline))
  8308. (dline2 (+ dline1 (if up -1 1)))
  8309. (tonew (if up 0 2))
  8310. txt hline2p)
  8311. (beginning-of-line tonew)
  8312. (unless (org-at-table-p)
  8313. (goto-char pos)
  8314. (error "Cannot move row further"))
  8315. (setq hline2p (looking-at org-table-hline-regexp))
  8316. (goto-char pos)
  8317. (beginning-of-line 1)
  8318. (setq pos (point))
  8319. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8320. (delete-region (point) (1+ (point-at-eol)))
  8321. (beginning-of-line tonew)
  8322. (insert txt)
  8323. (beginning-of-line 0)
  8324. (move-to-column col)
  8325. (unless (or hline1p hline2p)
  8326. (org-table-fix-formulas
  8327. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8328. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8329. (defun org-table-insert-row (&optional arg)
  8330. "Insert a new row above the current line into the table.
  8331. With prefix ARG, insert below the current line."
  8332. (interactive "P")
  8333. (if (not (org-at-table-p))
  8334. (error "Not at a table"))
  8335. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8336. (new (org-table-clean-line line)))
  8337. ;; Fix the first field if necessary
  8338. (if (string-match "^[ \t]*| *[#$] *|" line)
  8339. (setq new (replace-match (match-string 0 line) t t new)))
  8340. (beginning-of-line (if arg 2 1))
  8341. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8342. (beginning-of-line 0)
  8343. (re-search-forward "| ?" (point-at-eol) t)
  8344. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8345. (org-table-align))
  8346. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8347. (defun org-table-insert-hline (&optional above)
  8348. "Insert a horizontal-line below the current line into the table.
  8349. With prefix ABOVE, insert above the current line."
  8350. (interactive "P")
  8351. (if (not (org-at-table-p))
  8352. (error "Not at a table"))
  8353. (let ((line (org-table-clean-line
  8354. (buffer-substring (point-at-bol) (point-at-eol))))
  8355. (col (current-column)))
  8356. (while (string-match "|\\( +\\)|" line)
  8357. (setq line (replace-match
  8358. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8359. ?-) "|") t t line)))
  8360. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8361. (beginning-of-line (if above 1 2))
  8362. (insert line "\n")
  8363. (beginning-of-line (if above 1 -1))
  8364. (move-to-column col)
  8365. (and org-table-overlay-coordinates (org-table-align))))
  8366. (defun org-table-hline-and-move (&optional same-column)
  8367. "Insert a hline and move to the row below that line."
  8368. (interactive "P")
  8369. (let ((col (org-table-current-column)))
  8370. (org-table-maybe-eval-formula)
  8371. (org-table-maybe-recalculate-line)
  8372. (org-table-insert-hline)
  8373. (end-of-line 2)
  8374. (if (looking-at "\n[ \t]*|-")
  8375. (progn (insert "\n|") (org-table-align))
  8376. (org-table-next-field))
  8377. (if same-column (org-table-goto-column col))))
  8378. (defun org-table-clean-line (s)
  8379. "Convert a table line S into a string with only \"|\" and space.
  8380. In particular, this does handle wide and invisible characters."
  8381. (if (string-match "^[ \t]*|-" s)
  8382. ;; It's a hline, just map the characters
  8383. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8384. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8385. (setq s (replace-match
  8386. (concat "|" (make-string (org-string-width (match-string 1 s))
  8387. ?\ ) "|")
  8388. t t s)))
  8389. s))
  8390. (defun org-table-kill-row ()
  8391. "Delete the current row or horizontal line from the table."
  8392. (interactive)
  8393. (if (not (org-at-table-p))
  8394. (error "Not at a table"))
  8395. (let ((col (current-column))
  8396. (dline (org-table-current-dline)))
  8397. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8398. (if (not (org-at-table-p)) (beginning-of-line 0))
  8399. (move-to-column col)
  8400. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8401. dline -1 dline)))
  8402. (defun org-table-sort-lines (with-case &optional sorting-type)
  8403. "Sort table lines according to the column at point.
  8404. The position of point indicates the column to be used for
  8405. sorting, and the range of lines is the range between the nearest
  8406. horizontal separator lines, or the entire table of no such lines
  8407. exist. If point is before the first column, you will be prompted
  8408. for the sorting column. If there is an active region, the mark
  8409. specifies the first line and the sorting column, while point
  8410. should be in the last line to be included into the sorting.
  8411. The command then prompts for the sorting type which can be
  8412. alphabetically, numerically, or by time (as given in a time stamp
  8413. in the field). Sorting in reverse order is also possible.
  8414. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8415. If SORTING-TYPE is specified when this function is called from a Lisp
  8416. program, no prompting will take place. SORTING-TYPE must be a character,
  8417. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8418. should be done in reverse order."
  8419. (interactive "P")
  8420. (let* ((thisline (org-current-line))
  8421. (thiscol (org-table-current-column))
  8422. beg end bcol ecol tend tbeg column lns pos)
  8423. (when (equal thiscol 0)
  8424. (if (interactive-p)
  8425. (setq thiscol
  8426. (string-to-number
  8427. (read-string "Use column N for sorting: ")))
  8428. (setq thiscol 1))
  8429. (org-table-goto-column thiscol))
  8430. (org-table-check-inside-data-field)
  8431. (if (org-region-active-p)
  8432. (progn
  8433. (setq beg (region-beginning) end (region-end))
  8434. (goto-char beg)
  8435. (setq column (org-table-current-column)
  8436. beg (point-at-bol))
  8437. (goto-char end)
  8438. (setq end (point-at-bol 2)))
  8439. (setq column (org-table-current-column)
  8440. pos (point)
  8441. tbeg (org-table-begin)
  8442. tend (org-table-end))
  8443. (if (re-search-backward org-table-hline-regexp tbeg t)
  8444. (setq beg (point-at-bol 2))
  8445. (goto-char tbeg)
  8446. (setq beg (point-at-bol 1)))
  8447. (goto-char pos)
  8448. (if (re-search-forward org-table-hline-regexp tend t)
  8449. (setq end (point-at-bol 1))
  8450. (goto-char tend)
  8451. (setq end (point-at-bol))))
  8452. (setq beg (move-marker (make-marker) beg)
  8453. end (move-marker (make-marker) end))
  8454. (untabify beg end)
  8455. (goto-char beg)
  8456. (org-table-goto-column column)
  8457. (skip-chars-backward "^|")
  8458. (setq bcol (current-column))
  8459. (org-table-goto-column (1+ column))
  8460. (skip-chars-backward "^|")
  8461. (setq ecol (1- (current-column)))
  8462. (org-table-goto-column column)
  8463. (setq lns (mapcar (lambda(x) (cons
  8464. (org-sort-remove-invisible
  8465. (nth (1- column)
  8466. (org-split-string x "[ \t]*|[ \t]*")))
  8467. x))
  8468. (org-split-string (buffer-substring beg end) "\n")))
  8469. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8470. (delete-region beg end)
  8471. (move-marker beg nil)
  8472. (move-marker end nil)
  8473. (insert (mapconcat 'cdr lns "\n") "\n")
  8474. (goto-line thisline)
  8475. (org-table-goto-column thiscol)
  8476. (message "%d lines sorted, based on column %d" (length lns) column)))
  8477. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8478. (defun org-sort-remove-invisible (s)
  8479. (remove-text-properties 0 (length s) org-rm-props s)
  8480. (while (string-match org-bracket-link-regexp s)
  8481. (setq s (replace-match (if (match-end 2)
  8482. (match-string 3 s)
  8483. (match-string 1 s)) t t s)))
  8484. s)
  8485. (defun org-table-cut-region (beg end)
  8486. "Copy region in table to the clipboard and blank all relevant fields."
  8487. (interactive "r")
  8488. (org-table-copy-region beg end 'cut))
  8489. (defun org-table-copy-region (beg end &optional cut)
  8490. "Copy rectangular region in table to clipboard.
  8491. A special clipboard is used which can only be accessed
  8492. with `org-table-paste-rectangle'."
  8493. (interactive "rP")
  8494. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8495. region cols
  8496. (rpl (if cut " " nil)))
  8497. (goto-char beg)
  8498. (org-table-check-inside-data-field)
  8499. (setq l01 (org-current-line)
  8500. c01 (org-table-current-column))
  8501. (goto-char end)
  8502. (org-table-check-inside-data-field)
  8503. (setq l02 (org-current-line)
  8504. c02 (org-table-current-column))
  8505. (setq l1 (min l01 l02) l2 (max l01 l02)
  8506. c1 (min c01 c02) c2 (max c01 c02))
  8507. (catch 'exit
  8508. (while t
  8509. (catch 'nextline
  8510. (if (> l1 l2) (throw 'exit t))
  8511. (goto-line l1)
  8512. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8513. (setq cols nil ic1 c1 ic2 c2)
  8514. (while (< ic1 (1+ ic2))
  8515. (push (org-table-get-field ic1 rpl) cols)
  8516. (setq ic1 (1+ ic1)))
  8517. (push (nreverse cols) region)
  8518. (setq l1 (1+ l1)))))
  8519. (setq org-table-clip (nreverse region))
  8520. (if cut (org-table-align))
  8521. org-table-clip))
  8522. (defun org-table-paste-rectangle ()
  8523. "Paste a rectangular region into a table.
  8524. The upper right corner ends up in the current field. All involved fields
  8525. will be overwritten. If the rectangle does not fit into the present table,
  8526. the table is enlarged as needed. The process ignores horizontal separator
  8527. lines."
  8528. (interactive)
  8529. (unless (and org-table-clip (listp org-table-clip))
  8530. (error "First cut/copy a region to paste!"))
  8531. (org-table-check-inside-data-field)
  8532. (let* ((clip org-table-clip)
  8533. (line (org-current-line))
  8534. (col (org-table-current-column))
  8535. (org-enable-table-editor t)
  8536. (org-table-automatic-realign nil)
  8537. c cols field)
  8538. (while (setq cols (pop clip))
  8539. (while (org-at-table-hline-p) (beginning-of-line 2))
  8540. (if (not (org-at-table-p))
  8541. (progn (end-of-line 0) (org-table-next-field)))
  8542. (setq c col)
  8543. (while (setq field (pop cols))
  8544. (org-table-goto-column c nil 'force)
  8545. (org-table-get-field nil field)
  8546. (setq c (1+ c)))
  8547. (beginning-of-line 2))
  8548. (goto-line line)
  8549. (org-table-goto-column col)
  8550. (org-table-align)))
  8551. (defun org-table-convert ()
  8552. "Convert from `org-mode' table to table.el and back.
  8553. Obviously, this only works within limits. When an Org-mode table is
  8554. converted to table.el, all horizontal separator lines get lost, because
  8555. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8556. A table.el table can be converted to an Org-mode table only if it does not
  8557. do row or column spanning. Multiline cells will become multiple cells.
  8558. Beware, Org-mode does not test if the table can be successfully converted - it
  8559. blindly applies a recipe that works for simple tables."
  8560. (interactive)
  8561. (require 'table)
  8562. (if (org-at-table.el-p)
  8563. ;; convert to Org-mode table
  8564. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8565. (end (move-marker (make-marker) (org-table-end t))))
  8566. (table-unrecognize-region beg end)
  8567. (goto-char beg)
  8568. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8569. (replace-match ""))
  8570. (goto-char beg))
  8571. (if (org-at-table-p)
  8572. ;; convert to table.el table
  8573. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8574. (end (move-marker (make-marker) (org-table-end))))
  8575. ;; first, get rid of all horizontal lines
  8576. (goto-char beg)
  8577. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8578. (replace-match ""))
  8579. ;; insert a hline before first
  8580. (goto-char beg)
  8581. (org-table-insert-hline 'above)
  8582. (beginning-of-line -1)
  8583. ;; insert a hline after each line
  8584. (while (progn (beginning-of-line 3) (< (point) end))
  8585. (org-table-insert-hline))
  8586. (goto-char beg)
  8587. (setq end (move-marker end (org-table-end)))
  8588. ;; replace "+" at beginning and ending of hlines
  8589. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8590. (replace-match "\\1+-"))
  8591. (goto-char beg)
  8592. (while (re-search-forward "-|[ \t]*$" end t)
  8593. (replace-match "-+"))
  8594. (goto-char beg)))))
  8595. (defun org-table-wrap-region (arg)
  8596. "Wrap several fields in a column like a paragraph.
  8597. This is useful if you'd like to spread the contents of a field over several
  8598. lines, in order to keep the table compact.
  8599. If there is an active region, and both point and mark are in the same column,
  8600. the text in the column is wrapped to minimum width for the given number of
  8601. lines. Generally, this makes the table more compact. A prefix ARG may be
  8602. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8603. formats the selected text to two lines. If the region was longer than two
  8604. lines, the remaining lines remain empty. A negative prefix argument reduces
  8605. the current number of lines by that amount. The wrapped text is pasted back
  8606. into the table. If you formatted it to more lines than it was before, fields
  8607. further down in the table get overwritten - so you might need to make space in
  8608. the table first.
  8609. If there is no region, the current field is split at the cursor position and
  8610. the text fragment to the right of the cursor is prepended to the field one
  8611. line down.
  8612. If there is no region, but you specify a prefix ARG, the current field gets
  8613. blank, and the content is appended to the field above."
  8614. (interactive "P")
  8615. (org-table-check-inside-data-field)
  8616. (if (org-region-active-p)
  8617. ;; There is a region: fill as a paragraph
  8618. (let* ((beg (region-beginning))
  8619. (cline (save-excursion (goto-char beg) (org-current-line)))
  8620. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8621. nlines)
  8622. (org-table-cut-region (region-beginning) (region-end))
  8623. (if (> (length (car org-table-clip)) 1)
  8624. (error "Region must be limited to single column"))
  8625. (setq nlines (if arg
  8626. (if (< arg 1)
  8627. (+ (length org-table-clip) arg)
  8628. arg)
  8629. (length org-table-clip)))
  8630. (setq org-table-clip
  8631. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8632. nil nlines)))
  8633. (goto-line cline)
  8634. (org-table-goto-column ccol)
  8635. (org-table-paste-rectangle))
  8636. ;; No region, split the current field at point
  8637. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8638. (skip-chars-forward "^\r\n|"))
  8639. (if arg
  8640. ;; combine with field above
  8641. (let ((s (org-table-blank-field))
  8642. (col (org-table-current-column)))
  8643. (beginning-of-line 0)
  8644. (while (org-at-table-hline-p) (beginning-of-line 0))
  8645. (org-table-goto-column col)
  8646. (skip-chars-forward "^|")
  8647. (skip-chars-backward " ")
  8648. (insert " " (org-trim s))
  8649. (org-table-align))
  8650. ;; split field
  8651. (if (looking-at "\\([^|]+\\)+|")
  8652. (let ((s (match-string 1)))
  8653. (replace-match " |")
  8654. (goto-char (match-beginning 0))
  8655. (org-table-next-row)
  8656. (insert (org-trim s) " ")
  8657. (org-table-align))
  8658. (org-table-next-row)))))
  8659. (defvar org-field-marker nil)
  8660. (defun org-table-edit-field (arg)
  8661. "Edit table field in a different window.
  8662. This is mainly useful for fields that contain hidden parts.
  8663. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8664. it can be edited in place."
  8665. (interactive "P")
  8666. (if arg
  8667. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8668. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8669. (remove-text-properties b e '(org-cwidth t invisible t
  8670. display t intangible t))
  8671. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8672. (font-lock-fontify-block)))
  8673. (let ((pos (move-marker (make-marker) (point)))
  8674. (field (org-table-get-field))
  8675. (cw (current-window-configuration))
  8676. p)
  8677. (org-switch-to-buffer-other-window "*Org tmp*")
  8678. (erase-buffer)
  8679. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8680. (let ((org-inhibit-startup t)) (org-mode))
  8681. (goto-char (setq p (point-max)))
  8682. (insert (org-trim field))
  8683. (remove-text-properties p (point-max)
  8684. '(invisible t org-cwidth t display t
  8685. intangible t))
  8686. (goto-char p)
  8687. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8688. (org-set-local 'org-window-configuration cw)
  8689. (org-set-local 'org-field-marker pos)
  8690. (message "Edit and finish with C-c C-c"))))
  8691. (defun org-table-finish-edit-field ()
  8692. "Finish editing a table data field.
  8693. Remove all newline characters, insert the result into the table, realign
  8694. the table and kill the editing buffer."
  8695. (let ((pos org-field-marker)
  8696. (cw org-window-configuration)
  8697. (cb (current-buffer))
  8698. text)
  8699. (goto-char (point-min))
  8700. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8701. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8702. (replace-match " "))
  8703. (setq text (org-trim (buffer-string)))
  8704. (set-window-configuration cw)
  8705. (kill-buffer cb)
  8706. (select-window (get-buffer-window (marker-buffer pos)))
  8707. (goto-char pos)
  8708. (move-marker pos nil)
  8709. (org-table-check-inside-data-field)
  8710. (org-table-get-field nil text)
  8711. (org-table-align)
  8712. (message "New field value inserted")))
  8713. (defun org-trim (s)
  8714. "Remove whitespace at beginning and end of string."
  8715. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8716. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8717. s)
  8718. (defun org-wrap (string &optional width lines)
  8719. "Wrap string to either a number of lines, or a width in characters.
  8720. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8721. that costs. If there is a word longer than WIDTH, the text is actually
  8722. wrapped to the length of that word.
  8723. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8724. many lines, whatever width that takes.
  8725. The return value is a list of lines, without newlines at the end."
  8726. (let* ((words (org-split-string string "[ \t\n]+"))
  8727. (maxword (apply 'max (mapcar 'org-string-width words)))
  8728. w ll)
  8729. (cond (width
  8730. (org-do-wrap words (max maxword width)))
  8731. (lines
  8732. (setq w maxword)
  8733. (setq ll (org-do-wrap words maxword))
  8734. (if (<= (length ll) lines)
  8735. ll
  8736. (setq ll words)
  8737. (while (> (length ll) lines)
  8738. (setq w (1+ w))
  8739. (setq ll (org-do-wrap words w)))
  8740. ll))
  8741. (t (error "Cannot wrap this")))))
  8742. (defun org-do-wrap (words width)
  8743. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8744. (let (lines line)
  8745. (while words
  8746. (setq line (pop words))
  8747. (while (and words (< (+ (length line) (length (car words))) width))
  8748. (setq line (concat line " " (pop words))))
  8749. (setq lines (push line lines)))
  8750. (nreverse lines)))
  8751. (defun org-split-string (string &optional separators)
  8752. "Splits STRING into substrings at SEPARATORS.
  8753. No empty strings are returned if there are matches at the beginning
  8754. and end of string."
  8755. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8756. (start 0)
  8757. notfirst
  8758. (list nil))
  8759. (while (and (string-match rexp string
  8760. (if (and notfirst
  8761. (= start (match-beginning 0))
  8762. (< start (length string)))
  8763. (1+ start) start))
  8764. (< (match-beginning 0) (length string)))
  8765. (setq notfirst t)
  8766. (or (eq (match-beginning 0) 0)
  8767. (and (eq (match-beginning 0) (match-end 0))
  8768. (eq (match-beginning 0) start))
  8769. (setq list
  8770. (cons (substring string start (match-beginning 0))
  8771. list)))
  8772. (setq start (match-end 0)))
  8773. (or (eq start (length string))
  8774. (setq list
  8775. (cons (substring string start)
  8776. list)))
  8777. (nreverse list)))
  8778. (defun org-table-map-tables (function)
  8779. "Apply FUNCTION to the start of all tables in the buffer."
  8780. (save-excursion
  8781. (save-restriction
  8782. (widen)
  8783. (goto-char (point-min))
  8784. (while (re-search-forward org-table-any-line-regexp nil t)
  8785. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8786. (beginning-of-line 1)
  8787. (if (looking-at org-table-line-regexp)
  8788. (save-excursion (funcall function)))
  8789. (re-search-forward org-table-any-border-regexp nil 1))))
  8790. (message "Mapping tables: done"))
  8791. (defvar org-timecnt) ; dynamically scoped parameter
  8792. (defun org-table-sum (&optional beg end nlast)
  8793. "Sum numbers in region of current table column.
  8794. The result will be displayed in the echo area, and will be available
  8795. as kill to be inserted with \\[yank].
  8796. If there is an active region, it is interpreted as a rectangle and all
  8797. numbers in that rectangle will be summed. If there is no active
  8798. region and point is located in a table column, sum all numbers in that
  8799. column.
  8800. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8801. numbers are assumed to be times as well (in decimal hours) and the
  8802. numbers are added as such.
  8803. If NLAST is a number, only the NLAST fields will actually be summed."
  8804. (interactive)
  8805. (save-excursion
  8806. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8807. (cond
  8808. ((and beg end)) ; beg and end given explicitly
  8809. ((org-region-active-p)
  8810. (setq beg (region-beginning) end (region-end)))
  8811. (t
  8812. (setq col (org-table-current-column))
  8813. (goto-char (org-table-begin))
  8814. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8815. (error "No table data"))
  8816. (org-table-goto-column col)
  8817. (setq beg (point))
  8818. (goto-char (org-table-end))
  8819. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8820. (error "No table data"))
  8821. (org-table-goto-column col)
  8822. (setq end (point))))
  8823. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8824. (items1 (cond ((not nlast) items)
  8825. ((>= nlast (length items)) items)
  8826. (t (setq items (reverse items))
  8827. (setcdr (nthcdr (1- nlast) items) nil)
  8828. (nreverse items))))
  8829. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8830. items1)))
  8831. (res (apply '+ numbers))
  8832. (sres (if (= org-timecnt 0)
  8833. (format "%g" res)
  8834. (setq diff (* 3600 res)
  8835. h (floor (/ diff 3600)) diff (mod diff 3600)
  8836. m (floor (/ diff 60)) diff (mod diff 60)
  8837. s diff)
  8838. (format "%d:%02d:%02d" h m s))))
  8839. (kill-new sres)
  8840. (if (interactive-p)
  8841. (message "%s"
  8842. (substitute-command-keys
  8843. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8844. (length numbers) sres))))
  8845. sres))))
  8846. (defun org-table-get-number-for-summing (s)
  8847. (let (n)
  8848. (if (string-match "^ *|? *" s)
  8849. (setq s (replace-match "" nil nil s)))
  8850. (if (string-match " *|? *$" s)
  8851. (setq s (replace-match "" nil nil s)))
  8852. (setq n (string-to-number s))
  8853. (cond
  8854. ((and (string-match "0" s)
  8855. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8856. ((string-match "\\`[ \t]+\\'" s) nil)
  8857. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8858. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8859. (m (string-to-number (or (match-string 2 s) "0")))
  8860. (s (string-to-number (or (match-string 4 s) "0"))))
  8861. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8862. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8863. ((equal n 0) nil)
  8864. (t n))))
  8865. (defun org-table-current-field-formula (&optional key noerror)
  8866. "Return the formula active for the current field.
  8867. Assumes that specials are in place.
  8868. If KEY is given, return the key to this formula.
  8869. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8870. (let* ((name (car (rassoc (list (org-current-line)
  8871. (org-table-current-column))
  8872. org-table-named-field-locations)))
  8873. (col (org-table-current-column))
  8874. (scol (int-to-string col))
  8875. (ref (format "@%d$%d" (org-table-current-dline) col))
  8876. (stored-list (org-table-get-stored-formulas noerror))
  8877. (ass (or (assoc name stored-list)
  8878. (assoc ref stored-list)
  8879. (assoc scol stored-list))))
  8880. (if key
  8881. (car ass)
  8882. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8883. (cdr ass))))))
  8884. (defun org-table-get-formula (&optional equation named)
  8885. "Read a formula from the minibuffer, offer stored formula as default.
  8886. When NAMED is non-nil, look for a named equation."
  8887. (let* ((stored-list (org-table-get-stored-formulas))
  8888. (name (car (rassoc (list (org-current-line)
  8889. (org-table-current-column))
  8890. org-table-named-field-locations)))
  8891. (ref (format "@%d$%d" (org-table-current-dline)
  8892. (org-table-current-column)))
  8893. (refass (assoc ref stored-list))
  8894. (scol (if named
  8895. (if name name ref)
  8896. (int-to-string (org-table-current-column))))
  8897. (dummy (and (or name refass) (not named)
  8898. (not (y-or-n-p "Replace field formula with column formula? " ))
  8899. (error "Abort")))
  8900. (name (or name ref))
  8901. (org-table-may-need-update nil)
  8902. (stored (cdr (assoc scol stored-list)))
  8903. (eq (cond
  8904. ((and stored equation (string-match "^ *=? *$" equation))
  8905. stored)
  8906. ((stringp equation)
  8907. equation)
  8908. (t (org-table-formula-from-user
  8909. (read-string
  8910. (org-table-formula-to-user
  8911. (format "%s formula %s%s="
  8912. (if named "Field" "Column")
  8913. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8914. scol))
  8915. (if stored (org-table-formula-to-user stored) "")
  8916. 'org-table-formula-history
  8917. )))))
  8918. mustsave)
  8919. (when (not (string-match "\\S-" eq))
  8920. ;; remove formula
  8921. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8922. (org-table-store-formulas stored-list)
  8923. (error "Formula removed"))
  8924. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8925. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8926. (if (and name (not named))
  8927. ;; We set the column equation, delete the named one.
  8928. (setq stored-list (delq (assoc name stored-list) stored-list)
  8929. mustsave t))
  8930. (if stored
  8931. (setcdr (assoc scol stored-list) eq)
  8932. (setq stored-list (cons (cons scol eq) stored-list)))
  8933. (if (or mustsave (not (equal stored eq)))
  8934. (org-table-store-formulas stored-list))
  8935. eq))
  8936. (defun org-table-store-formulas (alist)
  8937. "Store the list of formulas below the current table."
  8938. (setq alist (sort alist 'org-table-formula-less-p))
  8939. (save-excursion
  8940. (goto-char (org-table-end))
  8941. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8942. (progn
  8943. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8944. (goto-char (match-beginning 2))
  8945. (delete-region (match-beginning 2) (match-end 0)))
  8946. (insert "#+TBLFM:"))
  8947. (insert " "
  8948. (mapconcat (lambda (x)
  8949. (concat
  8950. (if (equal (string-to-char (car x)) ?@) "" "$")
  8951. (car x) "=" (cdr x)))
  8952. alist "::")
  8953. "\n")))
  8954. (defsubst org-table-formula-make-cmp-string (a)
  8955. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8956. (concat
  8957. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8958. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8959. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8960. (defun org-table-formula-less-p (a b)
  8961. "Compare two formulas for sorting."
  8962. (let ((as (org-table-formula-make-cmp-string (car a)))
  8963. (bs (org-table-formula-make-cmp-string (car b))))
  8964. (and as bs (string< as bs))))
  8965. (defun org-table-get-stored-formulas (&optional noerror)
  8966. "Return an alist with the stored formulas directly after current table."
  8967. (interactive)
  8968. (let (scol eq eq-alist strings string seen)
  8969. (save-excursion
  8970. (goto-char (org-table-end))
  8971. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8972. (setq strings (org-split-string (match-string 2) " *:: *"))
  8973. (while (setq string (pop strings))
  8974. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8975. (setq scol (if (match-end 2)
  8976. (match-string 2 string)
  8977. (match-string 1 string))
  8978. eq (match-string 3 string)
  8979. eq-alist (cons (cons scol eq) eq-alist))
  8980. (if (member scol seen)
  8981. (if noerror
  8982. (progn
  8983. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8984. (ding)
  8985. (sit-for 2))
  8986. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8987. (push scol seen))))))
  8988. (nreverse eq-alist)))
  8989. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8990. "Modify the equations after the table structure has been edited.
  8991. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8992. For all numbers larger than LIMIT, shift them by DELTA."
  8993. (save-excursion
  8994. (goto-char (org-table-end))
  8995. (when (looking-at "#\\+TBLFM:")
  8996. (let ((re (concat key "\\([0-9]+\\)"))
  8997. (re2
  8998. (when remove
  8999. (if (equal key "$")
  9000. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9001. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9002. s n a)
  9003. (when remove
  9004. (while (re-search-forward re2 (point-at-eol) t)
  9005. (replace-match "")))
  9006. (while (re-search-forward re (point-at-eol) t)
  9007. (setq s (match-string 1) n (string-to-number s))
  9008. (cond
  9009. ((setq a (assoc s replace))
  9010. (replace-match (concat key (cdr a)) t t))
  9011. ((and limit (> n limit))
  9012. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9013. (defun org-table-get-specials ()
  9014. "Get the column names and local parameters for this table."
  9015. (save-excursion
  9016. (let ((beg (org-table-begin)) (end (org-table-end))
  9017. names name fields fields1 field cnt
  9018. c v l line col types dlines hlines)
  9019. (setq org-table-column-names nil
  9020. org-table-local-parameters nil
  9021. org-table-named-field-locations nil
  9022. org-table-current-begin-line nil
  9023. org-table-current-begin-pos nil
  9024. org-table-current-line-types nil)
  9025. (goto-char beg)
  9026. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9027. (setq names (org-split-string (match-string 1) " *| *")
  9028. cnt 1)
  9029. (while (setq name (pop names))
  9030. (setq cnt (1+ cnt))
  9031. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9032. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9033. (setq org-table-column-names (nreverse org-table-column-names))
  9034. (setq org-table-column-name-regexp
  9035. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9036. (goto-char beg)
  9037. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9038. (setq fields (org-split-string (match-string 1) " *| *"))
  9039. (while (setq field (pop fields))
  9040. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9041. (push (cons (match-string 1 field) (match-string 2 field))
  9042. org-table-local-parameters))))
  9043. (goto-char beg)
  9044. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9045. (setq c (match-string 1)
  9046. fields (org-split-string (match-string 2) " *| *"))
  9047. (save-excursion
  9048. (beginning-of-line (if (equal c "_") 2 0))
  9049. (setq line (org-current-line) col 1)
  9050. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9051. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9052. (while (and fields1 (setq field (pop fields)))
  9053. (setq v (pop fields1) col (1+ col))
  9054. (when (and (stringp field) (stringp v)
  9055. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9056. (push (cons field v) org-table-local-parameters)
  9057. (push (list field line col) org-table-named-field-locations))))
  9058. ;; Analyse the line types
  9059. (goto-char beg)
  9060. (setq org-table-current-begin-line (org-current-line)
  9061. org-table-current-begin-pos (point)
  9062. l org-table-current-begin-line)
  9063. (while (looking-at "[ \t]*|\\(-\\)?")
  9064. (push (if (match-end 1) 'hline 'dline) types)
  9065. (if (match-end 1) (push l hlines) (push l dlines))
  9066. (beginning-of-line 2)
  9067. (setq l (1+ l)))
  9068. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9069. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9070. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9071. (defun org-table-maybe-eval-formula ()
  9072. "Check if the current field starts with \"=\" or \":=\".
  9073. If yes, store the formula and apply it."
  9074. ;; We already know we are in a table. Get field will only return a formula
  9075. ;; when appropriate. It might return a separator line, but no problem.
  9076. (when org-table-formula-evaluate-inline
  9077. (let* ((field (org-trim (or (org-table-get-field) "")))
  9078. named eq)
  9079. (when (string-match "^:?=\\(.*\\)" field)
  9080. (setq named (equal (string-to-char field) ?:)
  9081. eq (match-string 1 field))
  9082. (if (or (fboundp 'calc-eval)
  9083. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9084. (org-table-eval-formula (if named '(4) nil)
  9085. (org-table-formula-from-user eq))
  9086. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9087. (defvar org-recalc-commands nil
  9088. "List of commands triggering the recalculation of a line.
  9089. Will be filled automatically during use.")
  9090. (defvar org-recalc-marks
  9091. '((" " . "Unmarked: no special line, no automatic recalculation")
  9092. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9093. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9094. ("!" . "Column name definition line. Reference in formula as $name.")
  9095. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9096. ("_" . "Names for values in row below this one.")
  9097. ("^" . "Names for values in row above this one.")))
  9098. (defun org-table-rotate-recalc-marks (&optional newchar)
  9099. "Rotate the recalculation mark in the first column.
  9100. If in any row, the first field is not consistent with a mark,
  9101. insert a new column for the markers.
  9102. When there is an active region, change all the lines in the region,
  9103. after prompting for the marking character.
  9104. After each change, a message will be displayed indicating the meaning
  9105. of the new mark."
  9106. (interactive)
  9107. (unless (org-at-table-p) (error "Not at a table"))
  9108. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9109. (beg (org-table-begin))
  9110. (end (org-table-end))
  9111. (l (org-current-line))
  9112. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9113. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9114. (have-col
  9115. (save-excursion
  9116. (goto-char beg)
  9117. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9118. (col (org-table-current-column))
  9119. (forcenew (car (assoc newchar org-recalc-marks)))
  9120. epos new)
  9121. (when l1
  9122. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9123. (setq newchar (char-to-string (read-char-exclusive))
  9124. forcenew (car (assoc newchar org-recalc-marks))))
  9125. (if (and newchar (not forcenew))
  9126. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9127. newchar))
  9128. (if l1 (goto-line l1))
  9129. (save-excursion
  9130. (beginning-of-line 1)
  9131. (unless (looking-at org-table-dataline-regexp)
  9132. (error "Not at a table data line")))
  9133. (unless have-col
  9134. (org-table-goto-column 1)
  9135. (org-table-insert-column)
  9136. (org-table-goto-column (1+ col)))
  9137. (setq epos (point-at-eol))
  9138. (save-excursion
  9139. (beginning-of-line 1)
  9140. (org-table-get-field
  9141. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9142. (concat " "
  9143. (setq new (or forcenew
  9144. (cadr (member (match-string 1) marks))))
  9145. " ")
  9146. " # ")))
  9147. (if (and l1 l2)
  9148. (progn
  9149. (goto-line l1)
  9150. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9151. (and (looking-at org-table-dataline-regexp)
  9152. (org-table-get-field 1 (concat " " new " "))))
  9153. (goto-line l1)))
  9154. (if (not (= epos (point-at-eol))) (org-table-align))
  9155. (goto-line l)
  9156. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9157. (defun org-table-maybe-recalculate-line ()
  9158. "Recompute the current line if marked for it, and if we haven't just done it."
  9159. (interactive)
  9160. (and org-table-allow-automatic-line-recalculation
  9161. (not (and (memq last-command org-recalc-commands)
  9162. (equal org-last-recalc-line (org-current-line))))
  9163. (save-excursion (beginning-of-line 1)
  9164. (looking-at org-table-auto-recalculate-regexp))
  9165. (org-table-recalculate) t))
  9166. (defvar org-table-formula-debug nil
  9167. "Non-nil means, debug table formulas.
  9168. When nil, simply write \"#ERROR\" in corrupted fields.")
  9169. (make-variable-buffer-local 'org-table-formula-debug)
  9170. (defvar modes)
  9171. (defsubst org-set-calc-mode (var &optional value)
  9172. (if (stringp var)
  9173. (setq var (assoc var '(("D" calc-angle-mode deg)
  9174. ("R" calc-angle-mode rad)
  9175. ("F" calc-prefer-frac t)
  9176. ("S" calc-symbolic-mode t)))
  9177. value (nth 2 var) var (nth 1 var)))
  9178. (if (memq var modes)
  9179. (setcar (cdr (memq var modes)) value)
  9180. (cons var (cons value modes)))
  9181. modes)
  9182. (defun org-table-eval-formula (&optional arg equation
  9183. suppress-align suppress-const
  9184. suppress-store suppress-analysis)
  9185. "Replace the table field value at the cursor by the result of a calculation.
  9186. This function makes use of Dave Gillespie's Calc package, in my view the
  9187. most exciting program ever written for GNU Emacs. So you need to have Calc
  9188. installed in order to use this function.
  9189. In a table, this command replaces the value in the current field with the
  9190. result of a formula. It also installs the formula as the \"current\" column
  9191. formula, by storing it in a special line below the table. When called
  9192. with a `C-u' prefix, the current field must ba a named field, and the
  9193. formula is installed as valid in only this specific field.
  9194. When called with two `C-u' prefixes, insert the active equation
  9195. for the field back into the current field, so that it can be
  9196. edited there. This is useful in order to use \\[org-table-show-reference]
  9197. to check the referenced fields.
  9198. When called, the command first prompts for a formula, which is read in
  9199. the minibuffer. Previously entered formulas are available through the
  9200. history list, and the last used formula is offered as a default.
  9201. These stored formulas are adapted correctly when moving, inserting, or
  9202. deleting columns with the corresponding commands.
  9203. The formula can be any algebraic expression understood by the Calc package.
  9204. For details, see the Org-mode manual.
  9205. This function can also be called from Lisp programs and offers
  9206. additional arguments: EQUATION can be the formula to apply. If this
  9207. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9208. used to speed-up recursive calls by by-passing unnecessary aligns.
  9209. SUPPRESS-CONST suppresses the interpretation of constants in the
  9210. formula, assuming that this has been done already outside the function.
  9211. SUPPRESS-STORE means the formula should not be stored, either because
  9212. it is already stored, or because it is a modified equation that should
  9213. not overwrite the stored one."
  9214. (interactive "P")
  9215. (org-table-check-inside-data-field)
  9216. (or suppress-analysis (org-table-get-specials))
  9217. (if (equal arg '(16))
  9218. (let ((eq (org-table-current-field-formula)))
  9219. (or eq (error "No equation active for current field"))
  9220. (org-table-get-field nil eq)
  9221. (org-table-align)
  9222. (setq org-table-may-need-update t))
  9223. (let* (fields
  9224. (ndown (if (integerp arg) arg 1))
  9225. (org-table-automatic-realign nil)
  9226. (case-fold-search nil)
  9227. (down (> ndown 1))
  9228. (formula (if (and equation suppress-store)
  9229. equation
  9230. (org-table-get-formula equation (equal arg '(4)))))
  9231. (n0 (org-table-current-column))
  9232. (modes (copy-sequence org-calc-default-modes))
  9233. (numbers nil) ; was a variable, now fixed default
  9234. (keep-empty nil)
  9235. n form form0 bw fmt x ev orig c lispp literal)
  9236. ;; Parse the format string. Since we have a lot of modes, this is
  9237. ;; a lot of work. However, I think calc still uses most of the time.
  9238. (if (string-match ";" formula)
  9239. (let ((tmp (org-split-string formula ";")))
  9240. (setq formula (car tmp)
  9241. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9242. (nth 1 tmp)))
  9243. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9244. (setq c (string-to-char (match-string 1 fmt))
  9245. n (string-to-number (match-string 2 fmt)))
  9246. (if (= c ?p)
  9247. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9248. (setq modes (org-set-calc-mode
  9249. 'calc-float-format
  9250. (list (cdr (assoc c '((?n . float) (?f . fix)
  9251. (?s . sci) (?e . eng))))
  9252. n))))
  9253. (setq fmt (replace-match "" t t fmt)))
  9254. (if (string-match "[NT]" fmt)
  9255. (setq numbers (equal (match-string 0 fmt) "N")
  9256. fmt (replace-match "" t t fmt)))
  9257. (if (string-match "L" fmt)
  9258. (setq literal t
  9259. fmt (replace-match "" t t fmt)))
  9260. (if (string-match "E" fmt)
  9261. (setq keep-empty t
  9262. fmt (replace-match "" t t fmt)))
  9263. (while (string-match "[DRFS]" fmt)
  9264. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9265. (setq fmt (replace-match "" t t fmt)))
  9266. (unless (string-match "\\S-" fmt)
  9267. (setq fmt nil))))
  9268. (if (and (not suppress-const) org-table-formula-use-constants)
  9269. (setq formula (org-table-formula-substitute-names formula)))
  9270. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9271. (while (> ndown 0)
  9272. (setq fields (org-split-string
  9273. (org-no-properties
  9274. (buffer-substring (point-at-bol) (point-at-eol)))
  9275. " *| *"))
  9276. (if (eq numbers t)
  9277. (setq fields (mapcar
  9278. (lambda (x) (number-to-string (string-to-number x)))
  9279. fields)))
  9280. (setq ndown (1- ndown))
  9281. (setq form (copy-sequence formula)
  9282. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9283. (if (and lispp literal) (setq lispp 'literal))
  9284. ;; Check for old vertical references
  9285. (setq form (org-rewrite-old-row-references form))
  9286. ;; Insert complex ranges
  9287. (while (string-match org-table-range-regexp form)
  9288. (setq form
  9289. (replace-match
  9290. (save-match-data
  9291. (org-table-make-reference
  9292. (org-table-get-range (match-string 0 form) nil n0)
  9293. keep-empty numbers lispp))
  9294. t t form)))
  9295. ;; Insert simple ranges
  9296. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9297. (setq form
  9298. (replace-match
  9299. (save-match-data
  9300. (org-table-make-reference
  9301. (org-sublist
  9302. fields (string-to-number (match-string 1 form))
  9303. (string-to-number (match-string 2 form)))
  9304. keep-empty numbers lispp))
  9305. t t form)))
  9306. (setq form0 form)
  9307. ;; Insert the references to fields in same row
  9308. (while (string-match "\\$\\([0-9]+\\)" form)
  9309. (setq n (string-to-number (match-string 1 form))
  9310. x (nth (1- (if (= n 0) n0 n)) fields))
  9311. (unless x (error "Invalid field specifier \"%s\""
  9312. (match-string 0 form)))
  9313. (setq form (replace-match
  9314. (save-match-data
  9315. (org-table-make-reference x nil numbers lispp))
  9316. t t form)))
  9317. (if lispp
  9318. (setq ev (condition-case nil
  9319. (eval (eval (read form)))
  9320. (error "#ERROR"))
  9321. ev (if (numberp ev) (number-to-string ev) ev))
  9322. (or (fboundp 'calc-eval)
  9323. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9324. (setq ev (calc-eval (cons form modes)
  9325. (if numbers 'num))))
  9326. (when org-table-formula-debug
  9327. (with-output-to-temp-buffer "*Substitution History*"
  9328. (princ (format "Substitution history of formula
  9329. Orig: %s
  9330. $xyz-> %s
  9331. @r$c-> %s
  9332. $1-> %s\n" orig formula form0 form))
  9333. (if (listp ev)
  9334. (princ (format " %s^\nError: %s"
  9335. (make-string (car ev) ?\-) (nth 1 ev)))
  9336. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9337. ev (or fmt "NONE")
  9338. (if fmt (format fmt (string-to-number ev)) ev)))))
  9339. (setq bw (get-buffer-window "*Substitution History*"))
  9340. (shrink-window-if-larger-than-buffer bw)
  9341. (unless (and (interactive-p) (not ndown))
  9342. (unless (let (inhibit-redisplay)
  9343. (y-or-n-p "Debugging Formula. Continue to next? "))
  9344. (org-table-align)
  9345. (error "Abort"))
  9346. (delete-window bw)
  9347. (message "")))
  9348. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9349. (org-table-justify-field-maybe
  9350. (if fmt (format fmt (string-to-number ev)) ev))
  9351. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9352. (call-interactively 'org-return)
  9353. (setq ndown 0)))
  9354. (and down (org-table-maybe-recalculate-line))
  9355. (or suppress-align (and org-table-may-need-update
  9356. (org-table-align))))))
  9357. (defun org-table-put-field-property (prop value)
  9358. (save-excursion
  9359. (put-text-property (progn (skip-chars-backward "^|") (point))
  9360. (progn (skip-chars-forward "^|") (point))
  9361. prop value)))
  9362. (defun org-table-get-range (desc &optional tbeg col highlight)
  9363. "Get a calc vector from a column, accorting to descriptor DESC.
  9364. Optional arguments TBEG and COL can give the beginning of the table and
  9365. the current column, to avoid unnecessary parsing.
  9366. HIGHLIGHT means, just highlight the range."
  9367. (if (not (equal (string-to-char desc) ?@))
  9368. (setq desc (concat "@" desc)))
  9369. (save-excursion
  9370. (or tbeg (setq tbeg (org-table-begin)))
  9371. (or col (setq col (org-table-current-column)))
  9372. (let ((thisline (org-current-line))
  9373. beg end c1 c2 r1 r2 rangep tmp)
  9374. (unless (string-match org-table-range-regexp desc)
  9375. (error "Invalid table range specifier `%s'" desc))
  9376. (setq rangep (match-end 3)
  9377. r1 (and (match-end 1) (match-string 1 desc))
  9378. r2 (and (match-end 4) (match-string 4 desc))
  9379. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9380. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9381. (and c1 (setq c1 (+ (string-to-number c1)
  9382. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9383. (and c2 (setq c2 (+ (string-to-number c2)
  9384. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9385. (if (equal r1 "") (setq r1 nil))
  9386. (if (equal r2 "") (setq r2 nil))
  9387. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9388. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9389. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9390. (if (not r1) (setq r1 thisline))
  9391. (if (not r2) (setq r2 thisline))
  9392. (if (not c1) (setq c1 col))
  9393. (if (not c2) (setq c2 col))
  9394. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9395. ;; just one field
  9396. (progn
  9397. (goto-line r1)
  9398. (while (not (looking-at org-table-dataline-regexp))
  9399. (beginning-of-line 2))
  9400. (prog1 (org-trim (org-table-get-field c1))
  9401. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9402. ;; A range, return a vector
  9403. ;; First sort the numbers to get a regular ractangle
  9404. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9405. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9406. (goto-line r1)
  9407. (while (not (looking-at org-table-dataline-regexp))
  9408. (beginning-of-line 2))
  9409. (org-table-goto-column c1)
  9410. (setq beg (point))
  9411. (goto-line r2)
  9412. (while (not (looking-at org-table-dataline-regexp))
  9413. (beginning-of-line 0))
  9414. (org-table-goto-column c2)
  9415. (setq end (point))
  9416. (if highlight
  9417. (org-table-highlight-rectangle
  9418. beg (progn (skip-chars-forward "^|\n") (point))))
  9419. ;; return string representation of calc vector
  9420. (mapcar 'org-trim
  9421. (apply 'append (org-table-copy-region beg end)))))))
  9422. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9423. "Analyze descriptor DESC and retrieve the corresponding line number.
  9424. The cursor is currently in line CLINE, the table begins in line BLINE,
  9425. and TABLE is a vector with line types."
  9426. (if (string-match "^[0-9]+$" desc)
  9427. (aref org-table-dlines (string-to-number desc))
  9428. (setq cline (or cline (org-current-line))
  9429. bline (or bline org-table-current-begin-line)
  9430. table (or table org-table-current-line-types))
  9431. (if (or
  9432. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9433. ;; 1 2 3 4 5 6
  9434. (and (not (match-end 3)) (not (match-end 6)))
  9435. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9436. (error "invalid row descriptor `%s'" desc))
  9437. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9438. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9439. (odir (and (match-end 5) (match-string 5 desc)))
  9440. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9441. (i (- cline bline))
  9442. (rel (and (match-end 6)
  9443. (or (and (match-end 1) (not (match-end 3)))
  9444. (match-end 5)))))
  9445. (if (and hn (not hdir))
  9446. (progn
  9447. (setq i 0 hdir "+")
  9448. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9449. (if (and (not hn) on (not odir))
  9450. (error "should never happen");;(aref org-table-dlines on)
  9451. (if (and hn (> hn 0))
  9452. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9453. (if on
  9454. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9455. (+ bline i)))))
  9456. (defun org-find-row-type (table i type backwards relative n)
  9457. (let ((l (length table)))
  9458. (while (> n 0)
  9459. (while (and (setq i (+ i (if backwards -1 1)))
  9460. (>= i 0) (< i l)
  9461. (not (eq (aref table i) type))
  9462. (if (and relative (eq (aref table i) 'hline))
  9463. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9464. t)))
  9465. (setq n (1- n)))
  9466. (if (or (< i 0) (>= i l))
  9467. (error "Row descriptior leads outside table")
  9468. i)))
  9469. (defun org-rewrite-old-row-references (s)
  9470. (if (string-match "&[-+0-9I]" s)
  9471. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9472. s))
  9473. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9474. "Convert list ELEMENTS to something appropriate to insert into formula.
  9475. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9476. NUMBERS indicates that everything should be converted to numbers.
  9477. LISPP means to return something appropriate for a Lisp list."
  9478. (if (stringp elements) ; just a single val
  9479. (if lispp
  9480. (if (eq lispp 'literal)
  9481. elements
  9482. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9483. (if (equal elements "") (setq elements "0"))
  9484. (if numbers (number-to-string (string-to-number elements)) elements))
  9485. (unless keep-empty
  9486. (setq elements
  9487. (delq nil
  9488. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9489. elements))))
  9490. (setq elements (or elements '("0")))
  9491. (if lispp
  9492. (mapconcat
  9493. (lambda (x)
  9494. (if (eq lispp 'literal)
  9495. x
  9496. (prin1-to-string (if numbers (string-to-number x) x))))
  9497. elements " ")
  9498. (concat "[" (mapconcat
  9499. (lambda (x)
  9500. (if numbers (number-to-string (string-to-number x)) x))
  9501. elements
  9502. ",") "]"))))
  9503. (defun org-table-recalculate (&optional all noalign)
  9504. "Recalculate the current table line by applying all stored formulas.
  9505. With prefix arg ALL, do this for all lines in the table."
  9506. (interactive "P")
  9507. (or (memq this-command org-recalc-commands)
  9508. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9509. (unless (org-at-table-p) (error "Not at a table"))
  9510. (if (equal all '(16))
  9511. (org-table-iterate)
  9512. (org-table-get-specials)
  9513. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9514. (lambda (a b) (string< (car a) (car b)))))
  9515. (inhibit-redisplay (not debug-on-error))
  9516. (line-re org-table-dataline-regexp)
  9517. (thisline (org-current-line))
  9518. (thiscol (org-table-current-column))
  9519. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9520. ;; Insert constants in all formulas
  9521. (setq eqlist
  9522. (mapcar (lambda (x)
  9523. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9524. x)
  9525. eqlist))
  9526. ;; Split the equation list
  9527. (while (setq eq (pop eqlist))
  9528. (if (<= (string-to-char (car eq)) ?9)
  9529. (push eq eqlnum)
  9530. (push eq eqlname)))
  9531. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9532. (if all
  9533. (progn
  9534. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9535. (goto-char (setq beg (org-table-begin)))
  9536. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9537. ;; This is a table with marked lines, compute selected lines
  9538. (setq line-re org-table-recalculate-regexp)
  9539. ;; Move forward to the first non-header line
  9540. (if (and (re-search-forward org-table-dataline-regexp end t)
  9541. (re-search-forward org-table-hline-regexp end t)
  9542. (re-search-forward org-table-dataline-regexp end t))
  9543. (setq beg (match-beginning 0))
  9544. nil))) ;; just leave beg where it is
  9545. (setq beg (point-at-bol)
  9546. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9547. (goto-char beg)
  9548. (and all (message "Re-applying formulas to full table..."))
  9549. ;; First find the named fields, and mark them untouchanble
  9550. (remove-text-properties beg end '(org-untouchable t))
  9551. (while (setq eq (pop eqlname))
  9552. (setq name (car eq)
  9553. a (assoc name org-table-named-field-locations))
  9554. (and (not a)
  9555. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9556. (setq a (list name
  9557. (aref org-table-dlines
  9558. (string-to-number (match-string 1 name)))
  9559. (string-to-number (match-string 2 name)))))
  9560. (when (and a (or all (equal (nth 1 a) thisline)))
  9561. (message "Re-applying formula to field: %s" name)
  9562. (goto-line (nth 1 a))
  9563. (org-table-goto-column (nth 2 a))
  9564. (push (append a (list (cdr eq))) eqlname1)
  9565. (org-table-put-field-property :org-untouchable t)))
  9566. ;; Now evauluate the column formulas, but skip fields covered by
  9567. ;; field formulas
  9568. (goto-char beg)
  9569. (while (re-search-forward line-re end t)
  9570. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9571. ;; Unprotected line, recalculate
  9572. (and all (message "Re-applying formulas to full table...(line %d)"
  9573. (setq cnt (1+ cnt))))
  9574. (setq org-last-recalc-line (org-current-line))
  9575. (setq eql eqlnum)
  9576. (while (setq entry (pop eql))
  9577. (goto-line org-last-recalc-line)
  9578. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9579. (unless (get-text-property (point) :org-untouchable)
  9580. (org-table-eval-formula nil (cdr entry)
  9581. 'noalign 'nocst 'nostore 'noanalysis)))))
  9582. ;; Now evaluate the field formulas
  9583. (while (setq eq (pop eqlname1))
  9584. (message "Re-applying formula to field: %s" (car eq))
  9585. (goto-line (nth 1 eq))
  9586. (org-table-goto-column (nth 2 eq))
  9587. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9588. 'nostore 'noanalysis))
  9589. (goto-line thisline)
  9590. (org-table-goto-column thiscol)
  9591. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9592. (or noalign (and org-table-may-need-update (org-table-align))
  9593. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9594. ;; back to initial position
  9595. (message "Re-applying formulas...done")
  9596. (goto-line thisline)
  9597. (org-table-goto-column thiscol)
  9598. (or noalign (and org-table-may-need-update (org-table-align))
  9599. (and all (message "Re-applying formulas...done"))))))
  9600. (defun org-table-iterate (&optional arg)
  9601. "Recalculate the table until it does not change anymore."
  9602. (interactive "P")
  9603. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9604. (i 0)
  9605. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9606. thistbl)
  9607. (catch 'exit
  9608. (while (< i imax)
  9609. (setq i (1+ i))
  9610. (org-table-recalculate 'all)
  9611. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9612. (if (not (string= lasttbl thistbl))
  9613. (setq lasttbl thistbl)
  9614. (if (> i 1)
  9615. (message "Convergence after %d iterations" i)
  9616. (message "Table was already stable"))
  9617. (throw 'exit t)))
  9618. (error "No convergence after %d iterations" i))))
  9619. (defun org-table-formula-substitute-names (f)
  9620. "Replace $const with values in string F."
  9621. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9622. ;; First, check for column names
  9623. (while (setq start (string-match org-table-column-name-regexp f start))
  9624. (setq start (1+ start))
  9625. (setq a (assoc (match-string 1 f) org-table-column-names))
  9626. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9627. ;; Parameters and constants
  9628. (setq start 0)
  9629. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9630. (setq start (1+ start))
  9631. (if (setq a (save-match-data
  9632. (org-table-get-constant (match-string 1 f))))
  9633. (setq f (replace-match
  9634. (concat (if pp "(") a (if pp ")")) t t f))))
  9635. (if org-table-formula-debug
  9636. (put-text-property 0 (length f) :orig-formula f1 f))
  9637. f))
  9638. (defun org-table-get-constant (const)
  9639. "Find the value for a parameter or constant in a formula.
  9640. Parameters get priority."
  9641. (or (cdr (assoc const org-table-local-parameters))
  9642. (cdr (assoc const org-table-formula-constants-local))
  9643. (cdr (assoc const org-table-formula-constants))
  9644. (and (fboundp 'constants-get) (constants-get const))
  9645. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9646. (org-entry-get nil (substring const 5) 'inherit))
  9647. "#UNDEFINED_NAME"))
  9648. (defvar org-table-fedit-map
  9649. (let ((map (make-sparse-keymap)))
  9650. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9651. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9652. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9653. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9654. (org-defkey map "\C-c?" 'org-table-show-reference)
  9655. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9656. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9657. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9658. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9659. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9660. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9661. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9662. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9663. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9664. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9665. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9666. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9667. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9668. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9669. map))
  9670. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9671. '("Edit-Formulas"
  9672. ["Finish and Install" org-table-fedit-finish t]
  9673. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9674. ["Abort" org-table-fedit-abort t]
  9675. "--"
  9676. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9677. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9678. "--"
  9679. "Shift Reference at Point"
  9680. ["Up" org-table-fedit-ref-up t]
  9681. ["Down" org-table-fedit-ref-down t]
  9682. ["Left" org-table-fedit-ref-left t]
  9683. ["Right" org-table-fedit-ref-right t]
  9684. "-"
  9685. "Change Test Row for Column Formulas"
  9686. ["Up" org-table-fedit-line-up t]
  9687. ["Down" org-table-fedit-line-down t]
  9688. "--"
  9689. ["Scroll Table Window" org-table-fedit-scroll t]
  9690. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9691. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9692. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9693. org-table-overlay-coordinates)]
  9694. "--"
  9695. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9696. :style toggle :selected org-table-buffer-is-an]))
  9697. (defvar org-pos)
  9698. (defun org-table-edit-formulas ()
  9699. "Edit the formulas of the current table in a separate buffer."
  9700. (interactive)
  9701. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9702. (beginning-of-line 0))
  9703. (unless (org-at-table-p) (error "Not at a table"))
  9704. (org-table-get-specials)
  9705. (let ((key (org-table-current-field-formula 'key 'noerror))
  9706. (eql (sort (org-table-get-stored-formulas 'noerror)
  9707. 'org-table-formula-less-p))
  9708. (pos (move-marker (make-marker) (point)))
  9709. (startline 1)
  9710. (wc (current-window-configuration))
  9711. (titles '((column . "# Column Formulas\n")
  9712. (field . "# Field Formulas\n")
  9713. (named . "# Named Field Formulas\n")))
  9714. entry s type title)
  9715. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9716. (erase-buffer)
  9717. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9718. (let ((font-lock-global-modes '(not fundamental-mode)))
  9719. (fundamental-mode))
  9720. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9721. (org-set-local 'org-pos pos)
  9722. (org-set-local 'org-window-configuration wc)
  9723. (use-local-map org-table-fedit-map)
  9724. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9725. (easy-menu-add org-table-fedit-menu)
  9726. (setq startline (org-current-line))
  9727. (while (setq entry (pop eql))
  9728. (setq type (cond
  9729. ((equal (string-to-char (car entry)) ?@) 'field)
  9730. ((string-match "^[0-9]" (car entry)) 'column)
  9731. (t 'named)))
  9732. (when (setq title (assq type titles))
  9733. (or (bobp) (insert "\n"))
  9734. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9735. (setq titles (delq title titles)))
  9736. (if (equal key (car entry)) (setq startline (org-current-line)))
  9737. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9738. (car entry) " = " (cdr entry) "\n"))
  9739. (remove-text-properties 0 (length s) '(face nil) s)
  9740. (insert s))
  9741. (if (eq org-table-use-standard-references t)
  9742. (org-table-fedit-toggle-ref-type))
  9743. (goto-line startline)
  9744. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9745. (defun org-table-fedit-post-command ()
  9746. (when (not (memq this-command '(lisp-complete-symbol)))
  9747. (let ((win (selected-window)))
  9748. (save-excursion
  9749. (condition-case nil
  9750. (org-table-show-reference)
  9751. (error nil))
  9752. (select-window win)))))
  9753. (defun org-table-formula-to-user (s)
  9754. "Convert a formula from internal to user representation."
  9755. (if (eq org-table-use-standard-references t)
  9756. (org-table-convert-refs-to-an s)
  9757. s))
  9758. (defun org-table-formula-from-user (s)
  9759. "Convert a formula from user to internal representation."
  9760. (if org-table-use-standard-references
  9761. (org-table-convert-refs-to-rc s)
  9762. s))
  9763. (defun org-table-convert-refs-to-rc (s)
  9764. "Convert spreadsheet references from AB7 to @7$28.
  9765. Works for single references, but also for entire formulas and even the
  9766. full TBLFM line."
  9767. (let ((start 0))
  9768. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9769. (cond
  9770. ((match-end 3)
  9771. ;; format match, just advance
  9772. (setq start (match-end 0)))
  9773. ((and (> (match-beginning 0) 0)
  9774. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9775. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9776. ;; 3.e5 or something like this.
  9777. (setq start (match-end 0)))
  9778. (t
  9779. (setq start (match-beginning 0)
  9780. s (replace-match
  9781. (if (equal (match-string 2 s) "&")
  9782. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9783. (format "@%d$%d"
  9784. (string-to-number (match-string 2 s))
  9785. (org-letters-to-number (match-string 1 s))))
  9786. t t s)))))
  9787. s))
  9788. (defun org-table-convert-refs-to-an (s)
  9789. "Convert spreadsheet references from to @7$28 to AB7.
  9790. Works for single references, but also for entire formulas and even the
  9791. full TBLFM line."
  9792. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9793. (setq s (replace-match
  9794. (format "%s%d"
  9795. (org-number-to-letters
  9796. (string-to-number (match-string 2 s)))
  9797. (string-to-number (match-string 1 s)))
  9798. t t s)))
  9799. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9800. (setq s (replace-match (concat "\\1"
  9801. (org-number-to-letters
  9802. (string-to-number (match-string 2 s))) "&")
  9803. t nil s)))
  9804. s)
  9805. (defun org-letters-to-number (s)
  9806. "Convert a base 26 number represented by letters into an integer.
  9807. For example: AB -> 28."
  9808. (let ((n 0))
  9809. (setq s (upcase s))
  9810. (while (> (length s) 0)
  9811. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9812. s (substring s 1)))
  9813. n))
  9814. (defun org-number-to-letters (n)
  9815. "Convert an integer into a base 26 number represented by letters.
  9816. For example: 28 -> AB."
  9817. (let ((s ""))
  9818. (while (> n 0)
  9819. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9820. n (/ (1- n) 26)))
  9821. s))
  9822. (defun org-table-fedit-convert-buffer (function)
  9823. "Convert all references in this buffer, using FUNTION."
  9824. (let ((line (org-current-line)))
  9825. (goto-char (point-min))
  9826. (while (not (eobp))
  9827. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9828. (delete-region (point) (point-at-eol))
  9829. (or (eobp) (forward-char 1)))
  9830. (goto-line line)))
  9831. (defun org-table-fedit-toggle-ref-type ()
  9832. "Convert all references in the buffer from B3 to @3$2 and back."
  9833. (interactive)
  9834. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9835. (org-table-fedit-convert-buffer
  9836. (if org-table-buffer-is-an
  9837. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9838. (message "Reference type switched to %s"
  9839. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9840. (defun org-table-fedit-ref-up ()
  9841. "Shift the reference at point one row/hline up."
  9842. (interactive)
  9843. (org-table-fedit-shift-reference 'up))
  9844. (defun org-table-fedit-ref-down ()
  9845. "Shift the reference at point one row/hline down."
  9846. (interactive)
  9847. (org-table-fedit-shift-reference 'down))
  9848. (defun org-table-fedit-ref-left ()
  9849. "Shift the reference at point one field to the left."
  9850. (interactive)
  9851. (org-table-fedit-shift-reference 'left))
  9852. (defun org-table-fedit-ref-right ()
  9853. "Shift the reference at point one field to the right."
  9854. (interactive)
  9855. (org-table-fedit-shift-reference 'right))
  9856. (defun org-table-fedit-shift-reference (dir)
  9857. (cond
  9858. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9859. (if (memq dir '(left right))
  9860. (org-rematch-and-replace 1 (eq dir 'left))
  9861. (error "Cannot shift reference in this direction")))
  9862. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9863. ;; A B3-like reference
  9864. (if (memq dir '(up down))
  9865. (org-rematch-and-replace 2 (eq dir 'up))
  9866. (org-rematch-and-replace 1 (eq dir 'left))))
  9867. ((org-at-regexp-p
  9868. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9869. ;; An internal reference
  9870. (if (memq dir '(up down))
  9871. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9872. (org-rematch-and-replace 5 (eq dir 'left))))))
  9873. (defun org-rematch-and-replace (n &optional decr hline)
  9874. "Re-match the group N, and replace it with the shifted refrence."
  9875. (or (match-end n) (error "Cannot shift reference in this direction"))
  9876. (goto-char (match-beginning n))
  9877. (and (looking-at (regexp-quote (match-string n)))
  9878. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9879. t t)))
  9880. (defun org-shift-refpart (ref &optional decr hline)
  9881. "Shift a refrence part REF.
  9882. If DECR is set, decrease the references row/column, else increase.
  9883. If HLINE is set, this may be a hline reference, it certainly is not
  9884. a translation reference."
  9885. (save-match-data
  9886. (let* ((sign (string-match "^[-+]" ref)) n)
  9887. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9888. (cond
  9889. ((and hline (string-match "^I+" ref))
  9890. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9891. (setq n (+ n (if decr -1 1)))
  9892. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9893. (if sign
  9894. (setq sign (if (< n 0) "-" "+") n (abs n))
  9895. (setq n (max 1 n)))
  9896. (concat sign (make-string n ?I)))
  9897. ((string-match "^[0-9]+" ref)
  9898. (setq n (string-to-number (concat sign ref)))
  9899. (setq n (+ n (if decr -1 1)))
  9900. (if sign
  9901. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9902. (number-to-string (max 1 n))))
  9903. ((string-match "^[a-zA-Z]+" ref)
  9904. (org-number-to-letters
  9905. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9906. (t (error "Cannot shift reference"))))))
  9907. (defun org-table-fedit-toggle-coordinates ()
  9908. "Toggle the display of coordinates in the refrenced table."
  9909. (interactive)
  9910. (let ((pos (marker-position org-pos)))
  9911. (with-current-buffer (marker-buffer org-pos)
  9912. (save-excursion
  9913. (goto-char pos)
  9914. (org-table-toggle-coordinate-overlays)))))
  9915. (defun org-table-fedit-finish (&optional arg)
  9916. "Parse the buffer for formula definitions and install them.
  9917. With prefix ARG, apply the new formulas to the table."
  9918. (interactive "P")
  9919. (org-table-remove-rectangle-highlight)
  9920. (if org-table-use-standard-references
  9921. (progn
  9922. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9923. (setq org-table-buffer-is-an nil)))
  9924. (let ((pos org-pos) eql var form)
  9925. (goto-char (point-min))
  9926. (while (re-search-forward
  9927. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9928. nil t)
  9929. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9930. form (match-string 3))
  9931. (setq form (org-trim form))
  9932. (when (not (equal form ""))
  9933. (while (string-match "[ \t]*\n[ \t]*" form)
  9934. (setq form (replace-match " " t t form)))
  9935. (when (assoc var eql)
  9936. (error "Double formulas for %s" var))
  9937. (push (cons var form) eql)))
  9938. (setq org-pos nil)
  9939. (set-window-configuration org-window-configuration)
  9940. (select-window (get-buffer-window (marker-buffer pos)))
  9941. (goto-char pos)
  9942. (unless (org-at-table-p)
  9943. (error "Lost table position - cannot install formulae"))
  9944. (org-table-store-formulas eql)
  9945. (move-marker pos nil)
  9946. (kill-buffer "*Edit Formulas*")
  9947. (if arg
  9948. (org-table-recalculate 'all)
  9949. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9950. (defun org-table-fedit-abort ()
  9951. "Abort editing formulas, without installing the changes."
  9952. (interactive)
  9953. (org-table-remove-rectangle-highlight)
  9954. (let ((pos org-pos))
  9955. (set-window-configuration org-window-configuration)
  9956. (select-window (get-buffer-window (marker-buffer pos)))
  9957. (goto-char pos)
  9958. (move-marker pos nil)
  9959. (message "Formula editing aborted without installing changes")))
  9960. (defun org-table-fedit-lisp-indent ()
  9961. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9962. (interactive)
  9963. (let ((pos (point)) beg end ind)
  9964. (beginning-of-line 1)
  9965. (cond
  9966. ((looking-at "[ \t]")
  9967. (goto-char pos)
  9968. (call-interactively 'lisp-indent-line))
  9969. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9970. ((not (fboundp 'pp-buffer))
  9971. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9972. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9973. (goto-char (- (match-end 0) 2))
  9974. (setq beg (point))
  9975. (setq ind (make-string (current-column) ?\ ))
  9976. (condition-case nil (forward-sexp 1)
  9977. (error
  9978. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9979. (setq end (point))
  9980. (save-restriction
  9981. (narrow-to-region beg end)
  9982. (if (eq last-command this-command)
  9983. (progn
  9984. (goto-char (point-min))
  9985. (setq this-command nil)
  9986. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9987. (replace-match " ")))
  9988. (pp-buffer)
  9989. (untabify (point-min) (point-max))
  9990. (goto-char (1+ (point-min)))
  9991. (while (re-search-forward "^." nil t)
  9992. (beginning-of-line 1)
  9993. (insert ind))
  9994. (goto-char (point-max))
  9995. (backward-delete-char 1)))
  9996. (goto-char beg))
  9997. (t nil))))
  9998. (defvar org-show-positions nil)
  9999. (defun org-table-show-reference (&optional local)
  10000. "Show the location/value of the $ expression at point."
  10001. (interactive)
  10002. (org-table-remove-rectangle-highlight)
  10003. (catch 'exit
  10004. (let ((pos (if local (point) org-pos))
  10005. (face2 'highlight)
  10006. (org-inhibit-highlight-removal t)
  10007. (win (selected-window))
  10008. (org-show-positions nil)
  10009. var name e what match dest)
  10010. (if local (org-table-get-specials))
  10011. (setq what (cond
  10012. ((or (org-at-regexp-p org-table-range-regexp2)
  10013. (org-at-regexp-p org-table-translate-regexp)
  10014. (org-at-regexp-p org-table-range-regexp))
  10015. (setq match
  10016. (save-match-data
  10017. (org-table-convert-refs-to-rc (match-string 0))))
  10018. 'range)
  10019. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10020. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10021. ((not local) nil)
  10022. (t (error "No reference at point")))
  10023. match (and what (or match (match-string 0))))
  10024. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10025. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10026. 'secondary-selection))
  10027. (org-add-hook 'before-change-functions
  10028. 'org-table-remove-rectangle-highlight)
  10029. (if (eq what 'name) (setq var (substring match 1)))
  10030. (when (eq what 'range)
  10031. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10032. (setq match (org-table-formula-substitute-names match)))
  10033. (unless local
  10034. (save-excursion
  10035. (end-of-line 1)
  10036. (re-search-backward "^\\S-" nil t)
  10037. (beginning-of-line 1)
  10038. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10039. (setq dest
  10040. (save-match-data
  10041. (org-table-convert-refs-to-rc (match-string 1))))
  10042. (org-table-add-rectangle-overlay
  10043. (match-beginning 1) (match-end 1) face2))))
  10044. (if (and (markerp pos) (marker-buffer pos))
  10045. (if (get-buffer-window (marker-buffer pos))
  10046. (select-window (get-buffer-window (marker-buffer pos)))
  10047. (org-switch-to-buffer-other-window (get-buffer-window
  10048. (marker-buffer pos)))))
  10049. (goto-char pos)
  10050. (org-table-force-dataline)
  10051. (when dest
  10052. (setq name (substring dest 1))
  10053. (cond
  10054. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10055. (setq e (assoc name org-table-named-field-locations))
  10056. (goto-line (nth 1 e))
  10057. (org-table-goto-column (nth 2 e)))
  10058. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10059. (let ((l (string-to-number (match-string 1 dest)))
  10060. (c (string-to-number (match-string 2 dest))))
  10061. (goto-line (aref org-table-dlines l))
  10062. (org-table-goto-column c)))
  10063. (t (org-table-goto-column (string-to-number name))))
  10064. (move-marker pos (point))
  10065. (org-table-highlight-rectangle nil nil face2))
  10066. (cond
  10067. ((equal dest match))
  10068. ((not match))
  10069. ((eq what 'range)
  10070. (condition-case nil
  10071. (save-excursion
  10072. (org-table-get-range match nil nil 'highlight))
  10073. (error nil)))
  10074. ((setq e (assoc var org-table-named-field-locations))
  10075. (goto-line (nth 1 e))
  10076. (org-table-goto-column (nth 2 e))
  10077. (org-table-highlight-rectangle (point) (point))
  10078. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10079. ((setq e (assoc var org-table-column-names))
  10080. (org-table-goto-column (string-to-number (cdr e)))
  10081. (org-table-highlight-rectangle (point) (point))
  10082. (goto-char (org-table-begin))
  10083. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10084. (org-table-end) t)
  10085. (progn
  10086. (goto-char (match-beginning 1))
  10087. (org-table-highlight-rectangle)
  10088. (message "Named column (column %s)" (cdr e)))
  10089. (error "Column name not found")))
  10090. ((eq what 'column)
  10091. ;; column number
  10092. (org-table-goto-column (string-to-number (substring match 1)))
  10093. (org-table-highlight-rectangle (point) (point))
  10094. (message "Column %s" (substring match 1)))
  10095. ((setq e (assoc var org-table-local-parameters))
  10096. (goto-char (org-table-begin))
  10097. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10098. (progn
  10099. (goto-char (match-beginning 1))
  10100. (org-table-highlight-rectangle)
  10101. (message "Local parameter."))
  10102. (error "Parameter not found")))
  10103. (t
  10104. (cond
  10105. ((not var) (error "No reference at point"))
  10106. ((setq e (assoc var org-table-formula-constants-local))
  10107. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10108. var (cdr e)))
  10109. ((setq e (assoc var org-table-formula-constants))
  10110. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10111. var (cdr e)))
  10112. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10113. (message "Constant: $%s=%s, from `constants.el'%s."
  10114. var e (format " (%s units)" constants-unit-system)))
  10115. (t (error "Undefined name $%s" var)))))
  10116. (goto-char pos)
  10117. (when (and org-show-positions
  10118. (not (memq this-command '(org-table-fedit-scroll
  10119. org-table-fedit-scroll-down))))
  10120. (push pos org-show-positions)
  10121. (push org-table-current-begin-pos org-show-positions)
  10122. (let ((min (apply 'min org-show-positions))
  10123. (max (apply 'max org-show-positions)))
  10124. (goto-char min) (recenter 0)
  10125. (goto-char max)
  10126. (or (pos-visible-in-window-p max) (recenter -1))))
  10127. (select-window win))))
  10128. (defun org-table-force-dataline ()
  10129. "Make sure the cursor is in a dataline in a table."
  10130. (unless (save-excursion
  10131. (beginning-of-line 1)
  10132. (looking-at org-table-dataline-regexp))
  10133. (let* ((re org-table-dataline-regexp)
  10134. (p1 (save-excursion (re-search-forward re nil 'move)))
  10135. (p2 (save-excursion (re-search-backward re nil 'move))))
  10136. (cond ((and p1 p2)
  10137. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10138. p1 p2)))
  10139. ((or p1 p2) (goto-char (or p1 p2)))
  10140. (t (error "No table dataline around here"))))))
  10141. (defun org-table-fedit-line-up ()
  10142. "Move cursor one line up in the window showing the table."
  10143. (interactive)
  10144. (org-table-fedit-move 'previous-line))
  10145. (defun org-table-fedit-line-down ()
  10146. "Move cursor one line down in the window showing the table."
  10147. (interactive)
  10148. (org-table-fedit-move 'next-line))
  10149. (defun org-table-fedit-move (command)
  10150. "Move the cursor in the window shoinw the table.
  10151. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10152. (let ((org-table-allow-automatic-line-recalculation nil)
  10153. (pos org-pos) (win (selected-window)) p)
  10154. (select-window (get-buffer-window (marker-buffer org-pos)))
  10155. (setq p (point))
  10156. (call-interactively command)
  10157. (while (and (org-at-table-p)
  10158. (org-at-table-hline-p))
  10159. (call-interactively command))
  10160. (or (org-at-table-p) (goto-char p))
  10161. (move-marker pos (point))
  10162. (select-window win)))
  10163. (defun org-table-fedit-scroll (N)
  10164. (interactive "p")
  10165. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10166. (scroll-other-window N)))
  10167. (defun org-table-fedit-scroll-down (N)
  10168. (interactive "p")
  10169. (org-table-fedit-scroll (- N)))
  10170. (defvar org-table-rectangle-overlays nil)
  10171. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10172. "Add a new overlay."
  10173. (let ((ov (org-make-overlay beg end)))
  10174. (org-overlay-put ov 'face (or face 'secondary-selection))
  10175. (push ov org-table-rectangle-overlays)))
  10176. (defun org-table-highlight-rectangle (&optional beg end face)
  10177. "Highlight rectangular region in a table."
  10178. (setq beg (or beg (point)) end (or end (point)))
  10179. (let ((b (min beg end))
  10180. (e (max beg end))
  10181. l1 c1 l2 c2 tmp)
  10182. (and (boundp 'org-show-positions)
  10183. (setq org-show-positions (cons b (cons e org-show-positions))))
  10184. (goto-char (min beg end))
  10185. (setq l1 (org-current-line)
  10186. c1 (org-table-current-column))
  10187. (goto-char (max beg end))
  10188. (setq l2 (org-current-line)
  10189. c2 (org-table-current-column))
  10190. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10191. (goto-line l1)
  10192. (beginning-of-line 1)
  10193. (loop for line from l1 to l2 do
  10194. (when (looking-at org-table-dataline-regexp)
  10195. (org-table-goto-column c1)
  10196. (skip-chars-backward "^|\n") (setq beg (point))
  10197. (org-table-goto-column c2)
  10198. (skip-chars-forward "^|\n") (setq end (point))
  10199. (org-table-add-rectangle-overlay beg end face))
  10200. (beginning-of-line 2))
  10201. (goto-char b))
  10202. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10203. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10204. "Remove the rectangle overlays."
  10205. (unless org-inhibit-highlight-removal
  10206. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10207. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10208. (setq org-table-rectangle-overlays nil)))
  10209. (defvar org-table-coordinate-overlays nil
  10210. "Collects the cooordinate grid overlays, so that they can be removed.")
  10211. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10212. (defun org-table-overlay-coordinates ()
  10213. "Add overlays to the table at point, to show row/column coordinates."
  10214. (interactive)
  10215. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10216. (setq org-table-coordinate-overlays nil)
  10217. (save-excursion
  10218. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10219. (goto-char (org-table-begin))
  10220. (while (org-at-table-p)
  10221. (setq eol (point-at-eol))
  10222. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10223. (push ov org-table-coordinate-overlays)
  10224. (setq hline (looking-at org-table-hline-regexp))
  10225. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10226. (format "%4d" (setq id (1+ id)))))
  10227. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10228. (when hline
  10229. (setq ic 0)
  10230. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10231. (setq beg (1+ (match-beginning 0))
  10232. ic (1+ ic)
  10233. s1 (concat "$" (int-to-string ic))
  10234. s2 (org-number-to-letters ic)
  10235. str (if (eq org-table-use-standard-references t) s2 s1))
  10236. (setq ov (org-make-overlay beg (+ beg (length str))))
  10237. (push ov org-table-coordinate-overlays)
  10238. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10239. (beginning-of-line 2)))))
  10240. (defun org-table-toggle-coordinate-overlays ()
  10241. "Toggle the display of Row/Column numbers in tables."
  10242. (interactive)
  10243. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10244. (message "Row/Column number display turned %s"
  10245. (if org-table-overlay-coordinates "on" "off"))
  10246. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10247. (org-table-align))
  10248. (unless org-table-overlay-coordinates
  10249. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10250. (setq org-table-coordinate-overlays nil)))
  10251. (defun org-table-toggle-formula-debugger ()
  10252. "Toggle the formula debugger in tables."
  10253. (interactive)
  10254. (setq org-table-formula-debug (not org-table-formula-debug))
  10255. (message "Formula debugging has been turned %s"
  10256. (if org-table-formula-debug "on" "off")))
  10257. ;;; The orgtbl minor mode
  10258. ;; Define a minor mode which can be used in other modes in order to
  10259. ;; integrate the org-mode table editor.
  10260. ;; This is really a hack, because the org-mode table editor uses several
  10261. ;; keys which normally belong to the major mode, for example the TAB and
  10262. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10263. ;; necessary to operate the table editor, but wraps the commands into a
  10264. ;; function which tests if the cursor is currently inside a table. If that
  10265. ;; is the case, the table editor command is executed. However, when any of
  10266. ;; those keys is used outside a table, the function uses `key-binding' to
  10267. ;; look up if the key has an associated command in another currently active
  10268. ;; keymap (minor modes, major mode, global), and executes that command.
  10269. ;; There might be problems if any of the keys used by the table editor is
  10270. ;; otherwise used as a prefix key.
  10271. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10272. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10273. ;; addresses this by checking explicitly for both bindings.
  10274. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10275. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10276. ;; Some modes bind other commands to simple characters, for example
  10277. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10278. ;; active, this binding is ignored inside tables and replaced with a
  10279. ;; modified self-insert.
  10280. (defvar orgtbl-mode nil
  10281. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10282. table editor in arbitrary modes.")
  10283. (make-variable-buffer-local 'orgtbl-mode)
  10284. (defvar orgtbl-mode-map (make-keymap)
  10285. "Keymap for `orgtbl-mode'.")
  10286. ;;;###autoload
  10287. (defun turn-on-orgtbl ()
  10288. "Unconditionally turn on `orgtbl-mode'."
  10289. (orgtbl-mode 1))
  10290. (defvar org-old-auto-fill-inhibit-regexp nil
  10291. "Local variable used by `orgtbl-mode'")
  10292. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10293. "Matches a line belonging to an orgtbl.")
  10294. (defconst orgtbl-extra-font-lock-keywords
  10295. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10296. 0 (quote 'org-table) 'prepend))
  10297. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10298. ;;;###autoload
  10299. (defun orgtbl-mode (&optional arg)
  10300. "The `org-mode' table editor as a minor mode for use in other modes."
  10301. (interactive)
  10302. (if (org-mode-p)
  10303. ;; Exit without error, in case some hook functions calls this
  10304. ;; by accident in org-mode.
  10305. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10306. (setq orgtbl-mode
  10307. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10308. (if orgtbl-mode
  10309. (progn
  10310. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10311. ;; Make sure we are first in minor-mode-map-alist
  10312. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10313. (and c (setq minor-mode-map-alist
  10314. (cons c (delq c minor-mode-map-alist)))))
  10315. (org-set-local (quote org-table-may-need-update) t)
  10316. (org-add-hook 'before-change-functions 'org-before-change-function
  10317. nil 'local)
  10318. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10319. auto-fill-inhibit-regexp)
  10320. (org-set-local 'auto-fill-inhibit-regexp
  10321. (if auto-fill-inhibit-regexp
  10322. (concat orgtbl-line-start-regexp "\\|"
  10323. auto-fill-inhibit-regexp)
  10324. orgtbl-line-start-regexp))
  10325. (org-add-to-invisibility-spec '(org-cwidth))
  10326. (when (fboundp 'font-lock-add-keywords)
  10327. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10328. (org-restart-font-lock))
  10329. (easy-menu-add orgtbl-mode-menu)
  10330. (run-hooks 'orgtbl-mode-hook))
  10331. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10332. (org-cleanup-narrow-column-properties)
  10333. (org-remove-from-invisibility-spec '(org-cwidth))
  10334. (remove-hook 'before-change-functions 'org-before-change-function t)
  10335. (when (fboundp 'font-lock-remove-keywords)
  10336. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10337. (org-restart-font-lock))
  10338. (easy-menu-remove orgtbl-mode-menu)
  10339. (force-mode-line-update 'all))))
  10340. (defun org-cleanup-narrow-column-properties ()
  10341. "Remove all properties related to narrow-column invisibility."
  10342. (let ((s 1))
  10343. (while (setq s (text-property-any s (point-max)
  10344. 'display org-narrow-column-arrow))
  10345. (remove-text-properties s (1+ s) '(display t)))
  10346. (setq s 1)
  10347. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10348. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10349. (setq s 1)
  10350. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10351. (remove-text-properties s (1+ s) '(invisible t)))))
  10352. ;; Install it as a minor mode.
  10353. (put 'orgtbl-mode :included t)
  10354. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10355. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10356. (defun orgtbl-make-binding (fun n &rest keys)
  10357. "Create a function for binding in the table minor mode.
  10358. FUN is the command to call inside a table. N is used to create a unique
  10359. command name. KEYS are keys that should be checked in for a command
  10360. to execute outside of tables."
  10361. (eval
  10362. (list 'defun
  10363. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10364. '(arg)
  10365. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10366. "Outside of tables, run the binding of `"
  10367. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10368. "'.")
  10369. '(interactive "p")
  10370. (list 'if
  10371. '(org-at-table-p)
  10372. (list 'call-interactively (list 'quote fun))
  10373. (list 'let '(orgtbl-mode)
  10374. (list 'call-interactively
  10375. (append '(or)
  10376. (mapcar (lambda (k)
  10377. (list 'key-binding k))
  10378. keys)
  10379. '('orgtbl-error))))))))
  10380. (defun orgtbl-error ()
  10381. "Error when there is no default binding for a table key."
  10382. (interactive)
  10383. (error "This key has no function outside tables"))
  10384. (defun orgtbl-setup ()
  10385. "Setup orgtbl keymaps."
  10386. (let ((nfunc 0)
  10387. (bindings
  10388. (list
  10389. '([(meta shift left)] org-table-delete-column)
  10390. '([(meta left)] org-table-move-column-left)
  10391. '([(meta right)] org-table-move-column-right)
  10392. '([(meta shift right)] org-table-insert-column)
  10393. '([(meta shift up)] org-table-kill-row)
  10394. '([(meta shift down)] org-table-insert-row)
  10395. '([(meta up)] org-table-move-row-up)
  10396. '([(meta down)] org-table-move-row-down)
  10397. '("\C-c\C-w" org-table-cut-region)
  10398. '("\C-c\M-w" org-table-copy-region)
  10399. '("\C-c\C-y" org-table-paste-rectangle)
  10400. '("\C-c-" org-table-insert-hline)
  10401. '("\C-c}" org-table-toggle-coordinate-overlays)
  10402. '("\C-c{" org-table-toggle-formula-debugger)
  10403. '("\C-m" org-table-next-row)
  10404. '([(shift return)] org-table-copy-down)
  10405. '("\C-c\C-q" org-table-wrap-region)
  10406. '("\C-c?" org-table-field-info)
  10407. '("\C-c " org-table-blank-field)
  10408. '("\C-c+" org-table-sum)
  10409. '("\C-c=" org-table-eval-formula)
  10410. '("\C-c'" org-table-edit-formulas)
  10411. '("\C-c`" org-table-edit-field)
  10412. '("\C-c*" org-table-recalculate)
  10413. '("\C-c|" org-table-create-or-convert-from-region)
  10414. '("\C-c^" org-table-sort-lines)
  10415. '([(control ?#)] org-table-rotate-recalc-marks)))
  10416. elt key fun cmd)
  10417. (while (setq elt (pop bindings))
  10418. (setq nfunc (1+ nfunc))
  10419. (setq key (org-key (car elt))
  10420. fun (nth 1 elt)
  10421. cmd (orgtbl-make-binding fun nfunc key))
  10422. (org-defkey orgtbl-mode-map key cmd))
  10423. ;; Special treatment needed for TAB and RET
  10424. (org-defkey orgtbl-mode-map [(return)]
  10425. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10426. (org-defkey orgtbl-mode-map "\C-m"
  10427. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10428. (org-defkey orgtbl-mode-map [(tab)]
  10429. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10430. (org-defkey orgtbl-mode-map "\C-i"
  10431. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10432. (org-defkey orgtbl-mode-map [(shift tab)]
  10433. (orgtbl-make-binding 'org-table-previous-field 104
  10434. [(shift tab)] [(tab)] "\C-i"))
  10435. (org-defkey orgtbl-mode-map "\M-\C-m"
  10436. (orgtbl-make-binding 'org-table-wrap-region 105
  10437. "\M-\C-m" [(meta return)]))
  10438. (org-defkey orgtbl-mode-map [(meta return)]
  10439. (orgtbl-make-binding 'org-table-wrap-region 106
  10440. [(meta return)] "\M-\C-m"))
  10441. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10442. (when orgtbl-optimized
  10443. ;; If the user wants maximum table support, we need to hijack
  10444. ;; some standard editing functions
  10445. (org-remap orgtbl-mode-map
  10446. 'self-insert-command 'orgtbl-self-insert-command
  10447. 'delete-char 'org-delete-char
  10448. 'delete-backward-char 'org-delete-backward-char)
  10449. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10450. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10451. '("OrgTbl"
  10452. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10453. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10454. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10455. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10456. "--"
  10457. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10458. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10459. ["Copy Field from Above"
  10460. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10461. "--"
  10462. ("Column"
  10463. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10464. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10465. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10466. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10467. ("Row"
  10468. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10469. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10470. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10471. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10472. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10473. "--"
  10474. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10475. ("Rectangle"
  10476. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10477. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10478. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10479. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10480. "--"
  10481. ("Radio tables"
  10482. ["Insert table template" orgtbl-insert-radio-table
  10483. (assq major-mode orgtbl-radio-table-templates)]
  10484. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10485. "--"
  10486. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10487. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10488. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10489. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10490. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10491. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10492. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10493. ["Sum Column/Rectangle" org-table-sum
  10494. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10495. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10496. ["Debug Formulas"
  10497. org-table-toggle-formula-debugger :active (org-at-table-p)
  10498. :keys "C-c {"
  10499. :style toggle :selected org-table-formula-debug]
  10500. ["Show Col/Row Numbers"
  10501. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10502. :keys "C-c }"
  10503. :style toggle :selected org-table-overlay-coordinates]
  10504. ))
  10505. t))
  10506. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10507. "If the cursor is inside a table, realign the table.
  10508. It it is a table to be sent away to a receiver, do it.
  10509. With prefix arg, also recompute table."
  10510. (interactive "P")
  10511. (let ((pos (point)) action)
  10512. (save-excursion
  10513. (beginning-of-line 1)
  10514. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10515. ((looking-at "[ \t]*|") pos)
  10516. ((looking-at "#\\+TBLFM:") 'recalc))))
  10517. (cond
  10518. ((integerp action)
  10519. (goto-char action)
  10520. (org-table-maybe-eval-formula)
  10521. (if arg
  10522. (call-interactively 'org-table-recalculate)
  10523. (org-table-maybe-recalculate-line))
  10524. (call-interactively 'org-table-align)
  10525. (orgtbl-send-table 'maybe))
  10526. ((eq action 'recalc)
  10527. (save-excursion
  10528. (beginning-of-line 1)
  10529. (skip-chars-backward " \r\n\t")
  10530. (if (org-at-table-p)
  10531. (org-call-with-arg 'org-table-recalculate t))))
  10532. (t (let (orgtbl-mode)
  10533. (call-interactively (key-binding "\C-c\C-c")))))))
  10534. (defun orgtbl-tab (arg)
  10535. "Justification and field motion for `orgtbl-mode'."
  10536. (interactive "P")
  10537. (if arg (org-table-edit-field t)
  10538. (org-table-justify-field-maybe)
  10539. (org-table-next-field)))
  10540. (defun orgtbl-ret ()
  10541. "Justification and field motion for `orgtbl-mode'."
  10542. (interactive)
  10543. (org-table-justify-field-maybe)
  10544. (org-table-next-row))
  10545. (defun orgtbl-self-insert-command (N)
  10546. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10547. If the cursor is in a table looking at whitespace, the whitespace is
  10548. overwritten, and the table is not marked as requiring realignment."
  10549. (interactive "p")
  10550. (if (and (org-at-table-p)
  10551. (or
  10552. (and org-table-auto-blank-field
  10553. (member last-command
  10554. '(orgtbl-hijacker-command-100
  10555. orgtbl-hijacker-command-101
  10556. orgtbl-hijacker-command-102
  10557. orgtbl-hijacker-command-103
  10558. orgtbl-hijacker-command-104
  10559. orgtbl-hijacker-command-105))
  10560. (org-table-blank-field))
  10561. t)
  10562. (eq N 1)
  10563. (looking-at "[^|\n]* +|"))
  10564. (let (org-table-may-need-update)
  10565. (goto-char (1- (match-end 0)))
  10566. (delete-backward-char 1)
  10567. (goto-char (match-beginning 0))
  10568. (self-insert-command N))
  10569. (setq org-table-may-need-update t)
  10570. (let (orgtbl-mode)
  10571. (call-interactively (key-binding (vector last-input-event))))))
  10572. (defun org-force-self-insert (N)
  10573. "Needed to enforce self-insert under remapping."
  10574. (interactive "p")
  10575. (self-insert-command N))
  10576. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10577. "Regula expression matching exponentials as produced by calc.")
  10578. (defvar org-table-clean-did-remove-column nil)
  10579. (defun orgtbl-export (table target)
  10580. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10581. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10582. org-table-last-alignment org-table-last-column-widths
  10583. maxcol column)
  10584. (if (not (fboundp func))
  10585. (error "Cannot export orgtbl table to %s" target))
  10586. (setq lines (org-table-clean-before-export lines))
  10587. (setq table
  10588. (mapcar
  10589. (lambda (x)
  10590. (if (string-match org-table-hline-regexp x)
  10591. 'hline
  10592. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10593. lines))
  10594. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10595. table)))
  10596. (loop for i from (1- maxcol) downto 0 do
  10597. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10598. (setq column (delq nil column))
  10599. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10600. (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))
  10601. (funcall func table nil)))
  10602. (defun orgtbl-send-table (&optional maybe)
  10603. "Send a tranformed version of this table to the receiver position.
  10604. With argument MAYBE, fail quietly if no transformation is defined for
  10605. this table."
  10606. (interactive)
  10607. (catch 'exit
  10608. (unless (org-at-table-p) (error "Not at a table"))
  10609. ;; when non-interactive, we assume align has just happened.
  10610. (when (interactive-p) (org-table-align))
  10611. (save-excursion
  10612. (goto-char (org-table-begin))
  10613. (beginning-of-line 0)
  10614. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10615. (if maybe
  10616. (throw 'exit nil)
  10617. (error "Don't know how to transform this table."))))
  10618. (let* ((name (match-string 1))
  10619. beg
  10620. (transform (intern (match-string 2)))
  10621. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10622. (skip (plist-get params :skip))
  10623. (skipcols (plist-get params :skipcols))
  10624. (txt (buffer-substring-no-properties
  10625. (org-table-begin) (org-table-end)))
  10626. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10627. (lines (org-table-clean-before-export lines))
  10628. (i0 (if org-table-clean-did-remove-column 2 1))
  10629. (table (mapcar
  10630. (lambda (x)
  10631. (if (string-match org-table-hline-regexp x)
  10632. 'hline
  10633. (org-remove-by-index
  10634. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10635. skipcols i0)))
  10636. lines))
  10637. (fun (if (= i0 2) 'cdr 'identity))
  10638. (org-table-last-alignment
  10639. (org-remove-by-index (funcall fun org-table-last-alignment)
  10640. skipcols i0))
  10641. (org-table-last-column-widths
  10642. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10643. skipcols i0)))
  10644. (unless (fboundp transform)
  10645. (error "No such transformation function %s" transform))
  10646. (setq txt (funcall transform table params))
  10647. ;; Find the insertion place
  10648. (save-excursion
  10649. (goto-char (point-min))
  10650. (unless (re-search-forward
  10651. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10652. (error "Don't know where to insert translated table"))
  10653. (goto-char (match-beginning 0))
  10654. (beginning-of-line 2)
  10655. (setq beg (point))
  10656. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10657. (error "Cannot find end of insertion region"))
  10658. (beginning-of-line 1)
  10659. (delete-region beg (point))
  10660. (goto-char beg)
  10661. (insert txt "\n"))
  10662. (message "Table converted and installed at receiver location"))))
  10663. (defun org-remove-by-index (list indices &optional i0)
  10664. "Remove the elements in LIST with indices in INDICES.
  10665. First element has index 0, or I0 if given."
  10666. (if (not indices)
  10667. list
  10668. (if (integerp indices) (setq indices (list indices)))
  10669. (setq i0 (1- (or i0 0)))
  10670. (delq :rm (mapcar (lambda (x)
  10671. (setq i0 (1+ i0))
  10672. (if (memq i0 indices) :rm x))
  10673. list))))
  10674. (defun orgtbl-toggle-comment ()
  10675. "Comment or uncomment the orgtbl at point."
  10676. (interactive)
  10677. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10678. (re2 (concat "^" orgtbl-line-start-regexp))
  10679. (commented (save-excursion (beginning-of-line 1)
  10680. (cond ((looking-at re1) t)
  10681. ((looking-at re2) nil)
  10682. (t (error "Not at an org table")))))
  10683. (re (if commented re1 re2))
  10684. beg end)
  10685. (save-excursion
  10686. (beginning-of-line 1)
  10687. (while (looking-at re) (beginning-of-line 0))
  10688. (beginning-of-line 2)
  10689. (setq beg (point))
  10690. (while (looking-at re) (beginning-of-line 2))
  10691. (setq end (point)))
  10692. (comment-region beg end (if commented '(4) nil))))
  10693. (defun orgtbl-insert-radio-table ()
  10694. "Insert a radio table template appropriate for this major mode."
  10695. (interactive)
  10696. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10697. (txt (nth 1 e))
  10698. name pos)
  10699. (unless e (error "No radio table setup defined for %s" major-mode))
  10700. (setq name (read-string "Table name: "))
  10701. (while (string-match "%n" txt)
  10702. (setq txt (replace-match name t t txt)))
  10703. (or (bolp) (insert "\n"))
  10704. (setq pos (point))
  10705. (insert txt)
  10706. (goto-char pos)))
  10707. (defun org-get-param (params header i sym &optional hsym)
  10708. "Get parameter value for symbol SYM.
  10709. If this is a header line, actually get the value for the symbol with an
  10710. additional \"h\" inserted after the colon.
  10711. If the value is a protperty list, get the element for the current column.
  10712. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10713. (let ((val (plist-get params sym)))
  10714. (and hsym header (setq val (or (plist-get params hsym) val)))
  10715. (if (consp val) (plist-get val i) val)))
  10716. (defun orgtbl-to-generic (table params)
  10717. "Convert the orgtbl-mode TABLE to some other format.
  10718. This generic routine can be used for many standard cases.
  10719. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10720. separator line, or a list of fields for that line.
  10721. PARAMS is a property list of parameters that can influence the conversion.
  10722. For the generic converter, some parameters are obligatory: You need to
  10723. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10724. :splice, you must have :tstart and :tend.
  10725. Valid parameters are
  10726. :tstart String to start the table. Ignored when :splice is t.
  10727. :tend String to end the table. Ignored when :splice is t.
  10728. :splice When set to t, return only table body lines, don't wrap
  10729. them into :tstart and :tend. Default is nil.
  10730. :hline String to be inserted on horizontal separation lines.
  10731. May be nil to ignore hlines.
  10732. :lstart String to start a new table line.
  10733. :lend String to end a table line
  10734. :sep Separator between two fields
  10735. :lfmt Format for entire line, with enough %s to capture all fields.
  10736. If this is present, :lstart, :lend, and :sep are ignored.
  10737. :fmt A format to be used to wrap the field, should contain
  10738. %s for the original field value. For example, to wrap
  10739. everything in dollars, you could use :fmt \"$%s$\".
  10740. This may also be a property list with column numbers and
  10741. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10742. :hlstart :hlend :hlsep :hlfmt :hfmt
  10743. Same as above, specific for the header lines in the table.
  10744. All lines before the first hline are treated as header.
  10745. If any of these is not present, the data line value is used.
  10746. :efmt Use this format to print numbers with exponentials.
  10747. The format should have %s twice for inserting mantissa
  10748. and exponent, for example \"%s\\\\times10^{%s}\". This
  10749. may also be a property list with column numbers and
  10750. formats. :fmt will still be applied after :efmt.
  10751. In addition to this, the parameters :skip and :skipcols are always handled
  10752. directly by `orgtbl-send-table'. See manual."
  10753. (interactive)
  10754. (let* ((p params)
  10755. (splicep (plist-get p :splice))
  10756. (hline (plist-get p :hline))
  10757. rtn line i fm efm lfmt h)
  10758. ;; Do we have a header?
  10759. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10760. (setq h t))
  10761. ;; Put header
  10762. (unless splicep
  10763. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10764. ;; Now loop over all lines
  10765. (while (setq line (pop table))
  10766. (if (eq line 'hline)
  10767. ;; A horizontal separator line
  10768. (progn (if hline (push hline rtn))
  10769. (setq h nil)) ; no longer in header
  10770. ;; A normal line. Convert the fields, push line onto the result list
  10771. (setq i 0)
  10772. (setq line
  10773. (mapcar
  10774. (lambda (f)
  10775. (setq i (1+ i)
  10776. fm (org-get-param p h i :fmt :hfmt)
  10777. efm (org-get-param p h i :efmt))
  10778. (if (and efm (string-match orgtbl-exp-regexp f))
  10779. (setq f (format
  10780. efm (match-string 1 f) (match-string 2 f))))
  10781. (if fm (setq f (format fm f)))
  10782. f)
  10783. line))
  10784. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10785. (push (apply 'format lfmt line) rtn)
  10786. (push (concat
  10787. (org-get-param p h i :lstart :hlstart)
  10788. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10789. (org-get-param p h i :lend :hlend))
  10790. rtn))))
  10791. (unless splicep
  10792. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10793. (mapconcat 'identity (nreverse rtn) "\n")))
  10794. (defun orgtbl-to-latex (table params)
  10795. "Convert the orgtbl-mode TABLE to LaTeX.
  10796. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10797. separator line, or a list of fields for that line.
  10798. PARAMS is a property list of parameters that can influence the conversion.
  10799. Supports all parameters from `orgtbl-to-generic'. Most important for
  10800. LaTeX are:
  10801. :splice When set to t, return only table body lines, don't wrap
  10802. them into a tabular environment. Default is nil.
  10803. :fmt A format to be used to wrap the field, should contain %s for the
  10804. original field value. For example, to wrap everything in dollars,
  10805. use :fmt \"$%s$\". This may also be a property list with column
  10806. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10807. :efmt Format for transforming numbers with exponentials. The format
  10808. should have %s twice for inserting mantissa and exponent, for
  10809. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10810. This may also be a property list with column numbers and formats.
  10811. The general parameters :skip and :skipcols have already been applied when
  10812. this function is called."
  10813. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10814. org-table-last-alignment ""))
  10815. (params2
  10816. (list
  10817. :tstart (concat "\\begin{tabular}{" alignment "}")
  10818. :tend "\\end{tabular}"
  10819. :lstart "" :lend " \\\\" :sep " & "
  10820. :efmt "%s\\,(%s)" :hline "\\hline")))
  10821. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10822. (defun orgtbl-to-html (table params)
  10823. "Convert the orgtbl-mode TABLE to LaTeX.
  10824. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10825. separator line, or a list of fields for that line.
  10826. PARAMS is a property list of parameters that can influence the conversion.
  10827. Currently this function recognizes the following parameters:
  10828. :splice When set to t, return only table body lines, don't wrap
  10829. them into a <table> environment. Default is nil.
  10830. The general parameters :skip and :skipcols have already been applied when
  10831. this function is called. The function does *not* use `orgtbl-to-generic',
  10832. so you cannot specify parameters for it."
  10833. (let* ((splicep (plist-get params :splice))
  10834. html)
  10835. ;; Just call the formatter we already have
  10836. ;; We need to make text lines for it, so put the fields back together.
  10837. (setq html (org-format-org-table-html
  10838. (mapcar
  10839. (lambda (x)
  10840. (if (eq x 'hline)
  10841. "|----+----|"
  10842. (concat "| " (mapconcat 'identity x " | ") " |")))
  10843. table)
  10844. splicep))
  10845. (if (string-match "\n+\\'" html)
  10846. (setq html (replace-match "" t t html)))
  10847. html))
  10848. (defun orgtbl-to-texinfo (table params)
  10849. "Convert the orgtbl-mode TABLE to TeXInfo.
  10850. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10851. separator line, or a list of fields for that line.
  10852. PARAMS is a property list of parameters that can influence the conversion.
  10853. Supports all parameters from `orgtbl-to-generic'. Most important for
  10854. TeXInfo are:
  10855. :splice nil/t When set to t, return only table body lines, don't wrap
  10856. them into a multitable environment. Default is nil.
  10857. :fmt fmt A format to be used to wrap the field, should contain
  10858. %s for the original field value. For example, to wrap
  10859. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10860. This may also be a property list with column numbers and
  10861. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10862. :cf \"f1 f2..\" The column fractions for the table. By default these
  10863. are computed automatically from the width of the columns
  10864. under org-mode.
  10865. The general parameters :skip and :skipcols have already been applied when
  10866. this function is called."
  10867. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10868. (colfrac (or (plist-get params :cf)
  10869. (mapconcat
  10870. (lambda (x) (format "%.3f" (/ (float x) total)))
  10871. org-table-last-column-widths " ")))
  10872. (params2
  10873. (list
  10874. :tstart (concat "@multitable @columnfractions " colfrac)
  10875. :tend "@end multitable"
  10876. :lstart "@item " :lend "" :sep " @tab "
  10877. :hlstart "@headitem ")))
  10878. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10879. ;;;; Link Stuff
  10880. ;;; Link abbreviations
  10881. (defun org-link-expand-abbrev (link)
  10882. "Apply replacements as defined in `org-link-abbrev-alist."
  10883. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10884. (let* ((key (match-string 1 link))
  10885. (as (or (assoc key org-link-abbrev-alist-local)
  10886. (assoc key org-link-abbrev-alist)))
  10887. (tag (and (match-end 2) (match-string 3 link)))
  10888. rpl)
  10889. (if (not as)
  10890. link
  10891. (setq rpl (cdr as))
  10892. (cond
  10893. ((symbolp rpl) (funcall rpl tag))
  10894. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10895. (t (concat rpl tag)))))
  10896. link))
  10897. ;;; Storing and inserting links
  10898. (defvar org-insert-link-history nil
  10899. "Minibuffer history for links inserted with `org-insert-link'.")
  10900. (defvar org-stored-links nil
  10901. "Contains the links stored with `org-store-link'.")
  10902. (defvar org-store-link-plist nil
  10903. "Plist with info about the most recently link created with `org-store-link'.")
  10904. (defvar org-link-protocols nil
  10905. "Link protocols added to Org-mode using `org-add-link-type'.")
  10906. (defvar org-store-link-functions nil
  10907. "List of functions that are called to create and store a link.
  10908. Each function will be called in turn until one returns a non-nil
  10909. value. Each function should check if it is responsible for creating
  10910. this link (for example by looking at the major mode).
  10911. If not, it must exit and return nil.
  10912. If yes, it should return a non-nil value after a calling
  10913. `org-store-link-props' with a list of properties and values.
  10914. Special properties are:
  10915. :type The link prefix. like \"http\". This must be given.
  10916. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10917. This is obligatory as well.
  10918. :description Optional default description for the second pair
  10919. of brackets in an Org-mode link. The user can still change
  10920. this when inserting this link into an Org-mode buffer.
  10921. In addition to these, any additional properties can be specified
  10922. and then used in remember templates.")
  10923. (defun org-add-link-type (type &optional follow publish)
  10924. "Add TYPE to the list of `org-link-types'.
  10925. Re-compute all regular expressions depending on `org-link-types'
  10926. FOLLOW and PUBLISH are two functions. Both take the link path as
  10927. an argument.
  10928. FOLLOW should do whatever is necessary to follow the link, for example
  10929. to find a file or display a mail message.
  10930. PUBLISH takes the path and retuns the string that should be used when
  10931. this document is published. FIMXE: This is actually not yet implemented."
  10932. (add-to-list 'org-link-types type t)
  10933. (org-make-link-regexps)
  10934. (add-to-list 'org-link-protocols
  10935. (list type follow publish)))
  10936. (defun org-add-agenda-custom-command (entry)
  10937. "Replace or add a command in `org-agenda-custom-commands'.
  10938. This is mostly for hacking and trying a new command - once the command
  10939. works you probably want to add it to `org-agenda-custom-commands' for good."
  10940. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10941. (if ass
  10942. (setcdr ass (cdr entry))
  10943. (push entry org-agenda-custom-commands))))
  10944. ;;;###autoload
  10945. (defun org-store-link (arg)
  10946. "\\<org-mode-map>Store an org-link to the current location.
  10947. This link is added to `org-stored-links' and can later be inserted
  10948. into an org-buffer with \\[org-insert-link].
  10949. For some link types, a prefix arg is interpreted:
  10950. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10951. For file links, arg negates `org-context-in-file-links'."
  10952. (interactive "P")
  10953. (setq org-store-link-plist nil) ; reset
  10954. (let (link cpltxt desc description search txt)
  10955. (cond
  10956. ((run-hook-with-args-until-success 'org-store-link-functions)
  10957. (setq link (plist-get org-store-link-plist :link)
  10958. desc (or (plist-get org-store-link-plist :description) link)))
  10959. ((eq major-mode 'bbdb-mode)
  10960. (let ((name (bbdb-record-name (bbdb-current-record)))
  10961. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10962. (setq cpltxt (concat "bbdb:" (or name company))
  10963. link (org-make-link cpltxt))
  10964. (org-store-link-props :type "bbdb" :name name :company company)))
  10965. ((eq major-mode 'Info-mode)
  10966. (setq link (org-make-link "info:"
  10967. (file-name-nondirectory Info-current-file)
  10968. ":" Info-current-node))
  10969. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10970. ":" Info-current-node))
  10971. (org-store-link-props :type "info" :file Info-current-file
  10972. :node Info-current-node))
  10973. ((eq major-mode 'calendar-mode)
  10974. (let ((cd (calendar-cursor-to-date)))
  10975. (setq link
  10976. (format-time-string
  10977. (car org-time-stamp-formats)
  10978. (apply 'encode-time
  10979. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10980. nil nil nil))))
  10981. (org-store-link-props :type "calendar" :date cd)))
  10982. ((or (eq major-mode 'vm-summary-mode)
  10983. (eq major-mode 'vm-presentation-mode))
  10984. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10985. (vm-follow-summary-cursor)
  10986. (save-excursion
  10987. (vm-select-folder-buffer)
  10988. (let* ((message (car vm-message-pointer))
  10989. (folder buffer-file-name)
  10990. (subject (vm-su-subject message))
  10991. (to (vm-get-header-contents message "To"))
  10992. (from (vm-get-header-contents message "From"))
  10993. (message-id (vm-su-message-id message)))
  10994. (org-store-link-props :type "vm" :from from :to to :subject subject
  10995. :message-id message-id)
  10996. (setq message-id (org-remove-angle-brackets message-id))
  10997. (setq folder (abbreviate-file-name folder))
  10998. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10999. folder)
  11000. (setq folder (replace-match "" t t folder)))
  11001. (setq cpltxt (org-email-link-description))
  11002. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11003. ((eq major-mode 'wl-summary-mode)
  11004. (let* ((msgnum (wl-summary-message-number))
  11005. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11006. msgnum 'message-id))
  11007. (wl-message-entity
  11008. (if (fboundp 'elmo-message-entity)
  11009. (elmo-message-entity
  11010. wl-summary-buffer-elmo-folder msgnum)
  11011. (elmo-msgdb-overview-get-entity
  11012. msgnum (wl-summary-buffer-msgdb))))
  11013. (from (wl-summary-line-from))
  11014. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11015. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11016. (wl-summary-line-subject))))
  11017. (org-store-link-props :type "wl" :from from :to to
  11018. :subject subject :message-id message-id)
  11019. (setq message-id (org-remove-angle-brackets message-id))
  11020. (setq cpltxt (org-email-link-description))
  11021. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11022. "#" message-id))))
  11023. ((or (equal major-mode 'mh-folder-mode)
  11024. (equal major-mode 'mh-show-mode))
  11025. (let ((from (org-mhe-get-header "From:"))
  11026. (to (org-mhe-get-header "To:"))
  11027. (message-id (org-mhe-get-header "Message-Id:"))
  11028. (subject (org-mhe-get-header "Subject:")))
  11029. (org-store-link-props :type "mh" :from from :to to
  11030. :subject subject :message-id message-id)
  11031. (setq cpltxt (org-email-link-description))
  11032. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11033. (org-remove-angle-brackets message-id)))))
  11034. ((or (eq major-mode 'rmail-mode)
  11035. (eq major-mode 'rmail-summary-mode))
  11036. (save-window-excursion
  11037. (save-restriction
  11038. (when (eq major-mode 'rmail-summary-mode)
  11039. (rmail-show-message rmail-current-message))
  11040. (rmail-narrow-to-non-pruned-header)
  11041. (let ((folder buffer-file-name)
  11042. (message-id (mail-fetch-field "message-id"))
  11043. (from (mail-fetch-field "from"))
  11044. (to (mail-fetch-field "to"))
  11045. (subject (mail-fetch-field "subject")))
  11046. (org-store-link-props
  11047. :type "rmail" :from from :to to
  11048. :subject subject :message-id message-id)
  11049. (setq message-id (org-remove-angle-brackets message-id))
  11050. (setq cpltxt (org-email-link-description))
  11051. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11052. (rmail-show-message rmail-current-message))))
  11053. ((eq major-mode 'gnus-group-mode)
  11054. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11055. (gnus-group-group-name)) ; version
  11056. ((fboundp 'gnus-group-name)
  11057. (gnus-group-name))
  11058. (t "???"))))
  11059. (unless group (error "Not on a group"))
  11060. (org-store-link-props :type "gnus" :group group)
  11061. (setq cpltxt (concat
  11062. (if (org-xor arg org-usenet-links-prefer-google)
  11063. "http://groups.google.com/groups?group="
  11064. "gnus:")
  11065. group)
  11066. link (org-make-link cpltxt))))
  11067. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11068. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11069. (let* ((group gnus-newsgroup-name)
  11070. (article (gnus-summary-article-number))
  11071. (header (gnus-summary-article-header article))
  11072. (from (mail-header-from header))
  11073. (message-id (mail-header-id header))
  11074. (date (mail-header-date header))
  11075. (subject (gnus-summary-subject-string)))
  11076. (org-store-link-props :type "gnus" :from from :subject subject
  11077. :message-id message-id :group group)
  11078. (setq cpltxt (org-email-link-description))
  11079. (if (org-xor arg org-usenet-links-prefer-google)
  11080. (setq link
  11081. (concat
  11082. cpltxt "\n "
  11083. (format "http://groups.google.com/groups?as_umsgid=%s"
  11084. (org-fixup-message-id-for-http message-id))))
  11085. (setq link (org-make-link "gnus:" group
  11086. "#" (number-to-string article))))))
  11087. ((eq major-mode 'w3-mode)
  11088. (setq cpltxt (url-view-url t)
  11089. link (org-make-link cpltxt))
  11090. (org-store-link-props :type "w3" :url (url-view-url t)))
  11091. ((eq major-mode 'w3m-mode)
  11092. (setq cpltxt (or w3m-current-title w3m-current-url)
  11093. link (org-make-link w3m-current-url))
  11094. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11095. ((setq search (run-hook-with-args-until-success
  11096. 'org-create-file-search-functions))
  11097. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11098. "::" search))
  11099. (setq cpltxt (or description link)))
  11100. ((eq major-mode 'image-mode)
  11101. (setq cpltxt (concat "file:"
  11102. (abbreviate-file-name buffer-file-name))
  11103. link (org-make-link cpltxt))
  11104. (org-store-link-props :type "image" :file buffer-file-name))
  11105. ((eq major-mode 'dired-mode)
  11106. ;; link to the file in the current line
  11107. (setq cpltxt (concat "file:"
  11108. (abbreviate-file-name
  11109. (expand-file-name
  11110. (dired-get-filename nil t))))
  11111. link (org-make-link cpltxt)))
  11112. ((and buffer-file-name (org-mode-p))
  11113. ;; Just link to current headline
  11114. (setq cpltxt (concat "file:"
  11115. (abbreviate-file-name buffer-file-name)))
  11116. ;; Add a context search string
  11117. (when (org-xor org-context-in-file-links arg)
  11118. ;; Check if we are on a target
  11119. (if (org-in-regexp "<<\\(.*?\\)>>")
  11120. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11121. (setq txt (cond
  11122. ((org-on-heading-p) nil)
  11123. ((org-region-active-p)
  11124. (buffer-substring (region-beginning) (region-end)))
  11125. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11126. (when (or (null txt) (string-match "\\S-" txt))
  11127. (setq cpltxt
  11128. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11129. desc "NONE"))))
  11130. (if (string-match "::\\'" cpltxt)
  11131. (setq cpltxt (substring cpltxt 0 -2)))
  11132. (setq link (org-make-link cpltxt)))
  11133. ((buffer-file-name (buffer-base-buffer))
  11134. ;; Just link to this file here.
  11135. (setq cpltxt (concat "file:"
  11136. (abbreviate-file-name
  11137. (buffer-file-name (buffer-base-buffer)))))
  11138. ;; Add a context string
  11139. (when (org-xor org-context-in-file-links arg)
  11140. (setq txt (if (org-region-active-p)
  11141. (buffer-substring (region-beginning) (region-end))
  11142. (buffer-substring (point-at-bol) (point-at-eol))))
  11143. ;; Only use search option if there is some text.
  11144. (when (string-match "\\S-" txt)
  11145. (setq cpltxt
  11146. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11147. desc "NONE")))
  11148. (setq link (org-make-link cpltxt)))
  11149. ((interactive-p)
  11150. (error "Cannot link to a buffer which is not visiting a file"))
  11151. (t (setq link nil)))
  11152. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11153. (setq link (or link cpltxt)
  11154. desc (or desc cpltxt))
  11155. (if (equal desc "NONE") (setq desc nil))
  11156. (if (and (interactive-p) link)
  11157. (progn
  11158. (setq org-stored-links
  11159. (cons (list link desc) org-stored-links))
  11160. (message "Stored: %s" (or desc link)))
  11161. (and link (org-make-link-string link desc)))))
  11162. (defun org-store-link-props (&rest plist)
  11163. "Store link properties, extract names and addresses."
  11164. (let (x adr)
  11165. (when (setq x (plist-get plist :from))
  11166. (setq adr (mail-extract-address-components x))
  11167. (plist-put plist :fromname (car adr))
  11168. (plist-put plist :fromaddress (nth 1 adr)))
  11169. (when (setq x (plist-get plist :to))
  11170. (setq adr (mail-extract-address-components x))
  11171. (plist-put plist :toname (car adr))
  11172. (plist-put plist :toaddress (nth 1 adr))))
  11173. (let ((from (plist-get plist :from))
  11174. (to (plist-get plist :to)))
  11175. (when (and from to org-from-is-user-regexp)
  11176. (plist-put plist :fromto
  11177. (if (string-match org-from-is-user-regexp from)
  11178. (concat "to %t")
  11179. (concat "from %f")))))
  11180. (setq org-store-link-plist plist))
  11181. (defun org-email-link-description (&optional fmt)
  11182. "Return the description part of an email link.
  11183. This takes information from `org-store-link-plist' and formats it
  11184. according to FMT (default from `org-email-link-description-format')."
  11185. (setq fmt (or fmt org-email-link-description-format))
  11186. (let* ((p org-store-link-plist)
  11187. (to (plist-get p :toaddress))
  11188. (from (plist-get p :fromaddress))
  11189. (table
  11190. (list
  11191. (cons "%c" (plist-get p :fromto))
  11192. (cons "%F" (plist-get p :from))
  11193. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11194. (cons "%T" (plist-get p :to))
  11195. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11196. (cons "%s" (plist-get p :subject))
  11197. (cons "%m" (plist-get p :message-id)))))
  11198. (when (string-match "%c" fmt)
  11199. ;; Check if the user wrote this message
  11200. (if (and org-from-is-user-regexp from to
  11201. (save-match-data (string-match org-from-is-user-regexp from)))
  11202. (setq fmt (replace-match "to %t" t t fmt))
  11203. (setq fmt (replace-match "from %f" t t fmt))))
  11204. (org-replace-escapes fmt table)))
  11205. (defun org-make-org-heading-search-string (&optional string heading)
  11206. "Make search string for STRING or current headline."
  11207. (interactive)
  11208. (let ((s (or string (org-get-heading))))
  11209. (unless (and string (not heading))
  11210. ;; We are using a headline, clean up garbage in there.
  11211. (if (string-match org-todo-regexp s)
  11212. (setq s (replace-match "" t t s)))
  11213. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11214. (setq s (replace-match "" t t s)))
  11215. (setq s (org-trim s))
  11216. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11217. org-comment-string "\\)") s)
  11218. (setq s (replace-match "" t t s)))
  11219. (while (string-match org-ts-regexp s)
  11220. (setq s (replace-match "" t t s))))
  11221. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11222. (setq s (replace-match " " t t s)))
  11223. (or string (setq s (concat "*" s))) ; Add * for headlines
  11224. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11225. (defun org-make-link (&rest strings)
  11226. "Concatenate STRINGS."
  11227. (apply 'concat strings))
  11228. (defun org-make-link-string (link &optional description)
  11229. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11230. (unless (string-match "\\S-" link)
  11231. (error "Empty link"))
  11232. (when (stringp description)
  11233. ;; Remove brackets from the description, they are fatal.
  11234. (while (string-match "\\[" description)
  11235. (setq description (replace-match "{" t t description)))
  11236. (while (string-match "\\]" description)
  11237. (setq description (replace-match "}" t t description))))
  11238. (when (equal (org-link-escape link) description)
  11239. ;; No description needed, it is identical
  11240. (setq description nil))
  11241. (when (and (not description)
  11242. (not (equal link (org-link-escape link))))
  11243. (setq description link))
  11244. (concat "[[" (org-link-escape link) "]"
  11245. (if description (concat "[" description "]") "")
  11246. "]"))
  11247. (defconst org-link-escape-chars
  11248. '((?\ . "%20")
  11249. (?\[ . "%5B")
  11250. (?\] . "%5D")
  11251. (?\340 . "%E0") ; `a
  11252. (?\342 . "%E2") ; ^a
  11253. (?\347 . "%E7") ; ,c
  11254. (?\350 . "%E8") ; `e
  11255. (?\351 . "%E9") ; 'e
  11256. (?\352 . "%EA") ; ^e
  11257. (?\356 . "%EE") ; ^i
  11258. (?\364 . "%F4") ; ^o
  11259. (?\371 . "%F9") ; `u
  11260. (?\373 . "%FB") ; ^u
  11261. (?\; . "%3B")
  11262. (?? . "%3F")
  11263. (?= . "%3D")
  11264. (?+ . "%2B")
  11265. )
  11266. "Association list of escapes for some characters problematic in links.
  11267. This is the list that is used for internal purposes.")
  11268. (defconst org-link-escape-chars-browser
  11269. '((?\ . "%20")) ; 32 for the SPC char
  11270. "Association list of escapes for some characters problematic in links.
  11271. This is the list that is used before handing over to the browser.")
  11272. (defun org-link-escape (text &optional table)
  11273. "Escape charaters in TEXT that are problematic for links."
  11274. (setq table (or table org-link-escape-chars))
  11275. (when text
  11276. (let ((re (mapconcat (lambda (x) (regexp-quote
  11277. (char-to-string (car x))))
  11278. table "\\|")))
  11279. (while (string-match re text)
  11280. (setq text
  11281. (replace-match
  11282. (cdr (assoc (string-to-char (match-string 0 text))
  11283. table))
  11284. t t text)))
  11285. text)))
  11286. (defun org-link-unescape (text &optional table)
  11287. "Reverse the action of `org-link-escape'."
  11288. (setq table (or table org-link-escape-chars))
  11289. (when text
  11290. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11291. table "\\|")))
  11292. (while (string-match re text)
  11293. (setq text
  11294. (replace-match
  11295. (char-to-string (car (rassoc (match-string 0 text) table)))
  11296. t t text)))
  11297. text)))
  11298. (defun org-xor (a b)
  11299. "Exclusive or."
  11300. (if a (not b) b))
  11301. (defun org-get-header (header)
  11302. "Find a header field in the current buffer."
  11303. (save-excursion
  11304. (goto-char (point-min))
  11305. (let ((case-fold-search t) s)
  11306. (cond
  11307. ((eq header 'from)
  11308. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11309. (setq s (match-string 1)))
  11310. (while (string-match "\"" s)
  11311. (setq s (replace-match "" t t s)))
  11312. (if (string-match "[<(].*" s)
  11313. (setq s (replace-match "" t t s))))
  11314. ((eq header 'message-id)
  11315. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11316. (setq s (match-string 1))))
  11317. ((eq header 'subject)
  11318. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11319. (setq s (match-string 1)))))
  11320. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11321. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11322. s)))
  11323. (defun org-fixup-message-id-for-http (s)
  11324. "Replace special characters in a message id, so it can be used in an http query."
  11325. (while (string-match "<" s)
  11326. (setq s (replace-match "%3C" t t s)))
  11327. (while (string-match ">" s)
  11328. (setq s (replace-match "%3E" t t s)))
  11329. (while (string-match "@" s)
  11330. (setq s (replace-match "%40" t t s)))
  11331. s)
  11332. ;;;###autoload
  11333. (defun org-insert-link-global ()
  11334. "Insert a link like Org-mode does.
  11335. This command can be called in any mode to insert a link in Org-mode syntax."
  11336. (interactive)
  11337. (org-run-like-in-org-mode 'org-insert-link))
  11338. (defun org-insert-link (&optional complete-file)
  11339. "Insert a link. At the prompt, enter the link.
  11340. Completion can be used to select a link previously stored with
  11341. `org-store-link'. When the empty string is entered (i.e. if you just
  11342. press RET at the prompt), the link defaults to the most recently
  11343. stored link. As SPC triggers completion in the minibuffer, you need to
  11344. use M-SPC or C-q SPC to force the insertion of a space character.
  11345. You will also be prompted for a description, and if one is given, it will
  11346. be displayed in the buffer instead of the link.
  11347. If there is already a link at point, this command will allow you to edit link
  11348. and description parts.
  11349. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11350. selected using completion. The path to the file will be relative to
  11351. the current directory if the file is in the current directory or a
  11352. subdirectory. Otherwise, the link will be the absolute path as
  11353. completed in the minibuffer (i.e. normally ~/path/to/file).
  11354. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11355. is in the current directory or below.
  11356. With three \\[universal-argument] prefixes, negate the meaning of
  11357. `org-keep-stored-link-after-insertion'."
  11358. (interactive "P")
  11359. (let* ((wcf (current-window-configuration))
  11360. (region (if (org-region-active-p)
  11361. (buffer-substring (region-beginning) (region-end))))
  11362. (remove (and region (list (region-beginning) (region-end))))
  11363. (desc region)
  11364. tmphist ; byte-compile incorrectly complains about this
  11365. link entry file)
  11366. (cond
  11367. ((org-in-regexp org-bracket-link-regexp 1)
  11368. ;; We do have a link at point, and we are going to edit it.
  11369. (setq remove (list (match-beginning 0) (match-end 0)))
  11370. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11371. (setq link (read-string "Link: "
  11372. (org-link-unescape
  11373. (org-match-string-no-properties 1)))))
  11374. ((or (org-in-regexp org-angle-link-re)
  11375. (org-in-regexp org-plain-link-re))
  11376. ;; Convert to bracket link
  11377. (setq remove (list (match-beginning 0) (match-end 0))
  11378. link (read-string "Link: "
  11379. (org-remove-angle-brackets (match-string 0)))))
  11380. ((equal complete-file '(4))
  11381. ;; Completing read for file names.
  11382. (setq file (read-file-name "File: "))
  11383. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11384. (pwd1 (file-name-as-directory (abbreviate-file-name
  11385. (expand-file-name ".")))))
  11386. (cond
  11387. ((equal complete-file '(16))
  11388. (setq link (org-make-link
  11389. "file:"
  11390. (abbreviate-file-name (expand-file-name file)))))
  11391. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11392. (setq link (org-make-link "file:" (match-string 1 file))))
  11393. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11394. (expand-file-name file))
  11395. (setq link (org-make-link
  11396. "file:" (match-string 1 (expand-file-name file)))))
  11397. (t (setq link (org-make-link "file:" file))))))
  11398. (t
  11399. ;; Read link, with completion for stored links.
  11400. (with-output-to-temp-buffer "*Org Links*"
  11401. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11402. (when org-stored-links
  11403. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11404. (princ (mapconcat
  11405. (lambda (x)
  11406. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11407. (reverse org-stored-links) "\n"))))
  11408. (let ((cw (selected-window)))
  11409. (select-window (get-buffer-window "*Org Links*"))
  11410. (shrink-window-if-larger-than-buffer)
  11411. (setq truncate-lines t)
  11412. (select-window cw))
  11413. ;; Fake a link history, containing the stored links.
  11414. (setq tmphist (append (mapcar 'car org-stored-links)
  11415. org-insert-link-history))
  11416. (unwind-protect
  11417. (setq link (org-completing-read
  11418. "Link: "
  11419. (append
  11420. (mapcar (lambda (x) (list (concat (car x) ":")))
  11421. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11422. (mapcar (lambda (x) (list (concat x ":")))
  11423. org-link-types))
  11424. nil nil nil
  11425. 'tmphist
  11426. (or (car (car org-stored-links)))))
  11427. (set-window-configuration wcf)
  11428. (kill-buffer "*Org Links*"))
  11429. (setq entry (assoc link org-stored-links))
  11430. (or entry (push link org-insert-link-history))
  11431. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11432. (not org-keep-stored-link-after-insertion))
  11433. (setq org-stored-links (delq (assoc link org-stored-links)
  11434. org-stored-links)))
  11435. (setq desc (or desc (nth 1 entry)))))
  11436. (if (string-match org-plain-link-re link)
  11437. ;; URL-like link, normalize the use of angular brackets.
  11438. (setq link (org-make-link (org-remove-angle-brackets link))))
  11439. ;; Check if we are linking to the current file with a search option
  11440. ;; If yes, simplify the link by using only the search option.
  11441. (when (and buffer-file-name
  11442. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11443. (let* ((path (match-string 1 link))
  11444. (case-fold-search nil)
  11445. (search (match-string 2 link)))
  11446. (save-match-data
  11447. (if (equal (file-truename buffer-file-name) (file-truename path))
  11448. ;; We are linking to this same file, with a search option
  11449. (setq link search)))))
  11450. ;; Check if we can/should use a relative path. If yes, simplify the link
  11451. (when (string-match "\\<file:\\(.*\\)" link)
  11452. (let* ((path (match-string 1 link))
  11453. (origpath path)
  11454. (case-fold-search nil))
  11455. (cond
  11456. ((eq org-link-file-path-type 'absolute)
  11457. (setq path (abbreviate-file-name (expand-file-name path))))
  11458. ((eq org-link-file-path-type 'noabbrev)
  11459. (setq path (expand-file-name path)))
  11460. ((eq org-link-file-path-type 'relative)
  11461. (setq path (file-relative-name path)))
  11462. (t
  11463. (save-match-data
  11464. (if (string-match (concat "^" (regexp-quote
  11465. (file-name-as-directory
  11466. (expand-file-name "."))))
  11467. (expand-file-name path))
  11468. ;; We are linking a file with relative path name.
  11469. (setq path (substring (expand-file-name path)
  11470. (match-end 0)))))))
  11471. (setq link (concat "file:" path))
  11472. (if (equal desc origpath)
  11473. (setq desc path))))
  11474. (setq desc (read-string "Description: " desc))
  11475. (unless (string-match "\\S-" desc) (setq desc nil))
  11476. (if remove (apply 'delete-region remove))
  11477. (insert (org-make-link-string link desc))))
  11478. (defun org-completing-read (&rest args)
  11479. (let ((minibuffer-local-completion-map
  11480. (copy-keymap minibuffer-local-completion-map)))
  11481. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11482. (apply 'completing-read args)))
  11483. ;;; Opening/following a link
  11484. (defvar org-link-search-failed nil)
  11485. (defun org-next-link ()
  11486. "Move forward to the next link.
  11487. If the link is in hidden text, expose it."
  11488. (interactive)
  11489. (when (and org-link-search-failed (eq this-command last-command))
  11490. (goto-char (point-min))
  11491. (message "Link search wrapped back to beginning of buffer"))
  11492. (setq org-link-search-failed nil)
  11493. (let* ((pos (point))
  11494. (ct (org-context))
  11495. (a (assoc :link ct)))
  11496. (if a (goto-char (nth 2 a)))
  11497. (if (re-search-forward org-any-link-re nil t)
  11498. (progn
  11499. (goto-char (match-beginning 0))
  11500. (if (org-invisible-p) (org-show-context)))
  11501. (goto-char pos)
  11502. (setq org-link-search-failed t)
  11503. (error "No further link found"))))
  11504. (defun org-previous-link ()
  11505. "Move backward to the previous link.
  11506. If the link is in hidden text, expose it."
  11507. (interactive)
  11508. (when (and org-link-search-failed (eq this-command last-command))
  11509. (goto-char (point-max))
  11510. (message "Link search wrapped back to end of buffer"))
  11511. (setq org-link-search-failed nil)
  11512. (let* ((pos (point))
  11513. (ct (org-context))
  11514. (a (assoc :link ct)))
  11515. (if a (goto-char (nth 1 a)))
  11516. (if (re-search-backward org-any-link-re nil t)
  11517. (progn
  11518. (goto-char (match-beginning 0))
  11519. (if (org-invisible-p) (org-show-context)))
  11520. (goto-char pos)
  11521. (setq org-link-search-failed t)
  11522. (error "No further link found"))))
  11523. (defun org-find-file-at-mouse (ev)
  11524. "Open file link or URL at mouse."
  11525. (interactive "e")
  11526. (mouse-set-point ev)
  11527. (org-open-at-point 'in-emacs))
  11528. (defun org-open-at-mouse (ev)
  11529. "Open file link or URL at mouse."
  11530. (interactive "e")
  11531. (mouse-set-point ev)
  11532. (org-open-at-point))
  11533. (defvar org-window-config-before-follow-link nil
  11534. "The window configuration before following a link.
  11535. This is saved in case the need arises to restore it.")
  11536. (defvar org-open-link-marker (make-marker)
  11537. "Marker pointing to the location where `org-open-at-point; was called.")
  11538. ;;;###autoload
  11539. (defun org-open-at-point-global ()
  11540. "Follow a link like Org-mode does.
  11541. This command can be called in any mode to follow a link that has
  11542. Org-mode syntax."
  11543. (interactive)
  11544. (org-run-like-in-org-mode 'org-open-at-point))
  11545. (defun org-open-at-point (&optional in-emacs)
  11546. "Open link at or after point.
  11547. If there is no link at point, this function will search forward up to
  11548. the end of the current subtree.
  11549. Normally, files will be opened by an appropriate application. If the
  11550. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11551. (interactive "P")
  11552. (catch 'abort
  11553. (move-marker org-open-link-marker (point))
  11554. (setq org-window-config-before-follow-link (current-window-configuration))
  11555. (org-remove-occur-highlights nil nil t)
  11556. (if (org-at-timestamp-p t)
  11557. (org-follow-timestamp-link)
  11558. (let (type path link line search (pos (point)))
  11559. (catch 'match
  11560. (save-excursion
  11561. (skip-chars-forward "^]\n\r")
  11562. (when (org-in-regexp org-bracket-link-regexp)
  11563. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11564. (while (string-match " *\n *" link)
  11565. (setq link (replace-match " " t t link)))
  11566. (setq link (org-link-expand-abbrev link))
  11567. (if (string-match org-link-re-with-space2 link)
  11568. (setq type (match-string 1 link) path (match-string 2 link))
  11569. (setq type "thisfile" path link))
  11570. (throw 'match t)))
  11571. (when (get-text-property (point) 'org-linked-text)
  11572. (setq type "thisfile"
  11573. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11574. (1+ (point)) (point))
  11575. path (buffer-substring
  11576. (previous-single-property-change pos 'org-linked-text)
  11577. (next-single-property-change pos 'org-linked-text)))
  11578. (throw 'match t))
  11579. (save-excursion
  11580. (when (or (org-in-regexp org-angle-link-re)
  11581. (org-in-regexp org-plain-link-re))
  11582. (setq type (match-string 1) path (match-string 2))
  11583. (throw 'match t)))
  11584. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11585. (setq type "tree-match"
  11586. path (match-string 1))
  11587. (throw 'match t))
  11588. (save-excursion
  11589. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11590. (setq type "tags"
  11591. path (match-string 1))
  11592. (while (string-match ":" path)
  11593. (setq path (replace-match "+" t t path)))
  11594. (throw 'match t))))
  11595. (unless path
  11596. (error "No link found"))
  11597. ;; Remove any trailing spaces in path
  11598. (if (string-match " +\\'" path)
  11599. (setq path (replace-match "" t t path)))
  11600. (cond
  11601. ((assoc type org-link-protocols)
  11602. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11603. ((equal type "mailto")
  11604. (let ((cmd (car org-link-mailto-program))
  11605. (args (cdr org-link-mailto-program)) args1
  11606. (address path) (subject "") a)
  11607. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11608. (setq address (match-string 1 path)
  11609. subject (org-link-escape (match-string 2 path))))
  11610. (while args
  11611. (cond
  11612. ((not (stringp (car args))) (push (pop args) args1))
  11613. (t (setq a (pop args))
  11614. (if (string-match "%a" a)
  11615. (setq a (replace-match address t t a)))
  11616. (if (string-match "%s" a)
  11617. (setq a (replace-match subject t t a)))
  11618. (push a args1))))
  11619. (apply cmd (nreverse args1))))
  11620. ((member type '("http" "https" "ftp" "news"))
  11621. (browse-url (concat type ":" (org-link-escape
  11622. path org-link-escape-chars-browser))))
  11623. ((member type '("message"))
  11624. (browse-url (concat type ":" path)))
  11625. ((string= type "tags")
  11626. (org-tags-view in-emacs path))
  11627. ((string= type "thisfile")
  11628. (if in-emacs
  11629. (switch-to-buffer-other-window
  11630. (org-get-buffer-for-internal-link (current-buffer)))
  11631. (org-mark-ring-push))
  11632. (let ((cmd `(org-link-search
  11633. ,path
  11634. ,(cond ((equal in-emacs '(4)) 'occur)
  11635. ((equal in-emacs '(16)) 'org-occur)
  11636. (t nil))
  11637. ,pos)))
  11638. (condition-case nil (eval cmd)
  11639. (error (progn (widen) (eval cmd))))))
  11640. ((string= type "tree-match")
  11641. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11642. ((string= type "file")
  11643. (if (string-match "::\\([0-9]+\\)\\'" path)
  11644. (setq line (string-to-number (match-string 1 path))
  11645. path (substring path 0 (match-beginning 0)))
  11646. (if (string-match "::\\(.+\\)\\'" path)
  11647. (setq search (match-string 1 path)
  11648. path (substring path 0 (match-beginning 0)))))
  11649. (if (string-match "[*?{]" (file-name-nondirectory path))
  11650. (dired path)
  11651. (org-open-file path in-emacs line search)))
  11652. ((string= type "news")
  11653. (org-follow-gnus-link path))
  11654. ((string= type "bbdb")
  11655. (org-follow-bbdb-link path))
  11656. ((string= type "info")
  11657. (org-follow-info-link path))
  11658. ((string= type "gnus")
  11659. (let (group article)
  11660. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11661. (error "Error in Gnus link"))
  11662. (setq group (match-string 1 path)
  11663. article (match-string 3 path))
  11664. (org-follow-gnus-link group article)))
  11665. ((string= type "vm")
  11666. (let (folder article)
  11667. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11668. (error "Error in VM link"))
  11669. (setq folder (match-string 1 path)
  11670. article (match-string 3 path))
  11671. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11672. (org-follow-vm-link folder article in-emacs)))
  11673. ((string= type "wl")
  11674. (let (folder article)
  11675. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11676. (error "Error in Wanderlust link"))
  11677. (setq folder (match-string 1 path)
  11678. article (match-string 3 path))
  11679. (org-follow-wl-link folder article)))
  11680. ((string= type "mhe")
  11681. (let (folder article)
  11682. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11683. (error "Error in MHE link"))
  11684. (setq folder (match-string 1 path)
  11685. article (match-string 3 path))
  11686. (org-follow-mhe-link folder article)))
  11687. ((string= type "rmail")
  11688. (let (folder article)
  11689. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11690. (error "Error in RMAIL link"))
  11691. (setq folder (match-string 1 path)
  11692. article (match-string 3 path))
  11693. (org-follow-rmail-link folder article)))
  11694. ((string= type "shell")
  11695. (let ((cmd path))
  11696. (if (or (not org-confirm-shell-link-function)
  11697. (funcall org-confirm-shell-link-function
  11698. (format "Execute \"%s\" in shell? "
  11699. (org-add-props cmd nil
  11700. 'face 'org-warning))))
  11701. (progn
  11702. (message "Executing %s" cmd)
  11703. (shell-command cmd))
  11704. (error "Abort"))))
  11705. ((string= type "elisp")
  11706. (let ((cmd path))
  11707. (if (or (not org-confirm-elisp-link-function)
  11708. (funcall org-confirm-elisp-link-function
  11709. (format "Execute \"%s\" as elisp? "
  11710. (org-add-props cmd nil
  11711. 'face 'org-warning))))
  11712. (message "%s => %s" cmd (eval (read cmd)))
  11713. (error "Abort"))))
  11714. (t
  11715. (browse-url-at-point)))))
  11716. (move-marker org-open-link-marker nil)))
  11717. ;;; File search
  11718. (defvar org-create-file-search-functions nil
  11719. "List of functions to construct the right search string for a file link.
  11720. These functions are called in turn with point at the location to
  11721. which the link should point.
  11722. A function in the hook should first test if it would like to
  11723. handle this file type, for example by checking the major-mode or
  11724. the file extension. If it decides not to handle this file, it
  11725. should just return nil to give other functions a chance. If it
  11726. does handle the file, it must return the search string to be used
  11727. when following the link. The search string will be part of the
  11728. file link, given after a double colon, and `org-open-at-point'
  11729. will automatically search for it. If special measures must be
  11730. taken to make the search successful, another function should be
  11731. added to the companion hook `org-execute-file-search-functions',
  11732. which see.
  11733. A function in this hook may also use `setq' to set the variable
  11734. `description' to provide a suggestion for the descriptive text to
  11735. be used for this link when it gets inserted into an Org-mode
  11736. buffer with \\[org-insert-link].")
  11737. (defvar org-execute-file-search-functions nil
  11738. "List of functions to execute a file search triggered by a link.
  11739. Functions added to this hook must accept a single argument, the
  11740. search string that was part of the file link, the part after the
  11741. double colon. The function must first check if it would like to
  11742. handle this search, for example by checking the major-mode or the
  11743. file extension. If it decides not to handle this search, it
  11744. should just return nil to give other functions a chance. If it
  11745. does handle the search, it must return a non-nil value to keep
  11746. other functions from trying.
  11747. Each function can access the current prefix argument through the
  11748. variable `current-prefix-argument'. Note that a single prefix is
  11749. used to force opening a link in Emacs, so it may be good to only
  11750. use a numeric or double prefix to guide the search function.
  11751. In case this is needed, a function in this hook can also restore
  11752. the window configuration before `org-open-at-point' was called using:
  11753. (set-window-configuration org-window-config-before-follow-link)")
  11754. (defun org-link-search (s &optional type avoid-pos)
  11755. "Search for a link search option.
  11756. If S is surrounded by forward slashes, it is interpreted as a
  11757. regular expression. In org-mode files, this will create an `org-occur'
  11758. sparse tree. In ordinary files, `occur' will be used to list matches.
  11759. If the current buffer is in `dired-mode', grep will be used to search
  11760. in all files. If AVOID-POS is given, ignore matches near that position."
  11761. (let ((case-fold-search t)
  11762. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11763. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11764. (append '(("") (" ") ("\t") ("\n"))
  11765. org-emphasis-alist)
  11766. "\\|") "\\)"))
  11767. (pos (point))
  11768. (pre "") (post "")
  11769. words re0 re1 re2 re3 re4 re5 re2a reall)
  11770. (cond
  11771. ;; First check if there are any special
  11772. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11773. ;; Now try the builtin stuff
  11774. ((save-excursion
  11775. (goto-char (point-min))
  11776. (and
  11777. (re-search-forward
  11778. (concat "<<" (regexp-quote s0) ">>") nil t)
  11779. (setq pos (match-beginning 0))))
  11780. ;; There is an exact target for this
  11781. (goto-char pos))
  11782. ((string-match "^/\\(.*\\)/$" s)
  11783. ;; A regular expression
  11784. (cond
  11785. ((org-mode-p)
  11786. (org-occur (match-string 1 s)))
  11787. ;;((eq major-mode 'dired-mode)
  11788. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11789. (t (org-do-occur (match-string 1 s)))))
  11790. (t
  11791. ;; A normal search strings
  11792. (when (equal (string-to-char s) ?*)
  11793. ;; Anchor on headlines, post may include tags.
  11794. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11795. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11796. s (substring s 1)))
  11797. (remove-text-properties
  11798. 0 (length s)
  11799. '(face nil mouse-face nil keymap nil fontified nil) s)
  11800. ;; Make a series of regular expressions to find a match
  11801. (setq words (org-split-string s "[ \n\r\t]+")
  11802. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11803. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11804. "\\)" markers)
  11805. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11806. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11807. re1 (concat pre re2 post)
  11808. re3 (concat pre re4 post)
  11809. re5 (concat pre ".*" re4)
  11810. re2 (concat pre re2)
  11811. re2a (concat pre re2a)
  11812. re4 (concat pre re4)
  11813. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11814. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11815. re5 "\\)"
  11816. ))
  11817. (cond
  11818. ((eq type 'org-occur) (org-occur reall))
  11819. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11820. (t (goto-char (point-min))
  11821. (if (or (org-search-not-self 1 re0 nil t)
  11822. (org-search-not-self 1 re1 nil t)
  11823. (org-search-not-self 1 re2 nil t)
  11824. (org-search-not-self 1 re2a nil t)
  11825. (org-search-not-self 1 re3 nil t)
  11826. (org-search-not-self 1 re4 nil t)
  11827. (org-search-not-self 1 re5 nil t)
  11828. )
  11829. (goto-char (match-beginning 1))
  11830. (goto-char pos)
  11831. (error "No match")))))
  11832. (t
  11833. ;; Normal string-search
  11834. (goto-char (point-min))
  11835. (if (search-forward s nil t)
  11836. (goto-char (match-beginning 0))
  11837. (error "No match"))))
  11838. (and (org-mode-p) (org-show-context 'link-search))))
  11839. (defun org-search-not-self (group &rest args)
  11840. "Execute `re-search-forward', but only accept matches that do not
  11841. enclose the position of `org-open-link-marker'."
  11842. (let ((m org-open-link-marker))
  11843. (catch 'exit
  11844. (while (apply 're-search-forward args)
  11845. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11846. (goto-char (match-end group))
  11847. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11848. (> (match-beginning 0) (marker-position m))
  11849. (< (match-end 0) (marker-position m)))
  11850. (save-match-data
  11851. (or (not (org-in-regexp
  11852. org-bracket-link-analytic-regexp 1))
  11853. (not (match-end 4)) ; no description
  11854. (and (<= (match-beginning 4) (point))
  11855. (>= (match-end 4) (point))))))
  11856. (throw 'exit (point))))))))
  11857. (defun org-get-buffer-for-internal-link (buffer)
  11858. "Return a buffer to be used for displaying the link target of internal links."
  11859. (cond
  11860. ((not org-display-internal-link-with-indirect-buffer)
  11861. buffer)
  11862. ((string-match "(Clone)$" (buffer-name buffer))
  11863. (message "Buffer is already a clone, not making another one")
  11864. ;; we also do not modify visibility in this case
  11865. buffer)
  11866. (t ; make a new indirect buffer for displaying the link
  11867. (let* ((bn (buffer-name buffer))
  11868. (ibn (concat bn "(Clone)"))
  11869. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11870. (with-current-buffer ib (org-overview))
  11871. ib))))
  11872. (defun org-do-occur (regexp &optional cleanup)
  11873. "Call the Emacs command `occur'.
  11874. If CLEANUP is non-nil, remove the printout of the regular expression
  11875. in the *Occur* buffer. This is useful if the regex is long and not useful
  11876. to read."
  11877. (occur regexp)
  11878. (when cleanup
  11879. (let ((cwin (selected-window)) win beg end)
  11880. (when (setq win (get-buffer-window "*Occur*"))
  11881. (select-window win))
  11882. (goto-char (point-min))
  11883. (when (re-search-forward "match[a-z]+" nil t)
  11884. (setq beg (match-end 0))
  11885. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11886. (setq end (1- (match-beginning 0)))))
  11887. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11888. (goto-char (point-min))
  11889. (select-window cwin))))
  11890. ;;; The mark ring for links jumps
  11891. (defvar org-mark-ring nil
  11892. "Mark ring for positions before jumps in Org-mode.")
  11893. (defvar org-mark-ring-last-goto nil
  11894. "Last position in the mark ring used to go back.")
  11895. ;; Fill and close the ring
  11896. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11897. (loop for i from 1 to org-mark-ring-length do
  11898. (push (make-marker) org-mark-ring))
  11899. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11900. org-mark-ring)
  11901. (defun org-mark-ring-push (&optional pos buffer)
  11902. "Put the current position or POS into the mark ring and rotate it."
  11903. (interactive)
  11904. (setq pos (or pos (point)))
  11905. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11906. (move-marker (car org-mark-ring)
  11907. (or pos (point))
  11908. (or buffer (current-buffer)))
  11909. (message "%s"
  11910. (substitute-command-keys
  11911. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11912. (defun org-mark-ring-goto (&optional n)
  11913. "Jump to the previous position in the mark ring.
  11914. With prefix arg N, jump back that many stored positions. When
  11915. called several times in succession, walk through the entire ring.
  11916. Org-mode commands jumping to a different position in the current file,
  11917. or to another Org-mode file, automatically push the old position
  11918. onto the ring."
  11919. (interactive "p")
  11920. (let (p m)
  11921. (if (eq last-command this-command)
  11922. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11923. (setq p org-mark-ring))
  11924. (setq org-mark-ring-last-goto p)
  11925. (setq m (car p))
  11926. (switch-to-buffer (marker-buffer m))
  11927. (goto-char m)
  11928. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11929. (defun org-remove-angle-brackets (s)
  11930. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11931. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11932. s)
  11933. (defun org-add-angle-brackets (s)
  11934. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11935. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11936. s)
  11937. ;;; Following specific links
  11938. (defun org-follow-timestamp-link ()
  11939. (cond
  11940. ((org-at-date-range-p t)
  11941. (let ((org-agenda-start-on-weekday)
  11942. (t1 (match-string 1))
  11943. (t2 (match-string 2)))
  11944. (setq t1 (time-to-days (org-time-string-to-time t1))
  11945. t2 (time-to-days (org-time-string-to-time t2)))
  11946. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11947. ((org-at-timestamp-p t)
  11948. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11949. (substring (match-string 1) 0 10)))
  11950. 1))
  11951. (t (error "This should not happen"))))
  11952. (defun org-follow-bbdb-link (name)
  11953. "Follow a BBDB link to NAME."
  11954. (require 'bbdb)
  11955. (let ((inhibit-redisplay (not debug-on-error))
  11956. (bbdb-electric-p nil))
  11957. (catch 'exit
  11958. ;; Exact match on name
  11959. (bbdb-name (concat "\\`" name "\\'") nil)
  11960. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11961. ;; Exact match on name
  11962. (bbdb-company (concat "\\`" name "\\'") nil)
  11963. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11964. ;; Partial match on name
  11965. (bbdb-name name nil)
  11966. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11967. ;; Partial match on company
  11968. (bbdb-company name nil)
  11969. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11970. ;; General match including network address and notes
  11971. (bbdb name nil)
  11972. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11973. (delete-window (get-buffer-window "*BBDB*"))
  11974. (error "No matching BBDB record")))))
  11975. (defun org-follow-info-link (name)
  11976. "Follow an info file & node link to NAME."
  11977. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11978. (string-match "\\(.*\\)" name))
  11979. (progn
  11980. (require 'info)
  11981. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11982. (Info-find-node (match-string 1 name) (match-string 2 name))
  11983. (Info-find-node (match-string 1 name) "Top")))
  11984. (message "Could not open: %s" name)))
  11985. (defun org-follow-gnus-link (&optional group article)
  11986. "Follow a Gnus link to GROUP and ARTICLE."
  11987. (require 'gnus)
  11988. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11989. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11990. (cond ((and group article)
  11991. (gnus-group-read-group 1 nil group)
  11992. (gnus-summary-goto-article (string-to-number article) nil t))
  11993. (group (gnus-group-jump-to-group group))))
  11994. (defun org-follow-vm-link (&optional folder article readonly)
  11995. "Follow a VM link to FOLDER and ARTICLE."
  11996. (require 'vm)
  11997. (setq article (org-add-angle-brackets article))
  11998. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11999. ;; ange-ftp or efs or tramp access
  12000. (let ((user (or (match-string 1 folder) (user-login-name)))
  12001. (host (match-string 2 folder))
  12002. (file (match-string 3 folder)))
  12003. (cond
  12004. ((featurep 'tramp)
  12005. ;; use tramp to access the file
  12006. (if (featurep 'xemacs)
  12007. (setq folder (format "[%s@%s]%s" user host file))
  12008. (setq folder (format "/%s@%s:%s" user host file))))
  12009. (t
  12010. ;; use ange-ftp or efs
  12011. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12012. (setq folder (format "/%s@%s:%s" user host file))))))
  12013. (when folder
  12014. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12015. (sit-for 0.1)
  12016. (when article
  12017. (vm-select-folder-buffer)
  12018. (widen)
  12019. (let ((case-fold-search t))
  12020. (goto-char (point-min))
  12021. (if (not (re-search-forward
  12022. (concat "^" "message-id: *" (regexp-quote article))))
  12023. (error "Could not find the specified message in this folder"))
  12024. (vm-isearch-update)
  12025. (vm-isearch-narrow)
  12026. (vm-beginning-of-message)
  12027. (vm-summarize)))))
  12028. (defun org-follow-wl-link (folder article)
  12029. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12030. (if (and (string= folder "%")
  12031. article
  12032. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12033. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12034. ;; Thus, we recompose folder and article ids.
  12035. (setq folder (format "%s#%s" folder (match-string 1 article))
  12036. article (match-string 3 article)))
  12037. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12038. (error "No such folder: %s" folder))
  12039. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12040. (and article
  12041. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12042. (wl-summary-redisplay)))
  12043. (defun org-follow-rmail-link (folder article)
  12044. "Follow an RMAIL link to FOLDER and ARTICLE."
  12045. (setq article (org-add-angle-brackets article))
  12046. (let (message-number)
  12047. (save-excursion
  12048. (save-window-excursion
  12049. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12050. (setq message-number
  12051. (save-restriction
  12052. (widen)
  12053. (goto-char (point-max))
  12054. (if (re-search-backward
  12055. (concat "^Message-ID:\\s-+" (regexp-quote
  12056. (or article "")))
  12057. nil t)
  12058. (rmail-what-message))))))
  12059. (if message-number
  12060. (progn
  12061. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12062. (rmail-show-message message-number)
  12063. message-number)
  12064. (error "Message not found"))))
  12065. ;;; mh-e integration based on planner-mode
  12066. (defun org-mhe-get-message-real-folder ()
  12067. "Return the name of the current message real folder, so if you use
  12068. sequences, it will now work."
  12069. (save-excursion
  12070. (let* ((folder
  12071. (if (equal major-mode 'mh-folder-mode)
  12072. mh-current-folder
  12073. ;; Refer to the show buffer
  12074. mh-show-folder-buffer))
  12075. (end-index
  12076. (if (boundp 'mh-index-folder)
  12077. (min (length mh-index-folder) (length folder))))
  12078. )
  12079. ;; a simple test on mh-index-data does not work, because
  12080. ;; mh-index-data is always nil in a show buffer.
  12081. (if (and (boundp 'mh-index-folder)
  12082. (string= mh-index-folder (substring folder 0 end-index)))
  12083. (if (equal major-mode 'mh-show-mode)
  12084. (save-window-excursion
  12085. (let (pop-up-frames)
  12086. (when (buffer-live-p (get-buffer folder))
  12087. (progn
  12088. (pop-to-buffer folder)
  12089. (org-mhe-get-message-folder-from-index)
  12090. )
  12091. )))
  12092. (org-mhe-get-message-folder-from-index)
  12093. )
  12094. folder
  12095. )
  12096. )))
  12097. (defun org-mhe-get-message-folder-from-index ()
  12098. "Returns the name of the message folder in a index folder buffer."
  12099. (save-excursion
  12100. (mh-index-previous-folder)
  12101. (re-search-forward "^\\(+.*\\)$" nil t)
  12102. (message "%s" (match-string 1))))
  12103. (defun org-mhe-get-message-folder ()
  12104. "Return the name of the current message folder. Be careful if you
  12105. use sequences."
  12106. (save-excursion
  12107. (if (equal major-mode 'mh-folder-mode)
  12108. mh-current-folder
  12109. ;; Refer to the show buffer
  12110. mh-show-folder-buffer)))
  12111. (defun org-mhe-get-message-num ()
  12112. "Return the number of the current message. Be careful if you
  12113. use sequences."
  12114. (save-excursion
  12115. (if (equal major-mode 'mh-folder-mode)
  12116. (mh-get-msg-num nil)
  12117. ;; Refer to the show buffer
  12118. (mh-show-buffer-message-number))))
  12119. (defun org-mhe-get-header (header)
  12120. "Return a header of the message in folder mode. This will create a
  12121. show buffer for the corresponding message. If you have a more clever
  12122. idea..."
  12123. (let* ((folder (org-mhe-get-message-folder))
  12124. (num (org-mhe-get-message-num))
  12125. (buffer (get-buffer-create (concat "show-" folder)))
  12126. (header-field))
  12127. (with-current-buffer buffer
  12128. (mh-display-msg num folder)
  12129. (if (equal major-mode 'mh-folder-mode)
  12130. (mh-header-display)
  12131. (mh-show-header-display))
  12132. (set-buffer buffer)
  12133. (setq header-field (mh-get-header-field header))
  12134. (if (equal major-mode 'mh-folder-mode)
  12135. (mh-show)
  12136. (mh-show-show))
  12137. header-field)))
  12138. (defun org-follow-mhe-link (folder article)
  12139. "Follow an MHE link to FOLDER and ARTICLE.
  12140. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12141. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12142. ARTICLE is searched in all folders. Indexed searches (swish++,
  12143. namazu, and others supported by MH-E) will always search in all
  12144. folders."
  12145. (require 'mh-e)
  12146. (require 'mh-search)
  12147. (require 'mh-utils)
  12148. (mh-find-path)
  12149. (if (not article)
  12150. (mh-visit-folder (mh-normalize-folder-name folder))
  12151. (setq article (org-add-angle-brackets article))
  12152. (mh-search-choose)
  12153. (if (equal mh-searcher 'pick)
  12154. (progn
  12155. (mh-search folder (list "--message-id" article))
  12156. (when (and org-mhe-search-all-folders
  12157. (not (org-mhe-get-message-real-folder)))
  12158. (kill-this-buffer)
  12159. (mh-search "+" (list "--message-id" article))))
  12160. (mh-search "+" article))
  12161. (if (org-mhe-get-message-real-folder)
  12162. (mh-show-msg 1)
  12163. (kill-this-buffer)
  12164. (error "Message not found"))))
  12165. ;;; BibTeX links
  12166. ;; Use the custom search meachnism to construct and use search strings for
  12167. ;; file links to BibTeX database entries.
  12168. (defun org-create-file-search-in-bibtex ()
  12169. "Create the search string and description for a BibTeX database entry."
  12170. (when (eq major-mode 'bibtex-mode)
  12171. ;; yes, we want to construct this search string.
  12172. ;; Make a good description for this entry, using names, year and the title
  12173. ;; Put it into the `description' variable which is dynamically scoped.
  12174. (let ((bibtex-autokey-names 1)
  12175. (bibtex-autokey-names-stretch 1)
  12176. (bibtex-autokey-name-case-convert-function 'identity)
  12177. (bibtex-autokey-name-separator " & ")
  12178. (bibtex-autokey-additional-names " et al.")
  12179. (bibtex-autokey-year-length 4)
  12180. (bibtex-autokey-name-year-separator " ")
  12181. (bibtex-autokey-titlewords 3)
  12182. (bibtex-autokey-titleword-separator " ")
  12183. (bibtex-autokey-titleword-case-convert-function 'identity)
  12184. (bibtex-autokey-titleword-length 'infty)
  12185. (bibtex-autokey-year-title-separator ": "))
  12186. (setq description (bibtex-generate-autokey)))
  12187. ;; Now parse the entry, get the key and return it.
  12188. (save-excursion
  12189. (bibtex-beginning-of-entry)
  12190. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12191. (defun org-execute-file-search-in-bibtex (s)
  12192. "Find the link search string S as a key for a database entry."
  12193. (when (eq major-mode 'bibtex-mode)
  12194. ;; Yes, we want to do the search in this file.
  12195. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12196. (goto-char (point-min))
  12197. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12198. (regexp-quote s) "[ \t\n]*,") nil t)
  12199. (goto-char (match-beginning 0)))
  12200. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12201. ;; Use double prefix to indicate that any web link should be browsed
  12202. (let ((b (current-buffer)) (p (point)))
  12203. ;; Restore the window configuration because we just use the web link
  12204. (set-window-configuration org-window-config-before-follow-link)
  12205. (save-excursion (set-buffer b) (goto-char p)
  12206. (bibtex-url)))
  12207. (recenter 0)) ; Move entry start to beginning of window
  12208. ;; return t to indicate that the search is done.
  12209. t))
  12210. ;; Finally add the functions to the right hooks.
  12211. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12212. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12213. ;; end of Bibtex link setup
  12214. ;;; Following file links
  12215. (defun org-open-file (path &optional in-emacs line search)
  12216. "Open the file at PATH.
  12217. First, this expands any special file name abbreviations. Then the
  12218. configuration variable `org-file-apps' is checked if it contains an
  12219. entry for this file type, and if yes, the corresponding command is launched.
  12220. If no application is found, Emacs simply visits the file.
  12221. With optional argument IN-EMACS, Emacs will visit the file.
  12222. Optional LINE specifies a line to go to, optional SEARCH a string to
  12223. search for. If LINE or SEARCH is given, the file will always be
  12224. opened in Emacs.
  12225. If the file does not exist, an error is thrown."
  12226. (setq in-emacs (or in-emacs line search))
  12227. (let* ((file (if (equal path "")
  12228. buffer-file-name
  12229. (substitute-in-file-name (expand-file-name path))))
  12230. (apps (append org-file-apps (org-default-apps)))
  12231. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12232. (dirp (if remp nil (file-directory-p file)))
  12233. (dfile (downcase file))
  12234. (old-buffer (current-buffer))
  12235. (old-pos (point))
  12236. (old-mode major-mode)
  12237. ext cmd)
  12238. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12239. (setq ext (match-string 1 dfile))
  12240. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12241. (setq ext (match-string 1 dfile))))
  12242. (if in-emacs
  12243. (setq cmd 'emacs)
  12244. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12245. (and dirp (cdr (assoc 'directory apps)))
  12246. (cdr (assoc ext apps))
  12247. (cdr (assoc t apps)))))
  12248. (when (eq cmd 'mailcap)
  12249. (require 'mailcap)
  12250. (mailcap-parse-mailcaps)
  12251. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12252. (command (mailcap-mime-info mime-type)))
  12253. (if (stringp command)
  12254. (setq cmd command)
  12255. (setq cmd 'emacs))))
  12256. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12257. (not (file-exists-p file))
  12258. (not org-open-non-existing-files))
  12259. (error "No such file: %s" file))
  12260. (cond
  12261. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12262. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12263. (while (string-match "['\"]%s['\"]" cmd)
  12264. (setq cmd (replace-match "%s" t t cmd)))
  12265. (while (string-match "%s" cmd)
  12266. (setq cmd (replace-match
  12267. (save-match-data (shell-quote-argument file))
  12268. t t cmd)))
  12269. (save-window-excursion
  12270. (start-process-shell-command cmd nil cmd)))
  12271. ((or (stringp cmd)
  12272. (eq cmd 'emacs))
  12273. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12274. (widen)
  12275. (if line (goto-line line)
  12276. (if search (org-link-search search))))
  12277. ((consp cmd)
  12278. (eval cmd))
  12279. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12280. (and (org-mode-p) (eq old-mode 'org-mode)
  12281. (or (not (equal old-buffer (current-buffer)))
  12282. (not (equal old-pos (point))))
  12283. (org-mark-ring-push old-pos old-buffer))))
  12284. (defun org-default-apps ()
  12285. "Return the default applications for this operating system."
  12286. (cond
  12287. ((eq system-type 'darwin)
  12288. org-file-apps-defaults-macosx)
  12289. ((eq system-type 'windows-nt)
  12290. org-file-apps-defaults-windowsnt)
  12291. (t org-file-apps-defaults-gnu)))
  12292. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12293. (defun org-file-remote-p (file)
  12294. "Test whether FILE specifies a location on a remote system.
  12295. Return non-nil if the location is indeed remote.
  12296. For example, the filename \"/user@host:/foo\" specifies a location
  12297. on the system \"/user@host:\"."
  12298. (cond ((fboundp 'file-remote-p)
  12299. (file-remote-p file))
  12300. ((fboundp 'tramp-handle-file-remote-p)
  12301. (tramp-handle-file-remote-p file))
  12302. ((and (boundp 'ange-ftp-name-format)
  12303. (string-match (car ange-ftp-name-format) file))
  12304. t)
  12305. (t nil)))
  12306. ;;;; Hooks for remember.el, and refiling
  12307. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12308. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12309. ;;;###autoload
  12310. (defun org-remember-insinuate ()
  12311. "Setup remember.el for use wiht Org-mode."
  12312. (require 'remember)
  12313. (setq remember-annotation-functions '(org-remember-annotation))
  12314. (setq remember-handler-functions '(org-remember-handler))
  12315. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12316. ;;;###autoload
  12317. (defun org-remember-annotation ()
  12318. "Return a link to the current location as an annotation for remember.el.
  12319. If you are using Org-mode files as target for data storage with
  12320. remember.el, then the annotations should include a link compatible with the
  12321. conventions in Org-mode. This function returns such a link."
  12322. (org-store-link nil))
  12323. (defconst org-remember-help
  12324. "Select a destination location for the note.
  12325. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12326. RET on headline -> Store as sublevel entry to current headline
  12327. RET at beg-of-buf -> Append to file as level 2 headline
  12328. <left>/<right> -> before/after current headline, same headings level")
  12329. (defvar org-remember-previous-location nil)
  12330. (defvar org-force-remember-template-char) ;; dynamically scoped
  12331. (defun org-select-remember-template (&optional use-char)
  12332. (when org-remember-templates
  12333. (let* ((templates (mapcar (lambda (x)
  12334. (if (stringp (car x))
  12335. (append (list (nth 1 x) (car x)) (cddr x))
  12336. (append (list (car x) "") (cdr x))))
  12337. org-remember-templates))
  12338. (char (or use-char
  12339. (cond
  12340. ((= (length templates) 1)
  12341. (caar templates))
  12342. ((and (boundp 'org-force-remember-template-char)
  12343. org-force-remember-template-char)
  12344. (if (stringp org-force-remember-template-char)
  12345. (string-to-char org-force-remember-template-char)
  12346. org-force-remember-template-char))
  12347. (t
  12348. (message "Select template: %s"
  12349. (mapconcat
  12350. (lambda (x)
  12351. (cond
  12352. ((not (string-match "\\S-" (nth 1 x)))
  12353. (format "[%c]" (car x)))
  12354. ((equal (downcase (car x))
  12355. (downcase (aref (nth 1 x) 0)))
  12356. (format "[%c]%s" (car x)
  12357. (substring (nth 1 x) 1)))
  12358. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12359. templates " "))
  12360. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12361. (when (equal char0 ?\C-g)
  12362. (jump-to-register remember-register)
  12363. (kill-buffer remember-buffer))
  12364. char0))))))
  12365. (cddr (assoc char templates)))))
  12366. (defvar x-last-selected-text)
  12367. (defvar x-last-selected-text-primary)
  12368. ;;;###autoload
  12369. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12370. "Initialize *remember* buffer with template, invoke `org-mode'.
  12371. This function should be placed into `remember-mode-hook' and in fact requires
  12372. to be run from that hook to function properly."
  12373. (if org-remember-templates
  12374. (let* ((entry (org-select-remember-template use-char))
  12375. (tpl (car entry))
  12376. (plist-p (if org-store-link-plist t nil))
  12377. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12378. (string-match "\\S-" (nth 1 entry)))
  12379. (nth 1 entry)
  12380. org-default-notes-file))
  12381. (headline (nth 2 entry))
  12382. (v-c (or (and (eq window-system 'x)
  12383. (fboundp 'x-cut-buffer-or-selection-value)
  12384. (x-cut-buffer-or-selection-value))
  12385. (org-bound-and-true-p x-last-selected-text)
  12386. (org-bound-and-true-p x-last-selected-text-primary)
  12387. (and (> (length kill-ring) 0) (current-kill 0))))
  12388. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12389. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12390. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12391. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12392. ;; `initial' and `annotation' are bound in `remember'
  12393. (v-i (if (boundp 'initial) initial))
  12394. (v-a (if (and (boundp 'annotation) annotation)
  12395. (if (equal annotation "[[]]") "" annotation)
  12396. ""))
  12397. (v-A (if (and v-a
  12398. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12399. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12400. v-a))
  12401. (v-n user-full-name)
  12402. (org-startup-folded nil)
  12403. org-time-was-given org-end-time-was-given x
  12404. prompt completions char time pos default histvar)
  12405. (setq org-store-link-plist
  12406. (append (list :annotation v-a :initial v-i)
  12407. org-store-link-plist))
  12408. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12409. (erase-buffer)
  12410. (insert (substitute-command-keys
  12411. (format
  12412. "## Filing location: Select interactively, default, or last used:
  12413. ## %s to select file and header location interactively.
  12414. ## %s \"%s\" -> \"* %s\"
  12415. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12416. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12417. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12418. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12419. (abbreviate-file-name (or file org-default-notes-file))
  12420. (or headline "")
  12421. (or (car org-remember-previous-location) "???")
  12422. (or (cdr org-remember-previous-location) "???"))))
  12423. (insert tpl) (goto-char (point-min))
  12424. ;; Simple %-escapes
  12425. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12426. (when (and initial (equal (match-string 0) "%i"))
  12427. (save-match-data
  12428. (let* ((lead (buffer-substring
  12429. (point-at-bol) (match-beginning 0))))
  12430. (setq v-i (mapconcat 'identity
  12431. (org-split-string initial "\n")
  12432. (concat "\n" lead))))))
  12433. (replace-match
  12434. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12435. t t))
  12436. ;; %[] Insert contents of a file.
  12437. (goto-char (point-min))
  12438. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12439. (let ((start (match-beginning 0))
  12440. (end (match-end 0))
  12441. (filename (expand-file-name (match-string 1))))
  12442. (goto-char start)
  12443. (delete-region start end)
  12444. (condition-case error
  12445. (insert-file-contents filename)
  12446. (error (insert (format "%%![Couldn't insert %s: %s]"
  12447. filename error))))))
  12448. ;; %() embedded elisp
  12449. (goto-char (point-min))
  12450. (while (re-search-forward "%\\((.+)\\)" nil t)
  12451. (goto-char (match-beginning 0))
  12452. (let ((template-start (point)))
  12453. (forward-char 1)
  12454. (let ((result
  12455. (condition-case error
  12456. (eval (read (current-buffer)))
  12457. (error (format "%%![Error: %s]" error)))))
  12458. (delete-region template-start (point))
  12459. (insert result))))
  12460. ;; From the property list
  12461. (when plist-p
  12462. (goto-char (point-min))
  12463. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12464. (and (setq x (or (plist-get org-store-link-plist
  12465. (intern (match-string 1))) ""))
  12466. (replace-match x t t))))
  12467. ;; Turn on org-mode in the remember buffer, set local variables
  12468. (org-mode)
  12469. (org-set-local 'org-finish-function 'org-remember-finalize)
  12470. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12471. (org-set-local 'org-default-notes-file file))
  12472. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12473. (org-set-local 'org-remember-default-headline headline))
  12474. ;; Interactive template entries
  12475. (goto-char (point-min))
  12476. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12477. (setq char (if (match-end 3) (match-string 3))
  12478. prompt (if (match-end 2) (match-string 2)))
  12479. (goto-char (match-beginning 0))
  12480. (replace-match "")
  12481. (setq completions nil default nil)
  12482. (when prompt
  12483. (setq completions (org-split-string prompt "|")
  12484. prompt (pop completions)
  12485. default (car completions)
  12486. histvar (intern (concat
  12487. "org-remember-template-prompt-history::"
  12488. (or prompt "")))
  12489. completions (mapcar 'list completions)))
  12490. (cond
  12491. ((member char '("G" "g"))
  12492. (let* ((org-last-tags-completion-table
  12493. (org-global-tags-completion-table
  12494. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12495. (org-add-colon-after-tag-completion t)
  12496. (ins (completing-read
  12497. (if prompt (concat prompt ": ") "Tags: ")
  12498. 'org-tags-completion-function nil nil nil
  12499. 'org-tags-history)))
  12500. (setq ins (mapconcat 'identity
  12501. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12502. ":"))
  12503. (when (string-match "\\S-" ins)
  12504. (or (equal (char-before) ?:) (insert ":"))
  12505. (insert ins)
  12506. (or (equal (char-after) ?:) (insert ":")))))
  12507. (char
  12508. (setq org-time-was-given (equal (upcase char) char))
  12509. (setq time (org-read-date (equal (upcase char) "U") t nil
  12510. prompt))
  12511. (org-insert-time-stamp time org-time-was-given
  12512. (member char '("u" "U"))
  12513. nil nil (list org-end-time-was-given)))
  12514. (t
  12515. (insert (org-completing-read
  12516. (concat (if prompt prompt "Enter string")
  12517. (if default (concat " [" default "]"))
  12518. ": ")
  12519. completions nil nil nil histvar default)))))
  12520. (goto-char (point-min))
  12521. (if (re-search-forward "%\\?" nil t)
  12522. (replace-match "")
  12523. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12524. (org-mode)
  12525. (org-set-local 'org-finish-function 'org-remember-finalize))
  12526. (when (save-excursion
  12527. (goto-char (point-min))
  12528. (re-search-forward "%!" nil t))
  12529. (replace-match "")
  12530. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12531. (defun org-remember-finish-immediately ()
  12532. "File remember note immediately.
  12533. This should be run in `post-command-hook' and will remove itself
  12534. from that hook."
  12535. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12536. (when org-finish-function
  12537. (funcall org-finish-function)))
  12538. (defvar org-clock-marker) ; Defined below
  12539. (defun org-remember-finalize ()
  12540. "Finalize the remember process."
  12541. (unless (fboundp 'remember-finalize)
  12542. (defalias 'remember-finalize 'remember-buffer))
  12543. (when (and org-clock-marker
  12544. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12545. ;; FIXME: test this, this is w/o notetaking!
  12546. (let (org-log-note-clock-out) (org-clock-out)))
  12547. (when buffer-file-name
  12548. (save-buffer)
  12549. (setq buffer-file-name nil))
  12550. (remember-finalize))
  12551. ;;;###autoload
  12552. (defun org-remember (&optional goto org-force-remember-template-char)
  12553. "Call `remember'. If this is already a remember buffer, re-apply template.
  12554. If there is an active region, make sure remember uses it as initial content
  12555. of the remember buffer.
  12556. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12557. anything, just go to the file/headline where the selected template usually
  12558. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12559. note stored by remember.
  12560. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12561. associated with a template in `org-remember-templates'."
  12562. (interactive "P")
  12563. (cond
  12564. ((equal goto '(4)) (org-go-to-remember-target))
  12565. ((equal goto '(16)) (org-remember-goto-last-stored))
  12566. (t
  12567. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12568. (progn
  12569. (when (< (length org-remember-templates) 2)
  12570. (error "No other template available"))
  12571. (erase-buffer)
  12572. (let ((annotation (plist-get org-store-link-plist :annotation))
  12573. (initial (plist-get org-store-link-plist :initial)))
  12574. (org-remember-apply-template))
  12575. (message "Press C-c C-c to remember data"))
  12576. (if (org-region-active-p)
  12577. (remember (buffer-substring (point) (mark)))
  12578. (call-interactively 'remember))))))
  12579. (defun org-remember-goto-last-stored ()
  12580. "Go to the location where the last remember note was stored."
  12581. (interactive)
  12582. (bookmark-jump "org-remember-last-stored")
  12583. (message "This is the last note stored by remember"))
  12584. (defun org-go-to-remember-target (&optional template-key)
  12585. "Go to the target location of a remember template.
  12586. The user is queried for the template."
  12587. (interactive)
  12588. (let* ((entry (org-select-remember-template template-key))
  12589. (file (nth 1 entry))
  12590. (heading (nth 2 entry))
  12591. visiting)
  12592. (unless (and file (stringp file) (string-match "\\S-" file))
  12593. (setq file org-default-notes-file))
  12594. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12595. (setq heading org-remember-default-headline))
  12596. (setq visiting (org-find-base-buffer-visiting file))
  12597. (if (not visiting) (find-file-noselect file))
  12598. (switch-to-buffer (or visiting (get-file-buffer file)))
  12599. (widen)
  12600. (goto-char (point-min))
  12601. (if (re-search-forward
  12602. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12603. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12604. nil t)
  12605. (goto-char (match-beginning 0))
  12606. (error "Target headline not found: %s" heading))))
  12607. (defvar org-note-abort nil) ; dynamically scoped
  12608. ;;;###autoload
  12609. (defun org-remember-handler ()
  12610. "Store stuff from remember.el into an org file.
  12611. First prompts for an org file. If the user just presses return, the value
  12612. of `org-default-notes-file' is used.
  12613. Then the command offers the headings tree of the selected file in order to
  12614. file the text at a specific location.
  12615. You can either immediately press RET to get the note appended to the
  12616. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12617. find a better place. Then press RET or <left> or <right> in insert the note.
  12618. Key Cursor position Note gets inserted
  12619. -----------------------------------------------------------------------------
  12620. RET buffer-start as level 1 heading at end of file
  12621. RET on headline as sublevel of the heading at cursor
  12622. RET no heading at cursor position, level taken from context.
  12623. Or use prefix arg to specify level manually.
  12624. <left> on headline as same level, before current heading
  12625. <right> on headline as same level, after current heading
  12626. So the fastest way to store the note is to press RET RET to append it to
  12627. the default file. This way your current train of thought is not
  12628. interrupted, in accordance with the principles of remember.el.
  12629. You can also get the fast execution without prompting by using
  12630. C-u C-c C-c to exit the remember buffer. See also the variable
  12631. `org-remember-store-without-prompt'.
  12632. Before being stored away, the function ensures that the text has a
  12633. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12634. is constructed from the current date and some additional data.
  12635. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12636. also indented so that it starts in the same column as the headline
  12637. \(i.e. after the stars).
  12638. See also the variable `org-reverse-note-order'."
  12639. (goto-char (point-min))
  12640. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12641. (replace-match ""))
  12642. (goto-char (point-max))
  12643. (beginning-of-line 1)
  12644. (while (looking-at "[ \t]*$\\|##.*")
  12645. (delete-region (1- (point)) (point-max))
  12646. (beginning-of-line 1))
  12647. (catch 'quit
  12648. (if org-note-abort (throw 'quit nil))
  12649. (let* ((txt (buffer-substring (point-min) (point-max)))
  12650. (fastp (org-xor (equal current-prefix-arg '(4))
  12651. org-remember-store-without-prompt))
  12652. (file (cond
  12653. (fastp org-default-notes-file)
  12654. ((and (eq org-remember-interactive-interface 'refile)
  12655. org-refile-targets)
  12656. org-default-notes-file)
  12657. ((not (and (equal current-prefix-arg '(16))
  12658. org-remember-previous-location))
  12659. (org-get-org-file))))
  12660. (heading org-remember-default-headline)
  12661. (visiting (and file (org-find-base-buffer-visiting file)))
  12662. (org-startup-folded nil)
  12663. (org-startup-align-all-tables nil)
  12664. (org-goto-start-pos 1)
  12665. spos exitcmd level indent reversed)
  12666. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12667. (setq file (car org-remember-previous-location)
  12668. heading (cdr org-remember-previous-location)
  12669. fastp t))
  12670. (setq current-prefix-arg nil)
  12671. (if (string-match "[ \t\n]+\\'" txt)
  12672. (setq txt (replace-match "" t t txt)))
  12673. ;; Modify text so that it becomes a nice subtree which can be inserted
  12674. ;; into an org tree.
  12675. (let* ((lines (split-string txt "\n"))
  12676. first)
  12677. (setq first (car lines) lines (cdr lines))
  12678. (if (string-match "^\\*+ " first)
  12679. ;; Is already a headline
  12680. (setq indent nil)
  12681. ;; We need to add a headline: Use time and first buffer line
  12682. (setq lines (cons first lines)
  12683. first (concat "* " (current-time-string)
  12684. " (" (remember-buffer-desc) ")")
  12685. indent " "))
  12686. (if (and org-adapt-indentation indent)
  12687. (setq lines (mapcar
  12688. (lambda (x)
  12689. (if (string-match "\\S-" x)
  12690. (concat indent x) x))
  12691. lines)))
  12692. (setq txt (concat first "\n"
  12693. (mapconcat 'identity lines "\n"))))
  12694. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12695. (setq txt (replace-match "\n\n" t t txt))
  12696. (if (string-match "[ \t\n]*\\'" txt)
  12697. (setq txt (replace-match "\n" t t txt))))
  12698. ;; Put the modified text back into the remember buffer, for refile.
  12699. (erase-buffer)
  12700. (insert txt)
  12701. (goto-char (point-min))
  12702. (when (and (eq org-remember-interactive-interface 'refile)
  12703. (not fastp))
  12704. (org-refile nil (or visiting (find-file-noselect file)))
  12705. (throw 'quit t))
  12706. ;; Find the file
  12707. (if (not visiting) (find-file-noselect file))
  12708. (with-current-buffer (or visiting (get-file-buffer file))
  12709. (unless (org-mode-p)
  12710. (error "Target files for remember notes must be in Org-mode"))
  12711. (save-excursion
  12712. (save-restriction
  12713. (widen)
  12714. (and (goto-char (point-min))
  12715. (not (re-search-forward "^\\* " nil t))
  12716. (insert "\n* " (or heading "Notes") "\n"))
  12717. (setq reversed (org-notes-order-reversed-p))
  12718. ;; Find the default location
  12719. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12720. (goto-char (point-min))
  12721. (if (re-search-forward
  12722. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12723. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12724. nil t)
  12725. (setq org-goto-start-pos (match-beginning 0))
  12726. (when fastp
  12727. (goto-char (point-max))
  12728. (unless (bolp) (newline))
  12729. (insert "* " heading "\n")
  12730. (setq org-goto-start-pos (point-at-bol 0)))))
  12731. ;; Ask the User for a location, using the appropriate interface
  12732. (cond
  12733. (fastp (setq spos org-goto-start-pos
  12734. exitcmd 'return))
  12735. ((eq org-remember-interactive-interface 'outline)
  12736. (setq spos (org-get-location (current-buffer)
  12737. org-remember-help)
  12738. exitcmd (cdr spos)
  12739. spos (car spos)))
  12740. ((eq org-remember-interactive-interface 'outline-path-completion)
  12741. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12742. (org-refile-use-outline-path t))
  12743. (setq spos (org-refile-get-location "Heading: ")
  12744. exitcmd 'return
  12745. spos (nth 3 spos))))
  12746. (t (error "this should not hapen")))
  12747. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12748. ; not handle this note
  12749. (goto-char spos)
  12750. (cond ((org-on-heading-p t)
  12751. (org-back-to-heading t)
  12752. (setq level (funcall outline-level))
  12753. (cond
  12754. ((eq exitcmd 'return)
  12755. ;; sublevel of current
  12756. (setq org-remember-previous-location
  12757. (cons (abbreviate-file-name file)
  12758. (org-get-heading 'notags)))
  12759. (if reversed
  12760. (outline-next-heading)
  12761. (org-end-of-subtree t)
  12762. (if (not (bolp))
  12763. (if (looking-at "[ \t]*\n")
  12764. (beginning-of-line 2)
  12765. (end-of-line 1)
  12766. (insert "\n"))))
  12767. (bookmark-set "org-remember-last-stored")
  12768. (org-paste-subtree (org-get-legal-level level 1) txt))
  12769. ((eq exitcmd 'left)
  12770. ;; before current
  12771. (bookmark-set "org-remember-last-stored")
  12772. (org-paste-subtree level txt))
  12773. ((eq exitcmd 'right)
  12774. ;; after current
  12775. (org-end-of-subtree t)
  12776. (bookmark-set "org-remember-last-stored")
  12777. (org-paste-subtree level txt))
  12778. (t (error "This should not happen"))))
  12779. ((and (bobp) (not reversed))
  12780. ;; Put it at the end, one level below level 1
  12781. (save-restriction
  12782. (widen)
  12783. (goto-char (point-max))
  12784. (if (not (bolp)) (newline))
  12785. (bookmark-set "org-remember-last-stored")
  12786. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12787. ((and (bobp) reversed)
  12788. ;; Put it at the start, as level 1
  12789. (save-restriction
  12790. (widen)
  12791. (goto-char (point-min))
  12792. (re-search-forward "^\\*+ " nil t)
  12793. (beginning-of-line 1)
  12794. (bookmark-set "org-remember-last-stored")
  12795. (org-paste-subtree 1 txt)))
  12796. (t
  12797. ;; Put it right there, with automatic level determined by
  12798. ;; org-paste-subtree or from prefix arg
  12799. (bookmark-set "org-remember-last-stored")
  12800. (org-paste-subtree
  12801. (if (numberp current-prefix-arg) current-prefix-arg)
  12802. txt)))
  12803. (when remember-save-after-remembering
  12804. (save-buffer)
  12805. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12806. t) ;; return t to indicate that we took care of this note.
  12807. (defun org-get-org-file ()
  12808. "Read a filename, with default directory `org-directory'."
  12809. (let ((default (or org-default-notes-file remember-data-file)))
  12810. (read-file-name (format "File name [%s]: " default)
  12811. (file-name-as-directory org-directory)
  12812. default)))
  12813. (defun org-notes-order-reversed-p ()
  12814. "Check if the current file should receive notes in reversed order."
  12815. (cond
  12816. ((not org-reverse-note-order) nil)
  12817. ((eq t org-reverse-note-order) t)
  12818. ((not (listp org-reverse-note-order)) nil)
  12819. (t (catch 'exit
  12820. (let ((all org-reverse-note-order)
  12821. entry)
  12822. (while (setq entry (pop all))
  12823. (if (string-match (car entry) buffer-file-name)
  12824. (throw 'exit (cdr entry))))
  12825. nil)))))
  12826. ;;; Refiling
  12827. (defvar org-refile-target-table nil
  12828. "The list of refile targets, created by `org-refile'.")
  12829. (defvar org-agenda-new-buffers nil
  12830. "Buffers created to visit agenda files.")
  12831. (defun org-get-refile-targets (&optional default-buffer)
  12832. "Produce a table with refile targets."
  12833. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12834. targets txt re files f desc descre)
  12835. (with-current-buffer (or default-buffer (current-buffer))
  12836. (while (setq entry (pop entries))
  12837. (setq files (car entry) desc (cdr entry))
  12838. (cond
  12839. ((null files) (setq files (list (current-buffer))))
  12840. ((eq files 'org-agenda-files)
  12841. (setq files (org-agenda-files 'unrestricted)))
  12842. ((and (symbolp files) (fboundp files))
  12843. (setq files (funcall files)))
  12844. ((and (symbolp files) (boundp files))
  12845. (setq files (symbol-value files))))
  12846. (if (stringp files) (setq files (list files)))
  12847. (cond
  12848. ((eq (car desc) :tag)
  12849. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12850. ((eq (car desc) :todo)
  12851. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12852. ((eq (car desc) :regexp)
  12853. (setq descre (cdr desc)))
  12854. ((eq (car desc) :level)
  12855. (setq descre (concat "^\\*\\{" (number-to-string
  12856. (if org-odd-levels-only
  12857. (1- (* 2 (cdr desc)))
  12858. (cdr desc)))
  12859. "\\}[ \t]")))
  12860. ((eq (car desc) :maxlevel)
  12861. (setq descre (concat "^\\*\\{1," (number-to-string
  12862. (if org-odd-levels-only
  12863. (1- (* 2 (cdr desc)))
  12864. (cdr desc)))
  12865. "\\}[ \t]")))
  12866. (t (error "Bad refiling target description %s" desc)))
  12867. (while (setq f (pop files))
  12868. (save-excursion
  12869. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12870. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12871. (save-excursion
  12872. (save-restriction
  12873. (widen)
  12874. (goto-char (point-min))
  12875. (while (re-search-forward descre nil t)
  12876. (goto-char (point-at-bol))
  12877. (when (looking-at org-complex-heading-regexp)
  12878. (setq txt (match-string 4)
  12879. re (concat "^" (regexp-quote
  12880. (buffer-substring (match-beginning 1)
  12881. (match-end 4)))))
  12882. (if (match-end 5) (setq re (concat re "[ \t]+"
  12883. (regexp-quote
  12884. (match-string 5)))))
  12885. (setq re (concat re "[ \t]*$"))
  12886. (when org-refile-use-outline-path
  12887. (setq txt (mapconcat 'identity
  12888. (append
  12889. (if (eq org-refile-use-outline-path 'file)
  12890. (list (file-name-nondirectory
  12891. (buffer-file-name (buffer-base-buffer))))
  12892. (if (eq org-refile-use-outline-path 'full-file-path)
  12893. (list (buffer-file-name (buffer-base-buffer)))))
  12894. (org-get-outline-path)
  12895. (list txt))
  12896. "/")))
  12897. (push (list txt f re (point)) targets))
  12898. (goto-char (point-at-eol))))))))
  12899. (nreverse targets))))
  12900. (defun org-get-outline-path ()
  12901. "Return the outline path to the current entry, as a list."
  12902. (let (rtn)
  12903. (save-excursion
  12904. (while (org-up-heading-safe)
  12905. (when (looking-at org-complex-heading-regexp)
  12906. (push (org-match-string-no-properties 4) rtn)))
  12907. rtn)))
  12908. (defvar org-refile-history nil
  12909. "History for refiling operations.")
  12910. (defun org-refile (&optional goto default-buffer)
  12911. "Move the entry at point to another heading.
  12912. The list of target headings is compiled using the information in
  12913. `org-refile-targets', which see. This list is created upon first use, and
  12914. you can update it by calling this command with a double prefix (`C-u C-u').
  12915. FIXME: Can we find a better way of updating?
  12916. At the target location, the entry is filed as a subitem of the target heading.
  12917. Depending on `org-reverse-note-order', the new subitem will either be the
  12918. first of the last subitem.
  12919. With prefix arg GOTO, the command will only visit the target location,
  12920. not actually move anything.
  12921. With a double prefix `C-c C-c', go to the location where the last refiling
  12922. operation has put the subtree.
  12923. With a double prefix argument, the command can be used to jump to any
  12924. heading in the current buffer."
  12925. (interactive "P")
  12926. (let* ((cbuf (current-buffer))
  12927. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12928. pos it nbuf file re level reversed)
  12929. (if (equal goto '(16))
  12930. (org-refile-goto-last-stored)
  12931. (when (setq it (org-refile-get-location
  12932. (if goto "Goto: " "Refile to: ") default-buffer))
  12933. (setq file (nth 1 it)
  12934. re (nth 2 it)
  12935. pos (nth 3 it))
  12936. (setq nbuf (or (find-buffer-visiting file)
  12937. (find-file-noselect file)))
  12938. (if goto
  12939. (progn
  12940. (switch-to-buffer nbuf)
  12941. (goto-char pos)
  12942. (org-show-context 'org-goto))
  12943. (org-copy-special)
  12944. (save-excursion
  12945. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12946. (find-file-noselect file))))
  12947. (setq reversed (org-notes-order-reversed-p))
  12948. (save-excursion
  12949. (save-restriction
  12950. (widen)
  12951. (goto-char pos)
  12952. (looking-at outline-regexp)
  12953. (setq level (org-get-legal-level (funcall outline-level) 1))
  12954. (goto-char
  12955. (if reversed
  12956. (outline-next-heading)
  12957. (or (save-excursion (outline-get-next-sibling))
  12958. (org-end-of-subtree t t)
  12959. (point-max))))
  12960. (bookmark-set "org-refile-last-stored")
  12961. (org-paste-subtree level))))
  12962. (org-cut-special)
  12963. (message "Entry refiled to \"%s\"" (car it)))))))
  12964. (defun org-refile-goto-last-stored ()
  12965. "Go to the location where the last refile was stored."
  12966. (interactive)
  12967. (bookmark-jump "org-refile-last-stored")
  12968. (message "This is the location of the last refile"))
  12969. (defun org-refile-get-location (&optional prompt default-buffer)
  12970. "Prompt the user for a refile location, using PROMPT."
  12971. (let ((org-refile-targets org-refile-targets)
  12972. (org-refile-use-outline-path org-refile-use-outline-path))
  12973. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12974. (unless org-refile-target-table
  12975. (error "No refile targets"))
  12976. (let* ((cbuf (current-buffer))
  12977. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12978. (fname (and filename (file-truename filename)))
  12979. (tbl (mapcar
  12980. (lambda (x)
  12981. (if (not (equal fname (file-truename (nth 1 x))))
  12982. (cons (concat (car x) " (" (file-name-nondirectory
  12983. (nth 1 x)) ")")
  12984. (cdr x))
  12985. x))
  12986. org-refile-target-table))
  12987. (completion-ignore-case t))
  12988. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12989. tbl)))
  12990. ;;;; Dynamic blocks
  12991. (defun org-find-dblock (name)
  12992. "Find the first dynamic block with name NAME in the buffer.
  12993. If not found, stay at current position and return nil."
  12994. (let (pos)
  12995. (save-excursion
  12996. (goto-char (point-min))
  12997. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12998. nil t)
  12999. (match-beginning 0))))
  13000. (if pos (goto-char pos))
  13001. pos))
  13002. (defconst org-dblock-start-re
  13003. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13004. "Matches the startline of a dynamic block, with parameters.")
  13005. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13006. "Matches the end of a dyhamic block.")
  13007. (defun org-create-dblock (plist)
  13008. "Create a dynamic block section, with parameters taken from PLIST.
  13009. PLIST must containe a :name entry which is used as name of the block."
  13010. (unless (bolp) (newline))
  13011. (let ((name (plist-get plist :name)))
  13012. (insert "#+BEGIN: " name)
  13013. (while plist
  13014. (if (eq (car plist) :name)
  13015. (setq plist (cddr plist))
  13016. (insert " " (prin1-to-string (pop plist)))))
  13017. (insert "\n\n#+END:\n")
  13018. (beginning-of-line -2)))
  13019. (defun org-prepare-dblock ()
  13020. "Prepare dynamic block for refresh.
  13021. This empties the block, puts the cursor at the insert position and returns
  13022. the property list including an extra property :name with the block name."
  13023. (unless (looking-at org-dblock-start-re)
  13024. (error "Not at a dynamic block"))
  13025. (let* ((begdel (1+ (match-end 0)))
  13026. (name (org-no-properties (match-string 1)))
  13027. (params (append (list :name name)
  13028. (read (concat "(" (match-string 3) ")")))))
  13029. (unless (re-search-forward org-dblock-end-re nil t)
  13030. (error "Dynamic block not terminated"))
  13031. (delete-region begdel (match-beginning 0))
  13032. (goto-char begdel)
  13033. (open-line 1)
  13034. params))
  13035. (defun org-map-dblocks (&optional command)
  13036. "Apply COMMAND to all dynamic blocks in the current buffer.
  13037. If COMMAND is not given, use `org-update-dblock'."
  13038. (let ((cmd (or command 'org-update-dblock))
  13039. pos)
  13040. (save-excursion
  13041. (goto-char (point-min))
  13042. (while (re-search-forward org-dblock-start-re nil t)
  13043. (goto-char (setq pos (match-beginning 0)))
  13044. (condition-case nil
  13045. (funcall cmd)
  13046. (error (message "Error during update of dynamic block")))
  13047. (goto-char pos)
  13048. (unless (re-search-forward org-dblock-end-re nil t)
  13049. (error "Dynamic block not terminated"))))))
  13050. (defun org-dblock-update (&optional arg)
  13051. "User command for updating dynamic blocks.
  13052. Update the dynamic block at point. With prefix ARG, update all dynamic
  13053. blocks in the buffer."
  13054. (interactive "P")
  13055. (if arg
  13056. (org-update-all-dblocks)
  13057. (or (looking-at org-dblock-start-re)
  13058. (org-beginning-of-dblock))
  13059. (org-update-dblock)))
  13060. (defun org-update-dblock ()
  13061. "Update the dynamic block at point
  13062. This means to empty the block, parse for parameters and then call
  13063. the correct writing function."
  13064. (save-window-excursion
  13065. (let* ((pos (point))
  13066. (line (org-current-line))
  13067. (params (org-prepare-dblock))
  13068. (name (plist-get params :name))
  13069. (cmd (intern (concat "org-dblock-write:" name))))
  13070. (message "Updating dynamic block `%s' at line %d..." name line)
  13071. (funcall cmd params)
  13072. (message "Updating dynamic block `%s' at line %d...done" name line)
  13073. (goto-char pos))))
  13074. (defun org-beginning-of-dblock ()
  13075. "Find the beginning of the dynamic block at point.
  13076. Error if there is no scuh block at point."
  13077. (let ((pos (point))
  13078. beg)
  13079. (end-of-line 1)
  13080. (if (and (re-search-backward org-dblock-start-re nil t)
  13081. (setq beg (match-beginning 0))
  13082. (re-search-forward org-dblock-end-re nil t)
  13083. (> (match-end 0) pos))
  13084. (goto-char beg)
  13085. (goto-char pos)
  13086. (error "Not in a dynamic block"))))
  13087. (defun org-update-all-dblocks ()
  13088. "Update all dynamic blocks in the buffer.
  13089. This function can be used in a hook."
  13090. (when (org-mode-p)
  13091. (org-map-dblocks 'org-update-dblock)))
  13092. ;;;; Completion
  13093. (defconst org-additional-option-like-keywords
  13094. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13095. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13096. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13097. (defun org-complete (&optional arg)
  13098. "Perform completion on word at point.
  13099. At the beginning of a headline, this completes TODO keywords as given in
  13100. `org-todo-keywords'.
  13101. If the current word is preceded by a backslash, completes the TeX symbols
  13102. that are supported for HTML support.
  13103. If the current word is preceded by \"#+\", completes special words for
  13104. setting file options.
  13105. In the line after \"#+STARTUP:, complete valid keywords.\"
  13106. At all other locations, this simply calls the value of
  13107. `org-completion-fallback-command'."
  13108. (interactive "P")
  13109. (org-without-partial-completion
  13110. (catch 'exit
  13111. (let* ((end (point))
  13112. (beg1 (save-excursion
  13113. (skip-chars-backward (org-re "[:alnum:]_@"))
  13114. (point)))
  13115. (beg (save-excursion
  13116. (skip-chars-backward "a-zA-Z0-9_:$")
  13117. (point)))
  13118. (confirm (lambda (x) (stringp (car x))))
  13119. (searchhead (equal (char-before beg) ?*))
  13120. (tag (and (equal (char-before beg1) ?:)
  13121. (equal (char-after (point-at-bol)) ?*)))
  13122. (prop (and (equal (char-before beg1) ?:)
  13123. (not (equal (char-after (point-at-bol)) ?*))))
  13124. (texp (equal (char-before beg) ?\\))
  13125. (link (equal (char-before beg) ?\[))
  13126. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13127. beg)
  13128. "#+"))
  13129. (startup (string-match "^#\\+STARTUP:.*"
  13130. (buffer-substring (point-at-bol) (point))))
  13131. (completion-ignore-case opt)
  13132. (type nil)
  13133. (tbl nil)
  13134. (table (cond
  13135. (opt
  13136. (setq type :opt)
  13137. (append
  13138. (mapcar
  13139. (lambda (x)
  13140. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13141. (cons (match-string 2 x) (match-string 1 x)))
  13142. (org-split-string (org-get-current-options) "\n"))
  13143. (mapcar 'list org-additional-option-like-keywords)))
  13144. (startup
  13145. (setq type :startup)
  13146. org-startup-options)
  13147. (link (append org-link-abbrev-alist-local
  13148. org-link-abbrev-alist))
  13149. (texp
  13150. (setq type :tex)
  13151. org-html-entities)
  13152. ((string-match "\\`\\*+[ \t]+\\'"
  13153. (buffer-substring (point-at-bol) beg))
  13154. (setq type :todo)
  13155. (mapcar 'list org-todo-keywords-1))
  13156. (searchhead
  13157. (setq type :searchhead)
  13158. (save-excursion
  13159. (goto-char (point-min))
  13160. (while (re-search-forward org-todo-line-regexp nil t)
  13161. (push (list
  13162. (org-make-org-heading-search-string
  13163. (match-string 3) t))
  13164. tbl)))
  13165. tbl)
  13166. (tag (setq type :tag beg beg1)
  13167. (or org-tag-alist (org-get-buffer-tags)))
  13168. (prop (setq type :prop beg beg1)
  13169. (mapcar 'list (org-buffer-property-keys nil t t)))
  13170. (t (progn
  13171. (call-interactively org-completion-fallback-command)
  13172. (throw 'exit nil)))))
  13173. (pattern (buffer-substring-no-properties beg end))
  13174. (completion (try-completion pattern table confirm)))
  13175. (cond ((eq completion t)
  13176. (if (not (assoc (upcase pattern) table))
  13177. (message "Already complete")
  13178. (if (equal type :opt)
  13179. (insert (substring (cdr (assoc (upcase pattern) table))
  13180. (length pattern)))
  13181. (if (memq type '(:tag :prop)) (insert ":")))))
  13182. ((null completion)
  13183. (message "Can't find completion for \"%s\"" pattern)
  13184. (ding))
  13185. ((not (string= pattern completion))
  13186. (delete-region beg end)
  13187. (if (string-match " +$" completion)
  13188. (setq completion (replace-match "" t t completion)))
  13189. (insert completion)
  13190. (if (get-buffer-window "*Completions*")
  13191. (delete-window (get-buffer-window "*Completions*")))
  13192. (if (assoc completion table)
  13193. (if (eq type :todo) (insert " ")
  13194. (if (memq type '(:tag :prop)) (insert ":"))))
  13195. (if (and (equal type :opt) (assoc completion table))
  13196. (message "%s" (substitute-command-keys
  13197. "Press \\[org-complete] again to insert example settings"))))
  13198. (t
  13199. (message "Making completion list...")
  13200. (let ((list (sort (all-completions pattern table confirm)
  13201. 'string<)))
  13202. (with-output-to-temp-buffer "*Completions*"
  13203. (condition-case nil
  13204. ;; Protection needed for XEmacs and emacs 21
  13205. (display-completion-list list pattern)
  13206. (error (display-completion-list list)))))
  13207. (message "Making completion list...%s" "done")))))))
  13208. ;;;; TODO, DEADLINE, Comments
  13209. (defun org-toggle-comment ()
  13210. "Change the COMMENT state of an entry."
  13211. (interactive)
  13212. (save-excursion
  13213. (org-back-to-heading)
  13214. (let (case-fold-search)
  13215. (if (looking-at (concat outline-regexp
  13216. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13217. (replace-match "" t t nil 1)
  13218. (if (looking-at outline-regexp)
  13219. (progn
  13220. (goto-char (match-end 0))
  13221. (insert org-comment-string " ")))))))
  13222. (defvar org-last-todo-state-is-todo nil
  13223. "This is non-nil when the last TODO state change led to a TODO state.
  13224. If the last change removed the TODO tag or switched to DONE, then
  13225. this is nil.")
  13226. (defvar org-setting-tags nil) ; dynamically skiped
  13227. ;; FIXME: better place
  13228. (defun org-property-or-variable-value (var &optional inherit)
  13229. "Check if there is a property fixing the value of VAR.
  13230. If yes, return this value. If not, return the current value of the variable."
  13231. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13232. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13233. (read prop)
  13234. (symbol-value var))))
  13235. (defun org-parse-local-options (string var)
  13236. "Parse STRING for startup setting relevant for variable VAR."
  13237. (let ((rtn (symbol-value var))
  13238. e opts)
  13239. (save-match-data
  13240. (if (or (not string) (not (string-match "\\S-" string)))
  13241. rtn
  13242. (setq opts (delq nil (mapcar (lambda (x)
  13243. (setq e (assoc x org-startup-options))
  13244. (if (eq (nth 1 e) var) e nil))
  13245. (org-split-string string "[ \t]+"))))
  13246. (if (not opts)
  13247. rtn
  13248. (setq rtn nil)
  13249. (while (setq e (pop opts))
  13250. (if (not (nth 3 e))
  13251. (setq rtn (nth 2 e))
  13252. (if (not (listp rtn)) (setq rtn nil))
  13253. (push (nth 2 e) rtn)))
  13254. rtn)))))
  13255. (defvar org-blocker-hook nil
  13256. "Hook for functions that are allowed to block a state change.
  13257. Each function gets as its single argument a property list, see
  13258. `org-trigger-hook' for more information about this list.
  13259. If any of the functions in this hook returns nil, the state change
  13260. is blocked.")
  13261. (defvar org-trigger-hook nil
  13262. "Hook for functions that are triggered by a state change.
  13263. Each function gets as its single argument a property list with at least
  13264. the following elements:
  13265. (:type type-of-change :position pos-at-entry-start
  13266. :from old-state :to new-state)
  13267. Depending on the type, more properties may be present.
  13268. This mechanism is currently implemented for:
  13269. TODO state changes
  13270. ------------------
  13271. :type todo-state-change
  13272. :from previous state (keyword as a string), or nil
  13273. :to new state (keyword as a string), or nil")
  13274. (defun org-todo (&optional arg)
  13275. "Change the TODO state of an item.
  13276. The state of an item is given by a keyword at the start of the heading,
  13277. like
  13278. *** TODO Write paper
  13279. *** DONE Call mom
  13280. The different keywords are specified in the variable `org-todo-keywords'.
  13281. By default the available states are \"TODO\" and \"DONE\".
  13282. So for this example: when the item starts with TODO, it is changed to DONE.
  13283. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13284. DONE are present, add TODO at the beginning of the heading.
  13285. With C-u prefix arg, use completion to determine the new state.
  13286. With numeric prefix arg, switch to that state.
  13287. For calling through lisp, arg is also interpreted in the following way:
  13288. 'none -> empty state
  13289. \"\"(empty string) -> switch to empty state
  13290. 'done -> switch to DONE
  13291. 'nextset -> switch to the next set of keywords
  13292. 'previousset -> switch to the previous set of keywords
  13293. \"WAITING\" -> switch to the specified keyword, but only if it
  13294. really is a member of `org-todo-keywords'."
  13295. (interactive "P")
  13296. (save-excursion
  13297. (catch 'exit
  13298. (org-back-to-heading)
  13299. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13300. (or (looking-at (concat " +" org-todo-regexp " *"))
  13301. (looking-at " *"))
  13302. (let* ((match-data (match-data))
  13303. (startpos (point-at-bol))
  13304. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13305. (org-log-done org-log-done)
  13306. (org-log-repeat org-log-repeat)
  13307. (org-todo-log-states org-todo-log-states)
  13308. (this (match-string 1))
  13309. (hl-pos (match-beginning 0))
  13310. (head (org-get-todo-sequence-head this))
  13311. (ass (assoc head org-todo-kwd-alist))
  13312. (interpret (nth 1 ass))
  13313. (done-word (nth 3 ass))
  13314. (final-done-word (nth 4 ass))
  13315. (last-state (or this ""))
  13316. (completion-ignore-case t)
  13317. (member (member this org-todo-keywords-1))
  13318. (tail (cdr member))
  13319. (state (cond
  13320. ((and org-todo-key-trigger
  13321. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13322. (and (not arg) org-use-fast-todo-selection
  13323. (not (eq org-use-fast-todo-selection 'prefix)))))
  13324. ;; Use fast selection
  13325. (org-fast-todo-selection))
  13326. ((and (equal arg '(4))
  13327. (or (not org-use-fast-todo-selection)
  13328. (not org-todo-key-trigger)))
  13329. ;; Read a state with completion
  13330. (completing-read "State: " (mapcar (lambda(x) (list x))
  13331. org-todo-keywords-1)
  13332. nil t))
  13333. ((eq arg 'right)
  13334. (if this
  13335. (if tail (car tail) nil)
  13336. (car org-todo-keywords-1)))
  13337. ((eq arg 'left)
  13338. (if (equal member org-todo-keywords-1)
  13339. nil
  13340. (if this
  13341. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13342. org-todo-keywords-1)
  13343. (org-last org-todo-keywords-1))))
  13344. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13345. (setq arg nil))) ; hack to fall back to cycling
  13346. (arg
  13347. ;; user or caller requests a specific state
  13348. (cond
  13349. ((equal arg "") nil)
  13350. ((eq arg 'none) nil)
  13351. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13352. ((eq arg 'nextset)
  13353. (or (car (cdr (member head org-todo-heads)))
  13354. (car org-todo-heads)))
  13355. ((eq arg 'previousset)
  13356. (let ((org-todo-heads (reverse org-todo-heads)))
  13357. (or (car (cdr (member head org-todo-heads)))
  13358. (car org-todo-heads))))
  13359. ((car (member arg org-todo-keywords-1)))
  13360. ((nth (1- (prefix-numeric-value arg))
  13361. org-todo-keywords-1))))
  13362. ((null member) (or head (car org-todo-keywords-1)))
  13363. ((equal this final-done-word) nil) ;; -> make empty
  13364. ((null tail) nil) ;; -> first entry
  13365. ((eq interpret 'sequence)
  13366. (car tail))
  13367. ((memq interpret '(type priority))
  13368. (if (eq this-command last-command)
  13369. (car tail)
  13370. (if (> (length tail) 0)
  13371. (or done-word (car org-done-keywords))
  13372. nil)))
  13373. (t nil)))
  13374. (next (if state (concat " " state " ") " "))
  13375. (change-plist (list :type 'todo-state-change :from this :to state
  13376. :position startpos))
  13377. dolog now-done-p)
  13378. (when org-blocker-hook
  13379. (unless (save-excursion
  13380. (save-match-data
  13381. (run-hook-with-args-until-failure
  13382. 'org-blocker-hook change-plist)))
  13383. (if (interactive-p)
  13384. (error "TODO state change from %s to %s blocked" this state)
  13385. ;; fail silently
  13386. (message "TODO state change from %s to %s blocked" this state)
  13387. (throw 'exit nil))))
  13388. (store-match-data match-data)
  13389. (replace-match next t t)
  13390. (unless (pos-visible-in-window-p hl-pos)
  13391. (message "TODO state changed to %s" (org-trim next)))
  13392. (unless head
  13393. (setq head (org-get-todo-sequence-head state)
  13394. ass (assoc head org-todo-kwd-alist)
  13395. interpret (nth 1 ass)
  13396. done-word (nth 3 ass)
  13397. final-done-word (nth 4 ass)))
  13398. (when (memq arg '(nextset previousset))
  13399. (message "Keyword-Set %d/%d: %s"
  13400. (- (length org-todo-sets) -1
  13401. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13402. (length org-todo-sets)
  13403. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13404. (setq org-last-todo-state-is-todo
  13405. (not (member state org-done-keywords)))
  13406. (setq now-done-p (and (member state org-done-keywords)
  13407. (not (member this org-done-keywords))))
  13408. (and logging (org-local-logging logging))
  13409. (when (and (or org-todo-log-states org-log-done)
  13410. (not (memq arg '(nextset previousset))))
  13411. ;; we need to look at recording a time and note
  13412. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13413. (nth 2 (assoc this org-todo-log-states))))
  13414. (when (and state
  13415. (member state org-not-done-keywords)
  13416. (not (member this org-not-done-keywords)))
  13417. ;; This is now a todo state and was not one before
  13418. ;; If there was a CLOSED time stamp, get rid of it.
  13419. (org-add-planning-info nil nil 'closed))
  13420. (when (and now-done-p org-log-done)
  13421. ;; It is now done, and it was not done before
  13422. (org-add-planning-info 'closed (org-current-time))
  13423. (if (and (not dolog) (eq 'note org-log-done))
  13424. (org-add-log-maybe 'done state 'findpos 'note)))
  13425. (when (and state dolog)
  13426. ;; This is a non-nil state, and we need to log it
  13427. (org-add-log-maybe 'state state 'findpos dolog)))
  13428. ;; Fixup tag positioning
  13429. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13430. (run-hooks 'org-after-todo-state-change-hook)
  13431. (if (and arg (not (member state org-done-keywords)))
  13432. (setq head (org-get-todo-sequence-head state)))
  13433. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13434. ;; Do we need to trigger a repeat?
  13435. (when now-done-p (org-auto-repeat-maybe state))
  13436. ;; Fixup cursor location if close to the keyword
  13437. (if (and (outline-on-heading-p)
  13438. (not (bolp))
  13439. (save-excursion (beginning-of-line 1)
  13440. (looking-at org-todo-line-regexp))
  13441. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13442. (progn
  13443. (goto-char (or (match-end 2) (match-end 1)))
  13444. (just-one-space)))
  13445. (when org-trigger-hook
  13446. (save-excursion
  13447. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13448. (defun org-local-logging (value)
  13449. "Get logging settings from a property VALUE."
  13450. (let* (words w a)
  13451. ;; directly set the variables, they are already local.
  13452. (setq org-log-done nil
  13453. org-log-repeat nil
  13454. org-todo-log-states nil)
  13455. (setq words (org-split-string value))
  13456. (while (setq w (pop words))
  13457. (cond
  13458. ((setq a (assoc w org-startup-options))
  13459. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13460. (set (nth 1 a) (nth 2 a))))
  13461. ((setq a (org-extract-log-state-settings w))
  13462. (and (member (car a) org-todo-keywords-1)
  13463. (push a org-todo-log-states)))))))
  13464. (defun org-get-todo-sequence-head (kwd)
  13465. "Return the head of the TODO sequence to which KWD belongs.
  13466. If KWD is not set, check if there is a text property remembering the
  13467. right sequence."
  13468. (let (p)
  13469. (cond
  13470. ((not kwd)
  13471. (or (get-text-property (point-at-bol) 'org-todo-head)
  13472. (progn
  13473. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13474. nil (point-at-eol)))
  13475. (get-text-property p 'org-todo-head))))
  13476. ((not (member kwd org-todo-keywords-1))
  13477. (car org-todo-keywords-1))
  13478. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13479. (defun org-fast-todo-selection ()
  13480. "Fast TODO keyword selection with single keys.
  13481. Returns the new TODO keyword, or nil if no state change should occur."
  13482. (let* ((fulltable org-todo-key-alist)
  13483. (done-keywords org-done-keywords) ;; needed for the faces.
  13484. (maxlen (apply 'max (mapcar
  13485. (lambda (x)
  13486. (if (stringp (car x)) (string-width (car x)) 0))
  13487. fulltable)))
  13488. (expert nil)
  13489. (fwidth (+ maxlen 3 1 3))
  13490. (ncol (/ (- (window-width) 4) fwidth))
  13491. tg cnt e c tbl
  13492. groups ingroup)
  13493. (save-window-excursion
  13494. (if expert
  13495. (set-buffer (get-buffer-create " *Org todo*"))
  13496. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13497. (erase-buffer)
  13498. (org-set-local 'org-done-keywords done-keywords)
  13499. (setq tbl fulltable cnt 0)
  13500. (while (setq e (pop tbl))
  13501. (cond
  13502. ((equal e '(:startgroup))
  13503. (push '() groups) (setq ingroup t)
  13504. (when (not (= cnt 0))
  13505. (setq cnt 0)
  13506. (insert "\n"))
  13507. (insert "{ "))
  13508. ((equal e '(:endgroup))
  13509. (setq ingroup nil cnt 0)
  13510. (insert "}\n"))
  13511. (t
  13512. (setq tg (car e) c (cdr e))
  13513. (if ingroup (push tg (car groups)))
  13514. (setq tg (org-add-props tg nil 'face
  13515. (org-get-todo-face tg)))
  13516. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13517. (insert "[" c "] " tg (make-string
  13518. (- fwidth 4 (length tg)) ?\ ))
  13519. (when (= (setq cnt (1+ cnt)) ncol)
  13520. (insert "\n")
  13521. (if ingroup (insert " "))
  13522. (setq cnt 0)))))
  13523. (insert "\n")
  13524. (goto-char (point-min))
  13525. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13526. (fit-window-to-buffer))
  13527. (message "[a-z..]:Set [SPC]:clear")
  13528. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13529. (cond
  13530. ((or (= c ?\C-g)
  13531. (and (= c ?q) (not (rassoc c fulltable))))
  13532. (setq quit-flag t))
  13533. ((= c ?\ ) nil)
  13534. ((setq e (rassoc c fulltable) tg (car e))
  13535. tg)
  13536. (t (setq quit-flag t))))))
  13537. (defun org-get-repeat ()
  13538. "Check if tere is a deadline/schedule with repeater in this entry."
  13539. (save-match-data
  13540. (save-excursion
  13541. (org-back-to-heading t)
  13542. (if (re-search-forward
  13543. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13544. (match-string 1)))))
  13545. (defvar org-last-changed-timestamp)
  13546. (defvar org-log-post-message)
  13547. (defvar org-log-note-purpose)
  13548. (defun org-auto-repeat-maybe (done-word)
  13549. "Check if the current headline contains a repeated deadline/schedule.
  13550. If yes, set TODO state back to what it was and change the base date
  13551. of repeating deadline/scheduled time stamps to new date.
  13552. This function is run automatically after each state change to a DONE state."
  13553. ;; last-state is dynamically scoped into this function
  13554. (let* ((repeat (org-get-repeat))
  13555. (aa (assoc last-state org-todo-kwd-alist))
  13556. (interpret (nth 1 aa))
  13557. (head (nth 2 aa))
  13558. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13559. (msg "Entry repeats: ")
  13560. (org-log-done nil)
  13561. (org-todo-log-states nil)
  13562. re type n what ts)
  13563. (when repeat
  13564. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13565. (org-todo (if (eq interpret 'type) last-state head))
  13566. (when (and org-log-repeat
  13567. (or (not (memq 'org-add-log-note
  13568. (default-value 'post-command-hook)))
  13569. (eq org-log-note-purpose 'done)))
  13570. ;; Make sure a note is taken;
  13571. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13572. 'findpos org-log-repeat))
  13573. (org-back-to-heading t)
  13574. (org-add-planning-info nil nil 'closed)
  13575. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13576. org-deadline-time-regexp "\\)\\|\\("
  13577. org-ts-regexp "\\)"))
  13578. (while (re-search-forward
  13579. re (save-excursion (outline-next-heading) (point)) t)
  13580. (setq type (if (match-end 1) org-scheduled-string
  13581. (if (match-end 3) org-deadline-string "Plain:"))
  13582. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13583. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13584. (setq n (string-to-number (match-string 1 ts))
  13585. what (match-string 2 ts))
  13586. (if (equal what "w") (setq n (* n 7) what "d"))
  13587. (org-timestamp-change n (cdr (assoc what whata)))
  13588. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13589. (setq org-log-post-message msg)
  13590. (message "%s" msg))))
  13591. (defun org-show-todo-tree (arg)
  13592. "Make a compact tree which shows all headlines marked with TODO.
  13593. The tree will show the lines where the regexp matches, and all higher
  13594. headlines above the match.
  13595. With a \\[universal-argument] prefix, also show the DONE entries.
  13596. With a numeric prefix N, construct a sparse tree for the Nth element
  13597. of `org-todo-keywords-1'."
  13598. (interactive "P")
  13599. (let ((case-fold-search nil)
  13600. (kwd-re
  13601. (cond ((null arg) org-not-done-regexp)
  13602. ((equal arg '(4))
  13603. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13604. (mapcar 'list org-todo-keywords-1))))
  13605. (concat "\\("
  13606. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13607. "\\)\\>")))
  13608. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13609. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13610. org-todo-keywords-1)))
  13611. (t (error "Invalid prefix argument: %s" arg)))))
  13612. (message "%d TODO entries found"
  13613. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13614. (defun org-deadline (&optional remove)
  13615. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13616. With argument REMOVE, remove any deadline from the item."
  13617. (interactive "P")
  13618. (if remove
  13619. (progn
  13620. (org-remove-timestamp-with-keyword org-deadline-string)
  13621. (message "Item no longer has a deadline."))
  13622. (org-add-planning-info 'deadline nil 'closed)))
  13623. (defun org-schedule (&optional remove)
  13624. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13625. With argument REMOVE, remove any scheduling date from the item."
  13626. (interactive "P")
  13627. (if remove
  13628. (progn
  13629. (org-remove-timestamp-with-keyword org-scheduled-string)
  13630. (message "Item is no longer scheduled."))
  13631. (org-add-planning-info 'scheduled nil 'closed)))
  13632. (defun org-remove-timestamp-with-keyword (keyword)
  13633. "Remove all time stamps with KEYWORD in the current entry."
  13634. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13635. beg)
  13636. (save-excursion
  13637. (org-back-to-heading t)
  13638. (setq beg (point))
  13639. (org-end-of-subtree t t)
  13640. (while (re-search-backward re beg t)
  13641. (replace-match "")
  13642. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13643. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13644. (defun org-add-planning-info (what &optional time &rest remove)
  13645. "Insert new timestamp with keyword in the line directly after the headline.
  13646. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13647. If non is given, the user is prompted for a date.
  13648. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13649. be removed."
  13650. (interactive)
  13651. (let (org-time-was-given org-end-time-was-given)
  13652. (when what (setq time (or time (org-read-date nil 'to-time))))
  13653. (when (and org-insert-labeled-timestamps-at-point
  13654. (member what '(scheduled deadline)))
  13655. (insert
  13656. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13657. (org-insert-time-stamp time org-time-was-given
  13658. nil nil nil (list org-end-time-was-given))
  13659. (setq what nil))
  13660. (save-excursion
  13661. (save-restriction
  13662. (let (col list elt ts buffer-invisibility-spec)
  13663. (org-back-to-heading t)
  13664. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13665. (goto-char (match-end 1))
  13666. (setq col (current-column))
  13667. (goto-char (match-end 0))
  13668. (if (eobp) (insert "\n") (forward-char 1))
  13669. (if (and (not (looking-at outline-regexp))
  13670. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13671. "[^\r\n]*"))
  13672. (not (equal (match-string 1) org-clock-string)))
  13673. (narrow-to-region (match-beginning 0) (match-end 0))
  13674. (insert-before-markers "\n")
  13675. (backward-char 1)
  13676. (narrow-to-region (point) (point))
  13677. (indent-to-column col))
  13678. ;; Check if we have to remove something.
  13679. (setq list (cons what remove))
  13680. (while list
  13681. (setq elt (pop list))
  13682. (goto-char (point-min))
  13683. (when (or (and (eq elt 'scheduled)
  13684. (re-search-forward org-scheduled-time-regexp nil t))
  13685. (and (eq elt 'deadline)
  13686. (re-search-forward org-deadline-time-regexp nil t))
  13687. (and (eq elt 'closed)
  13688. (re-search-forward org-closed-time-regexp nil t)))
  13689. (replace-match "")
  13690. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13691. (if (looking-at " +") (replace-match ""))))
  13692. (goto-char (point-max))
  13693. (when what
  13694. (insert
  13695. (if (not (equal (char-before) ?\ )) " " "")
  13696. (cond ((eq what 'scheduled) org-scheduled-string)
  13697. ((eq what 'deadline) org-deadline-string)
  13698. ((eq what 'closed) org-closed-string))
  13699. " ")
  13700. (setq ts (org-insert-time-stamp
  13701. time
  13702. (or org-time-was-given
  13703. (and (eq what 'closed) org-log-done-with-time))
  13704. (eq what 'closed)
  13705. nil nil (list org-end-time-was-given)))
  13706. (end-of-line 1))
  13707. (goto-char (point-min))
  13708. (widen)
  13709. (if (looking-at "[ \t]+\r?\n")
  13710. (replace-match ""))
  13711. ts)))))
  13712. (defvar org-log-note-marker (make-marker))
  13713. (defvar org-log-note-purpose nil)
  13714. (defvar org-log-note-state nil)
  13715. (defvar org-log-note-how nil)
  13716. (defvar org-log-note-window-configuration nil)
  13717. (defvar org-log-note-return-to (make-marker))
  13718. (defvar org-log-post-message nil
  13719. "Message to be displayed after a log note has been stored.
  13720. The auto-repeater uses this.")
  13721. (defun org-add-log-maybe (&optional purpose state findpos how)
  13722. "Set up the post command hook to take a note.
  13723. If this is about to TODO state change, the new state is expected in STATE.
  13724. When FINDPOS is non-nil, find the correct position for the note in
  13725. the current entry. If not, assume that it can be inserted at point."
  13726. (save-excursion
  13727. (when findpos
  13728. (org-back-to-heading t)
  13729. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13730. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13731. "[^\r\n]*\\)?"))
  13732. (goto-char (match-end 0))
  13733. (unless org-log-states-order-reversed
  13734. (and (= (char-after) ?\n) (forward-char 1))
  13735. (org-skip-over-state-notes)
  13736. (skip-chars-backward " \t\n\r")))
  13737. (move-marker org-log-note-marker (point))
  13738. (setq org-log-note-purpose purpose
  13739. org-log-note-state state
  13740. org-log-note-how how)
  13741. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13742. (defun org-skip-over-state-notes ()
  13743. "Skip past the list of State notes in an entry."
  13744. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13745. (while (looking-at "[ \t]*- State")
  13746. (condition-case nil
  13747. (org-next-item)
  13748. (error (org-end-of-item)))))
  13749. (defun org-add-log-note (&optional purpose)
  13750. "Pop up a window for taking a note, and add this note later at point."
  13751. (remove-hook 'post-command-hook 'org-add-log-note)
  13752. (setq org-log-note-window-configuration (current-window-configuration))
  13753. (delete-other-windows)
  13754. (move-marker org-log-note-return-to (point))
  13755. (switch-to-buffer (marker-buffer org-log-note-marker))
  13756. (goto-char org-log-note-marker)
  13757. (org-switch-to-buffer-other-window "*Org Note*")
  13758. (erase-buffer)
  13759. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13760. (org-store-log-note)
  13761. (let ((org-inhibit-startup t)) (org-mode))
  13762. (insert (format "# Insert note for %s.
  13763. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13764. (cond
  13765. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13766. ((eq org-log-note-purpose 'done) "closed todo item")
  13767. ((eq org-log-note-purpose 'state)
  13768. (format "state change to \"%s\"" org-log-note-state))
  13769. (t (error "This should not happen")))))
  13770. (org-set-local 'org-finish-function 'org-store-log-note)))
  13771. (defun org-store-log-note ()
  13772. "Finish taking a log note, and insert it to where it belongs."
  13773. (let ((txt (buffer-string))
  13774. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13775. lines ind)
  13776. (kill-buffer (current-buffer))
  13777. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13778. (setq txt (replace-match "" t t txt)))
  13779. (if (string-match "\\s-+\\'" txt)
  13780. (setq txt (replace-match "" t t txt)))
  13781. (setq lines (org-split-string txt "\n"))
  13782. (when (and note (string-match "\\S-" note))
  13783. (setq note
  13784. (org-replace-escapes
  13785. note
  13786. (list (cons "%u" (user-login-name))
  13787. (cons "%U" user-full-name)
  13788. (cons "%t" (format-time-string
  13789. (org-time-stamp-format 'long 'inactive)
  13790. (current-time)))
  13791. (cons "%s" (if org-log-note-state
  13792. (concat "\"" org-log-note-state "\"")
  13793. "")))))
  13794. (if lines (setq note (concat note " \\\\")))
  13795. (push note lines))
  13796. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13797. (when lines
  13798. (save-excursion
  13799. (set-buffer (marker-buffer org-log-note-marker))
  13800. (save-excursion
  13801. (goto-char org-log-note-marker)
  13802. (move-marker org-log-note-marker nil)
  13803. (end-of-line 1)
  13804. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13805. (indent-relative nil)
  13806. (insert "- " (pop lines))
  13807. (org-indent-line-function)
  13808. (beginning-of-line 1)
  13809. (looking-at "[ \t]*")
  13810. (setq ind (concat (match-string 0) " "))
  13811. (end-of-line 1)
  13812. (while lines (insert "\n" ind (pop lines)))))))
  13813. (set-window-configuration org-log-note-window-configuration)
  13814. (with-current-buffer (marker-buffer org-log-note-return-to)
  13815. (goto-char org-log-note-return-to))
  13816. (move-marker org-log-note-return-to nil)
  13817. (and org-log-post-message (message "%s" org-log-post-message)))
  13818. ;; FIXME: what else would be useful?
  13819. ;; - priority
  13820. ;; - date
  13821. (defun org-sparse-tree (&optional arg)
  13822. "Create a sparse tree, prompt for the details.
  13823. This command can create sparse trees. You first need to select the type
  13824. of match used to create the tree:
  13825. t Show entries with a specific TODO keyword.
  13826. T Show entries selected by a tags match.
  13827. p Enter a property name and its value (both with completion on existing
  13828. names/values) and show entries with that property.
  13829. r Show entries matching a regular expression
  13830. d Show deadlines due within `org-deadline-warning-days'."
  13831. (interactive "P")
  13832. (let (ans kwd value)
  13833. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13834. (setq ans (read-char-exclusive))
  13835. (cond
  13836. ((equal ans ?d)
  13837. (call-interactively 'org-check-deadlines))
  13838. ((equal ans ?b)
  13839. (call-interactively 'org-check-before-date))
  13840. ((equal ans ?t)
  13841. (org-show-todo-tree '(4)))
  13842. ((equal ans ?T)
  13843. (call-interactively 'org-tags-sparse-tree))
  13844. ((member ans '(?p ?P))
  13845. (setq kwd (completing-read "Property: "
  13846. (mapcar 'list (org-buffer-property-keys))))
  13847. (setq value (completing-read "Value: "
  13848. (mapcar 'list (org-property-values kwd))))
  13849. (unless (string-match "\\`{.*}\\'" value)
  13850. (setq value (concat "\"" value "\"")))
  13851. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13852. ((member ans '(?r ?R ?/))
  13853. (call-interactively 'org-occur))
  13854. (t (error "No such sparse tree command \"%c\"" ans)))))
  13855. (defvar org-occur-highlights nil)
  13856. (make-variable-buffer-local 'org-occur-highlights)
  13857. (defun org-occur (regexp &optional keep-previous callback)
  13858. "Make a compact tree which shows all matches of REGEXP.
  13859. The tree will show the lines where the regexp matches, and all higher
  13860. headlines above the match. It will also show the heading after the match,
  13861. to make sure editing the matching entry is easy.
  13862. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13863. call to `org-occur' will be kept, to allow stacking of calls to this
  13864. command.
  13865. If CALLBACK is non-nil, it is a function which is called to confirm
  13866. that the match should indeed be shown."
  13867. (interactive "sRegexp: \nP")
  13868. (or keep-previous (org-remove-occur-highlights nil nil t))
  13869. (let ((cnt 0))
  13870. (save-excursion
  13871. (goto-char (point-min))
  13872. (if (or (not keep-previous) ; do not want to keep
  13873. (not org-occur-highlights)) ; no previous matches
  13874. ;; hide everything
  13875. (org-overview))
  13876. (while (re-search-forward regexp nil t)
  13877. (when (or (not callback)
  13878. (save-match-data (funcall callback)))
  13879. (setq cnt (1+ cnt))
  13880. (when org-highlight-sparse-tree-matches
  13881. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13882. (org-show-context 'occur-tree))))
  13883. (when org-remove-highlights-with-change
  13884. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13885. nil 'local))
  13886. (unless org-sparse-tree-open-archived-trees
  13887. (org-hide-archived-subtrees (point-min) (point-max)))
  13888. (run-hooks 'org-occur-hook)
  13889. (if (interactive-p)
  13890. (message "%d match(es) for regexp %s" cnt regexp))
  13891. cnt))
  13892. (defun org-show-context (&optional key)
  13893. "Make sure point and context and visible.
  13894. How much context is shown depends upon the variables
  13895. `org-show-hierarchy-above', `org-show-following-heading'. and
  13896. `org-show-siblings'."
  13897. (let ((heading-p (org-on-heading-p t))
  13898. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13899. (following-p (org-get-alist-option org-show-following-heading key))
  13900. (entry-p (org-get-alist-option org-show-entry-below key))
  13901. (siblings-p (org-get-alist-option org-show-siblings key)))
  13902. (catch 'exit
  13903. ;; Show heading or entry text
  13904. (if (and heading-p (not entry-p))
  13905. (org-flag-heading nil) ; only show the heading
  13906. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13907. (org-show-hidden-entry))) ; show entire entry
  13908. (when following-p
  13909. ;; Show next sibling, or heading below text
  13910. (save-excursion
  13911. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13912. (org-flag-heading nil))))
  13913. (when siblings-p (org-show-siblings))
  13914. (when hierarchy-p
  13915. ;; show all higher headings, possibly with siblings
  13916. (save-excursion
  13917. (while (and (condition-case nil
  13918. (progn (org-up-heading-all 1) t)
  13919. (error nil))
  13920. (not (bobp)))
  13921. (org-flag-heading nil)
  13922. (when siblings-p (org-show-siblings))))))))
  13923. (defun org-reveal (&optional siblings)
  13924. "Show current entry, hierarchy above it, and the following headline.
  13925. This can be used to show a consistent set of context around locations
  13926. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13927. not t for the search context.
  13928. With optional argument SIBLINGS, on each level of the hierarchy all
  13929. siblings are shown. This repairs the tree structure to what it would
  13930. look like when opened with hierarchical calls to `org-cycle'."
  13931. (interactive "P")
  13932. (let ((org-show-hierarchy-above t)
  13933. (org-show-following-heading t)
  13934. (org-show-siblings (if siblings t org-show-siblings)))
  13935. (org-show-context nil)))
  13936. (defun org-highlight-new-match (beg end)
  13937. "Highlight from BEG to END and mark the highlight is an occur headline."
  13938. (let ((ov (org-make-overlay beg end)))
  13939. (org-overlay-put ov 'face 'secondary-selection)
  13940. (push ov org-occur-highlights)))
  13941. (defun org-remove-occur-highlights (&optional beg end noremove)
  13942. "Remove the occur highlights from the buffer.
  13943. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13944. from the `before-change-functions' in the current buffer."
  13945. (interactive)
  13946. (unless org-inhibit-highlight-removal
  13947. (mapc 'org-delete-overlay org-occur-highlights)
  13948. (setq org-occur-highlights nil)
  13949. (unless noremove
  13950. (remove-hook 'before-change-functions
  13951. 'org-remove-occur-highlights 'local))))
  13952. ;;;; Priorities
  13953. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13954. "Regular expression matching the priority indicator.")
  13955. (defvar org-remove-priority-next-time nil)
  13956. (defun org-priority-up ()
  13957. "Increase the priority of the current item."
  13958. (interactive)
  13959. (org-priority 'up))
  13960. (defun org-priority-down ()
  13961. "Decrease the priority of the current item."
  13962. (interactive)
  13963. (org-priority 'down))
  13964. (defun org-priority (&optional action)
  13965. "Change the priority of an item by ARG.
  13966. ACTION can be `set', `up', `down', or a character."
  13967. (interactive)
  13968. (setq action (or action 'set))
  13969. (let (current new news have remove)
  13970. (save-excursion
  13971. (org-back-to-heading)
  13972. (if (looking-at org-priority-regexp)
  13973. (setq current (string-to-char (match-string 2))
  13974. have t)
  13975. (setq current org-default-priority))
  13976. (cond
  13977. ((or (eq action 'set) (integerp action))
  13978. (if (integerp action)
  13979. (setq new action)
  13980. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13981. (setq new (read-char-exclusive)))
  13982. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13983. (= (upcase org-lowest-priority) org-lowest-priority))
  13984. (setq new (upcase new)))
  13985. (cond ((equal new ?\ ) (setq remove t))
  13986. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13987. (error "Priority must be between `%c' and `%c'"
  13988. org-highest-priority org-lowest-priority))))
  13989. ((eq action 'up)
  13990. (if (and (not have) (eq last-command this-command))
  13991. (setq new org-lowest-priority)
  13992. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13993. org-default-priority (1- current)))))
  13994. ((eq action 'down)
  13995. (if (and (not have) (eq last-command this-command))
  13996. (setq new org-highest-priority)
  13997. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13998. org-default-priority (1+ current)))))
  13999. (t (error "Invalid action")))
  14000. (if (or (< (upcase new) org-highest-priority)
  14001. (> (upcase new) org-lowest-priority))
  14002. (setq remove t))
  14003. (setq news (format "%c" new))
  14004. (if have
  14005. (if remove
  14006. (replace-match "" t t nil 1)
  14007. (replace-match news t t nil 2))
  14008. (if remove
  14009. (error "No priority cookie found in line")
  14010. (looking-at org-todo-line-regexp)
  14011. (if (match-end 2)
  14012. (progn
  14013. (goto-char (match-end 2))
  14014. (insert " [#" news "]"))
  14015. (goto-char (match-beginning 3))
  14016. (insert "[#" news "] ")))))
  14017. (org-preserve-lc (org-set-tags nil 'align))
  14018. (if remove
  14019. (message "Priority removed")
  14020. (message "Priority of current item set to %s" news))))
  14021. (defun org-get-priority (s)
  14022. "Find priority cookie and return priority."
  14023. (save-match-data
  14024. (if (not (string-match org-priority-regexp s))
  14025. (* 1000 (- org-lowest-priority org-default-priority))
  14026. (* 1000 (- org-lowest-priority
  14027. (string-to-char (match-string 2 s)))))))
  14028. ;;;; Tags
  14029. (defun org-scan-tags (action matcher &optional todo-only)
  14030. "Scan headline tags with inheritance and produce output ACTION.
  14031. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14032. evaluated, testing if a given set of tags qualifies a headline for
  14033. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14034. are included in the output."
  14035. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14036. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14037. (org-re
  14038. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14039. (props (list 'face nil
  14040. 'done-face 'org-done
  14041. 'undone-face nil
  14042. 'mouse-face 'highlight
  14043. 'org-not-done-regexp org-not-done-regexp
  14044. 'org-todo-regexp org-todo-regexp
  14045. 'keymap org-agenda-keymap
  14046. 'help-echo
  14047. (format "mouse-2 or RET jump to org file %s"
  14048. (abbreviate-file-name
  14049. (or (buffer-file-name (buffer-base-buffer))
  14050. (buffer-name (buffer-base-buffer)))))))
  14051. (case-fold-search nil)
  14052. lspos
  14053. tags tags-list tags-alist (llast 0) rtn level category i txt
  14054. todo marker entry priority)
  14055. (save-excursion
  14056. (goto-char (point-min))
  14057. (when (eq action 'sparse-tree)
  14058. (org-overview)
  14059. (org-remove-occur-highlights))
  14060. (while (re-search-forward re nil t)
  14061. (catch :skip
  14062. (setq todo (if (match-end 1) (match-string 2))
  14063. tags (if (match-end 4) (match-string 4)))
  14064. (goto-char (setq lspos (1+ (match-beginning 0))))
  14065. (setq level (org-reduced-level (funcall outline-level))
  14066. category (org-get-category))
  14067. (setq i llast llast level)
  14068. ;; remove tag lists from same and sublevels
  14069. (while (>= i level)
  14070. (when (setq entry (assoc i tags-alist))
  14071. (setq tags-alist (delete entry tags-alist)))
  14072. (setq i (1- i)))
  14073. ;; add the nex tags
  14074. (when tags
  14075. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14076. tags-alist
  14077. (cons (cons level tags) tags-alist)))
  14078. ;; compile tags for current headline
  14079. (setq tags-list
  14080. (if org-use-tag-inheritance
  14081. (apply 'append (mapcar 'cdr tags-alist))
  14082. tags))
  14083. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14084. (eval matcher)
  14085. (or (not org-agenda-skip-archived-trees)
  14086. (not (member org-archive-tag tags-list))))
  14087. (and (eq action 'agenda) (org-agenda-skip))
  14088. ;; list this headline
  14089. (if (eq action 'sparse-tree)
  14090. (progn
  14091. (and org-highlight-sparse-tree-matches
  14092. (org-get-heading) (match-end 0)
  14093. (org-highlight-new-match
  14094. (match-beginning 0) (match-beginning 1)))
  14095. (org-show-context 'tags-tree))
  14096. (setq txt (org-format-agenda-item
  14097. ""
  14098. (concat
  14099. (if org-tags-match-list-sublevels
  14100. (make-string (1- level) ?.) "")
  14101. (org-get-heading))
  14102. category tags-list)
  14103. priority (org-get-priority txt))
  14104. (goto-char lspos)
  14105. (setq marker (org-agenda-new-marker))
  14106. (org-add-props txt props
  14107. 'org-marker marker 'org-hd-marker marker 'org-category category
  14108. 'priority priority 'type "tagsmatch")
  14109. (push txt rtn))
  14110. ;; if we are to skip sublevels, jump to end of subtree
  14111. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14112. (when (and (eq action 'sparse-tree)
  14113. (not org-sparse-tree-open-archived-trees))
  14114. (org-hide-archived-subtrees (point-min) (point-max)))
  14115. (nreverse rtn)))
  14116. (defvar todo-only) ;; dynamically scoped
  14117. (defun org-tags-sparse-tree (&optional todo-only match)
  14118. "Create a sparse tree according to tags string MATCH.
  14119. MATCH can contain positive and negative selection of tags, like
  14120. \"+WORK+URGENT-WITHBOSS\".
  14121. If optional argument TODO_ONLY is non-nil, only select lines that are
  14122. also TODO lines."
  14123. (interactive "P")
  14124. (org-prepare-agenda-buffers (list (current-buffer)))
  14125. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14126. (defvar org-cached-props nil)
  14127. (defun org-cached-entry-get (pom property)
  14128. (if (or (eq t org-use-property-inheritance)
  14129. (member property org-use-property-inheritance))
  14130. ;; Caching is not possible, check it directly
  14131. (org-entry-get pom property 'inherit)
  14132. ;; Get all properties, so that we can do complicated checks easily
  14133. (cdr (assoc property (or org-cached-props
  14134. (setq org-cached-props
  14135. (org-entry-properties pom)))))))
  14136. (defun org-global-tags-completion-table (&optional files)
  14137. "Return the list of all tags in all agenda buffer/files."
  14138. (save-excursion
  14139. (org-uniquify
  14140. (delq nil
  14141. (apply 'append
  14142. (mapcar
  14143. (lambda (file)
  14144. (set-buffer (find-file-noselect file))
  14145. (append (org-get-buffer-tags)
  14146. (mapcar (lambda (x) (if (stringp (car-safe x))
  14147. (list (car-safe x)) nil))
  14148. org-tag-alist)))
  14149. (if (and files (car files))
  14150. files
  14151. (org-agenda-files))))))))
  14152. (defun org-make-tags-matcher (match)
  14153. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14154. ;; todo-only is scoped dynamically into this function, and the function
  14155. ;; may change it it the matcher asksk for it.
  14156. (unless match
  14157. ;; Get a new match request, with completion
  14158. (let ((org-last-tags-completion-table
  14159. (org-global-tags-completion-table)))
  14160. (setq match (completing-read
  14161. "Match: " 'org-tags-completion-function nil nil nil
  14162. 'org-tags-history))))
  14163. ;; Parse the string and create a lisp form
  14164. (let ((match0 match)
  14165. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14166. minus tag mm
  14167. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14168. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14169. (if (string-match "/+" match)
  14170. ;; match contains also a todo-matching request
  14171. (progn
  14172. (setq tagsmatch (substring match 0 (match-beginning 0))
  14173. todomatch (substring match (match-end 0)))
  14174. (if (string-match "^!" todomatch)
  14175. (setq todo-only t todomatch (substring todomatch 1)))
  14176. (if (string-match "^\\s-*$" todomatch)
  14177. (setq todomatch nil)))
  14178. ;; only matching tags
  14179. (setq tagsmatch match todomatch nil))
  14180. ;; Make the tags matcher
  14181. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14182. (setq tagsmatcher t)
  14183. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14184. (while (setq term (pop orterms))
  14185. (while (and (equal (substring term -1) "\\") orterms)
  14186. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14187. (while (string-match re term)
  14188. (setq minus (and (match-end 1)
  14189. (equal (match-string 1 term) "-"))
  14190. tag (match-string 2 term)
  14191. re-p (equal (string-to-char tag) ?{)
  14192. level-p (match-end 3)
  14193. prop-p (match-end 4)
  14194. mm (cond
  14195. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14196. (level-p `(= level ,(string-to-number
  14197. (match-string 3 term))))
  14198. (prop-p
  14199. (setq pn (match-string 4 term)
  14200. pv (match-string 5 term)
  14201. cat-p (equal pn "CATEGORY")
  14202. re-p (equal (string-to-char pv) ?{)
  14203. pv (substring pv 1 -1))
  14204. (if (equal pn "CATEGORY")
  14205. (setq gv '(get-text-property (point) 'org-category))
  14206. (setq gv `(org-cached-entry-get nil ,pn)))
  14207. (if re-p
  14208. `(string-match ,pv (or ,gv ""))
  14209. `(equal ,pv (or ,gv ""))))
  14210. (t `(member ,(downcase tag) tags-list)))
  14211. mm (if minus (list 'not mm) mm)
  14212. term (substring term (match-end 0)))
  14213. (push mm tagsmatcher))
  14214. (push (if (> (length tagsmatcher) 1)
  14215. (cons 'and tagsmatcher)
  14216. (car tagsmatcher))
  14217. orlist)
  14218. (setq tagsmatcher nil))
  14219. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14220. (setq tagsmatcher
  14221. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14222. ;; Make the todo matcher
  14223. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14224. (setq todomatcher t)
  14225. (setq orterms (org-split-string todomatch "|") orlist nil)
  14226. (while (setq term (pop orterms))
  14227. (while (string-match re term)
  14228. (setq minus (and (match-end 1)
  14229. (equal (match-string 1 term) "-"))
  14230. kwd (match-string 2 term)
  14231. re-p (equal (string-to-char kwd) ?{)
  14232. term (substring term (match-end 0))
  14233. mm (if re-p
  14234. `(string-match ,(substring kwd 1 -1) todo)
  14235. (list 'equal 'todo kwd))
  14236. mm (if minus (list 'not mm) mm))
  14237. (push mm todomatcher))
  14238. (push (if (> (length todomatcher) 1)
  14239. (cons 'and todomatcher)
  14240. (car todomatcher))
  14241. orlist)
  14242. (setq todomatcher nil))
  14243. (setq todomatcher (if (> (length orlist) 1)
  14244. (cons 'or orlist) (car orlist))))
  14245. ;; Return the string and lisp forms of the matcher
  14246. (setq matcher (if todomatcher
  14247. (list 'and tagsmatcher todomatcher)
  14248. tagsmatcher))
  14249. (cons match0 matcher)))
  14250. (defun org-match-any-p (re list)
  14251. "Does re match any element of list?"
  14252. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14253. (delq nil list))
  14254. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14255. (defvar org-tags-overlay (org-make-overlay 1 1))
  14256. (org-detach-overlay org-tags-overlay)
  14257. (defun org-align-tags-here (to-col)
  14258. ;; Assumes that this is a headline
  14259. (let ((pos (point)) (col (current-column)) tags)
  14260. (beginning-of-line 1)
  14261. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14262. (< pos (match-beginning 2)))
  14263. (progn
  14264. (setq tags (match-string 2))
  14265. (goto-char (match-beginning 1))
  14266. (insert " ")
  14267. (delete-region (point) (1+ (match-end 0)))
  14268. (backward-char 1)
  14269. (move-to-column
  14270. (max (1+ (current-column))
  14271. (1+ col)
  14272. (if (> to-col 0)
  14273. to-col
  14274. (- (abs to-col) (length tags))))
  14275. t)
  14276. (insert tags)
  14277. (move-to-column (min (current-column) col) t))
  14278. (goto-char pos))))
  14279. (defun org-set-tags (&optional arg just-align)
  14280. "Set the tags for the current headline.
  14281. With prefix ARG, realign all tags in headings in the current buffer."
  14282. (interactive "P")
  14283. (let* ((re (concat "^" outline-regexp))
  14284. (current (org-get-tags-string))
  14285. (col (current-column))
  14286. (org-setting-tags t)
  14287. table current-tags inherited-tags ; computed below when needed
  14288. tags p0 c0 c1 rpl)
  14289. (if arg
  14290. (save-excursion
  14291. (goto-char (point-min))
  14292. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14293. (while (re-search-forward re nil t)
  14294. (org-set-tags nil t)
  14295. (end-of-line 1)))
  14296. (message "All tags realigned to column %d" org-tags-column))
  14297. (if just-align
  14298. (setq tags current)
  14299. ;; Get a new set of tags from the user
  14300. (save-excursion
  14301. (setq table (or org-tag-alist (org-get-buffer-tags))
  14302. org-last-tags-completion-table table
  14303. current-tags (org-split-string current ":")
  14304. inherited-tags (nreverse
  14305. (nthcdr (length current-tags)
  14306. (nreverse (org-get-tags-at))))
  14307. tags
  14308. (if (or (eq t org-use-fast-tag-selection)
  14309. (and org-use-fast-tag-selection
  14310. (delq nil (mapcar 'cdr table))))
  14311. (org-fast-tag-selection
  14312. current-tags inherited-tags table
  14313. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14314. (let ((org-add-colon-after-tag-completion t))
  14315. (org-trim
  14316. (org-without-partial-completion
  14317. (completing-read "Tags: " 'org-tags-completion-function
  14318. nil nil current 'org-tags-history)))))))
  14319. (while (string-match "[-+&]+" tags)
  14320. ;; No boolean logic, just a list
  14321. (setq tags (replace-match ":" t t tags))))
  14322. (if (string-match "\\`[\t ]*\\'" tags)
  14323. (setq tags "")
  14324. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14325. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14326. ;; Insert new tags at the correct column
  14327. (beginning-of-line 1)
  14328. (cond
  14329. ((and (equal current "") (equal tags "")))
  14330. ((re-search-forward
  14331. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14332. (point-at-eol) t)
  14333. (if (equal tags "")
  14334. (setq rpl "")
  14335. (goto-char (match-beginning 0))
  14336. (setq c0 (current-column) p0 (point)
  14337. c1 (max (1+ c0) (if (> org-tags-column 0)
  14338. org-tags-column
  14339. (- (- org-tags-column) (length tags))))
  14340. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14341. (replace-match rpl t t)
  14342. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14343. tags)
  14344. (t (error "Tags alignment failed")))
  14345. (move-to-column col)
  14346. (unless just-align
  14347. (run-hooks 'org-after-tags-change-hook)))))
  14348. (defun org-change-tag-in-region (beg end tag off)
  14349. "Add or remove TAG for each entry in the region.
  14350. This works in the agenda, and also in an org-mode buffer."
  14351. (interactive
  14352. (list (region-beginning) (region-end)
  14353. (let ((org-last-tags-completion-table
  14354. (if (org-mode-p)
  14355. (org-get-buffer-tags)
  14356. (org-global-tags-completion-table))))
  14357. (completing-read
  14358. "Tag: " 'org-tags-completion-function nil nil nil
  14359. 'org-tags-history))
  14360. (progn
  14361. (message "[s]et or [r]emove? ")
  14362. (equal (read-char-exclusive) ?r))))
  14363. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14364. (let ((agendap (equal major-mode 'org-agenda-mode))
  14365. l1 l2 m buf pos newhead (cnt 0))
  14366. (goto-char end)
  14367. (setq l2 (1- (org-current-line)))
  14368. (goto-char beg)
  14369. (setq l1 (org-current-line))
  14370. (loop for l from l1 to l2 do
  14371. (goto-line l)
  14372. (setq m (get-text-property (point) 'org-hd-marker))
  14373. (when (or (and (org-mode-p) (org-on-heading-p))
  14374. (and agendap m))
  14375. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14376. pos (if agendap m (point)))
  14377. (with-current-buffer buf
  14378. (save-excursion
  14379. (save-restriction
  14380. (goto-char pos)
  14381. (setq cnt (1+ cnt))
  14382. (org-toggle-tag tag (if off 'off 'on))
  14383. (setq newhead (org-get-heading)))))
  14384. (and agendap (org-agenda-change-all-lines newhead m))))
  14385. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14386. (defun org-tags-completion-function (string predicate &optional flag)
  14387. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14388. (confirm (lambda (x) (stringp (car x)))))
  14389. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14390. (setq s1 (match-string 1 string)
  14391. s2 (match-string 2 string))
  14392. (setq s1 "" s2 string))
  14393. (cond
  14394. ((eq flag nil)
  14395. ;; try completion
  14396. (setq rtn (try-completion s2 ctable confirm))
  14397. (if (stringp rtn)
  14398. (setq rtn
  14399. (concat s1 s2 (substring rtn (length s2))
  14400. (if (and org-add-colon-after-tag-completion
  14401. (assoc rtn ctable))
  14402. ":" ""))))
  14403. rtn)
  14404. ((eq flag t)
  14405. ;; all-completions
  14406. (all-completions s2 ctable confirm)
  14407. )
  14408. ((eq flag 'lambda)
  14409. ;; exact match?
  14410. (assoc s2 ctable)))
  14411. ))
  14412. (defun org-fast-tag-insert (kwd tags face &optional end)
  14413. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14414. (insert (format "%-12s" (concat kwd ":"))
  14415. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14416. (or end "")))
  14417. (defun org-fast-tag-show-exit (flag)
  14418. (save-excursion
  14419. (goto-line 3)
  14420. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14421. (replace-match ""))
  14422. (when flag
  14423. (end-of-line 1)
  14424. (move-to-column (- (window-width) 19) t)
  14425. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14426. (defun org-set-current-tags-overlay (current prefix)
  14427. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14428. (if (featurep 'xemacs)
  14429. (org-overlay-display org-tags-overlay (concat prefix s)
  14430. 'secondary-selection)
  14431. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14432. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14433. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14434. "Fast tag selection with single keys.
  14435. CURRENT is the current list of tags in the headline, INHERITED is the
  14436. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14437. possibly with grouping information. TODO-TABLE is a similar table with
  14438. TODO keywords, should these have keys assigned to them.
  14439. If the keys are nil, a-z are automatically assigned.
  14440. Returns the new tags string, or nil to not change the current settings."
  14441. (let* ((fulltable (append table todo-table))
  14442. (maxlen (apply 'max (mapcar
  14443. (lambda (x)
  14444. (if (stringp (car x)) (string-width (car x)) 0))
  14445. fulltable)))
  14446. (buf (current-buffer))
  14447. (expert (eq org-fast-tag-selection-single-key 'expert))
  14448. (buffer-tags nil)
  14449. (fwidth (+ maxlen 3 1 3))
  14450. (ncol (/ (- (window-width) 4) fwidth))
  14451. (i-face 'org-done)
  14452. (c-face 'org-todo)
  14453. tg cnt e c char c1 c2 ntable tbl rtn
  14454. ov-start ov-end ov-prefix
  14455. (exit-after-next org-fast-tag-selection-single-key)
  14456. (done-keywords org-done-keywords)
  14457. groups ingroup)
  14458. (save-excursion
  14459. (beginning-of-line 1)
  14460. (if (looking-at
  14461. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14462. (setq ov-start (match-beginning 1)
  14463. ov-end (match-end 1)
  14464. ov-prefix "")
  14465. (setq ov-start (1- (point-at-eol))
  14466. ov-end (1+ ov-start))
  14467. (skip-chars-forward "^\n\r")
  14468. (setq ov-prefix
  14469. (concat
  14470. (buffer-substring (1- (point)) (point))
  14471. (if (> (current-column) org-tags-column)
  14472. " "
  14473. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14474. (org-move-overlay org-tags-overlay ov-start ov-end)
  14475. (save-window-excursion
  14476. (if expert
  14477. (set-buffer (get-buffer-create " *Org tags*"))
  14478. (delete-other-windows)
  14479. (split-window-vertically)
  14480. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14481. (erase-buffer)
  14482. (org-set-local 'org-done-keywords done-keywords)
  14483. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14484. (org-fast-tag-insert "Current" current c-face "\n\n")
  14485. (org-fast-tag-show-exit exit-after-next)
  14486. (org-set-current-tags-overlay current ov-prefix)
  14487. (setq tbl fulltable char ?a cnt 0)
  14488. (while (setq e (pop tbl))
  14489. (cond
  14490. ((equal e '(:startgroup))
  14491. (push '() groups) (setq ingroup t)
  14492. (when (not (= cnt 0))
  14493. (setq cnt 0)
  14494. (insert "\n"))
  14495. (insert "{ "))
  14496. ((equal e '(:endgroup))
  14497. (setq ingroup nil cnt 0)
  14498. (insert "}\n"))
  14499. (t
  14500. (setq tg (car e) c2 nil)
  14501. (if (cdr e)
  14502. (setq c (cdr e))
  14503. ;; automatically assign a character.
  14504. (setq c1 (string-to-char
  14505. (downcase (substring
  14506. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14507. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14508. (while (or (rassoc char ntable) (rassoc char table))
  14509. (setq char (1+ char)))
  14510. (setq c2 c1))
  14511. (setq c (or c2 char)))
  14512. (if ingroup (push tg (car groups)))
  14513. (setq tg (org-add-props tg nil 'face
  14514. (cond
  14515. ((not (assoc tg table))
  14516. (org-get-todo-face tg))
  14517. ((member tg current) c-face)
  14518. ((member tg inherited) i-face)
  14519. (t nil))))
  14520. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14521. (insert "[" c "] " tg (make-string
  14522. (- fwidth 4 (length tg)) ?\ ))
  14523. (push (cons tg c) ntable)
  14524. (when (= (setq cnt (1+ cnt)) ncol)
  14525. (insert "\n")
  14526. (if ingroup (insert " "))
  14527. (setq cnt 0)))))
  14528. (setq ntable (nreverse ntable))
  14529. (insert "\n")
  14530. (goto-char (point-min))
  14531. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14532. (fit-window-to-buffer))
  14533. (setq rtn
  14534. (catch 'exit
  14535. (while t
  14536. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14537. (if groups " [!] no groups" " [!]groups")
  14538. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14539. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14540. (cond
  14541. ((= c ?\r) (throw 'exit t))
  14542. ((= c ?!)
  14543. (setq groups (not groups))
  14544. (goto-char (point-min))
  14545. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14546. ((= c ?\C-c)
  14547. (if (not expert)
  14548. (org-fast-tag-show-exit
  14549. (setq exit-after-next (not exit-after-next)))
  14550. (setq expert nil)
  14551. (delete-other-windows)
  14552. (split-window-vertically)
  14553. (org-switch-to-buffer-other-window " *Org tags*")
  14554. (and (fboundp 'fit-window-to-buffer)
  14555. (fit-window-to-buffer))))
  14556. ((or (= c ?\C-g)
  14557. (and (= c ?q) (not (rassoc c ntable))))
  14558. (org-detach-overlay org-tags-overlay)
  14559. (setq quit-flag t))
  14560. ((= c ?\ )
  14561. (setq current nil)
  14562. (if exit-after-next (setq exit-after-next 'now)))
  14563. ((= c ?\t)
  14564. (condition-case nil
  14565. (setq tg (completing-read
  14566. "Tag: "
  14567. (or buffer-tags
  14568. (with-current-buffer buf
  14569. (org-get-buffer-tags)))))
  14570. (quit (setq tg "")))
  14571. (when (string-match "\\S-" tg)
  14572. (add-to-list 'buffer-tags (list tg))
  14573. (if (member tg current)
  14574. (setq current (delete tg current))
  14575. (push tg current)))
  14576. (if exit-after-next (setq exit-after-next 'now)))
  14577. ((setq e (rassoc c todo-table) tg (car e))
  14578. (with-current-buffer buf
  14579. (save-excursion (org-todo tg)))
  14580. (if exit-after-next (setq exit-after-next 'now)))
  14581. ((setq e (rassoc c ntable) tg (car e))
  14582. (if (member tg current)
  14583. (setq current (delete tg current))
  14584. (loop for g in groups do
  14585. (if (member tg g)
  14586. (mapc (lambda (x)
  14587. (setq current (delete x current)))
  14588. g)))
  14589. (push tg current))
  14590. (if exit-after-next (setq exit-after-next 'now))))
  14591. ;; Create a sorted list
  14592. (setq current
  14593. (sort current
  14594. (lambda (a b)
  14595. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14596. (if (eq exit-after-next 'now) (throw 'exit t))
  14597. (goto-char (point-min))
  14598. (beginning-of-line 2)
  14599. (delete-region (point) (point-at-eol))
  14600. (org-fast-tag-insert "Current" current c-face)
  14601. (org-set-current-tags-overlay current ov-prefix)
  14602. (while (re-search-forward
  14603. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14604. (setq tg (match-string 1))
  14605. (add-text-properties
  14606. (match-beginning 1) (match-end 1)
  14607. (list 'face
  14608. (cond
  14609. ((member tg current) c-face)
  14610. ((member tg inherited) i-face)
  14611. (t (get-text-property (match-beginning 1) 'face))))))
  14612. (goto-char (point-min)))))
  14613. (org-detach-overlay org-tags-overlay)
  14614. (if rtn
  14615. (mapconcat 'identity current ":")
  14616. nil))))
  14617. (defun org-get-tags-string ()
  14618. "Get the TAGS string in the current headline."
  14619. (unless (org-on-heading-p t)
  14620. (error "Not on a heading"))
  14621. (save-excursion
  14622. (beginning-of-line 1)
  14623. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14624. (org-match-string-no-properties 1)
  14625. "")))
  14626. (defun org-get-tags ()
  14627. "Get the list of tags specified in the current headline."
  14628. (org-split-string (org-get-tags-string) ":"))
  14629. (defun org-get-buffer-tags ()
  14630. "Get a table of all tags used in the buffer, for completion."
  14631. (let (tags)
  14632. (save-excursion
  14633. (goto-char (point-min))
  14634. (while (re-search-forward
  14635. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14636. (when (equal (char-after (point-at-bol 0)) ?*)
  14637. (mapc (lambda (x) (add-to-list 'tags x))
  14638. (org-split-string (org-match-string-no-properties 1) ":")))))
  14639. (mapcar 'list tags)))
  14640. ;;;; Properties
  14641. ;;; Setting and retrieving properties
  14642. (defconst org-special-properties
  14643. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14644. "TIMESTAMP" "TIMESTAMP_IA")
  14645. "The special properties valid in Org-mode.
  14646. These are properties that are not defined in the property drawer,
  14647. but in some other way.")
  14648. (defconst org-default-properties
  14649. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14650. "LOCATION" "LOGGING" "COLUMNS")
  14651. "Some properties that are used by Org-mode for various purposes.
  14652. Being in this list makes sure that they are offered for completion.")
  14653. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14654. "Regular expression matching the first line of a property drawer.")
  14655. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14656. "Regular expression matching the first line of a property drawer.")
  14657. (defun org-property-action ()
  14658. "Do an action on properties."
  14659. (interactive)
  14660. (let (c)
  14661. (org-at-property-p)
  14662. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14663. (setq c (read-char-exclusive))
  14664. (cond
  14665. ((equal c ?s)
  14666. (call-interactively 'org-set-property))
  14667. ((equal c ?d)
  14668. (call-interactively 'org-delete-property))
  14669. ((equal c ?D)
  14670. (call-interactively 'org-delete-property-globally))
  14671. ((equal c ?c)
  14672. (call-interactively 'org-compute-property-at-point))
  14673. (t (error "No such property action %c" c)))))
  14674. (defun org-at-property-p ()
  14675. "Is the cursor in a property line?"
  14676. ;; FIXME: Does not check if we are actually in the drawer.
  14677. ;; FIXME: also returns true on any drawers.....
  14678. ;; This is used by C-c C-c for property action.
  14679. (save-excursion
  14680. (beginning-of-line 1)
  14681. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14682. (defmacro org-with-point-at (pom &rest body)
  14683. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14684. (declare (indent 1) (debug t))
  14685. `(save-excursion
  14686. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14687. (save-excursion
  14688. (goto-char (or pom (point)))
  14689. ,@body)))
  14690. (defun org-get-property-block (&optional beg end force)
  14691. "Return the (beg . end) range of the body of the property drawer.
  14692. BEG and END can be beginning and end of subtree, if not given
  14693. they will be found.
  14694. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14695. (catch 'exit
  14696. (save-excursion
  14697. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14698. (end (or end (progn (outline-next-heading) (point)))))
  14699. (goto-char beg)
  14700. (if (re-search-forward org-property-start-re end t)
  14701. (setq beg (1+ (match-end 0)))
  14702. (if force
  14703. (save-excursion
  14704. (org-insert-property-drawer)
  14705. (setq end (progn (outline-next-heading) (point))))
  14706. (throw 'exit nil))
  14707. (goto-char beg)
  14708. (if (re-search-forward org-property-start-re end t)
  14709. (setq beg (1+ (match-end 0)))))
  14710. (if (re-search-forward org-property-end-re end t)
  14711. (setq end (match-beginning 0))
  14712. (or force (throw 'exit nil))
  14713. (goto-char beg)
  14714. (setq end beg)
  14715. (org-indent-line-function)
  14716. (insert ":END:\n"))
  14717. (cons beg end)))))
  14718. (defun org-entry-properties (&optional pom which)
  14719. "Get all properties of the entry at point-or-marker POM.
  14720. This includes the TODO keyword, the tags, time strings for deadline,
  14721. scheduled, and clocking, and any additional properties defined in the
  14722. entry. The return value is an alist, keys may occur multiple times
  14723. if the property key was used several times.
  14724. POM may also be nil, in which case the current entry is used.
  14725. If WHICH is nil or `all', get all properties. If WHICH is
  14726. `special' or `standard', only get that subclass."
  14727. (setq which (or which 'all))
  14728. (org-with-point-at pom
  14729. (let ((clockstr (substring org-clock-string 0 -1))
  14730. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14731. beg end range props sum-props key value string clocksum)
  14732. (save-excursion
  14733. (when (condition-case nil (org-back-to-heading t) (error nil))
  14734. (setq beg (point))
  14735. (setq sum-props (get-text-property (point) 'org-summaries))
  14736. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14737. (outline-next-heading)
  14738. (setq end (point))
  14739. (when (memq which '(all special))
  14740. ;; Get the special properties, like TODO and tags
  14741. (goto-char beg)
  14742. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14743. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14744. (when (looking-at org-priority-regexp)
  14745. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14746. (when (and (setq value (org-get-tags-string))
  14747. (string-match "\\S-" value))
  14748. (push (cons "TAGS" value) props))
  14749. (when (setq value (org-get-tags-at))
  14750. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14751. props))
  14752. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14753. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14754. string (if (equal key clockstr)
  14755. (org-no-properties
  14756. (org-trim
  14757. (buffer-substring
  14758. (match-beginning 3) (goto-char (point-at-eol)))))
  14759. (substring (org-match-string-no-properties 3) 1 -1)))
  14760. (unless key
  14761. (if (= (char-after (match-beginning 3)) ?\[)
  14762. (setq key "TIMESTAMP_IA")
  14763. (setq key "TIMESTAMP")))
  14764. (when (or (equal key clockstr) (not (assoc key props)))
  14765. (push (cons key string) props)))
  14766. )
  14767. (when (memq which '(all standard))
  14768. ;; Get the standard properties, like :PORP: ...
  14769. (setq range (org-get-property-block beg end))
  14770. (when range
  14771. (goto-char (car range))
  14772. (while (re-search-forward
  14773. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14774. (cdr range) t)
  14775. (setq key (org-match-string-no-properties 1)
  14776. value (org-trim (or (org-match-string-no-properties 2) "")))
  14777. (unless (member key excluded)
  14778. (push (cons key (or value "")) props)))))
  14779. (if clocksum
  14780. (push (cons "CLOCKSUM"
  14781. (org-column-number-to-string (/ (float clocksum) 60.)
  14782. 'add_times))
  14783. props))
  14784. (append sum-props (nreverse props)))))))
  14785. (defun org-entry-get (pom property &optional inherit)
  14786. "Get value of PROPERTY for entry at point-or-marker POM.
  14787. If INHERIT is non-nil and the entry does not have the property,
  14788. then also check higher levels of the hierarchy.
  14789. If the property is present but empty, the return value is the empty string.
  14790. If the property is not present at all, nil is returned."
  14791. (org-with-point-at pom
  14792. (if inherit
  14793. (org-entry-get-with-inheritance property)
  14794. (if (member property org-special-properties)
  14795. ;; We need a special property. Use brute force, get all properties.
  14796. (cdr (assoc property (org-entry-properties nil 'special)))
  14797. (let ((range (org-get-property-block)))
  14798. (if (and range
  14799. (goto-char (car range))
  14800. (re-search-forward
  14801. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14802. (cdr range) t))
  14803. ;; Found the property, return it.
  14804. (if (match-end 1)
  14805. (org-match-string-no-properties 1)
  14806. "")))))))
  14807. (defun org-entry-delete (pom property)
  14808. "Delete the property PROPERTY from entry at point-or-marker POM."
  14809. (org-with-point-at pom
  14810. (if (member property org-special-properties)
  14811. nil ; cannot delete these properties.
  14812. (let ((range (org-get-property-block)))
  14813. (if (and range
  14814. (goto-char (car range))
  14815. (re-search-forward
  14816. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14817. (cdr range) t))
  14818. (progn
  14819. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14820. t)
  14821. nil)))))
  14822. ;; Multi-values properties are properties that contain multiple values
  14823. ;; These values are assumed to be single words, separated by whitespace.
  14824. (defun org-entry-add-to-multivalued-property (pom property value)
  14825. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14826. (let* ((old (org-entry-get pom property))
  14827. (values (and old (org-split-string old "[ \t]"))))
  14828. (unless (member value values)
  14829. (setq values (cons value values))
  14830. (org-entry-put pom property
  14831. (mapconcat 'identity values " ")))))
  14832. (defun org-entry-remove-from-multivalued-property (pom property value)
  14833. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14834. (let* ((old (org-entry-get pom property))
  14835. (values (and old (org-split-string old "[ \t]"))))
  14836. (when (member value values)
  14837. (setq values (delete value values))
  14838. (org-entry-put pom property
  14839. (mapconcat 'identity values " ")))))
  14840. (defun org-entry-member-in-multivalued-property (pom property value)
  14841. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14842. (let* ((old (org-entry-get pom property))
  14843. (values (and old (org-split-string old "[ \t]"))))
  14844. (member value values)))
  14845. (defvar org-entry-property-inherited-from (make-marker))
  14846. (defun org-entry-get-with-inheritance (property)
  14847. "Get entry property, and search higher levels if not present."
  14848. (let (tmp)
  14849. (save-excursion
  14850. (save-restriction
  14851. (widen)
  14852. (catch 'ex
  14853. (while t
  14854. (when (setq tmp (org-entry-get nil property))
  14855. (org-back-to-heading t)
  14856. (move-marker org-entry-property-inherited-from (point))
  14857. (throw 'ex tmp))
  14858. (or (org-up-heading-safe) (throw 'ex nil)))))
  14859. (or tmp (cdr (assoc property org-local-properties))
  14860. (cdr (assoc property org-global-properties))))))
  14861. (defun org-entry-put (pom property value)
  14862. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14863. (org-with-point-at pom
  14864. (org-back-to-heading t)
  14865. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14866. range)
  14867. (cond
  14868. ((equal property "TODO")
  14869. (when (and (stringp value) (string-match "\\S-" value)
  14870. (not (member value org-todo-keywords-1)))
  14871. (error "\"%s\" is not a valid TODO state" value))
  14872. (if (or (not value)
  14873. (not (string-match "\\S-" value)))
  14874. (setq value 'none))
  14875. (org-todo value)
  14876. (org-set-tags nil 'align))
  14877. ((equal property "PRIORITY")
  14878. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14879. (string-to-char value) ?\ ))
  14880. (org-set-tags nil 'align))
  14881. ((equal property "SCHEDULED")
  14882. (if (re-search-forward org-scheduled-time-regexp end t)
  14883. (cond
  14884. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14885. ((eq value 'later) (org-timestamp-change 1 'day))
  14886. (t (call-interactively 'org-schedule)))
  14887. (call-interactively 'org-schedule)))
  14888. ((equal property "DEADLINE")
  14889. (if (re-search-forward org-deadline-time-regexp end t)
  14890. (cond
  14891. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14892. ((eq value 'later) (org-timestamp-change 1 'day))
  14893. (t (call-interactively 'org-deadline)))
  14894. (call-interactively 'org-deadline)))
  14895. ((member property org-special-properties)
  14896. (error "The %s property can not yet be set with `org-entry-put'"
  14897. property))
  14898. (t ; a non-special property
  14899. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14900. (setq range (org-get-property-block beg end 'force))
  14901. (goto-char (car range))
  14902. (if (re-search-forward
  14903. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14904. (progn
  14905. (delete-region (match-beginning 1) (match-end 1))
  14906. (goto-char (match-beginning 1)))
  14907. (goto-char (cdr range))
  14908. (insert "\n")
  14909. (backward-char 1)
  14910. (org-indent-line-function)
  14911. (insert ":" property ":"))
  14912. (and value (insert " " value))
  14913. (org-indent-line-function)))))))
  14914. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14915. "Get all property keys in the current buffer.
  14916. With INCLUDE-SPECIALS, also list the special properties that relect things
  14917. like tags and TODO state.
  14918. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14919. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14920. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14921. formats in the current buffer."
  14922. (let (rtn range cfmt cols s p)
  14923. (save-excursion
  14924. (save-restriction
  14925. (widen)
  14926. (goto-char (point-min))
  14927. (while (re-search-forward org-property-start-re nil t)
  14928. (setq range (org-get-property-block))
  14929. (goto-char (car range))
  14930. (while (re-search-forward
  14931. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14932. (cdr range) t)
  14933. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14934. (outline-next-heading))))
  14935. (when include-specials
  14936. (setq rtn (append org-special-properties rtn)))
  14937. (when include-defaults
  14938. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14939. (when include-columns
  14940. (save-excursion
  14941. (save-restriction
  14942. (widen)
  14943. (goto-char (point-min))
  14944. (while (re-search-forward
  14945. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14946. nil t)
  14947. (setq cfmt (match-string 2) s 0)
  14948. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14949. cfmt s)
  14950. (setq s (match-end 0)
  14951. p (match-string 1 cfmt))
  14952. (unless (or (equal p "ITEM")
  14953. (member p org-special-properties))
  14954. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14955. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14956. (defun org-property-values (key)
  14957. "Return a list of all values of property KEY."
  14958. (save-excursion
  14959. (save-restriction
  14960. (widen)
  14961. (goto-char (point-min))
  14962. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14963. values)
  14964. (while (re-search-forward re nil t)
  14965. (add-to-list 'values (org-trim (match-string 1))))
  14966. (delete "" values)))))
  14967. (defun org-insert-property-drawer ()
  14968. "Insert a property drawer into the current entry."
  14969. (interactive)
  14970. (org-back-to-heading t)
  14971. (looking-at outline-regexp)
  14972. (let ((indent (- (match-end 0)(match-beginning 0)))
  14973. (beg (point))
  14974. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14975. end hiddenp)
  14976. (outline-next-heading)
  14977. (setq end (point))
  14978. (goto-char beg)
  14979. (while (re-search-forward re end t))
  14980. (setq hiddenp (org-invisible-p))
  14981. (end-of-line 1)
  14982. (and (equal (char-after) ?\n) (forward-char 1))
  14983. (org-skip-over-state-notes)
  14984. (skip-chars-backward " \t\n\r")
  14985. (if (eq (char-before) ?*) (forward-char 1))
  14986. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14987. (beginning-of-line 0)
  14988. (indent-to-column indent)
  14989. (beginning-of-line 2)
  14990. (indent-to-column indent)
  14991. (beginning-of-line 0)
  14992. (if hiddenp
  14993. (save-excursion
  14994. (org-back-to-heading t)
  14995. (hide-entry))
  14996. (org-flag-drawer t))))
  14997. (defun org-set-property (property value)
  14998. "In the current entry, set PROPERTY to VALUE.
  14999. When called interactively, this will prompt for a property name, offering
  15000. completion on existing and default properties. And then it will prompt
  15001. for a value, offering competion either on allowed values (via an inherited
  15002. xxx_ALL property) or on existing values in other instances of this property
  15003. in the current file."
  15004. (interactive
  15005. (let* ((prop (completing-read
  15006. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15007. (cur (org-entry-get nil prop))
  15008. (allowed (org-property-get-allowed-values nil prop 'table))
  15009. (existing (mapcar 'list (org-property-values prop)))
  15010. (val (if allowed
  15011. (completing-read "Value: " allowed nil 'req-match)
  15012. (completing-read
  15013. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15014. (concat "[" cur "]") "")
  15015. ": ")
  15016. existing nil nil "" nil cur))))
  15017. (list prop (if (equal val "") cur val))))
  15018. (unless (equal (org-entry-get nil property) value)
  15019. (org-entry-put nil property value)))
  15020. (defun org-delete-property (property)
  15021. "In the current entry, delete PROPERTY."
  15022. (interactive
  15023. (let* ((prop (completing-read
  15024. "Property: " (org-entry-properties nil 'standard))))
  15025. (list prop)))
  15026. (message "Property %s %s" property
  15027. (if (org-entry-delete nil property)
  15028. "deleted"
  15029. "was not present in the entry")))
  15030. (defun org-delete-property-globally (property)
  15031. "Remove PROPERTY globally, from all entries."
  15032. (interactive
  15033. (let* ((prop (completing-read
  15034. "Globally remove property: "
  15035. (mapcar 'list (org-buffer-property-keys)))))
  15036. (list prop)))
  15037. (save-excursion
  15038. (save-restriction
  15039. (widen)
  15040. (goto-char (point-min))
  15041. (let ((cnt 0))
  15042. (while (re-search-forward
  15043. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15044. nil t)
  15045. (setq cnt (1+ cnt))
  15046. (replace-match ""))
  15047. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15048. (defvar org-columns-current-fmt-compiled) ; defined below
  15049. (defun org-compute-property-at-point ()
  15050. "Compute the property at point.
  15051. This looks for an enclosing column format, extracts the operator and
  15052. then applies it to the proerty in the column format's scope."
  15053. (interactive)
  15054. (unless (org-at-property-p)
  15055. (error "Not at a property"))
  15056. (let ((prop (org-match-string-no-properties 2)))
  15057. (org-columns-get-format-and-top-level)
  15058. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15059. (error "No operator defined for property %s" prop))
  15060. (org-columns-compute prop)))
  15061. (defun org-property-get-allowed-values (pom property &optional table)
  15062. "Get allowed values for the property PROPERTY.
  15063. When TABLE is non-nil, return an alist that can directly be used for
  15064. completion."
  15065. (let (vals)
  15066. (cond
  15067. ((equal property "TODO")
  15068. (setq vals (org-with-point-at pom
  15069. (append org-todo-keywords-1 '("")))))
  15070. ((equal property "PRIORITY")
  15071. (let ((n org-lowest-priority))
  15072. (while (>= n org-highest-priority)
  15073. (push (char-to-string n) vals)
  15074. (setq n (1- n)))))
  15075. ((member property org-special-properties))
  15076. (t
  15077. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15078. (when (and vals (string-match "\\S-" vals))
  15079. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15080. (setq vals (mapcar (lambda (x)
  15081. (cond ((stringp x) x)
  15082. ((numberp x) (number-to-string x))
  15083. ((symbolp x) (symbol-name x))
  15084. (t "???")))
  15085. vals)))))
  15086. (if table (mapcar 'list vals) vals)))
  15087. (defun org-property-previous-allowed-value (&optional previous)
  15088. "Switch to the next allowed value for this property."
  15089. (interactive)
  15090. (org-property-next-allowed-value t))
  15091. (defun org-property-next-allowed-value (&optional previous)
  15092. "Switch to the next allowed value for this property."
  15093. (interactive)
  15094. (unless (org-at-property-p)
  15095. (error "Not at a property"))
  15096. (let* ((key (match-string 2))
  15097. (value (match-string 3))
  15098. (allowed (or (org-property-get-allowed-values (point) key)
  15099. (and (member value '("[ ]" "[-]" "[X]"))
  15100. '("[ ]" "[X]"))))
  15101. nval)
  15102. (unless allowed
  15103. (error "Allowed values for this property have not been defined"))
  15104. (if previous (setq allowed (reverse allowed)))
  15105. (if (member value allowed)
  15106. (setq nval (car (cdr (member value allowed)))))
  15107. (setq nval (or nval (car allowed)))
  15108. (if (equal nval value)
  15109. (error "Only one allowed value for this property"))
  15110. (org-at-property-p)
  15111. (replace-match (concat " :" key ": " nval) t t)
  15112. (org-indent-line-function)
  15113. (beginning-of-line 1)
  15114. (skip-chars-forward " \t")))
  15115. (defun org-find-entry-with-id (ident)
  15116. "Locate the entry that contains the ID property with exact value IDENT.
  15117. IDENT can be a string, a symbol or a number, this function will search for
  15118. the string representation of it.
  15119. Return the position where this entry starts, or nil if there is no such entry."
  15120. (let ((id (cond
  15121. ((stringp ident) ident)
  15122. ((symbol-name ident) (symbol-name ident))
  15123. ((numberp ident) (number-to-string ident))
  15124. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15125. (case-fold-search nil))
  15126. (save-excursion
  15127. (save-restriction
  15128. (widen)
  15129. (goto-char (point-min))
  15130. (when (re-search-forward
  15131. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15132. nil t)
  15133. (org-back-to-heading)
  15134. (point))))))
  15135. ;;; Column View
  15136. (defvar org-columns-overlays nil
  15137. "Holds the list of current column overlays.")
  15138. (defvar org-columns-current-fmt nil
  15139. "Local variable, holds the currently active column format.")
  15140. (defvar org-columns-current-fmt-compiled nil
  15141. "Local variable, holds the currently active column format.
  15142. This is the compiled version of the format.")
  15143. (defvar org-columns-current-widths nil
  15144. "Loval variable, holds the currently widths of fields.")
  15145. (defvar org-columns-current-maxwidths nil
  15146. "Loval variable, holds the currently active maximum column widths.")
  15147. (defvar org-columns-begin-marker (make-marker)
  15148. "Points to the position where last a column creation command was called.")
  15149. (defvar org-columns-top-level-marker (make-marker)
  15150. "Points to the position where current columns region starts.")
  15151. (defvar org-columns-map (make-sparse-keymap)
  15152. "The keymap valid in column display.")
  15153. (defun org-columns-content ()
  15154. "Switch to contents view while in columns view."
  15155. (interactive)
  15156. (org-overview)
  15157. (org-content))
  15158. (org-defkey org-columns-map "c" 'org-columns-content)
  15159. (org-defkey org-columns-map "o" 'org-overview)
  15160. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15161. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15162. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15163. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15164. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15165. (org-defkey org-columns-map "q" 'org-columns-quit)
  15166. (org-defkey org-columns-map "r" 'org-columns-redo)
  15167. (org-defkey org-columns-map "g" 'org-columns-redo)
  15168. (org-defkey org-columns-map [left] 'backward-char)
  15169. (org-defkey org-columns-map "\M-b" 'backward-char)
  15170. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15171. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15172. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15173. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15174. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15175. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15176. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15177. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15178. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15179. (org-defkey org-columns-map ">" 'org-columns-widen)
  15180. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15181. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15182. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15183. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15184. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15185. '("Column"
  15186. ["Edit property" org-columns-edit-value t]
  15187. ["Next allowed value" org-columns-next-allowed-value t]
  15188. ["Previous allowed value" org-columns-previous-allowed-value t]
  15189. ["Show full value" org-columns-show-value t]
  15190. ["Edit allowed values" org-columns-edit-allowed t]
  15191. "--"
  15192. ["Edit column attributes" org-columns-edit-attributes t]
  15193. ["Increase column width" org-columns-widen t]
  15194. ["Decrease column width" org-columns-narrow t]
  15195. "--"
  15196. ["Move column right" org-columns-move-right t]
  15197. ["Move column left" org-columns-move-left t]
  15198. ["Add column" org-columns-new t]
  15199. ["Delete column" org-columns-delete t]
  15200. "--"
  15201. ["CONTENTS" org-columns-content t]
  15202. ["OVERVIEW" org-overview t]
  15203. ["Refresh columns display" org-columns-redo t]
  15204. "--"
  15205. ["Open link" org-columns-open-link t]
  15206. "--"
  15207. ["Quit" org-columns-quit t]))
  15208. (defun org-columns-new-overlay (beg end &optional string face)
  15209. "Create a new column overlay and add it to the list."
  15210. (let ((ov (org-make-overlay beg end)))
  15211. (org-overlay-put ov 'face (or face 'secondary-selection))
  15212. (org-overlay-display ov string face)
  15213. (push ov org-columns-overlays)
  15214. ov))
  15215. (defun org-columns-display-here (&optional props)
  15216. "Overlay the current line with column display."
  15217. (interactive)
  15218. (let* ((fmt org-columns-current-fmt-compiled)
  15219. (beg (point-at-bol))
  15220. (level-face (save-excursion
  15221. (beginning-of-line 1)
  15222. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15223. (org-get-level-face 2))))
  15224. (color (list :foreground
  15225. (face-attribute (or level-face 'default) :foreground)))
  15226. props pom property ass width f string ov column val modval)
  15227. ;; Check if the entry is in another buffer.
  15228. (unless props
  15229. (if (eq major-mode 'org-agenda-mode)
  15230. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15231. (get-text-property (point) 'org-marker))
  15232. props (if pom (org-entry-properties pom) nil))
  15233. (setq props (org-entry-properties nil))))
  15234. ;; Walk the format
  15235. (while (setq column (pop fmt))
  15236. (setq property (car column)
  15237. ass (if (equal property "ITEM")
  15238. (cons "ITEM"
  15239. (save-match-data
  15240. (org-no-properties
  15241. (org-remove-tabs
  15242. (buffer-substring-no-properties
  15243. (point-at-bol) (point-at-eol))))))
  15244. (assoc property props))
  15245. width (or (cdr (assoc property org-columns-current-maxwidths))
  15246. (nth 2 column)
  15247. (length property))
  15248. f (format "%%-%d.%ds | " width width)
  15249. val (or (cdr ass) "")
  15250. modval (if (equal property "ITEM")
  15251. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15252. string (format f (or modval val)))
  15253. ;; Create the overlay
  15254. (org-unmodified
  15255. (setq ov (org-columns-new-overlay
  15256. beg (setq beg (1+ beg)) string
  15257. (list color 'org-column)))
  15258. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15259. (org-overlay-put ov 'keymap org-columns-map)
  15260. (org-overlay-put ov 'org-columns-key property)
  15261. (org-overlay-put ov 'org-columns-value (cdr ass))
  15262. (org-overlay-put ov 'org-columns-value-modified modval)
  15263. (org-overlay-put ov 'org-columns-pom pom)
  15264. (org-overlay-put ov 'org-columns-format f))
  15265. (if (or (not (char-after beg))
  15266. (equal (char-after beg) ?\n))
  15267. (let ((inhibit-read-only t))
  15268. (save-excursion
  15269. (goto-char beg)
  15270. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15271. ;; Make the rest of the line disappear.
  15272. (org-unmodified
  15273. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15274. (org-overlay-put ov 'invisible t)
  15275. (org-overlay-put ov 'keymap org-columns-map)
  15276. (org-overlay-put ov 'intangible t)
  15277. (push ov org-columns-overlays)
  15278. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15279. (org-overlay-put ov 'keymap org-columns-map)
  15280. (push ov org-columns-overlays)
  15281. (let ((inhibit-read-only t))
  15282. (put-text-property (max (point-min) (1- (point-at-bol)))
  15283. (min (point-max) (1+ (point-at-eol)))
  15284. 'read-only "Type `e' to edit property")))))
  15285. (defvar org-previous-header-line-format nil
  15286. "The header line format before column view was turned on.")
  15287. (defvar org-columns-inhibit-recalculation nil
  15288. "Inhibit recomputing of columns on column view startup.")
  15289. (defvar header-line-format)
  15290. (defun org-columns-display-here-title ()
  15291. "Overlay the newline before the current line with the table title."
  15292. (interactive)
  15293. (let ((fmt org-columns-current-fmt-compiled)
  15294. string (title "")
  15295. property width f column str widths)
  15296. (while (setq column (pop fmt))
  15297. (setq property (car column)
  15298. str (or (nth 1 column) property)
  15299. width (or (cdr (assoc property org-columns-current-maxwidths))
  15300. (nth 2 column)
  15301. (length str))
  15302. widths (push width widths)
  15303. f (format "%%-%d.%ds | " width width)
  15304. string (format f str)
  15305. title (concat title string)))
  15306. (setq title (concat
  15307. (org-add-props " " nil 'display '(space :align-to 0))
  15308. (org-add-props title nil 'face '(:weight bold :underline t))))
  15309. (org-set-local 'org-previous-header-line-format header-line-format)
  15310. (org-set-local 'org-columns-current-widths (nreverse widths))
  15311. (setq header-line-format title)))
  15312. (defun org-columns-remove-overlays ()
  15313. "Remove all currently active column overlays."
  15314. (interactive)
  15315. (when (marker-buffer org-columns-begin-marker)
  15316. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15317. (when (local-variable-p 'org-previous-header-line-format)
  15318. (setq header-line-format org-previous-header-line-format)
  15319. (kill-local-variable 'org-previous-header-line-format))
  15320. (move-marker org-columns-begin-marker nil)
  15321. (move-marker org-columns-top-level-marker nil)
  15322. (org-unmodified
  15323. (mapc 'org-delete-overlay org-columns-overlays)
  15324. (setq org-columns-overlays nil)
  15325. (let ((inhibit-read-only t))
  15326. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15327. (defun org-columns-cleanup-item (item fmt)
  15328. "Remove from ITEM what is a column in the format FMT."
  15329. (if (not org-complex-heading-regexp)
  15330. item
  15331. (when (string-match org-complex-heading-regexp item)
  15332. (concat
  15333. (org-add-props (concat (match-string 1 item) " ") nil
  15334. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15335. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15336. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15337. " " (match-string 4 item)
  15338. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15339. (defun org-columns-show-value ()
  15340. "Show the full value of the property."
  15341. (interactive)
  15342. (let ((value (get-char-property (point) 'org-columns-value)))
  15343. (message "Value is: %s" (or value ""))))
  15344. (defun org-columns-quit ()
  15345. "Remove the column overlays and in this way exit column editing."
  15346. (interactive)
  15347. (org-unmodified
  15348. (org-columns-remove-overlays)
  15349. (let ((inhibit-read-only t))
  15350. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15351. (when (eq major-mode 'org-agenda-mode)
  15352. (message
  15353. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15354. (defun org-columns-check-computed ()
  15355. "Check if this column value is computed.
  15356. If yes, throw an error indicating that changing it does not make sense."
  15357. (let ((val (get-char-property (point) 'org-columns-value)))
  15358. (when (and (stringp val)
  15359. (get-char-property 0 'org-computed val))
  15360. (error "This value is computed from the entry's children"))))
  15361. (defun org-columns-todo (&optional arg)
  15362. "Change the TODO state during column view."
  15363. (interactive "P")
  15364. (org-columns-edit-value "TODO"))
  15365. (defun org-columns-set-tags-or-toggle (&optional arg)
  15366. "Toggle checkbox at point, or set tags for current headline."
  15367. (interactive "P")
  15368. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15369. (get-char-property (point) 'org-columns-value))
  15370. (org-columns-next-allowed-value)
  15371. (org-columns-edit-value "TAGS")))
  15372. (defun org-columns-edit-value (&optional key)
  15373. "Edit the value of the property at point in column view.
  15374. Where possible, use the standard interface for changing this line."
  15375. (interactive)
  15376. (org-columns-check-computed)
  15377. (let* ((external-key key)
  15378. (col (current-column))
  15379. (key (or key (get-char-property (point) 'org-columns-key)))
  15380. (value (get-char-property (point) 'org-columns-value))
  15381. (bol (point-at-bol)) (eol (point-at-eol))
  15382. (pom (or (get-text-property bol 'org-hd-marker)
  15383. (point))) ; keep despite of compiler waring
  15384. (line-overlays
  15385. (delq nil (mapcar (lambda (x)
  15386. (and (eq (overlay-buffer x) (current-buffer))
  15387. (>= (overlay-start x) bol)
  15388. (<= (overlay-start x) eol)
  15389. x))
  15390. org-columns-overlays)))
  15391. nval eval allowed)
  15392. (cond
  15393. ((equal key "CLOCKSUM")
  15394. (error "This special column cannot be edited"))
  15395. ((equal key "ITEM")
  15396. (setq eval '(org-with-point-at pom
  15397. (org-edit-headline))))
  15398. ((equal key "TODO")
  15399. (setq eval '(org-with-point-at pom
  15400. (let ((current-prefix-arg
  15401. (if external-key current-prefix-arg '(4))))
  15402. (call-interactively 'org-todo)))))
  15403. ((equal key "PRIORITY")
  15404. (setq eval '(org-with-point-at pom
  15405. (call-interactively 'org-priority))))
  15406. ((equal key "TAGS")
  15407. (setq eval '(org-with-point-at pom
  15408. (let ((org-fast-tag-selection-single-key
  15409. (if (eq org-fast-tag-selection-single-key 'expert)
  15410. t org-fast-tag-selection-single-key)))
  15411. (call-interactively 'org-set-tags)))))
  15412. ((equal key "DEADLINE")
  15413. (setq eval '(org-with-point-at pom
  15414. (call-interactively 'org-deadline))))
  15415. ((equal key "SCHEDULED")
  15416. (setq eval '(org-with-point-at pom
  15417. (call-interactively 'org-schedule))))
  15418. (t
  15419. (setq allowed (org-property-get-allowed-values pom key 'table))
  15420. (if allowed
  15421. (setq nval (completing-read "Value: " allowed nil t))
  15422. (setq nval (read-string "Edit: " value)))
  15423. (setq nval (org-trim nval))
  15424. (when (not (equal nval value))
  15425. (setq eval '(org-entry-put pom key nval)))))
  15426. (when eval
  15427. (let ((inhibit-read-only t))
  15428. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15429. (unwind-protect
  15430. (progn
  15431. (setq org-columns-overlays
  15432. (org-delete-all line-overlays org-columns-overlays))
  15433. (mapc 'org-delete-overlay line-overlays)
  15434. (org-columns-eval eval))
  15435. (org-columns-display-here))))
  15436. (move-to-column col)
  15437. (if (and (org-mode-p)
  15438. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15439. (org-columns-update key))))
  15440. (defun org-edit-headline () ; FIXME: this is not columns specific
  15441. "Edit the current headline, the part without TODO keyword, TAGS."
  15442. (org-back-to-heading)
  15443. (when (looking-at org-todo-line-regexp)
  15444. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15445. (txt (match-string 3))
  15446. (post "")
  15447. txt2)
  15448. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15449. (setq post (match-string 0 txt)
  15450. txt (substring txt 0 (match-beginning 0))))
  15451. (setq txt2 (read-string "Edit: " txt))
  15452. (when (not (equal txt txt2))
  15453. (beginning-of-line 1)
  15454. (insert pre txt2 post)
  15455. (delete-region (point) (point-at-eol))
  15456. (org-set-tags nil t)))))
  15457. (defun org-columns-edit-allowed ()
  15458. "Edit the list of allowed values for the current property."
  15459. (interactive)
  15460. (let* ((key (get-char-property (point) 'org-columns-key))
  15461. (key1 (concat key "_ALL"))
  15462. (allowed (org-entry-get (point) key1 t))
  15463. nval)
  15464. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15465. (setq nval (read-string "Allowed: " allowed))
  15466. (org-entry-put
  15467. (cond ((marker-position org-entry-property-inherited-from)
  15468. org-entry-property-inherited-from)
  15469. ((marker-position org-columns-top-level-marker)
  15470. org-columns-top-level-marker))
  15471. key1 nval)))
  15472. (defmacro org-no-warnings (&rest body)
  15473. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15474. (defun org-columns-eval (form)
  15475. (let (hidep)
  15476. (save-excursion
  15477. (beginning-of-line 1)
  15478. ;; `next-line' is needed here, because it skips invisible line.
  15479. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15480. (setq hidep (org-on-heading-p 1)))
  15481. (eval form)
  15482. (and hidep (hide-entry))))
  15483. (defun org-columns-previous-allowed-value ()
  15484. "Switch to the previous allowed value for this column."
  15485. (interactive)
  15486. (org-columns-next-allowed-value t))
  15487. (defun org-columns-next-allowed-value (&optional previous)
  15488. "Switch to the next allowed value for this column."
  15489. (interactive)
  15490. (org-columns-check-computed)
  15491. (let* ((col (current-column))
  15492. (key (get-char-property (point) 'org-columns-key))
  15493. (value (get-char-property (point) 'org-columns-value))
  15494. (bol (point-at-bol)) (eol (point-at-eol))
  15495. (pom (or (get-text-property bol 'org-hd-marker)
  15496. (point))) ; keep despite of compiler waring
  15497. (line-overlays
  15498. (delq nil (mapcar (lambda (x)
  15499. (and (eq (overlay-buffer x) (current-buffer))
  15500. (>= (overlay-start x) bol)
  15501. (<= (overlay-start x) eol)
  15502. x))
  15503. org-columns-overlays)))
  15504. (allowed (or (org-property-get-allowed-values pom key)
  15505. (and (memq
  15506. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15507. '(checkbox checkbox-n-of-m checkbox-percent))
  15508. '("[ ]" "[X]"))))
  15509. nval)
  15510. (when (equal key "ITEM")
  15511. (error "Cannot edit item headline from here"))
  15512. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15513. (error "Allowed values for this property have not been defined"))
  15514. (if (member key '("SCHEDULED" "DEADLINE"))
  15515. (setq nval (if previous 'earlier 'later))
  15516. (if previous (setq allowed (reverse allowed)))
  15517. (if (member value allowed)
  15518. (setq nval (car (cdr (member value allowed)))))
  15519. (setq nval (or nval (car allowed)))
  15520. (if (equal nval value)
  15521. (error "Only one allowed value for this property")))
  15522. (let ((inhibit-read-only t))
  15523. (remove-text-properties (1- bol) eol '(read-only t))
  15524. (unwind-protect
  15525. (progn
  15526. (setq org-columns-overlays
  15527. (org-delete-all line-overlays org-columns-overlays))
  15528. (mapc 'org-delete-overlay line-overlays)
  15529. (org-columns-eval '(org-entry-put pom key nval)))
  15530. (org-columns-display-here)))
  15531. (move-to-column col)
  15532. (if (and (org-mode-p)
  15533. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15534. (org-columns-update key))))
  15535. (defun org-verify-version (task)
  15536. (cond
  15537. ((eq task 'columns)
  15538. (if (or (featurep 'xemacs)
  15539. (< emacs-major-version 22))
  15540. (error "Emacs 22 is required for the columns feature")))))
  15541. (defun org-columns-open-link (&optional arg)
  15542. (interactive "P")
  15543. (let ((value (get-char-property (point) 'org-columns-value)))
  15544. (org-open-link-from-string value arg)))
  15545. (defun org-open-link-from-string (s &optional arg)
  15546. "Open a link in the string S, as if it was in Org-mode."
  15547. (interactive)
  15548. (with-temp-buffer
  15549. (let ((org-inhibit-startup t))
  15550. (org-mode)
  15551. (insert s)
  15552. (goto-char (point-min))
  15553. (org-open-at-point arg))))
  15554. (defun org-columns-get-format-and-top-level ()
  15555. (let (fmt)
  15556. (when (condition-case nil (org-back-to-heading) (error nil))
  15557. (move-marker org-entry-property-inherited-from nil)
  15558. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15559. (setq fmt (or fmt org-columns-default-format))
  15560. (org-set-local 'org-columns-current-fmt fmt)
  15561. (org-columns-compile-format fmt)
  15562. (if (marker-position org-entry-property-inherited-from)
  15563. (move-marker org-columns-top-level-marker
  15564. org-entry-property-inherited-from)
  15565. (move-marker org-columns-top-level-marker (point)))
  15566. fmt))
  15567. (defun org-columns ()
  15568. "Turn on column view on an org-mode file."
  15569. (interactive)
  15570. (org-verify-version 'columns)
  15571. (org-columns-remove-overlays)
  15572. (move-marker org-columns-begin-marker (point))
  15573. (let (beg end fmt cache maxwidths)
  15574. (setq fmt (org-columns-get-format-and-top-level))
  15575. (save-excursion
  15576. (goto-char org-columns-top-level-marker)
  15577. (setq beg (point))
  15578. (unless org-columns-inhibit-recalculation
  15579. (org-columns-compute-all))
  15580. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15581. (point-max)))
  15582. ;; Get and cache the properties
  15583. (goto-char beg)
  15584. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15585. (save-excursion
  15586. (save-restriction
  15587. (narrow-to-region beg end)
  15588. (org-clock-sum))))
  15589. (while (re-search-forward (concat "^" outline-regexp) end t)
  15590. (push (cons (org-current-line) (org-entry-properties)) cache))
  15591. (when cache
  15592. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15593. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15594. (org-columns-display-here-title)
  15595. (mapc (lambda (x)
  15596. (goto-line (car x))
  15597. (org-columns-display-here (cdr x)))
  15598. cache)))))
  15599. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15600. "Insert a new column, to the leeft o the current column."
  15601. (interactive)
  15602. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15603. cell)
  15604. (setq prop (completing-read
  15605. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15606. nil nil prop))
  15607. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15608. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15609. (if (string-match "\\S-" width)
  15610. (setq width (string-to-number width))
  15611. (setq width nil))
  15612. (setq fmt (completing-read "Summary [none]: "
  15613. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15614. nil t))
  15615. (if (string-match "\\S-" fmt)
  15616. (setq fmt (intern fmt))
  15617. (setq fmt nil))
  15618. (if (eq fmt 'none) (setq fmt nil))
  15619. (if editp
  15620. (progn
  15621. (setcar editp prop)
  15622. (setcdr editp (list title width nil fmt)))
  15623. (setq cell (nthcdr (1- (current-column))
  15624. org-columns-current-fmt-compiled))
  15625. (setcdr cell (cons (list prop title width nil fmt)
  15626. (cdr cell))))
  15627. (org-columns-store-format)
  15628. (org-columns-redo)))
  15629. (defun org-columns-delete ()
  15630. "Delete the column at point from columns view."
  15631. (interactive)
  15632. (let* ((n (current-column))
  15633. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15634. (when (y-or-n-p
  15635. (format "Are you sure you want to remove column \"%s\"? " title))
  15636. (setq org-columns-current-fmt-compiled
  15637. (delq (nth n org-columns-current-fmt-compiled)
  15638. org-columns-current-fmt-compiled))
  15639. (org-columns-store-format)
  15640. (org-columns-redo)
  15641. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15642. (backward-char 1)))))
  15643. (defun org-columns-edit-attributes ()
  15644. "Edit the attributes of the current column."
  15645. (interactive)
  15646. (let* ((n (current-column))
  15647. (info (nth n org-columns-current-fmt-compiled)))
  15648. (apply 'org-columns-new info)))
  15649. (defun org-columns-widen (arg)
  15650. "Make the column wider by ARG characters."
  15651. (interactive "p")
  15652. (let* ((n (current-column))
  15653. (entry (nth n org-columns-current-fmt-compiled))
  15654. (width (or (nth 2 entry)
  15655. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15656. (setq width (max 1 (+ width arg)))
  15657. (setcar (nthcdr 2 entry) width)
  15658. (org-columns-store-format)
  15659. (org-columns-redo)))
  15660. (defun org-columns-narrow (arg)
  15661. "Make the column nrrower by ARG characters."
  15662. (interactive "p")
  15663. (org-columns-widen (- arg)))
  15664. (defun org-columns-move-right ()
  15665. "Swap this column with the one to the right."
  15666. (interactive)
  15667. (let* ((n (current-column))
  15668. (cell (nthcdr n org-columns-current-fmt-compiled))
  15669. e)
  15670. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15671. (error "Cannot shift this column further to the right"))
  15672. (setq e (car cell))
  15673. (setcar cell (car (cdr cell)))
  15674. (setcdr cell (cons e (cdr (cdr cell))))
  15675. (org-columns-store-format)
  15676. (org-columns-redo)
  15677. (forward-char 1)))
  15678. (defun org-columns-move-left ()
  15679. "Swap this column with the one to the left."
  15680. (interactive)
  15681. (let* ((n (current-column)))
  15682. (when (= n 0)
  15683. (error "Cannot shift this column further to the left"))
  15684. (backward-char 1)
  15685. (org-columns-move-right)
  15686. (backward-char 1)))
  15687. (defun org-columns-store-format ()
  15688. "Store the text version of the current columns format in appropriate place.
  15689. This is either in the COLUMNS property of the node starting the current column
  15690. display, or in the #+COLUMNS line of the current buffer."
  15691. (let (fmt (cnt 0))
  15692. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15693. (org-set-local 'org-columns-current-fmt fmt)
  15694. (if (marker-position org-columns-top-level-marker)
  15695. (save-excursion
  15696. (goto-char org-columns-top-level-marker)
  15697. (if (and (org-at-heading-p)
  15698. (org-entry-get nil "COLUMNS"))
  15699. (org-entry-put nil "COLUMNS" fmt)
  15700. (goto-char (point-min))
  15701. ;; Overwrite all #+COLUMNS lines....
  15702. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15703. (setq cnt (1+ cnt))
  15704. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15705. (unless (> cnt 0)
  15706. (goto-char (point-min))
  15707. (or (org-on-heading-p t) (outline-next-heading))
  15708. (let ((inhibit-read-only t))
  15709. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15710. (org-set-local 'org-columns-default-format fmt))))))
  15711. (defvar org-overriding-columns-format nil
  15712. "When set, overrides any other definition.")
  15713. (defvar org-agenda-view-columns-initially nil
  15714. "When set, switch to columns view immediately after creating the agenda.")
  15715. (defun org-agenda-columns ()
  15716. "Turn on column view in the agenda."
  15717. (interactive)
  15718. (org-verify-version 'columns)
  15719. (org-columns-remove-overlays)
  15720. (move-marker org-columns-begin-marker (point))
  15721. (let (fmt cache maxwidths m)
  15722. (cond
  15723. ((and (local-variable-p 'org-overriding-columns-format)
  15724. org-overriding-columns-format)
  15725. (setq fmt org-overriding-columns-format))
  15726. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15727. (setq fmt (org-entry-get m "COLUMNS" t)))
  15728. ((and (boundp 'org-columns-current-fmt)
  15729. (local-variable-p 'org-columns-current-fmt)
  15730. org-columns-current-fmt)
  15731. (setq fmt org-columns-current-fmt))
  15732. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15733. (setq m (get-text-property m 'org-hd-marker))
  15734. (setq fmt (org-entry-get m "COLUMNS" t))))
  15735. (setq fmt (or fmt org-columns-default-format))
  15736. (org-set-local 'org-columns-current-fmt fmt)
  15737. (org-columns-compile-format fmt)
  15738. (save-excursion
  15739. ;; Get and cache the properties
  15740. (goto-char (point-min))
  15741. (while (not (eobp))
  15742. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15743. (get-text-property (point) 'org-marker)))
  15744. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15745. (beginning-of-line 2))
  15746. (when cache
  15747. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15748. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15749. (org-columns-display-here-title)
  15750. (mapc (lambda (x)
  15751. (goto-line (car x))
  15752. (org-columns-display-here (cdr x)))
  15753. cache)))))
  15754. (defun org-columns-get-autowidth-alist (s cache)
  15755. "Derive the maximum column widths from the format and the cache."
  15756. (let ((start 0) rtn)
  15757. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15758. (push (cons (match-string 1 s) 1) rtn)
  15759. (setq start (match-end 0)))
  15760. (mapc (lambda (x)
  15761. (setcdr x (apply 'max
  15762. (mapcar
  15763. (lambda (y)
  15764. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15765. cache))))
  15766. rtn)
  15767. rtn))
  15768. (defun org-columns-compute-all ()
  15769. "Compute all columns that have operators defined."
  15770. (org-unmodified
  15771. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15772. (let ((columns org-columns-current-fmt-compiled) col)
  15773. (while (setq col (pop columns))
  15774. (when (nth 3 col)
  15775. (save-excursion
  15776. (org-columns-compute (car col)))))))
  15777. (defun org-columns-update (property)
  15778. "Recompute PROPERTY, and update the columns display for it."
  15779. (org-columns-compute property)
  15780. (let (fmt val pos)
  15781. (save-excursion
  15782. (mapc (lambda (ov)
  15783. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15784. (setq pos (org-overlay-start ov))
  15785. (goto-char pos)
  15786. (when (setq val (cdr (assoc property
  15787. (get-text-property
  15788. (point-at-bol) 'org-summaries))))
  15789. (setq fmt (org-overlay-get ov 'org-columns-format))
  15790. (org-overlay-put ov 'org-columns-value val)
  15791. (org-overlay-put ov 'display (format fmt val)))))
  15792. org-columns-overlays))))
  15793. (defun org-columns-compute (property)
  15794. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15795. (interactive)
  15796. (let* ((re (concat "^" outline-regexp))
  15797. (lmax 30) ; Does anyone use deeper levels???
  15798. (lsum (make-vector lmax 0))
  15799. (lflag (make-vector lmax nil))
  15800. (level 0)
  15801. (ass (assoc property org-columns-current-fmt-compiled))
  15802. (format (nth 4 ass))
  15803. (printf (nth 5 ass))
  15804. (beg org-columns-top-level-marker)
  15805. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15806. (save-excursion
  15807. ;; Find the region to compute
  15808. (goto-char beg)
  15809. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15810. (goto-char end)
  15811. ;; Walk the tree from the back and do the computations
  15812. (while (re-search-backward re beg t)
  15813. (setq sumpos (match-beginning 0)
  15814. last-level level
  15815. level (org-outline-level)
  15816. val (org-entry-get nil property)
  15817. valflag (and val (string-match "\\S-" val)))
  15818. (cond
  15819. ((< level last-level)
  15820. ;; put the sum of lower levels here as a property
  15821. (setq sum (aref lsum last-level) ; current sum
  15822. flag (aref lflag last-level) ; any valid entries from children?
  15823. str (org-column-number-to-string sum format printf)
  15824. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15825. useval (if flag str1 (if valflag val ""))
  15826. sum-alist (get-text-property sumpos 'org-summaries))
  15827. (if (assoc property sum-alist)
  15828. (setcdr (assoc property sum-alist) useval)
  15829. (push (cons property useval) sum-alist)
  15830. (org-unmodified
  15831. (add-text-properties sumpos (1+ sumpos)
  15832. (list 'org-summaries sum-alist))))
  15833. (when val
  15834. (org-entry-put nil property (if flag str val)))
  15835. ;; add current to current level accumulator
  15836. (when (or flag valflag)
  15837. (aset lsum level (+ (aref lsum level)
  15838. (if flag sum (org-column-string-to-number
  15839. (if flag str val) format))))
  15840. (aset lflag level t))
  15841. ;; clear accumulators for deeper levels
  15842. (loop for l from (1+ level) to (1- lmax) do
  15843. (aset lsum l 0)
  15844. (aset lflag l nil)))
  15845. ((>= level last-level)
  15846. ;; add what we have here to the accumulator for this level
  15847. (aset lsum level (+ (aref lsum level)
  15848. (org-column-string-to-number (or val "0") format)))
  15849. (and valflag (aset lflag level t)))
  15850. (t (error "This should not happen")))))))
  15851. (defun org-columns-redo ()
  15852. "Construct the column display again."
  15853. (interactive)
  15854. (message "Recomputing columns...")
  15855. (save-excursion
  15856. (if (marker-position org-columns-begin-marker)
  15857. (goto-char org-columns-begin-marker))
  15858. (org-columns-remove-overlays)
  15859. (if (org-mode-p)
  15860. (call-interactively 'org-columns)
  15861. (call-interactively 'org-agenda-columns)))
  15862. (message "Recomputing columns...done"))
  15863. (defun org-columns-not-in-agenda ()
  15864. (if (eq major-mode 'org-agenda-mode)
  15865. (error "This command is only allowed in Org-mode buffers")))
  15866. (defun org-string-to-number (s)
  15867. "Convert string to number, and interpret hh:mm:ss."
  15868. (if (not (string-match ":" s))
  15869. (string-to-number s)
  15870. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15871. (while l
  15872. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15873. sum)))
  15874. (defun org-column-number-to-string (n fmt &optional printf)
  15875. "Convert a computed column number to a string value, according to FMT."
  15876. (cond
  15877. ((eq fmt 'add_times)
  15878. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15879. (format "%d:%02d" h m)))
  15880. ((eq fmt 'checkbox)
  15881. (cond ((= n (floor n)) "[X]")
  15882. ((> n 1.) "[-]")
  15883. (t "[ ]")))
  15884. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15885. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15886. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15887. (printf (format printf n))
  15888. ((eq fmt 'currency)
  15889. (format "%.2f" n))
  15890. (t (number-to-string n))))
  15891. (defun org-nofm-to-completion (n m &optional percent)
  15892. (if (not percent)
  15893. (format "[%d/%d]" n m)
  15894. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15895. (defun org-column-string-to-number (s fmt)
  15896. "Convert a column value to a number that can be used for column computing."
  15897. (cond
  15898. ((string-match ":" s)
  15899. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15900. (while l
  15901. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15902. sum))
  15903. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15904. (if (equal s "[X]") 1. 0.000001))
  15905. (t (string-to-number s))))
  15906. (defun org-columns-uncompile-format (cfmt)
  15907. "Turn the compiled columns format back into a string representation."
  15908. (let ((rtn "") e s prop title op width fmt printf)
  15909. (while (setq e (pop cfmt))
  15910. (setq prop (car e)
  15911. title (nth 1 e)
  15912. width (nth 2 e)
  15913. op (nth 3 e)
  15914. fmt (nth 4 e)
  15915. printf (nth 5 e))
  15916. (cond
  15917. ((eq fmt 'add_times) (setq op ":"))
  15918. ((eq fmt 'checkbox) (setq op "X"))
  15919. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15920. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15921. ((eq fmt 'add_numbers) (setq op "+"))
  15922. ((eq fmt 'currency) (setq op "$")))
  15923. (if (and op printf) (setq op (concat op ";" printf)))
  15924. (if (equal title prop) (setq title nil))
  15925. (setq s (concat "%" (if width (number-to-string width))
  15926. prop
  15927. (if title (concat "(" title ")"))
  15928. (if op (concat "{" op "}"))))
  15929. (setq rtn (concat rtn " " s)))
  15930. (org-trim rtn)))
  15931. (defun org-columns-compile-format (fmt)
  15932. "Turn a column format string into an alist of specifications.
  15933. The alist has one entry for each column in the format. The elements of
  15934. that list are:
  15935. property the property
  15936. title the title field for the columns
  15937. width the column width in characters, can be nil for automatic
  15938. operator the operator if any
  15939. format the output format for computed results, derived from operator
  15940. printf a printf format for computed values"
  15941. (let ((start 0) width prop title op f printf)
  15942. (setq org-columns-current-fmt-compiled nil)
  15943. (while (string-match
  15944. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15945. fmt start)
  15946. (setq start (match-end 0)
  15947. width (match-string 1 fmt)
  15948. prop (match-string 2 fmt)
  15949. title (or (match-string 3 fmt) prop)
  15950. op (match-string 4 fmt)
  15951. f nil
  15952. printf nil)
  15953. (if width (setq width (string-to-number width)))
  15954. (when (and op (string-match ";" op))
  15955. (setq printf (substring op (match-end 0))
  15956. op (substring op 0 (match-beginning 0))))
  15957. (cond
  15958. ((equal op "+") (setq f 'add_numbers))
  15959. ((equal op "$") (setq f 'currency))
  15960. ((equal op ":") (setq f 'add_times))
  15961. ((equal op "X") (setq f 'checkbox))
  15962. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15963. ((equal op "X%") (setq f 'checkbox-percent))
  15964. )
  15965. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15966. (setq org-columns-current-fmt-compiled
  15967. (nreverse org-columns-current-fmt-compiled))))
  15968. ;;; Dynamic block for Column view
  15969. (defun org-columns-capture-view ()
  15970. "Get the column view of the current buffer and return it as a list.
  15971. The list will contains the title row and all other rows. Each row is
  15972. a list of fields."
  15973. (save-excursion
  15974. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15975. (n (length title)) row tbl)
  15976. (goto-char (point-min))
  15977. (while (re-search-forward "^\\*+ " nil t)
  15978. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15979. (setq row nil)
  15980. (loop for i from 0 to (1- n) do
  15981. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15982. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15983. "")
  15984. row))
  15985. (setq row (nreverse row))
  15986. (push row tbl)))
  15987. (append (list title 'hline) (nreverse tbl)))))
  15988. (defun org-dblock-write:columnview (params)
  15989. "Write the column view table.
  15990. PARAMS is a property list of parameters:
  15991. :width enforce same column widths with <N> specifiers.
  15992. :id the :ID: property of the entry where the columns view
  15993. should be built, as a string. When `local', call locally.
  15994. When `global' call column view with the cursor at the beginning
  15995. of the buffer (usually this means that the whole buffer switches
  15996. to column view).
  15997. :hlines When t, insert a hline before each item. When a number, insert
  15998. a hline before each level <= that number.
  15999. :vlines When t, make each column a colgroup to enforce vertical lines."
  16000. (let ((pos (move-marker (make-marker) (point)))
  16001. (hlines (plist-get params :hlines))
  16002. (vlines (plist-get params :vlines))
  16003. tbl id idpos nfields tmp)
  16004. (save-excursion
  16005. (save-restriction
  16006. (when (setq id (plist-get params :id))
  16007. (cond ((not id) nil)
  16008. ((eq id 'global) (goto-char (point-min)))
  16009. ((eq id 'local) nil)
  16010. ((setq idpos (org-find-entry-with-id id))
  16011. (goto-char idpos))
  16012. (t (error "Cannot find entry with :ID: %s" id))))
  16013. (org-columns)
  16014. (setq tbl (org-columns-capture-view))
  16015. (setq nfields (length (car tbl)))
  16016. (org-columns-quit)))
  16017. (goto-char pos)
  16018. (move-marker pos nil)
  16019. (when tbl
  16020. (when (plist-get params :hlines)
  16021. (setq tmp nil)
  16022. (while tbl
  16023. (if (eq (car tbl) 'hline)
  16024. (push (pop tbl) tmp)
  16025. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16026. (if (and (not (eq (car tmp) 'hline))
  16027. (or (eq hlines t)
  16028. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16029. (push 'hline tmp)))
  16030. (push (pop tbl) tmp)))
  16031. (setq tbl (nreverse tmp)))
  16032. (when vlines
  16033. (setq tbl (mapcar (lambda (x)
  16034. (if (eq 'hline x) x (cons "" x)))
  16035. tbl))
  16036. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16037. (setq pos (point))
  16038. (insert (org-listtable-to-string tbl))
  16039. (when (plist-get params :width)
  16040. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16041. org-columns-current-widths "|")))
  16042. (goto-char pos)
  16043. (org-table-align))))
  16044. (defun org-listtable-to-string (tbl)
  16045. "Convert a listtable TBL to a string that contains the Org-mode table.
  16046. The table still need to be alligned. The resulting string has no leading
  16047. and tailing newline characters."
  16048. (mapconcat
  16049. (lambda (x)
  16050. (cond
  16051. ((listp x)
  16052. (concat "|" (mapconcat 'identity x "|") "|"))
  16053. ((eq x 'hline) "|-|")
  16054. (t (error "Garbage in listtable: %s" x))))
  16055. tbl "\n"))
  16056. (defun org-insert-columns-dblock ()
  16057. "Create a dynamic block capturing a column view table."
  16058. (interactive)
  16059. (let ((defaults '(:name "columnview" :hlines 1))
  16060. (id (completing-read
  16061. "Capture columns (local, global, entry with :ID: property) [local]: "
  16062. (append '(("global") ("local"))
  16063. (mapcar 'list (org-property-values "ID"))))))
  16064. (if (equal id "") (setq id 'local))
  16065. (if (equal id "global") (setq id 'global))
  16066. (setq defaults (append defaults (list :id id)))
  16067. (org-create-dblock defaults)
  16068. (org-update-dblock)))
  16069. ;;;; Timestamps
  16070. (defvar org-last-changed-timestamp nil)
  16071. (defvar org-time-was-given) ; dynamically scoped parameter
  16072. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16073. (defvar org-ts-what) ; dynamically scoped parameter
  16074. (defun org-time-stamp (arg)
  16075. "Prompt for a date/time and insert a time stamp.
  16076. If the user specifies a time like HH:MM, or if this command is called
  16077. with a prefix argument, the time stamp will contain date and time.
  16078. Otherwise, only the date will be included. All parts of a date not
  16079. specified by the user will be filled in from the current date/time.
  16080. So if you press just return without typing anything, the time stamp
  16081. will represent the current date/time. If there is already a timestamp
  16082. at the cursor, it will be modified."
  16083. (interactive "P")
  16084. (let* ((ts nil)
  16085. (default-time
  16086. ;; Default time is either today, or, when entering a range,
  16087. ;; the range start.
  16088. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16089. (save-excursion
  16090. (re-search-backward
  16091. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16092. (- (point) 20) t)))
  16093. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16094. (current-time)))
  16095. (default-input (and ts (org-get-compact-tod ts)))
  16096. org-time-was-given org-end-time-was-given time)
  16097. (cond
  16098. ((and (org-at-timestamp-p)
  16099. (eq last-command 'org-time-stamp)
  16100. (eq this-command 'org-time-stamp))
  16101. (insert "--")
  16102. (setq time (let ((this-command this-command))
  16103. (org-read-date arg 'totime nil nil default-time default-input)))
  16104. (org-insert-time-stamp time (or org-time-was-given arg)))
  16105. ((org-at-timestamp-p)
  16106. (setq time (let ((this-command this-command))
  16107. (org-read-date arg 'totime nil nil default-time default-input)))
  16108. (when (org-at-timestamp-p) ; just to get the match data
  16109. (replace-match "")
  16110. (setq org-last-changed-timestamp
  16111. (org-insert-time-stamp
  16112. time (or org-time-was-given arg)
  16113. nil nil nil (list org-end-time-was-given))))
  16114. (message "Timestamp updated"))
  16115. (t
  16116. (setq time (let ((this-command this-command))
  16117. (org-read-date arg 'totime nil nil default-time default-input)))
  16118. (org-insert-time-stamp time (or org-time-was-given arg)
  16119. nil nil nil (list org-end-time-was-given))))))
  16120. ;; FIXME: can we use this for something else????
  16121. ;; like computing time differences?????
  16122. (defun org-get-compact-tod (s)
  16123. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16124. (let* ((t1 (match-string 1 s))
  16125. (h1 (string-to-number (match-string 2 s)))
  16126. (m1 (string-to-number (match-string 3 s)))
  16127. (t2 (and (match-end 4) (match-string 5 s)))
  16128. (h2 (and t2 (string-to-number (match-string 6 s))))
  16129. (m2 (and t2 (string-to-number (match-string 7 s))))
  16130. dh dm)
  16131. (if (not t2)
  16132. t1
  16133. (setq dh (- h2 h1) dm (- m2 m1))
  16134. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16135. (concat t1 "+" (number-to-string dh)
  16136. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16137. (defun org-time-stamp-inactive (&optional arg)
  16138. "Insert an inactive time stamp.
  16139. An inactive time stamp is enclosed in square brackets instead of angle
  16140. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16141. does not link to the calendar and cannot be changed with the S-cursor keys.
  16142. So these are more for recording a certain time/date."
  16143. (interactive "P")
  16144. (let (org-time-was-given org-end-time-was-given time)
  16145. (setq time (org-read-date arg 'totime))
  16146. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16147. nil nil (list org-end-time-was-given))))
  16148. (defvar org-date-ovl (org-make-overlay 1 1))
  16149. (org-overlay-put org-date-ovl 'face 'org-warning)
  16150. (org-detach-overlay org-date-ovl)
  16151. (defvar org-ans1) ; dynamically scoped parameter
  16152. (defvar org-ans2) ; dynamically scoped parameter
  16153. (defvar org-plain-time-of-day-regexp) ; defined below
  16154. (defvar org-read-date-overlay nil)
  16155. (defvar org-dcst nil) ; dynamically scoped
  16156. (defun org-read-date (&optional with-time to-time from-string prompt
  16157. default-time default-input)
  16158. "Read a date, possibly a time, and make things smooth for the user.
  16159. The prompt will suggest to enter an ISO date, but you can also enter anything
  16160. which will at least partially be understood by `parse-time-string'.
  16161. Unrecognized parts of the date will default to the current day, month, year,
  16162. hour and minute. If this command is called to replace a timestamp at point,
  16163. of to enter the second timestamp of a range, the default time is taken from the
  16164. existing stamp. For example,
  16165. 3-2-5 --> 2003-02-05
  16166. feb 15 --> currentyear-02-15
  16167. sep 12 9 --> 2009-09-12
  16168. 12:45 --> today 12:45
  16169. 22 sept 0:34 --> currentyear-09-22 0:34
  16170. 12 --> currentyear-currentmonth-12
  16171. Fri --> nearest Friday (today or later)
  16172. etc.
  16173. Furthermore you can specify a relative date by giving, as the *first* thing
  16174. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16175. change in days weeks, months, years.
  16176. With a single plus or minus, the date is relative to today. With a double
  16177. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16178. +4d --> four days from today
  16179. +4 --> same as above
  16180. +2w --> two weeks from today
  16181. ++5 --> five days from default date
  16182. The function understands only English month and weekday abbreviations,
  16183. but this can be configured with the variables `parse-time-months' and
  16184. `parse-time-weekdays'.
  16185. While prompting, a calendar is popped up - you can also select the
  16186. date with the mouse (button 1). The calendar shows a period of three
  16187. months. To scroll it to other months, use the keys `>' and `<'.
  16188. If you don't like the calendar, turn it off with
  16189. \(setq org-read-date-popup-calendar nil)
  16190. With optional argument TO-TIME, the date will immediately be converted
  16191. to an internal time.
  16192. With an optional argument WITH-TIME, the prompt will suggest to also
  16193. insert a time. Note that when WITH-TIME is not set, you can still
  16194. enter a time, and this function will inform the calling routine about
  16195. this change. The calling routine may then choose to change the format
  16196. used to insert the time stamp into the buffer to include the time.
  16197. With optional argument FROM-STRING, read from this string instead from
  16198. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16199. the time/date that is used for everything that is not specified by the
  16200. user."
  16201. (require 'parse-time)
  16202. (let* ((org-time-stamp-rounding-minutes
  16203. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16204. (org-dcst org-display-custom-times)
  16205. (ct (org-current-time))
  16206. (def (or default-time ct))
  16207. (defdecode (decode-time def))
  16208. (dummy (progn
  16209. (when (< (nth 2 defdecode) org-extend-today-until)
  16210. (setcar (nthcdr 2 defdecode) -1)
  16211. (setcar (nthcdr 1 defdecode) 59)
  16212. (setq def (apply 'encode-time defdecode)
  16213. defdecode (decode-time def)))))
  16214. (calendar-move-hook nil)
  16215. (view-diary-entries-initially nil)
  16216. (view-calendar-holidays-initially nil)
  16217. (timestr (format-time-string
  16218. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16219. (prompt (concat (if prompt (concat prompt " ") "")
  16220. (format "Date+time [%s]: " timestr)))
  16221. ans (org-ans0 "") org-ans1 org-ans2 final)
  16222. (cond
  16223. (from-string (setq ans from-string))
  16224. (org-read-date-popup-calendar
  16225. (save-excursion
  16226. (save-window-excursion
  16227. (calendar)
  16228. (calendar-forward-day (- (time-to-days def)
  16229. (calendar-absolute-from-gregorian
  16230. (calendar-current-date))))
  16231. (org-eval-in-calendar nil t)
  16232. (let* ((old-map (current-local-map))
  16233. (map (copy-keymap calendar-mode-map))
  16234. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16235. (org-defkey map (kbd "RET") 'org-calendar-select)
  16236. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16237. 'org-calendar-select-mouse)
  16238. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16239. 'org-calendar-select-mouse)
  16240. (org-defkey minibuffer-local-map [(meta shift left)]
  16241. (lambda () (interactive)
  16242. (org-eval-in-calendar '(calendar-backward-month 1))))
  16243. (org-defkey minibuffer-local-map [(meta shift right)]
  16244. (lambda () (interactive)
  16245. (org-eval-in-calendar '(calendar-forward-month 1))))
  16246. (org-defkey minibuffer-local-map [(meta shift up)]
  16247. (lambda () (interactive)
  16248. (org-eval-in-calendar '(calendar-backward-year 1))))
  16249. (org-defkey minibuffer-local-map [(meta shift down)]
  16250. (lambda () (interactive)
  16251. (org-eval-in-calendar '(calendar-forward-year 1))))
  16252. (org-defkey minibuffer-local-map [(shift up)]
  16253. (lambda () (interactive)
  16254. (org-eval-in-calendar '(calendar-backward-week 1))))
  16255. (org-defkey minibuffer-local-map [(shift down)]
  16256. (lambda () (interactive)
  16257. (org-eval-in-calendar '(calendar-forward-week 1))))
  16258. (org-defkey minibuffer-local-map [(shift left)]
  16259. (lambda () (interactive)
  16260. (org-eval-in-calendar '(calendar-backward-day 1))))
  16261. (org-defkey minibuffer-local-map [(shift right)]
  16262. (lambda () (interactive)
  16263. (org-eval-in-calendar '(calendar-forward-day 1))))
  16264. (org-defkey minibuffer-local-map ">"
  16265. (lambda () (interactive)
  16266. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16267. (org-defkey minibuffer-local-map "<"
  16268. (lambda () (interactive)
  16269. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16270. (unwind-protect
  16271. (progn
  16272. (use-local-map map)
  16273. (add-hook 'post-command-hook 'org-read-date-display)
  16274. (setq org-ans0 (read-string prompt default-input nil nil))
  16275. ;; org-ans0: from prompt
  16276. ;; org-ans1: from mouse click
  16277. ;; org-ans2: from calendar motion
  16278. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16279. (remove-hook 'post-command-hook 'org-read-date-display)
  16280. (use-local-map old-map)
  16281. (when org-read-date-overlay
  16282. (org-delete-overlay org-read-date-overlay)
  16283. (setq org-read-date-overlay nil)))))))
  16284. (t ; Naked prompt only
  16285. (unwind-protect
  16286. (setq ans (read-string prompt default-input nil timestr))
  16287. (when org-read-date-overlay
  16288. (org-delete-overlay org-read-date-overlay)
  16289. (setq org-read-date-overlay nil)))))
  16290. (setq final (org-read-date-analyze ans def defdecode))
  16291. (if to-time
  16292. (apply 'encode-time final)
  16293. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16294. (format "%04d-%02d-%02d %02d:%02d"
  16295. (nth 5 final) (nth 4 final) (nth 3 final)
  16296. (nth 2 final) (nth 1 final))
  16297. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16298. (defvar def)
  16299. (defvar defdecode)
  16300. (defvar with-time)
  16301. (defun org-read-date-display ()
  16302. "Display the currrent date prompt interpretation in the minibuffer."
  16303. (when org-read-date-display-live
  16304. (when org-read-date-overlay
  16305. (org-delete-overlay org-read-date-overlay))
  16306. (let ((p (point)))
  16307. (end-of-line 1)
  16308. (while (not (equal (buffer-substring
  16309. (max (point-min) (- (point) 4)) (point))
  16310. " "))
  16311. (insert " "))
  16312. (goto-char p))
  16313. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16314. " " (or org-ans1 org-ans2)))
  16315. (org-end-time-was-given nil)
  16316. (f (org-read-date-analyze ans def defdecode))
  16317. (fmts (if org-dcst
  16318. org-time-stamp-custom-formats
  16319. org-time-stamp-formats))
  16320. (fmt (if (or with-time
  16321. (and (boundp 'org-time-was-given) org-time-was-given))
  16322. (cdr fmts)
  16323. (car fmts)))
  16324. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16325. (when (and org-end-time-was-given
  16326. (string-match org-plain-time-of-day-regexp txt))
  16327. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16328. org-end-time-was-given
  16329. (substring txt (match-end 0)))))
  16330. (setq org-read-date-overlay
  16331. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16332. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16333. (defun org-read-date-analyze (ans def defdecode)
  16334. "Analyze the combined answer of the date prompt."
  16335. ;; FIXME: cleanup and comment
  16336. (let (delta deltan deltaw deltadef year month day
  16337. hour minute second wday pm h2 m2 tl wday1)
  16338. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16339. (setq ans (replace-match "" t t ans)
  16340. deltan (car delta)
  16341. deltaw (nth 1 delta)
  16342. deltadef (nth 2 delta)))
  16343. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16344. (when (string-match
  16345. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16346. (setq year (if (match-end 2)
  16347. (string-to-number (match-string 2 ans))
  16348. (string-to-number (format-time-string "%Y")))
  16349. month (string-to-number (match-string 3 ans))
  16350. day (string-to-number (match-string 4 ans)))
  16351. (if (< year 100) (setq year (+ 2000 year)))
  16352. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16353. t nil ans)))
  16354. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16355. ;; If there is a time with am/pm, and *no* time without it, we convert
  16356. ;; so that matching will be successful.
  16357. (loop for i from 1 to 2 do ; twice, for end time as well
  16358. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16359. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16360. (setq hour (string-to-number (match-string 1 ans))
  16361. minute (if (match-end 3)
  16362. (string-to-number (match-string 3 ans))
  16363. 0)
  16364. pm (equal ?p
  16365. (string-to-char (downcase (match-string 4 ans)))))
  16366. (if (and (= hour 12) (not pm))
  16367. (setq hour 0)
  16368. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16369. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16370. t t ans))))
  16371. ;; Check if a time range is given as a duration
  16372. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16373. (setq hour (string-to-number (match-string 1 ans))
  16374. h2 (+ hour (string-to-number (match-string 3 ans)))
  16375. minute (string-to-number (match-string 2 ans))
  16376. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16377. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16378. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16379. ;; Check if there is a time range
  16380. (when (boundp 'org-end-time-was-given)
  16381. (setq org-time-was-given nil)
  16382. (when (and (string-match org-plain-time-of-day-regexp ans)
  16383. (match-end 8))
  16384. (setq org-end-time-was-given (match-string 8 ans))
  16385. (setq ans (concat (substring ans 0 (match-beginning 7))
  16386. (substring ans (match-end 7))))))
  16387. (setq tl (parse-time-string ans)
  16388. day (or (nth 3 tl) (nth 3 defdecode))
  16389. month (or (nth 4 tl)
  16390. (if (and org-read-date-prefer-future
  16391. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16392. (1+ (nth 4 defdecode))
  16393. (nth 4 defdecode)))
  16394. year (or (nth 5 tl)
  16395. (if (and org-read-date-prefer-future
  16396. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16397. (1+ (nth 5 defdecode))
  16398. (nth 5 defdecode)))
  16399. hour (or (nth 2 tl) (nth 2 defdecode))
  16400. minute (or (nth 1 tl) (nth 1 defdecode))
  16401. second (or (nth 0 tl) 0)
  16402. wday (nth 6 tl))
  16403. (when deltan
  16404. (unless deltadef
  16405. (let ((now (decode-time (current-time))))
  16406. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16407. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16408. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16409. ((equal deltaw "m") (setq month (+ month deltan)))
  16410. ((equal deltaw "y") (setq year (+ year deltan)))))
  16411. (when (and wday (not (nth 3 tl)))
  16412. ;; Weekday was given, but no day, so pick that day in the week
  16413. ;; on or after the derived date.
  16414. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16415. (unless (equal wday wday1)
  16416. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16417. (if (and (boundp 'org-time-was-given)
  16418. (nth 2 tl))
  16419. (setq org-time-was-given t))
  16420. (if (< year 100) (setq year (+ 2000 year)))
  16421. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16422. (list second minute hour day month year)))
  16423. (defvar parse-time-weekdays)
  16424. (defun org-read-date-get-relative (s today default)
  16425. "Check string S for special relative date string.
  16426. TODAY and DEFAULT are internal times, for today and for a default.
  16427. Return shift list (N what def-flag)
  16428. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16429. N is the number of WHATs to shift.
  16430. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16431. the DEFAULT date rather than TODAY."
  16432. (when (string-match
  16433. (concat
  16434. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16435. "\\([0-9]+\\)?"
  16436. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16437. "\\([ \t]\\|$\\)") s)
  16438. (let* ((dir (if (match-end 1)
  16439. (string-to-char (substring (match-string 1 s) -1))
  16440. ?+))
  16441. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16442. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16443. (what (if (match-end 3) (match-string 3 s) "d"))
  16444. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16445. (date (if rel default today))
  16446. (wday (nth 6 (decode-time date)))
  16447. delta)
  16448. (if wday1
  16449. (progn
  16450. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16451. (if (= dir ?-) (setq delta (- delta 7)))
  16452. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16453. (list delta "d" rel))
  16454. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16455. (defun org-eval-in-calendar (form &optional keepdate)
  16456. "Eval FORM in the calendar window and return to current window.
  16457. Also, store the cursor date in variable org-ans2."
  16458. (let ((sw (selected-window)))
  16459. (select-window (get-buffer-window "*Calendar*"))
  16460. (eval form)
  16461. (when (and (not keepdate) (calendar-cursor-to-date))
  16462. (let* ((date (calendar-cursor-to-date))
  16463. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16464. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16465. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16466. (select-window sw)))
  16467. ; ;; Update the prompt to show new default date
  16468. ; (save-excursion
  16469. ; (goto-char (point-min))
  16470. ; (when (and org-ans2
  16471. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16472. ; (get-text-property (match-end 0) 'field))
  16473. ; (let ((inhibit-read-only t))
  16474. ; (replace-match (concat "[" org-ans2 "]") t t)
  16475. ; (add-text-properties (point-min) (1+ (match-end 0))
  16476. ; (text-properties-at (1+ (point-min)))))))))
  16477. (defun org-calendar-select ()
  16478. "Return to `org-read-date' with the date currently selected.
  16479. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16480. (interactive)
  16481. (when (calendar-cursor-to-date)
  16482. (let* ((date (calendar-cursor-to-date))
  16483. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16484. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16485. (if (active-minibuffer-window) (exit-minibuffer))))
  16486. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16487. "Insert a date stamp for the date given by the internal TIME.
  16488. WITH-HM means, use the stamp format that includes the time of the day.
  16489. INACTIVE means use square brackets instead of angular ones, so that the
  16490. stamp will not contribute to the agenda.
  16491. PRE and POST are optional strings to be inserted before and after the
  16492. stamp.
  16493. The command returns the inserted time stamp."
  16494. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16495. stamp)
  16496. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16497. (insert-before-markers (or pre ""))
  16498. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16499. (when (listp extra)
  16500. (setq extra (car extra))
  16501. (if (and (stringp extra)
  16502. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16503. (setq extra (format "-%02d:%02d"
  16504. (string-to-number (match-string 1 extra))
  16505. (string-to-number (match-string 2 extra))))
  16506. (setq extra nil)))
  16507. (when extra
  16508. (backward-char 1)
  16509. (insert-before-markers extra)
  16510. (forward-char 1))
  16511. (insert-before-markers (or post ""))
  16512. stamp))
  16513. (defun org-toggle-time-stamp-overlays ()
  16514. "Toggle the use of custom time stamp formats."
  16515. (interactive)
  16516. (setq org-display-custom-times (not org-display-custom-times))
  16517. (unless org-display-custom-times
  16518. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16519. (while (setq p (next-single-property-change p 'display))
  16520. (if (and (get-text-property p 'display)
  16521. (eq (get-text-property p 'face) 'org-date))
  16522. (remove-text-properties
  16523. p (setq p (next-single-property-change p 'display))
  16524. '(display t))))
  16525. (set-buffer-modified-p bmp)))
  16526. (if (featurep 'xemacs)
  16527. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16528. (org-restart-font-lock)
  16529. (setq org-table-may-need-update t)
  16530. (if org-display-custom-times
  16531. (message "Time stamps are overlayed with custom format")
  16532. (message "Time stamp overlays removed")))
  16533. (defun org-display-custom-time (beg end)
  16534. "Overlay modified time stamp format over timestamp between BED and END."
  16535. (let* ((ts (buffer-substring beg end))
  16536. t1 w1 with-hm tf time str w2 (off 0))
  16537. (save-match-data
  16538. (setq t1 (org-parse-time-string ts t))
  16539. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16540. (setq off (- (match-end 0) (match-beginning 0)))))
  16541. (setq end (- end off))
  16542. (setq w1 (- end beg)
  16543. with-hm (and (nth 1 t1) (nth 2 t1))
  16544. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16545. time (org-fix-decoded-time t1)
  16546. str (org-add-props
  16547. (format-time-string
  16548. (substring tf 1 -1) (apply 'encode-time time))
  16549. nil 'mouse-face 'highlight)
  16550. w2 (length str))
  16551. (if (not (= w2 w1))
  16552. (add-text-properties (1+ beg) (+ 2 beg)
  16553. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16554. (if (featurep 'xemacs)
  16555. (progn
  16556. (put-text-property beg end 'invisible t)
  16557. (put-text-property beg end 'end-glyph (make-glyph str)))
  16558. (put-text-property beg end 'display str))))
  16559. (defun org-translate-time (string)
  16560. "Translate all timestamps in STRING to custom format.
  16561. But do this only if the variable `org-display-custom-times' is set."
  16562. (when org-display-custom-times
  16563. (save-match-data
  16564. (let* ((start 0)
  16565. (re org-ts-regexp-both)
  16566. t1 with-hm inactive tf time str beg end)
  16567. (while (setq start (string-match re string start))
  16568. (setq beg (match-beginning 0)
  16569. end (match-end 0)
  16570. t1 (save-match-data
  16571. (org-parse-time-string (substring string beg end) t))
  16572. with-hm (and (nth 1 t1) (nth 2 t1))
  16573. inactive (equal (substring string beg (1+ beg)) "[")
  16574. tf (funcall (if with-hm 'cdr 'car)
  16575. org-time-stamp-custom-formats)
  16576. time (org-fix-decoded-time t1)
  16577. str (format-time-string
  16578. (concat
  16579. (if inactive "[" "<") (substring tf 1 -1)
  16580. (if inactive "]" ">"))
  16581. (apply 'encode-time time))
  16582. string (replace-match str t t string)
  16583. start (+ start (length str)))))))
  16584. string)
  16585. (defun org-fix-decoded-time (time)
  16586. "Set 0 instead of nil for the first 6 elements of time.
  16587. Don't touch the rest."
  16588. (let ((n 0))
  16589. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16590. (defun org-days-to-time (timestamp-string)
  16591. "Difference between TIMESTAMP-STRING and now in days."
  16592. (- (time-to-days (org-time-string-to-time timestamp-string))
  16593. (time-to-days (current-time))))
  16594. (defun org-deadline-close (timestamp-string &optional ndays)
  16595. "Is the time in TIMESTAMP-STRING close to the current date?"
  16596. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16597. (and (< (org-days-to-time timestamp-string) ndays)
  16598. (not (org-entry-is-done-p))))
  16599. (defun org-get-wdays (ts)
  16600. "Get the deadline lead time appropriate for timestring TS."
  16601. (cond
  16602. ((<= org-deadline-warning-days 0)
  16603. ;; 0 or negative, enforce this value no matter what
  16604. (- org-deadline-warning-days))
  16605. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16606. ;; lead time is specified.
  16607. (floor (* (string-to-number (match-string 1 ts))
  16608. (cdr (assoc (match-string 2 ts)
  16609. '(("d" . 1) ("w" . 7)
  16610. ("m" . 30.4) ("y" . 365.25)))))))
  16611. ;; go for the default.
  16612. (t org-deadline-warning-days)))
  16613. (defun org-calendar-select-mouse (ev)
  16614. "Return to `org-read-date' with the date currently selected.
  16615. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16616. (interactive "e")
  16617. (mouse-set-point ev)
  16618. (when (calendar-cursor-to-date)
  16619. (let* ((date (calendar-cursor-to-date))
  16620. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16621. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16622. (if (active-minibuffer-window) (exit-minibuffer))))
  16623. (defun org-check-deadlines (ndays)
  16624. "Check if there are any deadlines due or past due.
  16625. A deadline is considered due if it happens within `org-deadline-warning-days'
  16626. days from today's date. If the deadline appears in an entry marked DONE,
  16627. it is not shown. The prefix arg NDAYS can be used to test that many
  16628. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16629. (interactive "P")
  16630. (let* ((org-warn-days
  16631. (cond
  16632. ((equal ndays '(4)) 100000)
  16633. (ndays (prefix-numeric-value ndays))
  16634. (t (abs org-deadline-warning-days))))
  16635. (case-fold-search nil)
  16636. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16637. (callback
  16638. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16639. (message "%d deadlines past-due or due within %d days"
  16640. (org-occur regexp nil callback)
  16641. org-warn-days)))
  16642. (defun org-check-before-date (date)
  16643. "Check if there are deadlines or scheduled entries before DATE."
  16644. (interactive (list (org-read-date)))
  16645. (let ((case-fold-search nil)
  16646. (regexp (concat "\\<\\(" org-deadline-string
  16647. "\\|" org-scheduled-string
  16648. "\\) *<\\([^>]+\\)>"))
  16649. (callback
  16650. (lambda () (time-less-p
  16651. (org-time-string-to-time (match-string 2))
  16652. (org-time-string-to-time date)))))
  16653. (message "%d entries before %s"
  16654. (org-occur regexp nil callback) date)))
  16655. (defun org-evaluate-time-range (&optional to-buffer)
  16656. "Evaluate a time range by computing the difference between start and end.
  16657. Normally the result is just printed in the echo area, but with prefix arg
  16658. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16659. If the time range is actually in a table, the result is inserted into the
  16660. next column.
  16661. For time difference computation, a year is assumed to be exactly 365
  16662. days in order to avoid rounding problems."
  16663. (interactive "P")
  16664. (or
  16665. (org-clock-update-time-maybe)
  16666. (save-excursion
  16667. (unless (org-at-date-range-p t)
  16668. (goto-char (point-at-bol))
  16669. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16670. (if (not (org-at-date-range-p t))
  16671. (error "Not at a time-stamp range, and none found in current line")))
  16672. (let* ((ts1 (match-string 1))
  16673. (ts2 (match-string 2))
  16674. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16675. (match-end (match-end 0))
  16676. (time1 (org-time-string-to-time ts1))
  16677. (time2 (org-time-string-to-time ts2))
  16678. (t1 (time-to-seconds time1))
  16679. (t2 (time-to-seconds time2))
  16680. (diff (abs (- t2 t1)))
  16681. (negative (< (- t2 t1) 0))
  16682. ;; (ys (floor (* 365 24 60 60)))
  16683. (ds (* 24 60 60))
  16684. (hs (* 60 60))
  16685. (fy "%dy %dd %02d:%02d")
  16686. (fy1 "%dy %dd")
  16687. (fd "%dd %02d:%02d")
  16688. (fd1 "%dd")
  16689. (fh "%02d:%02d")
  16690. y d h m align)
  16691. (if havetime
  16692. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16693. y 0
  16694. d (floor (/ diff ds)) diff (mod diff ds)
  16695. h (floor (/ diff hs)) diff (mod diff hs)
  16696. m (floor (/ diff 60)))
  16697. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16698. y 0
  16699. d (floor (+ (/ diff ds) 0.5))
  16700. h 0 m 0))
  16701. (if (not to-buffer)
  16702. (message "%s" (org-make-tdiff-string y d h m))
  16703. (if (org-at-table-p)
  16704. (progn
  16705. (goto-char match-end)
  16706. (setq align t)
  16707. (and (looking-at " *|") (goto-char (match-end 0))))
  16708. (goto-char match-end))
  16709. (if (looking-at
  16710. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16711. (replace-match ""))
  16712. (if negative (insert " -"))
  16713. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16714. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16715. (insert " " (format fh h m))))
  16716. (if align (org-table-align))
  16717. (message "Time difference inserted")))))
  16718. (defun org-make-tdiff-string (y d h m)
  16719. (let ((fmt "")
  16720. (l nil))
  16721. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16722. l (push y l)))
  16723. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16724. l (push d l)))
  16725. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16726. l (push h l)))
  16727. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16728. l (push m l)))
  16729. (apply 'format fmt (nreverse l))))
  16730. (defun org-time-string-to-time (s)
  16731. (apply 'encode-time (org-parse-time-string s)))
  16732. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16733. "Convert a time stamp to an absolute day number.
  16734. If there is a specifyer for a cyclic time stamp, get the closest date to
  16735. DAYNR."
  16736. (cond
  16737. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16738. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16739. daynr
  16740. (+ daynr 1000)))
  16741. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16742. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16743. (time-to-days (current-time))) (match-string 0 s)
  16744. prefer))
  16745. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16746. (defun org-time-from-absolute (d)
  16747. "Return the time corresponding to date D.
  16748. D may be an absolute day number, or a calendar-type list (month day year)."
  16749. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16750. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16751. (defun org-calendar-holiday ()
  16752. "List of holidays, for Diary display in Org-mode."
  16753. (require 'holidays)
  16754. (let ((hl (funcall
  16755. (if (fboundp 'calendar-check-holidays)
  16756. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16757. (if hl (mapconcat 'identity hl "; "))))
  16758. (defun org-diary-sexp-entry (sexp entry date)
  16759. "Process a SEXP diary ENTRY for DATE."
  16760. (require 'diary-lib)
  16761. (let ((result (if calendar-debug-sexp
  16762. (let ((stack-trace-on-error t))
  16763. (eval (car (read-from-string sexp))))
  16764. (condition-case nil
  16765. (eval (car (read-from-string sexp)))
  16766. (error
  16767. (beep)
  16768. (message "Bad sexp at line %d in %s: %s"
  16769. (org-current-line)
  16770. (buffer-file-name) sexp)
  16771. (sleep-for 2))))))
  16772. (cond ((stringp result) result)
  16773. ((and (consp result)
  16774. (stringp (cdr result))) (cdr result))
  16775. (result entry)
  16776. (t nil))))
  16777. (defun org-diary-to-ical-string (frombuf)
  16778. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16779. This uses the icalendar.el library."
  16780. (let* ((tmpdir (if (featurep 'xemacs)
  16781. (temp-directory)
  16782. temporary-file-directory))
  16783. (tmpfile (make-temp-name
  16784. (expand-file-name "orgics" tmpdir)))
  16785. buf rtn b e)
  16786. (save-excursion
  16787. (set-buffer frombuf)
  16788. (icalendar-export-region (point-min) (point-max) tmpfile)
  16789. (setq buf (find-buffer-visiting tmpfile))
  16790. (set-buffer buf)
  16791. (goto-char (point-min))
  16792. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16793. (setq b (match-beginning 0)))
  16794. (goto-char (point-max))
  16795. (if (re-search-backward "^END:VEVENT" nil t)
  16796. (setq e (match-end 0)))
  16797. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16798. (kill-buffer buf)
  16799. (kill-buffer frombuf)
  16800. (delete-file tmpfile)
  16801. rtn))
  16802. (defun org-closest-date (start current change prefer)
  16803. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16804. When PREFER is `past' return a date that is either CURRENT or past.
  16805. When PREFER is `future', return a date that is either CURRENT or future."
  16806. ;; Make the proper lists from the dates
  16807. (catch 'exit
  16808. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16809. dn dw sday cday n1 n2
  16810. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16811. (setq start (org-date-to-gregorian start)
  16812. current (org-date-to-gregorian
  16813. (if org-agenda-repeating-timestamp-show-all
  16814. current
  16815. (time-to-days (current-time))))
  16816. sday (calendar-absolute-from-gregorian start)
  16817. cday (calendar-absolute-from-gregorian current))
  16818. (if (<= cday sday) (throw 'exit sday))
  16819. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16820. (setq dn (string-to-number (match-string 1 change))
  16821. dw (cdr (assoc (match-string 2 change) a1)))
  16822. (error "Invalid change specifyer: %s" change))
  16823. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16824. (cond
  16825. ((eq dw 'day)
  16826. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16827. n2 (+ n1 dn)))
  16828. ((eq dw 'year)
  16829. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16830. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16831. (setq date1 (list m d y1)
  16832. n1 (calendar-absolute-from-gregorian date1)
  16833. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16834. n2 (calendar-absolute-from-gregorian date2)))
  16835. ((eq dw 'month)
  16836. ;; approx number of month between the tow dates
  16837. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16838. ;; How often does dn fit in there?
  16839. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16840. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16841. m (+ m nm)
  16842. ny (floor (/ m 12))
  16843. y (+ y ny)
  16844. m (- m (* ny 12)))
  16845. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16846. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16847. (setq m2 (+ m dn) y2 y)
  16848. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16849. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16850. (while (< n2 cday)
  16851. (setq n1 n2 m m2 y y2)
  16852. (setq m2 (+ m dn) y2 y)
  16853. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16854. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16855. (if org-agenda-repeating-timestamp-show-all
  16856. (cond
  16857. ((eq prefer 'past) n1)
  16858. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16859. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16860. (cond
  16861. ((eq prefer 'past) n1)
  16862. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16863. (t (if (= cday n1) n1 n2)))))))
  16864. (defun org-date-to-gregorian (date)
  16865. "Turn any specification of DATE into a gregorian date for the calendar."
  16866. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16867. ((and (listp date) (= (length date) 3)) date)
  16868. ((stringp date)
  16869. (setq date (org-parse-time-string date))
  16870. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16871. ((listp date)
  16872. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16873. (defun org-parse-time-string (s &optional nodefault)
  16874. "Parse the standard Org-mode time string.
  16875. This should be a lot faster than the normal `parse-time-string'.
  16876. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16877. hour and minute fields will be nil if not given."
  16878. (if (string-match org-ts-regexp0 s)
  16879. (list 0
  16880. (if (or (match-beginning 8) (not nodefault))
  16881. (string-to-number (or (match-string 8 s) "0")))
  16882. (if (or (match-beginning 7) (not nodefault))
  16883. (string-to-number (or (match-string 7 s) "0")))
  16884. (string-to-number (match-string 4 s))
  16885. (string-to-number (match-string 3 s))
  16886. (string-to-number (match-string 2 s))
  16887. nil nil nil)
  16888. (make-list 9 0)))
  16889. (defun org-timestamp-up (&optional arg)
  16890. "Increase the date item at the cursor by one.
  16891. If the cursor is on the year, change the year. If it is on the month or
  16892. the day, change that.
  16893. With prefix ARG, change by that many units."
  16894. (interactive "p")
  16895. (org-timestamp-change (prefix-numeric-value arg)))
  16896. (defun org-timestamp-down (&optional arg)
  16897. "Decrease the date item at the cursor by one.
  16898. If the cursor is on the year, change the year. If it is on the month or
  16899. the day, change that.
  16900. With prefix ARG, change by that many units."
  16901. (interactive "p")
  16902. (org-timestamp-change (- (prefix-numeric-value arg))))
  16903. (defun org-timestamp-up-day (&optional arg)
  16904. "Increase the date in the time stamp by one day.
  16905. With prefix ARG, change that many days."
  16906. (interactive "p")
  16907. (if (and (not (org-at-timestamp-p t))
  16908. (org-on-heading-p))
  16909. (org-todo 'up)
  16910. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16911. (defun org-timestamp-down-day (&optional arg)
  16912. "Decrease the date in the time stamp by one day.
  16913. With prefix ARG, change that many days."
  16914. (interactive "p")
  16915. (if (and (not (org-at-timestamp-p t))
  16916. (org-on-heading-p))
  16917. (org-todo 'down)
  16918. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16919. (defsubst org-pos-in-match-range (pos n)
  16920. (and (match-beginning n)
  16921. (<= (match-beginning n) pos)
  16922. (>= (match-end n) pos)))
  16923. (defun org-at-timestamp-p (&optional inactive-ok)
  16924. "Determine if the cursor is in or at a timestamp."
  16925. (interactive)
  16926. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16927. (pos (point))
  16928. (ans (or (looking-at tsr)
  16929. (save-excursion
  16930. (skip-chars-backward "^[<\n\r\t")
  16931. (if (> (point) (point-min)) (backward-char 1))
  16932. (and (looking-at tsr)
  16933. (> (- (match-end 0) pos) -1))))))
  16934. (and ans
  16935. (boundp 'org-ts-what)
  16936. (setq org-ts-what
  16937. (cond
  16938. ((= pos (match-beginning 0)) 'bracket)
  16939. ((= pos (1- (match-end 0))) 'bracket)
  16940. ((org-pos-in-match-range pos 2) 'year)
  16941. ((org-pos-in-match-range pos 3) 'month)
  16942. ((org-pos-in-match-range pos 7) 'hour)
  16943. ((org-pos-in-match-range pos 8) 'minute)
  16944. ((or (org-pos-in-match-range pos 4)
  16945. (org-pos-in-match-range pos 5)) 'day)
  16946. ((and (> pos (or (match-end 8) (match-end 5)))
  16947. (< pos (match-end 0)))
  16948. (- pos (or (match-end 8) (match-end 5))))
  16949. (t 'day))))
  16950. ans))
  16951. (defun org-toggle-timestamp-type ()
  16952. ""
  16953. (interactive)
  16954. (when (org-at-timestamp-p t)
  16955. (save-excursion
  16956. (goto-char (match-beginning 0))
  16957. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16958. (goto-char (1- (match-end 0)))
  16959. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16960. (message "Timestamp is now %sactive"
  16961. (if (equal (char-before) ?>) "in" ""))))
  16962. (defun org-timestamp-change (n &optional what)
  16963. "Change the date in the time stamp at point.
  16964. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16965. `year', `minute', `second'. If WHAT is not given, the cursor position
  16966. in the timestamp determines what will be changed."
  16967. (let ((pos (point))
  16968. with-hm inactive
  16969. org-ts-what
  16970. extra
  16971. ts time time0)
  16972. (if (not (org-at-timestamp-p t))
  16973. (error "Not at a timestamp"))
  16974. (if (and (not what) (eq org-ts-what 'bracket))
  16975. (org-toggle-timestamp-type)
  16976. (if (and (not what) (not (eq org-ts-what 'day))
  16977. org-display-custom-times
  16978. (get-text-property (point) 'display)
  16979. (not (get-text-property (1- (point)) 'display)))
  16980. (setq org-ts-what 'day))
  16981. (setq org-ts-what (or what org-ts-what)
  16982. inactive (= (char-after (match-beginning 0)) ?\[)
  16983. ts (match-string 0))
  16984. (replace-match "")
  16985. (if (string-match
  16986. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16987. ts)
  16988. (setq extra (match-string 1 ts)))
  16989. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16990. (setq with-hm t))
  16991. (setq time0 (org-parse-time-string ts))
  16992. (setq time
  16993. (encode-time (or (car time0) 0)
  16994. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16995. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16996. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16997. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16998. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16999. (nthcdr 6 time0)))
  17000. (when (integerp org-ts-what)
  17001. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17002. (if (eq what 'calendar)
  17003. (let ((cal-date (org-get-date-from-calendar)))
  17004. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17005. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17006. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17007. (setcar time0 (or (car time0) 0))
  17008. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17009. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17010. (setq time (apply 'encode-time time0))))
  17011. (setq org-last-changed-timestamp
  17012. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17013. (org-clock-update-time-maybe)
  17014. (goto-char pos)
  17015. ;; Try to recenter the calendar window, if any
  17016. (if (and org-calendar-follow-timestamp-change
  17017. (get-buffer-window "*Calendar*" t)
  17018. (memq org-ts-what '(day month year)))
  17019. (org-recenter-calendar (time-to-days time))))))
  17020. ;; FIXME: does not yet work for lead times
  17021. (defun org-modify-ts-extra (s pos n)
  17022. "Change the different parts of the lead-time and repeat fields in timestamp."
  17023. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17024. ng h m new)
  17025. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17026. (cond
  17027. ((or (org-pos-in-match-range pos 2)
  17028. (org-pos-in-match-range pos 3))
  17029. (setq m (string-to-number (match-string 3 s))
  17030. h (string-to-number (match-string 2 s)))
  17031. (if (org-pos-in-match-range pos 2)
  17032. (setq h (+ h n))
  17033. (setq m (+ m n)))
  17034. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17035. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17036. (setq h (min 24 (max 0 h)))
  17037. (setq ng 1 new (format "-%02d:%02d" h m)))
  17038. ((org-pos-in-match-range pos 6)
  17039. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17040. ((org-pos-in-match-range pos 5)
  17041. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17042. (when ng
  17043. (setq s (concat
  17044. (substring s 0 (match-beginning ng))
  17045. new
  17046. (substring s (match-end ng))))))
  17047. s))
  17048. (defun org-recenter-calendar (date)
  17049. "If the calendar is visible, recenter it to DATE."
  17050. (let* ((win (selected-window))
  17051. (cwin (get-buffer-window "*Calendar*" t))
  17052. (calendar-move-hook nil))
  17053. (when cwin
  17054. (select-window cwin)
  17055. (calendar-goto-date (if (listp date) date
  17056. (calendar-gregorian-from-absolute date)))
  17057. (select-window win))))
  17058. (defun org-goto-calendar (&optional arg)
  17059. "Go to the Emacs calendar at the current date.
  17060. If there is a time stamp in the current line, go to that date.
  17061. A prefix ARG can be used to force the current date."
  17062. (interactive "P")
  17063. (let ((tsr org-ts-regexp) diff
  17064. (calendar-move-hook nil)
  17065. (view-calendar-holidays-initially nil)
  17066. (view-diary-entries-initially nil))
  17067. (if (or (org-at-timestamp-p)
  17068. (save-excursion
  17069. (beginning-of-line 1)
  17070. (looking-at (concat ".*" tsr))))
  17071. (let ((d1 (time-to-days (current-time)))
  17072. (d2 (time-to-days
  17073. (org-time-string-to-time (match-string 1)))))
  17074. (setq diff (- d2 d1))))
  17075. (calendar)
  17076. (calendar-goto-today)
  17077. (if (and diff (not arg)) (calendar-forward-day diff))))
  17078. (defun org-get-date-from-calendar ()
  17079. "Return a list (month day year) of date at point in calendar."
  17080. (with-current-buffer "*Calendar*"
  17081. (save-match-data
  17082. (calendar-cursor-to-date))))
  17083. (defun org-date-from-calendar ()
  17084. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17085. If there is already a time stamp at the cursor position, update it."
  17086. (interactive)
  17087. (if (org-at-timestamp-p t)
  17088. (org-timestamp-change 0 'calendar)
  17089. (let ((cal-date (org-get-date-from-calendar)))
  17090. (org-insert-time-stamp
  17091. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17092. (defvar appt-time-msg-list)
  17093. ;;;###autoload
  17094. (defun org-agenda-to-appt (&optional refresh filter)
  17095. "Activate appointments found in `org-agenda-files'.
  17096. With a \\[universal-argument] prefix, refresh the list of
  17097. appointements.
  17098. If FILTER is t, interactively prompt the user for a regular
  17099. expression, and filter out entries that don't match it.
  17100. If FILTER is a string, use this string as a regular expression
  17101. for filtering entries out.
  17102. FILTER can also be an alist with the car of each cell being
  17103. either 'headline or 'category. For example:
  17104. '((headline \"IMPORTANT\")
  17105. (category \"Work\"))
  17106. will only add headlines containing IMPORTANT or headlines
  17107. belonging to the \"Work\" category."
  17108. (interactive "P")
  17109. (require 'calendar)
  17110. (if refresh (setq appt-time-msg-list nil))
  17111. (if (eq filter t)
  17112. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17113. (let* ((cnt 0) ; count added events
  17114. (org-agenda-new-buffers nil)
  17115. (org-deadline-warning-days 0)
  17116. (today (org-date-to-gregorian
  17117. (time-to-days (current-time))))
  17118. (files (org-agenda-files)) entries file)
  17119. ;; Get all entries which may contain an appt
  17120. (while (setq file (pop files))
  17121. (setq entries
  17122. (append entries
  17123. (org-agenda-get-day-entries
  17124. file today :timestamp :scheduled :deadline))))
  17125. (setq entries (delq nil entries))
  17126. ;; Map thru entries and find if we should filter them out
  17127. (mapc
  17128. (lambda(x)
  17129. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17130. (cat (get-text-property 1 'org-category x))
  17131. (tod (get-text-property 1 'time-of-day x))
  17132. (ok (or (null filter)
  17133. (and (stringp filter) (string-match filter evt))
  17134. (and (listp filter)
  17135. (or (string-match
  17136. (cadr (assoc 'category filter)) cat)
  17137. (string-match
  17138. (cadr (assoc 'headline filter)) evt))))))
  17139. ;; FIXME: Shall we remove text-properties for the appt text?
  17140. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17141. (when (and ok tod)
  17142. (setq tod (number-to-string tod)
  17143. tod (when (string-match
  17144. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17145. (concat (match-string 1 tod) ":"
  17146. (match-string 2 tod))))
  17147. (appt-add tod evt)
  17148. (setq cnt (1+ cnt))))) entries)
  17149. (org-release-buffers org-agenda-new-buffers)
  17150. (if (eq cnt 0)
  17151. (message "No event to add")
  17152. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17153. ;;; The clock for measuring work time.
  17154. (defvar org-mode-line-string "")
  17155. (put 'org-mode-line-string 'risky-local-variable t)
  17156. (defvar org-mode-line-timer nil)
  17157. (defvar org-clock-heading "")
  17158. (defvar org-clock-start-time "")
  17159. (defun org-update-mode-line ()
  17160. (let* ((delta (- (time-to-seconds (current-time))
  17161. (time-to-seconds org-clock-start-time)))
  17162. (h (floor delta 3600))
  17163. (m (floor (- delta (* 3600 h)) 60)))
  17164. (setq org-mode-line-string
  17165. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17166. 'help-echo "Org-mode clock is running"))
  17167. (force-mode-line-update)))
  17168. (defvar org-clock-marker (make-marker)
  17169. "Marker recording the last clock-in.")
  17170. (defvar org-clock-mode-line-entry nil
  17171. "Information for the modeline about the running clock.")
  17172. (defun org-clock-in ()
  17173. "Start the clock on the current item.
  17174. If necessary, clock-out of the currently active clock."
  17175. (interactive)
  17176. (org-clock-out t)
  17177. (let (ts)
  17178. (save-excursion
  17179. (org-back-to-heading t)
  17180. (when (and org-clock-in-switch-to-state
  17181. (not (looking-at (concat outline-regexp "[ \t]*"
  17182. org-clock-in-switch-to-state
  17183. "\\>"))))
  17184. (org-todo org-clock-in-switch-to-state))
  17185. (if (and org-clock-heading-function
  17186. (functionp org-clock-heading-function))
  17187. (setq org-clock-heading (funcall org-clock-heading-function))
  17188. (if (looking-at org-complex-heading-regexp)
  17189. (setq org-clock-heading (match-string 4))
  17190. (setq org-clock-heading "???")))
  17191. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17192. (org-clock-find-position)
  17193. (insert "\n") (backward-char 1)
  17194. (indent-relative)
  17195. (insert org-clock-string " ")
  17196. (setq org-clock-start-time (current-time))
  17197. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17198. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17199. (or global-mode-string (setq global-mode-string '("")))
  17200. (or (memq 'org-mode-line-string global-mode-string)
  17201. (setq global-mode-string
  17202. (append global-mode-string '(org-mode-line-string))))
  17203. (org-update-mode-line)
  17204. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17205. (message "Clock started at %s" ts))))
  17206. (defun org-clock-find-position ()
  17207. "Find the location where the next clock line should be inserted."
  17208. (org-back-to-heading t)
  17209. (catch 'exit
  17210. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17211. (re (concat "^[ \t]*" org-clock-string))
  17212. (cnt 0)
  17213. first last)
  17214. (goto-char beg)
  17215. (when (eobp) (newline) (setq end (max (point) end)))
  17216. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17217. ;; we seem to have a CLOCK drawer, so go there.
  17218. (beginning-of-line 2)
  17219. (throw 'exit t))
  17220. ;; Lets count the CLOCK lines
  17221. (goto-char beg)
  17222. (while (re-search-forward re end t)
  17223. (setq first (or first (match-beginning 0))
  17224. last (match-beginning 0)
  17225. cnt (1+ cnt)))
  17226. (when (and (integerp org-clock-into-drawer)
  17227. (>= (1+ cnt) org-clock-into-drawer))
  17228. ;; Wrap current entries into a new drawer
  17229. (goto-char last)
  17230. (beginning-of-line 2)
  17231. (if (org-at-item-p) (org-end-of-item))
  17232. (insert ":END:\n")
  17233. (beginning-of-line 0)
  17234. (org-indent-line-function)
  17235. (goto-char first)
  17236. (insert ":CLOCK:\n")
  17237. (beginning-of-line 0)
  17238. (org-indent-line-function)
  17239. (org-flag-drawer t)
  17240. (beginning-of-line 2)
  17241. (throw 'exit nil))
  17242. (goto-char beg)
  17243. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17244. (not (equal (match-string 1) org-clock-string)))
  17245. ;; Planning info, skip to after it
  17246. (beginning-of-line 2)
  17247. (or (bolp) (newline)))
  17248. (when (eq t org-clock-into-drawer)
  17249. (insert ":CLOCK:\n:END:\n")
  17250. (beginning-of-line -1)
  17251. (org-indent-line-function)
  17252. (org-flag-drawer t)
  17253. (beginning-of-line 2)
  17254. (org-indent-line-function)))))
  17255. (defun org-clock-out (&optional fail-quietly)
  17256. "Stop the currently running clock.
  17257. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17258. (interactive)
  17259. (catch 'exit
  17260. (if (not (marker-buffer org-clock-marker))
  17261. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17262. (let (ts te s h m)
  17263. (save-excursion
  17264. (set-buffer (marker-buffer org-clock-marker))
  17265. (goto-char org-clock-marker)
  17266. (beginning-of-line 1)
  17267. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17268. (equal (match-string 1) org-clock-string))
  17269. (setq ts (match-string 2))
  17270. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17271. (goto-char (match-end 0))
  17272. (delete-region (point) (point-at-eol))
  17273. (insert "--")
  17274. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17275. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17276. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17277. h (floor (/ s 3600))
  17278. s (- s (* 3600 h))
  17279. m (floor (/ s 60))
  17280. s (- s (* 60 s)))
  17281. (insert " => " (format "%2d:%02d" h m))
  17282. (move-marker org-clock-marker nil)
  17283. (when org-log-note-clock-out
  17284. (org-add-log-maybe 'clock-out))
  17285. (when org-mode-line-timer
  17286. (cancel-timer org-mode-line-timer)
  17287. (setq org-mode-line-timer nil))
  17288. (setq global-mode-string
  17289. (delq 'org-mode-line-string global-mode-string))
  17290. (force-mode-line-update)
  17291. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17292. (defun org-clock-cancel ()
  17293. "Cancel the running clock be removing the start timestamp."
  17294. (interactive)
  17295. (if (not (marker-buffer org-clock-marker))
  17296. (error "No active clock"))
  17297. (save-excursion
  17298. (set-buffer (marker-buffer org-clock-marker))
  17299. (goto-char org-clock-marker)
  17300. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17301. (setq global-mode-string
  17302. (delq 'org-mode-line-string global-mode-string))
  17303. (force-mode-line-update)
  17304. (message "Clock canceled"))
  17305. (defun org-clock-goto (&optional delete-windows)
  17306. "Go to the currently clocked-in entry."
  17307. (interactive "P")
  17308. (if (not (marker-buffer org-clock-marker))
  17309. (error "No active clock"))
  17310. (switch-to-buffer-other-window
  17311. (marker-buffer org-clock-marker))
  17312. (if delete-windows (delete-other-windows))
  17313. (goto-char org-clock-marker)
  17314. (org-show-entry)
  17315. (org-back-to-heading)
  17316. (recenter))
  17317. (defvar org-clock-file-total-minutes nil
  17318. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17319. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17320. (defun org-clock-sum (&optional tstart tend)
  17321. "Sum the times for each subtree.
  17322. Puts the resulting times in minutes as a text property on each headline."
  17323. (interactive)
  17324. (let* ((bmp (buffer-modified-p))
  17325. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17326. org-clock-string
  17327. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17328. (lmax 30)
  17329. (ltimes (make-vector lmax 0))
  17330. (t1 0)
  17331. (level 0)
  17332. ts te dt
  17333. time)
  17334. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17335. (save-excursion
  17336. (goto-char (point-max))
  17337. (while (re-search-backward re nil t)
  17338. (cond
  17339. ((match-end 2)
  17340. ;; Two time stamps
  17341. (setq ts (match-string 2)
  17342. te (match-string 3)
  17343. ts (time-to-seconds
  17344. (apply 'encode-time (org-parse-time-string ts)))
  17345. te (time-to-seconds
  17346. (apply 'encode-time (org-parse-time-string te)))
  17347. ts (if tstart (max ts tstart) ts)
  17348. te (if tend (min te tend) te)
  17349. dt (- te ts)
  17350. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17351. ((match-end 4)
  17352. ;; A naket time
  17353. (setq t1 (+ t1 (string-to-number (match-string 5))
  17354. (* 60 (string-to-number (match-string 4))))))
  17355. (t ;; A headline
  17356. (setq level (- (match-end 1) (match-beginning 1)))
  17357. (when (or (> t1 0) (> (aref ltimes level) 0))
  17358. (loop for l from 0 to level do
  17359. (aset ltimes l (+ (aref ltimes l) t1)))
  17360. (setq t1 0 time (aref ltimes level))
  17361. (loop for l from level to (1- lmax) do
  17362. (aset ltimes l 0))
  17363. (goto-char (match-beginning 0))
  17364. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17365. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17366. (set-buffer-modified-p bmp)))
  17367. (defun org-clock-display (&optional total-only)
  17368. "Show subtree times in the entire buffer.
  17369. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17370. in the echo area."
  17371. (interactive)
  17372. (org-remove-clock-overlays)
  17373. (let (time h m p)
  17374. (org-clock-sum)
  17375. (unless total-only
  17376. (save-excursion
  17377. (goto-char (point-min))
  17378. (while (or (and (equal (setq p (point)) (point-min))
  17379. (get-text-property p :org-clock-minutes))
  17380. (setq p (next-single-property-change
  17381. (point) :org-clock-minutes)))
  17382. (goto-char p)
  17383. (when (setq time (get-text-property p :org-clock-minutes))
  17384. (org-put-clock-overlay time (funcall outline-level))))
  17385. (setq h (/ org-clock-file-total-minutes 60)
  17386. m (- org-clock-file-total-minutes (* 60 h)))
  17387. ;; Arrange to remove the overlays upon next change.
  17388. (when org-remove-highlights-with-change
  17389. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17390. nil 'local))))
  17391. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17392. (defvar org-clock-overlays nil)
  17393. (make-variable-buffer-local 'org-clock-overlays)
  17394. (defun org-put-clock-overlay (time &optional level)
  17395. "Put an overlays on the current line, displaying TIME.
  17396. If LEVEL is given, prefix time with a corresponding number of stars.
  17397. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17398. will be easy to remove."
  17399. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17400. (l (if level (org-get-legal-level level 0) 0))
  17401. (off 0)
  17402. ov tx)
  17403. (move-to-column c)
  17404. (unless (eolp) (skip-chars-backward "^ \t"))
  17405. (skip-chars-backward " \t")
  17406. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17407. tx (concat (buffer-substring (1- (point)) (point))
  17408. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17409. (org-add-props (format "%s %2d:%02d%s"
  17410. (make-string l ?*) h m
  17411. (make-string (- 16 l) ?\ ))
  17412. '(face secondary-selection))
  17413. ""))
  17414. (if (not (featurep 'xemacs))
  17415. (org-overlay-put ov 'display tx)
  17416. (org-overlay-put ov 'invisible t)
  17417. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17418. (push ov org-clock-overlays)))
  17419. (defun org-remove-clock-overlays (&optional beg end noremove)
  17420. "Remove the occur highlights from the buffer.
  17421. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17422. from the `before-change-functions' in the current buffer."
  17423. (interactive)
  17424. (unless org-inhibit-highlight-removal
  17425. (mapc 'org-delete-overlay org-clock-overlays)
  17426. (setq org-clock-overlays nil)
  17427. (unless noremove
  17428. (remove-hook 'before-change-functions
  17429. 'org-remove-clock-overlays 'local))))
  17430. (defun org-clock-out-if-current ()
  17431. "Clock out if the current entry contains the running clock.
  17432. This is used to stop the clock after a TODO entry is marked DONE,
  17433. and is only done if the variable `org-clock-out-when-done' is not nil."
  17434. (when (and org-clock-out-when-done
  17435. (member state org-done-keywords)
  17436. (equal (marker-buffer org-clock-marker) (current-buffer))
  17437. (< (point) org-clock-marker)
  17438. (> (save-excursion (outline-next-heading) (point))
  17439. org-clock-marker))
  17440. ;; Clock out, but don't accept a logging message for this.
  17441. (let ((org-log-note-clock-out nil))
  17442. (org-clock-out))))
  17443. (add-hook 'org-after-todo-state-change-hook
  17444. 'org-clock-out-if-current)
  17445. (defun org-check-running-clock ()
  17446. "Check if the current buffer contains the running clock.
  17447. If yes, offer to stop it and to save the buffer with the changes."
  17448. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17449. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17450. (buffer-name))))
  17451. (org-clock-out)
  17452. (when (y-or-n-p "Save changed buffer?")
  17453. (save-buffer))))
  17454. (defun org-clock-report (&optional arg)
  17455. "Create a table containing a report about clocked time.
  17456. If the cursor is inside an existing clocktable block, then the table
  17457. will be updated. If not, a new clocktable will be inserted.
  17458. When called with a prefix argument, move to the first clock table in the
  17459. buffer and update it."
  17460. (interactive "P")
  17461. (org-remove-clock-overlays)
  17462. (when arg
  17463. (org-find-dblock "clocktable")
  17464. (org-show-entry))
  17465. (if (org-in-clocktable-p)
  17466. (goto-char (org-in-clocktable-p))
  17467. (org-create-dblock (list :name "clocktable"
  17468. :maxlevel 2 :scope 'file)))
  17469. (org-update-dblock))
  17470. (defun org-in-clocktable-p ()
  17471. "Check if the cursor is in a clocktable."
  17472. (let ((pos (point)) start)
  17473. (save-excursion
  17474. (end-of-line 1)
  17475. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17476. (setq start (match-beginning 0))
  17477. (re-search-forward "^#\\+END:.*" nil t)
  17478. (>= (match-end 0) pos)
  17479. start))))
  17480. (defun org-clock-update-time-maybe ()
  17481. "If this is a CLOCK line, update it and return t.
  17482. Otherwise, return nil."
  17483. (interactive)
  17484. (save-excursion
  17485. (beginning-of-line 1)
  17486. (skip-chars-forward " \t")
  17487. (when (looking-at org-clock-string)
  17488. (let ((re (concat "[ \t]*" org-clock-string
  17489. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17490. "\\([ \t]*=>.*\\)?"))
  17491. ts te h m s)
  17492. (if (not (looking-at re))
  17493. nil
  17494. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17495. (end-of-line 1)
  17496. (setq ts (match-string 1)
  17497. te (match-string 2))
  17498. (setq s (- (time-to-seconds
  17499. (apply 'encode-time (org-parse-time-string te)))
  17500. (time-to-seconds
  17501. (apply 'encode-time (org-parse-time-string ts))))
  17502. h (floor (/ s 3600))
  17503. s (- s (* 3600 h))
  17504. m (floor (/ s 60))
  17505. s (- s (* 60 s)))
  17506. (insert " => " (format "%2d:%02d" h m))
  17507. t)))))
  17508. (defun org-clock-special-range (key &optional time as-strings)
  17509. "Return two times bordering a special time range.
  17510. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17511. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17512. A week starts Monday 0:00 and ends Sunday 24:00.
  17513. The range is determined relative to TIME. TIME defaults to the current time.
  17514. The return value is a cons cell with two internal times like the ones
  17515. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17516. the returned times will be formatted strings."
  17517. (let* ((tm (decode-time (or time (current-time))))
  17518. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17519. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17520. (dow (nth 6 tm))
  17521. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17522. (cond
  17523. ((eq key 'today)
  17524. (setq h 0 m 0 h1 24 m1 0))
  17525. ((eq key 'yesterday)
  17526. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17527. ((eq key 'thisweek)
  17528. (setq diff (if (= dow 0) 6 (1- dow))
  17529. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17530. ((eq key 'lastweek)
  17531. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17532. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17533. ((eq key 'thismonth)
  17534. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17535. ((eq key 'lastmonth)
  17536. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17537. ((eq key 'thisyear)
  17538. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17539. ((eq key 'lastyear)
  17540. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17541. (t (error "No such time block %s" key)))
  17542. (setq ts (encode-time s m h d month y)
  17543. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17544. (or d1 d) (or month1 month) (or y1 y)))
  17545. (setq fm (cdr org-time-stamp-formats))
  17546. (if as-strings
  17547. (cons (format-time-string fm ts) (format-time-string fm te))
  17548. (cons ts te))))
  17549. (defun org-dblock-write:clocktable (params)
  17550. "Write the standard clocktable."
  17551. (catch 'exit
  17552. (let* ((hlchars '((1 . "*") (2 . "/")))
  17553. (ins (make-marker))
  17554. (total-time nil)
  17555. (scope (plist-get params :scope))
  17556. (tostring (plist-get params :tostring))
  17557. (multifile (plist-get params :multifile))
  17558. (header (plist-get params :header))
  17559. (maxlevel (or (plist-get params :maxlevel) 3))
  17560. (step (plist-get params :step))
  17561. (emph (plist-get params :emphasize))
  17562. (ts (plist-get params :tstart))
  17563. (te (plist-get params :tend))
  17564. (block (plist-get params :block))
  17565. ipos time h m p level hlc hdl
  17566. cc beg end pos tbl)
  17567. (when step
  17568. (org-clocktable-steps params)
  17569. (throw 'exit nil))
  17570. (when block
  17571. (setq cc (org-clock-special-range block nil t)
  17572. ts (car cc) te (cdr cc)))
  17573. (if ts (setq ts (time-to-seconds
  17574. (apply 'encode-time (org-parse-time-string ts)))))
  17575. (if te (setq te (time-to-seconds
  17576. (apply 'encode-time (org-parse-time-string te)))))
  17577. (move-marker ins (point))
  17578. (setq ipos (point))
  17579. ;; Get the right scope
  17580. (setq pos (point))
  17581. (save-restriction
  17582. (cond
  17583. ((not scope))
  17584. ((eq scope 'file) (widen))
  17585. ((eq scope 'subtree) (org-narrow-to-subtree))
  17586. ((eq scope 'tree)
  17587. (while (org-up-heading-safe))
  17588. (org-narrow-to-subtree))
  17589. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17590. (symbol-name scope)))
  17591. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17592. (catch 'exit
  17593. (while (org-up-heading-safe)
  17594. (looking-at outline-regexp)
  17595. (if (<= (org-reduced-level (funcall outline-level)) level)
  17596. (throw 'exit nil))))
  17597. (org-narrow-to-subtree))
  17598. ((or (listp scope) (eq scope 'agenda))
  17599. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17600. (scope 'agenda)
  17601. (p1 (copy-sequence params))
  17602. file)
  17603. (plist-put p1 :tostring t)
  17604. (plist-put p1 :multifile t)
  17605. (plist-put p1 :scope 'file)
  17606. (org-prepare-agenda-buffers files)
  17607. (while (setq file (pop files))
  17608. (with-current-buffer (find-buffer-visiting file)
  17609. (push (org-clocktable-add-file
  17610. file (org-dblock-write:clocktable p1)) tbl)
  17611. (setq total-time (+ (or total-time 0)
  17612. org-clock-file-total-minutes)))))))
  17613. (goto-char pos)
  17614. (unless (eq scope 'agenda)
  17615. (org-clock-sum ts te)
  17616. (goto-char (point-min))
  17617. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17618. (goto-char p)
  17619. (when (setq time (get-text-property p :org-clock-minutes))
  17620. (save-excursion
  17621. (beginning-of-line 1)
  17622. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17623. (setq level (org-reduced-level
  17624. (- (match-end 1) (match-beginning 1))))
  17625. (<= level maxlevel))
  17626. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17627. hdl (match-string 2)
  17628. h (/ time 60)
  17629. m (- time (* 60 h)))
  17630. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17631. (push (concat
  17632. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17633. (make-string (1- level) ?|)
  17634. hlc (format "%d:%02d" h m) hlc
  17635. " |") tbl))))))
  17636. (setq tbl (nreverse tbl))
  17637. (if tostring
  17638. (if tbl (mapconcat 'identity tbl "\n") nil)
  17639. (goto-char ins)
  17640. (insert-before-markers
  17641. (or header
  17642. (concat
  17643. "Clock summary at ["
  17644. (substring
  17645. (format-time-string (cdr org-time-stamp-formats))
  17646. 1 -1)
  17647. "]."
  17648. (if block
  17649. (format " Considered range is /%s/." block)
  17650. "")
  17651. "\n\n"))
  17652. (if (eq scope 'agenda) "|File" "")
  17653. "|L|Headline|Time|\n")
  17654. (setq total-time (or total-time org-clock-file-total-minutes)
  17655. h (/ total-time 60)
  17656. m (- total-time (* 60 h)))
  17657. (insert-before-markers
  17658. "|-\n|"
  17659. (if (eq scope 'agenda) "|" "")
  17660. "|"
  17661. "*Total time*| "
  17662. (format "*%d:%02d*" h m)
  17663. "|\n|-\n")
  17664. (setq tbl (delq nil tbl))
  17665. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17666. (equal (substring (car tbl) 0 2) "|-"))
  17667. (pop tbl))
  17668. (insert-before-markers (mapconcat
  17669. 'identity (delq nil tbl)
  17670. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17671. (backward-delete-char 1)
  17672. (goto-char ipos)
  17673. (skip-chars-forward "^|")
  17674. (org-table-align))))))
  17675. (defun org-clocktable-steps (params)
  17676. (let* ((p1 (copy-sequence params))
  17677. (ts (plist-get p1 :tstart))
  17678. (te (plist-get p1 :tend))
  17679. (step0 (plist-get p1 :step))
  17680. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17681. (block (plist-get p1 :block))
  17682. cc)
  17683. (when block
  17684. (setq cc (org-clock-special-range block nil t)
  17685. ts (car cc) te (cdr cc)))
  17686. (if ts (setq ts (time-to-seconds
  17687. (apply 'encode-time (org-parse-time-string ts)))))
  17688. (if te (setq te (time-to-seconds
  17689. (apply 'encode-time (org-parse-time-string te)))))
  17690. (plist-put p1 :header "")
  17691. (plist-put p1 :step nil)
  17692. (plist-put p1 :block nil)
  17693. (while (< ts te)
  17694. (or (bolp) (insert "\n"))
  17695. (plist-put p1 :tstart (format-time-string
  17696. (car org-time-stamp-formats)
  17697. (seconds-to-time ts)))
  17698. (plist-put p1 :tend (format-time-string
  17699. (car org-time-stamp-formats)
  17700. (seconds-to-time (setq ts (+ ts step)))))
  17701. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17702. (plist-get p1 :tstart) "\n")
  17703. (org-dblock-write:clocktable p1)
  17704. (re-search-forward "#\\+END:")
  17705. (end-of-line 0))))
  17706. (defun org-clocktable-add-file (file table)
  17707. (if table
  17708. (let ((lines (org-split-string table "\n"))
  17709. (ff (file-name-nondirectory file)))
  17710. (mapconcat 'identity
  17711. (mapcar (lambda (x)
  17712. (if (string-match org-table-dataline-regexp x)
  17713. (concat "|" ff x)
  17714. x))
  17715. lines)
  17716. "\n"))))
  17717. ;; FIXME: I don't think anybody uses this, ask David
  17718. (defun org-collect-clock-time-entries ()
  17719. "Return an internal list with clocking information.
  17720. This list has one entry for each CLOCK interval.
  17721. FIXME: describe the elements."
  17722. (interactive)
  17723. (let ((re (concat "^[ \t]*" org-clock-string
  17724. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17725. rtn beg end next cont level title total closedp leafp
  17726. clockpos titlepos h m donep)
  17727. (save-excursion
  17728. (org-clock-sum)
  17729. (goto-char (point-min))
  17730. (while (re-search-forward re nil t)
  17731. (setq clockpos (match-beginning 0)
  17732. beg (match-string 1) end (match-string 2)
  17733. cont (match-end 0))
  17734. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17735. end (apply 'encode-time (org-parse-time-string end)))
  17736. (org-back-to-heading t)
  17737. (setq donep (org-entry-is-done-p))
  17738. (setq titlepos (point)
  17739. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17740. h (/ total 60) m (- total (* 60 h))
  17741. total (cons h m))
  17742. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17743. (setq level (- (match-end 1) (match-beginning 1))
  17744. title (org-match-string-no-properties 2))
  17745. (save-excursion (outline-next-heading) (setq next (point)))
  17746. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17747. (goto-char next)
  17748. (setq leafp (and (looking-at "^\\*+ ")
  17749. (<= (- (match-end 0) (point)) level)))
  17750. (push (list beg end clockpos closedp donep
  17751. total title titlepos level leafp)
  17752. rtn)
  17753. (goto-char cont)))
  17754. (nreverse rtn)))
  17755. ;;;; Agenda, and Diary Integration
  17756. ;;; Define the Org-agenda-mode
  17757. (defvar org-agenda-mode-map (make-sparse-keymap)
  17758. "Keymap for `org-agenda-mode'.")
  17759. (defvar org-agenda-menu) ; defined later in this file.
  17760. (defvar org-agenda-follow-mode nil)
  17761. (defvar org-agenda-show-log nil)
  17762. (defvar org-agenda-redo-command nil)
  17763. (defvar org-agenda-mode-hook nil)
  17764. (defvar org-agenda-type nil)
  17765. (defvar org-agenda-force-single-file nil)
  17766. (defun org-agenda-mode ()
  17767. "Mode for time-sorted view on action items in Org-mode files.
  17768. The following commands are available:
  17769. \\{org-agenda-mode-map}"
  17770. (interactive)
  17771. (kill-all-local-variables)
  17772. (setq org-agenda-undo-list nil
  17773. org-agenda-pending-undo-list nil)
  17774. (setq major-mode 'org-agenda-mode)
  17775. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17776. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17777. (setq mode-name "Org-Agenda")
  17778. (use-local-map org-agenda-mode-map)
  17779. (easy-menu-add org-agenda-menu)
  17780. (if org-startup-truncated (setq truncate-lines t))
  17781. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17782. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17783. ;; Make sure properties are removed when copying text
  17784. (when (boundp 'buffer-substring-filters)
  17785. (org-set-local 'buffer-substring-filters
  17786. (cons (lambda (x)
  17787. (set-text-properties 0 (length x) nil x) x)
  17788. buffer-substring-filters)))
  17789. (unless org-agenda-keep-modes
  17790. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17791. org-agenda-show-log nil))
  17792. (easy-menu-change
  17793. '("Agenda") "Agenda Files"
  17794. (append
  17795. (list
  17796. (vector
  17797. (if (get 'org-agenda-files 'org-restrict)
  17798. "Restricted to single file"
  17799. "Edit File List")
  17800. '(org-edit-agenda-file-list)
  17801. (not (get 'org-agenda-files 'org-restrict)))
  17802. "--")
  17803. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17804. (org-agenda-set-mode-name)
  17805. (apply
  17806. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17807. (list 'org-agenda-mode-hook)))
  17808. (substitute-key-definition 'undo 'org-agenda-undo
  17809. org-agenda-mode-map global-map)
  17810. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17811. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17812. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17813. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17814. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17815. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17816. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17817. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17818. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17819. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17820. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17821. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17822. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17823. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17824. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17825. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17826. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17827. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17828. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17829. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17830. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17831. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17832. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17833. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17834. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17835. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17836. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17837. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17838. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17839. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17840. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17841. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17842. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17843. (while l (org-defkey org-agenda-mode-map
  17844. (int-to-string (pop l)) 'digit-argument)))
  17845. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17846. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17847. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17848. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17849. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17850. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17851. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17852. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17853. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17854. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17855. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17856. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17857. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17858. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17859. (org-defkey org-agenda-mode-map "n" 'next-line)
  17860. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17861. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17862. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17863. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17864. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17865. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17866. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17867. (eval-after-load "calendar"
  17868. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17869. 'org-calendar-goto-agenda))
  17870. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17871. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17872. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17873. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17874. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17875. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17876. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17877. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17878. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17879. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17880. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17881. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17882. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17883. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17884. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17885. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17886. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17887. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17888. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17889. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17890. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17891. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17892. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17893. "Local keymap for agenda entries from Org-mode.")
  17894. (org-defkey org-agenda-keymap
  17895. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17896. (org-defkey org-agenda-keymap
  17897. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17898. (when org-agenda-mouse-1-follows-link
  17899. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17900. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17901. '("Agenda"
  17902. ("Agenda Files")
  17903. "--"
  17904. ["Show" org-agenda-show t]
  17905. ["Go To (other window)" org-agenda-goto t]
  17906. ["Go To (this window)" org-agenda-switch-to t]
  17907. ["Follow Mode" org-agenda-follow-mode
  17908. :style toggle :selected org-agenda-follow-mode :active t]
  17909. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17910. "--"
  17911. ["Cycle TODO" org-agenda-todo t]
  17912. ["Archive subtree" org-agenda-archive t]
  17913. ["Delete subtree" org-agenda-kill t]
  17914. "--"
  17915. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17916. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17917. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17918. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17919. "--"
  17920. ("Tags and Properties"
  17921. ["Show all Tags" org-agenda-show-tags t]
  17922. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17923. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17924. "--"
  17925. ["Column View" org-columns t])
  17926. ("Date/Schedule"
  17927. ["Schedule" org-agenda-schedule t]
  17928. ["Set Deadline" org-agenda-deadline t]
  17929. "--"
  17930. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17931. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17932. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17933. ("Clock"
  17934. ["Clock in" org-agenda-clock-in t]
  17935. ["Clock out" org-agenda-clock-out t]
  17936. ["Clock cancel" org-agenda-clock-cancel t]
  17937. ["Goto running clock" org-clock-goto t])
  17938. ("Priority"
  17939. ["Set Priority" org-agenda-priority t]
  17940. ["Increase Priority" org-agenda-priority-up t]
  17941. ["Decrease Priority" org-agenda-priority-down t]
  17942. ["Show Priority" org-agenda-show-priority t])
  17943. ("Calendar/Diary"
  17944. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17945. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17946. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17947. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17948. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17949. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17950. "--"
  17951. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17952. "--"
  17953. ("View"
  17954. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17955. :style radio :selected (equal org-agenda-ndays 1)]
  17956. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17957. :style radio :selected (equal org-agenda-ndays 7)]
  17958. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17959. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17960. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17961. :style radio :selected (member org-agenda-ndays '(365 366))]
  17962. "--"
  17963. ["Show Logbook entries" org-agenda-log-mode
  17964. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17965. ["Include Diary" org-agenda-toggle-diary
  17966. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17967. ["Use Time Grid" org-agenda-toggle-time-grid
  17968. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17969. ["Write view to file" org-write-agenda t]
  17970. ["Rebuild buffer" org-agenda-redo t]
  17971. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17972. "--"
  17973. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17974. "--"
  17975. ["Quit" org-agenda-quit t]
  17976. ["Exit and Release Buffers" org-agenda-exit t]
  17977. ))
  17978. ;;; Agenda undo
  17979. (defvar org-agenda-allow-remote-undo t
  17980. "Non-nil means, allow remote undo from the agenda buffer.")
  17981. (defvar org-agenda-undo-list nil
  17982. "List of undoable operations in the agenda since last refresh.")
  17983. (defvar org-agenda-undo-has-started-in nil
  17984. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17985. (defvar org-agenda-pending-undo-list nil
  17986. "In a series of undo commands, this is the list of remaning undo items.")
  17987. (defmacro org-if-unprotected (&rest body)
  17988. "Execute BODY if there is no `org-protected' text property at point."
  17989. (declare (debug t))
  17990. `(unless (get-text-property (point) 'org-protected)
  17991. ,@body))
  17992. (defmacro org-with-remote-undo (_buffer &rest _body)
  17993. "Execute BODY while recording undo information in two buffers."
  17994. (declare (indent 1) (debug t))
  17995. `(let ((_cline (org-current-line))
  17996. (_cmd this-command)
  17997. (_buf1 (current-buffer))
  17998. (_buf2 ,_buffer)
  17999. (_undo1 buffer-undo-list)
  18000. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18001. _c1 _c2)
  18002. ,@_body
  18003. (when org-agenda-allow-remote-undo
  18004. (setq _c1 (org-verify-change-for-undo
  18005. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18006. _c2 (org-verify-change-for-undo
  18007. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18008. (when (or _c1 _c2)
  18009. ;; make sure there are undo boundaries
  18010. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18011. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18012. ;; remember which buffer to undo
  18013. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18014. org-agenda-undo-list)))))
  18015. (defun org-agenda-undo ()
  18016. "Undo a remote editing step in the agenda.
  18017. This undoes changes both in the agenda buffer and in the remote buffer
  18018. that have been changed along."
  18019. (interactive)
  18020. (or org-agenda-allow-remote-undo
  18021. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18022. (if (not (eq this-command last-command))
  18023. (setq org-agenda-undo-has-started-in nil
  18024. org-agenda-pending-undo-list org-agenda-undo-list))
  18025. (if (not org-agenda-pending-undo-list)
  18026. (error "No further undo information"))
  18027. (let* ((entry (pop org-agenda-pending-undo-list))
  18028. buf line cmd rembuf)
  18029. (setq cmd (pop entry) line (pop entry))
  18030. (setq rembuf (nth 2 entry))
  18031. (org-with-remote-undo rembuf
  18032. (while (bufferp (setq buf (pop entry)))
  18033. (if (pop entry)
  18034. (with-current-buffer buf
  18035. (let ((last-undo-buffer buf)
  18036. (inhibit-read-only t))
  18037. (unless (memq buf org-agenda-undo-has-started-in)
  18038. (push buf org-agenda-undo-has-started-in)
  18039. (make-local-variable 'pending-undo-list)
  18040. (undo-start))
  18041. (while (and pending-undo-list
  18042. (listp pending-undo-list)
  18043. (not (car pending-undo-list)))
  18044. (pop pending-undo-list))
  18045. (undo-more 1))))))
  18046. (goto-line line)
  18047. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18048. (defun org-verify-change-for-undo (l1 l2)
  18049. "Verify that a real change occurred between the undo lists L1 and L2."
  18050. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18051. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18052. (not (eq l1 l2)))
  18053. ;;; Agenda dispatch
  18054. (defvar org-agenda-restrict nil)
  18055. (defvar org-agenda-restrict-begin (make-marker))
  18056. (defvar org-agenda-restrict-end (make-marker))
  18057. (defvar org-agenda-last-dispatch-buffer nil)
  18058. (defvar org-agenda-overriding-restriction nil)
  18059. ;;;###autoload
  18060. (defun org-agenda (arg &optional keys restriction)
  18061. "Dispatch agenda commands to collect entries to the agenda buffer.
  18062. Prompts for a command to execute. Any prefix arg will be passed
  18063. on to the selected command. The default selections are:
  18064. a Call `org-agenda-list' to display the agenda for current day or week.
  18065. t Call `org-todo-list' to display the global todo list.
  18066. T Call `org-todo-list' to display the global todo list, select only
  18067. entries with a specific TODO keyword (the user gets a prompt).
  18068. m Call `org-tags-view' to display headlines with tags matching
  18069. a condition (the user is prompted for the condition).
  18070. M Like `m', but select only TODO entries, no ordinary headlines.
  18071. L Create a timeline for the current buffer.
  18072. e Export views to associated files.
  18073. More commands can be added by configuring the variable
  18074. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18075. searches can be pre-defined in this way.
  18076. If the current buffer is in Org-mode and visiting a file, you can also
  18077. first press `<' once to indicate that the agenda should be temporarily
  18078. \(until the next use of \\[org-agenda]) restricted to the current file.
  18079. Pressing `<' twice means to restrict to the current subtree or region
  18080. \(if active)."
  18081. (interactive "P")
  18082. (catch 'exit
  18083. (let* ((prefix-descriptions nil)
  18084. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18085. (org-agenda-custom-commands
  18086. ;; normalize different versions
  18087. (delq nil
  18088. (mapcar
  18089. (lambda (x)
  18090. (cond ((stringp (cdr x))
  18091. (push x prefix-descriptions)
  18092. nil)
  18093. ((stringp (nth 1 x)) x)
  18094. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18095. (t (cons (car x) (cons "" (cdr x))))))
  18096. org-agenda-custom-commands)))
  18097. (buf (current-buffer))
  18098. (bfn (buffer-file-name (buffer-base-buffer)))
  18099. entry key type match lprops ans)
  18100. ;; Turn off restriction unless there is an overriding one
  18101. (unless org-agenda-overriding-restriction
  18102. (put 'org-agenda-files 'org-restrict nil)
  18103. (setq org-agenda-restrict nil)
  18104. (move-marker org-agenda-restrict-begin nil)
  18105. (move-marker org-agenda-restrict-end nil))
  18106. ;; Delete old local properties
  18107. (put 'org-agenda-redo-command 'org-lprops nil)
  18108. ;; Remember where this call originated
  18109. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18110. (unless keys
  18111. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18112. keys (car ans)
  18113. restriction (cdr ans)))
  18114. ;; Estabish the restriction, if any
  18115. (when (and (not org-agenda-overriding-restriction) restriction)
  18116. (put 'org-agenda-files 'org-restrict (list bfn))
  18117. (cond
  18118. ((eq restriction 'region)
  18119. (setq org-agenda-restrict t)
  18120. (move-marker org-agenda-restrict-begin (region-beginning))
  18121. (move-marker org-agenda-restrict-end (region-end)))
  18122. ((eq restriction 'subtree)
  18123. (save-excursion
  18124. (setq org-agenda-restrict t)
  18125. (org-back-to-heading t)
  18126. (move-marker org-agenda-restrict-begin (point))
  18127. (move-marker org-agenda-restrict-end
  18128. (progn (org-end-of-subtree t)))))))
  18129. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18130. ;; For example the todo list should not need it (but does...)
  18131. (cond
  18132. ((setq entry (assoc keys org-agenda-custom-commands))
  18133. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18134. (progn
  18135. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18136. (put 'org-agenda-redo-command 'org-lprops lprops)
  18137. (cond
  18138. ((eq type 'agenda)
  18139. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18140. ((eq type 'alltodo)
  18141. (org-let lprops '(org-todo-list current-prefix-arg)))
  18142. ((eq type 'stuck)
  18143. (org-let lprops '(org-agenda-list-stuck-projects
  18144. current-prefix-arg)))
  18145. ((eq type 'tags)
  18146. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18147. ((eq type 'tags-todo)
  18148. (org-let lprops '(org-tags-view '(4) match)))
  18149. ((eq type 'todo)
  18150. (org-let lprops '(org-todo-list match)))
  18151. ((eq type 'tags-tree)
  18152. (org-check-for-org-mode)
  18153. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18154. ((eq type 'todo-tree)
  18155. (org-check-for-org-mode)
  18156. (org-let lprops
  18157. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18158. (regexp-quote match) "\\>"))))
  18159. ((eq type 'occur-tree)
  18160. (org-check-for-org-mode)
  18161. (org-let lprops '(org-occur match)))
  18162. ((functionp type)
  18163. (org-let lprops '(funcall type match)))
  18164. ((fboundp type)
  18165. (org-let lprops '(funcall type match)))
  18166. (t (error "Invalid custom agenda command type %s" type))))
  18167. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18168. ((equal keys "C")
  18169. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18170. (customize-variable 'org-agenda-custom-commands))
  18171. ((equal keys "a") (call-interactively 'org-agenda-list))
  18172. ((equal keys "t") (call-interactively 'org-todo-list))
  18173. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18174. ((equal keys "m") (call-interactively 'org-tags-view))
  18175. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18176. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18177. ((equal keys "L")
  18178. (unless (org-mode-p)
  18179. (error "This is not an Org-mode file"))
  18180. (unless restriction
  18181. (put 'org-agenda-files 'org-restrict (list bfn))
  18182. (org-call-with-arg 'org-timeline arg)))
  18183. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18184. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18185. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18186. (t (error "Invalid agenda key"))))))
  18187. (defun org-agenda-normalize-custom-commands (cmds)
  18188. (delq nil
  18189. (mapcar
  18190. (lambda (x)
  18191. (cond ((stringp (cdr x)) nil)
  18192. ((stringp (nth 1 x)) x)
  18193. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18194. (t (cons (car x) (cons "" (cdr x))))))
  18195. cmds)))
  18196. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18197. "The user interface for selecting an agenda command."
  18198. (catch 'exit
  18199. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18200. (restrict-ok (and bfn (org-mode-p)))
  18201. (region-p (org-region-active-p))
  18202. (custom org-agenda-custom-commands)
  18203. (selstring "")
  18204. restriction second-time
  18205. c entry key type match prefixes rmheader header-end custom1 desc)
  18206. (save-window-excursion
  18207. (delete-other-windows)
  18208. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18209. (erase-buffer)
  18210. (insert (eval-when-compile
  18211. (let ((header
  18212. "
  18213. Press key for an agenda command: < Buffer,subtree/region restriction
  18214. -------------------------------- > Remove restriction
  18215. a Agenda for current week or day e Export agenda views
  18216. t List of all TODO entries T Entries with special TODO kwd
  18217. m Match a TAGS query M Like m, but only TODO entries
  18218. L Timeline for current buffer # List stuck projects (!=configure)
  18219. / Multi-occur C Configure custom agenda commands
  18220. ")
  18221. (start 0))
  18222. (while (string-match
  18223. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18224. header start)
  18225. (setq start (match-end 0))
  18226. (add-text-properties (match-beginning 2) (match-end 2)
  18227. '(face bold) header))
  18228. header)))
  18229. (setq header-end (move-marker (make-marker) (point)))
  18230. (while t
  18231. (setq custom1 custom)
  18232. (when (eq rmheader t)
  18233. (goto-line 1)
  18234. (re-search-forward ":" nil t)
  18235. (delete-region (match-end 0) (point-at-eol))
  18236. (forward-char 1)
  18237. (looking-at "-+")
  18238. (delete-region (match-end 0) (point-at-eol))
  18239. (move-marker header-end (match-end 0)))
  18240. (goto-char header-end)
  18241. (delete-region (point) (point-max))
  18242. (while (setq entry (pop custom1))
  18243. (setq key (car entry) desc (nth 1 entry)
  18244. type (nth 2 entry) match (nth 3 entry))
  18245. (if (> (length key) 1)
  18246. (add-to-list 'prefixes (string-to-char key))
  18247. (insert
  18248. (format
  18249. "\n%-4s%-14s: %s"
  18250. (org-add-props (copy-sequence key)
  18251. '(face bold))
  18252. (cond
  18253. ((string-match "\\S-" desc) desc)
  18254. ((eq type 'agenda) "Agenda for current week or day")
  18255. ((eq type 'alltodo) "List of all TODO entries")
  18256. ((eq type 'stuck) "List of stuck projects")
  18257. ((eq type 'todo) "TODO keyword")
  18258. ((eq type 'tags) "Tags query")
  18259. ((eq type 'tags-todo) "Tags (TODO)")
  18260. ((eq type 'tags-tree) "Tags tree")
  18261. ((eq type 'todo-tree) "TODO kwd tree")
  18262. ((eq type 'occur-tree) "Occur tree")
  18263. ((functionp type) (if (symbolp type)
  18264. (symbol-name type)
  18265. "Lambda expression"))
  18266. (t "???"))
  18267. (cond
  18268. ((stringp match)
  18269. (org-add-props match nil 'face 'org-warning))
  18270. (match
  18271. (format "set of %d commands" (length match)))
  18272. (t ""))))))
  18273. (when prefixes
  18274. (mapc (lambda (x)
  18275. (insert
  18276. (format "\n%s %s"
  18277. (org-add-props (char-to-string x)
  18278. nil 'face 'bold)
  18279. (or (cdr (assoc (concat selstring (char-to-string x))
  18280. prefix-descriptions))
  18281. "Prefix key"))))
  18282. prefixes))
  18283. (goto-char (point-min))
  18284. (when (fboundp 'fit-window-to-buffer)
  18285. (if second-time
  18286. (if (not (pos-visible-in-window-p (point-max)))
  18287. (fit-window-to-buffer))
  18288. (setq second-time t)
  18289. (fit-window-to-buffer)))
  18290. (message "Press key for agenda command%s:"
  18291. (if (or restrict-ok org-agenda-overriding-restriction)
  18292. (if org-agenda-overriding-restriction
  18293. " (restriction lock active)"
  18294. (if restriction
  18295. (format " (restricted to %s)" restriction)
  18296. " (unrestricted)"))
  18297. ""))
  18298. (setq c (read-char-exclusive))
  18299. (message "")
  18300. (cond
  18301. ((assoc (char-to-string c) custom)
  18302. (setq selstring (concat selstring (char-to-string c)))
  18303. (throw 'exit (cons selstring restriction)))
  18304. ((memq c prefixes)
  18305. (setq selstring (concat selstring (char-to-string c))
  18306. prefixes nil
  18307. rmheader (or rmheader t)
  18308. custom (delq nil (mapcar
  18309. (lambda (x)
  18310. (if (or (= (length (car x)) 1)
  18311. (/= (string-to-char (car x)) c))
  18312. nil
  18313. (cons (substring (car x) 1) (cdr x))))
  18314. custom))))
  18315. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18316. (message "Restriction is only possible in Org-mode buffers")
  18317. (ding) (sit-for 1))
  18318. ((eq c ?1)
  18319. (org-agenda-remove-restriction-lock 'noupdate)
  18320. (setq restriction 'buffer))
  18321. ((eq c ?0)
  18322. (org-agenda-remove-restriction-lock 'noupdate)
  18323. (setq restriction (if region-p 'region 'subtree)))
  18324. ((eq c ?<)
  18325. (org-agenda-remove-restriction-lock 'noupdate)
  18326. (setq restriction
  18327. (cond
  18328. ((eq restriction 'buffer)
  18329. (if region-p 'region 'subtree))
  18330. ((memq restriction '(subtree region))
  18331. nil)
  18332. (t 'buffer))))
  18333. ((eq c ?>)
  18334. (org-agenda-remove-restriction-lock 'noupdate)
  18335. (setq restriction nil))
  18336. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18337. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18338. ((and (> (length selstring) 0) (eq c ?\d))
  18339. (delete-window)
  18340. (org-agenda-get-restriction-and-command prefix-descriptions))
  18341. ((equal c ?q) (error "Abort"))
  18342. (t (error "Invalid key %c" c))))))))
  18343. (defun org-run-agenda-series (name series)
  18344. (org-prepare-agenda name)
  18345. (let* ((org-agenda-multi t)
  18346. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18347. (cmds (car series))
  18348. (gprops (nth 1 series))
  18349. match ;; The byte compiler incorrectly complains about this. Keep it!
  18350. cmd type lprops)
  18351. (while (setq cmd (pop cmds))
  18352. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18353. (cond
  18354. ((eq type 'agenda)
  18355. (org-let2 gprops lprops
  18356. '(call-interactively 'org-agenda-list)))
  18357. ((eq type 'alltodo)
  18358. (org-let2 gprops lprops
  18359. '(call-interactively 'org-todo-list)))
  18360. ((eq type 'stuck)
  18361. (org-let2 gprops lprops
  18362. '(call-interactively 'org-agenda-list-stuck-projects)))
  18363. ((eq type 'tags)
  18364. (org-let2 gprops lprops
  18365. '(org-tags-view current-prefix-arg match)))
  18366. ((eq type 'tags-todo)
  18367. (org-let2 gprops lprops
  18368. '(org-tags-view '(4) match)))
  18369. ((eq type 'todo)
  18370. (org-let2 gprops lprops
  18371. '(org-todo-list match)))
  18372. ((fboundp type)
  18373. (org-let2 gprops lprops
  18374. '(funcall type match)))
  18375. (t (error "Invalid type in command series"))))
  18376. (widen)
  18377. (setq org-agenda-redo-command redo)
  18378. (goto-char (point-min)))
  18379. (org-finalize-agenda))
  18380. ;;;###autoload
  18381. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18382. "Run an agenda command in batch mode and send the result to STDOUT.
  18383. If CMD-KEY is a string of length 1, it is used as a key in
  18384. `org-agenda-custom-commands' and triggers this command. If it is a
  18385. longer string it is used as a tags/todo match string.
  18386. Paramters are alternating variable names and values that will be bound
  18387. before running the agenda command."
  18388. (let (pars)
  18389. (while parameters
  18390. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18391. (if (> (length cmd-key) 2)
  18392. (eval (list 'let (nreverse pars)
  18393. (list 'org-tags-view nil cmd-key)))
  18394. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18395. (set-buffer org-agenda-buffer-name)
  18396. (princ (org-encode-for-stdout (buffer-string)))))
  18397. (defun org-encode-for-stdout (string)
  18398. (if (fboundp 'encode-coding-string)
  18399. (encode-coding-string string buffer-file-coding-system)
  18400. string))
  18401. (defvar org-agenda-info nil)
  18402. ;;;###autoload
  18403. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18404. "Run an agenda command in batch mode and send the result to STDOUT.
  18405. If CMD-KEY is a string of length 1, it is used as a key in
  18406. `org-agenda-custom-commands' and triggers this command. If it is a
  18407. longer string it is used as a tags/todo match string.
  18408. Paramters are alternating variable names and values that will be bound
  18409. before running the agenda command.
  18410. The output gives a line for each selected agenda item. Each
  18411. item is a list of comma-separated values, like this:
  18412. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18413. category The category of the item
  18414. head The headline, without TODO kwd, TAGS and PRIORITY
  18415. type The type of the agenda entry, can be
  18416. todo selected in TODO match
  18417. tagsmatch selected in tags match
  18418. diary imported from diary
  18419. deadline a deadline on given date
  18420. scheduled scheduled on given date
  18421. timestamp entry has timestamp on given date
  18422. closed entry was closed on given date
  18423. upcoming-deadline warning about deadline
  18424. past-scheduled forwarded scheduled item
  18425. block entry has date block including g. date
  18426. todo The todo keyword, if any
  18427. tags All tags including inherited ones, separated by colons
  18428. date The relevant date, like 2007-2-14
  18429. time The time, like 15:00-16:50
  18430. extra Sting with extra planning info
  18431. priority-l The priority letter if any was given
  18432. priority-n The computed numerical priority
  18433. agenda-day The day in the agenda where this is listed"
  18434. (let (pars)
  18435. (while parameters
  18436. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18437. (push (list 'org-agenda-remove-tags t) pars)
  18438. (if (> (length cmd-key) 2)
  18439. (eval (list 'let (nreverse pars)
  18440. (list 'org-tags-view nil cmd-key)))
  18441. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18442. (set-buffer org-agenda-buffer-name)
  18443. (let* ((lines (org-split-string (buffer-string) "\n"))
  18444. line)
  18445. (while (setq line (pop lines))
  18446. (catch 'next
  18447. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18448. (setq org-agenda-info
  18449. (org-fix-agenda-info (text-properties-at 0 line)))
  18450. (princ
  18451. (org-encode-for-stdout
  18452. (mapconcat 'org-agenda-export-csv-mapper
  18453. '(org-category txt type todo tags date time-of-day extra
  18454. priority-letter priority agenda-day)
  18455. ",")))
  18456. (princ "\n"))))))
  18457. (defun org-fix-agenda-info (props)
  18458. "Make sure all properties on an agenda item have a canonical form,
  18459. so the export commands can easily use it."
  18460. (let (tmp re)
  18461. (when (setq tmp (plist-get props 'tags))
  18462. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18463. (when (setq tmp (plist-get props 'date))
  18464. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18465. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18466. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18467. (setq tmp (calendar-date-string tmp)))
  18468. (setq props (plist-put props 'date tmp)))
  18469. (when (setq tmp (plist-get props 'day))
  18470. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18471. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18472. (setq tmp (calendar-date-string tmp)))
  18473. (setq props (plist-put props 'day tmp))
  18474. (setq props (plist-put props 'agenda-day tmp)))
  18475. (when (setq tmp (plist-get props 'txt))
  18476. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18477. (plist-put props 'priority-letter (match-string 1 tmp))
  18478. (setq tmp (replace-match "" t t tmp)))
  18479. (when (and (setq re (plist-get props 'org-todo-regexp))
  18480. (setq re (concat "\\`\\.*" re " ?"))
  18481. (string-match re tmp))
  18482. (plist-put props 'todo (match-string 1 tmp))
  18483. (setq tmp (replace-match "" t t tmp)))
  18484. (plist-put props 'txt tmp)))
  18485. props)
  18486. (defun org-agenda-export-csv-mapper (prop)
  18487. (let ((res (plist-get org-agenda-info prop)))
  18488. (setq res
  18489. (cond
  18490. ((not res) "")
  18491. ((stringp res) res)
  18492. (t (prin1-to-string res))))
  18493. (while (string-match "," res)
  18494. (setq res (replace-match ";" t t res)))
  18495. (org-trim res)))
  18496. ;;;###autoload
  18497. (defun org-store-agenda-views (&rest parameters)
  18498. (interactive)
  18499. (eval (list 'org-batch-store-agenda-views)))
  18500. ;; FIXME, why is this a macro?????
  18501. ;;;###autoload
  18502. (defmacro org-batch-store-agenda-views (&rest parameters)
  18503. "Run all custom agenda commands that have a file argument."
  18504. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18505. (pop-up-frames nil)
  18506. (dir default-directory)
  18507. pars cmd thiscmdkey files opts)
  18508. (while parameters
  18509. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18510. (setq pars (reverse pars))
  18511. (save-window-excursion
  18512. (while cmds
  18513. (setq cmd (pop cmds)
  18514. thiscmdkey (car cmd)
  18515. opts (nth 4 cmd)
  18516. files (nth 5 cmd))
  18517. (if (stringp files) (setq files (list files)))
  18518. (when files
  18519. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18520. (list 'org-agenda nil thiscmdkey)))
  18521. (set-buffer org-agenda-buffer-name)
  18522. (while files
  18523. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18524. (list 'org-write-agenda
  18525. (expand-file-name (pop files) dir) t))))
  18526. (and (get-buffer org-agenda-buffer-name)
  18527. (kill-buffer org-agenda-buffer-name)))))))
  18528. (defun org-write-agenda (file &optional nosettings)
  18529. "Write the current buffer (an agenda view) as a file.
  18530. Depending on the extension of the file name, plain text (.txt),
  18531. HTML (.html or .htm) or Postscript (.ps) is produced.
  18532. If NOSETTINGS is given, do not scope the settings of
  18533. `org-agenda-exporter-settings' into the export commands. This is used when
  18534. the settings have already been scoped and we do not wish to overrule other,
  18535. higher priority settings."
  18536. (interactive "FWrite agenda to file: ")
  18537. (if (not (file-writable-p file))
  18538. (error "Cannot write agenda to file %s" file))
  18539. (cond
  18540. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18541. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18542. (org-let (if nosettings nil org-agenda-exporter-settings)
  18543. '(save-excursion
  18544. (save-window-excursion
  18545. (cond
  18546. ((string-match "\\.html?\\'" file)
  18547. (set-buffer (htmlize-buffer (current-buffer)))
  18548. (when (and org-agenda-export-html-style
  18549. (string-match "<style>" org-agenda-export-html-style))
  18550. ;; replace <style> section with org-agenda-export-html-style
  18551. (goto-char (point-min))
  18552. (kill-region (- (search-forward "<style") 6)
  18553. (search-forward "</style>"))
  18554. (insert org-agenda-export-html-style))
  18555. (write-file file)
  18556. (kill-buffer (current-buffer))
  18557. (message "HTML written to %s" file))
  18558. ((string-match "\\.ps\\'" file)
  18559. (ps-print-buffer-with-faces file)
  18560. (message "Postscript written to %s" file))
  18561. (t
  18562. (let ((bs (buffer-string)))
  18563. (find-file file)
  18564. (insert bs)
  18565. (save-buffer 0)
  18566. (kill-buffer (current-buffer))
  18567. (message "Plain text written to %s" file))))))
  18568. (set-buffer org-agenda-buffer-name)))
  18569. (defmacro org-no-read-only (&rest body)
  18570. "Inhibit read-only for BODY."
  18571. `(let ((inhibit-read-only t)) ,@body))
  18572. (defun org-check-for-org-mode ()
  18573. "Make sure current buffer is in org-mode. Error if not."
  18574. (or (org-mode-p)
  18575. (error "Cannot execute org-mode agenda command on buffer in %s."
  18576. major-mode)))
  18577. (defun org-fit-agenda-window ()
  18578. "Fit the window to the buffer size."
  18579. (and (memq org-agenda-window-setup '(reorganize-frame))
  18580. (fboundp 'fit-window-to-buffer)
  18581. (fit-window-to-buffer
  18582. nil
  18583. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18584. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18585. ;;; Agenda file list
  18586. (defun org-agenda-files (&optional unrestricted)
  18587. "Get the list of agenda files.
  18588. Optional UNRESTRICTED means return the full list even if a restriction
  18589. is currently in place."
  18590. (let ((files
  18591. (cond
  18592. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18593. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18594. ((listp org-agenda-files) org-agenda-files)
  18595. (t (error "Invalid value of `org-agenda-files'")))))
  18596. (setq files (apply 'append
  18597. (mapcar (lambda (f)
  18598. (if (file-directory-p f)
  18599. (directory-files f t
  18600. org-agenda-file-regexp)
  18601. (list f)))
  18602. files)))
  18603. (if org-agenda-skip-unavailable-files
  18604. (delq nil
  18605. (mapcar (function
  18606. (lambda (file)
  18607. (and (file-readable-p file) file)))
  18608. files))
  18609. files))) ; `org-check-agenda-file' will remove them from the list
  18610. (defun org-edit-agenda-file-list ()
  18611. "Edit the list of agenda files.
  18612. Depending on setup, this either uses customize to edit the variable
  18613. `org-agenda-files', or it visits the file that is holding the list. In the
  18614. latter case, the buffer is set up in a way that saving it automatically kills
  18615. the buffer and restores the previous window configuration."
  18616. (interactive)
  18617. (if (stringp org-agenda-files)
  18618. (let ((cw (current-window-configuration)))
  18619. (find-file org-agenda-files)
  18620. (org-set-local 'org-window-configuration cw)
  18621. (org-add-hook 'after-save-hook
  18622. (lambda ()
  18623. (set-window-configuration
  18624. (prog1 org-window-configuration
  18625. (kill-buffer (current-buffer))))
  18626. (org-install-agenda-files-menu)
  18627. (message "New agenda file list installed"))
  18628. nil 'local)
  18629. (message "%s" (substitute-command-keys
  18630. "Edit list and finish with \\[save-buffer]")))
  18631. (customize-variable 'org-agenda-files)))
  18632. (defun org-store-new-agenda-file-list (list)
  18633. "Set new value for the agenda file list and save it correcly."
  18634. (if (stringp org-agenda-files)
  18635. (let ((f org-agenda-files) b)
  18636. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18637. (with-temp-file f
  18638. (insert (mapconcat 'identity list "\n") "\n")))
  18639. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18640. (setq org-agenda-files list)
  18641. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18642. (defun org-read-agenda-file-list ()
  18643. "Read the list of agenda files from a file."
  18644. (when (stringp org-agenda-files)
  18645. (with-temp-buffer
  18646. (insert-file-contents org-agenda-files)
  18647. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18648. ;;;###autoload
  18649. (defun org-cycle-agenda-files ()
  18650. "Cycle through the files in `org-agenda-files'.
  18651. If the current buffer visits an agenda file, find the next one in the list.
  18652. If the current buffer does not, find the first agenda file."
  18653. (interactive)
  18654. (let* ((fs (org-agenda-files t))
  18655. (files (append fs (list (car fs))))
  18656. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18657. file)
  18658. (unless files (error "No agenda files"))
  18659. (catch 'exit
  18660. (while (setq file (pop files))
  18661. (if (equal (file-truename file) tcf)
  18662. (when (car files)
  18663. (find-file (car files))
  18664. (throw 'exit t))))
  18665. (find-file (car fs)))
  18666. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18667. (defun org-agenda-file-to-front (&optional to-end)
  18668. "Move/add the current file to the top of the agenda file list.
  18669. If the file is not present in the list, it is added to the front. If it is
  18670. present, it is moved there. With optional argument TO-END, add/move to the
  18671. end of the list."
  18672. (interactive "P")
  18673. (let ((org-agenda-skip-unavailable-files nil)
  18674. (file-alist (mapcar (lambda (x)
  18675. (cons (file-truename x) x))
  18676. (org-agenda-files t)))
  18677. (ctf (file-truename buffer-file-name))
  18678. x had)
  18679. (setq x (assoc ctf file-alist) had x)
  18680. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18681. (if to-end
  18682. (setq file-alist (append (delq x file-alist) (list x)))
  18683. (setq file-alist (cons x (delq x file-alist))))
  18684. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18685. (org-install-agenda-files-menu)
  18686. (message "File %s to %s of agenda file list"
  18687. (if had "moved" "added") (if to-end "end" "front"))))
  18688. (defun org-remove-file (&optional file)
  18689. "Remove current file from the list of files in variable `org-agenda-files'.
  18690. These are the files which are being checked for agenda entries.
  18691. Optional argument FILE means, use this file instead of the current."
  18692. (interactive)
  18693. (let* ((org-agenda-skip-unavailable-files nil)
  18694. (file (or file buffer-file-name))
  18695. (true-file (file-truename file))
  18696. (afile (abbreviate-file-name file))
  18697. (files (delq nil (mapcar
  18698. (lambda (x)
  18699. (if (equal true-file
  18700. (file-truename x))
  18701. nil x))
  18702. (org-agenda-files t)))))
  18703. (if (not (= (length files) (length (org-agenda-files t))))
  18704. (progn
  18705. (org-store-new-agenda-file-list files)
  18706. (org-install-agenda-files-menu)
  18707. (message "Removed file: %s" afile))
  18708. (message "File was not in list: %s (not removed)" afile))))
  18709. (defun org-file-menu-entry (file)
  18710. (vector file (list 'find-file file) t))
  18711. (defun org-check-agenda-file (file)
  18712. "Make sure FILE exists. If not, ask user what to do."
  18713. (when (not (file-exists-p file))
  18714. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18715. (abbreviate-file-name file))
  18716. (let ((r (downcase (read-char-exclusive))))
  18717. (cond
  18718. ((equal r ?r)
  18719. (org-remove-file file)
  18720. (throw 'nextfile t))
  18721. (t (error "Abort"))))))
  18722. ;;; Agenda prepare and finalize
  18723. (defvar org-agenda-multi nil) ; dynammically scoped
  18724. (defvar org-agenda-buffer-name "*Org Agenda*")
  18725. (defvar org-pre-agenda-window-conf nil)
  18726. (defvar org-agenda-name nil)
  18727. (defun org-prepare-agenda (&optional name)
  18728. (setq org-todo-keywords-for-agenda nil)
  18729. (setq org-done-keywords-for-agenda nil)
  18730. (if org-agenda-multi
  18731. (progn
  18732. (setq buffer-read-only nil)
  18733. (goto-char (point-max))
  18734. (unless (or (bobp) org-agenda-compact-blocks)
  18735. (insert "\n" (make-string (window-width) ?=) "\n"))
  18736. (narrow-to-region (point) (point-max)))
  18737. (org-agenda-reset-markers)
  18738. (org-prepare-agenda-buffers (org-agenda-files))
  18739. (setq org-todo-keywords-for-agenda
  18740. (org-uniquify org-todo-keywords-for-agenda))
  18741. (setq org-done-keywords-for-agenda
  18742. (org-uniquify org-done-keywords-for-agenda))
  18743. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18744. (awin (get-buffer-window abuf)))
  18745. (cond
  18746. ((equal (current-buffer) abuf) nil)
  18747. (awin (select-window awin))
  18748. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18749. ((equal org-agenda-window-setup 'current-window)
  18750. (switch-to-buffer abuf))
  18751. ((equal org-agenda-window-setup 'other-window)
  18752. (org-switch-to-buffer-other-window abuf))
  18753. ((equal org-agenda-window-setup 'other-frame)
  18754. (switch-to-buffer-other-frame abuf))
  18755. ((equal org-agenda-window-setup 'reorganize-frame)
  18756. (delete-other-windows)
  18757. (org-switch-to-buffer-other-window abuf))))
  18758. (setq buffer-read-only nil)
  18759. (erase-buffer)
  18760. (org-agenda-mode)
  18761. (and name (not org-agenda-name)
  18762. (org-set-local 'org-agenda-name name)))
  18763. (setq buffer-read-only nil))
  18764. (defun org-finalize-agenda ()
  18765. "Finishing touch for the agenda buffer, called just before displaying it."
  18766. (unless org-agenda-multi
  18767. (save-excursion
  18768. (let ((inhibit-read-only t))
  18769. (goto-char (point-min))
  18770. (while (org-activate-bracket-links (point-max))
  18771. (add-text-properties (match-beginning 0) (match-end 0)
  18772. '(face org-link)))
  18773. (org-agenda-align-tags)
  18774. (unless org-agenda-with-colors
  18775. (remove-text-properties (point-min) (point-max) '(face nil))))
  18776. (if (and (boundp 'org-overriding-columns-format)
  18777. org-overriding-columns-format)
  18778. (org-set-local 'org-overriding-columns-format
  18779. org-overriding-columns-format))
  18780. (if (and (boundp 'org-agenda-view-columns-initially)
  18781. org-agenda-view-columns-initially)
  18782. (org-agenda-columns))
  18783. (when org-agenda-fontify-priorities
  18784. (org-fontify-priorities))
  18785. (run-hooks 'org-finalize-agenda-hook)
  18786. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18787. )))
  18788. (defun org-fontify-priorities ()
  18789. "Make highest priority lines bold, and lowest italic."
  18790. (interactive)
  18791. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18792. (org-delete-overlay o)))
  18793. (org-overlays-in (point-min) (point-max)))
  18794. (save-excursion
  18795. (let ((inhibit-read-only t)
  18796. b e p ov h l)
  18797. (goto-char (point-min))
  18798. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18799. (setq h (or (get-char-property (point) 'org-highest-priority)
  18800. org-highest-priority)
  18801. l (or (get-char-property (point) 'org-lowest-priority)
  18802. org-lowest-priority)
  18803. p (string-to-char (match-string 1))
  18804. b (match-beginning 0) e (point-at-eol)
  18805. ov (org-make-overlay b e))
  18806. (org-overlay-put
  18807. ov 'face
  18808. (cond ((listp org-agenda-fontify-priorities)
  18809. (cdr (assoc p org-agenda-fontify-priorities)))
  18810. ((equal p l) 'italic)
  18811. ((equal p h) 'bold)))
  18812. (org-overlay-put ov 'org-type 'org-priority)))))
  18813. (defun org-prepare-agenda-buffers (files)
  18814. "Create buffers for all agenda files, protect archived trees and comments."
  18815. (interactive)
  18816. (let ((pa '(:org-archived t))
  18817. (pc '(:org-comment t))
  18818. (pall '(:org-archived t :org-comment t))
  18819. (inhibit-read-only t)
  18820. (rea (concat ":" org-archive-tag ":"))
  18821. bmp file re)
  18822. (save-excursion
  18823. (save-restriction
  18824. (while (setq file (pop files))
  18825. (if (bufferp file)
  18826. (set-buffer file)
  18827. (org-check-agenda-file file)
  18828. (set-buffer (org-get-agenda-file-buffer file)))
  18829. (widen)
  18830. (setq bmp (buffer-modified-p))
  18831. (org-refresh-category-properties)
  18832. (setq org-todo-keywords-for-agenda
  18833. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18834. (setq org-done-keywords-for-agenda
  18835. (append org-done-keywords-for-agenda org-done-keywords))
  18836. (save-excursion
  18837. (remove-text-properties (point-min) (point-max) pall)
  18838. (when org-agenda-skip-archived-trees
  18839. (goto-char (point-min))
  18840. (while (re-search-forward rea nil t)
  18841. (if (org-on-heading-p t)
  18842. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18843. (goto-char (point-min))
  18844. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18845. (while (re-search-forward re nil t)
  18846. (add-text-properties
  18847. (match-beginning 0) (org-end-of-subtree t) pc)))
  18848. (set-buffer-modified-p bmp))))))
  18849. (defvar org-agenda-skip-function nil
  18850. "Function to be called at each match during agenda construction.
  18851. If this function returns nil, the current match should not be skipped.
  18852. Otherwise, the function must return a position from where the search
  18853. should be continued.
  18854. This may also be a Lisp form, it will be evaluated.
  18855. Never set this variable using `setq' or so, because then it will apply
  18856. to all future agenda commands. Instead, bind it with `let' to scope
  18857. it dynamically into the agenda-constructing command. A good way to set
  18858. it is through options in org-agenda-custom-commands.")
  18859. (defun org-agenda-skip ()
  18860. "Throw to `:skip' in places that should be skipped.
  18861. Also moves point to the end of the skipped region, so that search can
  18862. continue from there."
  18863. (let ((p (point-at-bol)) to fp)
  18864. (and org-agenda-skip-archived-trees
  18865. (get-text-property p :org-archived)
  18866. (org-end-of-subtree t)
  18867. (throw :skip t))
  18868. (and (get-text-property p :org-comment)
  18869. (org-end-of-subtree t)
  18870. (throw :skip t))
  18871. (if (equal (char-after p) ?#) (throw :skip t))
  18872. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18873. (consp org-agenda-skip-function))
  18874. (setq to (save-excursion
  18875. (save-match-data
  18876. (if fp
  18877. (funcall org-agenda-skip-function)
  18878. (eval org-agenda-skip-function))))))
  18879. (goto-char to)
  18880. (throw :skip t))))
  18881. (defvar org-agenda-markers nil
  18882. "List of all currently active markers created by `org-agenda'.")
  18883. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18884. "Creation time of the last agenda marker.")
  18885. (defun org-agenda-new-marker (&optional pos)
  18886. "Return a new agenda marker.
  18887. Org-mode keeps a list of these markers and resets them when they are
  18888. no longer in use."
  18889. (let ((m (copy-marker (or pos (point)))))
  18890. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18891. (push m org-agenda-markers)
  18892. m))
  18893. (defun org-agenda-reset-markers ()
  18894. "Reset markers created by `org-agenda'."
  18895. (while org-agenda-markers
  18896. (move-marker (pop org-agenda-markers) nil)))
  18897. (defun org-get-agenda-file-buffer (file)
  18898. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18899. it to the list of buffers which might be released later."
  18900. (let ((buf (org-find-base-buffer-visiting file)))
  18901. (if buf
  18902. buf ; just return it
  18903. ;; Make a new buffer and remember it
  18904. (setq buf (find-file-noselect file))
  18905. (if buf (push buf org-agenda-new-buffers))
  18906. buf)))
  18907. (defun org-release-buffers (blist)
  18908. "Release all buffers in list, asking the user for confirmation when needed.
  18909. When a buffer is unmodified, it is just killed. When modified, it is saved
  18910. \(if the user agrees) and then killed."
  18911. (let (buf file)
  18912. (while (setq buf (pop blist))
  18913. (setq file (buffer-file-name buf))
  18914. (when (and (buffer-modified-p buf)
  18915. file
  18916. (y-or-n-p (format "Save file %s? " file)))
  18917. (with-current-buffer buf (save-buffer)))
  18918. (kill-buffer buf))))
  18919. (defun org-get-category (&optional pos)
  18920. "Get the category applying to position POS."
  18921. (get-text-property (or pos (point)) 'org-category))
  18922. ;;; Agenda timeline
  18923. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18924. (defun org-timeline (&optional include-all)
  18925. "Show a time-sorted view of the entries in the current org file.
  18926. Only entries with a time stamp of today or later will be listed. With
  18927. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18928. under the current date.
  18929. If the buffer contains an active region, only check the region for
  18930. dates."
  18931. (interactive "P")
  18932. (require 'calendar)
  18933. (org-compile-prefix-format 'timeline)
  18934. (org-set-sorting-strategy 'timeline)
  18935. (let* ((dopast t)
  18936. (dotodo include-all)
  18937. (doclosed org-agenda-show-log)
  18938. (entry buffer-file-name)
  18939. (date (calendar-current-date))
  18940. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18941. (end (if (org-region-active-p) (region-end) (point-max)))
  18942. (day-numbers (org-get-all-dates beg end 'no-ranges
  18943. t doclosed ; always include today
  18944. org-timeline-show-empty-dates))
  18945. (org-deadline-warning-days 0)
  18946. (org-agenda-only-exact-dates t)
  18947. (today (time-to-days (current-time)))
  18948. (past t)
  18949. args
  18950. s e rtn d emptyp)
  18951. (setq org-agenda-redo-command
  18952. (list 'progn
  18953. (list 'org-switch-to-buffer-other-window (current-buffer))
  18954. (list 'org-timeline (list 'quote include-all))))
  18955. (if (not dopast)
  18956. ;; Remove past dates from the list of dates.
  18957. (setq day-numbers (delq nil (mapcar (lambda(x)
  18958. (if (>= x today) x nil))
  18959. day-numbers))))
  18960. (org-prepare-agenda (concat "Timeline "
  18961. (file-name-nondirectory buffer-file-name)))
  18962. (if doclosed (push :closed args))
  18963. (push :timestamp args)
  18964. (push :deadline args)
  18965. (push :scheduled args)
  18966. (push :sexp args)
  18967. (if dotodo (push :todo args))
  18968. (while (setq d (pop day-numbers))
  18969. (if (and (listp d) (eq (car d) :omitted))
  18970. (progn
  18971. (setq s (point))
  18972. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18973. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18974. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18975. (if (and (>= d today)
  18976. dopast
  18977. past)
  18978. (progn
  18979. (setq past nil)
  18980. (insert (make-string 79 ?-) "\n")))
  18981. (setq date (calendar-gregorian-from-absolute d))
  18982. (setq s (point))
  18983. (setq rtn (and (not emptyp)
  18984. (apply 'org-agenda-get-day-entries entry
  18985. date args)))
  18986. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18987. (progn
  18988. (insert
  18989. (if (stringp org-agenda-format-date)
  18990. (format-time-string org-agenda-format-date
  18991. (org-time-from-absolute date))
  18992. (funcall org-agenda-format-date date))
  18993. "\n")
  18994. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18995. (put-text-property s (1- (point)) 'org-date-line t)
  18996. (if (equal d today)
  18997. (put-text-property s (1- (point)) 'org-today t))
  18998. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18999. (put-text-property s (1- (point)) 'day d)))))
  19000. (goto-char (point-min))
  19001. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19002. (point-min)))
  19003. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19004. (org-finalize-agenda)
  19005. (setq buffer-read-only t)))
  19006. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19007. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19008. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19009. not every single day in the range. If FORCE-TODAY is non-nil, make
  19010. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19011. inactive time stamps (those in square brackets) are included.
  19012. When EMPTY is non-nil, also include days without any entries."
  19013. (let ((re (concat
  19014. (if pre-re pre-re "")
  19015. (if inactive org-ts-regexp-both org-ts-regexp)))
  19016. dates dates1 date day day1 day2 ts1 ts2)
  19017. (if force-today
  19018. (setq dates (list (time-to-days (current-time)))))
  19019. (save-excursion
  19020. (goto-char beg)
  19021. (while (re-search-forward re end t)
  19022. (setq day (time-to-days (org-time-string-to-time
  19023. (substring (match-string 1) 0 10))))
  19024. (or (memq day dates) (push day dates)))
  19025. (unless no-ranges
  19026. (goto-char beg)
  19027. (while (re-search-forward org-tr-regexp end t)
  19028. (setq ts1 (substring (match-string 1) 0 10)
  19029. ts2 (substring (match-string 2) 0 10)
  19030. day1 (time-to-days (org-time-string-to-time ts1))
  19031. day2 (time-to-days (org-time-string-to-time ts2)))
  19032. (while (< (setq day1 (1+ day1)) day2)
  19033. (or (memq day1 dates) (push day1 dates)))))
  19034. (setq dates (sort dates '<))
  19035. (when empty
  19036. (while (setq day (pop dates))
  19037. (setq day2 (car dates))
  19038. (push day dates1)
  19039. (when (and day2 empty)
  19040. (if (or (eq empty t)
  19041. (and (numberp empty) (<= (- day2 day) empty)))
  19042. (while (< (setq day (1+ day)) day2)
  19043. (push (list day) dates1))
  19044. (push (cons :omitted (- day2 day)) dates1))))
  19045. (setq dates (nreverse dates1)))
  19046. dates)))
  19047. ;;; Agenda Daily/Weekly
  19048. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19049. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19050. (defvar org-agenda-last-arguments nil
  19051. "The arguments of the previous call to org-agenda")
  19052. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19053. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19054. (defvar org-include-all-loc nil) ; local variable
  19055. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19056. ;;;###autoload
  19057. (defun org-agenda-list (&optional include-all start-day ndays)
  19058. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19059. The view will be for the current day or week, but from the overview buffer
  19060. you will be able to go to other days/weeks.
  19061. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19062. all unfinished TODO items will also be shown, before the agenda.
  19063. This feature is considered obsolete, please use the TODO list or a block
  19064. agenda instead.
  19065. With a numeric prefix argument in an interactive call, the agenda will
  19066. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19067. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19068. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19069. given in `org-agenda-start-on-weekday'."
  19070. (interactive "P")
  19071. (if (and (integerp include-all) (> include-all 0))
  19072. (setq ndays include-all include-all nil))
  19073. (setq ndays (or ndays org-agenda-ndays)
  19074. start-day (or start-day org-agenda-start-day))
  19075. (if org-agenda-overriding-arguments
  19076. (setq include-all (car org-agenda-overriding-arguments)
  19077. start-day (nth 1 org-agenda-overriding-arguments)
  19078. ndays (nth 2 org-agenda-overriding-arguments)))
  19079. (if (stringp start-day)
  19080. ;; Convert to an absolute day number
  19081. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19082. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19083. (org-compile-prefix-format 'agenda)
  19084. (org-set-sorting-strategy 'agenda)
  19085. (require 'calendar)
  19086. (let* ((org-agenda-start-on-weekday
  19087. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19088. org-agenda-start-on-weekday nil))
  19089. (thefiles (org-agenda-files))
  19090. (files thefiles)
  19091. (today (time-to-days
  19092. (time-subtract (current-time)
  19093. (list 0 (* 3600 org-extend-today-until) 0))))
  19094. (sd (or start-day today))
  19095. (start (if (or (null org-agenda-start-on-weekday)
  19096. (< org-agenda-ndays 7))
  19097. sd
  19098. (let* ((nt (calendar-day-of-week
  19099. (calendar-gregorian-from-absolute sd)))
  19100. (n1 org-agenda-start-on-weekday)
  19101. (d (- nt n1)))
  19102. (- sd (+ (if (< d 0) 7 0) d)))))
  19103. (day-numbers (list start))
  19104. (day-cnt 0)
  19105. (inhibit-redisplay (not debug-on-error))
  19106. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19107. (setq org-agenda-redo-command
  19108. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19109. ;; Make the list of days
  19110. (setq ndays (or ndays org-agenda-ndays)
  19111. nd ndays)
  19112. (while (> ndays 1)
  19113. (push (1+ (car day-numbers)) day-numbers)
  19114. (setq ndays (1- ndays)))
  19115. (setq day-numbers (nreverse day-numbers))
  19116. (org-prepare-agenda "Day/Week")
  19117. (org-set-local 'org-starting-day (car day-numbers))
  19118. (org-set-local 'org-include-all-loc include-all)
  19119. (org-set-local 'org-agenda-span
  19120. (org-agenda-ndays-to-span nd))
  19121. (when (and (or include-all org-agenda-include-all-todo)
  19122. (member today day-numbers))
  19123. (setq files thefiles
  19124. rtnall nil)
  19125. (while (setq file (pop files))
  19126. (catch 'nextfile
  19127. (org-check-agenda-file file)
  19128. (setq date (calendar-gregorian-from-absolute today)
  19129. rtn (org-agenda-get-day-entries
  19130. file date :todo))
  19131. (setq rtnall (append rtnall rtn))))
  19132. (when rtnall
  19133. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19134. (add-text-properties (point-min) (1- (point))
  19135. (list 'face 'org-agenda-structure))
  19136. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19137. (unless org-agenda-compact-blocks
  19138. (setq s (point))
  19139. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19140. "-agenda:\n")
  19141. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19142. 'org-date-line t)))
  19143. (while (setq d (pop day-numbers))
  19144. (setq date (calendar-gregorian-from-absolute d)
  19145. s (point))
  19146. (if (or (setq todayp (= d today))
  19147. (and (not start-pos) (= d sd)))
  19148. (setq start-pos (point))
  19149. (if (and start-pos (not end-pos))
  19150. (setq end-pos (point))))
  19151. (setq files thefiles
  19152. rtnall nil)
  19153. (while (setq file (pop files))
  19154. (catch 'nextfile
  19155. (org-check-agenda-file file)
  19156. (if org-agenda-show-log
  19157. (setq rtn (org-agenda-get-day-entries
  19158. file date
  19159. :deadline :scheduled :timestamp :sexp :closed))
  19160. (setq rtn (org-agenda-get-day-entries
  19161. file date
  19162. :deadline :scheduled :sexp :timestamp)))
  19163. (setq rtnall (append rtnall rtn))))
  19164. (if org-agenda-include-diary
  19165. (progn
  19166. (require 'diary-lib)
  19167. (setq rtn (org-get-entries-from-diary date))
  19168. (setq rtnall (append rtnall rtn))))
  19169. (if (or rtnall org-agenda-show-all-dates)
  19170. (progn
  19171. (setq day-cnt (1+ day-cnt))
  19172. (insert
  19173. (if (stringp org-agenda-format-date)
  19174. (format-time-string org-agenda-format-date
  19175. (org-time-from-absolute date))
  19176. (funcall org-agenda-format-date date))
  19177. "\n")
  19178. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19179. (put-text-property s (1- (point)) 'org-date-line t)
  19180. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19181. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19182. (if rtnall (insert
  19183. (org-finalize-agenda-entries
  19184. (org-agenda-add-time-grid-maybe
  19185. rtnall nd todayp))
  19186. "\n"))
  19187. (put-text-property s (1- (point)) 'day d)
  19188. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19189. (goto-char (point-min))
  19190. (org-fit-agenda-window)
  19191. (unless (and (pos-visible-in-window-p (point-min))
  19192. (pos-visible-in-window-p (point-max)))
  19193. (goto-char (1- (point-max)))
  19194. (recenter -1)
  19195. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19196. (progn
  19197. (goto-char (or start-pos 1))
  19198. (recenter 1))))
  19199. (goto-char (or start-pos 1))
  19200. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19201. (org-finalize-agenda)
  19202. (setq buffer-read-only t)
  19203. (message "")))
  19204. (defun org-agenda-ndays-to-span (n)
  19205. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19206. ;;; Agenda TODO list
  19207. (defvar org-select-this-todo-keyword nil)
  19208. (defvar org-last-arg nil)
  19209. ;;;###autoload
  19210. (defun org-todo-list (arg)
  19211. "Show all TODO entries from all agenda file in a single list.
  19212. The prefix arg can be used to select a specific TODO keyword and limit
  19213. the list to these. When using \\[universal-argument], you will be prompted
  19214. for a keyword. A numeric prefix directly selects the Nth keyword in
  19215. `org-todo-keywords-1'."
  19216. (interactive "P")
  19217. (require 'calendar)
  19218. (org-compile-prefix-format 'todo)
  19219. (org-set-sorting-strategy 'todo)
  19220. (org-prepare-agenda "TODO")
  19221. (let* ((today (time-to-days (current-time)))
  19222. (date (calendar-gregorian-from-absolute today))
  19223. (kwds org-todo-keywords-for-agenda)
  19224. (completion-ignore-case t)
  19225. (org-select-this-todo-keyword
  19226. (if (stringp arg) arg
  19227. (and arg (integerp arg) (> arg 0)
  19228. (nth (1- arg) kwds))))
  19229. rtn rtnall files file pos)
  19230. (when (equal arg '(4))
  19231. (setq org-select-this-todo-keyword
  19232. (completing-read "Keyword (or KWD1|K2D2|...): "
  19233. (mapcar 'list kwds) nil nil)))
  19234. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19235. (org-set-local 'org-last-arg arg)
  19236. (setq org-agenda-redo-command
  19237. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19238. (setq files (org-agenda-files)
  19239. rtnall nil)
  19240. (while (setq file (pop files))
  19241. (catch 'nextfile
  19242. (org-check-agenda-file file)
  19243. (setq rtn (org-agenda-get-day-entries file date :todo))
  19244. (setq rtnall (append rtnall rtn))))
  19245. (if org-agenda-overriding-header
  19246. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19247. nil 'face 'org-agenda-structure) "\n")
  19248. (insert "Global list of TODO items of type: ")
  19249. (add-text-properties (point-min) (1- (point))
  19250. (list 'face 'org-agenda-structure))
  19251. (setq pos (point))
  19252. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19253. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19254. (setq pos (point))
  19255. (unless org-agenda-multi
  19256. (insert "Available with `N r': (0)ALL")
  19257. (let ((n 0) s)
  19258. (mapc (lambda (x)
  19259. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19260. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19261. (insert "\n "))
  19262. (insert " " s))
  19263. kwds))
  19264. (insert "\n"))
  19265. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19266. (when rtnall
  19267. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19268. (goto-char (point-min))
  19269. (org-fit-agenda-window)
  19270. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19271. (org-finalize-agenda)
  19272. (setq buffer-read-only t)))
  19273. ;;; Agenda tags match
  19274. ;;;###autoload
  19275. (defun org-tags-view (&optional todo-only match)
  19276. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19277. The prefix arg TODO-ONLY limits the search to TODO entries."
  19278. (interactive "P")
  19279. (org-compile-prefix-format 'tags)
  19280. (org-set-sorting-strategy 'tags)
  19281. (let* ((org-tags-match-list-sublevels
  19282. (if todo-only t org-tags-match-list-sublevels))
  19283. (completion-ignore-case t)
  19284. rtn rtnall files file pos matcher
  19285. buffer)
  19286. (setq matcher (org-make-tags-matcher match)
  19287. match (car matcher) matcher (cdr matcher))
  19288. (org-prepare-agenda (concat "TAGS " match))
  19289. (setq org-agenda-redo-command
  19290. (list 'org-tags-view (list 'quote todo-only)
  19291. (list 'if 'current-prefix-arg nil match)))
  19292. (setq files (org-agenda-files)
  19293. rtnall nil)
  19294. (while (setq file (pop files))
  19295. (catch 'nextfile
  19296. (org-check-agenda-file file)
  19297. (setq buffer (if (file-exists-p file)
  19298. (org-get-agenda-file-buffer file)
  19299. (error "No such file %s" file)))
  19300. (if (not buffer)
  19301. ;; If file does not exist, merror message to agenda
  19302. (setq rtn (list
  19303. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19304. rtnall (append rtnall rtn))
  19305. (with-current-buffer buffer
  19306. (unless (org-mode-p)
  19307. (error "Agenda file %s is not in `org-mode'" file))
  19308. (save-excursion
  19309. (save-restriction
  19310. (if org-agenda-restrict
  19311. (narrow-to-region org-agenda-restrict-begin
  19312. org-agenda-restrict-end)
  19313. (widen))
  19314. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19315. (setq rtnall (append rtnall rtn))))))))
  19316. (if org-agenda-overriding-header
  19317. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19318. nil 'face 'org-agenda-structure) "\n")
  19319. (insert "Headlines with TAGS match: ")
  19320. (add-text-properties (point-min) (1- (point))
  19321. (list 'face 'org-agenda-structure))
  19322. (setq pos (point))
  19323. (insert match "\n")
  19324. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19325. (setq pos (point))
  19326. (unless org-agenda-multi
  19327. (insert "Press `C-u r' to search again with new search string\n"))
  19328. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19329. (when rtnall
  19330. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19331. (goto-char (point-min))
  19332. (org-fit-agenda-window)
  19333. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19334. (org-finalize-agenda)
  19335. (setq buffer-read-only t)))
  19336. ;;; Agenda Finding stuck projects
  19337. (defvar org-agenda-skip-regexp nil
  19338. "Regular expression used in skipping subtrees for the agenda.
  19339. This is basically a temporary global variable that can be set and then
  19340. used by user-defined selections using `org-agenda-skip-function'.")
  19341. (defvar org-agenda-overriding-header nil
  19342. "When this is set during todo and tags searches, will replace header.")
  19343. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19344. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19345. If yes, it returns the end position of this tree, causing agenda commands
  19346. to skip this subtree. This is a function that can be put into
  19347. `org-agenda-skip-function' for the duration of a command."
  19348. (let ((end (save-excursion (org-end-of-subtree t)))
  19349. skip)
  19350. (save-excursion
  19351. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19352. (and skip end)))
  19353. (defun org-agenda-skip-entry-if (&rest conditions)
  19354. "Skip entry if any of CONDITIONS is true.
  19355. See `org-agenda-skip-if' for details."
  19356. (org-agenda-skip-if nil conditions))
  19357. (defun org-agenda-skip-subtree-if (&rest conditions)
  19358. "Skip entry if any of CONDITIONS is true.
  19359. See `org-agenda-skip-if' for details."
  19360. (org-agenda-skip-if t conditions))
  19361. (defun org-agenda-skip-if (subtree conditions)
  19362. "Checks current entity for CONDITIONS.
  19363. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19364. the entry, i.e. the text before the next heading is checked.
  19365. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19366. from different tests. Valid conditions are:
  19367. scheduled Check if there is a scheduled cookie
  19368. notscheduled Check if there is no scheduled cookie
  19369. deadline Check if there is a deadline
  19370. notdeadline Check if there is no deadline
  19371. regexp Check if regexp matches
  19372. notregexp Check if regexp does not match.
  19373. The regexp is taken from the conditions list, it must come right after
  19374. the `regexp' or `notregexp' element.
  19375. If any of these conditions is met, this function returns the end point of
  19376. the entity, causing the search to continue from there. This is a function
  19377. that can be put into `org-agenda-skip-function' for the duration of a command."
  19378. (let (beg end m)
  19379. (org-back-to-heading t)
  19380. (setq beg (point)
  19381. end (if subtree
  19382. (progn (org-end-of-subtree t) (point))
  19383. (progn (outline-next-heading) (1- (point)))))
  19384. (goto-char beg)
  19385. (and
  19386. (or
  19387. (and (memq 'scheduled conditions)
  19388. (re-search-forward org-scheduled-time-regexp end t))
  19389. (and (memq 'notscheduled conditions)
  19390. (not (re-search-forward org-scheduled-time-regexp end t)))
  19391. (and (memq 'deadline conditions)
  19392. (re-search-forward org-deadline-time-regexp end t))
  19393. (and (memq 'notdeadline conditions)
  19394. (not (re-search-forward org-deadline-time-regexp end t)))
  19395. (and (setq m (memq 'regexp conditions))
  19396. (stringp (nth 1 m))
  19397. (re-search-forward (nth 1 m) end t))
  19398. (and (setq m (memq 'notregexp conditions))
  19399. (stringp (nth 1 m))
  19400. (not (re-search-forward (nth 1 m) end t))))
  19401. end)))
  19402. ;;;###autoload
  19403. (defun org-agenda-list-stuck-projects (&rest ignore)
  19404. "Create agenda view for projects that are stuck.
  19405. Stuck projects are project that have no next actions. For the definitions
  19406. of what a project is and how to check if it stuck, customize the variable
  19407. `org-stuck-projects'.
  19408. MATCH is being ignored."
  19409. (interactive)
  19410. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19411. ;; FIXME: we could have used org-agenda-skip-if here.
  19412. (org-agenda-overriding-header "List of stuck projects: ")
  19413. (matcher (nth 0 org-stuck-projects))
  19414. (todo (nth 1 org-stuck-projects))
  19415. (todo-wds (if (member "*" todo)
  19416. (progn
  19417. (org-prepare-agenda-buffers (org-agenda-files))
  19418. (org-delete-all
  19419. org-done-keywords-for-agenda
  19420. (copy-sequence org-todo-keywords-for-agenda)))
  19421. todo))
  19422. (todo-re (concat "^\\*+[ \t]+\\("
  19423. (mapconcat 'identity todo-wds "\\|")
  19424. "\\)\\>"))
  19425. (tags (nth 2 org-stuck-projects))
  19426. (tags-re (if (member "*" tags)
  19427. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19428. (concat "^\\*+ .*:\\("
  19429. (mapconcat 'identity tags "\\|")
  19430. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19431. (gen-re (nth 3 org-stuck-projects))
  19432. (re-list
  19433. (delq nil
  19434. (list
  19435. (if todo todo-re)
  19436. (if tags tags-re)
  19437. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19438. gen-re)))))
  19439. (setq org-agenda-skip-regexp
  19440. (if re-list
  19441. (mapconcat 'identity re-list "\\|")
  19442. (error "No information how to identify unstuck projects")))
  19443. (org-tags-view nil matcher)
  19444. (with-current-buffer org-agenda-buffer-name
  19445. (setq org-agenda-redo-command
  19446. '(org-agenda-list-stuck-projects
  19447. (or current-prefix-arg org-last-arg))))))
  19448. ;;; Diary integration
  19449. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19450. (defun org-get-entries-from-diary (date)
  19451. "Get the (Emacs Calendar) diary entries for DATE."
  19452. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19453. (diary-display-hook '(fancy-diary-display))
  19454. (pop-up-frames nil)
  19455. (list-diary-entries-hook
  19456. (cons 'org-diary-default-entry list-diary-entries-hook))
  19457. (diary-file-name-prefix-function nil) ; turn this feature off
  19458. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19459. entries
  19460. (org-disable-agenda-to-diary t))
  19461. (save-excursion
  19462. (save-window-excursion
  19463. (funcall (if (fboundp 'diary-list-entries)
  19464. 'diary-list-entries 'list-diary-entries)
  19465. date 1)))
  19466. (if (not (get-buffer fancy-diary-buffer))
  19467. (setq entries nil)
  19468. (with-current-buffer fancy-diary-buffer
  19469. (setq buffer-read-only nil)
  19470. (if (zerop (buffer-size))
  19471. ;; No entries
  19472. (setq entries nil)
  19473. ;; Omit the date and other unnecessary stuff
  19474. (org-agenda-cleanup-fancy-diary)
  19475. ;; Add prefix to each line and extend the text properties
  19476. (if (zerop (buffer-size))
  19477. (setq entries nil)
  19478. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19479. (set-buffer-modified-p nil)
  19480. (kill-buffer fancy-diary-buffer)))
  19481. (when entries
  19482. (setq entries (org-split-string entries "\n"))
  19483. (setq entries
  19484. (mapcar
  19485. (lambda (x)
  19486. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19487. ;; Extend the text properties to the beginning of the line
  19488. (org-add-props x (text-properties-at (1- (length x)) x)
  19489. 'type "diary" 'date date))
  19490. entries)))))
  19491. (defun org-agenda-cleanup-fancy-diary ()
  19492. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19493. This gets rid of the date, the underline under the date, and
  19494. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19495. date. It also removes lines that contain only whitespace."
  19496. (goto-char (point-min))
  19497. (if (looking-at ".*?:[ \t]*")
  19498. (progn
  19499. (replace-match "")
  19500. (re-search-forward "\n=+$" nil t)
  19501. (replace-match "")
  19502. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19503. (re-search-forward "\n=+$" nil t)
  19504. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19505. (goto-char (point-min))
  19506. (while (re-search-forward "^ +\n" nil t)
  19507. (replace-match ""))
  19508. (goto-char (point-min))
  19509. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19510. (replace-match "")))
  19511. ;; Make sure entries from the diary have the right text properties.
  19512. (eval-after-load "diary-lib"
  19513. '(if (boundp 'diary-modify-entry-list-string-function)
  19514. ;; We can rely on the hook, nothing to do
  19515. nil
  19516. ;; Hook not avaiable, must use advice to make this work
  19517. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19518. "Make the position visible."
  19519. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19520. (stringp string)
  19521. buffer-file-name)
  19522. (setq string (org-modify-diary-entry-string string))))))
  19523. (defun org-modify-diary-entry-string (string)
  19524. "Add text properties to string, allowing org-mode to act on it."
  19525. (org-add-props string nil
  19526. 'mouse-face 'highlight
  19527. 'keymap org-agenda-keymap
  19528. 'help-echo (if buffer-file-name
  19529. (format "mouse-2 or RET jump to diary file %s"
  19530. (abbreviate-file-name buffer-file-name))
  19531. "")
  19532. 'org-agenda-diary-link t
  19533. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19534. (defun org-diary-default-entry ()
  19535. "Add a dummy entry to the diary.
  19536. Needed to avoid empty dates which mess up holiday display."
  19537. ;; Catch the error if dealing with the new add-to-diary-alist
  19538. (when org-disable-agenda-to-diary
  19539. (condition-case nil
  19540. (add-to-diary-list original-date "Org-mode dummy" "")
  19541. (error
  19542. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19543. ;;;###autoload
  19544. (defun org-diary (&rest args)
  19545. "Return diary information from org-files.
  19546. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19547. It accesses org files and extracts information from those files to be
  19548. listed in the diary. The function accepts arguments specifying what
  19549. items should be listed. The following arguments are allowed:
  19550. :timestamp List the headlines of items containing a date stamp or
  19551. date range matching the selected date. Deadlines will
  19552. also be listed, on the expiration day.
  19553. :sexp List entries resulting from diary-like sexps.
  19554. :deadline List any deadlines past due, or due within
  19555. `org-deadline-warning-days'. The listing occurs only
  19556. in the diary for *today*, not at any other date. If
  19557. an entry is marked DONE, it is no longer listed.
  19558. :scheduled List all items which are scheduled for the given date.
  19559. The diary for *today* also contains items which were
  19560. scheduled earlier and are not yet marked DONE.
  19561. :todo List all TODO items from the org-file. This may be a
  19562. long list - so this is not turned on by default.
  19563. Like deadlines, these entries only show up in the
  19564. diary for *today*, not at any other date.
  19565. The call in the diary file should look like this:
  19566. &%%(org-diary) ~/path/to/some/orgfile.org
  19567. Use a separate line for each org file to check. Or, if you omit the file name,
  19568. all files listed in `org-agenda-files' will be checked automatically:
  19569. &%%(org-diary)
  19570. If you don't give any arguments (as in the example above), the default
  19571. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19572. So the example above may also be written as
  19573. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19574. The function expects the lisp variables `entry' and `date' to be provided
  19575. by the caller, because this is how the calendar works. Don't use this
  19576. function from a program - use `org-agenda-get-day-entries' instead."
  19577. (when (> (- (time-to-seconds (current-time))
  19578. org-agenda-last-marker-time)
  19579. 5)
  19580. (org-agenda-reset-markers))
  19581. (org-compile-prefix-format 'agenda)
  19582. (org-set-sorting-strategy 'agenda)
  19583. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19584. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19585. (list entry)
  19586. (org-agenda-files t)))
  19587. file rtn results)
  19588. (org-prepare-agenda-buffers files)
  19589. ;; If this is called during org-agenda, don't return any entries to
  19590. ;; the calendar. Org Agenda will list these entries itself.
  19591. (if org-disable-agenda-to-diary (setq files nil))
  19592. (while (setq file (pop files))
  19593. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19594. (setq results (append results rtn)))
  19595. (if results
  19596. (concat (org-finalize-agenda-entries results) "\n"))))
  19597. ;;; Agenda entry finders
  19598. (defun org-agenda-get-day-entries (file date &rest args)
  19599. "Does the work for `org-diary' and `org-agenda'.
  19600. FILE is the path to a file to be checked for entries. DATE is date like
  19601. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19602. which kind of entries should be extracted. For details about these, see
  19603. the documentation of `org-diary'."
  19604. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19605. (let* ((org-startup-folded nil)
  19606. (org-startup-align-all-tables nil)
  19607. (buffer (if (file-exists-p file)
  19608. (org-get-agenda-file-buffer file)
  19609. (error "No such file %s" file)))
  19610. arg results rtn)
  19611. (if (not buffer)
  19612. ;; If file does not exist, make sure an error message ends up in diary
  19613. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19614. (with-current-buffer buffer
  19615. (unless (org-mode-p)
  19616. (error "Agenda file %s is not in `org-mode'" file))
  19617. (let ((case-fold-search nil))
  19618. (save-excursion
  19619. (save-restriction
  19620. (if org-agenda-restrict
  19621. (narrow-to-region org-agenda-restrict-begin
  19622. org-agenda-restrict-end)
  19623. (widen))
  19624. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19625. (while (setq arg (pop args))
  19626. (cond
  19627. ((and (eq arg :todo)
  19628. (equal date (calendar-current-date)))
  19629. (setq rtn (org-agenda-get-todos))
  19630. (setq results (append results rtn)))
  19631. ((eq arg :timestamp)
  19632. (setq rtn (org-agenda-get-blocks))
  19633. (setq results (append results rtn))
  19634. (setq rtn (org-agenda-get-timestamps))
  19635. (setq results (append results rtn)))
  19636. ((eq arg :sexp)
  19637. (setq rtn (org-agenda-get-sexps))
  19638. (setq results (append results rtn)))
  19639. ((eq arg :scheduled)
  19640. (setq rtn (org-agenda-get-scheduled))
  19641. (setq results (append results rtn)))
  19642. ((eq arg :closed)
  19643. (setq rtn (org-agenda-get-closed))
  19644. (setq results (append results rtn)))
  19645. ((eq arg :deadline)
  19646. (setq rtn (org-agenda-get-deadlines))
  19647. (setq results (append results rtn))))))))
  19648. results))))
  19649. (defun org-entry-is-todo-p ()
  19650. (member (org-get-todo-state) org-not-done-keywords))
  19651. (defun org-entry-is-done-p ()
  19652. (member (org-get-todo-state) org-done-keywords))
  19653. (defun org-get-todo-state ()
  19654. (save-excursion
  19655. (org-back-to-heading t)
  19656. (and (looking-at org-todo-line-regexp)
  19657. (match-end 2)
  19658. (match-string 2))))
  19659. (defun org-at-date-range-p (&optional inactive-ok)
  19660. "Is the cursor inside a date range?"
  19661. (interactive)
  19662. (save-excursion
  19663. (catch 'exit
  19664. (let ((pos (point)))
  19665. (skip-chars-backward "^[<\r\n")
  19666. (skip-chars-backward "<[")
  19667. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19668. (>= (match-end 0) pos)
  19669. (throw 'exit t))
  19670. (skip-chars-backward "^<[\r\n")
  19671. (skip-chars-backward "<[")
  19672. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19673. (>= (match-end 0) pos)
  19674. (throw 'exit t)))
  19675. nil)))
  19676. (defun org-agenda-get-todos ()
  19677. "Return the TODO information for agenda display."
  19678. (let* ((props (list 'face nil
  19679. 'done-face 'org-done
  19680. 'org-not-done-regexp org-not-done-regexp
  19681. 'org-todo-regexp org-todo-regexp
  19682. 'mouse-face 'highlight
  19683. 'keymap org-agenda-keymap
  19684. 'help-echo
  19685. (format "mouse-2 or RET jump to org file %s"
  19686. (abbreviate-file-name buffer-file-name))))
  19687. ;; FIXME: get rid of the \n at some point but watch out
  19688. (regexp (concat "^\\*+[ \t]+\\("
  19689. (if org-select-this-todo-keyword
  19690. (if (equal org-select-this-todo-keyword "*")
  19691. org-todo-regexp
  19692. (concat "\\<\\("
  19693. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19694. "\\)\\>"))
  19695. org-not-done-regexp)
  19696. "[^\n\r]*\\)"))
  19697. marker priority category tags
  19698. ee txt beg end)
  19699. (goto-char (point-min))
  19700. (while (re-search-forward regexp nil t)
  19701. (catch :skip
  19702. (save-match-data
  19703. (beginning-of-line)
  19704. (setq beg (point) end (progn (outline-next-heading) (point)))
  19705. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19706. (re-search-forward org-ts-regexp end t))
  19707. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19708. (re-search-forward org-scheduled-time-regexp end t))
  19709. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19710. (re-search-forward org-deadline-time-regexp end t)
  19711. (org-deadline-close (match-string 1))))
  19712. (goto-char (1+ beg))
  19713. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19714. (throw :skip nil)))
  19715. (goto-char beg)
  19716. (org-agenda-skip)
  19717. (goto-char (match-beginning 1))
  19718. (setq marker (org-agenda-new-marker (match-beginning 0))
  19719. category (org-get-category)
  19720. tags (org-get-tags-at (point))
  19721. txt (org-format-agenda-item "" (match-string 1) category tags)
  19722. priority (1+ (org-get-priority txt)))
  19723. (org-add-props txt props
  19724. 'org-marker marker 'org-hd-marker marker
  19725. 'priority priority 'org-category category
  19726. 'type "todo")
  19727. (push txt ee)
  19728. (if org-agenda-todo-list-sublevels
  19729. (goto-char (match-end 1))
  19730. (org-end-of-subtree 'invisible))))
  19731. (nreverse ee)))
  19732. (defconst org-agenda-no-heading-message
  19733. "No heading for this item in buffer or region.")
  19734. (defun org-agenda-get-timestamps ()
  19735. "Return the date stamp information for agenda display."
  19736. (let* ((props (list 'face nil
  19737. 'org-not-done-regexp org-not-done-regexp
  19738. 'org-todo-regexp org-todo-regexp
  19739. 'mouse-face 'highlight
  19740. 'keymap org-agenda-keymap
  19741. 'help-echo
  19742. (format "mouse-2 or RET jump to org file %s"
  19743. (abbreviate-file-name buffer-file-name))))
  19744. (d1 (calendar-absolute-from-gregorian date))
  19745. (remove-re
  19746. (concat
  19747. (regexp-quote
  19748. (format-time-string
  19749. "<%Y-%m-%d"
  19750. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19751. ".*?>"))
  19752. (regexp
  19753. (concat
  19754. (regexp-quote
  19755. (substring
  19756. (format-time-string
  19757. (car org-time-stamp-formats)
  19758. (apply 'encode-time ; DATE bound by calendar
  19759. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19760. 0 11))
  19761. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19762. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19763. marker hdmarker deadlinep scheduledp donep tmp priority category
  19764. ee txt timestr tags b0 b3 e3 head)
  19765. (goto-char (point-min))
  19766. (while (re-search-forward regexp nil t)
  19767. (setq b0 (match-beginning 0)
  19768. b3 (match-beginning 3) e3 (match-end 3))
  19769. (catch :skip
  19770. (and (org-at-date-range-p) (throw :skip nil))
  19771. (org-agenda-skip)
  19772. (if (and (match-end 1)
  19773. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19774. (throw :skip nil))
  19775. (if (and e3
  19776. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19777. (throw :skip nil))
  19778. (setq marker (org-agenda-new-marker b0)
  19779. category (org-get-category b0)
  19780. tmp (buffer-substring (max (point-min)
  19781. (- b0 org-ds-keyword-length))
  19782. b0)
  19783. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19784. deadlinep (string-match org-deadline-regexp tmp)
  19785. scheduledp (string-match org-scheduled-regexp tmp)
  19786. donep (org-entry-is-done-p))
  19787. (if (or scheduledp deadlinep) (throw :skip t))
  19788. (if (string-match ">" timestr)
  19789. ;; substring should only run to end of time stamp
  19790. (setq timestr (substring timestr 0 (match-end 0))))
  19791. (save-excursion
  19792. (if (re-search-backward "^\\*+ " nil t)
  19793. (progn
  19794. (goto-char (match-beginning 0))
  19795. (setq hdmarker (org-agenda-new-marker)
  19796. tags (org-get-tags-at))
  19797. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19798. (setq head (match-string 1))
  19799. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19800. (setq txt (org-format-agenda-item
  19801. nil head category tags timestr nil
  19802. remove-re)))
  19803. (setq txt org-agenda-no-heading-message))
  19804. (setq priority (org-get-priority txt))
  19805. (org-add-props txt props
  19806. 'org-marker marker 'org-hd-marker hdmarker)
  19807. (org-add-props txt nil 'priority priority
  19808. 'org-category category 'date date
  19809. 'type "timestamp")
  19810. (push txt ee))
  19811. (outline-next-heading)))
  19812. (nreverse ee)))
  19813. (defun org-agenda-get-sexps ()
  19814. "Return the sexp information for agenda display."
  19815. (require 'diary-lib)
  19816. (let* ((props (list 'face nil
  19817. 'mouse-face 'highlight
  19818. 'keymap org-agenda-keymap
  19819. 'help-echo
  19820. (format "mouse-2 or RET jump to org file %s"
  19821. (abbreviate-file-name buffer-file-name))))
  19822. (regexp "^&?%%(")
  19823. marker category ee txt tags entry result beg b sexp sexp-entry)
  19824. (goto-char (point-min))
  19825. (while (re-search-forward regexp nil t)
  19826. (catch :skip
  19827. (org-agenda-skip)
  19828. (setq beg (match-beginning 0))
  19829. (goto-char (1- (match-end 0)))
  19830. (setq b (point))
  19831. (forward-sexp 1)
  19832. (setq sexp (buffer-substring b (point)))
  19833. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19834. (org-trim (match-string 1))
  19835. ""))
  19836. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19837. (when result
  19838. (setq marker (org-agenda-new-marker beg)
  19839. category (org-get-category beg))
  19840. (if (string-match "\\S-" result)
  19841. (setq txt result)
  19842. (setq txt "SEXP entry returned empty string"))
  19843. (setq txt (org-format-agenda-item
  19844. "" txt category tags 'time))
  19845. (org-add-props txt props 'org-marker marker)
  19846. (org-add-props txt nil
  19847. 'org-category category 'date date
  19848. 'type "sexp")
  19849. (push txt ee))))
  19850. (nreverse ee)))
  19851. (defun org-agenda-get-closed ()
  19852. "Return the logged TODO entries for agenda display."
  19853. (let* ((props (list 'mouse-face 'highlight
  19854. 'org-not-done-regexp org-not-done-regexp
  19855. 'org-todo-regexp org-todo-regexp
  19856. 'keymap org-agenda-keymap
  19857. 'help-echo
  19858. (format "mouse-2 or RET jump to org file %s"
  19859. (abbreviate-file-name buffer-file-name))))
  19860. (regexp (concat
  19861. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19862. (regexp-quote
  19863. (substring
  19864. (format-time-string
  19865. (car org-time-stamp-formats)
  19866. (apply 'encode-time ; DATE bound by calendar
  19867. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19868. 1 11))))
  19869. marker hdmarker priority category tags closedp
  19870. ee txt timestr)
  19871. (goto-char (point-min))
  19872. (while (re-search-forward regexp nil t)
  19873. (catch :skip
  19874. (org-agenda-skip)
  19875. (setq marker (org-agenda-new-marker (match-beginning 0))
  19876. closedp (equal (match-string 1) org-closed-string)
  19877. category (org-get-category (match-beginning 0))
  19878. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19879. ;; donep (org-entry-is-done-p)
  19880. )
  19881. (if (string-match "\\]" timestr)
  19882. ;; substring should only run to end of time stamp
  19883. (setq timestr (substring timestr 0 (match-end 0))))
  19884. (save-excursion
  19885. (if (re-search-backward "^\\*+ " nil t)
  19886. (progn
  19887. (goto-char (match-beginning 0))
  19888. (setq hdmarker (org-agenda-new-marker)
  19889. tags (org-get-tags-at))
  19890. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19891. (setq txt (org-format-agenda-item
  19892. (if closedp "Closed: " "Clocked: ")
  19893. (match-string 1) category tags timestr)))
  19894. (setq txt org-agenda-no-heading-message))
  19895. (setq priority 100000)
  19896. (org-add-props txt props
  19897. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19898. 'priority priority 'org-category category
  19899. 'type "closed" 'date date
  19900. 'undone-face 'org-warning 'done-face 'org-done)
  19901. (push txt ee))
  19902. (goto-char (point-at-eol))))
  19903. (nreverse ee)))
  19904. (defun org-agenda-get-deadlines ()
  19905. "Return the deadline information for agenda display."
  19906. (let* ((props (list 'mouse-face 'highlight
  19907. 'org-not-done-regexp org-not-done-regexp
  19908. 'org-todo-regexp org-todo-regexp
  19909. 'keymap org-agenda-keymap
  19910. 'help-echo
  19911. (format "mouse-2 or RET jump to org file %s"
  19912. (abbreviate-file-name buffer-file-name))))
  19913. (regexp org-deadline-time-regexp)
  19914. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19915. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19916. d2 diff dfrac wdays pos pos1 category tags
  19917. ee txt head face s upcomingp donep timestr)
  19918. (goto-char (point-min))
  19919. (while (re-search-forward regexp nil t)
  19920. (catch :skip
  19921. (org-agenda-skip)
  19922. (setq s (match-string 1)
  19923. pos (1- (match-beginning 1))
  19924. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19925. diff (- d2 d1)
  19926. wdays (org-get-wdays s)
  19927. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19928. upcomingp (and todayp (> diff 0)))
  19929. ;; When to show a deadline in the calendar:
  19930. ;; If the expiration is within wdays warning time.
  19931. ;; Past-due deadlines are only shown on the current date
  19932. (if (or (and (<= diff wdays)
  19933. (and todayp (not org-agenda-only-exact-dates)))
  19934. (= diff 0))
  19935. (save-excursion
  19936. (setq category (org-get-category))
  19937. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19938. (progn
  19939. (goto-char (match-end 0))
  19940. (setq pos1 (match-beginning 0))
  19941. (setq tags (org-get-tags-at pos1))
  19942. (setq head (buffer-substring-no-properties
  19943. (point)
  19944. (progn (skip-chars-forward "^\r\n")
  19945. (point))))
  19946. (setq donep (string-match org-looking-at-done-regexp head))
  19947. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19948. (setq timestr
  19949. (concat (substring s (match-beginning 1)) " "))
  19950. (setq timestr 'time))
  19951. (if (and donep
  19952. (or org-agenda-skip-deadline-if-done
  19953. (not (= diff 0))))
  19954. (setq txt nil)
  19955. (setq txt (org-format-agenda-item
  19956. (if (= diff 0)
  19957. (car org-agenda-deadline-leaders)
  19958. (format (nth 1 org-agenda-deadline-leaders)
  19959. diff))
  19960. head category tags timestr))))
  19961. (setq txt org-agenda-no-heading-message))
  19962. (when txt
  19963. (setq face (org-agenda-deadline-face dfrac))
  19964. (org-add-props txt props
  19965. 'org-marker (org-agenda-new-marker pos)
  19966. 'org-hd-marker (org-agenda-new-marker pos1)
  19967. 'priority (+ (- diff)
  19968. (org-get-priority txt))
  19969. 'org-category category
  19970. 'type (if upcomingp "upcoming-deadline" "deadline")
  19971. 'date (if upcomingp date d2)
  19972. 'face (if donep 'org-done face)
  19973. 'undone-face face 'done-face 'org-done)
  19974. (push txt ee))))))
  19975. (nreverse ee)))
  19976. (defun org-agenda-deadline-face (fraction)
  19977. "Return the face to displaying a deadline item.
  19978. FRACTION is what fraction of the head-warning time has passed."
  19979. (let ((faces org-agenda-deadline-faces) f)
  19980. (catch 'exit
  19981. (while (setq f (pop faces))
  19982. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19983. (defun org-agenda-get-scheduled ()
  19984. "Return the scheduled information for agenda display."
  19985. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19986. 'org-todo-regexp org-todo-regexp
  19987. 'done-face 'org-done
  19988. 'mouse-face 'highlight
  19989. 'keymap org-agenda-keymap
  19990. 'help-echo
  19991. (format "mouse-2 or RET jump to org file %s"
  19992. (abbreviate-file-name buffer-file-name))))
  19993. (regexp org-scheduled-time-regexp)
  19994. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19995. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19996. d2 diff pos pos1 category tags
  19997. ee txt head pastschedp donep face timestr s)
  19998. (goto-char (point-min))
  19999. (while (re-search-forward regexp nil t)
  20000. (catch :skip
  20001. (org-agenda-skip)
  20002. (setq s (match-string 1)
  20003. pos (1- (match-beginning 1))
  20004. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20005. ;;; is this right?
  20006. ;;; do we need to do this for deadleine too????
  20007. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20008. diff (- d2 d1))
  20009. (setq pastschedp (and todayp (< diff 0)))
  20010. ;; When to show a scheduled item in the calendar:
  20011. ;; If it is on or past the date.
  20012. (if (or (and (< diff 0)
  20013. (and todayp (not org-agenda-only-exact-dates)))
  20014. (= diff 0))
  20015. (save-excursion
  20016. (setq category (org-get-category))
  20017. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20018. (progn
  20019. (goto-char (match-end 0))
  20020. (setq pos1 (match-beginning 0))
  20021. (setq tags (org-get-tags-at))
  20022. (setq head (buffer-substring-no-properties
  20023. (point)
  20024. (progn (skip-chars-forward "^\r\n") (point))))
  20025. (setq donep (string-match org-looking-at-done-regexp head))
  20026. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20027. (setq timestr
  20028. (concat (substring s (match-beginning 1)) " "))
  20029. (setq timestr 'time))
  20030. (if (and donep
  20031. (or org-agenda-skip-scheduled-if-done
  20032. (not (= diff 0))))
  20033. (setq txt nil)
  20034. (setq txt (org-format-agenda-item
  20035. (if (= diff 0)
  20036. (car org-agenda-scheduled-leaders)
  20037. (format (nth 1 org-agenda-scheduled-leaders)
  20038. (- 1 diff)))
  20039. head category tags timestr))))
  20040. (setq txt org-agenda-no-heading-message))
  20041. (when txt
  20042. (setq face (if pastschedp
  20043. 'org-scheduled-previously
  20044. 'org-scheduled-today))
  20045. (org-add-props txt props
  20046. 'undone-face face
  20047. 'face (if donep 'org-done face)
  20048. 'org-marker (org-agenda-new-marker pos)
  20049. 'org-hd-marker (org-agenda-new-marker pos1)
  20050. 'type (if pastschedp "past-scheduled" "scheduled")
  20051. 'date (if pastschedp d2 date)
  20052. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20053. 'org-category category)
  20054. (push txt ee))))))
  20055. (nreverse ee)))
  20056. (defun org-agenda-get-blocks ()
  20057. "Return the date-range information for agenda display."
  20058. (let* ((props (list 'face nil
  20059. 'org-not-done-regexp org-not-done-regexp
  20060. 'org-todo-regexp org-todo-regexp
  20061. 'mouse-face 'highlight
  20062. 'keymap org-agenda-keymap
  20063. 'help-echo
  20064. (format "mouse-2 or RET jump to org file %s"
  20065. (abbreviate-file-name buffer-file-name))))
  20066. (regexp org-tr-regexp)
  20067. (d0 (calendar-absolute-from-gregorian date))
  20068. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20069. donep head)
  20070. (goto-char (point-min))
  20071. (while (re-search-forward regexp nil t)
  20072. (catch :skip
  20073. (org-agenda-skip)
  20074. (setq pos (point))
  20075. (setq timestr (match-string 0)
  20076. s1 (match-string 1)
  20077. s2 (match-string 2)
  20078. d1 (time-to-days (org-time-string-to-time s1))
  20079. d2 (time-to-days (org-time-string-to-time s2)))
  20080. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20081. ;; Only allow days between the limits, because the normal
  20082. ;; date stamps will catch the limits.
  20083. (save-excursion
  20084. (setq marker (org-agenda-new-marker (point)))
  20085. (setq category (org-get-category))
  20086. (if (re-search-backward "^\\*+ " nil t)
  20087. (progn
  20088. (goto-char (match-beginning 0))
  20089. (setq hdmarker (org-agenda-new-marker (point)))
  20090. (setq tags (org-get-tags-at))
  20091. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20092. (setq head (match-string 1))
  20093. (and org-agenda-skip-timestamp-if-done
  20094. (org-entry-is-done-p)
  20095. (throw :skip t))
  20096. (setq txt (org-format-agenda-item
  20097. (format (if (= d1 d2) "" "(%d/%d): ")
  20098. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20099. head category tags
  20100. (if (= d0 d1) timestr))))
  20101. (setq txt org-agenda-no-heading-message))
  20102. (org-add-props txt props
  20103. 'org-marker marker 'org-hd-marker hdmarker
  20104. 'type "block" 'date date
  20105. 'priority (org-get-priority txt) 'org-category category)
  20106. (push txt ee)))
  20107. (goto-char pos)))
  20108. ;; Sort the entries by expiration date.
  20109. (nreverse ee)))
  20110. ;;; Agenda presentation and sorting
  20111. (defconst org-plain-time-of-day-regexp
  20112. (concat
  20113. "\\(\\<[012]?[0-9]"
  20114. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20115. "\\(--?"
  20116. "\\(\\<[012]?[0-9]"
  20117. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20118. "\\)?")
  20119. "Regular expression to match a plain time or time range.
  20120. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20121. groups carry important information:
  20122. 0 the full match
  20123. 1 the first time, range or not
  20124. 8 the second time, if it is a range.")
  20125. (defconst org-plain-time-extension-regexp
  20126. (concat
  20127. "\\(\\<[012]?[0-9]"
  20128. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20129. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20130. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20131. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20132. groups carry important information:
  20133. 0 the full match
  20134. 7 hours of duration
  20135. 9 minutes of duration")
  20136. (defconst org-stamp-time-of-day-regexp
  20137. (concat
  20138. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20139. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20140. "\\(--?"
  20141. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20142. "Regular expression to match a timestamp time or time range.
  20143. After a match, the following groups carry important information:
  20144. 0 the full match
  20145. 1 date plus weekday, for backreferencing to make sure both times on same day
  20146. 2 the first time, range or not
  20147. 4 the second time, if it is a range.")
  20148. (defvar org-prefix-has-time nil
  20149. "A flag, set by `org-compile-prefix-format'.
  20150. The flag is set if the currently compiled format contains a `%t'.")
  20151. (defvar org-prefix-has-tag nil
  20152. "A flag, set by `org-compile-prefix-format'.
  20153. The flag is set if the currently compiled format contains a `%T'.")
  20154. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20155. noprefix remove-re)
  20156. "Format TXT to be inserted into the agenda buffer.
  20157. In particular, it adds the prefix and corresponding text properties. EXTRA
  20158. must be a string and replaces the `%s' specifier in the prefix format.
  20159. CATEGORY (string, symbol or nil) may be used to overrule the default
  20160. category taken from local variable or file name. It will replace the `%c'
  20161. specifier in the format. DOTIME, when non-nil, indicates that a
  20162. time-of-day should be extracted from TXT for sorting of this entry, and for
  20163. the `%t' specifier in the format. When DOTIME is a string, this string is
  20164. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20165. only the correctly processes TXT should be returned - this is used by
  20166. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20167. Any match of REMOVE-RE will be removed from TXT."
  20168. (save-match-data
  20169. ;; Diary entries sometimes have extra whitespace at the beginning
  20170. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20171. (let* ((category (or category
  20172. org-category
  20173. (if buffer-file-name
  20174. (file-name-sans-extension
  20175. (file-name-nondirectory buffer-file-name))
  20176. "")))
  20177. (tag (if tags (nth (1- (length tags)) tags) ""))
  20178. time ; time and tag are needed for the eval of the prefix format
  20179. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20180. (time-of-day (and dotime (org-get-time-of-day ts)))
  20181. stamp plain s0 s1 s2 rtn srp)
  20182. (when (and dotime time-of-day org-prefix-has-time)
  20183. ;; Extract starting and ending time and move them to prefix
  20184. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20185. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20186. (setq s0 (match-string 0 ts)
  20187. srp (and stamp (match-end 3))
  20188. s1 (match-string (if plain 1 2) ts)
  20189. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20190. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20191. ;; them, we might want to remove them there to avoid duplication.
  20192. ;; The user can turn this off with a variable.
  20193. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20194. (string-match (concat (regexp-quote s0) " *") txt)
  20195. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20196. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20197. (= (match-beginning 0) 0)
  20198. t))
  20199. (setq txt (replace-match "" nil nil txt))))
  20200. ;; Normalize the time(s) to 24 hour
  20201. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20202. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20203. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20204. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20205. (let ((m (+ (string-to-number (match-string 2 s1))
  20206. (* 60 (string-to-number (match-string 1 s1)))
  20207. org-agenda-default-appointment-duration))
  20208. h)
  20209. (setq h (/ m 60) m (- m (* h 60)))
  20210. (setq s2 (format "%02d:%02d" h m))))
  20211. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20212. txt)
  20213. ;; Tags are in the string
  20214. (if (or (eq org-agenda-remove-tags t)
  20215. (and org-agenda-remove-tags
  20216. org-prefix-has-tag))
  20217. (setq txt (replace-match "" t t txt))
  20218. (setq txt (replace-match
  20219. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20220. (match-string 2 txt))
  20221. t t txt))))
  20222. (when remove-re
  20223. (while (string-match remove-re txt)
  20224. (setq txt (replace-match "" t t txt))))
  20225. ;; Create the final string
  20226. (if noprefix
  20227. (setq rtn txt)
  20228. ;; Prepare the variables needed in the eval of the compiled format
  20229. (setq time (cond (s2 (concat s1 "-" s2))
  20230. (s1 (concat s1 "......"))
  20231. (t ""))
  20232. extra (or extra "")
  20233. category (if (symbolp category) (symbol-name category) category))
  20234. ;; Evaluate the compiled format
  20235. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20236. ;; And finally add the text properties
  20237. (org-add-props rtn nil
  20238. 'org-category (downcase category) 'tags tags
  20239. 'org-highest-priority org-highest-priority
  20240. 'org-lowest-priority org-lowest-priority
  20241. 'prefix-length (- (length rtn) (length txt))
  20242. 'time-of-day time-of-day
  20243. 'txt txt
  20244. 'time time
  20245. 'extra extra
  20246. 'dotime dotime))))
  20247. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20248. (defvar org-agenda-sorting-strategy-selected nil)
  20249. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20250. (catch 'exit
  20251. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20252. ((and todayp (member 'today (car org-agenda-time-grid))))
  20253. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20254. ((member 'weekly (car org-agenda-time-grid)))
  20255. (t (throw 'exit list)))
  20256. (let* ((have (delq nil (mapcar
  20257. (lambda (x) (get-text-property 1 'time-of-day x))
  20258. list)))
  20259. (string (nth 1 org-agenda-time-grid))
  20260. (gridtimes (nth 2 org-agenda-time-grid))
  20261. (req (car org-agenda-time-grid))
  20262. (remove (member 'remove-match req))
  20263. new time)
  20264. (if (and (member 'require-timed req) (not have))
  20265. ;; don't show empty grid
  20266. (throw 'exit list))
  20267. (while (setq time (pop gridtimes))
  20268. (unless (and remove (member time have))
  20269. (setq time (int-to-string time))
  20270. (push (org-format-agenda-item
  20271. nil string "" nil
  20272. (concat (substring time 0 -2) ":" (substring time -2)))
  20273. new)
  20274. (put-text-property
  20275. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20276. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20277. (append new list)
  20278. (append list new)))))
  20279. (defun org-compile-prefix-format (key)
  20280. "Compile the prefix format into a Lisp form that can be evaluated.
  20281. The resulting form is returned and stored in the variable
  20282. `org-prefix-format-compiled'."
  20283. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20284. (let ((s (cond
  20285. ((stringp org-agenda-prefix-format)
  20286. org-agenda-prefix-format)
  20287. ((assq key org-agenda-prefix-format)
  20288. (cdr (assq key org-agenda-prefix-format)))
  20289. (t " %-12:c%?-12t% s")))
  20290. (start 0)
  20291. varform vars var e c f opt)
  20292. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20293. s start)
  20294. (setq var (cdr (assoc (match-string 4 s)
  20295. '(("c" . category) ("t" . time) ("s" . extra)
  20296. ("T" . tag))))
  20297. c (or (match-string 3 s) "")
  20298. opt (match-beginning 1)
  20299. start (1+ (match-beginning 0)))
  20300. (if (equal var 'time) (setq org-prefix-has-time t))
  20301. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20302. (setq f (concat "%" (match-string 2 s) "s"))
  20303. (if opt
  20304. (setq varform
  20305. `(if (equal "" ,var)
  20306. ""
  20307. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20308. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20309. (setq s (replace-match "%s" t nil s))
  20310. (push varform vars))
  20311. (setq vars (nreverse vars))
  20312. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20313. (defun org-set-sorting-strategy (key)
  20314. (if (symbolp (car org-agenda-sorting-strategy))
  20315. ;; the old format
  20316. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20317. (setq org-agenda-sorting-strategy-selected
  20318. (or (cdr (assq key org-agenda-sorting-strategy))
  20319. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20320. '(time-up category-keep priority-down)))))
  20321. (defun org-get-time-of-day (s &optional string mod24)
  20322. "Check string S for a time of day.
  20323. If found, return it as a military time number between 0 and 2400.
  20324. If not found, return nil.
  20325. The optional STRING argument forces conversion into a 5 character wide string
  20326. HH:MM."
  20327. (save-match-data
  20328. (when
  20329. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20330. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20331. (let* ((h (string-to-number (match-string 1 s)))
  20332. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20333. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20334. (am-p (equal ampm "am"))
  20335. (h1 (cond ((not ampm) h)
  20336. ((= h 12) (if am-p 0 12))
  20337. (t (+ h (if am-p 0 12)))))
  20338. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20339. (mod h1 24) h1))
  20340. (t0 (+ (* 100 h2) m))
  20341. (t1 (concat (if (>= h1 24) "+" " ")
  20342. (if (< t0 100) "0" "")
  20343. (if (< t0 10) "0" "")
  20344. (int-to-string t0))))
  20345. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20346. (defun org-finalize-agenda-entries (list &optional nosort)
  20347. "Sort and concatenate the agenda items."
  20348. (setq list (mapcar 'org-agenda-highlight-todo list))
  20349. (if nosort
  20350. list
  20351. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20352. (defun org-agenda-highlight-todo (x)
  20353. (let (re pl)
  20354. (if (eq x 'line)
  20355. (save-excursion
  20356. (beginning-of-line 1)
  20357. (setq re (get-text-property (point) 'org-todo-regexp))
  20358. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20359. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20360. (add-text-properties (match-beginning 0) (match-end 0)
  20361. (list 'face (org-get-todo-face 0)))
  20362. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20363. (delete-region (match-beginning 1) (1- (match-end 0)))
  20364. (goto-char (match-beginning 1))
  20365. (insert (format org-agenda-todo-keyword-format s)))))
  20366. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20367. pl (get-text-property 0 'prefix-length x))
  20368. (when (and re
  20369. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20370. x (or pl 0)) pl))
  20371. (add-text-properties
  20372. (or (match-end 1) (match-end 0)) (match-end 0)
  20373. (list 'face (org-get-todo-face (match-string 2 x)))
  20374. x)
  20375. (setq x (concat (substring x 0 (match-end 1))
  20376. (format org-agenda-todo-keyword-format
  20377. (match-string 2 x))
  20378. " "
  20379. (substring x (match-end 3)))))
  20380. x)))
  20381. (defsubst org-cmp-priority (a b)
  20382. "Compare the priorities of string A and B."
  20383. (let ((pa (or (get-text-property 1 'priority a) 0))
  20384. (pb (or (get-text-property 1 'priority b) 0)))
  20385. (cond ((> pa pb) +1)
  20386. ((< pa pb) -1)
  20387. (t nil))))
  20388. (defsubst org-cmp-category (a b)
  20389. "Compare the string values of categories of strings A and B."
  20390. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20391. (cb (or (get-text-property 1 'org-category b) "")))
  20392. (cond ((string-lessp ca cb) -1)
  20393. ((string-lessp cb ca) +1)
  20394. (t nil))))
  20395. (defsubst org-cmp-tag (a b)
  20396. "Compare the string values of categories of strings A and B."
  20397. (let ((ta (car (last (get-text-property 1 'tags a))))
  20398. (tb (car (last (get-text-property 1 'tags b)))))
  20399. (cond ((not ta) +1)
  20400. ((not tb) -1)
  20401. ((string-lessp ta tb) -1)
  20402. ((string-lessp tb ta) +1)
  20403. (t nil))))
  20404. (defsubst org-cmp-time (a b)
  20405. "Compare the time-of-day values of strings A and B."
  20406. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20407. (ta (or (get-text-property 1 'time-of-day a) def))
  20408. (tb (or (get-text-property 1 'time-of-day b) def)))
  20409. (cond ((< ta tb) -1)
  20410. ((< tb ta) +1)
  20411. (t nil))))
  20412. (defun org-entries-lessp (a b)
  20413. "Predicate for sorting agenda entries."
  20414. ;; The following variables will be used when the form is evaluated.
  20415. ;; So even though the compiler complains, keep them.
  20416. (let* ((time-up (org-cmp-time a b))
  20417. (time-down (if time-up (- time-up) nil))
  20418. (priority-up (org-cmp-priority a b))
  20419. (priority-down (if priority-up (- priority-up) nil))
  20420. (category-up (org-cmp-category a b))
  20421. (category-down (if category-up (- category-up) nil))
  20422. (category-keep (if category-up +1 nil))
  20423. (tag-up (org-cmp-tag a b))
  20424. (tag-down (if tag-up (- tag-up) nil)))
  20425. (cdr (assoc
  20426. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20427. '((-1 . t) (1 . nil) (nil . nil))))))
  20428. ;;; Agenda restriction lock
  20429. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20430. "Overlay to mark the headline to which arenda commands are restricted.")
  20431. (org-overlay-put org-agenda-restriction-lock-overlay
  20432. 'face 'org-agenda-restriction-lock)
  20433. (org-overlay-put org-agenda-restriction-lock-overlay
  20434. 'help-echo "Agendas are currently limited to this subtree.")
  20435. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20436. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20437. "Overlay marking the agenda restriction line in speedbar.")
  20438. (org-overlay-put org-speedbar-restriction-lock-overlay
  20439. 'face 'org-agenda-restriction-lock)
  20440. (org-overlay-put org-speedbar-restriction-lock-overlay
  20441. 'help-echo "Agendas are currently limited to this item.")
  20442. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20443. (defun org-agenda-set-restriction-lock (&optional type)
  20444. "Set restriction lock for agenda, to current subtree or file.
  20445. Restriction will be the file if TYPE is `file', or if type is the
  20446. universal prefix '(4), or if the cursor is before the first headline
  20447. in the file. Otherwise, restriction will be to the current subtree."
  20448. (interactive "P")
  20449. (and (equal type '(4)) (setq type 'file))
  20450. (setq type (cond
  20451. (type type)
  20452. ((org-at-heading-p) 'subtree)
  20453. ((condition-case nil (org-back-to-heading t) (error nil))
  20454. 'subtree)
  20455. (t 'file)))
  20456. (if (eq type 'subtree)
  20457. (progn
  20458. (setq org-agenda-restrict t)
  20459. (setq org-agenda-overriding-restriction 'subtree)
  20460. (put 'org-agenda-files 'org-restrict
  20461. (list (buffer-file-name (buffer-base-buffer))))
  20462. (org-back-to-heading t)
  20463. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20464. (move-marker org-agenda-restrict-begin (point))
  20465. (move-marker org-agenda-restrict-end
  20466. (save-excursion (org-end-of-subtree t)))
  20467. (message "Locking agenda restriction to subtree"))
  20468. (put 'org-agenda-files 'org-restrict
  20469. (list (buffer-file-name (buffer-base-buffer))))
  20470. (setq org-agenda-restrict nil)
  20471. (setq org-agenda-overriding-restriction 'file)
  20472. (move-marker org-agenda-restrict-begin nil)
  20473. (move-marker org-agenda-restrict-end nil)
  20474. (message "Locking agenda restriction to file"))
  20475. (setq current-prefix-arg nil)
  20476. (org-agenda-maybe-redo))
  20477. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20478. "Remove the agenda restriction lock."
  20479. (interactive "P")
  20480. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20481. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20482. (setq org-agenda-overriding-restriction nil)
  20483. (setq org-agenda-restrict nil)
  20484. (put 'org-agenda-files 'org-restrict nil)
  20485. (move-marker org-agenda-restrict-begin nil)
  20486. (move-marker org-agenda-restrict-end nil)
  20487. (setq current-prefix-arg nil)
  20488. (message "Agenda restriction lock removed")
  20489. (or noupdate (org-agenda-maybe-redo)))
  20490. (defun org-agenda-maybe-redo ()
  20491. "If there is any window showing the agenda view, update it."
  20492. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20493. (w0 (selected-window)))
  20494. (when w
  20495. (select-window w)
  20496. (org-agenda-redo)
  20497. (select-window w0)
  20498. (if org-agenda-overriding-restriction
  20499. (message "Agenda view shifted to new %s restriction"
  20500. org-agenda-overriding-restriction)
  20501. (message "Agenda restriction lock removed")))))
  20502. ;;; Agenda commands
  20503. (defun org-agenda-check-type (error &rest types)
  20504. "Check if agenda buffer is of allowed type.
  20505. If ERROR is non-nil, throw an error, otherwise just return nil."
  20506. (if (memq org-agenda-type types)
  20507. t
  20508. (if error
  20509. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20510. nil)))
  20511. (defun org-agenda-quit ()
  20512. "Exit agenda by removing the window or the buffer."
  20513. (interactive)
  20514. (let ((buf (current-buffer)))
  20515. (if (not (one-window-p)) (delete-window))
  20516. (kill-buffer buf)
  20517. (org-agenda-reset-markers)
  20518. (org-columns-remove-overlays))
  20519. ;; Maybe restore the pre-agenda window configuration.
  20520. (and org-agenda-restore-windows-after-quit
  20521. (not (eq org-agenda-window-setup 'other-frame))
  20522. org-pre-agenda-window-conf
  20523. (set-window-configuration org-pre-agenda-window-conf)))
  20524. (defun org-agenda-exit ()
  20525. "Exit agenda by removing the window or the buffer.
  20526. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20527. Org-mode buffers visited directly by the user will not be touched."
  20528. (interactive)
  20529. (org-release-buffers org-agenda-new-buffers)
  20530. (setq org-agenda-new-buffers nil)
  20531. (org-agenda-quit))
  20532. (defun org-agenda-execute (arg)
  20533. "Execute another agenda command, keeping same window.\\<global-map>
  20534. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20535. (interactive "P")
  20536. (let ((org-agenda-window-setup 'current-window))
  20537. (org-agenda arg)))
  20538. (defun org-save-all-org-buffers ()
  20539. "Save all Org-mode buffers without user confirmation."
  20540. (interactive)
  20541. (message "Saving all Org-mode buffers...")
  20542. (save-some-buffers t 'org-mode-p)
  20543. (message "Saving all Org-mode buffers... done"))
  20544. (defun org-agenda-redo ()
  20545. "Rebuild Agenda.
  20546. When this is the global TODO list, a prefix argument will be interpreted."
  20547. (interactive)
  20548. (let* ((org-agenda-keep-modes t)
  20549. (line (org-current-line))
  20550. (window-line (- line (org-current-line (window-start))))
  20551. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20552. (message "Rebuilding agenda buffer...")
  20553. (org-let lprops '(eval org-agenda-redo-command))
  20554. (setq org-agenda-undo-list nil
  20555. org-agenda-pending-undo-list nil)
  20556. (message "Rebuilding agenda buffer...done")
  20557. (goto-line line)
  20558. (recenter window-line)))
  20559. (defun org-agenda-goto-date (date)
  20560. "Jump to DATE in agenda."
  20561. (interactive (list (org-read-date)))
  20562. (org-agenda-list nil date))
  20563. (defun org-agenda-goto-today ()
  20564. "Go to today."
  20565. (interactive)
  20566. (org-agenda-check-type t 'timeline 'agenda)
  20567. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20568. (cond
  20569. (tdpos (goto-char tdpos))
  20570. ((eq org-agenda-type 'agenda)
  20571. (let* ((sd (time-to-days
  20572. (time-subtract (current-time)
  20573. (list 0 (* 3600 org-extend-today-until) 0))))
  20574. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20575. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20576. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20577. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20578. (org-agenda-redo)
  20579. (org-agenda-find-same-or-today-or-agenda)))
  20580. (t (error "Cannot find today")))))
  20581. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20582. (goto-char
  20583. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20584. (text-property-any (point-min) (point-max) 'org-today t)
  20585. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20586. (point-min))))
  20587. (defun org-agenda-later (arg)
  20588. "Go forward in time by thee current span.
  20589. With prefix ARG, go forward that many times the current span."
  20590. (interactive "p")
  20591. (org-agenda-check-type t 'agenda)
  20592. (let* ((span org-agenda-span)
  20593. (sd org-starting-day)
  20594. (greg (calendar-gregorian-from-absolute sd))
  20595. (cnt (get-text-property (point) 'org-day-cnt))
  20596. greg2 nd)
  20597. (cond
  20598. ((eq span 'day)
  20599. (setq sd (+ arg sd) nd 1))
  20600. ((eq span 'week)
  20601. (setq sd (+ (* 7 arg) sd) nd 7))
  20602. ((eq span 'month)
  20603. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20604. sd (calendar-absolute-from-gregorian greg2))
  20605. (setcar greg2 (1+ (car greg2)))
  20606. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20607. ((eq span 'year)
  20608. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20609. sd (calendar-absolute-from-gregorian greg2))
  20610. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20611. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20612. (let ((org-agenda-overriding-arguments
  20613. (list (car org-agenda-last-arguments) sd nd t)))
  20614. (org-agenda-redo)
  20615. (org-agenda-find-same-or-today-or-agenda cnt))))
  20616. (defun org-agenda-earlier (arg)
  20617. "Go backward in time by the current span.
  20618. With prefix ARG, go backward that many times the current span."
  20619. (interactive "p")
  20620. (org-agenda-later (- arg)))
  20621. (defun org-agenda-day-view ()
  20622. "Switch to daily view for agenda."
  20623. (interactive)
  20624. (setq org-agenda-ndays 1)
  20625. (org-agenda-change-time-span 'day))
  20626. (defun org-agenda-week-view ()
  20627. "Switch to daily view for agenda."
  20628. (interactive)
  20629. (setq org-agenda-ndays 7)
  20630. (org-agenda-change-time-span 'week))
  20631. (defun org-agenda-month-view ()
  20632. "Switch to daily view for agenda."
  20633. (interactive)
  20634. (org-agenda-change-time-span 'month))
  20635. (defun org-agenda-year-view ()
  20636. "Switch to daily view for agenda."
  20637. (interactive)
  20638. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20639. (org-agenda-change-time-span 'year)
  20640. (error "Abort")))
  20641. (defun org-agenda-change-time-span (span)
  20642. "Change the agenda view to SPAN.
  20643. SPAN may be `day', `week', `month', `year'."
  20644. (org-agenda-check-type t 'agenda)
  20645. (if (equal org-agenda-span span)
  20646. (error "Viewing span is already \"%s\"" span))
  20647. (let* ((sd (or (get-text-property (point) 'day)
  20648. org-starting-day))
  20649. (computed (org-agenda-compute-time-span sd span))
  20650. (org-agenda-overriding-arguments
  20651. (list (car org-agenda-last-arguments)
  20652. (car computed) (cdr computed) t)))
  20653. (org-agenda-redo)
  20654. (org-agenda-find-same-or-today-or-agenda))
  20655. (org-agenda-set-mode-name)
  20656. (message "Switched to %s view" span))
  20657. (defun org-agenda-compute-time-span (sd span)
  20658. "Compute starting date and number of days for agenda.
  20659. SPAN may be `day', `week', `month', `year'. The return value
  20660. is a cons cell with the starting date and the number of days,
  20661. so that the date SD will be in that range."
  20662. (let* ((greg (calendar-gregorian-from-absolute sd))
  20663. nd)
  20664. (cond
  20665. ((eq span 'day)
  20666. (setq nd 1))
  20667. ((eq span 'week)
  20668. (let* ((nt (calendar-day-of-week
  20669. (calendar-gregorian-from-absolute sd)))
  20670. (d (if org-agenda-start-on-weekday
  20671. (- nt org-agenda-start-on-weekday)
  20672. 0)))
  20673. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20674. (setq nd 7)))
  20675. ((eq span 'month)
  20676. (setq sd (calendar-absolute-from-gregorian
  20677. (list (car greg) 1 (nth 2 greg)))
  20678. nd (- (calendar-absolute-from-gregorian
  20679. (list (1+ (car greg)) 1 (nth 2 greg)))
  20680. sd)))
  20681. ((eq span 'year)
  20682. (setq sd (calendar-absolute-from-gregorian
  20683. (list 1 1 (nth 2 greg)))
  20684. nd (- (calendar-absolute-from-gregorian
  20685. (list 1 1 (1+ (nth 2 greg))))
  20686. sd))))
  20687. (cons sd nd)))
  20688. ;; FIXME: does not work if user makes date format that starts with a blank
  20689. (defun org-agenda-next-date-line (&optional arg)
  20690. "Jump to the next line indicating a date in agenda buffer."
  20691. (interactive "p")
  20692. (org-agenda-check-type t 'agenda 'timeline)
  20693. (beginning-of-line 1)
  20694. (if (looking-at "^\\S-") (forward-char 1))
  20695. (if (not (re-search-forward "^\\S-" nil t arg))
  20696. (progn
  20697. (backward-char 1)
  20698. (error "No next date after this line in this buffer")))
  20699. (goto-char (match-beginning 0)))
  20700. (defun org-agenda-previous-date-line (&optional arg)
  20701. "Jump to the previous line indicating a date in agenda buffer."
  20702. (interactive "p")
  20703. (org-agenda-check-type t 'agenda 'timeline)
  20704. (beginning-of-line 1)
  20705. (if (not (re-search-backward "^\\S-" nil t arg))
  20706. (error "No previous date before this line in this buffer")))
  20707. ;; Initialize the highlight
  20708. (defvar org-hl (org-make-overlay 1 1))
  20709. (org-overlay-put org-hl 'face 'highlight)
  20710. (defun org-highlight (begin end &optional buffer)
  20711. "Highlight a region with overlay."
  20712. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20713. org-hl begin end (or buffer (current-buffer))))
  20714. (defun org-unhighlight ()
  20715. "Detach overlay INDEX."
  20716. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20717. ;; FIXME this is currently not used.
  20718. (defun org-highlight-until-next-command (beg end &optional buffer)
  20719. (org-highlight beg end buffer)
  20720. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20721. (defun org-unhighlight-once ()
  20722. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20723. (org-unhighlight))
  20724. (defun org-agenda-follow-mode ()
  20725. "Toggle follow mode in an agenda buffer."
  20726. (interactive)
  20727. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20728. (org-agenda-set-mode-name)
  20729. (message "Follow mode is %s"
  20730. (if org-agenda-follow-mode "on" "off")))
  20731. (defun org-agenda-log-mode ()
  20732. "Toggle log mode in an agenda buffer."
  20733. (interactive)
  20734. (org-agenda-check-type t 'agenda 'timeline)
  20735. (setq org-agenda-show-log (not org-agenda-show-log))
  20736. (org-agenda-set-mode-name)
  20737. (org-agenda-redo)
  20738. (message "Log mode is %s"
  20739. (if org-agenda-show-log "on" "off")))
  20740. (defun org-agenda-toggle-diary ()
  20741. "Toggle diary inclusion in an agenda buffer."
  20742. (interactive)
  20743. (org-agenda-check-type t 'agenda)
  20744. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20745. (org-agenda-redo)
  20746. (org-agenda-set-mode-name)
  20747. (message "Diary inclusion turned %s"
  20748. (if org-agenda-include-diary "on" "off")))
  20749. (defun org-agenda-toggle-time-grid ()
  20750. "Toggle time grid in an agenda buffer."
  20751. (interactive)
  20752. (org-agenda-check-type t 'agenda)
  20753. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20754. (org-agenda-redo)
  20755. (org-agenda-set-mode-name)
  20756. (message "Time-grid turned %s"
  20757. (if org-agenda-use-time-grid "on" "off")))
  20758. (defun org-agenda-set-mode-name ()
  20759. "Set the mode name to indicate all the small mode settings."
  20760. (setq mode-name
  20761. (concat "Org-Agenda"
  20762. (if (equal org-agenda-ndays 1) " Day" "")
  20763. (if (equal org-agenda-ndays 7) " Week" "")
  20764. (if org-agenda-follow-mode " Follow" "")
  20765. (if org-agenda-include-diary " Diary" "")
  20766. (if org-agenda-use-time-grid " Grid" "")
  20767. (if org-agenda-show-log " Log" "")))
  20768. (force-mode-line-update))
  20769. (defun org-agenda-post-command-hook ()
  20770. (and (eolp) (not (bolp)) (backward-char 1))
  20771. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20772. (if (and org-agenda-follow-mode
  20773. (get-text-property (point) 'org-marker))
  20774. (org-agenda-show)))
  20775. (defun org-agenda-show-priority ()
  20776. "Show the priority of the current item.
  20777. This priority is composed of the main priority given with the [#A] cookies,
  20778. and by additional input from the age of a schedules or deadline entry."
  20779. (interactive)
  20780. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20781. (message "Priority is %d" (if pri pri -1000))))
  20782. (defun org-agenda-show-tags ()
  20783. "Show the tags applicable to the current item."
  20784. (interactive)
  20785. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20786. (if tags
  20787. (message "Tags are :%s:"
  20788. (org-no-properties (mapconcat 'identity tags ":")))
  20789. (message "No tags associated with this line"))))
  20790. (defun org-agenda-goto (&optional highlight)
  20791. "Go to the Org-mode file which contains the item at point."
  20792. (interactive)
  20793. (let* ((marker (or (get-text-property (point) 'org-marker)
  20794. (org-agenda-error)))
  20795. (buffer (marker-buffer marker))
  20796. (pos (marker-position marker)))
  20797. (switch-to-buffer-other-window buffer)
  20798. (widen)
  20799. (goto-char pos)
  20800. (when (org-mode-p)
  20801. (org-show-context 'agenda)
  20802. (save-excursion
  20803. (and (outline-next-heading)
  20804. (org-flag-heading nil)))) ; show the next heading
  20805. (recenter (/ (window-height) 2))
  20806. (run-hooks 'org-agenda-after-show-hook)
  20807. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20808. (defvar org-agenda-after-show-hook nil
  20809. "Normal hook run after an item has been shown from the agenda.
  20810. Point is in the buffer where the item originated.")
  20811. (defun org-agenda-kill ()
  20812. "Kill the entry or subtree belonging to the current agenda entry."
  20813. (interactive)
  20814. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20815. (let* ((marker (or (get-text-property (point) 'org-marker)
  20816. (org-agenda-error)))
  20817. (buffer (marker-buffer marker))
  20818. (pos (marker-position marker))
  20819. (type (get-text-property (point) 'type))
  20820. dbeg dend (n 0) conf)
  20821. (org-with-remote-undo buffer
  20822. (with-current-buffer buffer
  20823. (save-excursion
  20824. (goto-char pos)
  20825. (if (and (org-mode-p) (not (member type '("sexp"))))
  20826. (setq dbeg (progn (org-back-to-heading t) (point))
  20827. dend (org-end-of-subtree t t))
  20828. (setq dbeg (point-at-bol)
  20829. dend (min (point-max) (1+ (point-at-eol)))))
  20830. (goto-char dbeg)
  20831. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20832. (setq conf (or (eq t org-agenda-confirm-kill)
  20833. (and (numberp org-agenda-confirm-kill)
  20834. (> n org-agenda-confirm-kill))))
  20835. (and conf
  20836. (not (y-or-n-p
  20837. (format "Delete entry with %d lines in buffer \"%s\"? "
  20838. n (buffer-name buffer))))
  20839. (error "Abort"))
  20840. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20841. (with-current-buffer buffer (delete-region dbeg dend))
  20842. (message "Agenda item and source killed"))))
  20843. (defun org-agenda-archive ()
  20844. "Kill the entry or subtree belonging to the current agenda entry."
  20845. (interactive)
  20846. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20847. (let* ((marker (or (get-text-property (point) 'org-marker)
  20848. (org-agenda-error)))
  20849. (buffer (marker-buffer marker))
  20850. (pos (marker-position marker)))
  20851. (org-with-remote-undo buffer
  20852. (with-current-buffer buffer
  20853. (if (org-mode-p)
  20854. (save-excursion
  20855. (goto-char pos)
  20856. (org-remove-subtree-entries-from-agenda)
  20857. (org-back-to-heading t)
  20858. (org-archive-subtree))
  20859. (error "Archiving works only in Org-mode files"))))))
  20860. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20861. "Remove all lines in the agenda that correspond to a given subtree.
  20862. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20863. If this information is not given, the function uses the tree at point."
  20864. (let ((buf (or buf (current-buffer))) m p)
  20865. (save-excursion
  20866. (unless (and beg end)
  20867. (org-back-to-heading t)
  20868. (setq beg (point))
  20869. (org-end-of-subtree t)
  20870. (setq end (point)))
  20871. (set-buffer (get-buffer org-agenda-buffer-name))
  20872. (save-excursion
  20873. (goto-char (point-max))
  20874. (beginning-of-line 1)
  20875. (while (not (bobp))
  20876. (when (and (setq m (get-text-property (point) 'org-marker))
  20877. (equal buf (marker-buffer m))
  20878. (setq p (marker-position m))
  20879. (>= p beg)
  20880. (<= p end))
  20881. (let ((inhibit-read-only t))
  20882. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20883. (beginning-of-line 0))))))
  20884. (defun org-agenda-open-link ()
  20885. "Follow the link in the current line, if any."
  20886. (interactive)
  20887. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20888. (save-excursion
  20889. (save-restriction
  20890. (narrow-to-region (point-at-bol) (point-at-eol))
  20891. (org-open-at-point))))
  20892. (defun org-agenda-copy-local-variable (var)
  20893. "Get a variable from a referenced buffer and install it here."
  20894. (let ((m (get-text-property (point) 'org-marker)))
  20895. (when (and m (buffer-live-p (marker-buffer m)))
  20896. (org-set-local var (with-current-buffer (marker-buffer m)
  20897. (symbol-value var))))))
  20898. (defun org-agenda-switch-to (&optional delete-other-windows)
  20899. "Go to the Org-mode file which contains the item at point."
  20900. (interactive)
  20901. (let* ((marker (or (get-text-property (point) 'org-marker)
  20902. (org-agenda-error)))
  20903. (buffer (marker-buffer marker))
  20904. (pos (marker-position marker)))
  20905. (switch-to-buffer buffer)
  20906. (and delete-other-windows (delete-other-windows))
  20907. (widen)
  20908. (goto-char pos)
  20909. (when (org-mode-p)
  20910. (org-show-context 'agenda)
  20911. (save-excursion
  20912. (and (outline-next-heading)
  20913. (org-flag-heading nil)))))) ; show the next heading
  20914. (defun org-agenda-goto-mouse (ev)
  20915. "Go to the Org-mode file which contains the item at the mouse click."
  20916. (interactive "e")
  20917. (mouse-set-point ev)
  20918. (org-agenda-goto))
  20919. (defun org-agenda-show ()
  20920. "Display the Org-mode file which contains the item at point."
  20921. (interactive)
  20922. (let ((win (selected-window)))
  20923. (org-agenda-goto t)
  20924. (select-window win)))
  20925. (defun org-agenda-recenter (arg)
  20926. "Display the Org-mode file which contains the item at point and recenter."
  20927. (interactive "P")
  20928. (let ((win (selected-window)))
  20929. (org-agenda-goto t)
  20930. (recenter arg)
  20931. (select-window win)))
  20932. (defun org-agenda-show-mouse (ev)
  20933. "Display the Org-mode file which contains the item at the mouse click."
  20934. (interactive "e")
  20935. (mouse-set-point ev)
  20936. (org-agenda-show))
  20937. (defun org-agenda-check-no-diary ()
  20938. "Check if the entry is a diary link and abort if yes."
  20939. (if (get-text-property (point) 'org-agenda-diary-link)
  20940. (org-agenda-error)))
  20941. (defun org-agenda-error ()
  20942. (error "Command not allowed in this line"))
  20943. (defun org-agenda-tree-to-indirect-buffer ()
  20944. "Show the subtree corresponding to the current entry in an indirect buffer.
  20945. This calls the command `org-tree-to-indirect-buffer' from the original
  20946. Org-mode buffer.
  20947. With numerical prefix arg ARG, go up to this level and then take that tree.
  20948. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20949. dedicated frame)."
  20950. (interactive)
  20951. (org-agenda-check-no-diary)
  20952. (let* ((marker (or (get-text-property (point) 'org-marker)
  20953. (org-agenda-error)))
  20954. (buffer (marker-buffer marker))
  20955. (pos (marker-position marker)))
  20956. (with-current-buffer buffer
  20957. (save-excursion
  20958. (goto-char pos)
  20959. (call-interactively 'org-tree-to-indirect-buffer)))))
  20960. (defvar org-last-heading-marker (make-marker)
  20961. "Marker pointing to the headline that last changed its TODO state
  20962. by a remote command from the agenda.")
  20963. (defun org-agenda-todo-nextset ()
  20964. "Switch TODO entry to next sequence."
  20965. (interactive)
  20966. (org-agenda-todo 'nextset))
  20967. (defun org-agenda-todo-previousset ()
  20968. "Switch TODO entry to previous sequence."
  20969. (interactive)
  20970. (org-agenda-todo 'previousset))
  20971. (defun org-agenda-todo (&optional arg)
  20972. "Cycle TODO state of line at point, also in Org-mode file.
  20973. This changes the line at point, all other lines in the agenda referring to
  20974. the same tree node, and the headline of the tree node in the Org-mode file."
  20975. (interactive "P")
  20976. (org-agenda-check-no-diary)
  20977. (let* ((col (current-column))
  20978. (marker (or (get-text-property (point) 'org-marker)
  20979. (org-agenda-error)))
  20980. (buffer (marker-buffer marker))
  20981. (pos (marker-position marker))
  20982. (hdmarker (get-text-property (point) 'org-hd-marker))
  20983. (inhibit-read-only t)
  20984. newhead)
  20985. (org-with-remote-undo buffer
  20986. (with-current-buffer buffer
  20987. (widen)
  20988. (goto-char pos)
  20989. (org-show-context 'agenda)
  20990. (save-excursion
  20991. (and (outline-next-heading)
  20992. (org-flag-heading nil))) ; show the next heading
  20993. (org-todo arg)
  20994. (and (bolp) (forward-char 1))
  20995. (setq newhead (org-get-heading))
  20996. (save-excursion
  20997. (org-back-to-heading)
  20998. (move-marker org-last-heading-marker (point))))
  20999. (beginning-of-line 1)
  21000. (save-excursion
  21001. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21002. (move-to-column col))))
  21003. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21004. "Change all lines in the agenda buffer which match HDMARKER.
  21005. The new content of the line will be NEWHEAD (as modified by
  21006. `org-format-agenda-item'). HDMARKER is checked with
  21007. `equal' against all `org-hd-marker' text properties in the file.
  21008. If FIXFACE is non-nil, the face of each item is modified acording to
  21009. the new TODO state."
  21010. (let* ((inhibit-read-only t)
  21011. props m pl undone-face done-face finish new dotime cat tags)
  21012. (save-excursion
  21013. (goto-char (point-max))
  21014. (beginning-of-line 1)
  21015. (while (not finish)
  21016. (setq finish (bobp))
  21017. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21018. (equal m hdmarker))
  21019. (setq props (text-properties-at (point))
  21020. dotime (get-text-property (point) 'dotime)
  21021. cat (get-text-property (point) 'org-category)
  21022. tags (get-text-property (point) 'tags)
  21023. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21024. pl (get-text-property (point) 'prefix-length)
  21025. undone-face (get-text-property (point) 'undone-face)
  21026. done-face (get-text-property (point) 'done-face))
  21027. (move-to-column pl)
  21028. (cond
  21029. ((equal new "")
  21030. (beginning-of-line 1)
  21031. (and (looking-at ".*\n?") (replace-match "")))
  21032. ((looking-at ".*")
  21033. (replace-match new t t)
  21034. (beginning-of-line 1)
  21035. (add-text-properties (point-at-bol) (point-at-eol) props)
  21036. (when fixface
  21037. (add-text-properties
  21038. (point-at-bol) (point-at-eol)
  21039. (list 'face
  21040. (if org-last-todo-state-is-todo
  21041. undone-face done-face))))
  21042. (org-agenda-highlight-todo 'line)
  21043. (beginning-of-line 1))
  21044. (t (error "Line update did not work"))))
  21045. (beginning-of-line 0)))
  21046. (org-finalize-agenda)))
  21047. (defun org-agenda-align-tags (&optional line)
  21048. "Align all tags in agenda items to `org-agenda-tags-column'."
  21049. (let ((inhibit-read-only t) l c)
  21050. (save-excursion
  21051. (goto-char (if line (point-at-bol) (point-min)))
  21052. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21053. (if line (point-at-eol) nil) t)
  21054. (add-text-properties
  21055. (match-beginning 2) (match-end 2)
  21056. (list 'face (list 'org-tag (get-text-property
  21057. (match-beginning 2) 'face))))
  21058. (setq l (- (match-end 2) (match-beginning 2))
  21059. c (if (< org-agenda-tags-column 0)
  21060. (- (abs org-agenda-tags-column) l)
  21061. org-agenda-tags-column))
  21062. (delete-region (match-beginning 1) (match-end 1))
  21063. (goto-char (match-beginning 1))
  21064. (insert (org-add-props
  21065. (make-string (max 1 (- c (current-column))) ?\ )
  21066. (text-properties-at (point))))))))
  21067. (defun org-agenda-priority-up ()
  21068. "Increase the priority of line at point, also in Org-mode file."
  21069. (interactive)
  21070. (org-agenda-priority 'up))
  21071. (defun org-agenda-priority-down ()
  21072. "Decrease the priority of line at point, also in Org-mode file."
  21073. (interactive)
  21074. (org-agenda-priority 'down))
  21075. (defun org-agenda-priority (&optional force-direction)
  21076. "Set the priority of line at point, also in Org-mode file.
  21077. This changes the line at point, all other lines in the agenda referring to
  21078. the same tree node, and the headline of the tree node in the Org-mode file."
  21079. (interactive)
  21080. (org-agenda-check-no-diary)
  21081. (let* ((marker (or (get-text-property (point) 'org-marker)
  21082. (org-agenda-error)))
  21083. (hdmarker (get-text-property (point) 'org-hd-marker))
  21084. (buffer (marker-buffer hdmarker))
  21085. (pos (marker-position hdmarker))
  21086. (inhibit-read-only t)
  21087. newhead)
  21088. (org-with-remote-undo buffer
  21089. (with-current-buffer buffer
  21090. (widen)
  21091. (goto-char pos)
  21092. (org-show-context 'agenda)
  21093. (save-excursion
  21094. (and (outline-next-heading)
  21095. (org-flag-heading nil))) ; show the next heading
  21096. (funcall 'org-priority force-direction)
  21097. (end-of-line 1)
  21098. (setq newhead (org-get-heading)))
  21099. (org-agenda-change-all-lines newhead hdmarker)
  21100. (beginning-of-line 1))))
  21101. (defun org-get-tags-at (&optional pos)
  21102. "Get a list of all headline tags applicable at POS.
  21103. POS defaults to point. If tags are inherited, the list contains
  21104. the targets in the same sequence as the headlines appear, i.e.
  21105. the tags of the current headline come last."
  21106. (interactive)
  21107. (let (tags lastpos)
  21108. (save-excursion
  21109. (save-restriction
  21110. (widen)
  21111. (goto-char (or pos (point)))
  21112. (save-match-data
  21113. (org-back-to-heading t)
  21114. (condition-case nil
  21115. (while (not (equal lastpos (point)))
  21116. (setq lastpos (point))
  21117. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21118. (setq tags (append (org-split-string
  21119. (org-match-string-no-properties 1) ":")
  21120. tags)))
  21121. (or org-use-tag-inheritance (error ""))
  21122. (org-up-heading-all 1))
  21123. (error nil))))
  21124. tags)))
  21125. ;; FIXME: should fix the tags property of the agenda line.
  21126. (defun org-agenda-set-tags ()
  21127. "Set tags for the current headline."
  21128. (interactive)
  21129. (org-agenda-check-no-diary)
  21130. (if (and (org-region-active-p) (interactive-p))
  21131. (call-interactively 'org-change-tag-in-region)
  21132. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21133. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21134. (org-agenda-error)))
  21135. (buffer (marker-buffer hdmarker))
  21136. (pos (marker-position hdmarker))
  21137. (inhibit-read-only t)
  21138. newhead)
  21139. (org-with-remote-undo buffer
  21140. (with-current-buffer buffer
  21141. (widen)
  21142. (goto-char pos)
  21143. (save-excursion
  21144. (org-show-context 'agenda))
  21145. (save-excursion
  21146. (and (outline-next-heading)
  21147. (org-flag-heading nil))) ; show the next heading
  21148. (goto-char pos)
  21149. (call-interactively 'org-set-tags)
  21150. (end-of-line 1)
  21151. (setq newhead (org-get-heading)))
  21152. (org-agenda-change-all-lines newhead hdmarker)
  21153. (beginning-of-line 1)))))
  21154. (defun org-agenda-toggle-archive-tag ()
  21155. "Toggle the archive tag for the current entry."
  21156. (interactive)
  21157. (org-agenda-check-no-diary)
  21158. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21159. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21160. (org-agenda-error)))
  21161. (buffer (marker-buffer hdmarker))
  21162. (pos (marker-position hdmarker))
  21163. (inhibit-read-only t)
  21164. newhead)
  21165. (org-with-remote-undo buffer
  21166. (with-current-buffer buffer
  21167. (widen)
  21168. (goto-char pos)
  21169. (org-show-context 'agenda)
  21170. (save-excursion
  21171. (and (outline-next-heading)
  21172. (org-flag-heading nil))) ; show the next heading
  21173. (call-interactively 'org-toggle-archive-tag)
  21174. (end-of-line 1)
  21175. (setq newhead (org-get-heading)))
  21176. (org-agenda-change-all-lines newhead hdmarker)
  21177. (beginning-of-line 1))))
  21178. (defun org-agenda-date-later (arg &optional what)
  21179. "Change the date of this item to one day later."
  21180. (interactive "p")
  21181. (org-agenda-check-type t 'agenda 'timeline)
  21182. (org-agenda-check-no-diary)
  21183. (let* ((marker (or (get-text-property (point) 'org-marker)
  21184. (org-agenda-error)))
  21185. (buffer (marker-buffer marker))
  21186. (pos (marker-position marker)))
  21187. (org-with-remote-undo buffer
  21188. (with-current-buffer buffer
  21189. (widen)
  21190. (goto-char pos)
  21191. (if (not (org-at-timestamp-p))
  21192. (error "Cannot find time stamp"))
  21193. (org-timestamp-change arg (or what 'day)))
  21194. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21195. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21196. (defun org-agenda-date-earlier (arg &optional what)
  21197. "Change the date of this item to one day earlier."
  21198. (interactive "p")
  21199. (org-agenda-date-later (- arg) what))
  21200. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21201. "Show new date stamp via text properties."
  21202. ;; We use text properties to make this undoable
  21203. (let ((inhibit-read-only t))
  21204. (setq stamp (concat " " prefix " => " stamp))
  21205. (save-excursion
  21206. (goto-char (point-max))
  21207. (while (not (bobp))
  21208. (when (equal marker (get-text-property (point) 'org-marker))
  21209. (move-to-column (- (window-width) (length stamp)) t)
  21210. (if (featurep 'xemacs)
  21211. ;; Use `duplicable' property to trigger undo recording
  21212. (let ((ex (make-extent nil nil))
  21213. (gl (make-glyph stamp)))
  21214. (set-glyph-face gl 'secondary-selection)
  21215. (set-extent-properties
  21216. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21217. (insert-extent ex (1- (point)) (point-at-eol)))
  21218. (add-text-properties
  21219. (1- (point)) (point-at-eol)
  21220. (list 'display (org-add-props stamp nil
  21221. 'face 'secondary-selection))))
  21222. (beginning-of-line 1))
  21223. (beginning-of-line 0)))))
  21224. (defun org-agenda-date-prompt (arg)
  21225. "Change the date of this item. Date is prompted for, with default today.
  21226. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21227. be used to request time specification in the time stamp."
  21228. (interactive "P")
  21229. (org-agenda-check-type t 'agenda 'timeline)
  21230. (org-agenda-check-no-diary)
  21231. (let* ((marker (or (get-text-property (point) 'org-marker)
  21232. (org-agenda-error)))
  21233. (buffer (marker-buffer marker))
  21234. (pos (marker-position marker)))
  21235. (org-with-remote-undo buffer
  21236. (with-current-buffer buffer
  21237. (widen)
  21238. (goto-char pos)
  21239. (if (not (org-at-timestamp-p))
  21240. (error "Cannot find time stamp"))
  21241. (org-time-stamp arg)
  21242. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21243. (defun org-agenda-schedule (arg)
  21244. "Schedule the item at point."
  21245. (interactive "P")
  21246. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21247. (org-agenda-check-no-diary)
  21248. (let* ((marker (or (get-text-property (point) 'org-marker)
  21249. (org-agenda-error)))
  21250. (buffer (marker-buffer marker))
  21251. (pos (marker-position marker))
  21252. (org-insert-labeled-timestamps-at-point nil)
  21253. ts)
  21254. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21255. (set-marker-insertion-type marker t)
  21256. (org-with-remote-undo buffer
  21257. (with-current-buffer buffer
  21258. (widen)
  21259. (goto-char pos)
  21260. (setq ts (org-schedule arg)))
  21261. (org-agenda-show-new-time marker ts "S"))
  21262. (message "Item scheduled for %s" ts)))
  21263. (defun org-agenda-deadline (arg)
  21264. "Schedule the item at point."
  21265. (interactive "P")
  21266. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21267. (org-agenda-check-no-diary)
  21268. (let* ((marker (or (get-text-property (point) 'org-marker)
  21269. (org-agenda-error)))
  21270. (buffer (marker-buffer marker))
  21271. (pos (marker-position marker))
  21272. (org-insert-labeled-timestamps-at-point nil)
  21273. ts)
  21274. (org-with-remote-undo buffer
  21275. (with-current-buffer buffer
  21276. (widen)
  21277. (goto-char pos)
  21278. (setq ts (org-deadline arg)))
  21279. (org-agenda-show-new-time marker ts "S"))
  21280. (message "Deadline for this item set to %s" ts)))
  21281. (defun org-get-heading (&optional no-tags)
  21282. "Return the heading of the current entry, without the stars."
  21283. (save-excursion
  21284. (org-back-to-heading t)
  21285. (if (looking-at
  21286. (if no-tags
  21287. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21288. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21289. (match-string 1) "")))
  21290. (defun org-agenda-clock-in (&optional arg)
  21291. "Start the clock on the currently selected item."
  21292. (interactive "P")
  21293. (org-agenda-check-no-diary)
  21294. (let* ((marker (or (get-text-property (point) 'org-marker)
  21295. (org-agenda-error)))
  21296. (pos (marker-position marker)))
  21297. (org-with-remote-undo (marker-buffer marker)
  21298. (with-current-buffer (marker-buffer marker)
  21299. (widen)
  21300. (goto-char pos)
  21301. (org-clock-in)))))
  21302. (defun org-agenda-clock-out (&optional arg)
  21303. "Stop the currently running clock."
  21304. (interactive "P")
  21305. (unless (marker-buffer org-clock-marker)
  21306. (error "No running clock"))
  21307. (org-with-remote-undo (marker-buffer org-clock-marker)
  21308. (org-clock-out)))
  21309. (defun org-agenda-clock-cancel (&optional arg)
  21310. "Cancel the currently running clock."
  21311. (interactive "P")
  21312. (unless (marker-buffer org-clock-marker)
  21313. (error "No running clock"))
  21314. (org-with-remote-undo (marker-buffer org-clock-marker)
  21315. (org-clock-cancel)))
  21316. (defun org-agenda-diary-entry ()
  21317. "Make a diary entry, like the `i' command from the calendar.
  21318. All the standard commands work: block, weekly etc."
  21319. (interactive)
  21320. (org-agenda-check-type t 'agenda 'timeline)
  21321. (require 'diary-lib)
  21322. (let* ((char (progn
  21323. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21324. (read-char-exclusive)))
  21325. (cmd (cdr (assoc char
  21326. '((?d . insert-diary-entry)
  21327. (?w . insert-weekly-diary-entry)
  21328. (?m . insert-monthly-diary-entry)
  21329. (?y . insert-yearly-diary-entry)
  21330. (?a . insert-anniversary-diary-entry)
  21331. (?b . insert-block-diary-entry)
  21332. (?c . insert-cyclic-diary-entry)))))
  21333. (oldf (symbol-function 'calendar-cursor-to-date))
  21334. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21335. (point (point))
  21336. (mark (or (mark t) (point))))
  21337. (unless cmd
  21338. (error "No command associated with <%c>" char))
  21339. (unless (and (get-text-property point 'day)
  21340. (or (not (equal ?b char))
  21341. (get-text-property mark 'day)))
  21342. (error "Don't know which date to use for diary entry"))
  21343. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21344. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21345. (let ((calendar-mark-ring
  21346. (list (calendar-gregorian-from-absolute
  21347. (or (get-text-property mark 'day)
  21348. (get-text-property point 'day))))))
  21349. (unwind-protect
  21350. (progn
  21351. (fset 'calendar-cursor-to-date
  21352. (lambda (&optional error)
  21353. (calendar-gregorian-from-absolute
  21354. (get-text-property point 'day))))
  21355. (call-interactively cmd))
  21356. (fset 'calendar-cursor-to-date oldf)))))
  21357. (defun org-agenda-execute-calendar-command (cmd)
  21358. "Execute a calendar command from the agenda, with the date associated to
  21359. the cursor position."
  21360. (org-agenda-check-type t 'agenda 'timeline)
  21361. (require 'diary-lib)
  21362. (unless (get-text-property (point) 'day)
  21363. (error "Don't know which date to use for calendar command"))
  21364. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21365. (point (point))
  21366. (date (calendar-gregorian-from-absolute
  21367. (get-text-property point 'day)))
  21368. ;; the following 3 vars are needed in the calendar
  21369. (displayed-day (extract-calendar-day date))
  21370. (displayed-month (extract-calendar-month date))
  21371. (displayed-year (extract-calendar-year date)))
  21372. (unwind-protect
  21373. (progn
  21374. (fset 'calendar-cursor-to-date
  21375. (lambda (&optional error)
  21376. (calendar-gregorian-from-absolute
  21377. (get-text-property point 'day))))
  21378. (call-interactively cmd))
  21379. (fset 'calendar-cursor-to-date oldf))))
  21380. (defun org-agenda-phases-of-moon ()
  21381. "Display the phases of the moon for the 3 months around the cursor date."
  21382. (interactive)
  21383. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21384. (defun org-agenda-holidays ()
  21385. "Display the holidays for the 3 months around the cursor date."
  21386. (interactive)
  21387. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21388. (defun org-agenda-sunrise-sunset (arg)
  21389. "Display sunrise and sunset for the cursor date.
  21390. Latitude and longitude can be specified with the variables
  21391. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21392. argument, latitude and longitude will be prompted for."
  21393. (interactive "P")
  21394. (let ((calendar-longitude (if arg nil calendar-longitude))
  21395. (calendar-latitude (if arg nil calendar-latitude))
  21396. (calendar-location-name
  21397. (if arg "the given coordinates" calendar-location-name)))
  21398. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21399. (defun org-agenda-goto-calendar ()
  21400. "Open the Emacs calendar with the date at the cursor."
  21401. (interactive)
  21402. (org-agenda-check-type t 'agenda 'timeline)
  21403. (let* ((day (or (get-text-property (point) 'day)
  21404. (error "Don't know which date to open in calendar")))
  21405. (date (calendar-gregorian-from-absolute day))
  21406. (calendar-move-hook nil)
  21407. (view-calendar-holidays-initially nil)
  21408. (view-diary-entries-initially nil))
  21409. (calendar)
  21410. (calendar-goto-date date)))
  21411. (defun org-calendar-goto-agenda ()
  21412. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21413. This is a command that has to be installed in `calendar-mode-map'."
  21414. (interactive)
  21415. (org-agenda-list nil (calendar-absolute-from-gregorian
  21416. (calendar-cursor-to-date))
  21417. nil))
  21418. (defun org-agenda-convert-date ()
  21419. (interactive)
  21420. (org-agenda-check-type t 'agenda 'timeline)
  21421. (let ((day (get-text-property (point) 'day))
  21422. date s)
  21423. (unless day
  21424. (error "Don't know which date to convert"))
  21425. (setq date (calendar-gregorian-from-absolute day))
  21426. (setq s (concat
  21427. "Gregorian: " (calendar-date-string date) "\n"
  21428. "ISO: " (calendar-iso-date-string date) "\n"
  21429. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21430. "Julian: " (calendar-julian-date-string date) "\n"
  21431. "Astron. JD: " (calendar-astro-date-string date)
  21432. " (Julian date number at noon UTC)\n"
  21433. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21434. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21435. "French: " (calendar-french-date-string date) "\n"
  21436. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21437. "Mayan: " (calendar-mayan-date-string date) "\n"
  21438. "Coptic: " (calendar-coptic-date-string date) "\n"
  21439. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21440. "Persian: " (calendar-persian-date-string date) "\n"
  21441. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21442. (with-output-to-temp-buffer "*Dates*"
  21443. (princ s))
  21444. (if (fboundp 'fit-window-to-buffer)
  21445. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21446. ;;;; Embedded LaTeX
  21447. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21448. "Keymap for the minor `org-cdlatex-mode'.")
  21449. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21450. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21451. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21452. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21453. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21454. (defvar org-cdlatex-texmathp-advice-is-done nil
  21455. "Flag remembering if we have applied the advice to texmathp already.")
  21456. (define-minor-mode org-cdlatex-mode
  21457. "Toggle the minor `org-cdlatex-mode'.
  21458. This mode supports entering LaTeX environment and math in LaTeX fragments
  21459. in Org-mode.
  21460. \\{org-cdlatex-mode-map}"
  21461. nil " OCDL" nil
  21462. (when org-cdlatex-mode (require 'cdlatex))
  21463. (unless org-cdlatex-texmathp-advice-is-done
  21464. (setq org-cdlatex-texmathp-advice-is-done t)
  21465. (defadvice texmathp (around org-math-always-on activate)
  21466. "Always return t in org-mode buffers.
  21467. This is because we want to insert math symbols without dollars even outside
  21468. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21469. en embedded LaTeX fragement, let texmathp do its job.
  21470. \\[org-cdlatex-mode-map]"
  21471. (interactive)
  21472. (let (p)
  21473. (cond
  21474. ((not (org-mode-p)) ad-do-it)
  21475. ((eq this-command 'cdlatex-math-symbol)
  21476. (setq ad-return-value t
  21477. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21478. (t
  21479. (let ((p (org-inside-LaTeX-fragment-p)))
  21480. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21481. (setq ad-return-value t
  21482. texmathp-why '("Org-mode embedded math" . 0))
  21483. (if p ad-do-it)))))))))
  21484. (defun turn-on-org-cdlatex ()
  21485. "Unconditionally turn on `org-cdlatex-mode'."
  21486. (org-cdlatex-mode 1))
  21487. (defun org-inside-LaTeX-fragment-p ()
  21488. "Test if point is inside a LaTeX fragment.
  21489. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21490. sequence appearing also before point.
  21491. Even though the matchers for math are configurable, this function assumes
  21492. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21493. delimiters are skipped when they have been removed by customization.
  21494. The return value is nil, or a cons cell with the delimiter and
  21495. and the position of this delimiter.
  21496. This function does a reasonably good job, but can locally be fooled by
  21497. for example currency specifications. For example it will assume being in
  21498. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21499. fragments that are properly closed, but during editing, we have to live
  21500. with the uncertainty caused by missing closing delimiters. This function
  21501. looks only before point, not after."
  21502. (catch 'exit
  21503. (let ((pos (point))
  21504. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21505. (lim (progn
  21506. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21507. (point)))
  21508. dd-on str (start 0) m re)
  21509. (goto-char pos)
  21510. (when dodollar
  21511. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21512. re (nth 1 (assoc "$" org-latex-regexps)))
  21513. (while (string-match re str start)
  21514. (cond
  21515. ((= (match-end 0) (length str))
  21516. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21517. ((= (match-end 0) (- (length str) 5))
  21518. (throw 'exit nil))
  21519. (t (setq start (match-end 0))))))
  21520. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21521. (goto-char pos)
  21522. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21523. (and (match-beginning 2) (throw 'exit nil))
  21524. ;; count $$
  21525. (while (re-search-backward "\\$\\$" lim t)
  21526. (setq dd-on (not dd-on)))
  21527. (goto-char pos)
  21528. (if dd-on (cons "$$" m))))))
  21529. (defun org-try-cdlatex-tab ()
  21530. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21531. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21532. - inside a LaTeX fragment, or
  21533. - after the first word in a line, where an abbreviation expansion could
  21534. insert a LaTeX environment."
  21535. (when org-cdlatex-mode
  21536. (cond
  21537. ((save-excursion
  21538. (skip-chars-backward "a-zA-Z0-9*")
  21539. (skip-chars-backward " \t")
  21540. (bolp))
  21541. (cdlatex-tab) t)
  21542. ((org-inside-LaTeX-fragment-p)
  21543. (cdlatex-tab) t)
  21544. (t nil))))
  21545. (defun org-cdlatex-underscore-caret (&optional arg)
  21546. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21547. Revert to the normal definition outside of these fragments."
  21548. (interactive "P")
  21549. (if (org-inside-LaTeX-fragment-p)
  21550. (call-interactively 'cdlatex-sub-superscript)
  21551. (let (org-cdlatex-mode)
  21552. (call-interactively (key-binding (vector last-input-event))))))
  21553. (defun org-cdlatex-math-modify (&optional arg)
  21554. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21555. Revert to the normal definition outside of these fragments."
  21556. (interactive "P")
  21557. (if (org-inside-LaTeX-fragment-p)
  21558. (call-interactively 'cdlatex-math-modify)
  21559. (let (org-cdlatex-mode)
  21560. (call-interactively (key-binding (vector last-input-event))))))
  21561. (defvar org-latex-fragment-image-overlays nil
  21562. "List of overlays carrying the images of latex fragments.")
  21563. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21564. (defun org-remove-latex-fragment-image-overlays ()
  21565. "Remove all overlays with LaTeX fragment images in current buffer."
  21566. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21567. (setq org-latex-fragment-image-overlays nil))
  21568. (defun org-preview-latex-fragment (&optional subtree)
  21569. "Preview the LaTeX fragment at point, or all locally or globally.
  21570. If the cursor is in a LaTeX fragment, create the image and overlay
  21571. it over the source code. If there is no fragment at point, display
  21572. all fragments in the current text, from one headline to the next. With
  21573. prefix SUBTREE, display all fragments in the current subtree. With a
  21574. double prefix `C-u C-u', or when the cursor is before the first headline,
  21575. display all fragments in the buffer.
  21576. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21577. (interactive "P")
  21578. (org-remove-latex-fragment-image-overlays)
  21579. (save-excursion
  21580. (save-restriction
  21581. (let (beg end at msg)
  21582. (cond
  21583. ((or (equal subtree '(16))
  21584. (not (save-excursion
  21585. (re-search-backward (concat "^" outline-regexp) nil t))))
  21586. (setq beg (point-min) end (point-max)
  21587. msg "Creating images for buffer...%s"))
  21588. ((equal subtree '(4))
  21589. (org-back-to-heading)
  21590. (setq beg (point) end (org-end-of-subtree t)
  21591. msg "Creating images for subtree...%s"))
  21592. (t
  21593. (if (setq at (org-inside-LaTeX-fragment-p))
  21594. (goto-char (max (point-min) (- (cdr at) 2)))
  21595. (org-back-to-heading))
  21596. (setq beg (point) end (progn (outline-next-heading) (point))
  21597. msg (if at "Creating image...%s"
  21598. "Creating images for entry...%s"))))
  21599. (message msg "")
  21600. (narrow-to-region beg end)
  21601. (goto-char beg)
  21602. (org-format-latex
  21603. (concat "ltxpng/" (file-name-sans-extension
  21604. (file-name-nondirectory
  21605. buffer-file-name)))
  21606. default-directory 'overlays msg at 'forbuffer)
  21607. (message msg "done. Use `C-c C-c' to remove images.")))))
  21608. (defvar org-latex-regexps
  21609. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21610. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21611. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21612. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21613. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21614. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21615. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21616. "Regular expressions for matching embedded LaTeX.")
  21617. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21618. "Replace LaTeX fragments with links to an image, and produce images."
  21619. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21620. (let* ((prefixnodir (file-name-nondirectory prefix))
  21621. (absprefix (expand-file-name prefix dir))
  21622. (todir (file-name-directory absprefix))
  21623. (opt org-format-latex-options)
  21624. (matchers (plist-get opt :matchers))
  21625. (re-list org-latex-regexps)
  21626. (cnt 0) txt link beg end re e checkdir
  21627. m n block linkfile movefile ov)
  21628. ;; Check if there are old images files with this prefix, and remove them
  21629. (when (file-directory-p todir)
  21630. (mapc 'delete-file
  21631. (directory-files
  21632. todir 'full
  21633. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21634. ;; Check the different regular expressions
  21635. (while (setq e (pop re-list))
  21636. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21637. block (if (nth 3 e) "\n\n" ""))
  21638. (when (member m matchers)
  21639. (goto-char (point-min))
  21640. (while (re-search-forward re nil t)
  21641. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21642. (setq txt (match-string n)
  21643. beg (match-beginning n) end (match-end n)
  21644. cnt (1+ cnt)
  21645. linkfile (format "%s_%04d.png" prefix cnt)
  21646. movefile (format "%s_%04d.png" absprefix cnt)
  21647. link (concat block "[[file:" linkfile "]]" block))
  21648. (if msg (message msg cnt))
  21649. (goto-char beg)
  21650. (unless checkdir ; make sure the directory exists
  21651. (setq checkdir t)
  21652. (or (file-directory-p todir) (make-directory todir)))
  21653. (org-create-formula-image
  21654. txt movefile opt forbuffer)
  21655. (if overlays
  21656. (progn
  21657. (setq ov (org-make-overlay beg end))
  21658. (if (featurep 'xemacs)
  21659. (progn
  21660. (org-overlay-put ov 'invisible t)
  21661. (org-overlay-put
  21662. ov 'end-glyph
  21663. (make-glyph (vector 'png :file movefile))))
  21664. (org-overlay-put
  21665. ov 'display
  21666. (list 'image :type 'png :file movefile :ascent 'center)))
  21667. (push ov org-latex-fragment-image-overlays)
  21668. (goto-char end))
  21669. (delete-region beg end)
  21670. (insert link))))))))
  21671. ;; This function borrows from Ganesh Swami's latex2png.el
  21672. (defun org-create-formula-image (string tofile options buffer)
  21673. (let* ((tmpdir (if (featurep 'xemacs)
  21674. (temp-directory)
  21675. temporary-file-directory))
  21676. (texfilebase (make-temp-name
  21677. (expand-file-name "orgtex" tmpdir)))
  21678. (texfile (concat texfilebase ".tex"))
  21679. (dvifile (concat texfilebase ".dvi"))
  21680. (pngfile (concat texfilebase ".png"))
  21681. (fnh (face-attribute 'default :height nil))
  21682. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21683. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21684. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21685. "Black"))
  21686. (bg (or (plist-get options (if buffer :background :html-background))
  21687. "Transparent")))
  21688. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21689. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21690. (with-temp-file texfile
  21691. (insert org-format-latex-header
  21692. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21693. (let ((dir default-directory))
  21694. (condition-case nil
  21695. (progn
  21696. (cd tmpdir)
  21697. (call-process "latex" nil nil nil texfile))
  21698. (error nil))
  21699. (cd dir))
  21700. (if (not (file-exists-p dvifile))
  21701. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21702. (call-process "dvipng" nil nil nil
  21703. "-E" "-fg" fg "-bg" bg
  21704. "-D" dpi
  21705. ;;"-x" scale "-y" scale
  21706. "-T" "tight"
  21707. "-o" pngfile
  21708. dvifile)
  21709. (if (not (file-exists-p pngfile))
  21710. (progn (message "Failed to create png file from %s" texfile) nil)
  21711. ;; Use the requested file name and clean up
  21712. (copy-file pngfile tofile 'replace)
  21713. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21714. (delete-file (concat texfilebase e)))
  21715. pngfile))))
  21716. (defun org-dvipng-color (attr)
  21717. "Return an rgb color specification for dvipng."
  21718. (apply 'format "rgb %s %s %s"
  21719. (mapcar 'org-normalize-color
  21720. (color-values (face-attribute 'default attr nil)))))
  21721. (defun org-normalize-color (value)
  21722. "Return string to be used as color value for an RGB component."
  21723. (format "%g" (/ value 65535.0)))
  21724. ;;;; Exporting
  21725. ;;; Variables, constants, and parameter plists
  21726. (defconst org-level-max 20)
  21727. (defvar org-export-html-preamble nil
  21728. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21729. (defvar org-export-html-postamble nil
  21730. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21731. (defvar org-export-html-auto-preamble t
  21732. "Should default preamble be inserted? Set by publishing functions.")
  21733. (defvar org-export-html-auto-postamble t
  21734. "Should default postamble be inserted? Set by publishing functions.")
  21735. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21736. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21737. (defconst org-export-plist-vars
  21738. '((:language . org-export-default-language)
  21739. (:customtime . org-display-custom-times)
  21740. (:headline-levels . org-export-headline-levels)
  21741. (:section-numbers . org-export-with-section-numbers)
  21742. (:table-of-contents . org-export-with-toc)
  21743. (:preserve-breaks . org-export-preserve-breaks)
  21744. (:archived-trees . org-export-with-archived-trees)
  21745. (:emphasize . org-export-with-emphasize)
  21746. (:sub-superscript . org-export-with-sub-superscripts)
  21747. (:special-strings . org-export-with-special-strings)
  21748. (:footnotes . org-export-with-footnotes)
  21749. (:drawers . org-export-with-drawers)
  21750. (:tags . org-export-with-tags)
  21751. (:TeX-macros . org-export-with-TeX-macros)
  21752. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21753. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21754. (:fixed-width . org-export-with-fixed-width)
  21755. (:timestamps . org-export-with-timestamps)
  21756. (:author-info . org-export-author-info)
  21757. (:time-stamp-file . org-export-time-stamp-file)
  21758. (:tables . org-export-with-tables)
  21759. (:table-auto-headline . org-export-highlight-first-table-line)
  21760. (:style . org-export-html-style)
  21761. (:agenda-style . org-agenda-export-html-style)
  21762. (:convert-org-links . org-export-html-link-org-files-as-html)
  21763. (:inline-images . org-export-html-inline-images)
  21764. (:html-extension . org-export-html-extension)
  21765. (:html-table-tag . org-export-html-table-tag)
  21766. (:expand-quoted-html . org-export-html-expand)
  21767. (:timestamp . org-export-html-with-timestamp)
  21768. (:publishing-directory . org-export-publishing-directory)
  21769. (:preamble . org-export-html-preamble)
  21770. (:postamble . org-export-html-postamble)
  21771. (:auto-preamble . org-export-html-auto-preamble)
  21772. (:auto-postamble . org-export-html-auto-postamble)
  21773. (:author . user-full-name)
  21774. (:email . user-mail-address)))
  21775. (defun org-default-export-plist ()
  21776. "Return the property list with default settings for the export variables."
  21777. (let ((l org-export-plist-vars) rtn e)
  21778. (while (setq e (pop l))
  21779. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21780. rtn))
  21781. (defun org-infile-export-plist ()
  21782. "Return the property list with file-local settings for export."
  21783. (save-excursion
  21784. (save-restriction
  21785. (widen)
  21786. (goto-char 0)
  21787. (let ((re (org-make-options-regexp
  21788. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21789. p key val text options)
  21790. (while (re-search-forward re nil t)
  21791. (setq key (org-match-string-no-properties 1)
  21792. val (org-match-string-no-properties 2))
  21793. (cond
  21794. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21795. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21796. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21797. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21798. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21799. ((string-equal key "TEXT")
  21800. (setq text (if text (concat text "\n" val) val)))
  21801. ((string-equal key "OPTIONS") (setq options val))))
  21802. (setq p (plist-put p :text text))
  21803. (when options
  21804. (let ((op '(("H" . :headline-levels)
  21805. ("num" . :section-numbers)
  21806. ("toc" . :table-of-contents)
  21807. ("\\n" . :preserve-breaks)
  21808. ("@" . :expand-quoted-html)
  21809. (":" . :fixed-width)
  21810. ("|" . :tables)
  21811. ("^" . :sub-superscript)
  21812. ("-" . :special-strings)
  21813. ("f" . :footnotes)
  21814. ("d" . :drawers)
  21815. ("tags" . :tags)
  21816. ("*" . :emphasize)
  21817. ("TeX" . :TeX-macros)
  21818. ("LaTeX" . :LaTeX-fragments)
  21819. ("skip" . :skip-before-1st-heading)
  21820. ("author" . :author-info)
  21821. ("timestamp" . :time-stamp-file)))
  21822. o)
  21823. (while (setq o (pop op))
  21824. (if (string-match (concat (regexp-quote (car o))
  21825. ":\\([^ \t\n\r;,.]*\\)")
  21826. options)
  21827. (setq p (plist-put p (cdr o)
  21828. (car (read-from-string
  21829. (match-string 1 options)))))))))
  21830. p))))
  21831. (defun org-export-directory (type plist)
  21832. (let* ((val (plist-get plist :publishing-directory))
  21833. (dir (if (listp val)
  21834. (or (cdr (assoc type val)) ".")
  21835. val)))
  21836. dir))
  21837. (defun org-skip-comments (lines)
  21838. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21839. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21840. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21841. (case-fold-search nil)
  21842. rtn line level)
  21843. (while (setq line (pop lines))
  21844. (cond
  21845. ((and (string-match re1 line)
  21846. (setq level (- (match-end 1) (match-beginning 1))))
  21847. ;; Beginning of a COMMENT subtree. Skip it.
  21848. (while (and (setq line (pop lines))
  21849. (or (not (string-match re2 line))
  21850. (> (- (match-end 1) (match-beginning 1)) level))))
  21851. (setq lines (cons line lines)))
  21852. ((string-match "^#" line)
  21853. ;; an ordinary comment line
  21854. )
  21855. ((and org-export-table-remove-special-lines
  21856. (string-match "^[ \t]*|" line)
  21857. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21858. (and (string-match "| *<[0-9]+> *|" line)
  21859. (not (string-match "| *[^ <|]" line)))))
  21860. ;; a special table line that should be removed
  21861. )
  21862. (t (setq rtn (cons line rtn)))))
  21863. (nreverse rtn)))
  21864. (defun org-export (&optional arg)
  21865. (interactive)
  21866. (let ((help "[t] insert the export option template
  21867. \[v] limit export to visible part of outline tree
  21868. \[a] export as ASCII
  21869. \[h] export as HTML
  21870. \[H] export as HTML to temporary buffer
  21871. \[R] export region as HTML
  21872. \[b] export as HTML and browse immediately
  21873. \[x] export as XOXO
  21874. \[l] export as LaTeX
  21875. \[L] export as LaTeX to temporary buffer
  21876. \[i] export current file as iCalendar file
  21877. \[I] export all agenda files as iCalendar files
  21878. \[c] export agenda files into combined iCalendar file
  21879. \[F] publish current file
  21880. \[P] publish current project
  21881. \[X] publish... (project will be prompted for)
  21882. \[A] publish all projects")
  21883. (cmds
  21884. '((?t . org-insert-export-options-template)
  21885. (?v . org-export-visible)
  21886. (?a . org-export-as-ascii)
  21887. (?h . org-export-as-html)
  21888. (?b . org-export-as-html-and-open)
  21889. (?H . org-export-as-html-to-buffer)
  21890. (?R . org-export-region-as-html)
  21891. (?x . org-export-as-xoxo)
  21892. (?l . org-export-as-latex)
  21893. (?L . org-export-as-latex-to-buffer)
  21894. (?i . org-export-icalendar-this-file)
  21895. (?I . org-export-icalendar-all-agenda-files)
  21896. (?c . org-export-icalendar-combine-agenda-files)
  21897. (?F . org-publish-current-file)
  21898. (?P . org-publish-current-project)
  21899. (?X . org-publish)
  21900. (?A . org-publish-all)))
  21901. r1 r2 ass)
  21902. (save-window-excursion
  21903. (delete-other-windows)
  21904. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21905. (princ help))
  21906. (message "Select command: ")
  21907. (setq r1 (read-char-exclusive)))
  21908. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21909. (if (setq ass (assq r2 cmds))
  21910. (call-interactively (cdr ass))
  21911. (error "No command associated with key %c" r1))))
  21912. (defconst org-html-entities
  21913. '(("nbsp")
  21914. ("iexcl")
  21915. ("cent")
  21916. ("pound")
  21917. ("curren")
  21918. ("yen")
  21919. ("brvbar")
  21920. ("vert" . "&#124;")
  21921. ("sect")
  21922. ("uml")
  21923. ("copy")
  21924. ("ordf")
  21925. ("laquo")
  21926. ("not")
  21927. ("shy")
  21928. ("reg")
  21929. ("macr")
  21930. ("deg")
  21931. ("plusmn")
  21932. ("sup2")
  21933. ("sup3")
  21934. ("acute")
  21935. ("micro")
  21936. ("para")
  21937. ("middot")
  21938. ("odot"."o")
  21939. ("star"."*")
  21940. ("cedil")
  21941. ("sup1")
  21942. ("ordm")
  21943. ("raquo")
  21944. ("frac14")
  21945. ("frac12")
  21946. ("frac34")
  21947. ("iquest")
  21948. ("Agrave")
  21949. ("Aacute")
  21950. ("Acirc")
  21951. ("Atilde")
  21952. ("Auml")
  21953. ("Aring") ("AA"."&Aring;")
  21954. ("AElig")
  21955. ("Ccedil")
  21956. ("Egrave")
  21957. ("Eacute")
  21958. ("Ecirc")
  21959. ("Euml")
  21960. ("Igrave")
  21961. ("Iacute")
  21962. ("Icirc")
  21963. ("Iuml")
  21964. ("ETH")
  21965. ("Ntilde")
  21966. ("Ograve")
  21967. ("Oacute")
  21968. ("Ocirc")
  21969. ("Otilde")
  21970. ("Ouml")
  21971. ("times")
  21972. ("Oslash")
  21973. ("Ugrave")
  21974. ("Uacute")
  21975. ("Ucirc")
  21976. ("Uuml")
  21977. ("Yacute")
  21978. ("THORN")
  21979. ("szlig")
  21980. ("agrave")
  21981. ("aacute")
  21982. ("acirc")
  21983. ("atilde")
  21984. ("auml")
  21985. ("aring")
  21986. ("aelig")
  21987. ("ccedil")
  21988. ("egrave")
  21989. ("eacute")
  21990. ("ecirc")
  21991. ("euml")
  21992. ("igrave")
  21993. ("iacute")
  21994. ("icirc")
  21995. ("iuml")
  21996. ("eth")
  21997. ("ntilde")
  21998. ("ograve")
  21999. ("oacute")
  22000. ("ocirc")
  22001. ("otilde")
  22002. ("ouml")
  22003. ("divide")
  22004. ("oslash")
  22005. ("ugrave")
  22006. ("uacute")
  22007. ("ucirc")
  22008. ("uuml")
  22009. ("yacute")
  22010. ("thorn")
  22011. ("yuml")
  22012. ("fnof")
  22013. ("Alpha")
  22014. ("Beta")
  22015. ("Gamma")
  22016. ("Delta")
  22017. ("Epsilon")
  22018. ("Zeta")
  22019. ("Eta")
  22020. ("Theta")
  22021. ("Iota")
  22022. ("Kappa")
  22023. ("Lambda")
  22024. ("Mu")
  22025. ("Nu")
  22026. ("Xi")
  22027. ("Omicron")
  22028. ("Pi")
  22029. ("Rho")
  22030. ("Sigma")
  22031. ("Tau")
  22032. ("Upsilon")
  22033. ("Phi")
  22034. ("Chi")
  22035. ("Psi")
  22036. ("Omega")
  22037. ("alpha")
  22038. ("beta")
  22039. ("gamma")
  22040. ("delta")
  22041. ("epsilon")
  22042. ("varepsilon"."&epsilon;")
  22043. ("zeta")
  22044. ("eta")
  22045. ("theta")
  22046. ("iota")
  22047. ("kappa")
  22048. ("lambda")
  22049. ("mu")
  22050. ("nu")
  22051. ("xi")
  22052. ("omicron")
  22053. ("pi")
  22054. ("rho")
  22055. ("sigmaf") ("varsigma"."&sigmaf;")
  22056. ("sigma")
  22057. ("tau")
  22058. ("upsilon")
  22059. ("phi")
  22060. ("chi")
  22061. ("psi")
  22062. ("omega")
  22063. ("thetasym") ("vartheta"."&thetasym;")
  22064. ("upsih")
  22065. ("piv")
  22066. ("bull") ("bullet"."&bull;")
  22067. ("hellip") ("dots"."&hellip;")
  22068. ("prime")
  22069. ("Prime")
  22070. ("oline")
  22071. ("frasl")
  22072. ("weierp")
  22073. ("image")
  22074. ("real")
  22075. ("trade")
  22076. ("alefsym")
  22077. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22078. ("uarr") ("uparrow"."&uarr;")
  22079. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22080. ("darr")("downarrow"."&darr;")
  22081. ("harr") ("leftrightarrow"."&harr;")
  22082. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22083. ("lArr") ("Leftarrow"."&lArr;")
  22084. ("uArr") ("Uparrow"."&uArr;")
  22085. ("rArr") ("Rightarrow"."&rArr;")
  22086. ("dArr") ("Downarrow"."&dArr;")
  22087. ("hArr") ("Leftrightarrow"."&hArr;")
  22088. ("forall")
  22089. ("part") ("partial"."&part;")
  22090. ("exist") ("exists"."&exist;")
  22091. ("empty") ("emptyset"."&empty;")
  22092. ("nabla")
  22093. ("isin") ("in"."&isin;")
  22094. ("notin")
  22095. ("ni")
  22096. ("prod")
  22097. ("sum")
  22098. ("minus")
  22099. ("lowast") ("ast"."&lowast;")
  22100. ("radic")
  22101. ("prop") ("proptp"."&prop;")
  22102. ("infin") ("infty"."&infin;")
  22103. ("ang") ("angle"."&ang;")
  22104. ("and") ("wedge"."&and;")
  22105. ("or") ("vee"."&or;")
  22106. ("cap")
  22107. ("cup")
  22108. ("int")
  22109. ("there4")
  22110. ("sim")
  22111. ("cong") ("simeq"."&cong;")
  22112. ("asymp")("approx"."&asymp;")
  22113. ("ne") ("neq"."&ne;")
  22114. ("equiv")
  22115. ("le")
  22116. ("ge")
  22117. ("sub") ("subset"."&sub;")
  22118. ("sup") ("supset"."&sup;")
  22119. ("nsub")
  22120. ("sube")
  22121. ("supe")
  22122. ("oplus")
  22123. ("otimes")
  22124. ("perp")
  22125. ("sdot") ("cdot"."&sdot;")
  22126. ("lceil")
  22127. ("rceil")
  22128. ("lfloor")
  22129. ("rfloor")
  22130. ("lang")
  22131. ("rang")
  22132. ("loz") ("Diamond"."&loz;")
  22133. ("spades") ("spadesuit"."&spades;")
  22134. ("clubs") ("clubsuit"."&clubs;")
  22135. ("hearts") ("diamondsuit"."&hearts;")
  22136. ("diams") ("diamondsuit"."&diams;")
  22137. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22138. ("quot")
  22139. ("amp")
  22140. ("lt")
  22141. ("gt")
  22142. ("OElig")
  22143. ("oelig")
  22144. ("Scaron")
  22145. ("scaron")
  22146. ("Yuml")
  22147. ("circ")
  22148. ("tilde")
  22149. ("ensp")
  22150. ("emsp")
  22151. ("thinsp")
  22152. ("zwnj")
  22153. ("zwj")
  22154. ("lrm")
  22155. ("rlm")
  22156. ("ndash")
  22157. ("mdash")
  22158. ("lsquo")
  22159. ("rsquo")
  22160. ("sbquo")
  22161. ("ldquo")
  22162. ("rdquo")
  22163. ("bdquo")
  22164. ("dagger")
  22165. ("Dagger")
  22166. ("permil")
  22167. ("lsaquo")
  22168. ("rsaquo")
  22169. ("euro")
  22170. ("arccos"."arccos")
  22171. ("arcsin"."arcsin")
  22172. ("arctan"."arctan")
  22173. ("arg"."arg")
  22174. ("cos"."cos")
  22175. ("cosh"."cosh")
  22176. ("cot"."cot")
  22177. ("coth"."coth")
  22178. ("csc"."csc")
  22179. ("deg"."deg")
  22180. ("det"."det")
  22181. ("dim"."dim")
  22182. ("exp"."exp")
  22183. ("gcd"."gcd")
  22184. ("hom"."hom")
  22185. ("inf"."inf")
  22186. ("ker"."ker")
  22187. ("lg"."lg")
  22188. ("lim"."lim")
  22189. ("liminf"."liminf")
  22190. ("limsup"."limsup")
  22191. ("ln"."ln")
  22192. ("log"."log")
  22193. ("max"."max")
  22194. ("min"."min")
  22195. ("Pr"."Pr")
  22196. ("sec"."sec")
  22197. ("sin"."sin")
  22198. ("sinh"."sinh")
  22199. ("sup"."sup")
  22200. ("tan"."tan")
  22201. ("tanh"."tanh")
  22202. )
  22203. "Entities for TeX->HTML translation.
  22204. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22205. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22206. In that case, \"\\ent\" will be translated to \"&other;\".
  22207. The list contains HTML entities for Latin-1, Greek and other symbols.
  22208. It is supplemented by a number of commonly used TeX macros with appropriate
  22209. translations. There is currently no way for users to extend this.")
  22210. ;;; General functions for all backends
  22211. (defun org-cleaned-string-for-export (string &rest parameters)
  22212. "Cleanup a buffer STRING so that links can be created safely."
  22213. (interactive)
  22214. (let* ((re-radio (and org-target-link-regexp
  22215. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22216. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22217. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22218. (re-archive (concat ":" org-archive-tag ":"))
  22219. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22220. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22221. (htmlp (plist-get parameters :for-html))
  22222. (asciip (plist-get parameters :for-ascii))
  22223. (latexp (plist-get parameters :for-LaTeX))
  22224. (commentsp (plist-get parameters :comments))
  22225. (archived-trees (plist-get parameters :archived-trees))
  22226. (inhibit-read-only t)
  22227. (drawers org-drawers)
  22228. (exp-drawers (plist-get parameters :drawers))
  22229. (outline-regexp "\\*+ ")
  22230. a b xx
  22231. rtn p)
  22232. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22233. (erase-buffer)
  22234. (insert string)
  22235. ;; Remove license-to-kill stuff
  22236. (while (setq p (text-property-any (point-min) (point-max)
  22237. :org-license-to-kill t))
  22238. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22239. (let ((org-inhibit-startup t)) (org-mode))
  22240. (untabify (point-min) (point-max))
  22241. ;; Get rid of drawers
  22242. (unless (eq t exp-drawers)
  22243. (goto-char (point-min))
  22244. (let ((re (concat "^[ \t]*:\\("
  22245. (mapconcat
  22246. 'identity
  22247. (org-delete-all exp-drawers
  22248. (copy-sequence drawers))
  22249. "\\|")
  22250. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22251. (while (re-search-forward re nil t)
  22252. (replace-match ""))))
  22253. ;; Get the correct stuff before the first headline
  22254. (when (plist-get parameters :skip-before-1st-heading)
  22255. (goto-char (point-min))
  22256. (when (re-search-forward "^\\*+[ \t]" nil t)
  22257. (delete-region (point-min) (match-beginning 0))
  22258. (goto-char (point-min))
  22259. (insert "\n")))
  22260. (when (plist-get parameters :add-text)
  22261. (goto-char (point-min))
  22262. (insert (plist-get parameters :add-text) "\n"))
  22263. ;; Get rid of archived trees
  22264. (when (not (eq archived-trees t))
  22265. (goto-char (point-min))
  22266. (while (re-search-forward re-archive nil t)
  22267. (if (not (org-on-heading-p t))
  22268. (org-end-of-subtree t)
  22269. (beginning-of-line 1)
  22270. (setq a (if archived-trees
  22271. (1+ (point-at-eol)) (point))
  22272. b (org-end-of-subtree t))
  22273. (if (> b a) (delete-region a b)))))
  22274. ;; Find targets in comments and move them out of comments,
  22275. ;; but mark them as targets that should be invisible
  22276. (goto-char (point-min))
  22277. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22278. (replace-match "\\1(INVISIBLE)"))
  22279. ;; Protect backend specific stuff, throw away the others.
  22280. (let ((formatters
  22281. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22282. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22283. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22284. fmt)
  22285. (goto-char (point-min))
  22286. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22287. (goto-char (match-end 0))
  22288. (while (not (looking-at "#\\+END_EXAMPLE"))
  22289. (insert ": ")
  22290. (beginning-of-line 2)))
  22291. (goto-char (point-min))
  22292. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22293. (add-text-properties (match-beginning 0) (match-end 0)
  22294. '(org-protected t)))
  22295. (while formatters
  22296. (setq fmt (pop formatters))
  22297. (when (car fmt)
  22298. (goto-char (point-min))
  22299. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22300. ":[ \t]*\\(.*\\)") nil t)
  22301. (replace-match "\\1" t)
  22302. (add-text-properties
  22303. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22304. '(org-protected t))))
  22305. (goto-char (point-min))
  22306. (while (re-search-forward
  22307. (concat "^#\\+"
  22308. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22309. (cadddr fmt) "\\>.*\n?") nil t)
  22310. (if (car fmt)
  22311. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22312. '(org-protected t))
  22313. (delete-region (match-beginning 0) (match-end 0))))))
  22314. ;; Protect quoted subtrees
  22315. (goto-char (point-min))
  22316. (while (re-search-forward re-quote nil t)
  22317. (goto-char (match-beginning 0))
  22318. (end-of-line 1)
  22319. (add-text-properties (point) (org-end-of-subtree t)
  22320. '(org-protected t)))
  22321. ;; Protect verbatim elements
  22322. (goto-char (point-min))
  22323. (while (re-search-forward org-verbatim-re nil t)
  22324. (add-text-properties (match-beginning 4) (match-end 4)
  22325. '(org-protected t))
  22326. (goto-char (1+ (match-end 4))))
  22327. ;; Remove subtrees that are commented
  22328. (goto-char (point-min))
  22329. (while (re-search-forward re-commented nil t)
  22330. (goto-char (match-beginning 0))
  22331. (delete-region (point) (org-end-of-subtree t)))
  22332. ;; Remove special table lines
  22333. (when org-export-table-remove-special-lines
  22334. (goto-char (point-min))
  22335. (while (re-search-forward "^[ \t]*|" nil t)
  22336. (beginning-of-line 1)
  22337. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22338. (and (looking-at ".*?| *<[0-9]+> *|")
  22339. (not (looking-at ".*?| *[^ <|]"))))
  22340. (delete-region (max (point-min) (1- (point-at-bol)))
  22341. (point-at-eol))
  22342. (end-of-line 1))))
  22343. ;; Specific LaTeX stuff
  22344. (when latexp
  22345. (require 'org-export-latex nil)
  22346. (org-export-latex-cleaned-string))
  22347. (when asciip
  22348. (org-export-ascii-clean-string))
  22349. ;; Specific HTML stuff
  22350. (when htmlp
  22351. ;; Convert LaTeX fragments to images
  22352. (when (plist-get parameters :LaTeX-fragments)
  22353. (org-format-latex
  22354. (concat "ltxpng/" (file-name-sans-extension
  22355. (file-name-nondirectory
  22356. org-current-export-file)))
  22357. org-current-export-dir nil "Creating LaTeX image %s"))
  22358. (message "Exporting..."))
  22359. ;; Remove or replace comments
  22360. (goto-char (point-min))
  22361. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22362. (if commentsp
  22363. (progn (add-text-properties
  22364. (match-beginning 0) (match-end 0) '(org-protected t))
  22365. (replace-match (format commentsp (match-string 1)) t t))
  22366. (replace-match "")))
  22367. ;; Find matches for radio targets and turn them into internal links
  22368. (goto-char (point-min))
  22369. (when re-radio
  22370. (while (re-search-forward re-radio nil t)
  22371. (org-if-unprotected
  22372. (replace-match "\\1[[\\2]]"))))
  22373. ;; Find all links that contain a newline and put them into a single line
  22374. (goto-char (point-min))
  22375. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22376. (org-if-unprotected
  22377. (replace-match "\\1 \\3")
  22378. (goto-char (match-beginning 0))))
  22379. ;; Normalize links: Convert angle and plain links into bracket links
  22380. ;; Expand link abbreviations
  22381. (goto-char (point-min))
  22382. (while (re-search-forward re-plain-link nil t)
  22383. (goto-char (1- (match-end 0)))
  22384. (org-if-unprotected
  22385. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22386. ":" (match-string 3) "]]")))
  22387. ;; added 'org-link face to links
  22388. (put-text-property 0 (length s) 'face 'org-link s)
  22389. (replace-match s t t))))
  22390. (goto-char (point-min))
  22391. (while (re-search-forward re-angle-link nil t)
  22392. (goto-char (1- (match-end 0)))
  22393. (org-if-unprotected
  22394. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22395. ":" (match-string 3) "]]")))
  22396. (put-text-property 0 (length s) 'face 'org-link s)
  22397. (replace-match s t t))))
  22398. (goto-char (point-min))
  22399. (while (re-search-forward org-bracket-link-regexp nil t)
  22400. (org-if-unprotected
  22401. (let* ((s (concat "[[" (setq xx (save-match-data
  22402. (org-link-expand-abbrev (match-string 1))))
  22403. "]"
  22404. (if (match-end 3)
  22405. (match-string 2)
  22406. (concat "[" xx "]"))
  22407. "]")))
  22408. (put-text-property 0 (length s) 'face 'org-link s)
  22409. (replace-match s t t))))
  22410. ;; Find multiline emphasis and put them into single line
  22411. (when (plist-get parameters :emph-multiline)
  22412. (goto-char (point-min))
  22413. (while (re-search-forward org-emph-re nil t)
  22414. (if (not (= (char-after (match-beginning 3))
  22415. (char-after (match-beginning 4))))
  22416. (org-if-unprotected
  22417. (subst-char-in-region (match-beginning 0) (match-end 0)
  22418. ?\n ?\ t)
  22419. (goto-char (1- (match-end 0))))
  22420. (goto-char (1+ (match-beginning 0))))))
  22421. (setq rtn (buffer-string)))
  22422. (kill-buffer " org-mode-tmp")
  22423. rtn))
  22424. (defun org-export-grab-title-from-buffer ()
  22425. "Get a title for the current document, from looking at the buffer."
  22426. (let ((inhibit-read-only t))
  22427. (save-excursion
  22428. (goto-char (point-min))
  22429. (let ((end (save-excursion (outline-next-heading) (point))))
  22430. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22431. ;; Mark the line so that it will not be exported as normal text.
  22432. (org-unmodified
  22433. (add-text-properties (match-beginning 0) (match-end 0)
  22434. (list :org-license-to-kill t)))
  22435. ;; Return the title string
  22436. (org-trim (match-string 0)))))))
  22437. (defun org-export-get-title-from-subtree ()
  22438. "Return subtree title and exclude it from export."
  22439. (let (title (m (mark)))
  22440. (save-excursion
  22441. (goto-char (region-beginning))
  22442. (when (and (org-at-heading-p)
  22443. (>= (org-end-of-subtree t t) (region-end)))
  22444. ;; This is a subtree, we take the title from the first heading
  22445. (goto-char (region-beginning))
  22446. (looking-at org-todo-line-regexp)
  22447. (setq title (match-string 3))
  22448. (org-unmodified
  22449. (add-text-properties (point) (1+ (point-at-eol))
  22450. (list :org-license-to-kill t)))))
  22451. title))
  22452. (defun org-solidify-link-text (s &optional alist)
  22453. "Take link text and make a safe target out of it."
  22454. (save-match-data
  22455. (let* ((rtn
  22456. (mapconcat
  22457. 'identity
  22458. (org-split-string s "[ \t\r\n]+") "--"))
  22459. (a (assoc rtn alist)))
  22460. (or (cdr a) rtn))))
  22461. (defun org-get-min-level (lines)
  22462. "Get the minimum level in LINES."
  22463. (let ((re "^\\(\\*+\\) ") l min)
  22464. (catch 'exit
  22465. (while (setq l (pop lines))
  22466. (if (string-match re l)
  22467. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22468. 1)))
  22469. ;; Variable holding the vector with section numbers
  22470. (defvar org-section-numbers (make-vector org-level-max 0))
  22471. (defun org-init-section-numbers ()
  22472. "Initialize the vector for the section numbers."
  22473. (let* ((level -1)
  22474. (numbers (nreverse (org-split-string "" "\\.")))
  22475. (depth (1- (length org-section-numbers)))
  22476. (i depth) number-string)
  22477. (while (>= i 0)
  22478. (if (> i level)
  22479. (aset org-section-numbers i 0)
  22480. (setq number-string (or (car numbers) "0"))
  22481. (if (string-match "\\`[A-Z]\\'" number-string)
  22482. (aset org-section-numbers i
  22483. (- (string-to-char number-string) ?A -1))
  22484. (aset org-section-numbers i (string-to-number number-string)))
  22485. (pop numbers))
  22486. (setq i (1- i)))))
  22487. (defun org-section-number (&optional level)
  22488. "Return a string with the current section number.
  22489. When LEVEL is non-nil, increase section numbers on that level."
  22490. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22491. (when level
  22492. (when (> level -1)
  22493. (aset org-section-numbers
  22494. level (1+ (aref org-section-numbers level))))
  22495. (setq idx (1+ level))
  22496. (while (<= idx depth)
  22497. (if (not (= idx 1))
  22498. (aset org-section-numbers idx 0))
  22499. (setq idx (1+ idx))))
  22500. (setq idx 0)
  22501. (while (<= idx depth)
  22502. (setq n (aref org-section-numbers idx))
  22503. (setq string (concat string (if (not (string= string "")) "." "")
  22504. (int-to-string n)))
  22505. (setq idx (1+ idx)))
  22506. (save-match-data
  22507. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22508. (setq string (replace-match "" t nil string)))
  22509. (if (string-match "\\(\\.0\\)+\\'" string)
  22510. (setq string (replace-match "" t nil string))))
  22511. string))
  22512. ;;; ASCII export
  22513. (defvar org-last-level nil) ; dynamically scoped variable
  22514. (defvar org-min-level nil) ; dynamically scoped variable
  22515. (defvar org-levels-open nil) ; dynamically scoped parameter
  22516. (defvar org-ascii-current-indentation nil) ; For communication
  22517. (defun org-export-as-ascii (arg)
  22518. "Export the outline as a pretty ASCII file.
  22519. If there is an active region, export only the region.
  22520. The prefix ARG specifies how many levels of the outline should become
  22521. underlined headlines. The default is 3."
  22522. (interactive "P")
  22523. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22524. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22525. (org-infile-export-plist)))
  22526. (region-p (org-region-active-p))
  22527. (subtree-p
  22528. (when region-p
  22529. (save-excursion
  22530. (goto-char (region-beginning))
  22531. (and (org-at-heading-p)
  22532. (>= (org-end-of-subtree t t) (region-end))))))
  22533. (custom-times org-display-custom-times)
  22534. (org-ascii-current-indentation '(0 . 0))
  22535. (level 0) line txt
  22536. (umax nil)
  22537. (umax-toc nil)
  22538. (case-fold-search nil)
  22539. (filename (concat (file-name-as-directory
  22540. (org-export-directory :ascii opt-plist))
  22541. (file-name-sans-extension
  22542. (or (and subtree-p
  22543. (org-entry-get (region-beginning)
  22544. "EXPORT_FILE_NAME" t))
  22545. (file-name-nondirectory buffer-file-name)))
  22546. ".txt"))
  22547. (filename (if (equal (file-truename filename)
  22548. (file-truename buffer-file-name))
  22549. (concat filename ".txt")
  22550. filename))
  22551. (buffer (find-file-noselect filename))
  22552. (org-levels-open (make-vector org-level-max nil))
  22553. (odd org-odd-levels-only)
  22554. (date (plist-get opt-plist :date))
  22555. (author (plist-get opt-plist :author))
  22556. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22557. (plist-get opt-plist :title)
  22558. (and (not
  22559. (plist-get opt-plist :skip-before-1st-heading))
  22560. (org-export-grab-title-from-buffer))
  22561. (file-name-sans-extension
  22562. (file-name-nondirectory buffer-file-name))))
  22563. (email (plist-get opt-plist :email))
  22564. (language (plist-get opt-plist :language))
  22565. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22566. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22567. (todo nil)
  22568. (lang-words nil)
  22569. (region
  22570. (buffer-substring
  22571. (if (org-region-active-p) (region-beginning) (point-min))
  22572. (if (org-region-active-p) (region-end) (point-max))))
  22573. (lines (org-split-string
  22574. (org-cleaned-string-for-export
  22575. region
  22576. :for-ascii t
  22577. :skip-before-1st-heading
  22578. (plist-get opt-plist :skip-before-1st-heading)
  22579. :drawers (plist-get opt-plist :drawers)
  22580. :verbatim-multiline t
  22581. :archived-trees
  22582. (plist-get opt-plist :archived-trees)
  22583. :add-text (plist-get opt-plist :text))
  22584. "\n"))
  22585. thetoc have-headings first-heading-pos
  22586. table-open table-buffer)
  22587. (let ((inhibit-read-only t))
  22588. (org-unmodified
  22589. (remove-text-properties (point-min) (point-max)
  22590. '(:org-license-to-kill t))))
  22591. (setq org-min-level (org-get-min-level lines))
  22592. (setq org-last-level org-min-level)
  22593. (org-init-section-numbers)
  22594. (find-file-noselect filename)
  22595. (setq lang-words (or (assoc language org-export-language-setup)
  22596. (assoc "en" org-export-language-setup)))
  22597. (switch-to-buffer-other-window buffer)
  22598. (erase-buffer)
  22599. (fundamental-mode)
  22600. ;; create local variables for all options, to make sure all called
  22601. ;; functions get the correct information
  22602. (mapc (lambda (x)
  22603. (set (make-local-variable (cdr x))
  22604. (plist-get opt-plist (car x))))
  22605. org-export-plist-vars)
  22606. (org-set-local 'org-odd-levels-only odd)
  22607. (setq umax (if arg (prefix-numeric-value arg)
  22608. org-export-headline-levels))
  22609. (setq umax-toc (if (integerp org-export-with-toc)
  22610. (min org-export-with-toc umax)
  22611. umax))
  22612. ;; File header
  22613. (if title (org-insert-centered title ?=))
  22614. (insert "\n")
  22615. (if (and (or author email)
  22616. org-export-author-info)
  22617. (insert (concat (nth 1 lang-words) ": " (or author "")
  22618. (if email (concat " <" email ">") "")
  22619. "\n")))
  22620. (cond
  22621. ((and date (string-match "%" date))
  22622. (setq date (format-time-string date (current-time))))
  22623. (date)
  22624. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22625. (if (and date org-export-time-stamp-file)
  22626. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22627. (insert "\n\n")
  22628. (if org-export-with-toc
  22629. (progn
  22630. (push (concat (nth 3 lang-words) "\n") thetoc)
  22631. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22632. (mapc '(lambda (line)
  22633. (if (string-match org-todo-line-regexp
  22634. line)
  22635. ;; This is a headline
  22636. (progn
  22637. (setq have-headings t)
  22638. (setq level (- (match-end 1) (match-beginning 1))
  22639. level (org-tr-level level)
  22640. txt (match-string 3 line)
  22641. todo
  22642. (or (and org-export-mark-todo-in-toc
  22643. (match-beginning 2)
  22644. (not (member (match-string 2 line)
  22645. org-done-keywords)))
  22646. ; TODO, not DONE
  22647. (and org-export-mark-todo-in-toc
  22648. (= level umax-toc)
  22649. (org-search-todo-below
  22650. line lines level))))
  22651. (setq txt (org-html-expand-for-ascii txt))
  22652. (while (string-match org-bracket-link-regexp txt)
  22653. (setq txt
  22654. (replace-match
  22655. (match-string (if (match-end 2) 3 1) txt)
  22656. t t txt)))
  22657. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22658. (string-match
  22659. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22660. txt))
  22661. (setq txt (replace-match "" t t txt)))
  22662. (if (string-match quote-re0 txt)
  22663. (setq txt (replace-match "" t t txt)))
  22664. (if org-export-with-section-numbers
  22665. (setq txt (concat (org-section-number level)
  22666. " " txt)))
  22667. (if (<= level umax-toc)
  22668. (progn
  22669. (push
  22670. (concat
  22671. (make-string
  22672. (* (max 0 (- level org-min-level)) 4) ?\ )
  22673. (format (if todo "%s (*)\n" "%s\n") txt))
  22674. thetoc)
  22675. (setq org-last-level level))
  22676. ))))
  22677. lines)
  22678. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22679. (org-init-section-numbers)
  22680. (while (setq line (pop lines))
  22681. ;; Remove the quoted HTML tags.
  22682. (setq line (org-html-expand-for-ascii line))
  22683. ;; Remove targets
  22684. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22685. (setq line (replace-match "" t t line)))
  22686. ;; Replace internal links
  22687. (while (string-match org-bracket-link-regexp line)
  22688. (setq line (replace-match
  22689. (if (match-end 3) "[\\3]" "[\\1]")
  22690. t nil line)))
  22691. (when custom-times
  22692. (setq line (org-translate-time line)))
  22693. (cond
  22694. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22695. ;; a Headline
  22696. (setq first-heading-pos (or first-heading-pos (point)))
  22697. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22698. txt (match-string 2 line))
  22699. (org-ascii-level-start level txt umax lines))
  22700. ((and org-export-with-tables
  22701. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22702. (if (not table-open)
  22703. ;; New table starts
  22704. (setq table-open t table-buffer nil))
  22705. ;; Accumulate lines
  22706. (setq table-buffer (cons line table-buffer))
  22707. (when (or (not lines)
  22708. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22709. (car lines))))
  22710. (setq table-open nil
  22711. table-buffer (nreverse table-buffer))
  22712. (insert (mapconcat
  22713. (lambda (x)
  22714. (org-fix-indentation x org-ascii-current-indentation))
  22715. (org-format-table-ascii table-buffer)
  22716. "\n") "\n")))
  22717. (t
  22718. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22719. (if (and org-export-with-fixed-width
  22720. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22721. (setq line (replace-match "\\1" nil nil line)))
  22722. (insert line "\n"))))
  22723. (normal-mode)
  22724. ;; insert the table of contents
  22725. (when thetoc
  22726. (goto-char (point-min))
  22727. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22728. (progn
  22729. (goto-char (match-beginning 0))
  22730. (replace-match ""))
  22731. (goto-char first-heading-pos))
  22732. (mapc 'insert thetoc)
  22733. (or (looking-at "[ \t]*\n[ \t]*\n")
  22734. (insert "\n\n")))
  22735. ;; Convert whitespace place holders
  22736. (goto-char (point-min))
  22737. (let (beg end)
  22738. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22739. (setq end (next-single-property-change beg 'org-whitespace))
  22740. (goto-char beg)
  22741. (delete-region beg end)
  22742. (insert (make-string (- end beg) ?\ ))))
  22743. (save-buffer)
  22744. ;; remove display and invisible chars
  22745. (let (beg end)
  22746. (goto-char (point-min))
  22747. (while (setq beg (next-single-property-change (point) 'display))
  22748. (setq end (next-single-property-change beg 'display))
  22749. (delete-region beg end)
  22750. (goto-char beg)
  22751. (insert "=>"))
  22752. (goto-char (point-min))
  22753. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22754. (setq end (next-single-property-change beg 'org-cwidth))
  22755. (delete-region beg end)
  22756. (goto-char beg)))
  22757. (goto-char (point-min))))
  22758. (defun org-export-ascii-clean-string ()
  22759. "Do extra work for ASCII export"
  22760. (goto-char (point-min))
  22761. (while (re-search-forward org-verbatim-re nil t)
  22762. (goto-char (match-end 2))
  22763. (backward-delete-char 1) (insert "'")
  22764. (goto-char (match-beginning 2))
  22765. (delete-char 1) (insert "`")
  22766. (goto-char (match-end 2))))
  22767. (defun org-search-todo-below (line lines level)
  22768. "Search the subtree below LINE for any TODO entries."
  22769. (let ((rest (cdr (memq line lines)))
  22770. (re org-todo-line-regexp)
  22771. line lv todo)
  22772. (catch 'exit
  22773. (while (setq line (pop rest))
  22774. (if (string-match re line)
  22775. (progn
  22776. (setq lv (- (match-end 1) (match-beginning 1))
  22777. todo (and (match-beginning 2)
  22778. (not (member (match-string 2 line)
  22779. org-done-keywords))))
  22780. ; TODO, not DONE
  22781. (if (<= lv level) (throw 'exit nil))
  22782. (if todo (throw 'exit t))))))))
  22783. (defun org-html-expand-for-ascii (line)
  22784. "Handle quoted HTML for ASCII export."
  22785. (if org-export-html-expand
  22786. (while (string-match "@<[^<>\n]*>" line)
  22787. ;; We just remove the tags for now.
  22788. (setq line (replace-match "" nil nil line))))
  22789. line)
  22790. (defun org-insert-centered (s &optional underline)
  22791. "Insert the string S centered and underline it with character UNDERLINE."
  22792. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22793. (insert (make-string ind ?\ ) s "\n")
  22794. (if underline
  22795. (insert (make-string ind ?\ )
  22796. (make-string (string-width s) underline)
  22797. "\n"))))
  22798. (defun org-ascii-level-start (level title umax &optional lines)
  22799. "Insert a new level in ASCII export."
  22800. (let (char (n (- level umax 1)) (ind 0))
  22801. (if (> level umax)
  22802. (progn
  22803. (insert (make-string (* 2 n) ?\ )
  22804. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22805. org-export-ascii-bullets))
  22806. " " title "\n")
  22807. ;; find the indentation of the next non-empty line
  22808. (catch 'stop
  22809. (while lines
  22810. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22811. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22812. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22813. (pop lines)))
  22814. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22815. (if (or (not (equal (char-before) ?\n))
  22816. (not (equal (char-before (1- (point))) ?\n)))
  22817. (insert "\n"))
  22818. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22819. (unless org-export-with-tags
  22820. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22821. (setq title (replace-match "" t t title))))
  22822. (if org-export-with-section-numbers
  22823. (setq title (concat (org-section-number level) " " title)))
  22824. (insert title "\n" (make-string (string-width title) char) "\n")
  22825. (setq org-ascii-current-indentation '(0 . 0)))))
  22826. (defun org-export-visible (type arg)
  22827. "Create a copy of the visible part of the current buffer, and export it.
  22828. The copy is created in a temporary buffer and removed after use.
  22829. TYPE is the final key (as a string) that also select the export command in
  22830. the `C-c C-e' export dispatcher.
  22831. As a special case, if the you type SPC at the prompt, the temporary
  22832. org-mode file will not be removed but presented to you so that you can
  22833. continue to use it. The prefix arg ARG is passed through to the exporting
  22834. command."
  22835. (interactive
  22836. (list (progn
  22837. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22838. (read-char-exclusive))
  22839. current-prefix-arg))
  22840. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22841. (error "Invalid export key"))
  22842. (let* ((binding (cdr (assoc type
  22843. '((?a . org-export-as-ascii)
  22844. (?\C-a . org-export-as-ascii)
  22845. (?b . org-export-as-html-and-open)
  22846. (?\C-b . org-export-as-html-and-open)
  22847. (?h . org-export-as-html)
  22848. (?H . org-export-as-html-to-buffer)
  22849. (?R . org-export-region-as-html)
  22850. (?x . org-export-as-xoxo)))))
  22851. (keepp (equal type ?\ ))
  22852. (file buffer-file-name)
  22853. (buffer (get-buffer-create "*Org Export Visible*"))
  22854. s e)
  22855. ;; Need to hack the drawers here.
  22856. (save-excursion
  22857. (goto-char (point-min))
  22858. (while (re-search-forward org-drawer-regexp nil t)
  22859. (goto-char (match-beginning 1))
  22860. (or (org-invisible-p) (org-flag-drawer nil))))
  22861. (with-current-buffer buffer (erase-buffer))
  22862. (save-excursion
  22863. (setq s (goto-char (point-min)))
  22864. (while (not (= (point) (point-max)))
  22865. (goto-char (org-find-invisible))
  22866. (append-to-buffer buffer s (point))
  22867. (setq s (goto-char (org-find-visible))))
  22868. (org-cycle-hide-drawers 'all)
  22869. (goto-char (point-min))
  22870. (unless keepp
  22871. ;; Copy all comment lines to the end, to make sure #+ settings are
  22872. ;; still available for the second export step. Kind of a hack, but
  22873. ;; does do the trick.
  22874. (if (looking-at "#[^\r\n]*")
  22875. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22876. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22877. (append-to-buffer buffer (1+ (match-beginning 0))
  22878. (min (point-max) (1+ (match-end 0))))))
  22879. (set-buffer buffer)
  22880. (let ((buffer-file-name file)
  22881. (org-inhibit-startup t))
  22882. (org-mode)
  22883. (show-all)
  22884. (unless keepp (funcall binding arg))))
  22885. (if (not keepp)
  22886. (kill-buffer buffer)
  22887. (switch-to-buffer-other-window buffer)
  22888. (goto-char (point-min)))))
  22889. (defun org-find-visible ()
  22890. (let ((s (point)))
  22891. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22892. (get-char-property s 'invisible)))
  22893. s))
  22894. (defun org-find-invisible ()
  22895. (let ((s (point)))
  22896. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22897. (not (get-char-property s 'invisible))))
  22898. s))
  22899. ;;; HTML export
  22900. (defun org-get-current-options ()
  22901. "Return a string with current options as keyword options.
  22902. Does include HTML export options as well as TODO and CATEGORY stuff."
  22903. (format
  22904. "#+TITLE: %s
  22905. #+AUTHOR: %s
  22906. #+EMAIL: %s
  22907. #+LANGUAGE: %s
  22908. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22909. #+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
  22910. #+CATEGORY: %s
  22911. #+SEQ_TODO: %s
  22912. #+TYP_TODO: %s
  22913. #+PRIORITIES: %c %c %c
  22914. #+DRAWERS: %s
  22915. #+STARTUP: %s %s %s %s %s
  22916. #+TAGS: %s
  22917. #+ARCHIVE: %s
  22918. #+LINK: %s
  22919. "
  22920. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22921. org-export-headline-levels
  22922. org-export-with-section-numbers
  22923. org-export-with-toc
  22924. org-export-preserve-breaks
  22925. org-export-html-expand
  22926. org-export-with-fixed-width
  22927. org-export-with-tables
  22928. org-export-with-sub-superscripts
  22929. org-export-with-special-strings
  22930. org-export-with-footnotes
  22931. org-export-with-emphasize
  22932. org-export-with-TeX-macros
  22933. org-export-with-LaTeX-fragments
  22934. org-export-skip-text-before-1st-heading
  22935. org-export-with-drawers
  22936. org-export-with-tags
  22937. (file-name-nondirectory buffer-file-name)
  22938. "TODO FEEDBACK VERIFY DONE"
  22939. "Me Jason Marie DONE"
  22940. org-highest-priority org-lowest-priority org-default-priority
  22941. (mapconcat 'identity org-drawers " ")
  22942. (cdr (assoc org-startup-folded
  22943. '((nil . "showall") (t . "overview") (content . "content"))))
  22944. (if org-odd-levels-only "odd" "oddeven")
  22945. (if org-hide-leading-stars "hidestars" "showstars")
  22946. (if org-startup-align-all-tables "align" "noalign")
  22947. (cond ((eq org-log-done t) "logdone")
  22948. ((equal org-log-done 'note) "lognotedone")
  22949. ((not org-log-done) "nologdone"))
  22950. (or (mapconcat (lambda (x)
  22951. (cond
  22952. ((equal '(:startgroup) x) "{")
  22953. ((equal '(:endgroup) x) "}")
  22954. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22955. (t (car x))))
  22956. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22957. org-archive-location
  22958. "org file:~/org/%s.org"
  22959. ))
  22960. (defun org-insert-export-options-template ()
  22961. "Insert into the buffer a template with information for exporting."
  22962. (interactive)
  22963. (if (not (bolp)) (newline))
  22964. (let ((s (org-get-current-options)))
  22965. (and (string-match "#\\+CATEGORY" s)
  22966. (setq s (substring s 0 (match-beginning 0))))
  22967. (insert s)))
  22968. (defun org-toggle-fixed-width-section (arg)
  22969. "Toggle the fixed-width export.
  22970. If there is no active region, the QUOTE keyword at the current headline is
  22971. inserted or removed. When present, it causes the text between this headline
  22972. and the next to be exported as fixed-width text, and unmodified.
  22973. If there is an active region, this command adds or removes a colon as the
  22974. first character of this line. If the first character of a line is a colon,
  22975. this line is also exported in fixed-width font."
  22976. (interactive "P")
  22977. (let* ((cc 0)
  22978. (regionp (org-region-active-p))
  22979. (beg (if regionp (region-beginning) (point)))
  22980. (end (if regionp (region-end)))
  22981. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22982. (case-fold-search nil)
  22983. (re "[ \t]*\\(:\\)")
  22984. off)
  22985. (if regionp
  22986. (save-excursion
  22987. (goto-char beg)
  22988. (setq cc (current-column))
  22989. (beginning-of-line 1)
  22990. (setq off (looking-at re))
  22991. (while (> nlines 0)
  22992. (setq nlines (1- nlines))
  22993. (beginning-of-line 1)
  22994. (cond
  22995. (arg
  22996. (move-to-column cc t)
  22997. (insert ":\n")
  22998. (forward-line -1))
  22999. ((and off (looking-at re))
  23000. (replace-match "" t t nil 1))
  23001. ((not off) (move-to-column cc t) (insert ":")))
  23002. (forward-line 1)))
  23003. (save-excursion
  23004. (org-back-to-heading)
  23005. (if (looking-at (concat outline-regexp
  23006. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23007. (replace-match "" t t nil 1)
  23008. (if (looking-at outline-regexp)
  23009. (progn
  23010. (goto-char (match-end 0))
  23011. (insert org-quote-string " "))))))))
  23012. (defun org-export-as-html-and-open (arg)
  23013. "Export the outline as HTML and immediately open it with a browser.
  23014. If there is an active region, export only the region.
  23015. The prefix ARG specifies how many levels of the outline should become
  23016. headlines. The default is 3. Lower levels will become bulleted lists."
  23017. (interactive "P")
  23018. (org-export-as-html arg 'hidden)
  23019. (org-open-file buffer-file-name))
  23020. (defun org-export-as-html-batch ()
  23021. "Call `org-export-as-html', may be used in batch processing as
  23022. emacs --batch
  23023. --load=$HOME/lib/emacs/org.el
  23024. --eval \"(setq org-export-headline-levels 2)\"
  23025. --visit=MyFile --funcall org-export-as-html-batch"
  23026. (org-export-as-html org-export-headline-levels 'hidden))
  23027. (defun org-export-as-html-to-buffer (arg)
  23028. "Call `org-exort-as-html` with output to a temporary buffer.
  23029. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23030. (interactive "P")
  23031. (org-export-as-html arg nil nil "*Org HTML Export*")
  23032. (switch-to-buffer-other-window "*Org HTML Export*"))
  23033. (defun org-replace-region-by-html (beg end)
  23034. "Assume the current region has org-mode syntax, and convert it to HTML.
  23035. This can be used in any buffer. For example, you could write an
  23036. itemized list in org-mode syntax in an HTML buffer and then use this
  23037. command to convert it."
  23038. (interactive "r")
  23039. (let (reg html buf pop-up-frames)
  23040. (save-window-excursion
  23041. (if (org-mode-p)
  23042. (setq html (org-export-region-as-html
  23043. beg end t 'string))
  23044. (setq reg (buffer-substring beg end)
  23045. buf (get-buffer-create "*Org tmp*"))
  23046. (with-current-buffer buf
  23047. (erase-buffer)
  23048. (insert reg)
  23049. (org-mode)
  23050. (setq html (org-export-region-as-html
  23051. (point-min) (point-max) t 'string)))
  23052. (kill-buffer buf)))
  23053. (delete-region beg end)
  23054. (insert html)))
  23055. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23056. "Convert region from BEG to END in org-mode buffer to HTML.
  23057. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23058. contents, and only produce the region of converted text, useful for
  23059. cut-and-paste operations.
  23060. If BUFFER is a buffer or a string, use/create that buffer as a target
  23061. of the converted HTML. If BUFFER is the symbol `string', return the
  23062. produced HTML as a string and leave not buffer behind. For example,
  23063. a Lisp program could call this function in the following way:
  23064. (setq html (org-export-region-as-html beg end t 'string))
  23065. When called interactively, the output buffer is selected, and shown
  23066. in a window. A non-interactive call will only retunr the buffer."
  23067. (interactive "r\nP")
  23068. (when (interactive-p)
  23069. (setq buffer "*Org HTML Export*"))
  23070. (let ((transient-mark-mode t) (zmacs-regions t)
  23071. rtn)
  23072. (goto-char end)
  23073. (set-mark (point)) ;; to activate the region
  23074. (goto-char beg)
  23075. (setq rtn (org-export-as-html
  23076. nil nil nil
  23077. buffer body-only))
  23078. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23079. (if (and (interactive-p) (bufferp rtn))
  23080. (switch-to-buffer-other-window rtn)
  23081. rtn)))
  23082. (defvar html-table-tag nil) ; dynamically scoped into this.
  23083. (defun org-export-as-html (arg &optional hidden ext-plist
  23084. to-buffer body-only)
  23085. "Export the outline as a pretty HTML file.
  23086. If there is an active region, export only the region. The prefix
  23087. ARG specifies how many levels of the outline should become
  23088. headlines. The default is 3. Lower levels will become bulleted
  23089. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23090. EXT-PLIST is a property list with external parameters overriding
  23091. org-mode's default settings, but still inferior to file-local
  23092. settings. When TO-BUFFER is non-nil, create a buffer with that
  23093. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23094. don't leave any buffer behind but just return the resulting HTML as
  23095. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23096. simply return the content of <body>...</body>, without even
  23097. the body tags themselves."
  23098. (interactive "P")
  23099. ;; Make sure we have a file name when we need it.
  23100. (when (and (not (or to-buffer body-only))
  23101. (not buffer-file-name))
  23102. (if (buffer-base-buffer)
  23103. (org-set-local 'buffer-file-name
  23104. (with-current-buffer (buffer-base-buffer)
  23105. buffer-file-name))
  23106. (error "Need a file name to be able to export.")))
  23107. (message "Exporting...")
  23108. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23109. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23110. (setq-default org-done-keywords org-done-keywords)
  23111. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23112. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23113. ext-plist
  23114. (org-infile-export-plist)))
  23115. (style (plist-get opt-plist :style))
  23116. (html-extension (plist-get opt-plist :html-extension))
  23117. (link-validate (plist-get opt-plist :link-validation-function))
  23118. valid thetoc have-headings first-heading-pos
  23119. (odd org-odd-levels-only)
  23120. (region-p (org-region-active-p))
  23121. (subtree-p
  23122. (when region-p
  23123. (save-excursion
  23124. (goto-char (region-beginning))
  23125. (and (org-at-heading-p)
  23126. (>= (org-end-of-subtree t t) (region-end))))))
  23127. ;; The following two are dynamically scoped into other
  23128. ;; routines below.
  23129. (org-current-export-dir (org-export-directory :html opt-plist))
  23130. (org-current-export-file buffer-file-name)
  23131. (level 0) (line "") (origline "") txt todo
  23132. (umax nil)
  23133. (umax-toc nil)
  23134. (filename (if to-buffer nil
  23135. (expand-file-name
  23136. (concat
  23137. (file-name-sans-extension
  23138. (or (and subtree-p
  23139. (org-entry-get (region-beginning)
  23140. "EXPORT_FILE_NAME" t))
  23141. (file-name-nondirectory buffer-file-name)))
  23142. "." html-extension)
  23143. (file-name-as-directory
  23144. (org-export-directory :html opt-plist)))))
  23145. (current-dir (if buffer-file-name
  23146. (file-name-directory buffer-file-name)
  23147. default-directory))
  23148. (buffer (if to-buffer
  23149. (cond
  23150. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23151. (t (get-buffer-create to-buffer)))
  23152. (find-file-noselect filename)))
  23153. (org-levels-open (make-vector org-level-max nil))
  23154. (date (plist-get opt-plist :date))
  23155. (author (plist-get opt-plist :author))
  23156. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23157. (plist-get opt-plist :title)
  23158. (and (not
  23159. (plist-get opt-plist :skip-before-1st-heading))
  23160. (org-export-grab-title-from-buffer))
  23161. (and buffer-file-name
  23162. (file-name-sans-extension
  23163. (file-name-nondirectory buffer-file-name)))
  23164. "UNTITLED"))
  23165. (html-table-tag (plist-get opt-plist :html-table-tag))
  23166. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23167. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23168. (inquote nil)
  23169. (infixed nil)
  23170. (in-local-list nil)
  23171. (local-list-num nil)
  23172. (local-list-indent nil)
  23173. (llt org-plain-list-ordered-item-terminator)
  23174. (email (plist-get opt-plist :email))
  23175. (language (plist-get opt-plist :language))
  23176. (lang-words nil)
  23177. (target-alist nil) tg
  23178. (head-count 0) cnt
  23179. (start 0)
  23180. (coding-system (and (boundp 'buffer-file-coding-system)
  23181. buffer-file-coding-system))
  23182. (coding-system-for-write (or org-export-html-coding-system
  23183. coding-system))
  23184. (save-buffer-coding-system (or org-export-html-coding-system
  23185. coding-system))
  23186. (charset (and coding-system-for-write
  23187. (fboundp 'coding-system-get)
  23188. (coding-system-get coding-system-for-write
  23189. 'mime-charset)))
  23190. (region
  23191. (buffer-substring
  23192. (if region-p (region-beginning) (point-min))
  23193. (if region-p (region-end) (point-max))))
  23194. (lines
  23195. (org-split-string
  23196. (org-cleaned-string-for-export
  23197. region
  23198. :emph-multiline t
  23199. :for-html t
  23200. :skip-before-1st-heading
  23201. (plist-get opt-plist :skip-before-1st-heading)
  23202. :drawers (plist-get opt-plist :drawers)
  23203. :archived-trees
  23204. (plist-get opt-plist :archived-trees)
  23205. :add-text
  23206. (plist-get opt-plist :text)
  23207. :LaTeX-fragments
  23208. (plist-get opt-plist :LaTeX-fragments))
  23209. "[\r\n]"))
  23210. table-open type
  23211. table-buffer table-orig-buffer
  23212. ind start-is-num starter didclose
  23213. rpl path desc descp desc1 desc2 link
  23214. )
  23215. (let ((inhibit-read-only t))
  23216. (org-unmodified
  23217. (remove-text-properties (point-min) (point-max)
  23218. '(:org-license-to-kill t))))
  23219. (message "Exporting...")
  23220. (setq org-min-level (org-get-min-level lines))
  23221. (setq org-last-level org-min-level)
  23222. (org-init-section-numbers)
  23223. (cond
  23224. ((and date (string-match "%" date))
  23225. (setq date (format-time-string date (current-time))))
  23226. (date)
  23227. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23228. ;; Get the language-dependent settings
  23229. (setq lang-words (or (assoc language org-export-language-setup)
  23230. (assoc "en" org-export-language-setup)))
  23231. ;; Switch to the output buffer
  23232. (set-buffer buffer)
  23233. (let ((inhibit-read-only t)) (erase-buffer))
  23234. (fundamental-mode)
  23235. (and (fboundp 'set-buffer-file-coding-system)
  23236. (set-buffer-file-coding-system coding-system-for-write))
  23237. (let ((case-fold-search nil)
  23238. (org-odd-levels-only odd))
  23239. ;; create local variables for all options, to make sure all called
  23240. ;; functions get the correct information
  23241. (mapc (lambda (x)
  23242. (set (make-local-variable (cdr x))
  23243. (plist-get opt-plist (car x))))
  23244. org-export-plist-vars)
  23245. (setq umax (if arg (prefix-numeric-value arg)
  23246. org-export-headline-levels))
  23247. (setq umax-toc (if (integerp org-export-with-toc)
  23248. (min org-export-with-toc umax)
  23249. umax))
  23250. (unless body-only
  23251. ;; File header
  23252. (insert (format
  23253. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23254. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23255. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23256. lang=\"%s\" xml:lang=\"%s\">
  23257. <head>
  23258. <title>%s</title>
  23259. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23260. <meta name=\"generator\" content=\"Org-mode\"/>
  23261. <meta name=\"generated\" content=\"%s\"/>
  23262. <meta name=\"author\" content=\"%s\"/>
  23263. %s
  23264. </head><body>
  23265. "
  23266. language language (org-html-expand title)
  23267. (or charset "iso-8859-1") date author style))
  23268. (insert (or (plist-get opt-plist :preamble) ""))
  23269. (when (plist-get opt-plist :auto-preamble)
  23270. (if title (insert (format org-export-html-title-format
  23271. (org-html-expand title))))))
  23272. (if (and org-export-with-toc (not body-only))
  23273. (progn
  23274. (push (format "<h%d>%s</h%d>\n"
  23275. org-export-html-toplevel-hlevel
  23276. (nth 3 lang-words)
  23277. org-export-html-toplevel-hlevel)
  23278. thetoc)
  23279. (push "<ul>\n<li>" thetoc)
  23280. (setq lines
  23281. (mapcar '(lambda (line)
  23282. (if (string-match org-todo-line-regexp line)
  23283. ;; This is a headline
  23284. (progn
  23285. (setq have-headings t)
  23286. (setq level (- (match-end 1) (match-beginning 1))
  23287. level (org-tr-level level)
  23288. txt (save-match-data
  23289. (org-html-expand
  23290. (org-export-cleanup-toc-line
  23291. (match-string 3 line))))
  23292. todo
  23293. (or (and org-export-mark-todo-in-toc
  23294. (match-beginning 2)
  23295. (not (member (match-string 2 line)
  23296. org-done-keywords)))
  23297. ; TODO, not DONE
  23298. (and org-export-mark-todo-in-toc
  23299. (= level umax-toc)
  23300. (org-search-todo-below
  23301. line lines level))))
  23302. (if (string-match
  23303. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23304. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23305. (if (string-match quote-re0 txt)
  23306. (setq txt (replace-match "" t t txt)))
  23307. (if org-export-with-section-numbers
  23308. (setq txt (concat (org-section-number level)
  23309. " " txt)))
  23310. (if (<= level (max umax umax-toc))
  23311. (setq head-count (+ head-count 1)))
  23312. (if (<= level umax-toc)
  23313. (progn
  23314. (if (> level org-last-level)
  23315. (progn
  23316. (setq cnt (- level org-last-level))
  23317. (while (>= (setq cnt (1- cnt)) 0)
  23318. (push "\n<ul>\n<li>" thetoc))
  23319. (push "\n" thetoc)))
  23320. (if (< level org-last-level)
  23321. (progn
  23322. (setq cnt (- org-last-level level))
  23323. (while (>= (setq cnt (1- cnt)) 0)
  23324. (push "</li>\n</ul>" thetoc))
  23325. (push "\n" thetoc)))
  23326. ;; Check for targets
  23327. (while (string-match org-target-regexp line)
  23328. (setq tg (match-string 1 line)
  23329. line (replace-match
  23330. (concat "@<span class=\"target\">" tg "@</span> ")
  23331. t t line))
  23332. (push (cons (org-solidify-link-text tg)
  23333. (format "sec-%d" head-count))
  23334. target-alist))
  23335. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23336. (setq txt (replace-match "" t t txt)))
  23337. (push
  23338. (format
  23339. (if todo
  23340. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23341. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23342. head-count txt) thetoc)
  23343. (setq org-last-level level))
  23344. )))
  23345. line)
  23346. lines))
  23347. (while (> org-last-level (1- org-min-level))
  23348. (setq org-last-level (1- org-last-level))
  23349. (push "</li>\n</ul>\n" thetoc))
  23350. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23351. (setq head-count 0)
  23352. (org-init-section-numbers)
  23353. (while (setq line (pop lines) origline line)
  23354. (catch 'nextline
  23355. ;; end of quote section?
  23356. (when (and inquote (string-match "^\\*+ " line))
  23357. (insert "</pre>\n")
  23358. (setq inquote nil))
  23359. ;; inside a quote section?
  23360. (when inquote
  23361. (insert (org-html-protect line) "\n")
  23362. (throw 'nextline nil))
  23363. ;; verbatim lines
  23364. (when (and org-export-with-fixed-width
  23365. (string-match "^[ \t]*:\\(.*\\)" line))
  23366. (when (not infixed)
  23367. (setq infixed t)
  23368. (insert "<pre>\n"))
  23369. (insert (org-html-protect (match-string 1 line)) "\n")
  23370. (when (and lines
  23371. (not (string-match "^[ \t]*\\(:.*\\)"
  23372. (car lines))))
  23373. (setq infixed nil)
  23374. (insert "</pre>\n"))
  23375. (throw 'nextline nil))
  23376. ;; Protected HTML
  23377. (when (get-text-property 0 'org-protected line)
  23378. (let (par)
  23379. (when (re-search-backward
  23380. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23381. (setq par (match-string 1))
  23382. (replace-match "\\2\n"))
  23383. (insert line "\n")
  23384. (while (and lines
  23385. (or (= (length (car lines)) 0)
  23386. (get-text-property 0 'org-protected (car lines))))
  23387. (insert (pop lines) "\n"))
  23388. (and par (insert "<p>\n")))
  23389. (throw 'nextline nil))
  23390. ;; Horizontal line
  23391. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23392. (insert "\n<hr/>\n")
  23393. (throw 'nextline nil))
  23394. ;; make targets to anchors
  23395. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23396. (cond
  23397. ((match-end 2)
  23398. (setq line (replace-match
  23399. (concat "@<a name=\""
  23400. (org-solidify-link-text (match-string 1 line))
  23401. "\">\\nbsp@</a>")
  23402. t t line)))
  23403. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23404. (setq line (replace-match
  23405. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23406. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23407. t t line)))
  23408. (t
  23409. (setq line (replace-match
  23410. (concat "@<a name=\""
  23411. (org-solidify-link-text (match-string 1 line))
  23412. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23413. t t line)))))
  23414. (setq line (org-html-handle-time-stamps line))
  23415. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23416. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23417. ;; Also handle sub_superscripts and checkboxes
  23418. (or (string-match org-table-hline-regexp line)
  23419. (setq line (org-html-expand line)))
  23420. ;; Format the links
  23421. (setq start 0)
  23422. (while (string-match org-bracket-link-analytic-regexp line start)
  23423. (setq start (match-beginning 0))
  23424. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23425. (setq path (match-string 3 line))
  23426. (setq desc1 (if (match-end 5) (match-string 5 line))
  23427. desc2 (if (match-end 2) (concat type ":" path) path)
  23428. descp (and desc1 (not (equal desc1 desc2)))
  23429. desc (or desc1 desc2))
  23430. ;; Make an image out of the description if that is so wanted
  23431. (when (and descp (org-file-image-p desc))
  23432. (save-match-data
  23433. (if (string-match "^file:" desc)
  23434. (setq desc (substring desc (match-end 0)))))
  23435. (setq desc (concat "<img src=\"" desc "\"/>")))
  23436. ;; FIXME: do we need to unescape here somewhere?
  23437. (cond
  23438. ((equal type "internal")
  23439. (setq rpl
  23440. (concat
  23441. "<a href=\"#"
  23442. (org-solidify-link-text
  23443. (save-match-data (org-link-unescape path)) target-alist)
  23444. "\">" desc "</a>")))
  23445. ((member type '("http" "https"))
  23446. ;; standard URL, just check if we need to inline an image
  23447. (if (and (or (eq t org-export-html-inline-images)
  23448. (and org-export-html-inline-images (not descp)))
  23449. (org-file-image-p path))
  23450. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23451. (setq link (concat type ":" path))
  23452. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23453. ((member type '("ftp" "mailto" "news"))
  23454. ;; standard URL
  23455. (setq link (concat type ":" path))
  23456. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23457. ((string= type "file")
  23458. ;; FILE link
  23459. (let* ((filename path)
  23460. (abs-p (file-name-absolute-p filename))
  23461. thefile file-is-image-p search)
  23462. (save-match-data
  23463. (if (string-match "::\\(.*\\)" filename)
  23464. (setq search (match-string 1 filename)
  23465. filename (replace-match "" t nil filename)))
  23466. (setq valid
  23467. (if (functionp link-validate)
  23468. (funcall link-validate filename current-dir)
  23469. t))
  23470. (setq file-is-image-p (org-file-image-p filename))
  23471. (setq thefile (if abs-p (expand-file-name filename) filename))
  23472. (when (and org-export-html-link-org-files-as-html
  23473. (string-match "\\.org$" thefile))
  23474. (setq thefile (concat (substring thefile 0
  23475. (match-beginning 0))
  23476. "." html-extension))
  23477. (if (and search
  23478. ;; make sure this is can be used as target search
  23479. (not (string-match "^[0-9]*$" search))
  23480. (not (string-match "^\\*" search))
  23481. (not (string-match "^/.*/$" search)))
  23482. (setq thefile (concat thefile "#"
  23483. (org-solidify-link-text
  23484. (org-link-unescape search)))))
  23485. (when (string-match "^file:" desc)
  23486. (setq desc (replace-match "" t t desc))
  23487. (if (string-match "\\.org$" desc)
  23488. (setq desc (replace-match "" t t desc))))))
  23489. (setq rpl (if (and file-is-image-p
  23490. (or (eq t org-export-html-inline-images)
  23491. (and org-export-html-inline-images
  23492. (not descp))))
  23493. (concat "<img src=\"" thefile "\"/>")
  23494. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23495. (if (not valid) (setq rpl desc))))
  23496. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23497. (setq rpl (concat "<i>&lt;" type ":"
  23498. (save-match-data (org-link-unescape path))
  23499. "&gt;</i>"))))
  23500. (setq line (replace-match rpl t t line)
  23501. start (+ start (length rpl))))
  23502. ;; TODO items
  23503. (if (and (string-match org-todo-line-regexp line)
  23504. (match-beginning 2))
  23505. (setq line
  23506. (concat (substring line 0 (match-beginning 2))
  23507. "<span class=\""
  23508. (if (member (match-string 2 line)
  23509. org-done-keywords)
  23510. "done" "todo")
  23511. "\">" (match-string 2 line)
  23512. "</span>" (substring line (match-end 2)))))
  23513. ;; Does this contain a reference to a footnote?
  23514. (when org-export-with-footnotes
  23515. (setq start 0)
  23516. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23517. (if (get-text-property (match-beginning 2) 'org-protected line)
  23518. (setq start (match-end 2))
  23519. (let ((n (match-string 2 line)))
  23520. (setq line
  23521. (replace-match
  23522. (format
  23523. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23524. (match-string 1 line) n n n)
  23525. t t line))))))
  23526. (cond
  23527. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23528. ;; This is a headline
  23529. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23530. txt (match-string 2 line))
  23531. (if (string-match quote-re0 txt)
  23532. (setq txt (replace-match "" t t txt)))
  23533. (if (<= level (max umax umax-toc))
  23534. (setq head-count (+ head-count 1)))
  23535. (when in-local-list
  23536. ;; Close any local lists before inserting a new header line
  23537. (while local-list-num
  23538. (org-close-li)
  23539. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23540. (pop local-list-num))
  23541. (setq local-list-indent nil
  23542. in-local-list nil))
  23543. (setq first-heading-pos (or first-heading-pos (point)))
  23544. (org-html-level-start level txt umax
  23545. (and org-export-with-toc (<= level umax))
  23546. head-count)
  23547. ;; QUOTES
  23548. (when (string-match quote-re line)
  23549. (insert "<pre>")
  23550. (setq inquote t)))
  23551. ((and org-export-with-tables
  23552. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23553. (if (not table-open)
  23554. ;; New table starts
  23555. (setq table-open t table-buffer nil table-orig-buffer nil))
  23556. ;; Accumulate lines
  23557. (setq table-buffer (cons line table-buffer)
  23558. table-orig-buffer (cons origline table-orig-buffer))
  23559. (when (or (not lines)
  23560. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23561. (car lines))))
  23562. (setq table-open nil
  23563. table-buffer (nreverse table-buffer)
  23564. table-orig-buffer (nreverse table-orig-buffer))
  23565. (org-close-par-maybe)
  23566. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23567. (t
  23568. ;; Normal lines
  23569. (when (string-match
  23570. (cond
  23571. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23572. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23573. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23574. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23575. line)
  23576. (setq ind (org-get-string-indentation line)
  23577. start-is-num (match-beginning 4)
  23578. starter (if (match-beginning 2)
  23579. (substring (match-string 2 line) 0 -1))
  23580. line (substring line (match-beginning 5)))
  23581. (unless (string-match "[^ \t]" line)
  23582. ;; empty line. Pretend indentation is large.
  23583. (setq ind (if org-empty-line-terminates-plain-lists
  23584. 0
  23585. (1+ (or (car local-list-indent) 1)))))
  23586. (setq didclose nil)
  23587. (while (and in-local-list
  23588. (or (and (= ind (car local-list-indent))
  23589. (not starter))
  23590. (< ind (car local-list-indent))))
  23591. (setq didclose t)
  23592. (org-close-li)
  23593. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23594. (pop local-list-num) (pop local-list-indent)
  23595. (setq in-local-list local-list-indent))
  23596. (cond
  23597. ((and starter
  23598. (or (not in-local-list)
  23599. (> ind (car local-list-indent))))
  23600. ;; Start new (level of) list
  23601. (org-close-par-maybe)
  23602. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23603. (push start-is-num local-list-num)
  23604. (push ind local-list-indent)
  23605. (setq in-local-list t))
  23606. (starter
  23607. ;; continue current list
  23608. (org-close-li)
  23609. (insert "<li>\n"))
  23610. (didclose
  23611. ;; we did close a list, normal text follows: need <p>
  23612. (org-open-par)))
  23613. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23614. (setq line
  23615. (replace-match
  23616. (if (equal (match-string 1 line) "X")
  23617. "<b>[X]</b>"
  23618. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23619. t t line))))
  23620. ;; Empty lines start a new paragraph. If hand-formatted lists
  23621. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23622. ;; also start a new paragraph.
  23623. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23624. ;; Is this the start of a footnote?
  23625. (when org-export-with-footnotes
  23626. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23627. (org-close-par-maybe)
  23628. (let ((n (match-string 1 line)))
  23629. (setq line (replace-match
  23630. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23631. ;; Check if the line break needs to be conserved
  23632. (cond
  23633. ((string-match "\\\\\\\\[ \t]*$" line)
  23634. (setq line (replace-match "<br/>" t t line)))
  23635. (org-export-preserve-breaks
  23636. (setq line (concat line "<br/>"))))
  23637. (insert line "\n")))))
  23638. ;; Properly close all local lists and other lists
  23639. (when inquote (insert "</pre>\n"))
  23640. (when in-local-list
  23641. ;; Close any local lists before inserting a new header line
  23642. (while local-list-num
  23643. (org-close-li)
  23644. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23645. (pop local-list-num))
  23646. (setq local-list-indent nil
  23647. in-local-list nil))
  23648. (org-html-level-start 1 nil umax
  23649. (and org-export-with-toc (<= level umax))
  23650. head-count)
  23651. (unless body-only
  23652. (when (plist-get opt-plist :auto-postamble)
  23653. (insert "<div id=\"postamble\">")
  23654. (when (and org-export-author-info author)
  23655. (insert "<p class=\"author\"> "
  23656. (nth 1 lang-words) ": " author "\n")
  23657. (when email
  23658. (if (listp (split-string email ",+ *"))
  23659. (mapc (lambda(e)
  23660. (insert "<a href=\"mailto:" e "\">&lt;"
  23661. e "&gt;</a>\n"))
  23662. (split-string email ",+ *"))
  23663. (insert "<a href=\"mailto:" email "\">&lt;"
  23664. email "&gt;</a>\n")))
  23665. (insert "</p>\n"))
  23666. (when (and date org-export-time-stamp-file)
  23667. (insert "<p class=\"date\"> "
  23668. (nth 2 lang-words) ": "
  23669. date "</p>\n"))
  23670. (insert "</div>"))
  23671. (if org-export-html-with-timestamp
  23672. (insert org-export-html-html-helper-timestamp))
  23673. (insert (or (plist-get opt-plist :postamble) ""))
  23674. (insert "</body>\n</html>\n"))
  23675. (normal-mode)
  23676. (if (eq major-mode default-major-mode) (html-mode))
  23677. ;; insert the table of contents
  23678. (goto-char (point-min))
  23679. (when thetoc
  23680. (if (or (re-search-forward
  23681. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23682. (re-search-forward
  23683. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23684. (progn
  23685. (goto-char (match-beginning 0))
  23686. (replace-match ""))
  23687. (goto-char first-heading-pos)
  23688. (when (looking-at "\\s-*</p>")
  23689. (goto-char (match-end 0))
  23690. (insert "\n")))
  23691. (insert "<div id=\"table-of-contents\">\n")
  23692. (mapc 'insert thetoc)
  23693. (insert "</div>\n"))
  23694. ;; remove empty paragraphs and lists
  23695. (goto-char (point-min))
  23696. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23697. (replace-match ""))
  23698. (goto-char (point-min))
  23699. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23700. (replace-match ""))
  23701. (goto-char (point-min))
  23702. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23703. (replace-match ""))
  23704. ;; Convert whitespace place holders
  23705. (goto-char (point-min))
  23706. (let (beg end n)
  23707. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23708. (setq n (get-text-property beg 'org-whitespace)
  23709. end (next-single-property-change beg 'org-whitespace))
  23710. (goto-char beg)
  23711. (delete-region beg end)
  23712. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23713. (make-string n ?x)))))
  23714. (or to-buffer (save-buffer))
  23715. (goto-char (point-min))
  23716. (message "Exporting... done")
  23717. (if (eq to-buffer 'string)
  23718. (prog1 (buffer-substring (point-min) (point-max))
  23719. (kill-buffer (current-buffer)))
  23720. (current-buffer)))))
  23721. (defvar org-table-colgroup-info nil)
  23722. (defun org-format-table-ascii (lines)
  23723. "Format a table for ascii export."
  23724. (if (stringp lines)
  23725. (setq lines (org-split-string lines "\n")))
  23726. (if (not (string-match "^[ \t]*|" (car lines)))
  23727. ;; Table made by table.el - test for spanning
  23728. lines
  23729. ;; A normal org table
  23730. ;; Get rid of hlines at beginning and end
  23731. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23732. (setq lines (nreverse lines))
  23733. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23734. (setq lines (nreverse lines))
  23735. (when org-export-table-remove-special-lines
  23736. ;; Check if the table has a marking column. If yes remove the
  23737. ;; column and the special lines
  23738. (setq lines (org-table-clean-before-export lines)))
  23739. ;; Get rid of the vertical lines except for grouping
  23740. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23741. rtn line vl1 start)
  23742. (while (setq line (pop lines))
  23743. (if (string-match org-table-hline-regexp line)
  23744. (and (string-match "|\\(.*\\)|" line)
  23745. (setq line (replace-match " \\1" t nil line)))
  23746. (setq start 0 vl1 vl)
  23747. (while (string-match "|" line start)
  23748. (setq start (match-end 0))
  23749. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23750. (push line rtn))
  23751. (nreverse rtn))))
  23752. (defun org-colgroup-info-to-vline-list (info)
  23753. (let (vl new last)
  23754. (while info
  23755. (setq last new new (pop info))
  23756. (if (or (memq last '(:end :startend))
  23757. (memq new '(:start :startend)))
  23758. (push t vl)
  23759. (push nil vl)))
  23760. (setq vl (nreverse vl))
  23761. (and vl (setcar vl nil))
  23762. vl))
  23763. (defun org-format-table-html (lines olines)
  23764. "Find out which HTML converter to use and return the HTML code."
  23765. (if (stringp lines)
  23766. (setq lines (org-split-string lines "\n")))
  23767. (if (string-match "^[ \t]*|" (car lines))
  23768. ;; A normal org table
  23769. (org-format-org-table-html lines)
  23770. ;; Table made by table.el - test for spanning
  23771. (let* ((hlines (delq nil (mapcar
  23772. (lambda (x)
  23773. (if (string-match "^[ \t]*\\+-" x) x
  23774. nil))
  23775. lines)))
  23776. (first (car hlines))
  23777. (ll (and (string-match "\\S-+" first)
  23778. (match-string 0 first)))
  23779. (re (concat "^[ \t]*" (regexp-quote ll)))
  23780. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23781. hlines))))
  23782. (if (and (not spanning)
  23783. (not org-export-prefer-native-exporter-for-tables))
  23784. ;; We can use my own converter with HTML conversions
  23785. (org-format-table-table-html lines)
  23786. ;; Need to use the code generator in table.el, with the original text.
  23787. (org-format-table-table-html-using-table-generate-source olines)))))
  23788. (defun org-format-org-table-html (lines &optional splice)
  23789. "Format a table into HTML."
  23790. ;; Get rid of hlines at beginning and end
  23791. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23792. (setq lines (nreverse lines))
  23793. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23794. (setq lines (nreverse lines))
  23795. (when org-export-table-remove-special-lines
  23796. ;; Check if the table has a marking column. If yes remove the
  23797. ;; column and the special lines
  23798. (setq lines (org-table-clean-before-export lines)))
  23799. (let ((head (and org-export-highlight-first-table-line
  23800. (delq nil (mapcar
  23801. (lambda (x) (string-match "^[ \t]*|-" x))
  23802. (cdr lines)))))
  23803. (nlines 0) fnum i
  23804. tbopen line fields html gr colgropen)
  23805. (if splice (setq head nil))
  23806. (unless splice (push (if head "<thead>" "<tbody>") html))
  23807. (setq tbopen t)
  23808. (while (setq line (pop lines))
  23809. (catch 'next-line
  23810. (if (string-match "^[ \t]*|-" line)
  23811. (progn
  23812. (unless splice
  23813. (push (if head "</thead>" "</tbody>") html)
  23814. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23815. (setq head nil) ;; head ends here, first time around
  23816. ;; ignore this line
  23817. (throw 'next-line t)))
  23818. ;; Break the line into fields
  23819. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23820. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23821. (setq nlines (1+ nlines) i -1)
  23822. (push (concat "<tr>"
  23823. (mapconcat
  23824. (lambda (x)
  23825. (setq i (1+ i))
  23826. (if (and (< i nlines)
  23827. (string-match org-table-number-regexp x))
  23828. (incf (aref fnum i)))
  23829. (if head
  23830. (concat (car org-export-table-header-tags) x
  23831. (cdr org-export-table-header-tags))
  23832. (concat (car org-export-table-data-tags) x
  23833. (cdr org-export-table-data-tags))))
  23834. fields "")
  23835. "</tr>")
  23836. html)))
  23837. (unless splice (if tbopen (push "</tbody>" html)))
  23838. (unless splice (push "</table>\n" html))
  23839. (setq html (nreverse html))
  23840. (unless splice
  23841. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23842. (push (mapconcat
  23843. (lambda (x)
  23844. (setq gr (pop org-table-colgroup-info))
  23845. (format "%s<col align=\"%s\"></col>%s"
  23846. (if (memq gr '(:start :startend))
  23847. (prog1
  23848. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23849. (setq colgropen t))
  23850. "")
  23851. (if (> (/ (float x) nlines) org-table-number-fraction)
  23852. "right" "left")
  23853. (if (memq gr '(:end :startend))
  23854. (progn (setq colgropen nil) "</colgroup>")
  23855. "")))
  23856. fnum "")
  23857. html)
  23858. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23859. (push html-table-tag html))
  23860. (concat (mapconcat 'identity html "\n") "\n")))
  23861. (defun org-table-clean-before-export (lines)
  23862. "Check if the table has a marking column.
  23863. If yes remove the column and the special lines."
  23864. (setq org-table-colgroup-info nil)
  23865. (if (memq nil
  23866. (mapcar
  23867. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23868. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23869. lines))
  23870. (progn
  23871. (setq org-table-clean-did-remove-column nil)
  23872. (delq nil
  23873. (mapcar
  23874. (lambda (x)
  23875. (cond
  23876. ((string-match "^[ \t]*| */ *|" x)
  23877. (setq org-table-colgroup-info
  23878. (mapcar (lambda (x)
  23879. (cond ((member x '("<" "&lt;")) :start)
  23880. ((member x '(">" "&gt;")) :end)
  23881. ((member x '("<>" "&lt;&gt;")) :startend)
  23882. (t nil)))
  23883. (org-split-string x "[ \t]*|[ \t]*")))
  23884. nil)
  23885. (t x)))
  23886. lines)))
  23887. (setq org-table-clean-did-remove-column t)
  23888. (delq nil
  23889. (mapcar
  23890. (lambda (x)
  23891. (cond
  23892. ((string-match "^[ \t]*| */ *|" x)
  23893. (setq org-table-colgroup-info
  23894. (mapcar (lambda (x)
  23895. (cond ((member x '("<" "&lt;")) :start)
  23896. ((member x '(">" "&gt;")) :end)
  23897. ((member x '("<>" "&lt;&gt;")) :startend)
  23898. (t nil)))
  23899. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23900. nil)
  23901. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23902. nil) ; ignore this line
  23903. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23904. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23905. ;; remove the first column
  23906. (replace-match "\\1|" t nil x))))
  23907. lines))))
  23908. (defun org-format-table-table-html (lines)
  23909. "Format a table generated by table.el into HTML.
  23910. This conversion does *not* use `table-generate-source' from table.el.
  23911. This has the advantage that Org-mode's HTML conversions can be used.
  23912. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23913. (let (line field-buffer
  23914. (head org-export-highlight-first-table-line)
  23915. fields html empty)
  23916. (setq html (concat html-table-tag "\n"))
  23917. (while (setq line (pop lines))
  23918. (setq empty "&nbsp;")
  23919. (catch 'next-line
  23920. (if (string-match "^[ \t]*\\+-" line)
  23921. (progn
  23922. (if field-buffer
  23923. (progn
  23924. (setq
  23925. html
  23926. (concat
  23927. html
  23928. "<tr>"
  23929. (mapconcat
  23930. (lambda (x)
  23931. (if (equal x "") (setq x empty))
  23932. (if head
  23933. (concat (car org-export-table-header-tags) x
  23934. (cdr org-export-table-header-tags))
  23935. (concat (car org-export-table-data-tags) x
  23936. (cdr org-export-table-data-tags))))
  23937. field-buffer "\n")
  23938. "</tr>\n"))
  23939. (setq head nil)
  23940. (setq field-buffer nil)))
  23941. ;; Ignore this line
  23942. (throw 'next-line t)))
  23943. ;; Break the line into fields and store the fields
  23944. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23945. (if field-buffer
  23946. (setq field-buffer (mapcar
  23947. (lambda (x)
  23948. (concat x "<br/>" (pop fields)))
  23949. field-buffer))
  23950. (setq field-buffer fields))))
  23951. (setq html (concat html "</table>\n"))
  23952. html))
  23953. (defun org-format-table-table-html-using-table-generate-source (lines)
  23954. "Format a table into html, using `table-generate-source' from table.el.
  23955. This has the advantage that cell- or row-spanning is allowed.
  23956. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23957. (require 'table)
  23958. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23959. (erase-buffer)
  23960. (insert (mapconcat 'identity lines "\n"))
  23961. (goto-char (point-min))
  23962. (if (not (re-search-forward "|[^+]" nil t))
  23963. (error "Error processing table"))
  23964. (table-recognize-table)
  23965. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23966. (table-generate-source 'html " org-tmp2 ")
  23967. (set-buffer " org-tmp2 ")
  23968. (buffer-substring (point-min) (point-max))))
  23969. (defun org-html-handle-time-stamps (s)
  23970. "Format time stamps in string S, or remove them."
  23971. (catch 'exit
  23972. (let (r b)
  23973. (while (string-match org-maybe-keyword-time-regexp s)
  23974. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23975. ;; never export CLOCK
  23976. (throw 'exit ""))
  23977. (or b (setq b (substring s 0 (match-beginning 0))))
  23978. (if (not org-export-with-timestamps)
  23979. (setq r (concat r (substring s 0 (match-beginning 0)))
  23980. s (substring s (match-end 0)))
  23981. (setq r (concat
  23982. r (substring s 0 (match-beginning 0))
  23983. (if (match-end 1)
  23984. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23985. (match-string 1 s)))
  23986. (format " @<span class=\"timestamp\">%s@</span>"
  23987. (substring
  23988. (org-translate-time (match-string 3 s)) 1 -1)))
  23989. s (substring s (match-end 0)))))
  23990. ;; Line break if line started and ended with time stamp stuff
  23991. (if (not r)
  23992. s
  23993. (setq r (concat r s))
  23994. (unless (string-match "\\S-" (concat b s))
  23995. (setq r (concat r "@<br/>")))
  23996. r))))
  23997. (defun org-html-protect (s)
  23998. ;; convert & to &amp;, < to &lt; and > to &gt;
  23999. (let ((start 0))
  24000. (while (string-match "&" s start)
  24001. (setq s (replace-match "&amp;" t t s)
  24002. start (1+ (match-beginning 0))))
  24003. (while (string-match "<" s)
  24004. (setq s (replace-match "&lt;" t t s)))
  24005. (while (string-match ">" s)
  24006. (setq s (replace-match "&gt;" t t s))))
  24007. s)
  24008. (defun org-export-cleanup-toc-line (s)
  24009. "Remove tags and time staps from lines going into the toc."
  24010. (when (memq org-export-with-tags '(not-in-toc nil))
  24011. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24012. (setq s (replace-match "" t t s))))
  24013. (when org-export-remove-timestamps-from-toc
  24014. (while (string-match org-maybe-keyword-time-regexp s)
  24015. (setq s (replace-match "" t t s))))
  24016. (while (string-match org-bracket-link-regexp s)
  24017. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24018. t t s)))
  24019. s)
  24020. (defun org-html-expand (string)
  24021. "Prepare STRING for HTML export. Applies all active conversions.
  24022. If there are links in the string, don't modify these."
  24023. (let* ((re (concat org-bracket-link-regexp "\\|"
  24024. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24025. m s l res)
  24026. (while (setq m (string-match re string))
  24027. (setq s (substring string 0 m)
  24028. l (match-string 0 string)
  24029. string (substring string (match-end 0)))
  24030. (push (org-html-do-expand s) res)
  24031. (push l res))
  24032. (push (org-html-do-expand string) res)
  24033. (apply 'concat (nreverse res))))
  24034. (defun org-html-do-expand (s)
  24035. "Apply all active conversions to translate special ASCII to HTML."
  24036. (setq s (org-html-protect s))
  24037. (if org-export-html-expand
  24038. (let ((start 0))
  24039. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24040. (setq s (replace-match "<\\1>" t nil s)))))
  24041. (if org-export-with-emphasize
  24042. (setq s (org-export-html-convert-emphasize s)))
  24043. (if org-export-with-special-strings
  24044. (setq s (org-export-html-convert-special-strings s)))
  24045. (if org-export-with-sub-superscripts
  24046. (setq s (org-export-html-convert-sub-super s)))
  24047. (if org-export-with-TeX-macros
  24048. (let ((start 0) wd ass)
  24049. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24050. (if (get-text-property (match-beginning 0) 'org-protected s)
  24051. (setq start (match-end 0))
  24052. (setq wd (match-string 1 s))
  24053. (if (setq ass (assoc wd org-html-entities))
  24054. (setq s (replace-match (or (cdr ass)
  24055. (concat "&" (car ass) ";"))
  24056. t t s))
  24057. (setq start (+ start (length wd))))))))
  24058. s)
  24059. (defun org-create-multibrace-regexp (left right n)
  24060. "Create a regular expression which will match a balanced sexp.
  24061. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24062. as single character strings.
  24063. The regexp returned will match the entire expression including the
  24064. delimiters. It will also define a single group which contains the
  24065. match except for the outermost delimiters. The maximum depth of
  24066. stacked delimiters is N. Escaping delimiters is not possible."
  24067. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24068. (or "\\|")
  24069. (re nothing)
  24070. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24071. (while (> n 1)
  24072. (setq n (1- n)
  24073. re (concat re or next)
  24074. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24075. (concat left "\\(" re "\\)" right)))
  24076. (defvar org-match-substring-regexp
  24077. (concat
  24078. "\\([^\\]\\)\\([_^]\\)\\("
  24079. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24080. "\\|"
  24081. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24082. "\\|"
  24083. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24084. "The regular expression matching a sub- or superscript.")
  24085. (defvar org-match-substring-with-braces-regexp
  24086. (concat
  24087. "\\([^\\]\\)\\([_^]\\)\\("
  24088. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24089. "\\)")
  24090. "The regular expression matching a sub- or superscript, forcing braces.")
  24091. (defconst org-export-html-special-string-regexps
  24092. '(("\\\\-" . "&shy;")
  24093. ("---\\([^-]\\)" . "&mdash;\\1")
  24094. ("--\\([^-]\\)" . "&ndash;\\1")
  24095. ("\\.\\.\\." . "&hellip;"))
  24096. "Regular expressions for special string conversion.")
  24097. (defun org-export-html-convert-special-strings (string)
  24098. "Convert special characters in STRING to HTML."
  24099. (let ((all org-export-html-special-string-regexps)
  24100. e a re rpl start)
  24101. (while (setq a (pop all))
  24102. (setq re (car a) rpl (cdr a) start 0)
  24103. (while (string-match re string start)
  24104. (if (get-text-property (match-beginning 0) 'org-protected string)
  24105. (setq start (match-end 0))
  24106. (setq string (replace-match rpl t nil string)))))
  24107. string))
  24108. (defun org-export-html-convert-sub-super (string)
  24109. "Convert sub- and superscripts in STRING to HTML."
  24110. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24111. (while (string-match org-match-substring-regexp string s)
  24112. (cond
  24113. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24114. ((get-text-property (match-beginning 2) 'org-protected string)
  24115. (setq s (match-end 2)))
  24116. (t
  24117. (setq s (match-end 1)
  24118. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24119. c (or (match-string 8 string)
  24120. (match-string 6 string)
  24121. (match-string 5 string))
  24122. string (replace-match
  24123. (concat (match-string 1 string)
  24124. "<" key ">" c "</" key ">")
  24125. t t string)))))
  24126. (while (string-match "\\\\\\([_^]\\)" string)
  24127. (setq string (replace-match (match-string 1 string) t t string)))
  24128. string))
  24129. (defun org-export-html-convert-emphasize (string)
  24130. "Apply emphasis."
  24131. (let ((s 0) rpl)
  24132. (while (string-match org-emph-re string s)
  24133. (if (not (equal
  24134. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24135. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24136. (setq s (match-beginning 0)
  24137. rpl
  24138. (concat
  24139. (match-string 1 string)
  24140. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24141. (match-string 4 string)
  24142. (nth 3 (assoc (match-string 3 string)
  24143. org-emphasis-alist))
  24144. (match-string 5 string))
  24145. string (replace-match rpl t t string)
  24146. s (+ s (- (length rpl) 2)))
  24147. (setq s (1+ s))))
  24148. string))
  24149. (defvar org-par-open nil)
  24150. (defun org-open-par ()
  24151. "Insert <p>, but first close previous paragraph if any."
  24152. (org-close-par-maybe)
  24153. (insert "\n<p>")
  24154. (setq org-par-open t))
  24155. (defun org-close-par-maybe ()
  24156. "Close paragraph if there is one open."
  24157. (when org-par-open
  24158. (insert "</p>")
  24159. (setq org-par-open nil)))
  24160. (defun org-close-li ()
  24161. "Close <li> if necessary."
  24162. (org-close-par-maybe)
  24163. (insert "</li>\n"))
  24164. (defvar body-only) ; dynamically scoped into this.
  24165. (defun org-html-level-start (level title umax with-toc head-count)
  24166. "Insert a new level in HTML export.
  24167. When TITLE is nil, just close all open levels."
  24168. (org-close-par-maybe)
  24169. (let ((l org-level-max))
  24170. (while (>= l level)
  24171. (if (aref org-levels-open (1- l))
  24172. (progn
  24173. (org-html-level-close l umax)
  24174. (aset org-levels-open (1- l) nil)))
  24175. (setq l (1- l)))
  24176. (when title
  24177. ;; If title is nil, this means this function is called to close
  24178. ;; all levels, so the rest is done only if title is given
  24179. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24180. (setq title (replace-match
  24181. (if org-export-with-tags
  24182. (save-match-data
  24183. (concat
  24184. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24185. (mapconcat 'identity (org-split-string
  24186. (match-string 1 title) ":")
  24187. "&nbsp;")
  24188. "</span>"))
  24189. "")
  24190. t t title)))
  24191. (if (> level umax)
  24192. (progn
  24193. (if (aref org-levels-open (1- level))
  24194. (progn
  24195. (org-close-li)
  24196. (insert "<li>" title "<br/>\n"))
  24197. (aset org-levels-open (1- level) t)
  24198. (org-close-par-maybe)
  24199. (insert "<ul>\n<li>" title "<br/>\n")))
  24200. (aset org-levels-open (1- level) t)
  24201. (if (and org-export-with-section-numbers (not body-only))
  24202. (setq title (concat (org-section-number level) " " title)))
  24203. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24204. (if with-toc
  24205. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24206. level level head-count title level))
  24207. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24208. (org-open-par)))))
  24209. (defun org-html-level-close (level max-outline-level)
  24210. "Terminate one level in HTML export."
  24211. (if (<= level max-outline-level)
  24212. (insert "</div>\n")
  24213. (org-close-li)
  24214. (insert "</ul>\n")))
  24215. ;;; iCalendar export
  24216. ;;;###autoload
  24217. (defun org-export-icalendar-this-file ()
  24218. "Export current file as an iCalendar file.
  24219. The iCalendar file will be located in the same directory as the Org-mode
  24220. file, but with extension `.ics'."
  24221. (interactive)
  24222. (org-export-icalendar nil buffer-file-name))
  24223. ;;;###autoload
  24224. (defun org-export-icalendar-all-agenda-files ()
  24225. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24226. Each iCalendar file will be located in the same directory as the Org-mode
  24227. file, but with extension `.ics'."
  24228. (interactive)
  24229. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24230. ;;;###autoload
  24231. (defun org-export-icalendar-combine-agenda-files ()
  24232. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24233. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24234. (interactive)
  24235. (apply 'org-export-icalendar t (org-agenda-files t)))
  24236. (defun org-export-icalendar (combine &rest files)
  24237. "Create iCalendar files for all elements of FILES.
  24238. If COMBINE is non-nil, combine all calendar entries into a single large
  24239. file and store it under the name `org-combined-agenda-icalendar-file'."
  24240. (save-excursion
  24241. (org-prepare-agenda-buffers files)
  24242. (let* ((dir (org-export-directory
  24243. :ical (list :publishing-directory
  24244. org-export-publishing-directory)))
  24245. file ical-file ical-buffer category started org-agenda-new-buffers)
  24246. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24247. (when combine
  24248. (setq ical-file
  24249. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24250. org-combined-agenda-icalendar-file
  24251. (expand-file-name org-combined-agenda-icalendar-file dir))
  24252. ical-buffer (org-get-agenda-file-buffer ical-file))
  24253. (set-buffer ical-buffer) (erase-buffer))
  24254. (while (setq file (pop files))
  24255. (catch 'nextfile
  24256. (org-check-agenda-file file)
  24257. (set-buffer (org-get-agenda-file-buffer file))
  24258. (unless combine
  24259. (setq ical-file (concat (file-name-as-directory dir)
  24260. (file-name-sans-extension
  24261. (file-name-nondirectory buffer-file-name))
  24262. ".ics"))
  24263. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24264. (with-current-buffer ical-buffer (erase-buffer)))
  24265. (setq category (or org-category
  24266. (file-name-sans-extension
  24267. (file-name-nondirectory buffer-file-name))))
  24268. (if (symbolp category) (setq category (symbol-name category)))
  24269. (let ((standard-output ical-buffer))
  24270. (if combine
  24271. (and (not started) (setq started t)
  24272. (org-start-icalendar-file org-icalendar-combined-name))
  24273. (org-start-icalendar-file category))
  24274. (org-print-icalendar-entries combine)
  24275. (when (or (and combine (not files)) (not combine))
  24276. (org-finish-icalendar-file)
  24277. (set-buffer ical-buffer)
  24278. (save-buffer)
  24279. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24280. (org-release-buffers org-agenda-new-buffers))))
  24281. (defvar org-after-save-iCalendar-file-hook nil
  24282. "Hook run after an iCalendar file has been saved.
  24283. The iCalendar buffer is still current when this hook is run.
  24284. A good way to use this is to tell a desktop calenndar application to re-read
  24285. the iCalendar file.")
  24286. (defun org-print-icalendar-entries (&optional combine)
  24287. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24288. When COMBINE is non nil, add the category to each line."
  24289. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24290. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24291. (dts (org-ical-ts-to-string
  24292. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24293. "DTSTART"))
  24294. hd ts ts2 state status (inc t) pos b sexp rrule
  24295. scheduledp deadlinep tmp pri category entry location summary desc
  24296. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24297. (org-refresh-category-properties)
  24298. (save-excursion
  24299. (goto-char (point-min))
  24300. (while (re-search-forward re1 nil t)
  24301. (catch :skip
  24302. (org-agenda-skip)
  24303. (setq pos (match-beginning 0)
  24304. ts (match-string 0)
  24305. inc t
  24306. hd (org-get-heading)
  24307. summary (org-icalendar-cleanup-string
  24308. (org-entry-get nil "SUMMARY"))
  24309. desc (org-icalendar-cleanup-string
  24310. (or (org-entry-get nil "DESCRIPTION")
  24311. (and org-icalendar-include-body (org-get-entry)))
  24312. t org-icalendar-include-body)
  24313. location (org-icalendar-cleanup-string
  24314. (org-entry-get nil "LOCATION"))
  24315. category (org-get-category))
  24316. (if (looking-at re2)
  24317. (progn
  24318. (goto-char (match-end 0))
  24319. (setq ts2 (match-string 1) inc nil))
  24320. (setq tmp (buffer-substring (max (point-min)
  24321. (- pos org-ds-keyword-length))
  24322. pos)
  24323. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24324. (progn
  24325. (setq inc nil)
  24326. (replace-match "\\1" t nil ts))
  24327. ts)
  24328. deadlinep (string-match org-deadline-regexp tmp)
  24329. scheduledp (string-match org-scheduled-regexp tmp)
  24330. ;; donep (org-entry-is-done-p)
  24331. ))
  24332. (if (or (string-match org-tr-regexp hd)
  24333. (string-match org-ts-regexp hd))
  24334. (setq hd (replace-match "" t t hd)))
  24335. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24336. (setq rrule
  24337. (concat "\nRRULE:FREQ="
  24338. (cdr (assoc
  24339. (match-string 2 ts)
  24340. '(("d" . "DAILY")("w" . "WEEKLY")
  24341. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24342. ";INTERVAL=" (match-string 1 ts)))
  24343. (setq rrule ""))
  24344. (setq summary (or summary hd))
  24345. (if (string-match org-bracket-link-regexp summary)
  24346. (setq summary
  24347. (replace-match (if (match-end 3)
  24348. (match-string 3 summary)
  24349. (match-string 1 summary))
  24350. t t summary)))
  24351. (if deadlinep (setq summary (concat "DL: " summary)))
  24352. (if scheduledp (setq summary (concat "S: " summary)))
  24353. (if (string-match "\\`<%%" ts)
  24354. (with-current-buffer sexp-buffer
  24355. (insert (substring ts 1 -1) " " summary "\n"))
  24356. (princ (format "BEGIN:VEVENT
  24357. %s
  24358. %s%s
  24359. SUMMARY:%s%s%s
  24360. CATEGORIES:%s
  24361. END:VEVENT\n"
  24362. (org-ical-ts-to-string ts "DTSTART")
  24363. (org-ical-ts-to-string ts2 "DTEND" inc)
  24364. rrule summary
  24365. (if (and desc (string-match "\\S-" desc))
  24366. (concat "\nDESCRIPTION: " desc) "")
  24367. (if (and location (string-match "\\S-" location))
  24368. (concat "\nLOCATION: " location) "")
  24369. category)))))
  24370. (when (and org-icalendar-include-sexps
  24371. (condition-case nil (require 'icalendar) (error nil))
  24372. (fboundp 'icalendar-export-region))
  24373. ;; Get all the literal sexps
  24374. (goto-char (point-min))
  24375. (while (re-search-forward "^&?%%(" nil t)
  24376. (catch :skip
  24377. (org-agenda-skip)
  24378. (setq b (match-beginning 0))
  24379. (goto-char (1- (match-end 0)))
  24380. (forward-sexp 1)
  24381. (end-of-line 1)
  24382. (setq sexp (buffer-substring b (point)))
  24383. (with-current-buffer sexp-buffer
  24384. (insert sexp "\n"))
  24385. (princ (org-diary-to-ical-string sexp-buffer)))))
  24386. (when org-icalendar-include-todo
  24387. (goto-char (point-min))
  24388. (while (re-search-forward org-todo-line-regexp nil t)
  24389. (catch :skip
  24390. (org-agenda-skip)
  24391. (setq state (match-string 2))
  24392. (setq status (if (member state org-done-keywords)
  24393. "COMPLETED" "NEEDS-ACTION"))
  24394. (when (and state
  24395. (or (not (member state org-done-keywords))
  24396. (eq org-icalendar-include-todo 'all))
  24397. (not (member org-archive-tag (org-get-tags-at)))
  24398. )
  24399. (setq hd (match-string 3)
  24400. summary (org-icalendar-cleanup-string
  24401. (org-entry-get nil "SUMMARY"))
  24402. desc (org-icalendar-cleanup-string
  24403. (or (org-entry-get nil "DESCRIPTION")
  24404. (and org-icalendar-include-body (org-get-entry)))
  24405. t org-icalendar-include-body)
  24406. location (org-icalendar-cleanup-string
  24407. (org-entry-get nil "LOCATION")))
  24408. (if (string-match org-bracket-link-regexp hd)
  24409. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24410. (match-string 1 hd))
  24411. t t hd)))
  24412. (if (string-match org-priority-regexp hd)
  24413. (setq pri (string-to-char (match-string 2 hd))
  24414. hd (concat (substring hd 0 (match-beginning 1))
  24415. (substring hd (match-end 1))))
  24416. (setq pri org-default-priority))
  24417. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24418. (- org-lowest-priority org-highest-priority))))))
  24419. (princ (format "BEGIN:VTODO
  24420. %s
  24421. SUMMARY:%s%s%s
  24422. CATEGORIES:%s
  24423. SEQUENCE:1
  24424. PRIORITY:%d
  24425. STATUS:%s
  24426. END:VTODO\n"
  24427. dts
  24428. (or summary hd)
  24429. (if (and location (string-match "\\S-" location))
  24430. (concat "\nLOCATION: " location) "")
  24431. (if (and desc (string-match "\\S-" desc))
  24432. (concat "\nDESCRIPTION: " desc) "")
  24433. category pri status)))))))))
  24434. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24435. "Take out stuff and quote what needs to be quoted.
  24436. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24437. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24438. characters."
  24439. (if (not s)
  24440. nil
  24441. (when is-body
  24442. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24443. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24444. (while (string-match re s) (setq s (replace-match "" t t s)))
  24445. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24446. (let ((start 0))
  24447. (while (string-match "\\([,;\\]\\)" s start)
  24448. (setq start (+ (match-beginning 0) 2)
  24449. s (replace-match "\\\\\\1" nil nil s))))
  24450. (when is-body
  24451. (while (string-match "[ \t]*\n[ \t]*" s)
  24452. (setq s (replace-match "\\n" t t s))))
  24453. (setq s (org-trim s))
  24454. (if is-body
  24455. (if maxlength
  24456. (if (and (numberp maxlength)
  24457. (> (length s) maxlength))
  24458. (setq s (substring s 0 maxlength)))))
  24459. s))
  24460. (defun org-get-entry ()
  24461. "Clean-up description string."
  24462. (save-excursion
  24463. (org-back-to-heading t)
  24464. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24465. (defun org-start-icalendar-file (name)
  24466. "Start an iCalendar file by inserting the header."
  24467. (let ((user user-full-name)
  24468. (name (or name "unknown"))
  24469. (timezone (cadr (current-time-zone))))
  24470. (princ
  24471. (format "BEGIN:VCALENDAR
  24472. VERSION:2.0
  24473. X-WR-CALNAME:%s
  24474. PRODID:-//%s//Emacs with Org-mode//EN
  24475. X-WR-TIMEZONE:%s
  24476. CALSCALE:GREGORIAN\n" name user timezone))))
  24477. (defun org-finish-icalendar-file ()
  24478. "Finish an iCalendar file by inserting the END statement."
  24479. (princ "END:VCALENDAR\n"))
  24480. (defun org-ical-ts-to-string (s keyword &optional inc)
  24481. "Take a time string S and convert it to iCalendar format.
  24482. KEYWORD is added in front, to make a complete line like DTSTART....
  24483. When INC is non-nil, increase the hour by two (if time string contains
  24484. a time), or the day by one (if it does not contain a time)."
  24485. (let ((t1 (org-parse-time-string s 'nodefault))
  24486. t2 fmt have-time time)
  24487. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24488. (setq t2 t1 have-time t)
  24489. (setq t2 (org-parse-time-string s)))
  24490. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24491. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24492. (when inc
  24493. (if have-time
  24494. (if org-agenda-default-appointment-duration
  24495. (setq mi (+ org-agenda-default-appointment-duration mi))
  24496. (setq h (+ 2 h)))
  24497. (setq d (1+ d))))
  24498. (setq time (encode-time s mi h d m y)))
  24499. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24500. (concat keyword (format-time-string fmt time))))
  24501. ;;; XOXO export
  24502. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24503. (with-current-buffer buffer
  24504. (apply 'insert output)))
  24505. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24506. (defun org-export-as-xoxo (&optional buffer)
  24507. "Export the org buffer as XOXO.
  24508. The XOXO buffer is named *xoxo-<source buffer name>*"
  24509. (interactive (list (current-buffer)))
  24510. ;; A quickie abstraction
  24511. ;; Output everything as XOXO
  24512. (with-current-buffer (get-buffer buffer)
  24513. (let* ((pos (point))
  24514. (opt-plist (org-combine-plists (org-default-export-plist)
  24515. (org-infile-export-plist)))
  24516. (filename (concat (file-name-as-directory
  24517. (org-export-directory :xoxo opt-plist))
  24518. (file-name-sans-extension
  24519. (file-name-nondirectory buffer-file-name))
  24520. ".html"))
  24521. (out (find-file-noselect filename))
  24522. (last-level 1)
  24523. (hanging-li nil))
  24524. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24525. ;; Check the output buffer is empty.
  24526. (with-current-buffer out (erase-buffer))
  24527. ;; Kick off the output
  24528. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24529. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24530. (let* ((hd (match-string-no-properties 1))
  24531. (level (length hd))
  24532. (text (concat
  24533. (match-string-no-properties 2)
  24534. (save-excursion
  24535. (goto-char (match-end 0))
  24536. (let ((str ""))
  24537. (catch 'loop
  24538. (while 't
  24539. (forward-line)
  24540. (if (looking-at "^[ \t]\\(.*\\)")
  24541. (setq str (concat str (match-string-no-properties 1)))
  24542. (throw 'loop str)))))))))
  24543. ;; Handle level rendering
  24544. (cond
  24545. ((> level last-level)
  24546. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24547. ((< level last-level)
  24548. (dotimes (- (- last-level level) 1)
  24549. (if hanging-li
  24550. (org-export-as-xoxo-insert-into out "</li>\n"))
  24551. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24552. (when hanging-li
  24553. (org-export-as-xoxo-insert-into out "</li>\n")
  24554. (setq hanging-li nil)))
  24555. ((equal level last-level)
  24556. (if hanging-li
  24557. (org-export-as-xoxo-insert-into out "</li>\n")))
  24558. )
  24559. (setq last-level level)
  24560. ;; And output the new li
  24561. (setq hanging-li 't)
  24562. (if (equal ?+ (elt text 0))
  24563. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24564. (org-export-as-xoxo-insert-into out "<li>" text))))
  24565. ;; Finally finish off the ol
  24566. (dotimes (- last-level 1)
  24567. (if hanging-li
  24568. (org-export-as-xoxo-insert-into out "</li>\n"))
  24569. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24570. (goto-char pos)
  24571. ;; Finish the buffer off and clean it up.
  24572. (switch-to-buffer-other-window out)
  24573. (indent-region (point-min) (point-max) nil)
  24574. (save-buffer)
  24575. (goto-char (point-min))
  24576. )))
  24577. ;;;; Key bindings
  24578. ;; Make `C-c C-x' a prefix key
  24579. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24580. ;; TAB key with modifiers
  24581. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24582. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24583. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24584. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24585. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24586. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24587. ;; The following line is necessary under Suse GNU/Linux
  24588. (unless (featurep 'xemacs)
  24589. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24590. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24591. (define-key org-mode-map [backtab] 'org-shifttab)
  24592. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24593. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24594. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24595. ;; Cursor keys with modifiers
  24596. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24597. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24598. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24599. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24600. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24601. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24602. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24603. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24604. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24605. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24606. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24607. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24608. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24609. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24610. ;;; Extra keys for tty access.
  24611. ;; We only set them when really needed because otherwise the
  24612. ;; menus don't show the simple keys
  24613. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24614. (not window-system))
  24615. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24616. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24617. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24618. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24619. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24620. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24621. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24622. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24623. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24624. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24625. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24626. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24627. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24628. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24629. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24630. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24631. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24632. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24633. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24634. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24635. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24636. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24637. ;; All the other keys
  24638. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24639. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24640. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24641. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24642. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24643. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24644. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24645. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24646. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24647. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24648. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24649. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24650. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24651. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24652. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24653. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24654. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24655. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24656. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24657. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24658. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24659. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24660. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24661. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24662. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24663. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24664. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24665. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24666. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24667. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24668. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24669. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24670. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24671. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24672. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24673. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24674. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24675. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24676. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24677. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24678. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24679. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24680. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24681. (org-defkey org-mode-map "\C-m" 'org-return)
  24682. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24683. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24684. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24685. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24686. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24687. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24688. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24689. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24690. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24691. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24692. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24693. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24694. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24695. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24696. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24697. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24698. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24699. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24700. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24701. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24702. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24703. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24704. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24705. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24706. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24707. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24708. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24709. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24710. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24711. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24712. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24713. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24714. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24715. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24716. (when (featurep 'xemacs)
  24717. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24718. (defsubst org-table-p () (org-at-table-p))
  24719. (defun org-self-insert-command (N)
  24720. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24721. If the cursor is in a table looking at whitespace, the whitespace is
  24722. overwritten, and the table is not marked as requiring realignment."
  24723. (interactive "p")
  24724. (if (and (org-table-p)
  24725. (progn
  24726. ;; check if we blank the field, and if that triggers align
  24727. (and org-table-auto-blank-field
  24728. (member last-command
  24729. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24730. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24731. ;; got extra space, this field does not determine column width
  24732. (let (org-table-may-need-update) (org-table-blank-field))
  24733. ;; no extra space, this field may determine column width
  24734. (org-table-blank-field)))
  24735. t)
  24736. (eq N 1)
  24737. (looking-at "[^|\n]* |"))
  24738. (let (org-table-may-need-update)
  24739. (goto-char (1- (match-end 0)))
  24740. (delete-backward-char 1)
  24741. (goto-char (match-beginning 0))
  24742. (self-insert-command N))
  24743. (setq org-table-may-need-update t)
  24744. (self-insert-command N)
  24745. (org-fix-tags-on-the-fly)))
  24746. (defun org-fix-tags-on-the-fly ()
  24747. (when (and (equal (char-after (point-at-bol)) ?*)
  24748. (org-on-heading-p))
  24749. (org-align-tags-here org-tags-column)))
  24750. (defun org-delete-backward-char (N)
  24751. "Like `delete-backward-char', insert whitespace at field end in tables.
  24752. When deleting backwards, in tables this function will insert whitespace in
  24753. front of the next \"|\" separator, to keep the table aligned. The table will
  24754. still be marked for re-alignment if the field did fill the entire column,
  24755. because, in this case the deletion might narrow the column."
  24756. (interactive "p")
  24757. (if (and (org-table-p)
  24758. (eq N 1)
  24759. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24760. (looking-at ".*?|"))
  24761. (let ((pos (point))
  24762. (noalign (looking-at "[^|\n\r]* |"))
  24763. (c org-table-may-need-update))
  24764. (backward-delete-char N)
  24765. (skip-chars-forward "^|")
  24766. (insert " ")
  24767. (goto-char (1- pos))
  24768. ;; noalign: if there were two spaces at the end, this field
  24769. ;; does not determine the width of the column.
  24770. (if noalign (setq org-table-may-need-update c)))
  24771. (backward-delete-char N)
  24772. (org-fix-tags-on-the-fly)))
  24773. (defun org-delete-char (N)
  24774. "Like `delete-char', but insert whitespace at field end in tables.
  24775. When deleting characters, in tables this function will insert whitespace in
  24776. front of the next \"|\" separator, to keep the table aligned. The table will
  24777. still be marked for re-alignment if the field did fill the entire column,
  24778. because, in this case the deletion might narrow the column."
  24779. (interactive "p")
  24780. (if (and (org-table-p)
  24781. (not (bolp))
  24782. (not (= (char-after) ?|))
  24783. (eq N 1))
  24784. (if (looking-at ".*?|")
  24785. (let ((pos (point))
  24786. (noalign (looking-at "[^|\n\r]* |"))
  24787. (c org-table-may-need-update))
  24788. (replace-match (concat
  24789. (substring (match-string 0) 1 -1)
  24790. " |"))
  24791. (goto-char pos)
  24792. ;; noalign: if there were two spaces at the end, this field
  24793. ;; does not determine the width of the column.
  24794. (if noalign (setq org-table-may-need-update c)))
  24795. (delete-char N))
  24796. (delete-char N)
  24797. (org-fix-tags-on-the-fly)))
  24798. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24799. (put 'org-self-insert-command 'delete-selection t)
  24800. (put 'orgtbl-self-insert-command 'delete-selection t)
  24801. (put 'org-delete-char 'delete-selection 'supersede)
  24802. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24803. ;; Make `flyspell-mode' delay after some commands
  24804. (put 'org-self-insert-command 'flyspell-delayed t)
  24805. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24806. (put 'org-delete-char 'flyspell-delayed t)
  24807. (put 'org-delete-backward-char 'flyspell-delayed t)
  24808. ;; Make pabbrev-mode expand after org-mode commands
  24809. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24810. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24811. ;; How to do this: Measure non-white length of current string
  24812. ;; If equal to column width, we should realign.
  24813. (defun org-remap (map &rest commands)
  24814. "In MAP, remap the functions given in COMMANDS.
  24815. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24816. (let (new old)
  24817. (while commands
  24818. (setq old (pop commands) new (pop commands))
  24819. (if (fboundp 'command-remapping)
  24820. (org-defkey map (vector 'remap old) new)
  24821. (substitute-key-definition old new map global-map)))))
  24822. (when (eq org-enable-table-editor 'optimized)
  24823. ;; If the user wants maximum table support, we need to hijack
  24824. ;; some standard editing functions
  24825. (org-remap org-mode-map
  24826. 'self-insert-command 'org-self-insert-command
  24827. 'delete-char 'org-delete-char
  24828. 'delete-backward-char 'org-delete-backward-char)
  24829. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24830. (defun org-shiftcursor-error ()
  24831. "Throw an error because Shift-Cursor command was applied in wrong context."
  24832. (error "This command is active in special context like tables, headlines or timestamps"))
  24833. (defun org-shifttab (&optional arg)
  24834. "Global visibility cycling or move to previous table field.
  24835. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24836. on context.
  24837. See the individual commands for more information."
  24838. (interactive "P")
  24839. (cond
  24840. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24841. (arg (message "Content view to level: ")
  24842. (org-content (prefix-numeric-value arg))
  24843. (setq org-cycle-global-status 'overview))
  24844. (t (call-interactively 'org-global-cycle))))
  24845. (defun org-shiftmetaleft ()
  24846. "Promote subtree or delete table column.
  24847. Calls `org-promote-subtree', `org-outdent-item',
  24848. or `org-table-delete-column', depending on context.
  24849. See the individual commands for more information."
  24850. (interactive)
  24851. (cond
  24852. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24853. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24854. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24855. (t (org-shiftcursor-error))))
  24856. (defun org-shiftmetaright ()
  24857. "Demote subtree or insert table column.
  24858. Calls `org-demote-subtree', `org-indent-item',
  24859. or `org-table-insert-column', depending on context.
  24860. See the individual commands for more information."
  24861. (interactive)
  24862. (cond
  24863. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24864. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24865. ((org-at-item-p) (call-interactively 'org-indent-item))
  24866. (t (org-shiftcursor-error))))
  24867. (defun org-shiftmetaup (&optional arg)
  24868. "Move subtree up or kill table row.
  24869. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24870. `org-move-item-up' depending on context. See the individual commands
  24871. for more information."
  24872. (interactive "P")
  24873. (cond
  24874. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24875. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24876. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24877. (t (org-shiftcursor-error))))
  24878. (defun org-shiftmetadown (&optional arg)
  24879. "Move subtree down or insert table row.
  24880. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24881. `org-move-item-down', depending on context. See the individual
  24882. commands for more information."
  24883. (interactive "P")
  24884. (cond
  24885. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24886. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24887. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24888. (t (org-shiftcursor-error))))
  24889. (defun org-metaleft (&optional arg)
  24890. "Promote heading or move table column to left.
  24891. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24892. With no specific context, calls the Emacs default `backward-word'.
  24893. See the individual commands for more information."
  24894. (interactive "P")
  24895. (cond
  24896. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24897. ((or (org-on-heading-p) (org-region-active-p))
  24898. (call-interactively 'org-do-promote))
  24899. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24900. (t (call-interactively 'backward-word))))
  24901. (defun org-metaright (&optional arg)
  24902. "Demote subtree or move table column to right.
  24903. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24904. With no specific context, calls the Emacs default `forward-word'.
  24905. See the individual commands for more information."
  24906. (interactive "P")
  24907. (cond
  24908. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24909. ((or (org-on-heading-p) (org-region-active-p))
  24910. (call-interactively 'org-do-demote))
  24911. ((org-at-item-p) (call-interactively 'org-indent-item))
  24912. (t (call-interactively 'forward-word))))
  24913. (defun org-metaup (&optional arg)
  24914. "Move subtree up or move table row up.
  24915. Calls `org-move-subtree-up' or `org-table-move-row' or
  24916. `org-move-item-up', depending on context. See the individual commands
  24917. for more information."
  24918. (interactive "P")
  24919. (cond
  24920. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24921. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24922. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24923. (t (transpose-lines 1) (beginning-of-line -1))))
  24924. (defun org-metadown (&optional arg)
  24925. "Move subtree down or move table row down.
  24926. Calls `org-move-subtree-down' or `org-table-move-row' or
  24927. `org-move-item-down', depending on context. See the individual
  24928. commands for more information."
  24929. (interactive "P")
  24930. (cond
  24931. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24932. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24933. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24934. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24935. (defun org-shiftup (&optional arg)
  24936. "Increase item in timestamp or increase priority of current headline.
  24937. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24938. depending on context. See the individual commands for more information."
  24939. (interactive "P")
  24940. (cond
  24941. ((org-at-timestamp-p t)
  24942. (call-interactively (if org-edit-timestamp-down-means-later
  24943. 'org-timestamp-down 'org-timestamp-up)))
  24944. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24945. ((org-at-item-p) (call-interactively 'org-previous-item))
  24946. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24947. (defun org-shiftdown (&optional arg)
  24948. "Decrease item in timestamp or decrease priority of current headline.
  24949. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24950. depending on context. See the individual commands for more information."
  24951. (interactive "P")
  24952. (cond
  24953. ((org-at-timestamp-p t)
  24954. (call-interactively (if org-edit-timestamp-down-means-later
  24955. 'org-timestamp-up 'org-timestamp-down)))
  24956. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24957. (t (call-interactively 'org-next-item))))
  24958. (defun org-shiftright ()
  24959. "Next TODO keyword or timestamp one day later, depending on context."
  24960. (interactive)
  24961. (cond
  24962. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24963. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24964. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24965. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24966. (t (org-shiftcursor-error))))
  24967. (defun org-shiftleft ()
  24968. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24969. (interactive)
  24970. (cond
  24971. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24972. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24973. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24974. ((org-at-property-p)
  24975. (call-interactively 'org-property-previous-allowed-value))
  24976. (t (org-shiftcursor-error))))
  24977. (defun org-shiftcontrolright ()
  24978. "Switch to next TODO set."
  24979. (interactive)
  24980. (cond
  24981. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24982. (t (org-shiftcursor-error))))
  24983. (defun org-shiftcontrolleft ()
  24984. "Switch to previous TODO set."
  24985. (interactive)
  24986. (cond
  24987. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24988. (t (org-shiftcursor-error))))
  24989. (defun org-ctrl-c-ret ()
  24990. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24991. (interactive)
  24992. (cond
  24993. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24994. (t (call-interactively 'org-insert-heading))))
  24995. (defun org-copy-special ()
  24996. "Copy region in table or copy current subtree.
  24997. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24998. See the individual commands for more information."
  24999. (interactive)
  25000. (call-interactively
  25001. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25002. (defun org-cut-special ()
  25003. "Cut region in table or cut current subtree.
  25004. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25005. See the individual commands for more information."
  25006. (interactive)
  25007. (call-interactively
  25008. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25009. (defun org-paste-special (arg)
  25010. "Paste rectangular region into table, or past subtree relative to level.
  25011. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25012. See the individual commands for more information."
  25013. (interactive "P")
  25014. (if (org-at-table-p)
  25015. (org-table-paste-rectangle)
  25016. (org-paste-subtree arg)))
  25017. (defun org-ctrl-c-ctrl-c (&optional arg)
  25018. "Set tags in headline, or update according to changed information at point.
  25019. This command does many different things, depending on context:
  25020. - If the cursor is in a headline, prompt for tags and insert them
  25021. into the current line, aligned to `org-tags-column'. When called
  25022. with prefix arg, realign all tags in the current buffer.
  25023. - If the cursor is in one of the special #+KEYWORD lines, this
  25024. triggers scanning the buffer for these lines and updating the
  25025. information.
  25026. - If the cursor is inside a table, realign the table. This command
  25027. works even if the automatic table editor has been turned off.
  25028. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25029. the entire table.
  25030. - If the cursor is a the beginning of a dynamic block, update it.
  25031. - If the cursor is inside a table created by the table.el package,
  25032. activate that table.
  25033. - If the current buffer is a remember buffer, close note and file it.
  25034. with a prefix argument, file it without further interaction to the default
  25035. location.
  25036. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25037. links in this buffer.
  25038. - If the cursor is on a numbered item in a plain list, renumber the
  25039. ordered list.
  25040. - If the cursor is on a checkbox, toggle it."
  25041. (interactive "P")
  25042. (let ((org-enable-table-editor t))
  25043. (cond
  25044. ((or org-clock-overlays
  25045. org-occur-highlights
  25046. org-latex-fragment-image-overlays)
  25047. (org-remove-clock-overlays)
  25048. (org-remove-occur-highlights)
  25049. (org-remove-latex-fragment-image-overlays)
  25050. (message "Temporary highlights/overlays removed from current buffer"))
  25051. ((and (local-variable-p 'org-finish-function (current-buffer))
  25052. (fboundp org-finish-function))
  25053. (funcall org-finish-function))
  25054. ((org-at-property-p)
  25055. (call-interactively 'org-property-action))
  25056. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25057. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25058. ((org-at-table.el-p)
  25059. (require 'table)
  25060. (beginning-of-line 1)
  25061. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25062. (call-interactively 'table-recognize-table))
  25063. ((org-at-table-p)
  25064. (org-table-maybe-eval-formula)
  25065. (if arg
  25066. (call-interactively 'org-table-recalculate)
  25067. (org-table-maybe-recalculate-line))
  25068. (call-interactively 'org-table-align))
  25069. ((org-at-item-checkbox-p)
  25070. (call-interactively 'org-toggle-checkbox))
  25071. ((org-at-item-p)
  25072. (call-interactively 'org-maybe-renumber-ordered-list))
  25073. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25074. ;; Dynamic block
  25075. (beginning-of-line 1)
  25076. (org-update-dblock))
  25077. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25078. (cond
  25079. ((equal (match-string 1) "TBLFM")
  25080. ;; Recalculate the table before this line
  25081. (save-excursion
  25082. (beginning-of-line 1)
  25083. (skip-chars-backward " \r\n\t")
  25084. (if (org-at-table-p)
  25085. (org-call-with-arg 'org-table-recalculate t))))
  25086. (t
  25087. (call-interactively 'org-mode-restart))))
  25088. (t (error "C-c C-c can do nothing useful at this location.")))))
  25089. (defun org-mode-restart ()
  25090. "Restart Org-mode, to scan again for special lines.
  25091. Also updates the keyword regular expressions."
  25092. (interactive)
  25093. (let ((org-inhibit-startup t)) (org-mode))
  25094. (message "Org-mode restarted to refresh keyword and special line setup"))
  25095. (defun org-kill-note-or-show-branches ()
  25096. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25097. (interactive)
  25098. (if (not org-finish-function)
  25099. (call-interactively 'show-branches)
  25100. (let ((org-note-abort t))
  25101. (funcall org-finish-function))))
  25102. (defun org-return (&optional indent)
  25103. "Goto next table row or insert a newline.
  25104. Calls `org-table-next-row' or `newline', depending on context.
  25105. See the individual commands for more information."
  25106. (interactive)
  25107. (cond
  25108. ((bobp) (if indent (newline-and-indent) (newline)))
  25109. ((and (org-at-heading-p)
  25110. (looking-at
  25111. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25112. (org-show-entry)
  25113. (end-of-line 1)
  25114. (newline))
  25115. ((org-at-table-p)
  25116. (org-table-justify-field-maybe)
  25117. (call-interactively 'org-table-next-row))
  25118. (t (if indent (newline-and-indent) (newline)))))
  25119. (defun org-return-indent ()
  25120. (interactive)
  25121. "Goto next table row or insert a newline and indent.
  25122. Calls `org-table-next-row' or `newline-and-indent', depending on
  25123. context. See the individual commands for more information."
  25124. (org-return t))
  25125. (defun org-ctrl-c-minus ()
  25126. "Insert separator line in table or modify bullet type in list.
  25127. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25128. depending on context."
  25129. (interactive)
  25130. (cond
  25131. ((org-at-table-p)
  25132. (call-interactively 'org-table-insert-hline))
  25133. ((org-on-heading-p)
  25134. ;; Convert to item
  25135. (save-excursion
  25136. (beginning-of-line 1)
  25137. (if (looking-at "\\*+ ")
  25138. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25139. ((org-in-item-p)
  25140. (call-interactively 'org-cycle-list-bullet))
  25141. (t (error "`C-c -' does have no function here."))))
  25142. (defun org-meta-return (&optional arg)
  25143. "Insert a new heading or wrap a region in a table.
  25144. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25145. See the individual commands for more information."
  25146. (interactive "P")
  25147. (cond
  25148. ((org-at-table-p)
  25149. (call-interactively 'org-table-wrap-region))
  25150. (t (call-interactively 'org-insert-heading))))
  25151. ;;; Menu entries
  25152. ;; Define the Org-mode menus
  25153. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25154. '("Tbl"
  25155. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25156. ["Next Field" org-cycle (org-at-table-p)]
  25157. ["Previous Field" org-shifttab (org-at-table-p)]
  25158. ["Next Row" org-return (org-at-table-p)]
  25159. "--"
  25160. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25161. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25162. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25163. "--"
  25164. ("Column"
  25165. ["Move Column Left" org-metaleft (org-at-table-p)]
  25166. ["Move Column Right" org-metaright (org-at-table-p)]
  25167. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25168. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25169. ("Row"
  25170. ["Move Row Up" org-metaup (org-at-table-p)]
  25171. ["Move Row Down" org-metadown (org-at-table-p)]
  25172. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25173. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25174. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25175. "--"
  25176. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25177. ("Rectangle"
  25178. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25179. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25180. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25181. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25182. "--"
  25183. ("Calculate"
  25184. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25185. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25186. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25187. "--"
  25188. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25189. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25190. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25191. "--"
  25192. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25193. "--"
  25194. ["Sum Column/Rectangle" org-table-sum
  25195. (or (org-at-table-p) (org-region-active-p))]
  25196. ["Which Column?" org-table-current-column (org-at-table-p)])
  25197. ["Debug Formulas"
  25198. org-table-toggle-formula-debugger
  25199. :style toggle :selected org-table-formula-debug]
  25200. ["Show Col/Row Numbers"
  25201. org-table-toggle-coordinate-overlays
  25202. :style toggle :selected org-table-overlay-coordinates]
  25203. "--"
  25204. ["Create" org-table-create (and (not (org-at-table-p))
  25205. org-enable-table-editor)]
  25206. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25207. ["Import from File" org-table-import (not (org-at-table-p))]
  25208. ["Export to File" org-table-export (org-at-table-p)]
  25209. "--"
  25210. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25211. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25212. '("Org"
  25213. ("Show/Hide"
  25214. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25215. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25216. ["Sparse Tree" org-occur t]
  25217. ["Reveal Context" org-reveal t]
  25218. ["Show All" show-all t]
  25219. "--"
  25220. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25221. "--"
  25222. ["New Heading" org-insert-heading t]
  25223. ("Navigate Headings"
  25224. ["Up" outline-up-heading t]
  25225. ["Next" outline-next-visible-heading t]
  25226. ["Previous" outline-previous-visible-heading t]
  25227. ["Next Same Level" outline-forward-same-level t]
  25228. ["Previous Same Level" outline-backward-same-level t]
  25229. "--"
  25230. ["Jump" org-goto t])
  25231. ("Edit Structure"
  25232. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25233. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25234. "--"
  25235. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25236. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25237. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25238. "--"
  25239. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25240. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25241. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25242. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25243. "--"
  25244. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25245. "--"
  25246. ["Convert to odd levels" org-convert-to-odd-levels t]
  25247. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25248. ("Editing"
  25249. ["Emphasis..." org-emphasize t])
  25250. ("Archive"
  25251. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25252. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25253. ; :active t :keys "C-u C-c C-x C-a"]
  25254. ["Sparse trees open ARCHIVE trees"
  25255. (setq org-sparse-tree-open-archived-trees
  25256. (not org-sparse-tree-open-archived-trees))
  25257. :style toggle :selected org-sparse-tree-open-archived-trees]
  25258. ["Cycling opens ARCHIVE trees"
  25259. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25260. :style toggle :selected org-cycle-open-archived-trees]
  25261. ["Agenda includes ARCHIVE trees"
  25262. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25263. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25264. "--"
  25265. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25266. ; ["Check and Move Children" (org-archive-subtree '(4))
  25267. ; :active t :keys "C-u C-c C-x C-s"]
  25268. )
  25269. "--"
  25270. ("TODO Lists"
  25271. ["TODO/DONE/-" org-todo t]
  25272. ("Select keyword"
  25273. ["Next keyword" org-shiftright (org-on-heading-p)]
  25274. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25275. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25276. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25277. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25278. ["Show TODO Tree" org-show-todo-tree t]
  25279. ["Global TODO list" org-todo-list t]
  25280. "--"
  25281. ["Set Priority" org-priority t]
  25282. ["Priority Up" org-shiftup t]
  25283. ["Priority Down" org-shiftdown t])
  25284. ("TAGS and Properties"
  25285. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25286. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25287. "--"
  25288. ["Set property" 'org-set-property t]
  25289. ["Column view of properties" org-columns t]
  25290. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25291. ("Dates and Scheduling"
  25292. ["Timestamp" org-time-stamp t]
  25293. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25294. ("Change Date"
  25295. ["1 Day Later" org-shiftright t]
  25296. ["1 Day Earlier" org-shiftleft t]
  25297. ["1 ... Later" org-shiftup t]
  25298. ["1 ... Earlier" org-shiftdown t])
  25299. ["Compute Time Range" org-evaluate-time-range t]
  25300. ["Schedule Item" org-schedule t]
  25301. ["Deadline" org-deadline t]
  25302. "--"
  25303. ["Custom time format" org-toggle-time-stamp-overlays
  25304. :style radio :selected org-display-custom-times]
  25305. "--"
  25306. ["Goto Calendar" org-goto-calendar t]
  25307. ["Date from Calendar" org-date-from-calendar t])
  25308. ("Logging work"
  25309. ["Clock in" org-clock-in t]
  25310. ["Clock out" org-clock-out t]
  25311. ["Clock cancel" org-clock-cancel t]
  25312. ["Goto running clock" org-clock-goto t]
  25313. ["Display times" org-clock-display t]
  25314. ["Create clock table" org-clock-report t]
  25315. "--"
  25316. ["Record DONE time"
  25317. (progn (setq org-log-done (not org-log-done))
  25318. (message "Switching to %s will %s record a timestamp"
  25319. (car org-done-keywords)
  25320. (if org-log-done "automatically" "not")))
  25321. :style toggle :selected org-log-done])
  25322. "--"
  25323. ["Agenda Command..." org-agenda t]
  25324. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25325. ("File List for Agenda")
  25326. ("Special views current file"
  25327. ["TODO Tree" org-show-todo-tree t]
  25328. ["Check Deadlines" org-check-deadlines t]
  25329. ["Timeline" org-timeline t]
  25330. ["Tags Tree" org-tags-sparse-tree t])
  25331. "--"
  25332. ("Hyperlinks"
  25333. ["Store Link (Global)" org-store-link t]
  25334. ["Insert Link" org-insert-link t]
  25335. ["Follow Link" org-open-at-point t]
  25336. "--"
  25337. ["Next link" org-next-link t]
  25338. ["Previous link" org-previous-link t]
  25339. "--"
  25340. ["Descriptive Links"
  25341. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25342. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25343. ["Literal Links"
  25344. (progn
  25345. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25346. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25347. "--"
  25348. ["Export/Publish..." org-export t]
  25349. ("LaTeX"
  25350. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25351. :selected org-cdlatex-mode]
  25352. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25353. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25354. ["Modify math symbol" org-cdlatex-math-modify
  25355. (org-inside-LaTeX-fragment-p)]
  25356. ["Export LaTeX fragments as images"
  25357. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25358. :style toggle :selected org-export-with-LaTeX-fragments])
  25359. "--"
  25360. ("Documentation"
  25361. ["Show Version" org-version t]
  25362. ["Info Documentation" org-info t])
  25363. ("Customize"
  25364. ["Browse Org Group" org-customize t]
  25365. "--"
  25366. ["Expand This Menu" org-create-customize-menu
  25367. (fboundp 'customize-menu-create)])
  25368. "--"
  25369. ["Refresh setup" org-mode-restart t]
  25370. ))
  25371. (defun org-info (&optional node)
  25372. "Read documentation for Org-mode in the info system.
  25373. With optional NODE, go directly to that node."
  25374. (interactive)
  25375. (require 'info)
  25376. (info (format "(org)%s" (or node ""))))
  25377. (defun org-install-agenda-files-menu ()
  25378. (let ((bl (buffer-list)))
  25379. (save-excursion
  25380. (while bl
  25381. (set-buffer (pop bl))
  25382. (if (org-mode-p) (setq bl nil)))
  25383. (when (org-mode-p)
  25384. (easy-menu-change
  25385. '("Org") "File List for Agenda"
  25386. (append
  25387. (list
  25388. ["Edit File List" (org-edit-agenda-file-list) t]
  25389. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25390. ["Remove Current File from List" org-remove-file t]
  25391. ["Cycle through agenda files" org-cycle-agenda-files t]
  25392. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25393. "--")
  25394. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25395. ;;;; Documentation
  25396. (defun org-customize ()
  25397. "Call the customize function with org as argument."
  25398. (interactive)
  25399. (customize-browse 'org))
  25400. (defun org-create-customize-menu ()
  25401. "Create a full customization menu for Org-mode, insert it into the menu."
  25402. (interactive)
  25403. (if (fboundp 'customize-menu-create)
  25404. (progn
  25405. (easy-menu-change
  25406. '("Org") "Customize"
  25407. `(["Browse Org group" org-customize t]
  25408. "--"
  25409. ,(customize-menu-create 'org)
  25410. ["Set" Custom-set t]
  25411. ["Save" Custom-save t]
  25412. ["Reset to Current" Custom-reset-current t]
  25413. ["Reset to Saved" Custom-reset-saved t]
  25414. ["Reset to Standard Settings" Custom-reset-standard t]))
  25415. (message "\"Org\"-menu now contains full customization menu"))
  25416. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25417. ;;;; Miscellaneous stuff
  25418. ;;; Generally useful functions
  25419. (defun org-context ()
  25420. "Return a list of contexts of the current cursor position.
  25421. If several contexts apply, all are returned.
  25422. Each context entry is a list with a symbol naming the context, and
  25423. two positions indicating start and end of the context. Possible
  25424. contexts are:
  25425. :headline anywhere in a headline
  25426. :headline-stars on the leading stars in a headline
  25427. :todo-keyword on a TODO keyword (including DONE) in a headline
  25428. :tags on the TAGS in a headline
  25429. :priority on the priority cookie in a headline
  25430. :item on the first line of a plain list item
  25431. :item-bullet on the bullet/number of a plain list item
  25432. :checkbox on the checkbox in a plain list item
  25433. :table in an org-mode table
  25434. :table-special on a special filed in a table
  25435. :table-table in a table.el table
  25436. :link on a hyperlink
  25437. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25438. :target on a <<target>>
  25439. :radio-target on a <<<radio-target>>>
  25440. :latex-fragment on a LaTeX fragment
  25441. :latex-preview on a LaTeX fragment with overlayed preview image
  25442. This function expects the position to be visible because it uses font-lock
  25443. faces as a help to recognize the following contexts: :table-special, :link,
  25444. and :keyword."
  25445. (let* ((f (get-text-property (point) 'face))
  25446. (faces (if (listp f) f (list f)))
  25447. (p (point)) clist o)
  25448. ;; First the large context
  25449. (cond
  25450. ((org-on-heading-p t)
  25451. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25452. (when (progn
  25453. (beginning-of-line 1)
  25454. (looking-at org-todo-line-tags-regexp))
  25455. (push (org-point-in-group p 1 :headline-stars) clist)
  25456. (push (org-point-in-group p 2 :todo-keyword) clist)
  25457. (push (org-point-in-group p 4 :tags) clist))
  25458. (goto-char p)
  25459. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25460. (if (looking-at "\\[#[A-Z0-9]\\]")
  25461. (push (org-point-in-group p 0 :priority) clist)))
  25462. ((org-at-item-p)
  25463. (push (org-point-in-group p 2 :item-bullet) clist)
  25464. (push (list :item (point-at-bol)
  25465. (save-excursion (org-end-of-item) (point)))
  25466. clist)
  25467. (and (org-at-item-checkbox-p)
  25468. (push (org-point-in-group p 0 :checkbox) clist)))
  25469. ((org-at-table-p)
  25470. (push (list :table (org-table-begin) (org-table-end)) clist)
  25471. (if (memq 'org-formula faces)
  25472. (push (list :table-special
  25473. (previous-single-property-change p 'face)
  25474. (next-single-property-change p 'face)) clist)))
  25475. ((org-at-table-p 'any)
  25476. (push (list :table-table) clist)))
  25477. (goto-char p)
  25478. ;; Now the small context
  25479. (cond
  25480. ((org-at-timestamp-p)
  25481. (push (org-point-in-group p 0 :timestamp) clist))
  25482. ((memq 'org-link faces)
  25483. (push (list :link
  25484. (previous-single-property-change p 'face)
  25485. (next-single-property-change p 'face)) clist))
  25486. ((memq 'org-special-keyword faces)
  25487. (push (list :keyword
  25488. (previous-single-property-change p 'face)
  25489. (next-single-property-change p 'face)) clist))
  25490. ((org-on-target-p)
  25491. (push (org-point-in-group p 0 :target) clist)
  25492. (goto-char (1- (match-beginning 0)))
  25493. (if (looking-at org-radio-target-regexp)
  25494. (push (org-point-in-group p 0 :radio-target) clist))
  25495. (goto-char p))
  25496. ((setq o (car (delq nil
  25497. (mapcar
  25498. (lambda (x)
  25499. (if (memq x org-latex-fragment-image-overlays) x))
  25500. (org-overlays-at (point))))))
  25501. (push (list :latex-fragment
  25502. (org-overlay-start o) (org-overlay-end o)) clist)
  25503. (push (list :latex-preview
  25504. (org-overlay-start o) (org-overlay-end o)) clist))
  25505. ((org-inside-LaTeX-fragment-p)
  25506. ;; FIXME: positions wrong.
  25507. (push (list :latex-fragment (point) (point)) clist)))
  25508. (setq clist (nreverse (delq nil clist)))
  25509. clist))
  25510. ;; FIXME: Compare with at-regexp-p Do we need both?
  25511. (defun org-in-regexp (re &optional nlines visually)
  25512. "Check if point is inside a match of regexp.
  25513. Normally only the current line is checked, but you can include NLINES extra
  25514. lines both before and after point into the search.
  25515. If VISUALLY is set, require that the cursor is not after the match but
  25516. really on, so that the block visually is on the match."
  25517. (catch 'exit
  25518. (let ((pos (point))
  25519. (eol (point-at-eol (+ 1 (or nlines 0))))
  25520. (inc (if visually 1 0)))
  25521. (save-excursion
  25522. (beginning-of-line (- 1 (or nlines 0)))
  25523. (while (re-search-forward re eol t)
  25524. (if (and (<= (match-beginning 0) pos)
  25525. (>= (+ inc (match-end 0)) pos))
  25526. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25527. (defun org-at-regexp-p (regexp)
  25528. "Is point inside a match of REGEXP in the current line?"
  25529. (catch 'exit
  25530. (save-excursion
  25531. (let ((pos (point)) (end (point-at-eol)))
  25532. (beginning-of-line 1)
  25533. (while (re-search-forward regexp end t)
  25534. (if (and (<= (match-beginning 0) pos)
  25535. (>= (match-end 0) pos))
  25536. (throw 'exit t)))
  25537. nil))))
  25538. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25539. "Call `multi-occur' with buffers for all agenda files."
  25540. (interactive "sOrg-files matching: \np")
  25541. (let* ((files (org-agenda-files))
  25542. (tnames (mapcar 'file-truename files))
  25543. (extra org-agenda-multi-occur-extra-files)
  25544. f)
  25545. (while (setq f (pop extra))
  25546. (unless (member (file-truename f) tnames)
  25547. (add-to-list 'files f 'append)
  25548. (add-to-list 'tnames (file-truename f) 'append)))
  25549. (multi-occur
  25550. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25551. regexp)))
  25552. (if (boundp 'occur-mode-find-occurrence-hook)
  25553. ;; Emacs 23
  25554. (add-hook 'occur-mode-find-occurrence-hook
  25555. (lambda ()
  25556. (when (org-mode-p)
  25557. (org-reveal))))
  25558. ;; Emacs 22
  25559. (defadvice occur-mode-goto-occurrence
  25560. (after org-occur-reveal activate)
  25561. (and (org-mode-p) (org-reveal)))
  25562. (defadvice occur-mode-goto-occurrence-other-window
  25563. (after org-occur-reveal activate)
  25564. (and (org-mode-p) (org-reveal)))
  25565. (defadvice occur-mode-display-occurrence
  25566. (after org-occur-reveal activate)
  25567. (when (org-mode-p)
  25568. (let ((pos (occur-mode-find-occurrence)))
  25569. (with-current-buffer (marker-buffer pos)
  25570. (save-excursion
  25571. (goto-char pos)
  25572. (org-reveal)))))))
  25573. (defun org-uniquify (list)
  25574. "Remove duplicate elements from LIST."
  25575. (let (res)
  25576. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25577. res))
  25578. (defun org-delete-all (elts list)
  25579. "Remove all elements in ELTS from LIST."
  25580. (while elts
  25581. (setq list (delete (pop elts) list)))
  25582. list)
  25583. (defun org-back-over-empty-lines ()
  25584. "Move backwards over witespace, to the beginning of the first empty line.
  25585. Returns the number o empty lines passed."
  25586. (let ((pos (point)))
  25587. (skip-chars-backward " \t\n\r")
  25588. (beginning-of-line 2)
  25589. (goto-char (min (point) pos))
  25590. (count-lines (point) pos)))
  25591. (defun org-skip-whitespace ()
  25592. (skip-chars-forward " \t\n\r"))
  25593. (defun org-point-in-group (point group &optional context)
  25594. "Check if POINT is in match-group GROUP.
  25595. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25596. match. If the match group does ot exist or point is not inside it,
  25597. return nil."
  25598. (and (match-beginning group)
  25599. (>= point (match-beginning group))
  25600. (<= point (match-end group))
  25601. (if context
  25602. (list context (match-beginning group) (match-end group))
  25603. t)))
  25604. (defun org-switch-to-buffer-other-window (&rest args)
  25605. "Switch to buffer in a second window on the current frame.
  25606. In particular, do not allow pop-up frames."
  25607. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25608. special-display-function)
  25609. (apply 'switch-to-buffer-other-window args)))
  25610. (defun org-combine-plists (&rest plists)
  25611. "Create a single property list from all plists in PLISTS.
  25612. The process starts by copying the first list, and then setting properties
  25613. from the other lists. Settings in the last list are the most significant
  25614. ones and overrule settings in the other lists."
  25615. (let ((rtn (copy-sequence (pop plists)))
  25616. p v ls)
  25617. (while plists
  25618. (setq ls (pop plists))
  25619. (while ls
  25620. (setq p (pop ls) v (pop ls))
  25621. (setq rtn (plist-put rtn p v))))
  25622. rtn))
  25623. (defun org-move-line-down (arg)
  25624. "Move the current line down. With prefix argument, move it past ARG lines."
  25625. (interactive "p")
  25626. (let ((col (current-column))
  25627. beg end pos)
  25628. (beginning-of-line 1) (setq beg (point))
  25629. (beginning-of-line 2) (setq end (point))
  25630. (beginning-of-line (+ 1 arg))
  25631. (setq pos (move-marker (make-marker) (point)))
  25632. (insert (delete-and-extract-region beg end))
  25633. (goto-char pos)
  25634. (move-to-column col)))
  25635. (defun org-move-line-up (arg)
  25636. "Move the current line up. With prefix argument, move it past ARG lines."
  25637. (interactive "p")
  25638. (let ((col (current-column))
  25639. beg end pos)
  25640. (beginning-of-line 1) (setq beg (point))
  25641. (beginning-of-line 2) (setq end (point))
  25642. (beginning-of-line (- arg))
  25643. (setq pos (move-marker (make-marker) (point)))
  25644. (insert (delete-and-extract-region beg end))
  25645. (goto-char pos)
  25646. (move-to-column col)))
  25647. (defun org-replace-escapes (string table)
  25648. "Replace %-escapes in STRING with values in TABLE.
  25649. TABLE is an association list with keys like \"%a\" and string values.
  25650. The sequences in STRING may contain normal field width and padding information,
  25651. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25652. so values can contain further %-escapes if they are define later in TABLE."
  25653. (let ((case-fold-search nil)
  25654. e re rpl)
  25655. (while (setq e (pop table))
  25656. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25657. (while (string-match re string)
  25658. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25659. (cdr e)))
  25660. (setq string (replace-match rpl t t string))))
  25661. string))
  25662. (defun org-sublist (list start end)
  25663. "Return a section of LIST, from START to END.
  25664. Counting starts at 1."
  25665. (let (rtn (c start))
  25666. (setq list (nthcdr (1- start) list))
  25667. (while (and list (<= c end))
  25668. (push (pop list) rtn)
  25669. (setq c (1+ c)))
  25670. (nreverse rtn)))
  25671. (defun org-find-base-buffer-visiting (file)
  25672. "Like `find-buffer-visiting' but alway return the base buffer and
  25673. not an indirect buffer"
  25674. (let ((buf (find-buffer-visiting file)))
  25675. (if buf
  25676. (or (buffer-base-buffer buf) buf)
  25677. nil)))
  25678. (defun org-image-file-name-regexp ()
  25679. "Return regexp matching the file names of images."
  25680. (if (fboundp 'image-file-name-regexp)
  25681. (image-file-name-regexp)
  25682. (let ((image-file-name-extensions
  25683. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25684. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25685. (concat "\\."
  25686. (regexp-opt (nconc (mapcar 'upcase
  25687. image-file-name-extensions)
  25688. image-file-name-extensions)
  25689. t)
  25690. "\\'"))))
  25691. (defun org-file-image-p (file)
  25692. "Return non-nil if FILE is an image."
  25693. (save-match-data
  25694. (string-match (org-image-file-name-regexp) file)))
  25695. ;;; Paragraph filling stuff.
  25696. ;; We want this to be just right, so use the full arsenal.
  25697. (defun org-indent-line-function ()
  25698. "Indent line like previous, but further if previous was headline or item."
  25699. (interactive)
  25700. (let* ((pos (point))
  25701. (itemp (org-at-item-p))
  25702. column bpos bcol tpos tcol bullet btype bullet-type)
  25703. ;; Find the previous relevant line
  25704. (beginning-of-line 1)
  25705. (cond
  25706. ((looking-at "#") (setq column 0))
  25707. ((looking-at "\\*+ ") (setq column 0))
  25708. (t
  25709. (beginning-of-line 0)
  25710. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25711. (beginning-of-line 0))
  25712. (cond
  25713. ((looking-at "\\*+[ \t]+")
  25714. (goto-char (match-end 0))
  25715. (setq column (current-column)))
  25716. ((org-in-item-p)
  25717. (org-beginning-of-item)
  25718. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25719. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25720. (setq bpos (match-beginning 1) tpos (match-end 0)
  25721. bcol (progn (goto-char bpos) (current-column))
  25722. tcol (progn (goto-char tpos) (current-column))
  25723. bullet (match-string 1)
  25724. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25725. (if (not itemp)
  25726. (setq column tcol)
  25727. (goto-char pos)
  25728. (beginning-of-line 1)
  25729. (if (looking-at "\\S-")
  25730. (progn
  25731. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25732. (setq bullet (match-string 1)
  25733. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25734. (setq column (if (equal btype bullet-type) bcol tcol)))
  25735. (setq column (org-get-indentation)))))
  25736. (t (setq column (org-get-indentation))))))
  25737. (goto-char pos)
  25738. (if (<= (current-column) (current-indentation))
  25739. (indent-line-to column)
  25740. (save-excursion (indent-line-to column)))
  25741. (setq column (current-column))
  25742. (beginning-of-line 1)
  25743. (if (looking-at
  25744. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25745. (replace-match (concat "\\1" (format org-property-format
  25746. (match-string 2) (match-string 3)))
  25747. t nil))
  25748. (move-to-column column)))
  25749. (defun org-set-autofill-regexps ()
  25750. (interactive)
  25751. ;; In the paragraph separator we include headlines, because filling
  25752. ;; text in a line directly attached to a headline would otherwise
  25753. ;; fill the headline as well.
  25754. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25755. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25756. ;; The paragraph starter includes hand-formatted lists.
  25757. (org-set-local 'paragraph-start
  25758. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25759. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25760. ;; But only if the user has not turned off tables or fixed-width regions
  25761. (org-set-local
  25762. 'auto-fill-inhibit-regexp
  25763. (concat "\\*+ \\|#\\+"
  25764. "\\|[ \t]*" org-keyword-time-regexp
  25765. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25766. (concat
  25767. "\\|[ \t]*["
  25768. (if org-enable-table-editor "|" "")
  25769. (if org-enable-fixed-width-editor ":" "")
  25770. "]"))))
  25771. ;; We use our own fill-paragraph function, to make sure that tables
  25772. ;; and fixed-width regions are not wrapped. That function will pass
  25773. ;; through to `fill-paragraph' when appropriate.
  25774. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25775. ; Adaptive filling: To get full control, first make sure that
  25776. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25777. (org-set-local 'adaptive-fill-regexp "\000")
  25778. (org-set-local 'adaptive-fill-function
  25779. 'org-adaptive-fill-function)
  25780. (org-set-local
  25781. 'align-mode-rules-list
  25782. '((org-in-buffer-settings
  25783. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25784. (modes . '(org-mode))))))
  25785. (defun org-fill-paragraph (&optional justify)
  25786. "Re-align a table, pass through to fill-paragraph if no table."
  25787. (let ((table-p (org-at-table-p))
  25788. (table.el-p (org-at-table.el-p)))
  25789. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25790. (save-excursion (goto-char (point-at-bol))
  25791. (looking-at outline-regexp)))
  25792. t) ; skip headlines
  25793. (table.el-p t) ; skip table.el tables
  25794. (table-p (org-table-align) t) ; align org-mode tables
  25795. (t nil)))) ; call paragraph-fill
  25796. ;; For reference, this is the default value of adaptive-fill-regexp
  25797. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25798. (defun org-adaptive-fill-function ()
  25799. "Return a fill prefix for org-mode files.
  25800. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25801. work correctly."
  25802. (cond ((looking-at "#[ \t]+")
  25803. (match-string 0))
  25804. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25805. (save-excursion
  25806. (goto-char (match-end 0))
  25807. (make-string (current-column) ?\ )))
  25808. (t nil)))
  25809. ;;;; Functions extending outline functionality
  25810. (defun org-beginning-of-line (&optional arg)
  25811. "Go to the beginning of the current line. If that is invisible, continue
  25812. to a visible line beginning. This makes the function of C-a more intuitive.
  25813. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25814. first attempt, and only move to after the tags when the cursor is already
  25815. beyond the end of the headline."
  25816. (interactive "P")
  25817. (let ((pos (point)))
  25818. (beginning-of-line 1)
  25819. (if (bobp)
  25820. nil
  25821. (backward-char 1)
  25822. (if (org-invisible-p)
  25823. (while (and (not (bobp)) (org-invisible-p))
  25824. (backward-char 1)
  25825. (beginning-of-line 1))
  25826. (forward-char 1)))
  25827. (when org-special-ctrl-a/e
  25828. (cond
  25829. ((and (looking-at org-todo-line-regexp)
  25830. (= (char-after (match-end 1)) ?\ ))
  25831. (goto-char
  25832. (if (eq org-special-ctrl-a/e t)
  25833. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25834. ((= pos (point)) (match-beginning 3))
  25835. (t (point)))
  25836. (cond ((> pos (point)) (point))
  25837. ((not (eq last-command this-command)) (point))
  25838. (t (match-beginning 3))))))
  25839. ((org-at-item-p)
  25840. (goto-char
  25841. (if (eq org-special-ctrl-a/e t)
  25842. (cond ((> pos (match-end 4)) (match-end 4))
  25843. ((= pos (point)) (match-end 4))
  25844. (t (point)))
  25845. (cond ((> pos (point)) (point))
  25846. ((not (eq last-command this-command)) (point))
  25847. (t (match-end 4))))))))))
  25848. (defun org-end-of-line (&optional arg)
  25849. "Go to the end of the line.
  25850. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25851. first attempt, and only move to after the tags when the cursor is already
  25852. beyond the end of the headline."
  25853. (interactive "P")
  25854. (if (or (not org-special-ctrl-a/e)
  25855. (not (org-on-heading-p)))
  25856. (end-of-line arg)
  25857. (let ((pos (point)))
  25858. (beginning-of-line 1)
  25859. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25860. (if (eq org-special-ctrl-a/e t)
  25861. (if (or (< pos (match-beginning 1))
  25862. (= pos (match-end 0)))
  25863. (goto-char (match-beginning 1))
  25864. (goto-char (match-end 0)))
  25865. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25866. (goto-char (match-end 0))
  25867. (goto-char (match-beginning 1))))
  25868. (end-of-line arg)))))
  25869. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25870. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25871. (defun org-kill-line (&optional arg)
  25872. "Kill line, to tags or end of line."
  25873. (interactive "P")
  25874. (cond
  25875. ((or (not org-special-ctrl-k)
  25876. (bolp)
  25877. (not (org-on-heading-p)))
  25878. (call-interactively 'kill-line))
  25879. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25880. (kill-region (point) (match-beginning 1))
  25881. (org-set-tags nil t))
  25882. (t (kill-region (point) (point-at-eol)))))
  25883. (define-key org-mode-map "\C-k" 'org-kill-line)
  25884. (defun org-invisible-p ()
  25885. "Check if point is at a character currently not visible."
  25886. ;; Early versions of noutline don't have `outline-invisible-p'.
  25887. (if (fboundp 'outline-invisible-p)
  25888. (outline-invisible-p)
  25889. (get-char-property (point) 'invisible)))
  25890. (defun org-invisible-p2 ()
  25891. "Check if point is at a character currently not visible."
  25892. (save-excursion
  25893. (if (and (eolp) (not (bobp))) (backward-char 1))
  25894. ;; Early versions of noutline don't have `outline-invisible-p'.
  25895. (if (fboundp 'outline-invisible-p)
  25896. (outline-invisible-p)
  25897. (get-char-property (point) 'invisible))))
  25898. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25899. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25900. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25901. (defun org-at-heading-or-item-p ()
  25902. (or (org-on-heading-p) (org-at-item-p)))
  25903. (defun org-on-target-p ()
  25904. (or (org-in-regexp org-radio-target-regexp)
  25905. (org-in-regexp org-target-regexp)))
  25906. (defun org-up-heading-all (arg)
  25907. "Move to the heading line of which the present line is a subheading.
  25908. This function considers both visible and invisible heading lines.
  25909. With argument, move up ARG levels."
  25910. (if (fboundp 'outline-up-heading-all)
  25911. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25912. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25913. (defun org-up-heading-safe ()
  25914. "Move to the heading line of which the present line is a subheading.
  25915. This version will not throw an error. It will return the level of the
  25916. headline found, or nil if no higher level is found."
  25917. (let ((pos (point)) start-level level
  25918. (re (concat "^" outline-regexp)))
  25919. (catch 'exit
  25920. (outline-back-to-heading t)
  25921. (setq start-level (funcall outline-level))
  25922. (if (equal start-level 1) (throw 'exit nil))
  25923. (while (re-search-backward re nil t)
  25924. (setq level (funcall outline-level))
  25925. (if (< level start-level) (throw 'exit level)))
  25926. nil)))
  25927. (defun org-first-sibling-p ()
  25928. "Is this heading the first child of its parents?"
  25929. (interactive)
  25930. (let ((re (concat "^" outline-regexp))
  25931. level l)
  25932. (unless (org-at-heading-p t)
  25933. (error "Not at a heading"))
  25934. (setq level (funcall outline-level))
  25935. (save-excursion
  25936. (if (not (re-search-backward re nil t))
  25937. t
  25938. (setq l (funcall outline-level))
  25939. (< l level)))))
  25940. (defun org-goto-sibling (&optional previous)
  25941. "Goto the next sibling, even if it is invisible.
  25942. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25943. when a sibling was found. When none is found, return nil and don't
  25944. move point."
  25945. (let ((fun (if previous 're-search-backward 're-search-forward))
  25946. (pos (point))
  25947. (re (concat "^" outline-regexp))
  25948. level l)
  25949. (when (condition-case nil (org-back-to-heading t) (error nil))
  25950. (setq level (funcall outline-level))
  25951. (catch 'exit
  25952. (or previous (forward-char 1))
  25953. (while (funcall fun re nil t)
  25954. (setq l (funcall outline-level))
  25955. (when (< l level) (goto-char pos) (throw 'exit nil))
  25956. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25957. (goto-char pos)
  25958. nil))))
  25959. (defun org-show-siblings ()
  25960. "Show all siblings of the current headline."
  25961. (save-excursion
  25962. (while (org-goto-sibling) (org-flag-heading nil)))
  25963. (save-excursion
  25964. (while (org-goto-sibling 'previous)
  25965. (org-flag-heading nil))))
  25966. (defun org-show-hidden-entry ()
  25967. "Show an entry where even the heading is hidden."
  25968. (save-excursion
  25969. (org-show-entry)))
  25970. (defun org-flag-heading (flag &optional entry)
  25971. "Flag the current heading. FLAG non-nil means make invisible.
  25972. When ENTRY is non-nil, show the entire entry."
  25973. (save-excursion
  25974. (org-back-to-heading t)
  25975. ;; Check if we should show the entire entry
  25976. (if entry
  25977. (progn
  25978. (org-show-entry)
  25979. (save-excursion
  25980. (and (outline-next-heading)
  25981. (org-flag-heading nil))))
  25982. (outline-flag-region (max (point-min) (1- (point)))
  25983. (save-excursion (outline-end-of-heading) (point))
  25984. flag))))
  25985. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25986. ;; This is an exact copy of the original function, but it uses
  25987. ;; `org-back-to-heading', to make it work also in invisible
  25988. ;; trees. And is uses an invisible-OK argument.
  25989. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25990. (org-back-to-heading invisible-OK)
  25991. (let ((first t)
  25992. (level (funcall outline-level)))
  25993. (while (and (not (eobp))
  25994. (or first (> (funcall outline-level) level)))
  25995. (setq first nil)
  25996. (outline-next-heading))
  25997. (unless to-heading
  25998. (if (memq (preceding-char) '(?\n ?\^M))
  25999. (progn
  26000. ;; Go to end of line before heading
  26001. (forward-char -1)
  26002. (if (memq (preceding-char) '(?\n ?\^M))
  26003. ;; leave blank line before heading
  26004. (forward-char -1))))))
  26005. (point))
  26006. (defun org-show-subtree ()
  26007. "Show everything after this heading at deeper levels."
  26008. (outline-flag-region
  26009. (point)
  26010. (save-excursion
  26011. (outline-end-of-subtree) (outline-next-heading) (point))
  26012. nil))
  26013. (defun org-show-entry ()
  26014. "Show the body directly following this heading.
  26015. Show the heading too, if it is currently invisible."
  26016. (interactive)
  26017. (save-excursion
  26018. (condition-case nil
  26019. (progn
  26020. (org-back-to-heading t)
  26021. (outline-flag-region
  26022. (max (point-min) (1- (point)))
  26023. (save-excursion
  26024. (re-search-forward
  26025. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26026. (or (match-beginning 1) (point-max)))
  26027. nil))
  26028. (error nil))))
  26029. (defun org-make-options-regexp (kwds)
  26030. "Make a regular expression for keyword lines."
  26031. (concat
  26032. "^"
  26033. "#?[ \t]*\\+\\("
  26034. (mapconcat 'regexp-quote kwds "\\|")
  26035. "\\):[ \t]*"
  26036. "\\(.+\\)"))
  26037. ;; Make isearch reveal the necessary context
  26038. (defun org-isearch-end ()
  26039. "Reveal context after isearch exits."
  26040. (when isearch-success ; only if search was successful
  26041. (if (featurep 'xemacs)
  26042. ;; Under XEmacs, the hook is run in the correct place,
  26043. ;; we directly show the context.
  26044. (org-show-context 'isearch)
  26045. ;; In Emacs the hook runs *before* restoring the overlays.
  26046. ;; So we have to use a one-time post-command-hook to do this.
  26047. ;; (Emacs 22 has a special variable, see function `org-mode')
  26048. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26049. isearch-mode-end-hook-quit)
  26050. ;; Only when the isearch was not quitted.
  26051. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26052. 'append 'local)))))
  26053. (defun org-isearch-post-command ()
  26054. "Remove self from hook, and show context."
  26055. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26056. (org-show-context 'isearch))
  26057. ;;;; Integration with and fixes for other packages
  26058. ;;; Imenu support
  26059. (defvar org-imenu-markers nil
  26060. "All markers currently used by Imenu.")
  26061. (make-variable-buffer-local 'org-imenu-markers)
  26062. (defun org-imenu-new-marker (&optional pos)
  26063. "Return a new marker for use by Imenu, and remember the marker."
  26064. (let ((m (make-marker)))
  26065. (move-marker m (or pos (point)))
  26066. (push m org-imenu-markers)
  26067. m))
  26068. (defun org-imenu-get-tree ()
  26069. "Produce the index for Imenu."
  26070. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26071. (setq org-imenu-markers nil)
  26072. (let* ((n org-imenu-depth)
  26073. (re (concat "^" outline-regexp))
  26074. (subs (make-vector (1+ n) nil))
  26075. (last-level 0)
  26076. m tree level head)
  26077. (save-excursion
  26078. (save-restriction
  26079. (widen)
  26080. (goto-char (point-max))
  26081. (while (re-search-backward re nil t)
  26082. (setq level (org-reduced-level (funcall outline-level)))
  26083. (when (<= level n)
  26084. (looking-at org-complex-heading-regexp)
  26085. (setq head (org-match-string-no-properties 4)
  26086. m (org-imenu-new-marker))
  26087. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26088. (if (>= level last-level)
  26089. (push (cons head m) (aref subs level))
  26090. (push (cons head (aref subs (1+ level))) (aref subs level))
  26091. (loop for i from (1+ level) to n do (aset subs i nil)))
  26092. (setq last-level level)))))
  26093. (aref subs 1)))
  26094. (eval-after-load "imenu"
  26095. '(progn
  26096. (add-hook 'imenu-after-jump-hook
  26097. (lambda () (org-show-context 'org-goto)))))
  26098. ;; Speedbar support
  26099. (defun org-speedbar-set-agenda-restriction ()
  26100. "Restrict future agenda commands to the location at point in speedbar.
  26101. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26102. (interactive)
  26103. (let (p m tp np dir txt w)
  26104. (cond
  26105. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26106. 'org-imenu t))
  26107. (setq m (get-text-property p 'org-imenu-marker))
  26108. (save-excursion
  26109. (save-restriction
  26110. (set-buffer (marker-buffer m))
  26111. (goto-char m)
  26112. (org-agenda-set-restriction-lock 'subtree))))
  26113. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26114. 'speedbar-function 'speedbar-find-file))
  26115. (setq tp (previous-single-property-change
  26116. (1+ p) 'speedbar-function)
  26117. np (next-single-property-change
  26118. tp 'speedbar-function)
  26119. dir (speedbar-line-directory)
  26120. txt (buffer-substring-no-properties (or tp (point-min))
  26121. (or np (point-max))))
  26122. (save-excursion
  26123. (save-restriction
  26124. (set-buffer (find-file-noselect
  26125. (let ((default-directory dir))
  26126. (expand-file-name txt))))
  26127. (unless (org-mode-p)
  26128. (error "Cannot restrict to non-Org-mode file"))
  26129. (org-agenda-set-restriction-lock 'file))))
  26130. (t (error "Don't know how to restrict Org-mode's agenda")))
  26131. (org-move-overlay org-speedbar-restriction-lock-overlay
  26132. (point-at-bol) (point-at-eol))
  26133. (setq current-prefix-arg nil)
  26134. (org-agenda-maybe-redo)))
  26135. (eval-after-load "speedbar"
  26136. '(progn
  26137. (speedbar-add-supported-extension ".org")
  26138. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26139. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26140. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26141. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26142. (add-hook 'speedbar-visiting-tag-hook
  26143. (lambda () (org-show-context 'org-goto)))))
  26144. ;;; Fixes and Hacks
  26145. ;; Make flyspell not check words in links, to not mess up our keymap
  26146. (defun org-mode-flyspell-verify ()
  26147. "Don't let flyspell put overlays at active buttons."
  26148. (not (get-text-property (point) 'keymap)))
  26149. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26150. (eval-after-load "bookmark"
  26151. '(if (boundp 'bookmark-after-jump-hook)
  26152. ;; We can use the hook
  26153. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26154. ;; Hook not available, use advice
  26155. (defadvice bookmark-jump (after org-make-visible activate)
  26156. "Make the position visible."
  26157. (org-bookmark-jump-unhide))))
  26158. (defun org-bookmark-jump-unhide ()
  26159. "Unhide the current position, to show the bookmark location."
  26160. (and (org-mode-p)
  26161. (or (org-invisible-p)
  26162. (save-excursion (goto-char (max (point-min) (1- (point))))
  26163. (org-invisible-p)))
  26164. (org-show-context 'bookmark-jump)))
  26165. ;; Fix a bug in htmlize where there are text properties (face nil)
  26166. (eval-after-load "htmlize"
  26167. '(progn
  26168. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26169. "Make sure there are no nil faces"
  26170. (setq ad-return-value (delq nil ad-return-value)))))
  26171. ;; Make session.el ignore our circular variable
  26172. (eval-after-load "session"
  26173. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26174. ;;;; Experimental code
  26175. (defun org-closed-in-range ()
  26176. "Sparse tree of items closed in a certain time range.
  26177. Still experimental, may disappear in the future."
  26178. (interactive)
  26179. ;; Get the time interval from the user.
  26180. (let* ((time1 (time-to-seconds
  26181. (org-read-date nil 'to-time nil "Starting date: ")))
  26182. (time2 (time-to-seconds
  26183. (org-read-date nil 'to-time nil "End date:")))
  26184. ;; callback function
  26185. (callback (lambda ()
  26186. (let ((time
  26187. (time-to-seconds
  26188. (apply 'encode-time
  26189. (org-parse-time-string
  26190. (match-string 1))))))
  26191. ;; check if time in interval
  26192. (and (>= time time1) (<= time time2))))))
  26193. ;; make tree, check each match with the callback
  26194. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26195. ;;;; Finish up
  26196. (provide 'org)
  26197. (run-hooks 'org-load-hook)
  26198. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26199. ;;; org.el ends here