org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870
  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 nil
  138. "Hook that is run after org.el has been loaded."
  139. :group 'org
  140. :type 'hook)
  141. (defcustom org-default-extensions '(org-irc)
  142. "Extensions that should always be loaded together with org.el.
  143. If the description starts with <A>, this means the extension
  144. will be autoloaded when needed, preloading is not necessary."
  145. :group 'org
  146. :type
  147. '(set :greedy t
  148. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  149. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  150. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  151. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  152. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  153. (defun org-load-default-extensions ()
  154. "Load all extensions listed in `org-default-extensions'."
  155. (mapc (lambda (ext)
  156. (condition-case nil (require ext)
  157. (error (message "Problems while trying to load feature `%s'" ext))))
  158. org-default-extensions))
  159. (eval-after-load "org" '(org-load-default-extensions))
  160. ;; FIXME: Needs a separate group...
  161. (defcustom org-completion-fallback-command 'hippie-expand
  162. "The expansion command called by \\[org-complete] in normal context.
  163. Normal means, no org-mode-specific context."
  164. :group 'org
  165. :type 'function)
  166. (defgroup org-startup nil
  167. "Options concerning startup of Org-mode."
  168. :tag "Org Startup"
  169. :group 'org)
  170. (defcustom org-startup-folded t
  171. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  172. This can also be configured on a per-file basis by adding one of
  173. the following lines anywhere in the buffer:
  174. #+STARTUP: fold
  175. #+STARTUP: nofold
  176. #+STARTUP: content"
  177. :group 'org-startup
  178. :type '(choice
  179. (const :tag "nofold: show all" nil)
  180. (const :tag "fold: overview" t)
  181. (const :tag "content: all headlines" content)))
  182. (defcustom org-startup-truncated t
  183. "Non-nil means, entering Org-mode will set `truncate-lines'.
  184. This is useful since some lines containing links can be very long and
  185. uninteresting. Also tables look terrible when wrapped."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (defcustom org-startup-align-all-tables nil
  189. "Non-nil means, align all tables when visiting a file.
  190. This is useful when the column width in tables is forced with <N> cookies
  191. in table fields. Such tables will look correct only after the first re-align.
  192. This can also be configured on a per-file basis by adding one of
  193. the following lines anywhere in the buffer:
  194. #+STARTUP: align
  195. #+STARTUP: noalign"
  196. :group 'org-startup
  197. :type 'boolean)
  198. (defcustom org-insert-mode-line-in-empty-file nil
  199. "Non-nil means insert the first line setting Org-mode in empty files.
  200. When the function `org-mode' is called interactively in an empty file, this
  201. normally means that the file name does not automatically trigger Org-mode.
  202. To ensure that the file will always be in Org-mode in the future, a
  203. line enforcing Org-mode will be inserted into the buffer, if this option
  204. has been set."
  205. :group 'org-startup
  206. :type 'boolean)
  207. (defcustom org-replace-disputed-keys nil
  208. "Non-nil means use alternative key bindings for some keys.
  209. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  210. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  211. If you want to use Org-mode together with one of these other modes,
  212. or more generally if you would like to move some Org-mode commands to
  213. other keys, set this variable and configure the keys with the variable
  214. `org-disputed-keys'.
  215. This option is only relevant at load-time of Org-mode, and must be set
  216. *before* org.el is loaded. Changing it requires a restart of Emacs to
  217. become effective."
  218. :group 'org-startup
  219. :type 'boolean)
  220. (if (fboundp 'defvaralias)
  221. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  222. (defcustom org-disputed-keys
  223. '(([(shift up)] . [(meta p)])
  224. ([(shift down)] . [(meta n)])
  225. ([(shift left)] . [(meta -)])
  226. ([(shift right)] . [(meta +)])
  227. ([(control shift right)] . [(meta shift +)])
  228. ([(control shift left)] . [(meta shift -)]))
  229. "Keys for which Org-mode and other modes compete.
  230. This is an alist, cars are the default keys, second element specifies
  231. the alternative to use when `org-replace-disputed-keys' is t.
  232. Keys can be specified in any syntax supported by `define-key'.
  233. The value of this option takes effect only at Org-mode's startup,
  234. therefore you'll have to restart Emacs to apply it after changing."
  235. :group 'org-startup
  236. :type 'alist)
  237. (defun org-key (key)
  238. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  239. Or return the original if not disputed."
  240. (if org-replace-disputed-keys
  241. (let* ((nkey (key-description key))
  242. (x (org-find-if (lambda (x)
  243. (equal (key-description (car x)) nkey))
  244. org-disputed-keys)))
  245. (if x (cdr x) key))
  246. key))
  247. (defun org-find-if (predicate seq)
  248. (catch 'exit
  249. (while seq
  250. (if (funcall predicate (car seq))
  251. (throw 'exit (car seq))
  252. (pop seq)))))
  253. (defun org-defkey (keymap key def)
  254. "Define a key, possibly translated, as returned by `org-key'."
  255. (define-key keymap (org-key key) def))
  256. (defcustom org-ellipsis nil
  257. "The ellipsis to use in the Org-mode outline.
  258. When nil, just use the standard three dots. When a string, use that instead,
  259. When a face, use the standart 3 dots, but with the specified face.
  260. The change affects only Org-mode (which will then use its own display table).
  261. Changing this requires executing `M-x org-mode' in a buffer to become
  262. effective."
  263. :group 'org-startup
  264. :type '(choice (const :tag "Default" nil)
  265. (face :tag "Face" :value org-warning)
  266. (string :tag "String" :value "...#")))
  267. (defvar org-display-table nil
  268. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  269. (defgroup org-keywords nil
  270. "Keywords in Org-mode."
  271. :tag "Org Keywords"
  272. :group 'org)
  273. (defcustom org-deadline-string "DEADLINE:"
  274. "String to mark deadline entries.
  275. A deadline is this string, followed by a time stamp. Should be a word,
  276. terminated by a colon. You can insert a schedule keyword and
  277. a timestamp with \\[org-deadline].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-scheduled-string "SCHEDULED:"
  282. "String to mark scheduled TODO entries.
  283. A schedule is this string, followed by a time stamp. Should be a word,
  284. terminated by a colon. You can insert a schedule keyword and
  285. a timestamp with \\[org-schedule].
  286. Changes become only effective after restarting Emacs."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defcustom org-closed-string "CLOSED:"
  290. "String used as the prefix for timestamps logging closing a TODO entry."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-clock-string "CLOCK:"
  294. "String used as prefix for timestamps clocking work hours on an item."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-comment-string "COMMENT"
  298. "Entries starting with this keyword will never be exported.
  299. An entry can be toggled between COMMENT and normal with
  300. \\[org-toggle-comment].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-quote-string "QUOTE"
  305. "Entries starting with this keyword will be exported in fixed-width font.
  306. Quoting applies only to the text in the entry following the headline, and does
  307. not extend beyond the next headline, even if that is lower level.
  308. An entry can be toggled between QUOTE and normal with
  309. \\[org-toggle-fixed-width-section]."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defconst org-repeat-re
  313. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  314. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  315. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  316. "Regular expression for specifying repeated events.
  317. After a match, group 1 contains the repeat expression.")
  318. (defgroup org-structure nil
  319. "Options concerning the general structure of Org-mode files."
  320. :tag "Org Structure"
  321. :group 'org)
  322. (defgroup org-reveal-location nil
  323. "Options about how to make context of a location visible."
  324. :tag "Org Reveal Location"
  325. :group 'org-structure)
  326. (defconst org-context-choice
  327. '(choice
  328. (const :tag "Always" t)
  329. (const :tag "Never" nil)
  330. (repeat :greedy t :tag "Individual contexts"
  331. (cons
  332. (choice :tag "Context"
  333. (const agenda)
  334. (const org-goto)
  335. (const occur-tree)
  336. (const tags-tree)
  337. (const link-search)
  338. (const mark-goto)
  339. (const bookmark-jump)
  340. (const isearch)
  341. (const default))
  342. (boolean))))
  343. "Contexts for the reveal options.")
  344. (defcustom org-show-hierarchy-above '((default . t))
  345. "Non-nil means, show full hierarchy when revealing a location.
  346. Org-mode often shows locations in an org-mode file which might have
  347. been invisible before. When this is set, the hierarchy of headings
  348. above the exposed location is shown.
  349. Turning this off for example for sparse trees makes them very compact.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. Valid contexts are
  352. agenda when exposing an entry from the agenda
  353. org-goto when using the command `org-goto' on key C-c C-j
  354. occur-tree when using the command `org-occur' on key C-c /
  355. tags-tree when constructing a sparse tree based on tags matches
  356. link-search when exposing search matches associated with a link
  357. mark-goto when exposing the jump goal of a mark
  358. bookmark-jump when exposing a bookmark location
  359. isearch when exiting from an incremental search
  360. default default for all contexts not set explicitly"
  361. :group 'org-reveal-location
  362. :type org-context-choice)
  363. (defcustom org-show-following-heading '((default . nil))
  364. "Non-nil means, show following heading when revealing a location.
  365. Org-mode often shows locations in an org-mode file which might have
  366. been invisible before. When this is set, the heading following the
  367. match is shown.
  368. Turning this off for example for sparse trees makes them very compact,
  369. but makes it harder to edit the location of the match. In such a case,
  370. use the command \\[org-reveal] to show more context.
  371. Instead of t, this can also be an alist specifying this option for different
  372. contexts. See `org-show-hierarchy-above' for valid contexts."
  373. :group 'org-reveal-location
  374. :type org-context-choice)
  375. (defcustom org-show-siblings '((default . nil) (isearch t))
  376. "Non-nil means, show all sibling heading when revealing a location.
  377. Org-mode often shows locations in an org-mode file which might have
  378. been invisible before. When this is set, the sibling of the current entry
  379. heading are all made visible. If `org-show-hierarchy-above' is t,
  380. the same happens on each level of the hierarchy above the current entry.
  381. By default this is on for the isearch context, off for all other contexts.
  382. Turning this off for example for sparse trees makes them very compact,
  383. but makes it harder to edit the location of the match. In such a case,
  384. use the command \\[org-reveal] to show more context.
  385. Instead of t, this can also be an alist specifying this option for different
  386. contexts. See `org-show-hierarchy-above' for valid contexts."
  387. :group 'org-reveal-location
  388. :type org-context-choice)
  389. (defcustom org-show-entry-below '((default . nil))
  390. "Non-nil means, show the entry below a headline when revealing a location.
  391. Org-mode often shows locations in an org-mode file which might have
  392. been invisible before. When this is set, the text below the headline that is
  393. exposed is also shown.
  394. By default this is off for all contexts.
  395. Instead of t, this can also be an alist specifying this option for different
  396. contexts. See `org-show-hierarchy-above' for valid contexts."
  397. :group 'org-reveal-location
  398. :type org-context-choice)
  399. (defgroup org-cycle nil
  400. "Options concerning visibility cycling in Org-mode."
  401. :tag "Org Cycle"
  402. :group 'org-structure)
  403. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  404. "Names of drawers. Drawers are not opened by cycling on the headline above.
  405. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  406. this:
  407. :DRAWERNAME:
  408. .....
  409. :END:
  410. The drawer \"PROPERTIES\" is special for capturing properties through
  411. the property API.
  412. Drawers can be defined on the per-file basis with a line like:
  413. #+DRAWERS: HIDDEN STATE PROPERTIES"
  414. :group 'org-structure
  415. :type '(repeat (string :tag "Drawer Name")))
  416. (defcustom org-cycle-global-at-bob nil
  417. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  418. This makes it possible to do global cycling without having to use S-TAB or
  419. C-u TAB. For this special case to work, the first line of the buffer
  420. must not be a headline - it may be empty ot some other text. When used in
  421. this way, `org-cycle-hook' is disables temporarily, to make sure the
  422. cursor stays at the beginning of the buffer.
  423. When this option is nil, don't do anything special at the beginning
  424. of the buffer."
  425. :group 'org-cycle
  426. :type 'boolean)
  427. (defcustom org-cycle-emulate-tab t
  428. "Where should `org-cycle' emulate TAB.
  429. nil Never
  430. white Only in completely white lines
  431. whitestart Only at the beginning of lines, before the first non-white char
  432. t Everywhere except in headlines
  433. exc-hl-bol Everywhere except at the start of a headline
  434. If TAB is used in a place where it does not emulate TAB, the current subtree
  435. visibility is cycled."
  436. :group 'org-cycle
  437. :type '(choice (const :tag "Never" nil)
  438. (const :tag "Only in completely white lines" white)
  439. (const :tag "Before first char in a line" whitestart)
  440. (const :tag "Everywhere except in headlines" t)
  441. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  442. ))
  443. (defcustom org-cycle-separator-lines 2
  444. "Number of empty lines needed to keep an empty line between collapsed trees.
  445. If you leave an empty line between the end of a subtree and the following
  446. headline, this empty line is hidden when the subtree is folded.
  447. Org-mode will leave (exactly) one empty line visible if the number of
  448. empty lines is equal or larger to the number given in this variable.
  449. So the default 2 means, at least 2 empty lines after the end of a subtree
  450. are needed to produce free space between a collapsed subtree and the
  451. following headline.
  452. Special case: when 0, never leave empty lines in collapsed view."
  453. :group 'org-cycle
  454. :type 'integer)
  455. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  456. org-cycle-hide-drawers
  457. org-cycle-show-empty-lines
  458. org-optimize-window-after-visibility-change)
  459. "Hook that is run after `org-cycle' has changed the buffer visibility.
  460. The function(s) in this hook must accept a single argument which indicates
  461. the new state that was set by the most recent `org-cycle' command. The
  462. argument is a symbol. After a global state change, it can have the values
  463. `overview', `content', or `all'. After a local state change, it can have
  464. the values `folded', `children', or `subtree'."
  465. :group 'org-cycle
  466. :type 'hook)
  467. (defgroup org-edit-structure nil
  468. "Options concerning structure editing in Org-mode."
  469. :tag "Org Edit Structure"
  470. :group 'org-structure)
  471. (defcustom org-odd-levels-only nil
  472. "Non-nil means, skip even levels and only use odd levels for the outline.
  473. This has the effect that two stars are being added/taken away in
  474. promotion/demotion commands. It also influences how levels are
  475. handled by the exporters.
  476. Changing it requires restart of `font-lock-mode' to become effective
  477. for fontification also in regions already fontified.
  478. You may also set this on a per-file basis by adding one of the following
  479. lines to the buffer:
  480. #+STARTUP: odd
  481. #+STARTUP: oddeven"
  482. :group 'org-edit-structure
  483. :group 'org-font-lock
  484. :type 'boolean)
  485. (defcustom org-adapt-indentation t
  486. "Non-nil means, adapt indentation when promoting and demoting.
  487. When this is set and the *entire* text in an entry is indented, the
  488. indentation is increased by one space in a demotion command, and
  489. decreased by one in a promotion command. If any line in the entry
  490. body starts at column 0, indentation is not changed at all."
  491. :group 'org-edit-structure
  492. :type 'boolean)
  493. (defcustom org-special-ctrl-a/e nil
  494. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  495. When t, `C-a' will bring back the cursor to the beginning of the
  496. headline text, i.e. after the stars and after a possible TODO keyword.
  497. In an item, this will be the position after the bullet.
  498. When the cursor is already at that position, another `C-a' will bring
  499. it to the beginning of the line.
  500. `C-e' will jump to the end of the headline, ignoring the presence of tags
  501. in the headline. A second `C-e' will then jump to the true end of the
  502. line, after any tags.
  503. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  504. and only a directly following, identical keypress will bring the cursor
  505. to the special positions."
  506. :group 'org-edit-structure
  507. :type '(choice
  508. (const :tag "off" nil)
  509. (const :tag "after bullet first" t)
  510. (const :tag "border first" reversed)))
  511. (if (fboundp 'defvaralias)
  512. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  513. (defcustom org-special-ctrl-k nil
  514. "Non-nil means `C-k' will behave specially in headlines.
  515. When nil, `C-k' will call the default `kill-line' command.
  516. When t, the following will happen while the cursor is in the headline:
  517. - When the cursor is at the beginning of a headline, kill the entire
  518. line and possible the folded subtree below the line.
  519. - When in the middle of the headline text, kill the headline up to the tags.
  520. - When after the headline text, kill the tags."
  521. :group 'org-edit-structure
  522. :type 'boolean)
  523. (defcustom org-M-RET-may-split-line '((default . t))
  524. "Non-nil means, M-RET will split the line at the cursor position.
  525. When nil, it will go to the end of the line before making a
  526. new line.
  527. You may also set this option in a different way for different
  528. contexts. Valid contexts are:
  529. headline when creating a new headline
  530. item when creating a new item
  531. table in a table field
  532. default the value to be used for all contexts not explicitly
  533. customized"
  534. :group 'org-structure
  535. :group 'org-table
  536. :type '(choice
  537. (const :tag "Always" t)
  538. (const :tag "Never" nil)
  539. (repeat :greedy t :tag "Individual contexts"
  540. (cons
  541. (choice :tag "Context"
  542. (const headline)
  543. (const item)
  544. (const table)
  545. (const default))
  546. (boolean)))))
  547. (defcustom org-blank-before-new-entry '((heading . nil)
  548. (plain-list-item . nil))
  549. "Should `org-insert-heading' leave a blank line before new heading/item?
  550. The value is an alist, with `heading' and `plain-list-item' as car,
  551. and a boolean flag as cdr."
  552. :group 'org-edit-structure
  553. :type '(list
  554. (cons (const heading) (boolean))
  555. (cons (const plain-list-item) (boolean))))
  556. (defcustom org-insert-heading-hook nil
  557. "Hook being run after inserting a new heading."
  558. :group 'org-edit-structure
  559. :type 'hook)
  560. (defcustom org-enable-fixed-width-editor t
  561. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  562. This currently only means, they are never auto-wrapped.
  563. When nil, such lines will be treated like ordinary lines.
  564. See also the QUOTE keyword."
  565. :group 'org-edit-structure
  566. :type 'boolean)
  567. (defcustom org-goto-auto-isearch t
  568. "Non-nil means, typing characters in org-goto starts incremental search."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defgroup org-sparse-trees nil
  572. "Options concerning sparse trees in Org-mode."
  573. :tag "Org Sparse Trees"
  574. :group 'org-structure)
  575. (defcustom org-highlight-sparse-tree-matches t
  576. "Non-nil means, highlight all matches that define a sparse tree.
  577. The highlights will automatically disappear the next time the buffer is
  578. changed by an edit command."
  579. :group 'org-sparse-trees
  580. :type 'boolean)
  581. (defcustom org-remove-highlights-with-change t
  582. "Non-nil means, any change to the buffer will remove temporary highlights.
  583. Such highlights are created by `org-occur' and `org-clock-display'.
  584. When nil, `C-c C-c needs to be used to get rid of the highlights.
  585. The highlights created by `org-preview-latex-fragment' always need
  586. `C-c C-c' to be removed."
  587. :group 'org-sparse-trees
  588. :group 'org-time
  589. :type 'boolean)
  590. (defcustom org-occur-hook '(org-first-headline-recenter)
  591. "Hook that is run after `org-occur' has constructed a sparse tree.
  592. This can be used to recenter the window to show as much of the structure
  593. as possible."
  594. :group 'org-sparse-trees
  595. :type 'hook)
  596. (defgroup org-plain-lists nil
  597. "Options concerning plain lists in Org-mode."
  598. :tag "Org Plain lists"
  599. :group 'org-structure)
  600. (defcustom org-cycle-include-plain-lists nil
  601. "Non-nil means, include plain lists into visibility cycling.
  602. This means that during cycling, plain list items will *temporarily* be
  603. interpreted as outline headlines with a level given by 1000+i where i is the
  604. indentation of the bullet. In all other operations, plain list items are
  605. not seen as headlines. For example, you cannot assign a TODO keyword to
  606. such an item."
  607. :group 'org-plain-lists
  608. :type 'boolean)
  609. (defcustom org-plain-list-ordered-item-terminator t
  610. "The character that makes a line with leading number an ordered list item.
  611. Valid values are ?. and ?\). To get both terminators, use t. While
  612. ?. may look nicer, it creates the danger that a line with leading
  613. number may be incorrectly interpreted as an item. ?\) therefore is
  614. the safe choice."
  615. :group 'org-plain-lists
  616. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  617. (const :tag "paren like in \"2)\"" ?\))
  618. (const :tab "both" t)))
  619. (defcustom org-auto-renumber-ordered-lists t
  620. "Non-nil means, automatically renumber ordered plain lists.
  621. Renumbering happens when the sequence have been changed with
  622. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  623. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  624. :group 'org-plain-lists
  625. :type 'boolean)
  626. (defcustom org-provide-checkbox-statistics t
  627. "Non-nil means, update checkbox statistics after insert and toggle.
  628. When this is set, checkbox statistics is updated each time you either insert
  629. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  630. with \\[org-ctrl-c-ctrl-c\\]."
  631. :group 'org-plain-lists
  632. :type 'boolean)
  633. (defgroup org-archive nil
  634. "Options concerning archiving in Org-mode."
  635. :tag "Org Archive"
  636. :group 'org-structure)
  637. (defcustom org-archive-tag "ARCHIVE"
  638. "The tag that marks a subtree as archived.
  639. An archived subtree does not open during visibility cycling, and does
  640. not contribute to the agenda listings.
  641. After changing this, font-lock must be restarted in the relevant buffers to
  642. get the proper fontification."
  643. :group 'org-archive
  644. :group 'org-keywords
  645. :type 'string)
  646. (defcustom org-agenda-skip-archived-trees t
  647. "Non-nil means, the agenda will skip any items located in archived trees.
  648. An archived tree is a tree marked with the tag ARCHIVE."
  649. :group 'org-archive
  650. :group 'org-agenda-skip
  651. :type 'boolean)
  652. (defcustom org-cycle-open-archived-trees nil
  653. "Non-nil means, `org-cycle' will open archived trees.
  654. An archived tree is a tree marked with the tag ARCHIVE.
  655. When nil, archived trees will stay folded. You can still open them with
  656. normal outline commands like `show-all', but not with the cycling commands."
  657. :group 'org-archive
  658. :group 'org-cycle
  659. :type 'boolean)
  660. (defcustom org-sparse-tree-open-archived-trees nil
  661. "Non-nil means sparse tree construction shows matches in archived trees.
  662. When nil, matches in these trees are highlighted, but the trees are kept in
  663. collapsed state."
  664. :group 'org-archive
  665. :group 'org-sparse-trees
  666. :type 'boolean)
  667. (defcustom org-archive-location "%s_archive::"
  668. "The location where subtrees should be archived.
  669. This string consists of two parts, separated by a double-colon.
  670. The first part is a file name - when omitted, archiving happens in the same
  671. file. %s will be replaced by the current file name (without directory part).
  672. Archiving to a different file is useful to keep archived entries from
  673. contributing to the Org-mode Agenda.
  674. The part after the double colon is a headline. The archived entries will be
  675. filed under that headline. When omitted, the subtrees are simply filed away
  676. at the end of the file, as top-level entries.
  677. Here are a few examples:
  678. \"%s_archive::\"
  679. If the current file is Projects.org, archive in file
  680. Projects.org_archive, as top-level trees. This is the default.
  681. \"::* Archived Tasks\"
  682. Archive in the current file, under the top-level headline
  683. \"* Archived Tasks\".
  684. \"~/org/archive.org::\"
  685. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  686. \"basement::** Finished Tasks\"
  687. Archive in file ./basement (relative path), as level 3 trees
  688. below the level 2 heading \"** Finished Tasks\".
  689. You may set this option on a per-file basis by adding to the buffer a
  690. line like
  691. #+ARCHIVE: basement::** Finished Tasks"
  692. :group 'org-archive
  693. :type 'string)
  694. (defcustom org-archive-mark-done t
  695. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  696. This can be a string to set the keyword to use. When t, Org-mode will
  697. use the first keyword in its list that means done."
  698. :group 'org-archive
  699. :type '(choice
  700. (const :tag "No" nil)
  701. (const :tag "Yes" t)
  702. (string :tag "Use this keyword")))
  703. (defcustom org-archive-stamp-time t
  704. "Non-nil means, add a time stamp to entries moved to an archive file.
  705. This variable is obsolete and has no effect anymore, instead add ot remove
  706. `time' from the variablle `org-archive-save-context-info'."
  707. :group 'org-archive
  708. :type 'boolean)
  709. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  710. "Parts of context info that should be stored as properties when archiving.
  711. When a subtree is moved to an archive file, it looses information given by
  712. context, like inherited tags, the category, and possibly also the TODO
  713. state (depending on the variable `org-archive-mark-done').
  714. This variable can be a list of any of the following symbols:
  715. time The time of archiving.
  716. file The file where the entry originates.
  717. itags The local tags, in the headline of the subtree.
  718. ltags The tags the subtree inherits from further up the hierarchy.
  719. todo The pre-archive TODO state.
  720. category The category, taken from file name or #+CATEGORY lines.
  721. olpath The outline path to the item. These are all headlines above
  722. the current item, separated by /, like a file path.
  723. For each symbol present in the list, a property will be created in
  724. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  725. information."
  726. :group 'org-archive
  727. :type '(set :greedy t
  728. (const :tag "Time" time)
  729. (const :tag "File" file)
  730. (const :tag "Category" category)
  731. (const :tag "TODO state" todo)
  732. (const :tag "TODO state" priority)
  733. (const :tag "Inherited tags" itags)
  734. (const :tag "Outline path" olpath)
  735. (const :tag "Local tags" ltags)))
  736. (defgroup org-imenu-and-speedbar nil
  737. "Options concerning imenu and speedbar in Org-mode."
  738. :tag "Org Imenu and Speedbar"
  739. :group 'org-structure)
  740. (defcustom org-imenu-depth 2
  741. "The maximum level for Imenu access to Org-mode headlines.
  742. This also applied for speedbar access."
  743. :group 'org-imenu-and-speedbar
  744. :type 'number)
  745. (defgroup org-table nil
  746. "Options concerning tables in Org-mode."
  747. :tag "Org Table"
  748. :group 'org)
  749. (defcustom org-enable-table-editor 'optimized
  750. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  751. When nil, such lines will be treated like ordinary lines.
  752. When equal to the symbol `optimized', the table editor will be optimized to
  753. do the following:
  754. - Automatic overwrite mode in front of whitespace in table fields.
  755. This makes the structure of the table stay in tact as long as the edited
  756. field does not exceed the column width.
  757. - Minimize the number of realigns. Normally, the table is aligned each time
  758. TAB or RET are pressed to move to another field. With optimization this
  759. happens only if changes to a field might have changed the column width.
  760. Optimization requires replacing the functions `self-insert-command',
  761. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  762. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  763. very good at guessing when a re-align will be necessary, but you can always
  764. force one with \\[org-ctrl-c-ctrl-c].
  765. If you would like to use the optimized version in Org-mode, but the
  766. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  767. This variable can be used to turn on and off the table editor during a session,
  768. but in order to toggle optimization, a restart is required.
  769. See also the variable `org-table-auto-blank-field'."
  770. :group 'org-table
  771. :type '(choice
  772. (const :tag "off" nil)
  773. (const :tag "on" t)
  774. (const :tag "on, optimized" optimized)))
  775. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  776. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  777. In the optimized version, the table editor takes over all simple keys that
  778. normally just insert a character. In tables, the characters are inserted
  779. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  780. for empty fields). Outside tables, the correct binding of the keys is
  781. restored.
  782. The default for this option is t if the optimized version is also used in
  783. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  784. this variable requires a restart of Emacs to become effective."
  785. :group 'org-table
  786. :type 'boolean)
  787. (defcustom orgtbl-radio-table-templates
  788. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  789. % END RECEIVE ORGTBL %n
  790. \\begin{comment}
  791. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  792. | | |
  793. \\end{comment}\n")
  794. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  795. @c END RECEIVE ORGTBL %n
  796. @ignore
  797. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  798. | | |
  799. @end ignore\n")
  800. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  801. <!-- END RECEIVE ORGTBL %n -->
  802. <!--
  803. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  804. | | |
  805. -->\n"))
  806. "Templates for radio tables in different major modes.
  807. All occurrences of %n in a template will be replaced with the name of the
  808. table, obtained by prompting the user."
  809. :group 'org-table
  810. :type '(repeat
  811. (list (symbol :tag "Major mode")
  812. (string :tag "Format"))))
  813. (defgroup org-table-settings nil
  814. "Settings for tables in Org-mode."
  815. :tag "Org Table Settings"
  816. :group 'org-table)
  817. (defcustom org-table-default-size "5x2"
  818. "The default size for newly created tables, Columns x Rows."
  819. :group 'org-table-settings
  820. :type 'string)
  821. (defcustom org-table-number-regexp
  822. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  823. "Regular expression for recognizing numbers in table columns.
  824. If a table column contains mostly numbers, it will be aligned to the
  825. right. If not, it will be aligned to the left.
  826. The default value of this option is a regular expression which allows
  827. anything which looks remotely like a number as used in scientific
  828. context. For example, all of the following will be considered a
  829. number:
  830. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  831. Other options offered by the customize interface are more restrictive."
  832. :group 'org-table-settings
  833. :type '(choice
  834. (const :tag "Positive Integers"
  835. "^[0-9]+$")
  836. (const :tag "Integers"
  837. "^[-+]?[0-9]+$")
  838. (const :tag "Floating Point Numbers"
  839. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  840. (const :tag "Floating Point Number or Integer"
  841. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  842. (const :tag "Exponential, Floating point, Integer"
  843. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  844. (const :tag "Very General Number-Like, including hex"
  845. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  846. (string :tag "Regexp:")))
  847. (defcustom org-table-number-fraction 0.5
  848. "Fraction of numbers in a column required to make the column align right.
  849. In a column all non-white fields are considered. If at least this
  850. fraction of fields is matched by `org-table-number-fraction',
  851. alignment to the right border applies."
  852. :group 'org-table-settings
  853. :type 'number)
  854. (defgroup org-table-editing nil
  855. "Behavior of tables during editing in Org-mode."
  856. :tag "Org Table Editing"
  857. :group 'org-table)
  858. (defcustom org-table-automatic-realign t
  859. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  860. When nil, aligning is only done with \\[org-table-align], or after column
  861. removal/insertion."
  862. :group 'org-table-editing
  863. :type 'boolean)
  864. (defcustom org-table-auto-blank-field t
  865. "Non-nil means, automatically blank table field when starting to type into it.
  866. This only happens when typing immediately after a field motion
  867. command (TAB, S-TAB or RET).
  868. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  869. :group 'org-table-editing
  870. :type 'boolean)
  871. (defcustom org-table-tab-jumps-over-hlines t
  872. "Non-nil means, tab in the last column of a table with jump over a hline.
  873. If a horizontal separator line is following the current line,
  874. `org-table-next-field' can either create a new row before that line, or jump
  875. over the line. When this option is nil, a new line will be created before
  876. this line."
  877. :group 'org-table-editing
  878. :type 'boolean)
  879. (defcustom org-table-tab-recognizes-table.el t
  880. "Non-nil means, TAB will automatically notice a table.el table.
  881. When it sees such a table, it moves point into it and - if necessary -
  882. calls `table-recognize-table'."
  883. :group 'org-table-editing
  884. :type 'boolean)
  885. (defgroup org-table-calculation nil
  886. "Options concerning tables in Org-mode."
  887. :tag "Org Table Calculation"
  888. :group 'org-table)
  889. (defcustom org-table-use-standard-references t
  890. "Should org-mode work with table refrences like B3 instead of @3$2?
  891. Possible values are:
  892. nil never use them
  893. from accept as input, do not present for editing
  894. t: accept as input and present for editing"
  895. :group 'org-table-calculation
  896. :type '(choice
  897. (const :tag "Never, don't even check unser input for them" nil)
  898. (const :tag "Always, both as user input, and when editing" t)
  899. (const :tag "Convert user input, don't offer during editing" 'from)))
  900. (defcustom org-table-copy-increment t
  901. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  902. :group 'org-table-calculation
  903. :type 'boolean)
  904. (defcustom org-calc-default-modes
  905. '(calc-internal-prec 12
  906. calc-float-format (float 5)
  907. calc-angle-mode deg
  908. calc-prefer-frac nil
  909. calc-symbolic-mode nil
  910. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  911. calc-display-working-message t
  912. )
  913. "List with Calc mode settings for use in calc-eval for table formulas.
  914. The list must contain alternating symbols (Calc modes variables and values).
  915. Don't remove any of the default settings, just change the values. Org-mode
  916. relies on the variables to be present in the list."
  917. :group 'org-table-calculation
  918. :type 'plist)
  919. (defcustom org-table-formula-evaluate-inline t
  920. "Non-nil means, TAB and RET evaluate a formula in current table field.
  921. If the current field starts with an equal sign, it is assumed to be a formula
  922. which should be evaluated as described in the manual and in the documentation
  923. string of the command `org-table-eval-formula'. This feature requires the
  924. Emacs calc package.
  925. When this variable is nil, formula calculation is only available through
  926. the command \\[org-table-eval-formula]."
  927. :group 'org-table-calculation
  928. :type 'boolean)
  929. (defcustom org-table-formula-use-constants t
  930. "Non-nil means, interpret constants in formulas in tables.
  931. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  932. by the value given in `org-table-formula-constants', or by a value obtained
  933. from the `constants.el' package."
  934. :group 'org-table-calculation
  935. :type 'boolean)
  936. (defcustom org-table-formula-constants nil
  937. "Alist with constant names and values, for use in table formulas.
  938. The car of each element is a name of a constant, without the `$' before it.
  939. The cdr is the value as a string. For example, if you'd like to use the
  940. speed of light in a formula, you would configure
  941. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  942. and then use it in an equation like `$1*$c'.
  943. Constants can also be defined on a per-file basis using a line like
  944. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  945. :group 'org-table-calculation
  946. :type '(repeat
  947. (cons (string :tag "name")
  948. (string :tag "value"))))
  949. (defvar org-table-formula-constants-local nil
  950. "Local version of `org-table-formula-constants'.")
  951. (make-variable-buffer-local 'org-table-formula-constants-local)
  952. (defcustom org-table-allow-automatic-line-recalculation t
  953. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  954. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  955. :group 'org-table-calculation
  956. :type 'boolean)
  957. (defgroup org-link nil
  958. "Options concerning links in Org-mode."
  959. :tag "Org Link"
  960. :group 'org)
  961. (defvar org-link-abbrev-alist-local nil
  962. "Buffer-local version of `org-link-abbrev-alist', which see.
  963. The value of this is taken from the #+LINK lines.")
  964. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  965. (defcustom org-link-abbrev-alist nil
  966. "Alist of link abbreviations.
  967. The car of each element is a string, to be replaced at the start of a link.
  968. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  969. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  970. [[linkkey:tag][description]]
  971. If REPLACE is a string, the tag will simply be appended to create the link.
  972. If the string contains \"%s\", the tag will be inserted there.
  973. REPLACE may also be a function that will be called with the tag as the
  974. only argument to create the link, which should be returned as a string.
  975. See the manual for examples."
  976. :group 'org-link
  977. :type 'alist)
  978. (defcustom org-descriptive-links t
  979. "Non-nil means, hide link part and only show description of bracket links.
  980. Bracket links are like [[link][descritpion]]. This variable sets the initial
  981. state in new org-mode buffers. The setting can then be toggled on a
  982. per-buffer basis from the Org->Hyperlinks menu."
  983. :group 'org-link
  984. :type 'boolean)
  985. (defcustom org-link-file-path-type 'adaptive
  986. "How the path name in file links should be stored.
  987. Valid values are:
  988. relative Relative to the current directory, i.e. the directory of the file
  989. into which the link is being inserted.
  990. absolute Absolute path, if possible with ~ for home directory.
  991. noabbrev Absolute path, no abbreviation of home directory.
  992. adaptive Use relative path for files in the current directory and sub-
  993. directories of it. For other files, use an absolute path."
  994. :group 'org-link
  995. :type '(choice
  996. (const relative)
  997. (const absolute)
  998. (const noabbrev)
  999. (const adaptive)))
  1000. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1001. "Types of links that should be activated in Org-mode files.
  1002. This is a list of symbols, each leading to the activation of a certain link
  1003. type. In principle, it does not hurt to turn on most link types - there may
  1004. be a small gain when turning off unused link types. The types are:
  1005. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1006. angular Links in angular brackes that may contain whitespace like
  1007. <bbdb:Carsten Dominik>.
  1008. plain Plain links in normal text, no whitespace, like http://google.com.
  1009. radio Text that is matched by a radio target, see manual for details.
  1010. tag Tag settings in a headline (link to tag search).
  1011. date Time stamps (link to calendar).
  1012. Changing this variable requires a restart of Emacs to become effective."
  1013. :group 'org-link
  1014. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1015. (const :tag "Angular bracket links (old style)" angular)
  1016. (const :tag "Plain text links" plain)
  1017. (const :tag "Radio target matches" radio)
  1018. (const :tag "Tags" tag)
  1019. (const :tag "Timestamps" date)))
  1020. (defgroup org-link-store nil
  1021. "Options concerning storing links in Org-mode"
  1022. :tag "Org Store Link"
  1023. :group 'org-link)
  1024. (defcustom org-email-link-description-format "Email %c: %.30s"
  1025. "Format of the description part of a link to an email or usenet message.
  1026. The following %-excapes will be replaced by corresponding information:
  1027. %F full \"From\" field
  1028. %f name, taken from \"From\" field, address if no name
  1029. %T full \"To\" field
  1030. %t first name in \"To\" field, address if no name
  1031. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1032. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1033. %s subject
  1034. %m message-id.
  1035. You may use normal field width specification between the % and the letter.
  1036. This is for example useful to limit the length of the subject.
  1037. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1038. :group 'org-link-store
  1039. :type 'string)
  1040. (defcustom org-from-is-user-regexp
  1041. (let (r1 r2)
  1042. (when (and user-mail-address (not (string= user-mail-address "")))
  1043. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1044. (when (and user-full-name (not (string= user-full-name "")))
  1045. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1046. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1047. "Regexp mached against the \"From:\" header of an email or usenet message.
  1048. It should match if the message is from the user him/herself."
  1049. :group 'org-link-store
  1050. :type 'regexp)
  1051. (defcustom org-context-in-file-links t
  1052. "Non-nil means, file links from `org-store-link' contain context.
  1053. A search string will be added to the file name with :: as separator and
  1054. used to find the context when the link is activated by the command
  1055. `org-open-at-point'.
  1056. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1057. negates this setting for the duration of the command."
  1058. :group 'org-link-store
  1059. :type 'boolean)
  1060. (defcustom org-keep-stored-link-after-insertion nil
  1061. "Non-nil means, keep link in list for entire session.
  1062. The command `org-store-link' adds a link pointing to the current
  1063. location to an internal list. These links accumulate during a session.
  1064. The command `org-insert-link' can be used to insert links into any
  1065. Org-mode file (offering completion for all stored links). When this
  1066. option is nil, every link which has been inserted once using \\[org-insert-link]
  1067. will be removed from the list, to make completing the unused links
  1068. more efficient."
  1069. :group 'org-link-store
  1070. :type 'boolean)
  1071. (defcustom org-usenet-links-prefer-google nil
  1072. "Non-nil means, `org-store-link' will create web links to Google groups.
  1073. When nil, Gnus will be used for such links.
  1074. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1075. negates this setting for the duration of the command."
  1076. :group 'org-link-store
  1077. :type 'boolean)
  1078. (defgroup org-link-follow nil
  1079. "Options concerning following links in Org-mode"
  1080. :tag "Org Follow Link"
  1081. :group 'org-link)
  1082. (defcustom org-follow-link-hook nil
  1083. "Hook that is run after a link has been followed."
  1084. :group 'org-link-follow
  1085. :type 'hook)
  1086. (defcustom org-tab-follows-link nil
  1087. "Non-nil means, on links TAB 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-return-follows-link nil
  1092. "Non-nil means, on links RET will follow the link.
  1093. Needs to be set before org.el is loaded."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-mouse-1-follows-link
  1097. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1098. "Non-nil means, mouse-1 on a link will follow the link.
  1099. A longer mouse click will still set point. Does not work on XEmacs.
  1100. Needs to be set before org.el is loaded."
  1101. :group 'org-link-follow
  1102. :type 'boolean)
  1103. (defcustom org-mark-ring-length 4
  1104. "Number of different positions to be recorded in the ring
  1105. Changing this requires a restart of Emacs to work correctly."
  1106. :group 'org-link-follow
  1107. :type 'interger)
  1108. (defcustom org-link-frame-setup
  1109. '((vm . vm-visit-folder-other-frame)
  1110. (gnus . gnus-other-frame)
  1111. (file . find-file-other-window))
  1112. "Setup the frame configuration for following links.
  1113. When following a link with Emacs, it may often be useful to display
  1114. this link in another window or frame. This variable can be used to
  1115. set this up for the different types of links.
  1116. For VM, use any of
  1117. `vm-visit-folder'
  1118. `vm-visit-folder-other-frame'
  1119. For Gnus, use any of
  1120. `gnus'
  1121. `gnus-other-frame'
  1122. For FILE, use any of
  1123. `find-file'
  1124. `find-file-other-window'
  1125. `find-file-other-frame'
  1126. For the calendar, use the variable `calendar-setup'.
  1127. For BBDB, it is currently only possible to display the matches in
  1128. another window."
  1129. :group 'org-link-follow
  1130. :type '(list
  1131. (cons (const vm)
  1132. (choice
  1133. (const vm-visit-folder)
  1134. (const vm-visit-folder-other-window)
  1135. (const vm-visit-folder-other-frame)))
  1136. (cons (const gnus)
  1137. (choice
  1138. (const gnus)
  1139. (const gnus-other-frame)))
  1140. (cons (const file)
  1141. (choice
  1142. (const find-file)
  1143. (const find-file-other-window)
  1144. (const find-file-other-frame)))))
  1145. (defcustom org-display-internal-link-with-indirect-buffer nil
  1146. "Non-nil means, use indirect buffer to display infile links.
  1147. Activating internal links (from one location in a file to another location
  1148. in the same file) normally just jumps to the location. When the link is
  1149. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1150. another window. When this option is set, the other window actually displays
  1151. an indirect buffer clone of the current buffer, to avoid any visibility
  1152. changes to the current buffer."
  1153. :group 'org-link-follow
  1154. :type 'boolean)
  1155. (defcustom org-open-non-existing-files nil
  1156. "Non-nil means, `org-open-file' will open non-existing files.
  1157. When nil, an error will be generated."
  1158. :group 'org-link-follow
  1159. :type 'boolean)
  1160. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1161. "Function and arguments to call for following mailto links.
  1162. This is a list with the first element being a lisp function, and the
  1163. remaining elements being arguments to the function. In string arguments,
  1164. %a will be replaced by the address, and %s will be replaced by the subject
  1165. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1166. :group 'org-link-follow
  1167. :type '(choice
  1168. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1169. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1170. (const :tag "message-mail" (message-mail "%a" "%s"))
  1171. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1172. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1173. "Non-nil means, ask for confirmation before executing shell links.
  1174. Shell links can be dangerous: just think about a link
  1175. [[shell:rm -rf ~/*][Google Search]]
  1176. This link would show up in your Org-mode document as \"Google Search\",
  1177. but really it would remove your entire home directory.
  1178. Therefore we advise against setting this variable to nil.
  1179. Just change it to `y-or-n-p' of you want to confirm with a
  1180. single keystroke rather than having to type \"yes\"."
  1181. :group 'org-link-follow
  1182. :type '(choice
  1183. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1184. (const :tag "with y-or-n (faster)" y-or-n-p)
  1185. (const :tag "no confirmation (dangerous)" nil)))
  1186. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1187. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1188. Elisp links can be dangerous: just think about a link
  1189. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1190. This link would show up in your Org-mode document as \"Google Search\",
  1191. but really it would remove your entire home directory.
  1192. Therefore we advise against setting this variable to nil.
  1193. Just change it to `y-or-n-p' of you want to confirm with a
  1194. single keystroke rather than having to type \"yes\"."
  1195. :group 'org-link-follow
  1196. :type '(choice
  1197. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1198. (const :tag "with y-or-n (faster)" y-or-n-p)
  1199. (const :tag "no confirmation (dangerous)" nil)))
  1200. (defconst org-file-apps-defaults-gnu
  1201. '((remote . emacs)
  1202. (t . mailcap))
  1203. "Default file applications on a UNIX or GNU/Linux system.
  1204. See `org-file-apps'.")
  1205. (defconst org-file-apps-defaults-macosx
  1206. '((remote . emacs)
  1207. (t . "open %s")
  1208. ("ps" . "gv %s")
  1209. ("ps.gz" . "gv %s")
  1210. ("eps" . "gv %s")
  1211. ("eps.gz" . "gv %s")
  1212. ("dvi" . "xdvi %s")
  1213. ("fig" . "xfig %s"))
  1214. "Default file applications on a MacOS X system.
  1215. The system \"open\" is known as a default, but we use X11 applications
  1216. for some files for which the OS does not have a good default.
  1217. See `org-file-apps'.")
  1218. (defconst org-file-apps-defaults-windowsnt
  1219. (list
  1220. '(remote . emacs)
  1221. (cons t
  1222. (list (if (featurep 'xemacs)
  1223. 'mswindows-shell-execute
  1224. 'w32-shell-execute)
  1225. "open" 'file)))
  1226. "Default file applications on a Windows NT system.
  1227. The system \"open\" is used for most files.
  1228. See `org-file-apps'.")
  1229. (defcustom org-file-apps
  1230. '(
  1231. ("txt" . emacs)
  1232. ("tex" . emacs)
  1233. ("ltx" . emacs)
  1234. ("org" . emacs)
  1235. ("el" . emacs)
  1236. ("bib" . emacs)
  1237. )
  1238. "External applications for opening `file:path' items in a document.
  1239. Org-mode uses system defaults for different file types, but
  1240. you can use this variable to set the application for a given file
  1241. extension. The entries in this list are cons cells where the car identifies
  1242. files and the cdr the corresponding command. Possible values for the
  1243. file identifier are
  1244. \"ext\" A string identifying an extension
  1245. `directory' Matches a directory
  1246. `remote' Matches a remote file, accessible through tramp or efs.
  1247. Remote files most likely should be visited through Emacs
  1248. because external applications cannot handle such paths.
  1249. t Default for all remaining files
  1250. Possible values for the command are:
  1251. `emacs' The file will be visited by the current Emacs process.
  1252. `default' Use the default application for this file type.
  1253. string A command to be executed by a shell; %s will be replaced
  1254. by the path to the file.
  1255. sexp A Lisp form which will be evaluated. The file path will
  1256. be available in the Lisp variable `file'.
  1257. For more examples, see the system specific constants
  1258. `org-file-apps-defaults-macosx'
  1259. `org-file-apps-defaults-windowsnt'
  1260. `org-file-apps-defaults-gnu'."
  1261. :group 'org-link-follow
  1262. :type '(repeat
  1263. (cons (choice :value ""
  1264. (string :tag "Extension")
  1265. (const :tag "Default for unrecognized files" t)
  1266. (const :tag "Remote file" remote)
  1267. (const :tag "Links to a directory" directory))
  1268. (choice :value ""
  1269. (const :tag "Visit with Emacs" emacs)
  1270. (const :tag "Use system default" default)
  1271. (string :tag "Command")
  1272. (sexp :tag "Lisp form")))))
  1273. (defcustom org-mhe-search-all-folders nil
  1274. "Non-nil means, that the search for the mh-message will be extended to
  1275. all folders if the message cannot be found in the folder given in the link.
  1276. Searching all folders is very efficient with one of the search engines
  1277. supported by MH-E, but will be slow with pick."
  1278. :group 'org-link-follow
  1279. :type 'boolean)
  1280. (defgroup org-remember nil
  1281. "Options concerning interaction with remember.el."
  1282. :tag "Org Remember"
  1283. :group 'org)
  1284. (defcustom org-directory "~/org"
  1285. "Directory with org files.
  1286. This directory will be used as default to prompt for org files.
  1287. Used by the hooks for remember.el."
  1288. :group 'org-remember
  1289. :type 'directory)
  1290. (defcustom org-default-notes-file "~/.notes"
  1291. "Default target for storing notes.
  1292. Used by the hooks for remember.el. This can be a string, or nil to mean
  1293. the value of `remember-data-file'.
  1294. You can set this on a per-template basis with the variable
  1295. `org-remember-templates'."
  1296. :group 'org-remember
  1297. :type '(choice
  1298. (const :tag "Default from remember-data-file" nil)
  1299. file))
  1300. (defcustom org-remember-store-without-prompt t
  1301. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1302. In this case, you need `C-u C-c C-c' to get the prompts for
  1303. note file and headline.
  1304. When this variable is nil, `C-c C-c' give you the prompts, and
  1305. `C-u C-c C-c' trigger the fasttrack."
  1306. :group 'org-remember
  1307. :type 'boolean)
  1308. (defcustom org-remember-interactive-interface 'refile
  1309. "The interface to be used for interactive filing of remember notes.
  1310. This is only used when the interactive mode for selecting a filing
  1311. location is used (see the variable `org-remember-store-without-prompt').
  1312. Allowed vaues are:
  1313. outline The interface shows an outline of the relevant file
  1314. and the correct heading is found by moving through
  1315. the outline or by searching with incremental search.
  1316. outline-path-completion Headlines in the current buffer are offered via
  1317. completion.
  1318. refile Use the refile interface, and offer headlines,
  1319. possibly from different buffers."
  1320. :group 'org-remember
  1321. :type '(choice
  1322. (const :tag "Refile" refile)
  1323. (const :tag "Outline" outline)
  1324. (const :tag "Outline-path-completion" outline-path-completion)))
  1325. (defcustom org-goto-interface 'outline
  1326. "The default interface to be used for `org-goto'.
  1327. Allowed vaues are:
  1328. outline The interface shows an outline of the relevant file
  1329. and the correct heading is found by moving through
  1330. the outline or by searching with incremental search.
  1331. outline-path-completion Headlines in the current buffer are offered via
  1332. completion."
  1333. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1334. :type '(choice
  1335. (const :tag "Outline" outline)
  1336. (const :tag "Outline-path-completion" outline-path-completion)))
  1337. (defcustom org-remember-default-headline ""
  1338. "The headline that should be the default location in the notes file.
  1339. When filing remember notes, the cursor will start at that position.
  1340. You can set this on a per-template basis with the variable
  1341. `org-remember-templates'."
  1342. :group 'org-remember
  1343. :type 'string)
  1344. (defcustom org-remember-templates nil
  1345. "Templates for the creation of remember buffers.
  1346. When nil, just let remember make the buffer.
  1347. When not nil, this is a list of 5-element lists. In each entry, the first
  1348. element is the name of the template, which should be a single short word.
  1349. The second element is a character, a unique key to select this template.
  1350. The third element is the template. The fourth element is optional and can
  1351. specify a destination file for remember items created with this template.
  1352. The default file is given by `org-default-notes-file'. An optional fifth
  1353. element can specify the headline in that file that should be offered
  1354. first when the user is asked to file the entry. The default headline is
  1355. given in the variable `org-remember-default-headline'.
  1356. An optional sixth element can specify the context in which the user should
  1357. be able to select this template. If this element is a list of major modes,
  1358. the template will only be available while invoking `org-remember' from a
  1359. buffer in one of these modes. If it is a function, the template will only
  1360. be selected if the function returns `t'. A value of `t' means select
  1361. this template in any context. When the element is `nil', the template
  1362. will be selected by default, i.e. when all contextual checks failed.
  1363. The template specifies the structure of the remember buffer. It should have
  1364. a first line starting with a star, to act as the org-mode headline.
  1365. Furthermore, the following %-escapes will be replaced with content:
  1366. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1367. A default value and a completion table ca be specified like this:
  1368. %^{prompt|default|completion2|completion3|...}
  1369. %t time stamp, date only
  1370. %T time stamp with date and time
  1371. %u, %U like the above, but inactive time stamps
  1372. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1373. You may define a prompt like %^{Please specify birthday}t
  1374. %n user name (taken from `user-full-name')
  1375. %a annotation, normally the link created with org-store-link
  1376. %i initial content, the region active. If %i is indented,
  1377. the entire inserted text will be indented as well.
  1378. %c content of the clipboard, or current kill ring head
  1379. %^g prompt for tags, with completion on tags in target file
  1380. %^G prompt for tags, with completion all tags in all agenda files
  1381. %:keyword specific information for certain link types, see below
  1382. %[pathname] insert the contents of the file given by `pathname'
  1383. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1384. %! Store this note immediately after filling the template
  1385. %? After completing the template, position cursor here.
  1386. Apart from these general escapes, you can access information specific to the
  1387. link type that is created. For example, calling `remember' in emails or gnus
  1388. will record the author and the subject of the message, which you can access
  1389. with %:author and %:subject, respectively. Here is a complete list of what
  1390. is recorded for each link type.
  1391. Link type | Available information
  1392. -------------------+------------------------------------------------------
  1393. bbdb | %:type %:name %:company
  1394. vm, wl, mh, rmail | %:type %:subject %:message-id
  1395. | %:from %:fromname %:fromaddress
  1396. | %:to %:toname %:toaddress
  1397. | %:fromto (either \"to NAME\" or \"from NAME\")
  1398. gnus | %:group, for messages also all email fields
  1399. w3, w3m | %:type %:url
  1400. info | %:type %:file %:node
  1401. calendar | %:type %:date"
  1402. :group 'org-remember
  1403. :get (lambda (var) ; Make sure all entries have at least 5 elements
  1404. (mapcar (lambda (x)
  1405. (if (not (stringp (car x))) (setq x (cons "" x)))
  1406. (cond ((= (length x) 4) (append x '("")))
  1407. ((= (length x) 3) (append x '("" "")))
  1408. (t x)))
  1409. (default-value var)))
  1410. :type '(repeat
  1411. :tag "enabled"
  1412. (list :value ("" ?a "\n" nil nil nil)
  1413. (string :tag "Name")
  1414. (character :tag "Selection Key")
  1415. (string :tag "Template")
  1416. (choice
  1417. (file :tag "Destination file")
  1418. (const :tag "Prompt for file" nil))
  1419. (choice
  1420. (string :tag "Destination headline")
  1421. (const :tag "Selection interface for heading"))
  1422. (choice
  1423. (const :tag "Use by default" nil)
  1424. (const :tag "Use in all contexts" t)
  1425. (repeat :tag "Use only if in major mode"
  1426. (symbol :tag "Major mode"))
  1427. (function :tag "Perform a check against function")))))
  1428. (defcustom org-reverse-note-order nil
  1429. "Non-nil means, store new notes at the beginning of a file or entry.
  1430. When nil, new notes will be filed to the end of a file or entry.
  1431. This can also be a list with cons cells of regular expressions that
  1432. are matched against file names, and values."
  1433. :group 'org-remember
  1434. :type '(choice
  1435. (const :tag "Reverse always" t)
  1436. (const :tag "Reverse never" nil)
  1437. (repeat :tag "By file name regexp"
  1438. (cons regexp boolean))))
  1439. (defcustom org-refile-targets nil
  1440. "Targets for refiling entries with \\[org-refile].
  1441. This is list of cons cells. Each cell contains:
  1442. - a specification of the files to be considered, either a list of files,
  1443. or a symbol whose function or value fields will be used to retrieve
  1444. a file name or a list of file names. Nil means, refile to a different
  1445. heading in the current buffer.
  1446. - A specification of how to find candidate refile targets. This may be
  1447. any of
  1448. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1449. This tag has to be present in all target headlines, inheritance will
  1450. not be considered.
  1451. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1452. todo keyword.
  1453. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1454. headlines that are refiling targets.
  1455. - a cons cell (:level . N). Any headline of level N is considered a target.
  1456. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1457. ;; FIXME: what if there are a var and func with same name???
  1458. :group 'org-remember
  1459. :type '(repeat
  1460. (cons
  1461. (choice :value org-agenda-files
  1462. (const :tag "All agenda files" org-agenda-files)
  1463. (const :tag "Current buffer" nil)
  1464. (function) (variable) (file))
  1465. (choice :tag "Identify target headline by"
  1466. (cons :tag "Specific tag" (const :tag) (string))
  1467. (cons :tag "TODO keyword" (const :todo) (string))
  1468. (cons :tag "Regular expression" (const :regexp) (regexp))
  1469. (cons :tag "Level number" (const :level) (integer))
  1470. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1471. (defcustom org-refile-use-outline-path nil
  1472. "Non-nil means, provide refile targets as paths.
  1473. So a level 3 headline will be available as level1/level2/level3.
  1474. When the value is `file', also include the file name (without directory)
  1475. into the path. When `full-file-path', include the full file path."
  1476. :group 'org-remember
  1477. :type '(choice
  1478. (const :tag "Not" nil)
  1479. (const :tag "Yes" t)
  1480. (const :tag "Start with file name" file)
  1481. (const :tag "Start with full file path" full-file-path)))
  1482. (defgroup org-todo nil
  1483. "Options concerning TODO items in Org-mode."
  1484. :tag "Org TODO"
  1485. :group 'org)
  1486. (defgroup org-progress nil
  1487. "Options concerning Progress logging in Org-mode."
  1488. :tag "Org Progress"
  1489. :group 'org-time)
  1490. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1491. "List of TODO entry keyword sequences and their interpretation.
  1492. \\<org-mode-map>This is a list of sequences.
  1493. Each sequence starts with a symbol, either `sequence' or `type',
  1494. indicating if the keywords should be interpreted as a sequence of
  1495. action steps, or as different types of TODO items. The first
  1496. keywords are states requiring action - these states will select a headline
  1497. for inclusion into the global TODO list Org-mode produces. If one of
  1498. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1499. signify that no further action is necessary. If \"|\" is not found,
  1500. the last keyword is treated as the only DONE state of the sequence.
  1501. The command \\[org-todo] cycles an entry through these states, and one
  1502. additional state where no keyword is present. For details about this
  1503. cycling, see the manual.
  1504. TODO keywords and interpretation can also be set on a per-file basis with
  1505. the special #+SEQ_TODO and #+TYP_TODO lines.
  1506. Each keyword can optionally specify a character for fast state selection
  1507. \(in combination with the variable `org-use-fast-todo-selection')
  1508. and specifiers for state change logging, using the same syntax
  1509. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1510. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1511. indicates to record a time stamp each time this state is selected.
  1512. Each keyword may also specify if a timestamp or a note should be
  1513. recorded when entering or leaving the state, by adding additional
  1514. characters in the parenthesis after the keyword. This looks like this:
  1515. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1516. record only the time of the state change. With X and Y being either
  1517. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1518. Y when leaving the state if and only if the *target* state does not
  1519. define X. You may omit any of the fast-selection key or X or /Y,
  1520. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1521. For backward compatibility, this variable may also be just a list
  1522. of keywords - in this case the interptetation (sequence or type) will be
  1523. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1524. :group 'org-todo
  1525. :group 'org-keywords
  1526. :type '(choice
  1527. (repeat :tag "Old syntax, just keywords"
  1528. (string :tag "Keyword"))
  1529. (repeat :tag "New syntax"
  1530. (cons
  1531. (choice
  1532. :tag "Interpretation"
  1533. (const :tag "Sequence (cycling hits every state)" sequence)
  1534. (const :tag "Type (cycling directly to DONE)" type))
  1535. (repeat
  1536. (string :tag "Keyword"))))))
  1537. (defvar org-todo-keywords-1 nil
  1538. "All TODO and DONE keywords active in a buffer.")
  1539. (make-variable-buffer-local 'org-todo-keywords-1)
  1540. (defvar org-todo-keywords-for-agenda nil)
  1541. (defvar org-done-keywords-for-agenda nil)
  1542. (defvar org-not-done-keywords nil)
  1543. (make-variable-buffer-local 'org-not-done-keywords)
  1544. (defvar org-done-keywords nil)
  1545. (make-variable-buffer-local 'org-done-keywords)
  1546. (defvar org-todo-heads nil)
  1547. (make-variable-buffer-local 'org-todo-heads)
  1548. (defvar org-todo-sets nil)
  1549. (make-variable-buffer-local 'org-todo-sets)
  1550. (defvar org-todo-log-states nil)
  1551. (make-variable-buffer-local 'org-todo-log-states)
  1552. (defvar org-todo-kwd-alist nil)
  1553. (make-variable-buffer-local 'org-todo-kwd-alist)
  1554. (defvar org-todo-key-alist nil)
  1555. (make-variable-buffer-local 'org-todo-key-alist)
  1556. (defvar org-todo-key-trigger nil)
  1557. (make-variable-buffer-local 'org-todo-key-trigger)
  1558. (defcustom org-todo-interpretation 'sequence
  1559. "Controls how TODO keywords are interpreted.
  1560. This variable is in principle obsolete and is only used for
  1561. backward compatibility, if the interpretation of todo keywords is
  1562. not given already in `org-todo-keywords'. See that variable for
  1563. more information."
  1564. :group 'org-todo
  1565. :group 'org-keywords
  1566. :type '(choice (const sequence)
  1567. (const type)))
  1568. (defcustom org-use-fast-todo-selection 'prefix
  1569. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1570. This variable describes if and under what circumstances the cycling
  1571. mechanism for TODO keywords will be replaced by a single-key, direct
  1572. selection scheme.
  1573. When nil, fast selection is never used.
  1574. When the symbol `prefix', it will be used when `org-todo' is called with
  1575. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1576. in an agenda buffer.
  1577. When t, fast selection is used by default. In this case, the prefix
  1578. argument forces cycling instead.
  1579. In all cases, the special interface is only used if access keys have actually
  1580. been assigned by the user, i.e. if keywords in the configuration are followed
  1581. by a letter in parenthesis, like TODO(t)."
  1582. :group 'org-todo
  1583. :type '(choice
  1584. (const :tag "Never" nil)
  1585. (const :tag "By default" t)
  1586. (const :tag "Only with C-u C-c C-t" prefix)))
  1587. (defcustom org-after-todo-state-change-hook nil
  1588. "Hook which is run after the state of a TODO item was changed.
  1589. The new state (a string with a TODO keyword, or nil) is available in the
  1590. Lisp variable `state'."
  1591. :group 'org-todo
  1592. :type 'hook)
  1593. (defcustom org-log-done nil
  1594. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1595. When equal to the list (done), also prompt for a closing note.
  1596. This can also be configured on a per-file basis by adding one of
  1597. the following lines anywhere in the buffer:
  1598. #+STARTUP: logdone
  1599. #+STARTUP: lognotedone
  1600. #+STARTUP: nologdone"
  1601. :group 'org-todo
  1602. :group 'org-progress
  1603. :type '(choice
  1604. (const :tag "No logging" nil)
  1605. (const :tag "Record CLOSED timestamp" time)
  1606. (const :tag "Record CLOSED timestamp with closing note." note)))
  1607. ;; Normalize old uses of org-log-done.
  1608. (cond
  1609. ((eq org-log-done t) (setq org-log-done 'time))
  1610. ((and (listp org-log-done) (memq 'done org-log-done))
  1611. (setq org-log-done 'note)))
  1612. ;; FIXME: document
  1613. (defcustom org-log-note-clock-out nil
  1614. "Non-nil means, recored a note when clocking out of an item.
  1615. This can also be configured on a per-file basis by adding one of
  1616. the following lines anywhere in the buffer:
  1617. #+STARTUP: lognoteclock-out
  1618. #+STARTUP: nolognoteclock-out"
  1619. :group 'org-todo
  1620. :group 'org-progress
  1621. :type 'boolean)
  1622. (defcustom org-log-done-with-time t
  1623. "Non-nil means, the CLOSED time stamp will contain date and time.
  1624. When nil, only the date will be recorded."
  1625. :group 'org-progress
  1626. :type 'boolean)
  1627. (defcustom org-log-note-headings
  1628. '((done . "CLOSING NOTE %t")
  1629. (state . "State %-12s %t")
  1630. (clock-out . ""))
  1631. "Headings for notes added when clocking out or closing TODO items.
  1632. The value is an alist, with the car being a symbol indicating the note
  1633. context, and the cdr is the heading to be used. The heading may also be the
  1634. empty string.
  1635. %t in the heading will be replaced by a time stamp.
  1636. %s will be replaced by the new TODO state, in double quotes.
  1637. %u will be replaced by the user name.
  1638. %U will be replaced by the full user name."
  1639. :group 'org-todo
  1640. :group 'org-progress
  1641. :type '(list :greedy t
  1642. (cons (const :tag "Heading when closing an item" done) string)
  1643. (cons (const :tag
  1644. "Heading when changing todo state (todo sequence only)"
  1645. state) string)
  1646. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1647. (defcustom org-log-states-order-reversed t
  1648. "Non-nil means, the latest state change note will be directly after heading.
  1649. When nil, the notes will be orderer according to time."
  1650. :group 'org-todo
  1651. :group 'org-progress
  1652. :type 'boolean)
  1653. (defcustom org-log-repeat 'time
  1654. "Non-nil means, record moving through the DONE state when triggering repeat.
  1655. An auto-repeating tasks is immediately switched back to TODO when marked
  1656. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1657. the TODO keyword definition, or recording a cloing note by setting
  1658. `org-log-done', there will be no record of the task moving trhough DONE.
  1659. This variable forces taking a note anyway. Possible values are:
  1660. nil Don't force a record
  1661. time Record a time stamp
  1662. note Record a note
  1663. This option can also be set with on a per-file-basis with
  1664. #+STARTUP: logrepeat
  1665. #+STARTUP: lognoterepeat
  1666. #+STARTUP: nologrepeat
  1667. You can have local logging settings for a subtree by setting the LOGGING
  1668. property to one or more of these keywords."
  1669. :group 'org-todo
  1670. :group 'org-progress
  1671. :type '(choice
  1672. (const :tag "Don't force a record" nil)
  1673. (const :tag "Force recording the DONE state" time)
  1674. (const :tag "Force recording a note with the DONE state" note)))
  1675. (defcustom org-clock-into-drawer 2
  1676. "Should clocking info be wrapped into a drawer?
  1677. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1678. If necessary, the drawer will be created.
  1679. When nil, the drawer will not be created, but used when present.
  1680. When an integer and the number of clocking entries in an item
  1681. reaches or exceeds this number, a drawer will be created."
  1682. :group 'org-todo
  1683. :group 'org-progress
  1684. :type '(choice
  1685. (const :tag "Always" t)
  1686. (const :tag "Only when drawer exists" nil)
  1687. (integer :tag "When at least N clock entries")))
  1688. (defcustom org-clock-out-when-done t
  1689. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1690. Nil means, clock will keep running until stopped explicitly with
  1691. `C-c C-x C-o', or until the clock is started in a different item."
  1692. :group 'org-progress
  1693. :type 'boolean)
  1694. (defcustom org-clock-in-switch-to-state nil
  1695. "Set task to a special todo state while clocking it.
  1696. The value should be the state to which the entry should be switched."
  1697. :group 'org-progress
  1698. :group 'org-todo
  1699. :type '(choice
  1700. (const :tag "Don't force a state" nil)
  1701. (string :tag "State")))
  1702. (defgroup org-priorities nil
  1703. "Priorities in Org-mode."
  1704. :tag "Org Priorities"
  1705. :group 'org-todo)
  1706. (defcustom org-highest-priority ?A
  1707. "The highest priority of TODO items. A character like ?A, ?B etc.
  1708. Must have a smaller ASCII number than `org-lowest-priority'."
  1709. :group 'org-priorities
  1710. :type 'character)
  1711. (defcustom org-lowest-priority ?C
  1712. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1713. Must have a larger ASCII number than `org-highest-priority'."
  1714. :group 'org-priorities
  1715. :type 'character)
  1716. (defcustom org-default-priority ?B
  1717. "The default priority of TODO items.
  1718. This is the priority an item get if no explicit priority is given."
  1719. :group 'org-priorities
  1720. :type 'character)
  1721. (defcustom org-priority-start-cycle-with-default t
  1722. "Non-nil means, start with default priority when starting to cycle.
  1723. When this is nil, the first step in the cycle will be (depending on the
  1724. command used) one higher or lower that the default priority."
  1725. :group 'org-priorities
  1726. :type 'boolean)
  1727. (defgroup org-time nil
  1728. "Options concerning time stamps and deadlines in Org-mode."
  1729. :tag "Org Time"
  1730. :group 'org)
  1731. (defcustom org-insert-labeled-timestamps-at-point nil
  1732. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1733. When nil, these labeled time stamps are forces into the second line of an
  1734. entry, just after the headline. When scheduling from the global TODO list,
  1735. the time stamp will always be forced into the second line."
  1736. :group 'org-time
  1737. :type 'boolean)
  1738. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1739. "Formats for `format-time-string' which are used for time stamps.
  1740. It is not recommended to change this constant.")
  1741. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1742. "Number of minutes to round time stamps to.
  1743. These are two values, the first applies when first creating a time stamp.
  1744. The second applies when changing it with the commands `S-up' and `S-down'.
  1745. When changing the time stamp, this means that it will change in steps
  1746. of N minues, as given by the second value.
  1747. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1748. numbers should be factors of 60, so for example 5, 10, 15.
  1749. When this is larger than 1, you can still force an exact time-stamp by using
  1750. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1751. and by using a prefix arg to `S-up/down' to specify the exact number
  1752. of minutes to shift."
  1753. :group 'org-time
  1754. :get '(lambda (var) ; Make sure all entries have 5 elements
  1755. (if (integerp (default-value var))
  1756. (list (default-value var) 5)
  1757. (default-value var)))
  1758. :type '(list
  1759. (integer :tag "when inserting times")
  1760. (integer :tag "when modifying times")))
  1761. (defcustom org-display-custom-times nil
  1762. "Non-nil means, overlay custom formats over all time stamps.
  1763. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1764. To turn this on on a per-file basis, insert anywhere in the file:
  1765. #+STARTUP: customtime"
  1766. :group 'org-time
  1767. :set 'set-default
  1768. :type 'sexp)
  1769. (make-variable-buffer-local 'org-display-custom-times)
  1770. (defcustom org-time-stamp-custom-formats
  1771. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1772. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1773. These are overlayed over the default ISO format if the variable
  1774. `org-display-custom-times' is set. Time like %H:%M should be at the
  1775. end of the second format."
  1776. :group 'org-time
  1777. :type 'sexp)
  1778. (defun org-time-stamp-format (&optional long inactive)
  1779. "Get the right format for a time string."
  1780. (let ((f (if long (cdr org-time-stamp-formats)
  1781. (car org-time-stamp-formats))))
  1782. (if inactive
  1783. (concat "[" (substring f 1 -1) "]")
  1784. f)))
  1785. (defcustom org-read-date-prefer-future t
  1786. "Non-nil means, assume future for incomplete date input from user.
  1787. This affects the following situations:
  1788. 1. The user gives a day, but no month.
  1789. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1790. read this as the third of *next* month. However, if you enter \"17\",
  1791. it will be considered as *this* month.
  1792. 2. The user gives a month but not a year.
  1793. For example, if it is april and you enter \"feb 2\", this will be read
  1794. as feb 2, *next* year. \"May 5\", however, will be this year.
  1795. When this option is nil, the current month and year will always be used
  1796. as defaults."
  1797. :group 'org-time
  1798. :type 'boolean)
  1799. (defcustom org-read-date-display-live t
  1800. "Non-nil means, display current interpretation of date prompt live.
  1801. This display will be in an overlay, in the minibuffer."
  1802. :group 'org-time
  1803. :type 'boolean)
  1804. (defcustom org-read-date-popup-calendar t
  1805. "Non-nil means, pop up a calendar when prompting for a date.
  1806. In the calendar, the date can be selected with mouse-1. However, the
  1807. minibuffer will also be active, and you can simply enter the date as well.
  1808. When nil, only the minibuffer will be available."
  1809. :group 'org-time
  1810. :type 'boolean)
  1811. (if (fboundp 'defvaralias)
  1812. (defvaralias 'org-popup-calendar-for-date-prompt
  1813. 'org-read-date-popup-calendar))
  1814. (defcustom org-extend-today-until 0
  1815. "The hour when your day really ends.
  1816. This has influence for the following applications:
  1817. - When switching the agenda to \"today\". It it is still earlier than
  1818. the time given here, the day recognized as TODAY is actually yesterday.
  1819. - When a date is read from the user and it is still before the time given
  1820. here, the current date and time will be assumed to be yesterday, 23:59.
  1821. FIXME:
  1822. IMPORTANT: This is still a very experimental feature, it may disappear
  1823. again or it may be extended to mean more things."
  1824. :group 'org-time
  1825. :type 'number)
  1826. (defcustom org-edit-timestamp-down-means-later nil
  1827. "Non-nil means, S-down will increase the time in a time stamp.
  1828. When nil, S-up will increase."
  1829. :group 'org-time
  1830. :type 'boolean)
  1831. (defcustom org-calendar-follow-timestamp-change t
  1832. "Non-nil means, make the calendar window follow timestamp changes.
  1833. When a timestamp is modified and the calendar window is visible, it will be
  1834. moved to the new date."
  1835. :group 'org-time
  1836. :type 'boolean)
  1837. (defcustom org-clock-heading-function nil
  1838. "When non-nil, should be a function to create `org-clock-heading'.
  1839. This is the string shown in the mode line when a clock is running.
  1840. The function is called with point at the beginning of the headline."
  1841. :group 'org-time ; FIXME: Should we have a separate group????
  1842. :type 'function)
  1843. (defgroup org-tags nil
  1844. "Options concerning tags in Org-mode."
  1845. :tag "Org Tags"
  1846. :group 'org)
  1847. (defcustom org-tag-alist nil
  1848. "List of tags allowed in Org-mode files.
  1849. When this list is nil, Org-mode will base TAG input on what is already in the
  1850. buffer.
  1851. The value of this variable is an alist, the car of each entry must be a
  1852. keyword as a string, the cdr may be a character that is used to select
  1853. that tag through the fast-tag-selection interface.
  1854. See the manual for details."
  1855. :group 'org-tags
  1856. :type '(repeat
  1857. (choice
  1858. (cons (string :tag "Tag name")
  1859. (character :tag "Access char"))
  1860. (const :tag "Start radio group" (:startgroup))
  1861. (const :tag "End radio group" (:endgroup)))))
  1862. (defcustom org-use-fast-tag-selection 'auto
  1863. "Non-nil means, use fast tag selection scheme.
  1864. This is a special interface to select and deselect tags with single keys.
  1865. When nil, fast selection is never used.
  1866. When the symbol `auto', fast selection is used if and only if selection
  1867. characters for tags have been configured, either through the variable
  1868. `org-tag-alist' or through a #+TAGS line in the buffer.
  1869. When t, fast selection is always used and selection keys are assigned
  1870. automatically if necessary."
  1871. :group 'org-tags
  1872. :type '(choice
  1873. (const :tag "Always" t)
  1874. (const :tag "Never" nil)
  1875. (const :tag "When selection characters are configured" 'auto)))
  1876. (defcustom org-fast-tag-selection-single-key nil
  1877. "Non-nil means, fast tag selection exits after first change.
  1878. When nil, you have to press RET to exit it.
  1879. During fast tag selection, you can toggle this flag with `C-c'.
  1880. This variable can also have the value `expert'. In this case, the window
  1881. displaying the tags menu is not even shown, until you press C-c again."
  1882. :group 'org-tags
  1883. :type '(choice
  1884. (const :tag "No" nil)
  1885. (const :tag "Yes" t)
  1886. (const :tag "Expert" expert)))
  1887. (defvar org-fast-tag-selection-include-todo nil
  1888. "Non-nil means, fast tags selection interface will also offer TODO states.
  1889. This is an undocumented feature, you should not rely on it.")
  1890. (defcustom org-tags-column -80
  1891. "The column to which tags should be indented in a headline.
  1892. If this number is positive, it specifies the column. If it is negative,
  1893. it means that the tags should be flushright to that column. For example,
  1894. -80 works well for a normal 80 character screen."
  1895. :group 'org-tags
  1896. :type 'integer)
  1897. (defcustom org-auto-align-tags t
  1898. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1899. These operations change the length of a headline and therefore shift
  1900. the tags around. With this options turned on, after each such operation
  1901. the tags are again aligned to `org-tags-column'."
  1902. :group 'org-tags
  1903. :type 'boolean)
  1904. (defcustom org-use-tag-inheritance t
  1905. "Non-nil means, tags in levels apply also for sublevels.
  1906. When nil, only the tags directly given in a specific line apply there.
  1907. If you turn off this option, you very likely want to turn on the
  1908. companion option `org-tags-match-list-sublevels'."
  1909. :group 'org-tags
  1910. :type 'boolean)
  1911. (defcustom org-tags-match-list-sublevels nil
  1912. "Non-nil means list also sublevels of headlines matching tag search.
  1913. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1914. the sublevels of a headline matching a tag search often also match
  1915. the same search. Listing all of them can create very long lists.
  1916. Setting this variable to nil causes subtrees of a match to be skipped.
  1917. This option is off by default, because inheritance in on. If you turn
  1918. inheritance off, you very likely want to turn this option on.
  1919. As a special case, if the tag search is restricted to TODO items, the
  1920. value of this variable is ignored and sublevels are always checked, to
  1921. make sure all corresponding TODO items find their way into the list."
  1922. :group 'org-tags
  1923. :type 'boolean)
  1924. (defvar org-tags-history nil
  1925. "History of minibuffer reads for tags.")
  1926. (defvar org-last-tags-completion-table nil
  1927. "The last used completion table for tags.")
  1928. (defvar org-after-tags-change-hook nil
  1929. "Hook that is run after the tags in a line have changed.")
  1930. (defgroup org-properties nil
  1931. "Options concerning properties in Org-mode."
  1932. :tag "Org Properties"
  1933. :group 'org)
  1934. (defcustom org-property-format "%-10s %s"
  1935. "How property key/value pairs should be formatted by `indent-line'.
  1936. When `indent-line' hits a property definition, it will format the line
  1937. according to this format, mainly to make sure that the values are
  1938. lined-up with respect to each other."
  1939. :group 'org-properties
  1940. :type 'string)
  1941. (defcustom org-use-property-inheritance nil
  1942. "Non-nil means, properties apply also for sublevels.
  1943. This setting is only relevant during property searches, not when querying
  1944. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1945. you need to call `org-entry-get' with the inheritance flag.
  1946. Turning this on can cause significant overhead when doing a search, so
  1947. this is turned off by default.
  1948. When nil, only the properties directly given in the current entry count.
  1949. The value may also be a list of properties that shouldhave inheritance.
  1950. However, note that some special properties use inheritance under special
  1951. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1952. and the properties ending in \"_ALL\" when they are used as descriptor
  1953. for valid values of a property."
  1954. :group 'org-properties
  1955. :type '(choice
  1956. (const :tag "Not" nil)
  1957. (const :tag "Always" nil)
  1958. (repeat :tag "Specific properties" (string :tag "Property"))))
  1959. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1960. "The default column format, if no other format has been defined.
  1961. This variable can be set on the per-file basis by inserting a line
  1962. #+COLUMNS: %25ITEM ....."
  1963. :group 'org-properties
  1964. :type 'string)
  1965. (defcustom org-global-properties nil
  1966. "List of property/value pairs that can be inherited by any entry.
  1967. You can set buffer-local values for this by adding lines like
  1968. #+PROPERTY: NAME VALUE"
  1969. :group 'org-properties
  1970. :type '(repeat
  1971. (cons (string :tag "Property")
  1972. (string :tag "Value"))))
  1973. (defvar org-local-properties nil
  1974. "List of property/value pairs that can be inherited by any entry.
  1975. Valid for the current buffer.
  1976. This variable is populated from #+PROPERTY lines.")
  1977. (defgroup org-agenda nil
  1978. "Options concerning agenda views in Org-mode."
  1979. :tag "Org Agenda"
  1980. :group 'org)
  1981. (defvar org-category nil
  1982. "Variable used by org files to set a category for agenda display.
  1983. Such files should use a file variable to set it, for example
  1984. # -*- mode: org; org-category: \"ELisp\"
  1985. or contain a special line
  1986. #+CATEGORY: ELisp
  1987. If the file does not specify a category, then file's base name
  1988. is used instead.")
  1989. (make-variable-buffer-local 'org-category)
  1990. (defcustom org-agenda-files nil
  1991. "The files to be used for agenda display.
  1992. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1993. \\[org-remove-file]. You can also use customize to edit the list.
  1994. If an entry is a directory, all files in that directory that are matched by
  1995. `org-agenda-file-regexp' will be part of the file list.
  1996. If the value of the variable is not a list but a single file name, then
  1997. the list of agenda files is actually stored and maintained in that file, one
  1998. agenda file per line."
  1999. :group 'org-agenda
  2000. :type '(choice
  2001. (repeat :tag "List of files and directories" file)
  2002. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2003. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2004. "Regular expression to match files for `org-agenda-files'.
  2005. If any element in the list in that variable contains a directory instead
  2006. of a normal file, all files in that directory that are matched by this
  2007. regular expression will be included."
  2008. :group 'org-agenda
  2009. :type 'regexp)
  2010. (defcustom org-agenda-skip-unavailable-files nil
  2011. "t means to just skip non-reachable files in `org-agenda-files'.
  2012. Nil means to remove them, after a query, from the list."
  2013. :group 'org-agenda
  2014. :type 'boolean)
  2015. (defcustom org-agenda-text-search-extra-files nil
  2016. "List of extra files to be searched by text search commands.
  2017. These files will be search in addition to the agenda files bu the
  2018. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2019. Note that these files will only be searched for text search commands,
  2020. not for the other agenda views like todo lists, tag earches or the weekly
  2021. agenda. This variable is intended to list notes and possibly archive files
  2022. that should also be searched by these two commands."
  2023. :group 'org-agenda
  2024. :type '(repeat file))
  2025. (if (fboundp 'defvaralias)
  2026. (defvaralias 'org-agenda-multi-occur-extra-files
  2027. 'org-agenda-text-search-extra-files))
  2028. (defcustom org-agenda-confirm-kill 1
  2029. "When set, remote killing from the agenda buffer needs confirmation.
  2030. When t, a confirmation is always needed. When a number N, confirmation is
  2031. only needed when the text to be killed contains more than N non-white lines."
  2032. :group 'org-agenda
  2033. :type '(choice
  2034. (const :tag "Never" nil)
  2035. (const :tag "Always" t)
  2036. (number :tag "When more than N lines")))
  2037. (defcustom org-calendar-to-agenda-key [?c]
  2038. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2039. The command `org-calendar-goto-agenda' will be bound to this key. The
  2040. default is the character `c' because then `c' can be used to switch back and
  2041. forth between agenda and calendar."
  2042. :group 'org-agenda
  2043. :type 'sexp)
  2044. (defcustom org-agenda-compact-blocks nil
  2045. "Non-nil means, make the block agenda more compact.
  2046. This is done by leaving out unnecessary lines."
  2047. :group 'org-agenda
  2048. :type nil)
  2049. (defgroup org-agenda-export nil
  2050. "Options concerning exporting agenda views in Org-mode."
  2051. :tag "Org Agenda Export"
  2052. :group 'org-agenda)
  2053. (defcustom org-agenda-with-colors t
  2054. "Non-nil means, use colors in agenda views."
  2055. :group 'org-agenda-export
  2056. :type 'boolean)
  2057. (defcustom org-agenda-exporter-settings nil
  2058. "Alist of variable/value pairs that should be active during agenda export.
  2059. This is a good place to set uptions for ps-print and for htmlize."
  2060. :group 'org-agenda-export
  2061. :type '(repeat
  2062. (list
  2063. (variable)
  2064. (sexp :tag "Value"))))
  2065. (defcustom org-agenda-export-html-style ""
  2066. "The style specification for exported HTML Agenda files.
  2067. If this variable contains a string, it will replace the default <style>
  2068. section as produced by `htmlize'.
  2069. Since there are different ways of setting style information, this variable
  2070. needs to contain the full HTML structure to provide a style, including the
  2071. surrounding HTML tags. The style specifications should include definitions
  2072. the fonts used by the agenda, here is an example:
  2073. <style type=\"text/css\">
  2074. p { font-weight: normal; color: gray; }
  2075. .org-agenda-structure {
  2076. font-size: 110%;
  2077. color: #003399;
  2078. font-weight: 600;
  2079. }
  2080. .org-todo {
  2081. color: #cc6666;Week-agenda:
  2082. font-weight: bold;
  2083. }
  2084. .org-done {
  2085. color: #339933;
  2086. }
  2087. .title { text-align: center; }
  2088. .todo, .deadline { color: red; }
  2089. .done { color: green; }
  2090. </style>
  2091. or, if you want to keep the style in a file,
  2092. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2093. As the value of this option simply gets inserted into the HTML <head> header,
  2094. you can \"misuse\" it to also add other text to the header. However,
  2095. <style>...</style> is required, if not present the variable will be ignored."
  2096. :group 'org-agenda-export
  2097. :group 'org-export-html
  2098. :type 'string)
  2099. (defgroup org-agenda-custom-commands nil
  2100. "Options concerning agenda views in Org-mode."
  2101. :tag "Org Agenda Custom Commands"
  2102. :group 'org-agenda)
  2103. (defconst org-sorting-choice
  2104. '(choice
  2105. (const time-up) (const time-down)
  2106. (const category-keep) (const category-up) (const category-down)
  2107. (const tag-down) (const tag-up)
  2108. (const priority-up) (const priority-down))
  2109. "Sorting choices.")
  2110. (defconst org-agenda-custom-commands-local-options
  2111. `(repeat :tag "Local settings for this command. Remember to quote values"
  2112. (choice :tag "Setting"
  2113. (list :tag "Any variable"
  2114. (variable :tag "Variable")
  2115. (sexp :tag "Value"))
  2116. (list :tag "Files to be searched"
  2117. (const org-agenda-files)
  2118. (list
  2119. (const :format "" quote)
  2120. (repeat
  2121. (file))))
  2122. (list :tag "Sorting strategy"
  2123. (const org-agenda-sorting-strategy)
  2124. (list
  2125. (const :format "" quote)
  2126. (repeat
  2127. ,org-sorting-choice)))
  2128. (list :tag "Prefix format"
  2129. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  2130. (string))
  2131. (list :tag "Number of days in agenda"
  2132. (const org-agenda-ndays)
  2133. (integer :value 1))
  2134. (list :tag "Fixed starting date"
  2135. (const org-agenda-start-day)
  2136. (string :value "2007-11-01"))
  2137. (list :tag "Start on day of week"
  2138. (const org-agenda-start-on-weekday)
  2139. (choice :value 1
  2140. (const :tag "Today" nil)
  2141. (number :tag "Weekday No.")))
  2142. (list :tag "Include data from diary"
  2143. (const org-agenda-include-diary)
  2144. (boolean))
  2145. (list :tag "Deadline Warning days"
  2146. (const org-deadline-warning-days)
  2147. (integer :value 1))
  2148. (list :tag "Standard skipping condition"
  2149. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  2150. (const org-agenda-skip-function)
  2151. (list
  2152. (const :format "" quote)
  2153. (list
  2154. (choice
  2155. :tag "Skiping range"
  2156. (const :tag "Skip entry" org-agenda-skip-entry-if)
  2157. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  2158. (repeat :inline t :tag "Conditions for skipping"
  2159. (choice
  2160. :tag "Condition type"
  2161. (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
  2162. (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
  2163. (const :tag "scheduled" 'scheduled)
  2164. (const :tag "not scheduled" 'notscheduled)
  2165. (const :tag "deadline" 'deadline)
  2166. (const :tag "no deadline" 'notdeadline))))))
  2167. (list :tag "Non-standard skipping condition"
  2168. :value (org-agenda-skip-function)
  2169. (list
  2170. (const org-agenda-skip-function)
  2171. (sexp :tag "Function or form (quoted!)")))))
  2172. "Selection of examples for agenda command settings.
  2173. This will be spliced into the custom type of
  2174. `org-agenda-custom-commands'.")
  2175. (defcustom org-agenda-custom-commands nil
  2176. "Custom commands for the agenda.
  2177. These commands will be offered on the splash screen displayed by the
  2178. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2179. (key desc type match settings files)
  2180. key The key (one or more characters as a string) to be associated
  2181. with the command.
  2182. desc A description of the command, when omitted or nil, a default
  2183. description is built using MATCH.
  2184. type The command type, any of the following symbols:
  2185. agenda The daily/weekly agenda.
  2186. todo Entries with a specific TODO keyword, in all agenda files.
  2187. search Entries containing search words entry or headline.
  2188. tags Tags/Property/TODO match in all agenda files.
  2189. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2190. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2191. tags-tree Sparse tree with all tags matches in *current* file.
  2192. occur-tree Occur sparse tree for *current* file.
  2193. ... A user-defined function.
  2194. match What to search for:
  2195. - a single keyword for TODO keyword searches
  2196. - a tags match expression for tags searches
  2197. - a word search expression for text searches.
  2198. - a regular expression for occur searches
  2199. For all other commands, this should be the empty string.
  2200. settings A list of option settings, similar to that in a let form, so like
  2201. this: ((opt1 val1) (opt2 val2) ...). The values will be
  2202. evaluated at the moment of execution, so quote them when needed.
  2203. files A list of files file to write the produced agenda buffer to
  2204. with the command `org-store-agenda-views'.
  2205. If a file name ends in \".html\", an HTML version of the buffer
  2206. is written out. If it ends in \".ps\", a postscript version is
  2207. produced. Otherwide, only the plain text is written to the file.
  2208. You can also define a set of commands, to create a composite agenda buffer.
  2209. In this case, an entry looks like this:
  2210. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  2211. where
  2212. desc A description string to be displayed in the dispatcher menu.
  2213. cmd An agenda command, similar to the above. However, tree commands
  2214. are no allowed, but instead you can get agenda and global todo list.
  2215. So valid commands for a set are:
  2216. (agenda \"\" settings)
  2217. (alltodo \"\" settings)
  2218. (stuck \"\" settings)
  2219. (todo \"match\" settings files)
  2220. (search \"match\" settings files)
  2221. (tags \"match\" settings files)
  2222. (tags-todo \"match\" settings files)
  2223. Each command can carry a list of options, and another set of options can be
  2224. given for the whole set of commands. Individual command options take
  2225. precedence over the general options.
  2226. When using several characters as key to a command, the first characters
  2227. are prefix commands. For the dispatcher to display useful information, you
  2228. should provide a description for the prefix, like
  2229. (setq org-agenda-custom-commands
  2230. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2231. (\"hl\" tags \"+HOME+Lisa\")
  2232. (\"hp\" tags \"+HOME+Peter\")
  2233. (\"hk\" tags \"+HOME+Kim\")))"
  2234. :group 'org-agenda-custom-commands
  2235. :type `(repeat
  2236. (choice :value ("x" "Describe command here" tags "" nil)
  2237. (list :tag "Single command"
  2238. (string :tag "Access Key(s) ")
  2239. (option (string :tag "Description"))
  2240. (choice
  2241. (const :tag "Agenda" agenda)
  2242. (const :tag "TODO list" alltodo)
  2243. (const :tag "Search words" search)
  2244. (const :tag "Stuck projects" stuck)
  2245. (const :tag "Tags search (all agenda files)" tags)
  2246. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2247. (const :tag "TODO keyword search (all agenda files)" todo)
  2248. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2249. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2250. (const :tag "Occur tree (current buffer)" occur-tree)
  2251. (sexp :tag "Other, user-defined function"))
  2252. (string :tag "Match (only for some commands)")
  2253. ,org-agenda-custom-commands-local-options
  2254. (option (repeat :tag "Export" (file :tag "Export to"))))
  2255. (list :tag "Command series, all agenda files"
  2256. (string :tag "Access Key(s)")
  2257. (string :tag "Description ")
  2258. (repeat :tag "Component"
  2259. (choice
  2260. (list :tag "Agenda"
  2261. (const :format "" agenda)
  2262. (const :tag "" :format "" "")
  2263. ,org-agenda-custom-commands-local-options)
  2264. (list :tag "TODO list (all keywords)"
  2265. (const :format "" alltodo)
  2266. (const :tag "" :format "" "")
  2267. ,org-agenda-custom-commands-local-options)
  2268. (list :tag "Search words"
  2269. (const :format "" search)
  2270. (string :tag "Match")
  2271. ,org-agenda-custom-commands-local-options)
  2272. (list :tag "Stuck projects"
  2273. (const :format "" stuck)
  2274. (const :tag "" :format "" "")
  2275. ,org-agenda-custom-commands-local-options)
  2276. (list :tag "Tags search"
  2277. (const :format "" tags)
  2278. (string :tag "Match")
  2279. ,org-agenda-custom-commands-local-options)
  2280. (list :tag "Tags search, TODO entries only"
  2281. (const :format "" tags-todo)
  2282. (string :tag "Match")
  2283. ,org-agenda-custom-commands-local-options)
  2284. (list :tag "TODO keyword search"
  2285. (const :format "" todo)
  2286. (string :tag "Match")
  2287. ,org-agenda-custom-commands-local-options)
  2288. (list :tag "Other, user-defined function"
  2289. (symbol :tag "function")
  2290. (string :tag "Match")
  2291. ,org-agenda-custom-commands-local-options)))
  2292. (repeat :tag "Settings for entire command set"
  2293. (list (variable :tag "Any variable")
  2294. (sexp :tag "Value")))
  2295. (option (repeat :tag "Export" (file :tag "Export to"))))
  2296. (cons :tag "Prefix key documentation"
  2297. (string :tag "Access Key(s)")
  2298. (string :tag "Description ")))))
  2299. (defcustom org-agenda-query-register ?o
  2300. "The register holding the current query string.
  2301. The prupose of this is that if you construct a query string interactively,
  2302. you can then use it to define a custom command."
  2303. :group 'org-agenda-custom-commands
  2304. :type 'character)
  2305. (defcustom org-stuck-projects
  2306. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2307. "How to identify stuck projects.
  2308. This is a list of four items:
  2309. 1. A tags/todo matcher string that is used to identify a project.
  2310. The entire tree below a headline matched by this is considered one project.
  2311. 2. A list of TODO keywords identifying non-stuck projects.
  2312. If the project subtree contains any headline with one of these todo
  2313. keywords, the project is considered to be not stuck. If you specify
  2314. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2315. 3. A list of tags identifying non-stuck projects.
  2316. If the project subtree contains any headline with one of these tags,
  2317. the project is considered to be not stuck. If you specify \"*\" as
  2318. a tag, any tag will mark the project unstuck.
  2319. 4. An arbitrary regular expression matching non-stuck projects.
  2320. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2321. or `C-c a #' to produce the list."
  2322. :group 'org-agenda-custom-commands
  2323. :type '(list
  2324. (string :tag "Tags/TODO match to identify a project")
  2325. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2326. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2327. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2328. (defgroup org-agenda-skip nil
  2329. "Options concerning skipping parts of agenda files."
  2330. :tag "Org Agenda Skip"
  2331. :group 'org-agenda)
  2332. (defcustom org-agenda-todo-list-sublevels t
  2333. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2334. When nil, the sublevels of a TODO entry are not checked, resulting in
  2335. potentially much shorter TODO lists."
  2336. :group 'org-agenda-skip
  2337. :group 'org-todo
  2338. :type 'boolean)
  2339. (defcustom org-agenda-todo-ignore-with-date nil
  2340. "Non-nil means, don't show entries with a date in the global todo list.
  2341. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2342. but don't want them to show up in the TODO list.
  2343. When this is set, it also covers deadlines and scheduled items, the settings
  2344. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2345. will be ignored."
  2346. :group 'org-agenda-skip
  2347. :group 'org-todo
  2348. :type 'boolean)
  2349. (defcustom org-agenda-todo-ignore-scheduled nil
  2350. "Non-nil means, don't show scheduled entries in the global todo list.
  2351. The idea behind this is that by scheduling it, you have already taken care
  2352. of this item.
  2353. See also `org-agenda-todo-ignore-with-date'."
  2354. :group 'org-agenda-skip
  2355. :group 'org-todo
  2356. :type 'boolean)
  2357. (defcustom org-agenda-todo-ignore-deadlines nil
  2358. "Non-nil means, don't show near deadline entries in the global todo list.
  2359. Near means closer than `org-deadline-warning-days' days.
  2360. The idea behind this is that such items will appear in the agenda anyway.
  2361. See also `org-agenda-todo-ignore-with-date'."
  2362. :group 'org-agenda-skip
  2363. :group 'org-todo
  2364. :type 'boolean)
  2365. (defcustom org-agenda-skip-scheduled-if-done nil
  2366. "Non-nil means don't show scheduled items in agenda when they are done.
  2367. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2368. it applies only to the actual date of the scheduling. Warnings about
  2369. an item with a past scheduling dates are always turned off when the item
  2370. is DONE."
  2371. :group 'org-agenda-skip
  2372. :type 'boolean)
  2373. (defcustom org-agenda-skip-deadline-if-done nil
  2374. "Non-nil means don't show deadines when the corresponding item is done.
  2375. When nil, the deadline is still shown and should give you a happy feeling.
  2376. This is relevant for the daily/weekly agenda. And it applied only to the
  2377. actualy date of the deadline. Warnings about approching and past-due
  2378. deadlines are always turned off when the item is DONE."
  2379. :group 'org-agenda-skip
  2380. :type 'boolean)
  2381. (defcustom org-agenda-skip-timestamp-if-done nil
  2382. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2383. :group 'org-agenda-skip
  2384. :type 'boolean)
  2385. (defcustom org-timeline-show-empty-dates 3
  2386. "Non-nil means, `org-timeline' also shows dates without an entry.
  2387. When nil, only the days which actually have entries are shown.
  2388. When t, all days between the first and the last date are shown.
  2389. When an integer, show also empty dates, but if there is a gap of more than
  2390. N days, just insert a special line indicating the size of the gap."
  2391. :group 'org-agenda-skip
  2392. :type '(choice
  2393. (const :tag "None" nil)
  2394. (const :tag "All" t)
  2395. (number :tag "at most")))
  2396. (defgroup org-agenda-startup nil
  2397. "Options concerning initial settings in the Agenda in Org Mode."
  2398. :tag "Org Agenda Startup"
  2399. :group 'org-agenda)
  2400. (defcustom org-finalize-agenda-hook nil
  2401. "Hook run just before displaying an agenda buffer."
  2402. :group 'org-agenda-startup
  2403. :type 'hook)
  2404. (defcustom org-agenda-mouse-1-follows-link nil
  2405. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2406. A longer mouse click will still set point. Does not work on XEmacs.
  2407. Needs to be set before org.el is loaded."
  2408. :group 'org-agenda-startup
  2409. :type 'boolean)
  2410. (defcustom org-agenda-start-with-follow-mode nil
  2411. "The initial value of follow-mode in a newly created agenda window."
  2412. :group 'org-agenda-startup
  2413. :type 'boolean)
  2414. (defgroup org-agenda-windows nil
  2415. "Options concerning the windows used by the Agenda in Org Mode."
  2416. :tag "Org Agenda Windows"
  2417. :group 'org-agenda)
  2418. (defcustom org-agenda-window-setup 'reorganize-frame
  2419. "How the agenda buffer should be displayed.
  2420. Possible values for this option are:
  2421. current-window Show agenda in the current window, keeping all other windows.
  2422. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2423. other-window Use `switch-to-buffer-other-window' to display agenda.
  2424. reorganize-frame Show only two windows on the current frame, the current
  2425. window and the agenda.
  2426. See also the variable `org-agenda-restore-windows-after-quit'."
  2427. :group 'org-agenda-windows
  2428. :type '(choice
  2429. (const current-window)
  2430. (const other-frame)
  2431. (const other-window)
  2432. (const reorganize-frame)))
  2433. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2434. "The min and max height of the agenda window as a fraction of frame height.
  2435. The value of the variable is a cons cell with two numbers between 0 and 1.
  2436. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2437. :group 'org-agenda-windows
  2438. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2439. (defcustom org-agenda-restore-windows-after-quit nil
  2440. "Non-nil means, restore window configuration open exiting agenda.
  2441. Before the window configuration is changed for displaying the agenda,
  2442. the current status is recorded. When the agenda is exited with
  2443. `q' or `x' and this option is set, the old state is restored. If
  2444. `org-agenda-window-setup' is `other-frame', the value of this
  2445. option will be ignored.."
  2446. :group 'org-agenda-windows
  2447. :type 'boolean)
  2448. (defcustom org-indirect-buffer-display 'other-window
  2449. "How should indirect tree buffers be displayed?
  2450. This applies to indirect buffers created with the commands
  2451. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2452. Valid values are:
  2453. current-window Display in the current window
  2454. other-window Just display in another window.
  2455. dedicated-frame Create one new frame, and re-use it each time.
  2456. new-frame Make a new frame each time. Note that in this case
  2457. previously-made indirect buffers are kept, and you need to
  2458. kill these buffers yourself."
  2459. :group 'org-structure
  2460. :group 'org-agenda-windows
  2461. :type '(choice
  2462. (const :tag "In current window" current-window)
  2463. (const :tag "In current frame, other window" other-window)
  2464. (const :tag "Each time a new frame" new-frame)
  2465. (const :tag "One dedicated frame" dedicated-frame)))
  2466. (defgroup org-agenda-daily/weekly nil
  2467. "Options concerning the daily/weekly agenda."
  2468. :tag "Org Agenda Daily/Weekly"
  2469. :group 'org-agenda)
  2470. (defcustom org-agenda-ndays 7
  2471. "Number of days to include in overview display.
  2472. Should be 1 or 7."
  2473. :group 'org-agenda-daily/weekly
  2474. :type 'number)
  2475. (defcustom org-agenda-start-on-weekday 1
  2476. "Non-nil means, start the overview always on the specified weekday.
  2477. 0 denotes Sunday, 1 denotes Monday etc.
  2478. When nil, always start on the current day."
  2479. :group 'org-agenda-daily/weekly
  2480. :type '(choice (const :tag "Today" nil)
  2481. (number :tag "Weekday No.")))
  2482. (defcustom org-agenda-show-all-dates t
  2483. "Non-nil means, `org-agenda' shows every day in the selected range.
  2484. When nil, only the days which actually have entries are shown."
  2485. :group 'org-agenda-daily/weekly
  2486. :type 'boolean)
  2487. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2488. "Format string for displaying dates in the agenda.
  2489. Used by the daily/weekly agenda and by the timeline. This should be
  2490. a format string understood by `format-time-string', or a function returning
  2491. the formatted date as a string. The function must take a single argument,
  2492. a calendar-style date list like (month day year)."
  2493. :group 'org-agenda-daily/weekly
  2494. :type '(choice
  2495. (string :tag "Format string")
  2496. (function :tag "Function")))
  2497. (defun org-agenda-format-date-aligned (date)
  2498. "Format a date string for display in the daily/weekly agenda, or timeline.
  2499. This function makes sure that dates are aligned for easy reading."
  2500. (format "%-9s %2d %s %4d"
  2501. (calendar-day-name date)
  2502. (extract-calendar-day date)
  2503. (calendar-month-name (extract-calendar-month date))
  2504. (extract-calendar-year date)))
  2505. (defcustom org-agenda-include-diary nil
  2506. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2507. :group 'org-agenda-daily/weekly
  2508. :type 'boolean)
  2509. (defcustom org-agenda-include-all-todo nil
  2510. "Set means weekly/daily agenda will always contain all TODO entries.
  2511. The TODO entries will be listed at the top of the agenda, before
  2512. the entries for specific days."
  2513. :group 'org-agenda-daily/weekly
  2514. :type 'boolean)
  2515. (defcustom org-agenda-repeating-timestamp-show-all t
  2516. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2517. When nil, only one occurence is shown, either today or the
  2518. nearest into the future."
  2519. :group 'org-agenda-daily/weekly
  2520. :type 'boolean)
  2521. (defcustom org-deadline-warning-days 14
  2522. "No. of days before expiration during which a deadline becomes active.
  2523. This variable governs the display in sparse trees and in the agenda.
  2524. When 0 or negative, it means use this number (the absolute value of it)
  2525. even if a deadline has a different individual lead time specified."
  2526. :group 'org-time
  2527. :group 'org-agenda-daily/weekly
  2528. :type 'number)
  2529. (defcustom org-scheduled-past-days 10000
  2530. "No. of days to continue listing scheduled items that are not marked DONE.
  2531. When an item is scheduled on a date, it shows up in the agenda on this
  2532. day and will be listed until it is marked done for the number of days
  2533. given here."
  2534. :group 'org-agenda-daily/weekly
  2535. :type 'number)
  2536. (defgroup org-agenda-time-grid nil
  2537. "Options concerning the time grid in the Org-mode Agenda."
  2538. :tag "Org Agenda Time Grid"
  2539. :group 'org-agenda)
  2540. (defcustom org-agenda-use-time-grid t
  2541. "Non-nil means, show a time grid in the agenda schedule.
  2542. A time grid is a set of lines for specific times (like every two hours between
  2543. 8:00 and 20:00). The items scheduled for a day at specific times are
  2544. sorted in between these lines.
  2545. For details about when the grid will be shown, and what it will look like, see
  2546. the variable `org-agenda-time-grid'."
  2547. :group 'org-agenda-time-grid
  2548. :type 'boolean)
  2549. (defcustom org-agenda-time-grid
  2550. '((daily today require-timed)
  2551. "----------------"
  2552. (800 1000 1200 1400 1600 1800 2000))
  2553. "The settings for time grid for agenda display.
  2554. This is a list of three items. The first item is again a list. It contains
  2555. symbols specifying conditions when the grid should be displayed:
  2556. daily if the agenda shows a single day
  2557. weekly if the agenda shows an entire week
  2558. today show grid on current date, independent of daily/weekly display
  2559. require-timed show grid only if at least one item has a time specification
  2560. The second item is a string which will be places behing the grid time.
  2561. The third item is a list of integers, indicating the times that should have
  2562. a grid line."
  2563. :group 'org-agenda-time-grid
  2564. :type
  2565. '(list
  2566. (set :greedy t :tag "Grid Display Options"
  2567. (const :tag "Show grid in single day agenda display" daily)
  2568. (const :tag "Show grid in weekly agenda display" weekly)
  2569. (const :tag "Always show grid for today" today)
  2570. (const :tag "Show grid only if any timed entries are present"
  2571. require-timed)
  2572. (const :tag "Skip grid times already present in an entry"
  2573. remove-match))
  2574. (string :tag "Grid String")
  2575. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2576. (defgroup org-agenda-sorting nil
  2577. "Options concerning sorting in the Org-mode Agenda."
  2578. :tag "Org Agenda Sorting"
  2579. :group 'org-agenda)
  2580. (defcustom org-agenda-sorting-strategy
  2581. '((agenda time-up category-keep priority-down)
  2582. (todo category-keep priority-down)
  2583. (tags category-keep priority-down)
  2584. (search category-keep))
  2585. "Sorting structure for the agenda items of a single day.
  2586. This is a list of symbols which will be used in sequence to determine
  2587. if an entry should be listed before another entry. The following
  2588. symbols are recognized:
  2589. time-up Put entries with time-of-day indications first, early first
  2590. time-down Put entries with time-of-day indications first, late first
  2591. category-keep Keep the default order of categories, corresponding to the
  2592. sequence in `org-agenda-files'.
  2593. category-up Sort alphabetically by category, A-Z.
  2594. category-down Sort alphabetically by category, Z-A.
  2595. tag-up Sort alphabetically by last tag, A-Z.
  2596. tag-down Sort alphabetically by last tag, Z-A.
  2597. priority-up Sort numerically by priority, high priority last.
  2598. priority-down Sort numerically by priority, high priority first.
  2599. The different possibilities will be tried in sequence, and testing stops
  2600. if one comparison returns a \"not-equal\". For example, the default
  2601. '(time-up category-keep priority-down)
  2602. means: Pull out all entries having a specified time of day and sort them,
  2603. in order to make a time schedule for the current day the first thing in the
  2604. agenda listing for the day. Of the entries without a time indication, keep
  2605. the grouped in categories, don't sort the categories, but keep them in
  2606. the sequence given in `org-agenda-files'. Within each category sort by
  2607. priority.
  2608. Leaving out `category-keep' would mean that items will be sorted across
  2609. categories by priority.
  2610. Instead of a single list, this can also be a set of list for specific
  2611. contents, with a context symbol in the car of the list, any of
  2612. `agenda', `todo', `tags' for the corresponding agenda views."
  2613. :group 'org-agenda-sorting
  2614. :type `(choice
  2615. (repeat :tag "General" ,org-sorting-choice)
  2616. (list :tag "Individually"
  2617. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2618. (repeat ,org-sorting-choice))
  2619. (cons (const :tag "Strategy for TODO lists" todo)
  2620. (repeat ,org-sorting-choice))
  2621. (cons (const :tag "Strategy for Tags matches" tags)
  2622. (repeat ,org-sorting-choice)))))
  2623. (defcustom org-sort-agenda-notime-is-late t
  2624. "Non-nil means, items without time are considered late.
  2625. This is only relevant for sorting. When t, items which have no explicit
  2626. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2627. do have a time. When nil, the default time is before 0:00. You can use this
  2628. option to decide if the schedule for today should come before or after timeless
  2629. agenda entries."
  2630. :group 'org-agenda-sorting
  2631. :type 'boolean)
  2632. (defgroup org-agenda-line-format nil
  2633. "Options concerning the entry prefix in the Org-mode agenda display."
  2634. :tag "Org Agenda Line Format"
  2635. :group 'org-agenda)
  2636. (defcustom org-agenda-prefix-format
  2637. '((agenda . " %-12:c%?-12t% s")
  2638. (timeline . " % s")
  2639. (todo . " %-12:c")
  2640. (tags . " %-12:c")
  2641. (search . " %-12:c"))
  2642. "Format specifications for the prefix of items in the agenda views.
  2643. An alist with four entries, for the different agenda types. The keys to the
  2644. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2645. are format strings.
  2646. This format works similar to a printf format, with the following meaning:
  2647. %c the category of the item, \"Diary\" for entries from the diary, or
  2648. as given by the CATEGORY keyword or derived from the file name.
  2649. %T the *last* tag of the item. Last because inherited tags come
  2650. first in the list.
  2651. %t the time-of-day specification if one applies to the entry, in the
  2652. format HH:MM
  2653. %s Scheduling/Deadline information, a short string
  2654. All specifiers work basically like the standard `%s' of printf, but may
  2655. contain two additional characters: A question mark just after the `%' and
  2656. a whitespace/punctuation character just before the final letter.
  2657. If the first character after `%' is a question mark, the entire field
  2658. will only be included if the corresponding value applies to the
  2659. current entry. This is useful for fields which should have fixed
  2660. width when present, but zero width when absent. For example,
  2661. \"%?-12t\" will result in a 12 character time field if a time of the
  2662. day is specified, but will completely disappear in entries which do
  2663. not contain a time.
  2664. If there is punctuation or whitespace character just before the final
  2665. format letter, this character will be appended to the field value if
  2666. the value is not empty. For example, the format \"%-12:c\" leads to
  2667. \"Diary: \" if the category is \"Diary\". If the category were be
  2668. empty, no additional colon would be interted.
  2669. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2670. - Indent the line with two space characters
  2671. - Give the category in a 12 chars wide field, padded with whitespace on
  2672. the right (because of `-'). Append a colon if there is a category
  2673. (because of `:').
  2674. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2675. time, don't put in an empty field, just skip it (because of '?').
  2676. - Finally, put the scheduling information and append a whitespace.
  2677. As another example, if you don't want the time-of-day of entries in
  2678. the prefix, you could use:
  2679. (setq org-agenda-prefix-format \" %-11:c% s\")
  2680. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2681. `org-agenda-remove-tags'."
  2682. :type '(choice
  2683. (string :tag "General format")
  2684. (list :greedy t :tag "View dependent"
  2685. (cons (const agenda) (string :tag "Format"))
  2686. (cons (const timeline) (string :tag "Format"))
  2687. (cons (const todo) (string :tag "Format"))
  2688. (cons (const tags) (string :tag "Format"))
  2689. (cons (const search) (string :tag "Format"))))
  2690. :group 'org-agenda-line-format)
  2691. (defvar org-prefix-format-compiled nil
  2692. "The compiled version of the most recently used prefix format.
  2693. See the variable `org-agenda-prefix-format'.")
  2694. (defcustom org-agenda-todo-keyword-format "%-1s"
  2695. "Format for the TODO keyword in agenda lines.
  2696. Set this to something like \"%-12s\" if you want all TODO keywords
  2697. to occupy a fixed space in the agenda display."
  2698. :group 'org-agenda-line-format
  2699. :type 'string)
  2700. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2701. "Text preceeding scheduled items in the agenda view.
  2702. This is a list with two strings. The first applies when the item is
  2703. scheduled on the current day. The second applies when it has been scheduled
  2704. previously, it may contain a %d to capture how many days ago the item was
  2705. scheduled."
  2706. :group 'org-agenda-line-format
  2707. :type '(list
  2708. (string :tag "Scheduled today ")
  2709. (string :tag "Scheduled previously")))
  2710. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2711. "Text preceeding deadline items in the agenda view.
  2712. This is a list with two strings. The first applies when the item has its
  2713. deadline on the current day. The second applies when it is in the past or
  2714. in the future, it may contain %d to capture how many days away the deadline
  2715. is (was)."
  2716. :group 'org-agenda-line-format
  2717. :type '(list
  2718. (string :tag "Deadline today ")
  2719. (string :tag "Deadline relative")))
  2720. (defcustom org-agenda-remove-times-when-in-prefix t
  2721. "Non-nil means, remove duplicate time specifications in agenda items.
  2722. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2723. time-of-day specification in a headline or diary entry is extracted and
  2724. placed into the prefix. If this option is non-nil, the original specification
  2725. \(a timestamp or -range, or just a plain time(range) specification like
  2726. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2727. cluttered.
  2728. The option can be t or nil. It may also be the symbol `beg', indicating
  2729. that the time should only be removed what it is located at the beginning of
  2730. the headline/diary entry."
  2731. :group 'org-agenda-line-format
  2732. :type '(choice
  2733. (const :tag "Always" t)
  2734. (const :tag "Never" nil)
  2735. (const :tag "When at beginning of entry" beg)))
  2736. (defcustom org-agenda-default-appointment-duration nil
  2737. "Default duration for appointments that only have a starting time.
  2738. When nil, no duration is specified in such cases.
  2739. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2740. :group 'org-agenda-line-format
  2741. :type '(choice
  2742. (integer :tag "Minutes")
  2743. (const :tag "No default duration")))
  2744. (defcustom org-agenda-remove-tags nil
  2745. "Non-nil means, remove the tags from the headline copy in the agenda.
  2746. When this is the symbol `prefix', only remove tags when
  2747. `org-agenda-prefix-format' contains a `%T' specifier."
  2748. :group 'org-agenda-line-format
  2749. :type '(choice
  2750. (const :tag "Always" t)
  2751. (const :tag "Never" nil)
  2752. (const :tag "When prefix format contains %T" prefix)))
  2753. (if (fboundp 'defvaralias)
  2754. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2755. 'org-agenda-remove-tags))
  2756. (defcustom org-agenda-tags-column -80
  2757. "Shift tags in agenda items to this column.
  2758. If this number is positive, it specifies the column. If it is negative,
  2759. it means that the tags should be flushright to that column. For example,
  2760. -80 works well for a normal 80 character screen."
  2761. :group 'org-agenda-line-format
  2762. :type 'integer)
  2763. (if (fboundp 'defvaralias)
  2764. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2765. (defcustom org-agenda-fontify-priorities t
  2766. "Non-nil means, highlight low and high priorities in agenda.
  2767. When t, the highest priority entries are bold, lowest priority italic.
  2768. This may also be an association list of priority faces. The face may be
  2769. a names face, or a list like `(:background \"Red\")'."
  2770. :group 'org-agenda-line-format
  2771. :type '(choice
  2772. (const :tag "Never" nil)
  2773. (const :tag "Defaults" t)
  2774. (repeat :tag "Specify"
  2775. (list (character :tag "Priority" :value ?A)
  2776. (sexp :tag "face")))))
  2777. (defgroup org-latex nil
  2778. "Options for embedding LaTeX code into Org-mode"
  2779. :tag "Org LaTeX"
  2780. :group 'org)
  2781. (defcustom org-format-latex-options
  2782. '(:foreground default :background default :scale 1.0
  2783. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2784. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2785. "Options for creating images from LaTeX fragments.
  2786. This is a property list with the following properties:
  2787. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2788. `default' means use the forground of the default face.
  2789. :background the background color, or \"Transparent\".
  2790. `default' means use the background of the default face.
  2791. :scale a scaling factor for the size of the images
  2792. :html-foreground, :html-background, :html-scale
  2793. The same numbers for HTML export.
  2794. :matchers a list indicating which matchers should be used to
  2795. find LaTeX fragments. Valid members of this list are:
  2796. \"begin\" find environments
  2797. \"$\" find math expressions surrounded by $...$
  2798. \"$$\" find math expressions surrounded by $$....$$
  2799. \"\\(\" find math expressions surrounded by \\(...\\)
  2800. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2801. :group 'org-latex
  2802. :type 'plist)
  2803. (defcustom org-format-latex-header "\\documentclass{article}
  2804. \\usepackage{fullpage} % do not remove
  2805. \\usepackage{amssymb}
  2806. \\usepackage[usenames]{color}
  2807. \\usepackage{amsmath}
  2808. \\usepackage{latexsym}
  2809. \\usepackage[mathscr]{eucal}
  2810. \\pagestyle{empty} % do not remove"
  2811. "The document header used for processing LaTeX fragments."
  2812. :group 'org-latex
  2813. :type 'string)
  2814. (defgroup org-export nil
  2815. "Options for exporting org-listings."
  2816. :tag "Org Export"
  2817. :group 'org)
  2818. (defgroup org-export-general nil
  2819. "General options for exporting Org-mode files."
  2820. :tag "Org Export General"
  2821. :group 'org-export)
  2822. ;; FIXME
  2823. (defvar org-export-publishing-directory nil)
  2824. (defcustom org-export-with-special-strings t
  2825. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2826. When this option is turned on, these strings will be exported as:
  2827. Org HTML LaTeX
  2828. -----+----------+--------
  2829. \\- &shy; \\-
  2830. -- &ndash; --
  2831. --- &mdash; ---
  2832. ... &hellip; \ldots
  2833. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2834. :group 'org-export-translation
  2835. :type 'boolean)
  2836. (defcustom org-export-language-setup
  2837. '(("en" "Author" "Date" "Table of Contents")
  2838. ("cs" "Autor" "Datum" "Obsah")
  2839. ("da" "Ophavsmand" "Dato" "Indhold")
  2840. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2841. ("es" "Autor" "Fecha" "\xcdndice")
  2842. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2843. ("it" "Autore" "Data" "Indice")
  2844. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2845. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2846. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2847. "Terms used in export text, translated to different languages.
  2848. Use the variable `org-export-default-language' to set the language,
  2849. or use the +OPTION lines for a per-file setting."
  2850. :group 'org-export-general
  2851. :type '(repeat
  2852. (list
  2853. (string :tag "HTML language tag")
  2854. (string :tag "Author")
  2855. (string :tag "Date")
  2856. (string :tag "Table of Contents"))))
  2857. (defcustom org-export-default-language "en"
  2858. "The default language of HTML export, as a string.
  2859. This should have an association in `org-export-language-setup'."
  2860. :group 'org-export-general
  2861. :type 'string)
  2862. (defcustom org-export-skip-text-before-1st-heading t
  2863. "Non-nil means, skip all text before the first headline when exporting.
  2864. When nil, that text is exported as well."
  2865. :group 'org-export-general
  2866. :type 'boolean)
  2867. (defcustom org-export-headline-levels 3
  2868. "The last level which is still exported as a headline.
  2869. Inferior levels will produce itemize lists when exported.
  2870. Note that a numeric prefix argument to an exporter function overrides
  2871. this setting.
  2872. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2873. :group 'org-export-general
  2874. :type 'number)
  2875. (defcustom org-export-with-section-numbers t
  2876. "Non-nil means, add section numbers to headlines when exporting.
  2877. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2878. :group 'org-export-general
  2879. :type 'boolean)
  2880. (defcustom org-export-with-toc t
  2881. "Non-nil means, create a table of contents in exported files.
  2882. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2883. When an integer, include levels up to N in the toc, this may then be
  2884. different from `org-export-headline-levels', but it will not be allowed
  2885. to be larger than the number of headline levels.
  2886. When nil, no table of contents is made.
  2887. Headlines which contain any TODO items will be marked with \"(*)\" in
  2888. ASCII export, and with red color in HTML output, if the option
  2889. `org-export-mark-todo-in-toc' is set.
  2890. In HTML output, the TOC will be clickable.
  2891. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2892. or \"toc:3\"."
  2893. :group 'org-export-general
  2894. :type '(choice
  2895. (const :tag "No Table of Contents" nil)
  2896. (const :tag "Full Table of Contents" t)
  2897. (integer :tag "TOC to level")))
  2898. (defcustom org-export-mark-todo-in-toc nil
  2899. "Non-nil means, mark TOC lines that contain any open TODO items."
  2900. :group 'org-export-general
  2901. :type 'boolean)
  2902. (defcustom org-export-preserve-breaks nil
  2903. "Non-nil means, preserve all line breaks when exporting.
  2904. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2905. export, line breaks will always be preserved, regardless of this variable.
  2906. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2907. :group 'org-export-general
  2908. :type 'boolean)
  2909. (defcustom org-export-with-archived-trees 'headline
  2910. "Whether subtrees with the ARCHIVE tag should be exported.
  2911. This can have three different values
  2912. nil Do not export, pretend this tree is not present
  2913. t Do export the entire tree
  2914. headline Only export the headline, but skip the tree below it."
  2915. :group 'org-export-general
  2916. :group 'org-archive
  2917. :type '(choice
  2918. (const :tag "not at all" nil)
  2919. (const :tag "headline only" 'headline)
  2920. (const :tag "entirely" t)))
  2921. (defcustom org-export-author-info t
  2922. "Non-nil means, insert author name and email into the exported file.
  2923. This option can also be set with the +OPTIONS line,
  2924. e.g. \"author-info:nil\"."
  2925. :group 'org-export-general
  2926. :type 'boolean)
  2927. (defcustom org-export-time-stamp-file t
  2928. "Non-nil means, insert a time stamp into the exported file.
  2929. The time stamp shows when the file was created.
  2930. This option can also be set with the +OPTIONS line,
  2931. e.g. \"timestamp:nil\"."
  2932. :group 'org-export-general
  2933. :type 'boolean)
  2934. (defcustom org-export-with-timestamps t
  2935. "If nil, do not export time stamps and associated keywords."
  2936. :group 'org-export-general
  2937. :type 'boolean)
  2938. (defcustom org-export-remove-timestamps-from-toc t
  2939. "If nil, remove timestamps from the table of contents entries."
  2940. :group 'org-export-general
  2941. :type 'boolean)
  2942. (defcustom org-export-with-tags 'not-in-toc
  2943. "If nil, do not export tags, just remove them from headlines.
  2944. If this is the symbol `not-in-toc', tags will be removed from table of
  2945. contents entries, but still be shown in the headlines of the document.
  2946. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2947. :group 'org-export-general
  2948. :type '(choice
  2949. (const :tag "Off" nil)
  2950. (const :tag "Not in TOC" not-in-toc)
  2951. (const :tag "On" t)))
  2952. (defcustom org-export-with-drawers nil
  2953. "Non-nil means, export with drawers like the property drawer.
  2954. When t, all drawers are exported. This may also be a list of
  2955. drawer names to export."
  2956. :group 'org-export-general
  2957. :type '(choice
  2958. (const :tag "All drawers" t)
  2959. (const :tag "None" nil)
  2960. (repeat :tag "Selected drawers"
  2961. (string :tag "Drawer name"))))
  2962. (defgroup org-export-translation nil
  2963. "Options for translating special ascii sequences for the export backends."
  2964. :tag "Org Export Translation"
  2965. :group 'org-export)
  2966. (defcustom org-export-with-emphasize t
  2967. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2968. If the export target supports emphasizing text, the word will be
  2969. typeset in bold, italic, or underlined, respectively. Works only for
  2970. single words, but you can say: I *really* *mean* *this*.
  2971. Not all export backends support this.
  2972. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2973. :group 'org-export-translation
  2974. :type 'boolean)
  2975. (defcustom org-export-with-footnotes t
  2976. "If nil, export [1] as a footnote marker.
  2977. Lines starting with [1] will be formatted as footnotes.
  2978. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2979. :group 'org-export-translation
  2980. :type 'boolean)
  2981. (defcustom org-export-with-sub-superscripts t
  2982. "Non-nil means, interpret \"_\" and \"^\" for export.
  2983. When this option is turned on, you can use TeX-like syntax for sub- and
  2984. superscripts. Several characters after \"_\" or \"^\" will be
  2985. considered as a single item - so grouping with {} is normally not
  2986. needed. For example, the following things will be parsed as single
  2987. sub- or superscripts.
  2988. 10^24 or 10^tau several digits will be considered 1 item.
  2989. 10^-12 or 10^-tau a leading sign with digits or a word
  2990. x^2-y^3 will be read as x^2 - y^3, because items are
  2991. terminated by almost any nonword/nondigit char.
  2992. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2993. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2994. sub/superscript. If you set this variable to the symbol `{}',
  2995. the braces are *required* in order to trigger interpretations as
  2996. sub/superscript. This can be helpful in documents that need \"_\"
  2997. frequently in plain text.
  2998. Not all export backends support this, but HTML does.
  2999. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  3000. :group 'org-export-translation
  3001. :type '(choice
  3002. (const :tag "Always interpret" t)
  3003. (const :tag "Only with braces" {})
  3004. (const :tag "Never interpret" nil)))
  3005. (defcustom org-export-with-special-strings t
  3006. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  3007. When this option is turned on, these strings will be exported as:
  3008. \\- : &shy;
  3009. -- : &ndash;
  3010. --- : &mdash;
  3011. Not all export backends support this, but HTML does.
  3012. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  3013. :group 'org-export-translation
  3014. :type 'boolean)
  3015. (defcustom org-export-with-TeX-macros t
  3016. "Non-nil means, interpret simple TeX-like macros when exporting.
  3017. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  3018. No only real TeX macros will work here, but the standard HTML entities
  3019. for math can be used as macro names as well. For a list of supported
  3020. names in HTML export, see the constant `org-html-entities'.
  3021. Not all export backends support this.
  3022. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  3023. :group 'org-export-translation
  3024. :group 'org-export-latex
  3025. :type 'boolean)
  3026. (defcustom org-export-with-LaTeX-fragments nil
  3027. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  3028. When set, the exporter will find LaTeX environments if the \\begin line is
  3029. the first non-white thing on a line. It will also find the math delimiters
  3030. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  3031. display math.
  3032. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  3033. :group 'org-export-translation
  3034. :group 'org-export-latex
  3035. :type 'boolean)
  3036. (defcustom org-export-with-fixed-width t
  3037. "Non-nil means, lines starting with \":\" will be in fixed width font.
  3038. This can be used to have pre-formatted text, fragments of code etc. For
  3039. example:
  3040. : ;; Some Lisp examples
  3041. : (while (defc cnt)
  3042. : (ding))
  3043. will be looking just like this in also HTML. See also the QUOTE keyword.
  3044. Not all export backends support this.
  3045. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  3046. :group 'org-export-translation
  3047. :type 'boolean)
  3048. (defcustom org-match-sexp-depth 3
  3049. "Number of stacked braces for sub/superscript matching.
  3050. This has to be set before loading org.el to be effective."
  3051. :group 'org-export-translation
  3052. :type 'integer)
  3053. (defgroup org-export-tables nil
  3054. "Options for exporting tables in Org-mode."
  3055. :tag "Org Export Tables"
  3056. :group 'org-export)
  3057. (defcustom org-export-with-tables t
  3058. "If non-nil, lines starting with \"|\" define a table.
  3059. For example:
  3060. | Name | Address | Birthday |
  3061. |-------------+----------+-----------|
  3062. | Arthur Dent | England | 29.2.2100 |
  3063. Not all export backends support this.
  3064. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  3065. :group 'org-export-tables
  3066. :type 'boolean)
  3067. (defcustom org-export-highlight-first-table-line t
  3068. "Non-nil means, highlight the first table line.
  3069. In HTML export, this means use <th> instead of <td>.
  3070. In tables created with table.el, this applies to the first table line.
  3071. In Org-mode tables, all lines before the first horizontal separator
  3072. line will be formatted with <th> tags."
  3073. :group 'org-export-tables
  3074. :type 'boolean)
  3075. (defcustom org-export-table-remove-special-lines t
  3076. "Remove special lines and marking characters in calculating tables.
  3077. This removes the special marking character column from tables that are set
  3078. up for spreadsheet calculations. It also removes the entire lines
  3079. marked with `!', `_', or `^'. The lines with `$' are kept, because
  3080. the values of constants may be useful to have."
  3081. :group 'org-export-tables
  3082. :type 'boolean)
  3083. (defcustom org-export-prefer-native-exporter-for-tables nil
  3084. "Non-nil means, always export tables created with table.el natively.
  3085. Natively means, use the HTML code generator in table.el.
  3086. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  3087. the table does not use row- or column-spanning). This has the
  3088. advantage, that the automatic HTML conversions for math symbols and
  3089. sub/superscripts can be applied. Org-mode's HTML generator is also
  3090. much faster."
  3091. :group 'org-export-tables
  3092. :type 'boolean)
  3093. (defgroup org-export-ascii nil
  3094. "Options specific for ASCII export of Org-mode files."
  3095. :tag "Org Export ASCII"
  3096. :group 'org-export)
  3097. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  3098. "Characters for underlining headings in ASCII export.
  3099. In the given sequence, these characters will be used for level 1, 2, ..."
  3100. :group 'org-export-ascii
  3101. :type '(repeat character))
  3102. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3103. "Bullet characters for headlines converted to lists in ASCII export.
  3104. The first character is used for the first lest level generated in this
  3105. way, and so on. If there are more levels than characters given here,
  3106. the list will be repeated.
  3107. Note that plain lists will keep the same bullets as the have in the
  3108. Org-mode file."
  3109. :group 'org-export-ascii
  3110. :type '(repeat character))
  3111. (defgroup org-export-xml nil
  3112. "Options specific for XML export of Org-mode files."
  3113. :tag "Org Export XML"
  3114. :group 'org-export)
  3115. (defgroup org-export-html nil
  3116. "Options specific for HTML export of Org-mode files."
  3117. :tag "Org Export HTML"
  3118. :group 'org-export)
  3119. (defcustom org-export-html-coding-system nil
  3120. ""
  3121. :group 'org-export-html
  3122. :type 'coding-system)
  3123. (defcustom org-export-html-extension "html"
  3124. "The extension for exported HTML files."
  3125. :group 'org-export-html
  3126. :type 'string)
  3127. (defcustom org-export-html-style
  3128. "<style type=\"text/css\">
  3129. html {
  3130. font-family: Times, serif;
  3131. font-size: 12pt;
  3132. }
  3133. .title { text-align: center; }
  3134. .todo { color: red; }
  3135. .done { color: green; }
  3136. .timestamp { color: grey }
  3137. .timestamp-kwd { color: CadetBlue }
  3138. .tag { background-color:lightblue; font-weight:normal }
  3139. .target { background-color: lavender; }
  3140. pre {
  3141. border: 1pt solid #AEBDCC;
  3142. background-color: #F3F5F7;
  3143. padding: 5pt;
  3144. font-family: courier, monospace;
  3145. }
  3146. table { border-collapse: collapse; }
  3147. td, th {
  3148. vertical-align: top;
  3149. <!--border: 1pt solid #ADB9CC;-->
  3150. }
  3151. </style>"
  3152. "The default style specification for exported HTML files.
  3153. Since there are different ways of setting style information, this variable
  3154. needs to contain the full HTML structure to provide a style, including the
  3155. surrounding HTML tags. The style specifications should include definitions
  3156. for new classes todo, done, title, and deadline. For example, valid values
  3157. would be:
  3158. <style type=\"text/css\">
  3159. p { font-weight: normal; color: gray; }
  3160. h1 { color: black; }
  3161. .title { text-align: center; }
  3162. .todo, .deadline { color: red; }
  3163. .done { color: green; }
  3164. </style>
  3165. or, if you want to keep the style in a file,
  3166. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3167. As the value of this option simply gets inserted into the HTML <head> header,
  3168. you can \"misuse\" it to add arbitrary text to the header."
  3169. :group 'org-export-html
  3170. :type 'string)
  3171. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3172. "Format for typesetting the document title in HTML export."
  3173. :group 'org-export-html
  3174. :type 'string)
  3175. (defcustom org-export-html-toplevel-hlevel 2
  3176. "The <H> level for level 1 headings in HTML export."
  3177. :group 'org-export-html
  3178. :type 'string)
  3179. (defcustom org-export-html-link-org-files-as-html t
  3180. "Non-nil means, make file links to `file.org' point to `file.html'.
  3181. When org-mode is exporting an org-mode file to HTML, links to
  3182. non-html files are directly put into a href tag in HTML.
  3183. However, links to other Org-mode files (recognized by the
  3184. extension `.org.) should become links to the corresponding html
  3185. file, assuming that the linked org-mode file will also be
  3186. converted to HTML.
  3187. When nil, the links still point to the plain `.org' file."
  3188. :group 'org-export-html
  3189. :type 'boolean)
  3190. (defcustom org-export-html-inline-images 'maybe
  3191. "Non-nil means, inline images into exported HTML pages.
  3192. This is done using an <img> tag. When nil, an anchor with href is used to
  3193. link to the image. If this option is `maybe', then images in links with
  3194. an empty description will be inlined, while images with a description will
  3195. be linked only."
  3196. :group 'org-export-html
  3197. :type '(choice (const :tag "Never" nil)
  3198. (const :tag "Always" t)
  3199. (const :tag "When there is no description" maybe)))
  3200. ;; FIXME: rename
  3201. (defcustom org-export-html-expand t
  3202. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3203. When nil, these tags will be exported as plain text and therefore
  3204. not be interpreted by a browser.
  3205. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3206. :group 'org-export-html
  3207. :type 'boolean)
  3208. (defcustom org-export-html-table-tag
  3209. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3210. "The HTML tag that is used to start a table.
  3211. This must be a <table> tag, but you may change the options like
  3212. borders and spacing."
  3213. :group 'org-export-html
  3214. :type 'string)
  3215. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3216. "The opening tag for table header fields.
  3217. This is customizable so that alignment options can be specified."
  3218. :group 'org-export-tables
  3219. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3220. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3221. "The opening tag for table data fields.
  3222. This is customizable so that alignment options can be specified."
  3223. :group 'org-export-tables
  3224. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3225. (defcustom org-export-html-with-timestamp nil
  3226. "If non-nil, write `org-export-html-html-helper-timestamp'
  3227. into the exported HTML text. Otherwise, the buffer will just be saved
  3228. to a file."
  3229. :group 'org-export-html
  3230. :type 'boolean)
  3231. (defcustom org-export-html-html-helper-timestamp
  3232. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3233. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3234. :group 'org-export-html
  3235. :type 'string)
  3236. (defgroup org-export-icalendar nil
  3237. "Options specific for iCalendar export of Org-mode files."
  3238. :tag "Org Export iCalendar"
  3239. :group 'org-export)
  3240. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3241. "The file name for the iCalendar file covering all agenda files.
  3242. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3243. The file name should be absolute, the file will be overwritten without warning."
  3244. :group 'org-export-icalendar
  3245. :type 'file)
  3246. (defcustom org-icalendar-include-todo nil
  3247. "Non-nil means, export to iCalendar files should also cover TODO items."
  3248. :group 'org-export-icalendar
  3249. :type '(choice
  3250. (const :tag "None" nil)
  3251. (const :tag "Unfinished" t)
  3252. (const :tag "All" all)))
  3253. (defcustom org-icalendar-include-sexps t
  3254. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3255. These are entries like in the diary, but directly in an Org-mode file."
  3256. :group 'org-export-icalendar
  3257. :type 'boolean)
  3258. (defcustom org-icalendar-include-body 100
  3259. "Amount of text below headline to be included in iCalendar export.
  3260. This is a number of characters that should maximally be included.
  3261. Properties, scheduling and clocking lines will always be removed.
  3262. The text will be inserted into the DESCRIPTION field."
  3263. :group 'org-export-icalendar
  3264. :type '(choice
  3265. (const :tag "Nothing" nil)
  3266. (const :tag "Everything" t)
  3267. (integer :tag "Max characters")))
  3268. (defcustom org-icalendar-combined-name "OrgMode"
  3269. "Calendar name for the combined iCalendar representing all agenda files."
  3270. :group 'org-export-icalendar
  3271. :type 'string)
  3272. (defgroup org-font-lock nil
  3273. "Font-lock settings for highlighting in Org-mode."
  3274. :tag "Org Font Lock"
  3275. :group 'org)
  3276. (defcustom org-level-color-stars-only nil
  3277. "Non-nil means fontify only the stars in each headline.
  3278. When nil, the entire headline is fontified.
  3279. Changing it requires restart of `font-lock-mode' to become effective
  3280. also in regions already fontified."
  3281. :group 'org-font-lock
  3282. :type 'boolean)
  3283. (defcustom org-hide-leading-stars nil
  3284. "Non-nil means, hide the first N-1 stars in a headline.
  3285. This works by using the face `org-hide' for these stars. This
  3286. face is white for a light background, and black for a dark
  3287. background. You may have to customize the face `org-hide' to
  3288. make this work.
  3289. Changing it requires restart of `font-lock-mode' to become effective
  3290. also in regions already fontified.
  3291. You may also set this on a per-file basis by adding one of the following
  3292. lines to the buffer:
  3293. #+STARTUP: hidestars
  3294. #+STARTUP: showstars"
  3295. :group 'org-font-lock
  3296. :type 'boolean)
  3297. (defcustom org-fontify-done-headline nil
  3298. "Non-nil means, change the face of a headline if it is marked DONE.
  3299. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3300. When this is non-nil, the headline after the keyword is set to the
  3301. `org-headline-done' as an additional indication."
  3302. :group 'org-font-lock
  3303. :type 'boolean)
  3304. (defcustom org-fontify-emphasized-text t
  3305. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3306. Changing this variable requires a restart of Emacs to take effect."
  3307. :group 'org-font-lock
  3308. :type 'boolean)
  3309. (defcustom org-highlight-latex-fragments-and-specials nil
  3310. "Non-nil means, fontify what is treated specially by the exporters."
  3311. :group 'org-font-lock
  3312. :type 'boolean)
  3313. (defcustom org-hide-emphasis-markers nil
  3314. "Non-nil mean font-lock should hide the emphasis marker characters."
  3315. :group 'org-font-lock
  3316. :type 'boolean)
  3317. (defvar org-emph-re nil
  3318. "Regular expression for matching emphasis.")
  3319. (defvar org-verbatim-re nil
  3320. "Regular expression for matching verbatim text.")
  3321. (defvar org-emphasis-regexp-components) ; defined just below
  3322. (defvar org-emphasis-alist) ; defined just below
  3323. (defun org-set-emph-re (var val)
  3324. "Set variable and compute the emphasis regular expression."
  3325. (set var val)
  3326. (when (and (boundp 'org-emphasis-alist)
  3327. (boundp 'org-emphasis-regexp-components)
  3328. org-emphasis-alist org-emphasis-regexp-components)
  3329. (let* ((e org-emphasis-regexp-components)
  3330. (pre (car e))
  3331. (post (nth 1 e))
  3332. (border (nth 2 e))
  3333. (body (nth 3 e))
  3334. (nl (nth 4 e))
  3335. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3336. (body1 (concat body "*?"))
  3337. (markers (mapconcat 'car org-emphasis-alist ""))
  3338. (vmarkers (mapconcat
  3339. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3340. org-emphasis-alist "")))
  3341. ;; make sure special characters appear at the right position in the class
  3342. (if (string-match "\\^" markers)
  3343. (setq markers (concat (replace-match "" t t markers) "^")))
  3344. (if (string-match "-" markers)
  3345. (setq markers (concat (replace-match "" t t markers) "-")))
  3346. (if (string-match "\\^" vmarkers)
  3347. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3348. (if (string-match "-" vmarkers)
  3349. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3350. (if (> nl 0)
  3351. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3352. (int-to-string nl) "\\}")))
  3353. ;; Make the regexp
  3354. (setq org-emph-re
  3355. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3356. "\\("
  3357. "\\([" markers "]\\)"
  3358. "\\("
  3359. "[^" border "]\\|"
  3360. "[^" border (if (and nil stacked) markers) "]"
  3361. body1
  3362. "[^" border (if (and nil stacked) markers) "]"
  3363. "\\)"
  3364. "\\3\\)"
  3365. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3366. (setq org-verbatim-re
  3367. (concat "\\([" pre "]\\|^\\)"
  3368. "\\("
  3369. "\\([" vmarkers "]\\)"
  3370. "\\("
  3371. "[^" border "]\\|"
  3372. "[^" border "]"
  3373. body1
  3374. "[^" border "]"
  3375. "\\)"
  3376. "\\3\\)"
  3377. "\\([" post "]\\|$\\)")))))
  3378. (defcustom org-emphasis-regexp-components
  3379. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3380. "Components used to build the regular expression for emphasis.
  3381. This is a list with 6 entries. Terminology: In an emphasis string
  3382. like \" *strong word* \", we call the initial space PREMATCH, the final
  3383. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3384. and \"trong wor\" is the body. The different components in this variable
  3385. specify what is allowed/forbidden in each part:
  3386. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3387. post Chars allowed as postmatch. End of line will be allowed too.
  3388. border The chars *forbidden* as border characters.
  3389. body-regexp A regexp like \".\" to match a body character. Don't use
  3390. non-shy groups here, and don't allow newline here.
  3391. newline The maximum number of newlines allowed in an emphasis exp.
  3392. Use customize to modify this, or restart Emacs after changing it."
  3393. :group 'org-font-lock
  3394. :set 'org-set-emph-re
  3395. :type '(list
  3396. (sexp :tag "Allowed chars in pre ")
  3397. (sexp :tag "Allowed chars in post ")
  3398. (sexp :tag "Forbidden chars in border ")
  3399. (sexp :tag "Regexp for body ")
  3400. (integer :tag "number of newlines allowed")
  3401. (option (boolean :tag "Stacking (DISABLED) "))))
  3402. (defcustom org-emphasis-alist
  3403. '(("*" bold "<b>" "</b>")
  3404. ("/" italic "<i>" "</i>")
  3405. ("_" underline "<u>" "</u>")
  3406. ("=" org-code "<code>" "</code>" verbatim)
  3407. ("~" org-verbatim "" "" verbatim)
  3408. ("+" (:strike-through t) "<del>" "</del>")
  3409. )
  3410. "Special syntax for emphasized text.
  3411. Text starting and ending with a special character will be emphasized, for
  3412. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3413. characters, the face to be used by font-lock for highlighting in Org-mode
  3414. Emacs buffers, and the HTML tags to be used for this.
  3415. Use customize to modify this, or restart Emacs after changing it."
  3416. :group 'org-font-lock
  3417. :set 'org-set-emph-re
  3418. :type '(repeat
  3419. (list
  3420. (string :tag "Marker character")
  3421. (choice
  3422. (face :tag "Font-lock-face")
  3423. (plist :tag "Face property list"))
  3424. (string :tag "HTML start tag")
  3425. (string :tag "HTML end tag")
  3426. (option (const verbatim)))))
  3427. ;;; The faces
  3428. (defgroup org-faces nil
  3429. "Faces in Org-mode."
  3430. :tag "Org Faces"
  3431. :group 'org-font-lock)
  3432. (defun org-compatible-face (inherits specs)
  3433. "Make a compatible face specification.
  3434. If INHERITS is an existing face and if the Emacs version supports it,
  3435. just inherit the face. If not, use SPECS to define the face.
  3436. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3437. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3438. to the top of the list. The `min-colors' attribute will be removed from
  3439. any other entries, and any resulting duplicates will be removed entirely."
  3440. (cond
  3441. ((and inherits (facep inherits)
  3442. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3443. ;; In Emacs 23, we use inheritance where possible.
  3444. ;; We only do this in Emacs 23, because only there the outline
  3445. ;; faces have been changed to the original org-mode-level-faces.
  3446. (list (list t :inherit inherits)))
  3447. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3448. ;; These do not understand the `min-colors' attribute.
  3449. (let (r e a)
  3450. (while (setq e (pop specs))
  3451. (cond
  3452. ((memq (car e) '(t default)) (push e r))
  3453. ((setq a (member '(min-colors 8) (car e)))
  3454. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3455. (cdr e)))))
  3456. ((setq a (assq 'min-colors (car e)))
  3457. (setq e (cons (delq a (car e)) (cdr e)))
  3458. (or (assoc (car e) r) (push e r)))
  3459. (t (or (assoc (car e) r) (push e r)))))
  3460. (nreverse r)))
  3461. (t specs)))
  3462. (put 'org-compatible-face 'lisp-indent-function 1)
  3463. (defface org-hide
  3464. '((((background light)) (:foreground "white"))
  3465. (((background dark)) (:foreground "black")))
  3466. "Face used to hide leading stars in headlines.
  3467. The forground color of this face should be equal to the background
  3468. color of the frame."
  3469. :group 'org-faces)
  3470. (defface org-level-1 ;; font-lock-function-name-face
  3471. (org-compatible-face 'outline-1
  3472. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3473. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3474. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3475. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3476. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3477. (t (:bold t))))
  3478. "Face used for level 1 headlines."
  3479. :group 'org-faces)
  3480. (defface org-level-2 ;; font-lock-variable-name-face
  3481. (org-compatible-face 'outline-2
  3482. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3483. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3484. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3485. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3486. (t (:bold t))))
  3487. "Face used for level 2 headlines."
  3488. :group 'org-faces)
  3489. (defface org-level-3 ;; font-lock-keyword-face
  3490. (org-compatible-face 'outline-3
  3491. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3492. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3493. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3494. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3495. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3496. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3497. (t (:bold t))))
  3498. "Face used for level 3 headlines."
  3499. :group 'org-faces)
  3500. (defface org-level-4 ;; font-lock-comment-face
  3501. (org-compatible-face 'outline-4
  3502. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3503. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3504. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3505. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3506. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3507. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3508. (t (:bold t))))
  3509. "Face used for level 4 headlines."
  3510. :group 'org-faces)
  3511. (defface org-level-5 ;; font-lock-type-face
  3512. (org-compatible-face 'outline-5
  3513. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3514. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3515. (((class color) (min-colors 8)) (:foreground "green"))))
  3516. "Face used for level 5 headlines."
  3517. :group 'org-faces)
  3518. (defface org-level-6 ;; font-lock-constant-face
  3519. (org-compatible-face 'outline-6
  3520. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3521. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3522. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3523. "Face used for level 6 headlines."
  3524. :group 'org-faces)
  3525. (defface org-level-7 ;; font-lock-builtin-face
  3526. (org-compatible-face 'outline-7
  3527. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3528. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3529. (((class color) (min-colors 8)) (:foreground "blue"))))
  3530. "Face used for level 7 headlines."
  3531. :group 'org-faces)
  3532. (defface org-level-8 ;; font-lock-string-face
  3533. (org-compatible-face 'outline-8
  3534. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3535. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3536. (((class color) (min-colors 8)) (:foreground "green"))))
  3537. "Face used for level 8 headlines."
  3538. :group 'org-faces)
  3539. (defface org-special-keyword ;; font-lock-string-face
  3540. (org-compatible-face nil
  3541. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3542. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3543. (t (:italic t))))
  3544. "Face used for special keywords."
  3545. :group 'org-faces)
  3546. (defface org-drawer ;; font-lock-function-name-face
  3547. (org-compatible-face nil
  3548. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3549. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3550. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3551. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3552. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3553. (t (:bold t))))
  3554. "Face used for drawers."
  3555. :group 'org-faces)
  3556. (defface org-property-value nil
  3557. "Face used for the value of a property."
  3558. :group 'org-faces)
  3559. (defface org-column
  3560. (org-compatible-face nil
  3561. '((((class color) (min-colors 16) (background light))
  3562. (:background "grey90"))
  3563. (((class color) (min-colors 16) (background dark))
  3564. (:background "grey30"))
  3565. (((class color) (min-colors 8))
  3566. (:background "cyan" :foreground "black"))
  3567. (t (:inverse-video t))))
  3568. "Face for column display of entry properties."
  3569. :group 'org-faces)
  3570. (when (fboundp 'set-face-attribute)
  3571. ;; Make sure that a fixed-width face is used when we have a column table.
  3572. (set-face-attribute 'org-column nil
  3573. :height (face-attribute 'default :height)
  3574. :family (face-attribute 'default :family)))
  3575. (defface org-warning
  3576. (org-compatible-face 'font-lock-warning-face
  3577. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3578. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3579. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3580. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3581. (t (:bold t))))
  3582. "Face for deadlines and TODO keywords."
  3583. :group 'org-faces)
  3584. (defface org-archived ; similar to shadow
  3585. (org-compatible-face 'shadow
  3586. '((((class color grayscale) (min-colors 88) (background light))
  3587. (:foreground "grey50"))
  3588. (((class color grayscale) (min-colors 88) (background dark))
  3589. (:foreground "grey70"))
  3590. (((class color) (min-colors 8) (background light))
  3591. (:foreground "green"))
  3592. (((class color) (min-colors 8) (background dark))
  3593. (:foreground "yellow"))))
  3594. "Face for headline with the ARCHIVE tag."
  3595. :group 'org-faces)
  3596. (defface org-link
  3597. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3598. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3599. (t (:underline t)))
  3600. "Face for links."
  3601. :group 'org-faces)
  3602. (defface org-ellipsis
  3603. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3604. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3605. (t (:strike-through t)))
  3606. "Face for the ellipsis in folded text."
  3607. :group 'org-faces)
  3608. (defface org-target
  3609. '((((class color) (background light)) (:underline t))
  3610. (((class color) (background dark)) (:underline t))
  3611. (t (:underline t)))
  3612. "Face for links."
  3613. :group 'org-faces)
  3614. (defface org-date
  3615. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3616. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3617. (t (:underline t)))
  3618. "Face for links."
  3619. :group 'org-faces)
  3620. (defface org-sexp-date
  3621. '((((class color) (background light)) (:foreground "Purple"))
  3622. (((class color) (background dark)) (:foreground "Cyan"))
  3623. (t (:underline t)))
  3624. "Face for links."
  3625. :group 'org-faces)
  3626. (defface org-tag
  3627. '((t (:bold t)))
  3628. "Face for tags."
  3629. :group 'org-faces)
  3630. (defface org-todo ; font-lock-warning-face
  3631. (org-compatible-face nil
  3632. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3633. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3634. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3635. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3636. (t (:inverse-video t :bold t))))
  3637. "Face for TODO keywords."
  3638. :group 'org-faces)
  3639. (defface org-done ;; font-lock-type-face
  3640. (org-compatible-face nil
  3641. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3642. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3643. (((class color) (min-colors 8)) (:foreground "green"))
  3644. (t (:bold t))))
  3645. "Face used for todo keywords that indicate DONE items."
  3646. :group 'org-faces)
  3647. (defface org-headline-done ;; font-lock-string-face
  3648. (org-compatible-face nil
  3649. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3650. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3651. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3652. "Face used to indicate that a headline is DONE.
  3653. This face is only used if `org-fontify-done-headline' is set. If applies
  3654. to the part of the headline after the DONE keyword."
  3655. :group 'org-faces)
  3656. (defcustom org-todo-keyword-faces nil
  3657. "Faces for specific TODO keywords.
  3658. This is a list of cons cells, with TODO keywords in the car
  3659. and faces in the cdr. The face can be a symbol, or a property
  3660. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3661. :group 'org-faces
  3662. :group 'org-todo
  3663. :type '(repeat
  3664. (cons
  3665. (string :tag "keyword")
  3666. (sexp :tag "face"))))
  3667. (defface org-table ;; font-lock-function-name-face
  3668. (org-compatible-face nil
  3669. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3670. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3671. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3672. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3673. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3674. (((class color) (min-colors 8) (background dark)))))
  3675. "Face used for tables."
  3676. :group 'org-faces)
  3677. (defface org-formula
  3678. (org-compatible-face nil
  3679. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3680. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3681. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3682. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3683. (t (:bold t :italic t))))
  3684. "Face for formulas."
  3685. :group 'org-faces)
  3686. (defface org-code
  3687. (org-compatible-face nil
  3688. '((((class color grayscale) (min-colors 88) (background light))
  3689. (:foreground "grey50"))
  3690. (((class color grayscale) (min-colors 88) (background dark))
  3691. (:foreground "grey70"))
  3692. (((class color) (min-colors 8) (background light))
  3693. (:foreground "green"))
  3694. (((class color) (min-colors 8) (background dark))
  3695. (:foreground "yellow"))))
  3696. "Face for fixed-with text like code snippets."
  3697. :group 'org-faces
  3698. :version "22.1")
  3699. (defface org-verbatim
  3700. (org-compatible-face nil
  3701. '((((class color grayscale) (min-colors 88) (background light))
  3702. (:foreground "grey50" :underline t))
  3703. (((class color grayscale) (min-colors 88) (background dark))
  3704. (:foreground "grey70" :underline t))
  3705. (((class color) (min-colors 8) (background light))
  3706. (:foreground "green" :underline t))
  3707. (((class color) (min-colors 8) (background dark))
  3708. (:foreground "yellow" :underline t))))
  3709. "Face for fixed-with text like code snippets."
  3710. :group 'org-faces
  3711. :version "22.1")
  3712. (defface org-agenda-structure ;; font-lock-function-name-face
  3713. (org-compatible-face nil
  3714. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3715. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3716. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3717. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3718. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3719. (t (:bold t))))
  3720. "Face used in agenda for captions and dates."
  3721. :group 'org-faces)
  3722. (defface org-scheduled-today
  3723. (org-compatible-face nil
  3724. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3725. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3726. (((class color) (min-colors 8)) (:foreground "green"))
  3727. (t (:bold t :italic t))))
  3728. "Face for items scheduled for a certain day."
  3729. :group 'org-faces)
  3730. (defface org-scheduled-previously
  3731. (org-compatible-face nil
  3732. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3733. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3734. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3735. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3736. (t (:bold t))))
  3737. "Face for items scheduled previously, and not yet done."
  3738. :group 'org-faces)
  3739. (defface org-upcoming-deadline
  3740. (org-compatible-face nil
  3741. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3742. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3743. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3744. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3745. (t (:bold t))))
  3746. "Face for items scheduled previously, and not yet done."
  3747. :group 'org-faces)
  3748. (defcustom org-agenda-deadline-faces
  3749. '((1.0 . org-warning)
  3750. (0.5 . org-upcoming-deadline)
  3751. (0.0 . default))
  3752. "Faces for showing deadlines in the agenda.
  3753. This is a list of cons cells. The cdr of each cell is a face to be used,
  3754. and it can also just be like '(:foreground \"yellow\").
  3755. Each car is a fraction of the head-warning time that must have passed for
  3756. this the face in the cdr to be used for display. The numbers must be
  3757. given in descending order. The head-warning time is normally taken
  3758. from `org-deadline-warning-days', but can also be specified in the deadline
  3759. timestamp itself, like this:
  3760. DEADLINE: <2007-08-13 Mon -8d>
  3761. You may use d for days, w for weeks, m for months and y for years. Months
  3762. and years will only be treated in an approximate fashion (30.4 days for a
  3763. month and 365.24 days for a year)."
  3764. :group 'org-faces
  3765. :group 'org-agenda-daily/weekly
  3766. :type '(repeat
  3767. (cons
  3768. (number :tag "Fraction of head-warning time passed")
  3769. (sexp :tag "Face"))))
  3770. ;; FIXME: this is not a good face yet.
  3771. (defface org-agenda-restriction-lock
  3772. (org-compatible-face nil
  3773. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3774. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3775. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3776. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3777. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3778. (t (:inverse-video t))))
  3779. "Face for showing the agenda restriction lock."
  3780. :group 'org-faces)
  3781. (defface org-time-grid ;; font-lock-variable-name-face
  3782. (org-compatible-face nil
  3783. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3784. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3785. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3786. "Face used for time grids."
  3787. :group 'org-faces)
  3788. (defconst org-level-faces
  3789. '(org-level-1 org-level-2 org-level-3 org-level-4
  3790. org-level-5 org-level-6 org-level-7 org-level-8
  3791. ))
  3792. (defcustom org-n-level-faces (length org-level-faces)
  3793. "The number of different faces to be used for headlines.
  3794. Org-mode defines 8 different headline faces, so this can be at most 8.
  3795. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3796. :type 'number
  3797. :group 'org-faces)
  3798. ;;; Functions and variables from ther packages
  3799. ;; Declared here to avoid compiler warnings
  3800. (eval-and-compile
  3801. (unless (fboundp 'declare-function)
  3802. (defmacro declare-function (fn file &optional arglist fileonly))))
  3803. ;; XEmacs only
  3804. (defvar outline-mode-menu-heading)
  3805. (defvar outline-mode-menu-show)
  3806. (defvar outline-mode-menu-hide)
  3807. (defvar zmacs-regions) ; XEmacs regions
  3808. ;; Emacs only
  3809. (defvar mark-active)
  3810. ;; Various packages
  3811. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3812. (declare-function add-to-diary-list "diary-lib"
  3813. (date string specifier &optional marker globcolor literal))
  3814. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3815. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3816. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3817. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3818. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3819. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3820. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3821. (declare-function bbdb-record-name "ext:bbdb" (record))
  3822. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3823. (declare-function bibtex-generate-autokey "bibtex" ())
  3824. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3825. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3826. (defvar calc-embedded-close-formula)
  3827. (defvar calc-embedded-open-formula)
  3828. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3829. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3830. (declare-function calendar-check-holidays "holidays" (date))
  3831. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3832. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3833. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3834. (declare-function calendar-forward-day "cal-move" (arg))
  3835. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3836. (declare-function calendar-goto-date "cal-move" (date))
  3837. (declare-function calendar-goto-today "cal-move" ())
  3838. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3839. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3840. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3841. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3842. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3843. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3844. (defvar calendar-mode-map)
  3845. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3846. (declare-function cdlatex-tab "ext:cdlatex" ())
  3847. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3848. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3849. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3850. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3851. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3852. (defvar font-lock-unfontify-region-function)
  3853. (declare-function gnus-article-show-summary "gnus-art" ())
  3854. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3855. (defvar gnus-other-frame-object)
  3856. (defvar gnus-group-name)
  3857. (defvar gnus-article-current)
  3858. (defvar Info-current-file)
  3859. (defvar Info-current-node)
  3860. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3861. (declare-function mh-find-path "mh-utils" ())
  3862. (declare-function mh-get-header-field "mh-utils" (field))
  3863. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3864. (declare-function mh-header-display "mh-show" ())
  3865. (declare-function mh-index-previous-folder "mh-search" ())
  3866. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3867. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3868. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3869. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3870. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3871. (declare-function mh-show-header-display "mh-show" t t)
  3872. (declare-function mh-show-msg "mh-show" (msg))
  3873. (declare-function mh-show-show "mh-show" t t)
  3874. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3875. (defvar mh-progs)
  3876. (defvar mh-current-folder)
  3877. (defvar mh-show-folder-buffer)
  3878. (defvar mh-index-folder)
  3879. (defvar mh-searcher)
  3880. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3881. (declare-function parse-time-string "parse-time" (string))
  3882. (declare-function remember "remember" (&optional initial))
  3883. (declare-function remember-buffer-desc "remember" ())
  3884. (declare-function remember-finalize "remember" ())
  3885. (defvar remember-save-after-remembering)
  3886. (defvar remember-data-file)
  3887. (defvar remember-register)
  3888. (defvar remember-buffer)
  3889. (defvar remember-handler-functions)
  3890. (defvar remember-annotation-functions)
  3891. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3892. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3893. (declare-function rmail-what-message "rmail" ())
  3894. (defvar rmail-current-message)
  3895. (defvar texmathp-why)
  3896. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3897. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3898. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3899. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3900. (declare-function vm-isearch-update "ext:vm-search" ())
  3901. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3902. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3903. (declare-function vm-su-subject "ext:vm-summary" (m))
  3904. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3905. (defvar vm-message-pointer)
  3906. (defvar vm-folder-directory)
  3907. (defvar w3m-current-url)
  3908. (defvar w3m-current-title)
  3909. ;; backward compatibility to old version of wl
  3910. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3911. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3912. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3913. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3914. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3915. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3916. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3917. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3918. (defvar wl-summary-buffer-elmo-folder)
  3919. (defvar wl-summary-buffer-folder-name)
  3920. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3921. (defvar org-latex-regexps)
  3922. (defvar constants-unit-system)
  3923. ;;; Variables for pre-computed regular expressions, all buffer local
  3924. (defvar org-drawer-regexp nil
  3925. "Matches first line of a hidden block.")
  3926. (make-variable-buffer-local 'org-drawer-regexp)
  3927. (defvar org-todo-regexp nil
  3928. "Matches any of the TODO state keywords.")
  3929. (make-variable-buffer-local 'org-todo-regexp)
  3930. (defvar org-not-done-regexp nil
  3931. "Matches any of the TODO state keywords except the last one.")
  3932. (make-variable-buffer-local 'org-not-done-regexp)
  3933. (defvar org-todo-line-regexp nil
  3934. "Matches a headline and puts TODO state into group 2 if present.")
  3935. (make-variable-buffer-local 'org-todo-line-regexp)
  3936. (defvar org-complex-heading-regexp nil
  3937. "Matches a headline and puts everything into groups:
  3938. group 1: the stars
  3939. group 2: The todo keyword, maybe
  3940. group 3: Priority cookie
  3941. group 4: True headline
  3942. group 5: Tags")
  3943. (make-variable-buffer-local 'org-complex-heading-regexp)
  3944. (defvar org-todo-line-tags-regexp nil
  3945. "Matches a headline and puts TODO state into group 2 if present.
  3946. Also put tags into group 4 if tags are present.")
  3947. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3948. (defvar org-nl-done-regexp nil
  3949. "Matches newline followed by a headline with the DONE keyword.")
  3950. (make-variable-buffer-local 'org-nl-done-regexp)
  3951. (defvar org-looking-at-done-regexp nil
  3952. "Matches the DONE keyword a point.")
  3953. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3954. (defvar org-ds-keyword-length 12
  3955. "Maximum length of the Deadline and SCHEDULED keywords.")
  3956. (make-variable-buffer-local 'org-ds-keyword-length)
  3957. (defvar org-deadline-regexp nil
  3958. "Matches the DEADLINE keyword.")
  3959. (make-variable-buffer-local 'org-deadline-regexp)
  3960. (defvar org-deadline-time-regexp nil
  3961. "Matches the DEADLINE keyword together with a time stamp.")
  3962. (make-variable-buffer-local 'org-deadline-time-regexp)
  3963. (defvar org-deadline-line-regexp nil
  3964. "Matches the DEADLINE keyword and the rest of the line.")
  3965. (make-variable-buffer-local 'org-deadline-line-regexp)
  3966. (defvar org-scheduled-regexp nil
  3967. "Matches the SCHEDULED keyword.")
  3968. (make-variable-buffer-local 'org-scheduled-regexp)
  3969. (defvar org-scheduled-time-regexp nil
  3970. "Matches the SCHEDULED keyword together with a time stamp.")
  3971. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3972. (defvar org-closed-time-regexp nil
  3973. "Matches the CLOSED keyword together with a time stamp.")
  3974. (make-variable-buffer-local 'org-closed-time-regexp)
  3975. (defvar org-keyword-time-regexp nil
  3976. "Matches any of the 4 keywords, together with the time stamp.")
  3977. (make-variable-buffer-local 'org-keyword-time-regexp)
  3978. (defvar org-keyword-time-not-clock-regexp nil
  3979. "Matches any of the 3 keywords, together with the time stamp.")
  3980. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3981. (defvar org-maybe-keyword-time-regexp nil
  3982. "Matches a timestamp, possibly preceeded by a keyword.")
  3983. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3984. (defvar org-planning-or-clock-line-re nil
  3985. "Matches a line with planning or clock info.")
  3986. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3987. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3988. rear-nonsticky t mouse-map t fontified t)
  3989. "Properties to remove when a string without properties is wanted.")
  3990. (defsubst org-match-string-no-properties (num &optional string)
  3991. (if (featurep 'xemacs)
  3992. (let ((s (match-string num string)))
  3993. (remove-text-properties 0 (length s) org-rm-props s)
  3994. s)
  3995. (match-string-no-properties num string)))
  3996. (defsubst org-no-properties (s)
  3997. (if (fboundp 'set-text-properties)
  3998. (set-text-properties 0 (length s) nil s)
  3999. (remove-text-properties 0 (length s) org-rm-props s))
  4000. s)
  4001. (defsubst org-get-alist-option (option key)
  4002. (cond ((eq key t) t)
  4003. ((eq option t) t)
  4004. ((assoc key option) (cdr (assoc key option)))
  4005. (t (cdr (assq 'default option)))))
  4006. (defsubst org-inhibit-invisibility ()
  4007. "Modified `buffer-invisibility-spec' for Emacs 21.
  4008. Some ops with invisible text do not work correctly on Emacs 21. For these
  4009. we turn off invisibility temporarily. Use this in a `let' form."
  4010. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  4011. (defsubst org-set-local (var value)
  4012. "Make VAR local in current buffer and set it to VALUE."
  4013. (set (make-variable-buffer-local var) value))
  4014. (defsubst org-mode-p ()
  4015. "Check if the current buffer is in Org-mode."
  4016. (eq major-mode 'org-mode))
  4017. (defsubst org-last (list)
  4018. "Return the last element of LIST."
  4019. (car (last list)))
  4020. (defun org-let (list &rest body)
  4021. (eval (cons 'let (cons list body))))
  4022. (put 'org-let 'lisp-indent-function 1)
  4023. (defun org-let2 (list1 list2 &rest body)
  4024. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  4025. (put 'org-let2 'lisp-indent-function 2)
  4026. (defconst org-startup-options
  4027. '(("fold" org-startup-folded t)
  4028. ("overview" org-startup-folded t)
  4029. ("nofold" org-startup-folded nil)
  4030. ("showall" org-startup-folded nil)
  4031. ("content" org-startup-folded content)
  4032. ("hidestars" org-hide-leading-stars t)
  4033. ("showstars" org-hide-leading-stars nil)
  4034. ("odd" org-odd-levels-only t)
  4035. ("oddeven" org-odd-levels-only nil)
  4036. ("align" org-startup-align-all-tables t)
  4037. ("noalign" org-startup-align-all-tables nil)
  4038. ("customtime" org-display-custom-times t)
  4039. ("logdone" org-log-done time)
  4040. ("lognotedone" org-log-done note)
  4041. ("nologdone" org-log-done nil)
  4042. ("lognoteclock-out" org-log-note-clock-out t)
  4043. ("nolognoteclock-out" org-log-note-clock-out nil)
  4044. ("logrepeat" org-log-repeat state)
  4045. ("lognoterepeat" org-log-repeat note)
  4046. ("nologrepeat" org-log-repeat nil)
  4047. ("constcgs" constants-unit-system cgs)
  4048. ("constSI" constants-unit-system SI))
  4049. "Variable associated with STARTUP options for org-mode.
  4050. Each element is a list of three items: The startup options as written
  4051. in the #+STARTUP line, the corresponding variable, and the value to
  4052. set this variable to if the option is found. An optional forth element PUSH
  4053. means to push this value onto the list in the variable.")
  4054. (defun org-set-regexps-and-options ()
  4055. "Precompute regular expressions for current buffer."
  4056. (when (org-mode-p)
  4057. (org-set-local 'org-todo-kwd-alist nil)
  4058. (org-set-local 'org-todo-key-alist nil)
  4059. (org-set-local 'org-todo-key-trigger nil)
  4060. (org-set-local 'org-todo-keywords-1 nil)
  4061. (org-set-local 'org-done-keywords nil)
  4062. (org-set-local 'org-todo-heads nil)
  4063. (org-set-local 'org-todo-sets nil)
  4064. (org-set-local 'org-todo-log-states nil)
  4065. (let ((re (org-make-options-regexp
  4066. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  4067. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  4068. "CONSTANTS" "PROPERTY" "DRAWERS")))
  4069. (splitre "[ \t]+")
  4070. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4071. tail sep kws1 prio props drawers)
  4072. (save-excursion
  4073. (save-restriction
  4074. (widen)
  4075. (goto-char (point-min))
  4076. (while (re-search-forward re nil t)
  4077. (setq key (match-string 1) value (org-match-string-no-properties 2))
  4078. (cond
  4079. ((equal key "CATEGORY")
  4080. (if (string-match "[ \t]+$" value)
  4081. (setq value (replace-match "" t t value)))
  4082. (setq cat value))
  4083. ((member key '("SEQ_TODO" "TODO"))
  4084. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4085. ((equal key "TYP_TODO")
  4086. (push (cons 'type (org-split-string value splitre)) kwds))
  4087. ((equal key "TAGS")
  4088. (setq tags (append tags (org-split-string value splitre))))
  4089. ((equal key "COLUMNS")
  4090. (org-set-local 'org-columns-default-format value))
  4091. ((equal key "LINK")
  4092. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4093. (push (cons (match-string 1 value)
  4094. (org-trim (match-string 2 value)))
  4095. links)))
  4096. ((equal key "PRIORITIES")
  4097. (setq prio (org-split-string value " +")))
  4098. ((equal key "PROPERTY")
  4099. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4100. (push (cons (match-string 1 value) (match-string 2 value))
  4101. props)))
  4102. ((equal key "DRAWERS")
  4103. (setq drawers (org-split-string value splitre)))
  4104. ((equal key "CONSTANTS")
  4105. (setq const (append const (org-split-string value splitre))))
  4106. ((equal key "STARTUP")
  4107. (let ((opts (org-split-string value splitre))
  4108. l var val)
  4109. (while (setq l (pop opts))
  4110. (when (setq l (assoc l org-startup-options))
  4111. (setq var (nth 1 l) val (nth 2 l))
  4112. (if (not (nth 3 l))
  4113. (set (make-local-variable var) val)
  4114. (if (not (listp (symbol-value var)))
  4115. (set (make-local-variable var) nil))
  4116. (set (make-local-variable var) (symbol-value var))
  4117. (add-to-list var val))))))
  4118. ((equal key "ARCHIVE")
  4119. (string-match " *$" value)
  4120. (setq arch (replace-match "" t t value))
  4121. (remove-text-properties 0 (length arch)
  4122. '(face t fontified t) arch)))
  4123. )))
  4124. (when cat
  4125. (org-set-local 'org-category (intern cat))
  4126. (push (cons "CATEGORY" cat) props))
  4127. (when prio
  4128. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4129. (setq prio (mapcar 'string-to-char prio))
  4130. (org-set-local 'org-highest-priority (nth 0 prio))
  4131. (org-set-local 'org-lowest-priority (nth 1 prio))
  4132. (org-set-local 'org-default-priority (nth 2 prio)))
  4133. (and props (org-set-local 'org-local-properties (nreverse props)))
  4134. (and drawers (org-set-local 'org-drawers drawers))
  4135. (and arch (org-set-local 'org-archive-location arch))
  4136. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4137. ;; Process the TODO keywords
  4138. (unless kwds
  4139. ;; Use the global values as if they had been given locally.
  4140. (setq kwds (default-value 'org-todo-keywords))
  4141. (if (stringp (car kwds))
  4142. (setq kwds (list (cons org-todo-interpretation
  4143. (default-value 'org-todo-keywords)))))
  4144. (setq kwds (reverse kwds)))
  4145. (setq kwds (nreverse kwds))
  4146. (let (inter kws kw)
  4147. (while (setq kws (pop kwds))
  4148. (setq inter (pop kws) sep (member "|" kws)
  4149. kws0 (delete "|" (copy-sequence kws))
  4150. kwsa nil
  4151. kws1 (mapcar
  4152. (lambda (x)
  4153. ;; 1 2
  4154. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4155. (progn
  4156. (setq kw (match-string 1 x)
  4157. key (and (match-end 2) (match-string 2 x))
  4158. log (org-extract-log-state-settings x))
  4159. (push (cons kw (and key (string-to-char key))) kwsa)
  4160. (and log (push log org-todo-log-states))
  4161. kw)
  4162. (error "Invalid TODO keyword %s" x)))
  4163. kws0)
  4164. kwsa (if kwsa (append '((:startgroup))
  4165. (nreverse kwsa)
  4166. '((:endgroup))))
  4167. hw (car kws1)
  4168. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4169. tail (list inter hw (car dws) (org-last dws)))
  4170. (add-to-list 'org-todo-heads hw 'append)
  4171. (push kws1 org-todo-sets)
  4172. (setq org-done-keywords (append org-done-keywords dws nil))
  4173. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4174. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4175. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4176. (setq org-todo-sets (nreverse org-todo-sets)
  4177. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4178. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4179. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4180. ;; Process the constants
  4181. (when const
  4182. (let (e cst)
  4183. (while (setq e (pop const))
  4184. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4185. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4186. (setq org-table-formula-constants-local cst)))
  4187. ;; Process the tags.
  4188. (when tags
  4189. (let (e tgs)
  4190. (while (setq e (pop tags))
  4191. (cond
  4192. ((equal e "{") (push '(:startgroup) tgs))
  4193. ((equal e "}") (push '(:endgroup) tgs))
  4194. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4195. (push (cons (match-string 1 e)
  4196. (string-to-char (match-string 2 e)))
  4197. tgs))
  4198. (t (push (list e) tgs))))
  4199. (org-set-local 'org-tag-alist nil)
  4200. (while (setq e (pop tgs))
  4201. (or (and (stringp (car e))
  4202. (assoc (car e) org-tag-alist))
  4203. (push e org-tag-alist))))))
  4204. ;; Compute the regular expressions and other local variables
  4205. (if (not org-done-keywords)
  4206. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4207. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4208. (length org-scheduled-string)))
  4209. org-drawer-regexp
  4210. (concat "^[ \t]*:\\("
  4211. (mapconcat 'regexp-quote org-drawers "\\|")
  4212. "\\):[ \t]*$")
  4213. org-not-done-keywords
  4214. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4215. org-todo-regexp
  4216. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4217. "\\|") "\\)\\>")
  4218. org-not-done-regexp
  4219. (concat "\\<\\("
  4220. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4221. "\\)\\>")
  4222. org-todo-line-regexp
  4223. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4224. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4225. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4226. org-complex-heading-regexp
  4227. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4228. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4229. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4230. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4231. org-nl-done-regexp
  4232. (concat "\n\\*+[ \t]+"
  4233. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4234. "\\)" "\\>")
  4235. org-todo-line-tags-regexp
  4236. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4237. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4238. (org-re
  4239. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4240. org-looking-at-done-regexp
  4241. (concat "^" "\\(?:"
  4242. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4243. "\\>")
  4244. org-deadline-regexp (concat "\\<" org-deadline-string)
  4245. org-deadline-time-regexp
  4246. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4247. org-deadline-line-regexp
  4248. (concat "\\<\\(" org-deadline-string "\\).*")
  4249. org-scheduled-regexp
  4250. (concat "\\<" org-scheduled-string)
  4251. org-scheduled-time-regexp
  4252. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4253. org-closed-time-regexp
  4254. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4255. org-keyword-time-regexp
  4256. (concat "\\<\\(" org-scheduled-string
  4257. "\\|" org-deadline-string
  4258. "\\|" org-closed-string
  4259. "\\|" org-clock-string "\\)"
  4260. " *[[<]\\([^]>]+\\)[]>]")
  4261. org-keyword-time-not-clock-regexp
  4262. (concat "\\<\\(" org-scheduled-string
  4263. "\\|" org-deadline-string
  4264. "\\|" org-closed-string
  4265. "\\)"
  4266. " *[[<]\\([^]>]+\\)[]>]")
  4267. org-maybe-keyword-time-regexp
  4268. (concat "\\(\\<\\(" org-scheduled-string
  4269. "\\|" org-deadline-string
  4270. "\\|" org-closed-string
  4271. "\\|" org-clock-string "\\)\\)?"
  4272. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4273. org-planning-or-clock-line-re
  4274. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4275. "\\|" org-deadline-string
  4276. "\\|" org-closed-string "\\|" org-clock-string
  4277. "\\)\\>\\)")
  4278. )
  4279. (org-compute-latex-and-specials-regexp)
  4280. (org-set-font-lock-defaults)))
  4281. (defun org-extract-log-state-settings (x)
  4282. "Extract the log state setting from a TODO keyword string.
  4283. This will extract info from a string like \"WAIT(w@/!)\"."
  4284. (let (kw key log1 log2)
  4285. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4286. (setq kw (match-string 1 x)
  4287. key (and (match-end 2) (match-string 2 x))
  4288. log1 (and (match-end 3) (match-string 3 x))
  4289. log2 (and (match-end 4) (match-string 4 x)))
  4290. (and (or log1 log2)
  4291. (list kw
  4292. (and log1 (if (equal log1 "!") 'time 'note))
  4293. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4294. (defun org-remove-keyword-keys (list)
  4295. "Remove a pair of parenthesis at the end of each string in LIST."
  4296. (mapcar (lambda (x)
  4297. (if (string-match "(.*)$" x)
  4298. (substring x 0 (match-beginning 0))
  4299. x))
  4300. list))
  4301. ;; FIXME: this could be done much better, using second characters etc.
  4302. (defun org-assign-fast-keys (alist)
  4303. "Assign fast keys to a keyword-key alist.
  4304. Respect keys that are already there."
  4305. (let (new e k c c1 c2 (char ?a))
  4306. (while (setq e (pop alist))
  4307. (cond
  4308. ((equal e '(:startgroup)) (push e new))
  4309. ((equal e '(:endgroup)) (push e new))
  4310. (t
  4311. (setq k (car e) c2 nil)
  4312. (if (cdr e)
  4313. (setq c (cdr e))
  4314. ;; automatically assign a character.
  4315. (setq c1 (string-to-char
  4316. (downcase (substring
  4317. k (if (= (string-to-char k) ?@) 1 0)))))
  4318. (if (or (rassoc c1 new) (rassoc c1 alist))
  4319. (while (or (rassoc char new) (rassoc char alist))
  4320. (setq char (1+ char)))
  4321. (setq c2 c1))
  4322. (setq c (or c2 char)))
  4323. (push (cons k c) new))))
  4324. (nreverse new)))
  4325. ;;; Some variables ujsed in various places
  4326. (defvar org-window-configuration nil
  4327. "Used in various places to store a window configuration.")
  4328. (defvar org-finish-function nil
  4329. "Function to be called when `C-c C-c' is used.
  4330. This is for getting out of special buffers like remember.")
  4331. ;; FIXME: Occasionally check by commenting these, to make sure
  4332. ;; no other functions uses these, forgetting to let-bind them.
  4333. (defvar entry)
  4334. (defvar state)
  4335. (defvar last-state)
  4336. (defvar date)
  4337. (defvar description)
  4338. ;; Defined somewhere in this file, but used before definition.
  4339. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4340. (defvar org-agenda-buffer-name)
  4341. (defvar org-agenda-undo-list)
  4342. (defvar org-agenda-pending-undo-list)
  4343. (defvar org-agenda-overriding-header)
  4344. (defvar orgtbl-mode)
  4345. (defvar org-html-entities)
  4346. (defvar org-struct-menu)
  4347. (defvar org-org-menu)
  4348. (defvar org-tbl-menu)
  4349. (defvar org-agenda-keymap)
  4350. ;;;; Emacs/XEmacs compatibility
  4351. ;; Overlay compatibility functions
  4352. (defun org-make-overlay (beg end &optional buffer)
  4353. (if (featurep 'xemacs)
  4354. (make-extent beg end buffer)
  4355. (make-overlay beg end buffer)))
  4356. (defun org-delete-overlay (ovl)
  4357. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4358. (defun org-detach-overlay (ovl)
  4359. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4360. (defun org-move-overlay (ovl beg end &optional buffer)
  4361. (if (featurep 'xemacs)
  4362. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4363. (move-overlay ovl beg end buffer)))
  4364. (defun org-overlay-put (ovl prop value)
  4365. (if (featurep 'xemacs)
  4366. (set-extent-property ovl prop value)
  4367. (overlay-put ovl prop value)))
  4368. (defun org-overlay-display (ovl text &optional face evap)
  4369. "Make overlay OVL display TEXT with face FACE."
  4370. (if (featurep 'xemacs)
  4371. (let ((gl (make-glyph text)))
  4372. (and face (set-glyph-face gl face))
  4373. (set-extent-property ovl 'invisible t)
  4374. (set-extent-property ovl 'end-glyph gl))
  4375. (overlay-put ovl 'display text)
  4376. (if face (overlay-put ovl 'face face))
  4377. (if evap (overlay-put ovl 'evaporate t))))
  4378. (defun org-overlay-before-string (ovl text &optional face evap)
  4379. "Make overlay OVL display TEXT with face FACE."
  4380. (if (featurep 'xemacs)
  4381. (let ((gl (make-glyph text)))
  4382. (and face (set-glyph-face gl face))
  4383. (set-extent-property ovl 'begin-glyph gl))
  4384. (if face (org-add-props text nil 'face face))
  4385. (overlay-put ovl 'before-string text)
  4386. (if evap (overlay-put ovl 'evaporate t))))
  4387. (defun org-overlay-get (ovl prop)
  4388. (if (featurep 'xemacs)
  4389. (extent-property ovl prop)
  4390. (overlay-get ovl prop)))
  4391. (defun org-overlays-at (pos)
  4392. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4393. (defun org-overlays-in (&optional start end)
  4394. (if (featurep 'xemacs)
  4395. (extent-list nil start end)
  4396. (overlays-in start end)))
  4397. (defun org-overlay-start (o)
  4398. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4399. (defun org-overlay-end (o)
  4400. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4401. (defun org-find-overlays (prop &optional pos delete)
  4402. "Find all overlays specifying PROP at POS or point.
  4403. If DELETE is non-nil, delete all those overlays."
  4404. (let ((overlays (org-overlays-at (or pos (point))))
  4405. ov found)
  4406. (while (setq ov (pop overlays))
  4407. (if (org-overlay-get ov prop)
  4408. (if delete (org-delete-overlay ov) (push ov found))))
  4409. found))
  4410. ;; Region compatibility
  4411. (defun org-add-hook (hook function &optional append local)
  4412. "Add-hook, compatible with both Emacsen."
  4413. (if (and local (featurep 'xemacs))
  4414. (add-local-hook hook function append)
  4415. (add-hook hook function append local)))
  4416. (defvar org-ignore-region nil
  4417. "To temporarily disable the active region.")
  4418. (defun org-region-active-p ()
  4419. "Is `transient-mark-mode' on and the region active?
  4420. Works on both Emacs and XEmacs."
  4421. (if org-ignore-region
  4422. nil
  4423. (if (featurep 'xemacs)
  4424. (and zmacs-regions (region-active-p))
  4425. (if (fboundp 'use-region-p)
  4426. (use-region-p)
  4427. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4428. ;; Invisibility compatibility
  4429. (defun org-add-to-invisibility-spec (arg)
  4430. "Add elements to `buffer-invisibility-spec'.
  4431. See documentation for `buffer-invisibility-spec' for the kind of elements
  4432. that can be added."
  4433. (cond
  4434. ((fboundp 'add-to-invisibility-spec)
  4435. (add-to-invisibility-spec arg))
  4436. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4437. (setq buffer-invisibility-spec (list arg)))
  4438. (t
  4439. (setq buffer-invisibility-spec
  4440. (cons arg buffer-invisibility-spec)))))
  4441. (defun org-remove-from-invisibility-spec (arg)
  4442. "Remove elements from `buffer-invisibility-spec'."
  4443. (if (fboundp 'remove-from-invisibility-spec)
  4444. (remove-from-invisibility-spec arg)
  4445. (if (consp buffer-invisibility-spec)
  4446. (setq buffer-invisibility-spec
  4447. (delete arg buffer-invisibility-spec)))))
  4448. (defun org-in-invisibility-spec-p (arg)
  4449. "Is ARG a member of `buffer-invisibility-spec'?"
  4450. (if (consp buffer-invisibility-spec)
  4451. (member arg buffer-invisibility-spec)
  4452. nil))
  4453. ;;;; Define the Org-mode
  4454. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4455. (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."))
  4456. ;; We use a before-change function to check if a table might need
  4457. ;; an update.
  4458. (defvar org-table-may-need-update t
  4459. "Indicates that a table might need an update.
  4460. This variable is set by `org-before-change-function'.
  4461. `org-table-align' sets it back to nil.")
  4462. (defvar org-mode-map)
  4463. (defvar org-mode-hook nil)
  4464. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4465. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4466. (defvar org-table-buffer-is-an nil)
  4467. (defconst org-outline-regexp "\\*+ ")
  4468. ;;;###autoload
  4469. (define-derived-mode org-mode outline-mode "Org"
  4470. "Outline-based notes management and organizer, alias
  4471. \"Carsten's outline-mode for keeping track of everything.\"
  4472. Org-mode develops organizational tasks around a NOTES file which
  4473. contains information about projects as plain text. Org-mode is
  4474. implemented on top of outline-mode, which is ideal to keep the content
  4475. of large files well structured. It supports ToDo items, deadlines and
  4476. time stamps, which magically appear in the diary listing of the Emacs
  4477. calendar. Tables are easily created with a built-in table editor.
  4478. Plain text URL-like links connect to websites, emails (VM), Usenet
  4479. messages (Gnus), BBDB entries, and any files related to the project.
  4480. For printing and sharing of notes, an Org-mode file (or a part of it)
  4481. can be exported as a structured ASCII or HTML file.
  4482. The following commands are available:
  4483. \\{org-mode-map}"
  4484. ;; Get rid of Outline menus, they are not needed
  4485. ;; Need to do this here because define-derived-mode sets up
  4486. ;; the keymap so late. Still, it is a waste to call this each time
  4487. ;; we switch another buffer into org-mode.
  4488. (if (featurep 'xemacs)
  4489. (when (boundp 'outline-mode-menu-heading)
  4490. ;; Assume this is Greg's port, it used easymenu
  4491. (easy-menu-remove outline-mode-menu-heading)
  4492. (easy-menu-remove outline-mode-menu-show)
  4493. (easy-menu-remove outline-mode-menu-hide))
  4494. (define-key org-mode-map [menu-bar headings] 'undefined)
  4495. (define-key org-mode-map [menu-bar hide] 'undefined)
  4496. (define-key org-mode-map [menu-bar show] 'undefined))
  4497. (easy-menu-add org-org-menu)
  4498. (easy-menu-add org-tbl-menu)
  4499. (org-install-agenda-files-menu)
  4500. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4501. (org-add-to-invisibility-spec '(org-cwidth))
  4502. (when (featurep 'xemacs)
  4503. (org-set-local 'line-move-ignore-invisible t))
  4504. (org-set-local 'outline-regexp org-outline-regexp)
  4505. (org-set-local 'outline-level 'org-outline-level)
  4506. (when (and org-ellipsis
  4507. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4508. (fboundp 'make-glyph-code))
  4509. (unless org-display-table
  4510. (setq org-display-table (make-display-table)))
  4511. (set-display-table-slot
  4512. org-display-table 4
  4513. (vconcat (mapcar
  4514. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4515. org-ellipsis)))
  4516. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4517. (setq buffer-display-table org-display-table))
  4518. (org-set-regexps-and-options)
  4519. ;; Calc embedded
  4520. (org-set-local 'calc-embedded-open-mode "# ")
  4521. (modify-syntax-entry ?# "<")
  4522. (modify-syntax-entry ?@ "w")
  4523. (if org-startup-truncated (setq truncate-lines t))
  4524. (org-set-local 'font-lock-unfontify-region-function
  4525. 'org-unfontify-region)
  4526. ;; Activate before-change-function
  4527. (org-set-local 'org-table-may-need-update t)
  4528. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4529. 'local)
  4530. ;; Check for running clock before killing a buffer
  4531. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4532. ;; Paragraphs and auto-filling
  4533. (org-set-autofill-regexps)
  4534. (setq indent-line-function 'org-indent-line-function)
  4535. (org-update-radio-target-regexp)
  4536. ;; Comment characters
  4537. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4538. (org-set-local 'comment-padding " ")
  4539. ;; Align options lines
  4540. (org-set-local
  4541. 'align-mode-rules-list
  4542. '((org-in-buffer-settings
  4543. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4544. (modes . '(org-mode)))))
  4545. ;; Imenu
  4546. (org-set-local 'imenu-create-index-function
  4547. 'org-imenu-get-tree)
  4548. ;; Make isearch reveal context
  4549. (if (or (featurep 'xemacs)
  4550. (not (boundp 'outline-isearch-open-invisible-function)))
  4551. ;; Emacs 21 and XEmacs make use of the hook
  4552. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4553. ;; Emacs 22 deals with this through a special variable
  4554. (org-set-local 'outline-isearch-open-invisible-function
  4555. (lambda (&rest ignore) (org-show-context 'isearch))))
  4556. ;; If empty file that did not turn on org-mode automatically, make it to.
  4557. (if (and org-insert-mode-line-in-empty-file
  4558. (interactive-p)
  4559. (= (point-min) (point-max)))
  4560. (insert "# -*- mode: org -*-\n\n"))
  4561. (unless org-inhibit-startup
  4562. (when org-startup-align-all-tables
  4563. (let ((bmp (buffer-modified-p)))
  4564. (org-table-map-tables 'org-table-align)
  4565. (set-buffer-modified-p bmp)))
  4566. (org-cycle-hide-drawers 'all)
  4567. (cond
  4568. ((eq org-startup-folded t)
  4569. (org-cycle '(4)))
  4570. ((eq org-startup-folded 'content)
  4571. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4572. (org-cycle '(4)) (org-cycle '(4)))))))
  4573. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4574. (defsubst org-call-with-arg (command arg)
  4575. "Call COMMAND interactively, but pretend prefix are was ARG."
  4576. (let ((current-prefix-arg arg)) (call-interactively command)))
  4577. (defsubst org-current-line (&optional pos)
  4578. (save-excursion
  4579. (and pos (goto-char pos))
  4580. ;; works also in narrowed buffer, because we start at 1, not point-min
  4581. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4582. (defun org-current-time ()
  4583. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4584. (if (> (car org-time-stamp-rounding-minutes) 1)
  4585. (let ((r (car org-time-stamp-rounding-minutes))
  4586. (time (decode-time)))
  4587. (apply 'encode-time
  4588. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4589. (nthcdr 2 time))))
  4590. (current-time)))
  4591. (defun org-add-props (string plist &rest props)
  4592. "Add text properties to entire string, from beginning to end.
  4593. PLIST may be a list of properties, PROPS are individual properties and values
  4594. that will be added to PLIST. Returns the string that was modified."
  4595. (add-text-properties
  4596. 0 (length string) (if props (append plist props) plist) string)
  4597. string)
  4598. (put 'org-add-props 'lisp-indent-function 2)
  4599. ;;;; Font-Lock stuff, including the activators
  4600. (defvar org-mouse-map (make-sparse-keymap))
  4601. (org-defkey org-mouse-map
  4602. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4603. (org-defkey org-mouse-map
  4604. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4605. (when org-mouse-1-follows-link
  4606. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4607. (when org-tab-follows-link
  4608. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4609. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4610. (when org-return-follows-link
  4611. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4612. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4613. (require 'font-lock)
  4614. (defconst org-non-link-chars "]\t\n\r<>")
  4615. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4616. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4617. (defvar org-link-re-with-space nil
  4618. "Matches a link with spaces, optional angular brackets around it.")
  4619. (defvar org-link-re-with-space2 nil
  4620. "Matches a link with spaces, optional angular brackets around it.")
  4621. (defvar org-angle-link-re nil
  4622. "Matches link with angular brackets, spaces are allowed.")
  4623. (defvar org-plain-link-re nil
  4624. "Matches plain link, without spaces.")
  4625. (defvar org-bracket-link-regexp nil
  4626. "Matches a link in double brackets.")
  4627. (defvar org-bracket-link-analytic-regexp nil
  4628. "Regular expression used to analyze links.
  4629. Here is what the match groups contain after a match:
  4630. 1: http:
  4631. 2: http
  4632. 3: path
  4633. 4: [desc]
  4634. 5: desc")
  4635. (defvar org-any-link-re nil
  4636. "Regular expression matching any link.")
  4637. (defun org-make-link-regexps ()
  4638. "Update the link regular expressions.
  4639. This should be called after the variable `org-link-types' has changed."
  4640. (setq org-link-re-with-space
  4641. (concat
  4642. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4643. "\\([^" org-non-link-chars " ]"
  4644. "[^" org-non-link-chars "]*"
  4645. "[^" org-non-link-chars " ]\\)>?")
  4646. org-link-re-with-space2
  4647. (concat
  4648. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4649. "\\([^" org-non-link-chars " ]"
  4650. "[^]\t\n\r]*"
  4651. "[^" org-non-link-chars " ]\\)>?")
  4652. org-angle-link-re
  4653. (concat
  4654. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4655. "\\([^" org-non-link-chars " ]"
  4656. "[^" org-non-link-chars "]*"
  4657. "\\)>")
  4658. org-plain-link-re
  4659. (concat
  4660. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4661. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4662. org-bracket-link-regexp
  4663. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4664. org-bracket-link-analytic-regexp
  4665. (concat
  4666. "\\[\\["
  4667. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4668. "\\([^]]+\\)"
  4669. "\\]"
  4670. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4671. "\\]")
  4672. org-any-link-re
  4673. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4674. org-angle-link-re "\\)\\|\\("
  4675. org-plain-link-re "\\)")))
  4676. (org-make-link-regexps)
  4677. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4678. "Regular expression for fast time stamp matching.")
  4679. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4680. "Regular expression for fast time stamp matching.")
  4681. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4682. "Regular expression matching time strings for analysis.
  4683. This one does not require the space after the date.")
  4684. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4685. "Regular expression matching time strings for analysis.")
  4686. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4687. "Regular expression matching time stamps, with groups.")
  4688. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4689. "Regular expression matching time stamps (also [..]), with groups.")
  4690. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4691. "Regular expression matching a time stamp range.")
  4692. (defconst org-tr-regexp-both
  4693. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4694. "Regular expression matching a time stamp range.")
  4695. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4696. org-ts-regexp "\\)?")
  4697. "Regular expression matching a time stamp or time stamp range.")
  4698. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4699. org-ts-regexp-both "\\)?")
  4700. "Regular expression matching a time stamp or time stamp range.
  4701. The time stamps may be either active or inactive.")
  4702. (defvar org-emph-face nil)
  4703. (defun org-do-emphasis-faces (limit)
  4704. "Run through the buffer and add overlays to links."
  4705. (let (rtn)
  4706. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4707. (if (not (= (char-after (match-beginning 3))
  4708. (char-after (match-beginning 4))))
  4709. (progn
  4710. (setq rtn t)
  4711. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4712. 'face
  4713. (nth 1 (assoc (match-string 3)
  4714. org-emphasis-alist)))
  4715. (add-text-properties (match-beginning 2) (match-end 2)
  4716. '(font-lock-multiline t))
  4717. (when org-hide-emphasis-markers
  4718. (add-text-properties (match-end 4) (match-beginning 5)
  4719. '(invisible org-link))
  4720. (add-text-properties (match-beginning 3) (match-end 3)
  4721. '(invisible org-link)))))
  4722. (backward-char 1))
  4723. rtn))
  4724. (defun org-emphasize (&optional char)
  4725. "Insert or change an emphasis, i.e. a font like bold or italic.
  4726. If there is an active region, change that region to a new emphasis.
  4727. If there is no region, just insert the marker characters and position
  4728. the cursor between them.
  4729. CHAR should be either the marker character, or the first character of the
  4730. HTML tag associated with that emphasis. If CHAR is a space, the means
  4731. to remove the emphasis of the selected region.
  4732. If char is not given (for example in an interactive call) it
  4733. will be prompted for."
  4734. (interactive)
  4735. (let ((eal org-emphasis-alist) e det
  4736. (erc org-emphasis-regexp-components)
  4737. (prompt "")
  4738. (string "") beg end move tag c s)
  4739. (if (org-region-active-p)
  4740. (setq beg (region-beginning) end (region-end)
  4741. string (buffer-substring beg end))
  4742. (setq move t))
  4743. (while (setq e (pop eal))
  4744. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4745. c (aref tag 0))
  4746. (push (cons c (string-to-char (car e))) det)
  4747. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4748. (substring tag 1)))))
  4749. (unless char
  4750. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4751. (setq char (read-char-exclusive)))
  4752. (setq char (or (cdr (assoc char det)) char))
  4753. (if (equal char ?\ )
  4754. (setq s "" move nil)
  4755. (unless (assoc (char-to-string char) org-emphasis-alist)
  4756. (error "No such emphasis marker: \"%c\"" char))
  4757. (setq s (char-to-string char)))
  4758. (while (and (> (length string) 1)
  4759. (equal (substring string 0 1) (substring string -1))
  4760. (assoc (substring string 0 1) org-emphasis-alist))
  4761. (setq string (substring string 1 -1)))
  4762. (setq string (concat s string s))
  4763. (if beg (delete-region beg end))
  4764. (unless (or (bolp)
  4765. (string-match (concat "[" (nth 0 erc) "\n]")
  4766. (char-to-string (char-before (point)))))
  4767. (insert " "))
  4768. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4769. (char-to-string (char-after (point))))
  4770. (insert " ") (backward-char 1))
  4771. (insert string)
  4772. (and move (backward-char 1))))
  4773. (defconst org-nonsticky-props
  4774. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4775. (defun org-activate-plain-links (limit)
  4776. "Run through the buffer and add overlays to links."
  4777. (catch 'exit
  4778. (let (f)
  4779. (while (re-search-forward org-plain-link-re limit t)
  4780. (setq f (get-text-property (match-beginning 0) 'face))
  4781. (if (or (eq f 'org-tag)
  4782. (and (listp f) (memq 'org-tag f)))
  4783. nil
  4784. (add-text-properties (match-beginning 0) (match-end 0)
  4785. (list 'mouse-face 'highlight
  4786. 'rear-nonsticky org-nonsticky-props
  4787. 'keymap org-mouse-map
  4788. ))
  4789. (throw 'exit t))))))
  4790. (defun org-activate-code (limit)
  4791. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4792. (unless (get-text-property (match-beginning 1) 'face)
  4793. (remove-text-properties (match-beginning 0) (match-end 0)
  4794. '(display t invisible t intangible t))
  4795. t)))
  4796. (defun org-activate-angle-links (limit)
  4797. "Run through the buffer and add overlays to links."
  4798. (if (re-search-forward org-angle-link-re limit t)
  4799. (progn
  4800. (add-text-properties (match-beginning 0) (match-end 0)
  4801. (list 'mouse-face 'highlight
  4802. 'rear-nonsticky org-nonsticky-props
  4803. 'keymap org-mouse-map
  4804. ))
  4805. t)))
  4806. (defmacro org-maybe-intangible (props)
  4807. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4808. In emacs 21, invisible text is not avoided by the command loop, so the
  4809. intangible property is needed to make sure point skips this text.
  4810. In Emacs 22, this is not necessary. The intangible text property has
  4811. led to problems with flyspell. These problems are fixed in flyspell.el,
  4812. but we still avoid setting the property in Emacs 22 and later.
  4813. We use a macro so that the test can happen at compilation time."
  4814. (if (< emacs-major-version 22)
  4815. `(append '(intangible t) ,props)
  4816. props))
  4817. (defun org-activate-bracket-links (limit)
  4818. "Run through the buffer and add overlays to bracketed links."
  4819. (if (re-search-forward org-bracket-link-regexp limit t)
  4820. (let* ((help (concat "LINK: "
  4821. (org-match-string-no-properties 1)))
  4822. ;; FIXME: above we should remove the escapes.
  4823. ;; but that requires another match, protecting match data,
  4824. ;; a lot of overhead for font-lock.
  4825. (ip (org-maybe-intangible
  4826. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4827. 'keymap org-mouse-map 'mouse-face 'highlight
  4828. 'font-lock-multiline t 'help-echo help)))
  4829. (vp (list 'rear-nonsticky org-nonsticky-props
  4830. 'keymap org-mouse-map 'mouse-face 'highlight
  4831. ' font-lock-multiline t 'help-echo help)))
  4832. ;; We need to remove the invisible property here. Table narrowing
  4833. ;; may have made some of this invisible.
  4834. (remove-text-properties (match-beginning 0) (match-end 0)
  4835. '(invisible nil))
  4836. (if (match-end 3)
  4837. (progn
  4838. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4839. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4840. (add-text-properties (match-end 3) (match-end 0) ip))
  4841. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4842. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4843. (add-text-properties (match-end 1) (match-end 0) ip))
  4844. t)))
  4845. (defun org-activate-dates (limit)
  4846. "Run through the buffer and add overlays to dates."
  4847. (if (re-search-forward org-tsr-regexp-both limit t)
  4848. (progn
  4849. (add-text-properties (match-beginning 0) (match-end 0)
  4850. (list 'mouse-face 'highlight
  4851. 'rear-nonsticky org-nonsticky-props
  4852. 'keymap org-mouse-map))
  4853. (when org-display-custom-times
  4854. (if (match-end 3)
  4855. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4856. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4857. t)))
  4858. (defvar org-target-link-regexp nil
  4859. "Regular expression matching radio targets in plain text.")
  4860. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4861. "Regular expression matching a link target.")
  4862. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4863. "Regular expression matching a radio target.")
  4864. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4865. "Regular expression matching any target.")
  4866. (defun org-activate-target-links (limit)
  4867. "Run through the buffer and add overlays to target matches."
  4868. (when org-target-link-regexp
  4869. (let ((case-fold-search t))
  4870. (if (re-search-forward org-target-link-regexp limit t)
  4871. (progn
  4872. (add-text-properties (match-beginning 0) (match-end 0)
  4873. (list 'mouse-face 'highlight
  4874. 'rear-nonsticky org-nonsticky-props
  4875. 'keymap org-mouse-map
  4876. 'help-echo "Radio target link"
  4877. 'org-linked-text t))
  4878. t)))))
  4879. (defun org-update-radio-target-regexp ()
  4880. "Find all radio targets in this file and update the regular expression."
  4881. (interactive)
  4882. (when (memq 'radio org-activate-links)
  4883. (setq org-target-link-regexp
  4884. (org-make-target-link-regexp (org-all-targets 'radio)))
  4885. (org-restart-font-lock)))
  4886. (defun org-hide-wide-columns (limit)
  4887. (let (s e)
  4888. (setq s (text-property-any (point) (or limit (point-max))
  4889. 'org-cwidth t))
  4890. (when s
  4891. (setq e (next-single-property-change s 'org-cwidth))
  4892. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4893. (goto-char e)
  4894. t)))
  4895. (defvar org-latex-and-specials-regexp nil
  4896. "Regular expression for highlighting export special stuff.")
  4897. (defvar org-match-substring-regexp)
  4898. (defvar org-match-substring-with-braces-regexp)
  4899. (defvar org-export-html-special-string-regexps)
  4900. (defun org-compute-latex-and-specials-regexp ()
  4901. "Compute regular expression for stuff treated specially by exporters."
  4902. (if (not org-highlight-latex-fragments-and-specials)
  4903. (org-set-local 'org-latex-and-specials-regexp nil)
  4904. (let*
  4905. ((matchers (plist-get org-format-latex-options :matchers))
  4906. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4907. org-latex-regexps)))
  4908. (options (org-combine-plists (org-default-export-plist)
  4909. (org-infile-export-plist)))
  4910. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4911. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4912. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4913. (org-export-html-expand (plist-get options :expand-quoted-html))
  4914. (org-export-with-special-strings (plist-get options :special-strings))
  4915. (re-sub
  4916. (cond
  4917. ((equal org-export-with-sub-superscripts '{})
  4918. (list org-match-substring-with-braces-regexp))
  4919. (org-export-with-sub-superscripts
  4920. (list org-match-substring-regexp))
  4921. (t nil)))
  4922. (re-latex
  4923. (if org-export-with-LaTeX-fragments
  4924. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4925. (re-macros
  4926. (if org-export-with-TeX-macros
  4927. (list (concat "\\\\"
  4928. (regexp-opt
  4929. (append (mapcar 'car org-html-entities)
  4930. (if (boundp 'org-latex-entities)
  4931. org-latex-entities nil))
  4932. 'words))) ; FIXME
  4933. ))
  4934. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4935. (re-special (if org-export-with-special-strings
  4936. (mapcar (lambda (x) (car x))
  4937. org-export-html-special-string-regexps)))
  4938. (re-rest
  4939. (delq nil
  4940. (list
  4941. (if org-export-html-expand "@<[^>\n]+>")
  4942. ))))
  4943. (org-set-local
  4944. 'org-latex-and-specials-regexp
  4945. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4946. re-rest) "\\|")))))
  4947. (defface org-latex-and-export-specials
  4948. (let ((font (cond ((assq :inherit custom-face-attributes)
  4949. '(:inherit underline))
  4950. (t '(:underline t)))))
  4951. `((((class grayscale) (background light))
  4952. (:foreground "DimGray" ,@font))
  4953. (((class grayscale) (background dark))
  4954. (:foreground "LightGray" ,@font))
  4955. (((class color) (background light))
  4956. (:foreground "SaddleBrown"))
  4957. (((class color) (background dark))
  4958. (:foreground "burlywood"))
  4959. (t (,@font))))
  4960. "Face used to highlight math latex and other special exporter stuff."
  4961. :group 'org-faces)
  4962. (defun org-do-latex-and-special-faces (limit)
  4963. "Run through the buffer and add overlays to links."
  4964. (when org-latex-and-specials-regexp
  4965. (let (rtn d)
  4966. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4967. limit t))
  4968. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4969. 'face))
  4970. '(org-code org-verbatim underline)))
  4971. (progn
  4972. (setq rtn t
  4973. d (cond ((member (char-after (1+ (match-beginning 0)))
  4974. '(?_ ?^)) 1)
  4975. (t 0)))
  4976. (font-lock-prepend-text-property
  4977. (+ d (match-beginning 0)) (match-end 0)
  4978. 'face 'org-latex-and-export-specials)
  4979. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4980. '(font-lock-multiline t)))))
  4981. rtn)))
  4982. (defun org-restart-font-lock ()
  4983. "Restart font-lock-mode, to force refontification."
  4984. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4985. (font-lock-mode -1)
  4986. (font-lock-mode 1)))
  4987. (defun org-all-targets (&optional radio)
  4988. "Return a list of all targets in this file.
  4989. With optional argument RADIO, only find radio targets."
  4990. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4991. rtn)
  4992. (save-excursion
  4993. (goto-char (point-min))
  4994. (while (re-search-forward re nil t)
  4995. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4996. rtn)))
  4997. (defun org-make-target-link-regexp (targets)
  4998. "Make regular expression matching all strings in TARGETS.
  4999. The regular expression finds the targets also if there is a line break
  5000. between words."
  5001. (and targets
  5002. (concat
  5003. "\\<\\("
  5004. (mapconcat
  5005. (lambda (x)
  5006. (while (string-match " +" x)
  5007. (setq x (replace-match "\\s-+" t t x)))
  5008. x)
  5009. targets
  5010. "\\|")
  5011. "\\)\\>")))
  5012. (defun org-activate-tags (limit)
  5013. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  5014. (progn
  5015. (add-text-properties (match-beginning 1) (match-end 1)
  5016. (list 'mouse-face 'highlight
  5017. 'rear-nonsticky org-nonsticky-props
  5018. 'keymap org-mouse-map))
  5019. t)))
  5020. (defun org-outline-level ()
  5021. (save-excursion
  5022. (looking-at outline-regexp)
  5023. (if (match-beginning 1)
  5024. (+ (org-get-string-indentation (match-string 1)) 1000)
  5025. (1- (- (match-end 0) (match-beginning 0))))))
  5026. (defvar org-font-lock-keywords nil)
  5027. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  5028. "Regular expression matching a property line.")
  5029. (defun org-set-font-lock-defaults ()
  5030. (let* ((em org-fontify-emphasized-text)
  5031. (lk org-activate-links)
  5032. (org-font-lock-extra-keywords
  5033. (list
  5034. ;; Headlines
  5035. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  5036. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  5037. ;; Table lines
  5038. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5039. (1 'org-table t))
  5040. ;; Table internals
  5041. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5042. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5043. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5044. ;; Drawers
  5045. (list org-drawer-regexp '(0 'org-special-keyword t))
  5046. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5047. ;; Properties
  5048. (list org-property-re
  5049. '(1 'org-special-keyword t)
  5050. '(3 'org-property-value t))
  5051. (if org-format-transports-properties-p
  5052. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  5053. ;; Links
  5054. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5055. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5056. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5057. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5058. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5059. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5060. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5061. '(org-hide-wide-columns (0 nil append))
  5062. ;; TODO lines
  5063. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  5064. '(1 (org-get-todo-face 1) t))
  5065. ;; DONE
  5066. (if org-fontify-done-headline
  5067. (list (concat "^[*]+ +\\<\\("
  5068. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5069. "\\)\\(.*\\)")
  5070. '(2 'org-headline-done t))
  5071. nil)
  5072. ;; Priorities
  5073. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  5074. ;; Special keywords
  5075. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5076. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5077. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5078. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5079. ;; Emphasis
  5080. (if em
  5081. (if (featurep 'xemacs)
  5082. '(org-do-emphasis-faces (0 nil append))
  5083. '(org-do-emphasis-faces)))
  5084. ;; Checkboxes
  5085. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  5086. 2 'bold prepend)
  5087. (if org-provide-checkbox-statistics
  5088. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5089. (0 (org-get-checkbox-statistics-face) t)))
  5090. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5091. '(1 'org-archived prepend))
  5092. ;; Specials
  5093. '(org-do-latex-and-special-faces)
  5094. ;; Code
  5095. '(org-activate-code (1 'org-code t))
  5096. ;; COMMENT
  5097. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5098. "\\|" org-quote-string "\\)\\>")
  5099. '(1 'org-special-keyword t))
  5100. '("^#.*" (0 'font-lock-comment-face t))
  5101. )))
  5102. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5103. ;; Now set the full font-lock-keywords
  5104. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5105. (org-set-local 'font-lock-defaults
  5106. '(org-font-lock-keywords t nil nil backward-paragraph))
  5107. (kill-local-variable 'font-lock-keywords) nil))
  5108. (defvar org-m nil)
  5109. (defvar org-l nil)
  5110. (defvar org-f nil)
  5111. (defun org-get-level-face (n)
  5112. "Get the right face for match N in font-lock matching of healdines."
  5113. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5114. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5115. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5116. (cond
  5117. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5118. ((eq n 2) org-f)
  5119. (t (if org-level-color-stars-only nil org-f))))
  5120. (defun org-get-todo-face (kwd)
  5121. "Get the right face for a TODO keyword KWD.
  5122. If KWD is a number, get the corresponding match group."
  5123. (if (numberp kwd) (setq kwd (match-string kwd)))
  5124. (or (cdr (assoc kwd org-todo-keyword-faces))
  5125. (and (member kwd org-done-keywords) 'org-done)
  5126. 'org-todo))
  5127. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5128. "Remove fontification and activation overlays from links."
  5129. (font-lock-default-unfontify-region beg end)
  5130. (let* ((buffer-undo-list t)
  5131. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5132. (inhibit-modification-hooks t)
  5133. deactivate-mark buffer-file-name buffer-file-truename)
  5134. (remove-text-properties beg end
  5135. '(mouse-face t keymap t org-linked-text t
  5136. invisible t intangible t))))
  5137. ;;;; Visibility cycling, including org-goto and indirect buffer
  5138. ;;; Cycling
  5139. (defvar org-cycle-global-status nil)
  5140. (make-variable-buffer-local 'org-cycle-global-status)
  5141. (defvar org-cycle-subtree-status nil)
  5142. (make-variable-buffer-local 'org-cycle-subtree-status)
  5143. ;;;###autoload
  5144. (defun org-cycle (&optional arg)
  5145. "Visibility cycling for Org-mode.
  5146. - When this function is called with a prefix argument, rotate the entire
  5147. buffer through 3 states (global cycling)
  5148. 1. OVERVIEW: Show only top-level headlines.
  5149. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5150. 3. SHOW ALL: Show everything.
  5151. - When point is at the beginning of a headline, rotate the subtree started
  5152. by this line through 3 different states (local cycling)
  5153. 1. FOLDED: Only the main headline is shown.
  5154. 2. CHILDREN: The main headline and the direct children are shown.
  5155. From this state, you can move to one of the children
  5156. and zoom in further.
  5157. 3. SUBTREE: Show the entire subtree, including body text.
  5158. - When there is a numeric prefix, go up to a heading with level ARG, do
  5159. a `show-subtree' and return to the previous cursor position. If ARG
  5160. is negative, go up that many levels.
  5161. - When point is not at the beginning of a headline, execute
  5162. `indent-relative', like TAB normally does. See the option
  5163. `org-cycle-emulate-tab' for details.
  5164. - Special case: if point is at the beginning of the buffer and there is
  5165. no headline in line 1, this function will act as if called with prefix arg.
  5166. But only if also the variable `org-cycle-global-at-bob' is t."
  5167. (interactive "P")
  5168. (let* ((outline-regexp
  5169. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5170. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5171. outline-regexp))
  5172. (bob-special (and org-cycle-global-at-bob (bobp)
  5173. (not (looking-at outline-regexp))))
  5174. (org-cycle-hook
  5175. (if bob-special
  5176. (delq 'org-optimize-window-after-visibility-change
  5177. (copy-sequence org-cycle-hook))
  5178. org-cycle-hook))
  5179. (pos (point)))
  5180. (if (or bob-special (equal arg '(4)))
  5181. ;; special case: use global cycling
  5182. (setq arg t))
  5183. (cond
  5184. ((org-at-table-p 'any)
  5185. ;; Enter the table or move to the next field in the table
  5186. (or (org-table-recognize-table.el)
  5187. (progn
  5188. (if arg (org-table-edit-field t)
  5189. (org-table-justify-field-maybe)
  5190. (call-interactively 'org-table-next-field)))))
  5191. ((eq arg t) ;; Global cycling
  5192. (cond
  5193. ((and (eq last-command this-command)
  5194. (eq org-cycle-global-status 'overview))
  5195. ;; We just created the overview - now do table of contents
  5196. ;; This can be slow in very large buffers, so indicate action
  5197. (message "CONTENTS...")
  5198. (org-content)
  5199. (message "CONTENTS...done")
  5200. (setq org-cycle-global-status 'contents)
  5201. (run-hook-with-args 'org-cycle-hook 'contents))
  5202. ((and (eq last-command this-command)
  5203. (eq org-cycle-global-status 'contents))
  5204. ;; We just showed the table of contents - now show everything
  5205. (show-all)
  5206. (message "SHOW ALL")
  5207. (setq org-cycle-global-status 'all)
  5208. (run-hook-with-args 'org-cycle-hook 'all))
  5209. (t
  5210. ;; Default action: go to overview
  5211. (org-overview)
  5212. (message "OVERVIEW")
  5213. (setq org-cycle-global-status 'overview)
  5214. (run-hook-with-args 'org-cycle-hook 'overview))))
  5215. ((and org-drawers org-drawer-regexp
  5216. (save-excursion
  5217. (beginning-of-line 1)
  5218. (looking-at org-drawer-regexp)))
  5219. ;; Toggle block visibility
  5220. (org-flag-drawer
  5221. (not (get-char-property (match-end 0) 'invisible))))
  5222. ((integerp arg)
  5223. ;; Show-subtree, ARG levels up from here.
  5224. (save-excursion
  5225. (org-back-to-heading)
  5226. (outline-up-heading (if (< arg 0) (- arg)
  5227. (- (funcall outline-level) arg)))
  5228. (org-show-subtree)))
  5229. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5230. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5231. ;; At a heading: rotate between three different views
  5232. (org-back-to-heading)
  5233. (let ((goal-column 0) eoh eol eos)
  5234. ;; First, some boundaries
  5235. (save-excursion
  5236. (org-back-to-heading)
  5237. (save-excursion
  5238. (beginning-of-line 2)
  5239. (while (and (not (eobp)) ;; this is like `next-line'
  5240. (get-char-property (1- (point)) 'invisible))
  5241. (beginning-of-line 2)) (setq eol (point)))
  5242. (outline-end-of-heading) (setq eoh (point))
  5243. (org-end-of-subtree t)
  5244. (unless (eobp)
  5245. (skip-chars-forward " \t\n")
  5246. (beginning-of-line 1) ; in case this is an item
  5247. )
  5248. (setq eos (1- (point))))
  5249. ;; Find out what to do next and set `this-command'
  5250. (cond
  5251. ((= eos eoh)
  5252. ;; Nothing is hidden behind this heading
  5253. (message "EMPTY ENTRY")
  5254. (setq org-cycle-subtree-status nil)
  5255. (save-excursion
  5256. (goto-char eos)
  5257. (outline-next-heading)
  5258. (if (org-invisible-p) (org-flag-heading nil))))
  5259. ((or (>= eol eos)
  5260. (not (string-match "\\S-" (buffer-substring eol eos))))
  5261. ;; Entire subtree is hidden in one line: open it
  5262. (org-show-entry)
  5263. (show-children)
  5264. (message "CHILDREN")
  5265. (save-excursion
  5266. (goto-char eos)
  5267. (outline-next-heading)
  5268. (if (org-invisible-p) (org-flag-heading nil)))
  5269. (setq org-cycle-subtree-status 'children)
  5270. (run-hook-with-args 'org-cycle-hook 'children))
  5271. ((and (eq last-command this-command)
  5272. (eq org-cycle-subtree-status 'children))
  5273. ;; We just showed the children, now show everything.
  5274. (org-show-subtree)
  5275. (message "SUBTREE")
  5276. (setq org-cycle-subtree-status 'subtree)
  5277. (run-hook-with-args 'org-cycle-hook 'subtree))
  5278. (t
  5279. ;; Default action: hide the subtree.
  5280. (hide-subtree)
  5281. (message "FOLDED")
  5282. (setq org-cycle-subtree-status 'folded)
  5283. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5284. ;; TAB emulation
  5285. (buffer-read-only (org-back-to-heading))
  5286. ((org-try-cdlatex-tab))
  5287. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5288. (or (not (bolp))
  5289. (not (looking-at outline-regexp))))
  5290. (call-interactively (global-key-binding "\t")))
  5291. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5292. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5293. (or (and (eq org-cycle-emulate-tab 'white)
  5294. (= (match-end 0) (point-at-eol)))
  5295. (and (eq org-cycle-emulate-tab 'whitestart)
  5296. (>= (match-end 0) pos))))
  5297. t
  5298. (eq org-cycle-emulate-tab t))
  5299. ; (if (and (looking-at "[ \n\r\t]")
  5300. ; (string-match "^[ \t]*$" (buffer-substring
  5301. ; (point-at-bol) (point))))
  5302. ; (progn
  5303. ; (beginning-of-line 1)
  5304. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5305. (call-interactively (global-key-binding "\t")))
  5306. (t (save-excursion
  5307. (org-back-to-heading)
  5308. (org-cycle))))))
  5309. ;;;###autoload
  5310. (defun org-global-cycle (&optional arg)
  5311. "Cycle the global visibility. For details see `org-cycle'."
  5312. (interactive "P")
  5313. (let ((org-cycle-include-plain-lists
  5314. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5315. (if (integerp arg)
  5316. (progn
  5317. (show-all)
  5318. (hide-sublevels arg)
  5319. (setq org-cycle-global-status 'contents))
  5320. (org-cycle '(4)))))
  5321. (defun org-overview ()
  5322. "Switch to overview mode, shoing only top-level headlines.
  5323. Really, this shows all headlines with level equal or greater than the level
  5324. of the first headline in the buffer. This is important, because if the
  5325. first headline is not level one, then (hide-sublevels 1) gives confusing
  5326. results."
  5327. (interactive)
  5328. (let ((level (save-excursion
  5329. (goto-char (point-min))
  5330. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5331. (progn
  5332. (goto-char (match-beginning 0))
  5333. (funcall outline-level))))))
  5334. (and level (hide-sublevels level))))
  5335. (defun org-content (&optional arg)
  5336. "Show all headlines in the buffer, like a table of contents.
  5337. With numerical argument N, show content up to level N."
  5338. (interactive "P")
  5339. (save-excursion
  5340. ;; Visit all headings and show their offspring
  5341. (and (integerp arg) (org-overview))
  5342. (goto-char (point-max))
  5343. (catch 'exit
  5344. (while (and (progn (condition-case nil
  5345. (outline-previous-visible-heading 1)
  5346. (error (goto-char (point-min))))
  5347. t)
  5348. (looking-at outline-regexp))
  5349. (if (integerp arg)
  5350. (show-children (1- arg))
  5351. (show-branches))
  5352. (if (bobp) (throw 'exit nil))))))
  5353. (defun org-optimize-window-after-visibility-change (state)
  5354. "Adjust the window after a change in outline visibility.
  5355. This function is the default value of the hook `org-cycle-hook'."
  5356. (when (get-buffer-window (current-buffer))
  5357. (cond
  5358. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5359. ; ((eq state 'overview) (org-beginning-of-line))
  5360. ((eq state 'content) nil)
  5361. ((eq state 'all) nil)
  5362. ((eq state 'folded) nil)
  5363. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5364. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5365. (defun org-compact-display-after-subtree-move ()
  5366. (let (beg end)
  5367. (save-excursion
  5368. (if (org-up-heading-safe)
  5369. (progn
  5370. (hide-subtree)
  5371. (show-entry)
  5372. (show-children)
  5373. (org-cycle-show-empty-lines 'children)
  5374. (org-cycle-hide-drawers 'children))
  5375. (org-overview)))))
  5376. (defun org-cycle-show-empty-lines (state)
  5377. "Show empty lines above all visible headlines.
  5378. The region to be covered depends on STATE when called through
  5379. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5380. entire buffer covered. Note that an empty line is only shown if there
  5381. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5382. (when (> org-cycle-separator-lines 0)
  5383. (save-excursion
  5384. (let* ((n org-cycle-separator-lines)
  5385. (re (cond
  5386. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5387. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5388. (t (let ((ns (number-to-string (- n 2))))
  5389. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5390. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5391. beg end)
  5392. (cond
  5393. ((memq state '(overview contents t))
  5394. (setq beg (point-min) end (point-max)))
  5395. ((memq state '(children folded))
  5396. (setq beg (point) end (progn (org-end-of-subtree t t)
  5397. (beginning-of-line 2)
  5398. (point)))))
  5399. (when beg
  5400. (goto-char beg)
  5401. (while (re-search-forward re end t)
  5402. (if (not (get-char-property (match-end 1) 'invisible))
  5403. (outline-flag-region
  5404. (match-beginning 1) (match-end 1) nil)))))))
  5405. ;; Never hide empty lines at the end of the file.
  5406. (save-excursion
  5407. (goto-char (point-max))
  5408. (outline-previous-heading)
  5409. (outline-end-of-heading)
  5410. (if (and (looking-at "[ \t\n]+")
  5411. (= (match-end 0) (point-max)))
  5412. (outline-flag-region (point) (match-end 0) nil))))
  5413. (defun org-subtree-end-visible-p ()
  5414. "Is the end of the current subtree visible?"
  5415. (pos-visible-in-window-p
  5416. (save-excursion (org-end-of-subtree t) (point))))
  5417. (defun org-first-headline-recenter (&optional N)
  5418. "Move cursor to the first headline and recenter the headline.
  5419. Optional argument N means, put the headline into the Nth line of the window."
  5420. (goto-char (point-min))
  5421. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5422. (beginning-of-line)
  5423. (recenter (prefix-numeric-value N))))
  5424. ;;; Org-goto
  5425. (defvar org-goto-window-configuration nil)
  5426. (defvar org-goto-marker nil)
  5427. (defvar org-goto-map
  5428. (let ((map (make-sparse-keymap)))
  5429. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5430. (while (setq cmd (pop cmds))
  5431. (substitute-key-definition cmd cmd map global-map)))
  5432. (suppress-keymap map)
  5433. (org-defkey map "\C-m" 'org-goto-ret)
  5434. (org-defkey map [(return)] 'org-goto-ret)
  5435. (org-defkey map [(left)] 'org-goto-left)
  5436. (org-defkey map [(right)] 'org-goto-right)
  5437. (org-defkey map [(control ?g)] 'org-goto-quit)
  5438. (org-defkey map "\C-i" 'org-cycle)
  5439. (org-defkey map [(tab)] 'org-cycle)
  5440. (org-defkey map [(down)] 'outline-next-visible-heading)
  5441. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5442. (if org-goto-auto-isearch
  5443. (if (fboundp 'define-key-after)
  5444. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5445. nil)
  5446. (org-defkey map "q" 'org-goto-quit)
  5447. (org-defkey map "n" 'outline-next-visible-heading)
  5448. (org-defkey map "p" 'outline-previous-visible-heading)
  5449. (org-defkey map "f" 'outline-forward-same-level)
  5450. (org-defkey map "b" 'outline-backward-same-level)
  5451. (org-defkey map "u" 'outline-up-heading))
  5452. (org-defkey map "/" 'org-occur)
  5453. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5454. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5455. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5456. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5457. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5458. map))
  5459. (defconst org-goto-help
  5460. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5461. RET=jump to location [Q]uit and return to previous location
  5462. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5463. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5464. (defun org-goto (&optional alternative-interface)
  5465. "Look up a different location in the current file, keeping current visibility.
  5466. When you want look-up or go to a different location in a document, the
  5467. fastest way is often to fold the entire buffer and then dive into the tree.
  5468. This method has the disadvantage, that the previous location will be folded,
  5469. which may not be what you want.
  5470. This command works around this by showing a copy of the current buffer
  5471. in an indirect buffer, in overview mode. You can dive into the tree in
  5472. that copy, use org-occur and incremental search to find a location.
  5473. When pressing RET or `Q', the command returns to the original buffer in
  5474. which the visibility is still unchanged. After RET is will also jump to
  5475. the location selected in the indirect buffer and expose the
  5476. the headline hierarchy above."
  5477. (interactive "P")
  5478. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5479. (org-refile-use-outline-path t)
  5480. (interface
  5481. (if (not alternative-interface)
  5482. org-goto-interface
  5483. (if (eq org-goto-interface 'outline)
  5484. 'outline-path-completion
  5485. 'outline)))
  5486. (org-goto-start-pos (point))
  5487. (selected-point
  5488. (if (eq interface 'outline)
  5489. (car (org-get-location (current-buffer) org-goto-help))
  5490. (nth 3 (org-refile-get-location "Goto: ")))))
  5491. (if selected-point
  5492. (progn
  5493. (org-mark-ring-push org-goto-start-pos)
  5494. (goto-char selected-point)
  5495. (if (or (org-invisible-p) (org-invisible-p2))
  5496. (org-show-context 'org-goto)))
  5497. (message "Quit"))))
  5498. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5499. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5500. (defvar org-goto-local-auto-isearch-map) ; defined below
  5501. (defun org-get-location (buf help)
  5502. "Let the user select a location in the Org-mode buffer BUF.
  5503. This function uses a recursive edit. It returns the selected position
  5504. or nil."
  5505. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5506. (isearch-hide-immediately nil)
  5507. (isearch-search-fun-function
  5508. (lambda () 'org-goto-local-search-forward-headings))
  5509. (org-goto-selected-point org-goto-exit-command))
  5510. (save-excursion
  5511. (save-window-excursion
  5512. (delete-other-windows)
  5513. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5514. (switch-to-buffer
  5515. (condition-case nil
  5516. (make-indirect-buffer (current-buffer) "*org-goto*")
  5517. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5518. (with-output-to-temp-buffer "*Help*"
  5519. (princ help))
  5520. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5521. (setq buffer-read-only nil)
  5522. (let ((org-startup-truncated t)
  5523. (org-startup-folded nil)
  5524. (org-startup-align-all-tables nil))
  5525. (org-mode)
  5526. (org-overview))
  5527. (setq buffer-read-only t)
  5528. (if (and (boundp 'org-goto-start-pos)
  5529. (integer-or-marker-p org-goto-start-pos))
  5530. (let ((org-show-hierarchy-above t)
  5531. (org-show-siblings t)
  5532. (org-show-following-heading t))
  5533. (goto-char org-goto-start-pos)
  5534. (and (org-invisible-p) (org-show-context)))
  5535. (goto-char (point-min)))
  5536. (org-beginning-of-line)
  5537. (message "Select location and press RET")
  5538. (use-local-map org-goto-map)
  5539. (recursive-edit)
  5540. ))
  5541. (kill-buffer "*org-goto*")
  5542. (cons org-goto-selected-point org-goto-exit-command)))
  5543. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5544. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5545. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5546. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5547. (defun org-goto-local-search-forward-headings (string bound noerror)
  5548. "Search and make sure that anu matches are in headlines."
  5549. (catch 'return
  5550. (while (search-forward string bound noerror)
  5551. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5552. (and (member :headline context)
  5553. (not (member :tags context))))
  5554. (throw 'return (point))))))
  5555. (defun org-goto-local-auto-isearch ()
  5556. "Start isearch."
  5557. (interactive)
  5558. (goto-char (point-min))
  5559. (let ((keys (this-command-keys)))
  5560. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5561. (isearch-mode t)
  5562. (isearch-process-search-char (string-to-char keys)))))
  5563. (defun org-goto-ret (&optional arg)
  5564. "Finish `org-goto' by going to the new location."
  5565. (interactive "P")
  5566. (setq org-goto-selected-point (point)
  5567. org-goto-exit-command 'return)
  5568. (throw 'exit nil))
  5569. (defun org-goto-left ()
  5570. "Finish `org-goto' by going to the new location."
  5571. (interactive)
  5572. (if (org-on-heading-p)
  5573. (progn
  5574. (beginning-of-line 1)
  5575. (setq org-goto-selected-point (point)
  5576. org-goto-exit-command 'left)
  5577. (throw 'exit nil))
  5578. (error "Not on a heading")))
  5579. (defun org-goto-right ()
  5580. "Finish `org-goto' by going to the new location."
  5581. (interactive)
  5582. (if (org-on-heading-p)
  5583. (progn
  5584. (setq org-goto-selected-point (point)
  5585. org-goto-exit-command 'right)
  5586. (throw 'exit nil))
  5587. (error "Not on a heading")))
  5588. (defun org-goto-quit ()
  5589. "Finish `org-goto' without cursor motion."
  5590. (interactive)
  5591. (setq org-goto-selected-point nil)
  5592. (setq org-goto-exit-command 'quit)
  5593. (throw 'exit nil))
  5594. ;;; Indirect buffer display of subtrees
  5595. (defvar org-indirect-dedicated-frame nil
  5596. "This is the frame being used for indirect tree display.")
  5597. (defvar org-last-indirect-buffer nil)
  5598. (defun org-tree-to-indirect-buffer (&optional arg)
  5599. "Create indirect buffer and narrow it to current subtree.
  5600. With numerical prefix ARG, go up to this level and then take that tree.
  5601. If ARG is negative, go up that many levels.
  5602. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5603. indirect buffer previously made with this command, to avoid proliferation of
  5604. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5605. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5606. is kept so that you can work with several indirect buffers at the same time.
  5607. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5608. requests that a new frame be made for the new buffer, so that the dedicated
  5609. frame is not changed."
  5610. (interactive "P")
  5611. (let ((cbuf (current-buffer))
  5612. (cwin (selected-window))
  5613. (pos (point))
  5614. beg end level heading ibuf)
  5615. (save-excursion
  5616. (org-back-to-heading t)
  5617. (when (numberp arg)
  5618. (setq level (org-outline-level))
  5619. (if (< arg 0) (setq arg (+ level arg)))
  5620. (while (> (setq level (org-outline-level)) arg)
  5621. (outline-up-heading 1 t)))
  5622. (setq beg (point)
  5623. heading (org-get-heading))
  5624. (org-end-of-subtree t) (setq end (point)))
  5625. (if (and (buffer-live-p org-last-indirect-buffer)
  5626. (not (eq org-indirect-buffer-display 'new-frame))
  5627. (not arg))
  5628. (kill-buffer org-last-indirect-buffer))
  5629. (setq ibuf (org-get-indirect-buffer cbuf)
  5630. org-last-indirect-buffer ibuf)
  5631. (cond
  5632. ((or (eq org-indirect-buffer-display 'new-frame)
  5633. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5634. (select-frame (make-frame))
  5635. (delete-other-windows)
  5636. (switch-to-buffer ibuf)
  5637. (org-set-frame-title heading))
  5638. ((eq org-indirect-buffer-display 'dedicated-frame)
  5639. (raise-frame
  5640. (select-frame (or (and org-indirect-dedicated-frame
  5641. (frame-live-p org-indirect-dedicated-frame)
  5642. org-indirect-dedicated-frame)
  5643. (setq org-indirect-dedicated-frame (make-frame)))))
  5644. (delete-other-windows)
  5645. (switch-to-buffer ibuf)
  5646. (org-set-frame-title (concat "Indirect: " heading)))
  5647. ((eq org-indirect-buffer-display 'current-window)
  5648. (switch-to-buffer ibuf))
  5649. ((eq org-indirect-buffer-display 'other-window)
  5650. (pop-to-buffer ibuf))
  5651. (t (error "Invalid value.")))
  5652. (if (featurep 'xemacs)
  5653. (save-excursion (org-mode) (turn-on-font-lock)))
  5654. (narrow-to-region beg end)
  5655. (show-all)
  5656. (goto-char pos)
  5657. (and (window-live-p cwin) (select-window cwin))))
  5658. (defun org-get-indirect-buffer (&optional buffer)
  5659. (setq buffer (or buffer (current-buffer)))
  5660. (let ((n 1) (base (buffer-name buffer)) bname)
  5661. (while (buffer-live-p
  5662. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5663. (setq n (1+ n)))
  5664. (condition-case nil
  5665. (make-indirect-buffer buffer bname 'clone)
  5666. (error (make-indirect-buffer buffer bname)))))
  5667. (defun org-set-frame-title (title)
  5668. "Set the title of the current frame to the string TITLE."
  5669. ;; FIXME: how to name a single frame in XEmacs???
  5670. (unless (featurep 'xemacs)
  5671. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5672. ;;;; Structure editing
  5673. ;;; Inserting headlines
  5674. (defun org-insert-heading (&optional force-heading)
  5675. "Insert a new heading or item with same depth at point.
  5676. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5677. If point is at the beginning of a headline, insert a sibling before the
  5678. current headline. If point is not at the beginning, do not split the line,
  5679. but create the new hedline after the current line."
  5680. (interactive "P")
  5681. (if (= (buffer-size) 0)
  5682. (insert "\n* ")
  5683. (when (or force-heading (not (org-insert-item)))
  5684. (let* ((head (save-excursion
  5685. (condition-case nil
  5686. (progn
  5687. (org-back-to-heading)
  5688. (match-string 0))
  5689. (error "*"))))
  5690. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5691. pos)
  5692. (cond
  5693. ((and (org-on-heading-p) (bolp)
  5694. (or (bobp)
  5695. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5696. ;; insert before the current line
  5697. (open-line (if blank 2 1)))
  5698. ((and (bolp)
  5699. (or (bobp)
  5700. (save-excursion
  5701. (backward-char 1) (not (org-invisible-p)))))
  5702. ;; insert right here
  5703. nil)
  5704. (t
  5705. ; ;; in the middle of the line
  5706. ; (org-show-entry)
  5707. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5708. ; (if (and
  5709. ; (org-on-heading-p)
  5710. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5711. ; ;; protect the tags
  5712. ;; (let ((tags (match-string 2)) pos)
  5713. ; (delete-region (match-beginning 1) (match-end 1))
  5714. ; (setq pos (point-at-bol))
  5715. ; (newline (if blank 2 1))
  5716. ; (save-excursion
  5717. ; (goto-char pos)
  5718. ; (end-of-line 1)
  5719. ; (insert " " tags)
  5720. ; (org-set-tags nil 'align)))
  5721. ; (newline (if blank 2 1)))
  5722. ; (newline (if blank 2 1))))
  5723. ;; in the middle of the line
  5724. (org-show-entry)
  5725. (let ((split
  5726. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5727. tags pos)
  5728. (if (org-on-heading-p)
  5729. (progn
  5730. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5731. (setq tags (and (match-end 2) (match-string 2)))
  5732. (and (match-end 1)
  5733. (delete-region (match-beginning 1) (match-end 1)))
  5734. (setq pos (point-at-bol))
  5735. (or split (end-of-line 1))
  5736. (delete-horizontal-space)
  5737. (newline (if blank 2 1))
  5738. (when tags
  5739. (save-excursion
  5740. (goto-char pos)
  5741. (end-of-line 1)
  5742. (insert " " tags)
  5743. (org-set-tags nil 'align))))
  5744. (or split (end-of-line 1))
  5745. (newline (if blank 2 1))))))
  5746. (insert head) (just-one-space)
  5747. (setq pos (point))
  5748. (end-of-line 1)
  5749. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5750. (run-hooks 'org-insert-heading-hook)))))
  5751. (defun org-insert-heading-after-current ()
  5752. "Insert a new heading with same level as current, after current subtree."
  5753. (interactive)
  5754. (org-back-to-heading)
  5755. (org-insert-heading)
  5756. (org-move-subtree-down)
  5757. (end-of-line 1))
  5758. (defun org-insert-todo-heading (arg)
  5759. "Insert a new heading with the same level and TODO state as current heading.
  5760. If the heading has no TODO state, or if the state is DONE, use the first
  5761. state (TODO by default). Also with prefix arg, force first state."
  5762. (interactive "P")
  5763. (when (not (org-insert-item 'checkbox))
  5764. (org-insert-heading)
  5765. (save-excursion
  5766. (org-back-to-heading)
  5767. (outline-previous-heading)
  5768. (looking-at org-todo-line-regexp))
  5769. (if (or arg
  5770. (not (match-beginning 2))
  5771. (member (match-string 2) org-done-keywords))
  5772. (insert (car org-todo-keywords-1) " ")
  5773. (insert (match-string 2) " "))))
  5774. (defun org-insert-subheading (arg)
  5775. "Insert a new subheading and demote it.
  5776. Works for outline headings and for plain lists alike."
  5777. (interactive "P")
  5778. (org-insert-heading arg)
  5779. (cond
  5780. ((org-on-heading-p) (org-do-demote))
  5781. ((org-at-item-p) (org-indent-item 1))))
  5782. (defun org-insert-todo-subheading (arg)
  5783. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5784. Works for outline headings and for plain lists alike."
  5785. (interactive "P")
  5786. (org-insert-todo-heading arg)
  5787. (cond
  5788. ((org-on-heading-p) (org-do-demote))
  5789. ((org-at-item-p) (org-indent-item 1))))
  5790. ;;; Promotion and Demotion
  5791. (defun org-promote-subtree ()
  5792. "Promote the entire subtree.
  5793. See also `org-promote'."
  5794. (interactive)
  5795. (save-excursion
  5796. (org-map-tree 'org-promote))
  5797. (org-fix-position-after-promote))
  5798. (defun org-demote-subtree ()
  5799. "Demote the entire subtree. See `org-demote'.
  5800. See also `org-promote'."
  5801. (interactive)
  5802. (save-excursion
  5803. (org-map-tree 'org-demote))
  5804. (org-fix-position-after-promote))
  5805. (defun org-do-promote ()
  5806. "Promote the current heading higher up the tree.
  5807. If the region is active in `transient-mark-mode', promote all headings
  5808. in the region."
  5809. (interactive)
  5810. (save-excursion
  5811. (if (org-region-active-p)
  5812. (org-map-region 'org-promote (region-beginning) (region-end))
  5813. (org-promote)))
  5814. (org-fix-position-after-promote))
  5815. (defun org-do-demote ()
  5816. "Demote the current heading lower down the tree.
  5817. If the region is active in `transient-mark-mode', demote all headings
  5818. in the region."
  5819. (interactive)
  5820. (save-excursion
  5821. (if (org-region-active-p)
  5822. (org-map-region 'org-demote (region-beginning) (region-end))
  5823. (org-demote)))
  5824. (org-fix-position-after-promote))
  5825. (defun org-fix-position-after-promote ()
  5826. "Make sure that after pro/demotion cursor position is right."
  5827. (let ((pos (point)))
  5828. (when (save-excursion
  5829. (beginning-of-line 1)
  5830. (looking-at org-todo-line-regexp)
  5831. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5832. (cond ((eobp) (insert " "))
  5833. ((eolp) (insert " "))
  5834. ((equal (char-after) ?\ ) (forward-char 1))))))
  5835. (defun org-reduced-level (l)
  5836. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5837. (defun org-get-valid-level (level &optional change)
  5838. "Rectify a level change under the influence of `org-odd-levels-only'
  5839. LEVEL is a current level, CHANGE is by how much the level should be
  5840. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5841. even level numbers will become the next higher odd number."
  5842. (if org-odd-levels-only
  5843. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5844. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5845. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5846. (max 1 (+ level change))))
  5847. (define-obsolete-function-alias 'org-get-legal-level
  5848. 'org-get-valid-level "23.1")
  5849. (defun org-promote ()
  5850. "Promote the current heading higher up the tree.
  5851. If the region is active in `transient-mark-mode', promote all headings
  5852. in the region."
  5853. (org-back-to-heading t)
  5854. (let* ((level (save-match-data (funcall outline-level)))
  5855. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5856. (diff (abs (- level (length up-head) -1))))
  5857. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5858. (replace-match up-head nil t)
  5859. ;; Fixup tag positioning
  5860. (and org-auto-align-tags (org-set-tags nil t))
  5861. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5862. (defun org-demote ()
  5863. "Demote the current heading lower down the tree.
  5864. If the region is active in `transient-mark-mode', demote all headings
  5865. in the region."
  5866. (org-back-to-heading t)
  5867. (let* ((level (save-match-data (funcall outline-level)))
  5868. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5869. (diff (abs (- level (length down-head) -1))))
  5870. (replace-match down-head nil t)
  5871. ;; Fixup tag positioning
  5872. (and org-auto-align-tags (org-set-tags nil t))
  5873. (if org-adapt-indentation (org-fixup-indentation diff))))
  5874. (defun org-map-tree (fun)
  5875. "Call FUN for every heading underneath the current one."
  5876. (org-back-to-heading)
  5877. (let ((level (funcall outline-level)))
  5878. (save-excursion
  5879. (funcall fun)
  5880. (while (and (progn
  5881. (outline-next-heading)
  5882. (> (funcall outline-level) level))
  5883. (not (eobp)))
  5884. (funcall fun)))))
  5885. (defun org-map-region (fun beg end)
  5886. "Call FUN for every heading between BEG and END."
  5887. (let ((org-ignore-region t))
  5888. (save-excursion
  5889. (setq end (copy-marker end))
  5890. (goto-char beg)
  5891. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5892. (< (point) end))
  5893. (funcall fun))
  5894. (while (and (progn
  5895. (outline-next-heading)
  5896. (< (point) end))
  5897. (not (eobp)))
  5898. (funcall fun)))))
  5899. (defun org-fixup-indentation (diff)
  5900. "Change the indentation in the current entry by DIFF
  5901. However, if any line in the current entry has no indentation, or if it
  5902. would end up with no indentation after the change, nothing at all is done."
  5903. (save-excursion
  5904. (let ((end (save-excursion (outline-next-heading)
  5905. (point-marker)))
  5906. (prohibit (if (> diff 0)
  5907. "^\\S-"
  5908. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5909. col)
  5910. (unless (save-excursion (end-of-line 1)
  5911. (re-search-forward prohibit end t))
  5912. (while (and (< (point) end)
  5913. (re-search-forward "^[ \t]+" end t))
  5914. (goto-char (match-end 0))
  5915. (setq col (current-column))
  5916. (if (< diff 0) (replace-match ""))
  5917. (indent-to (+ diff col))))
  5918. (move-marker end nil))))
  5919. (defun org-convert-to-odd-levels ()
  5920. "Convert an org-mode file with all levels allowed to one with odd levels.
  5921. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5922. level 5 etc."
  5923. (interactive)
  5924. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5925. (let ((org-odd-levels-only nil) n)
  5926. (save-excursion
  5927. (goto-char (point-min))
  5928. (while (re-search-forward "^\\*\\*+ " nil t)
  5929. (setq n (- (length (match-string 0)) 2))
  5930. (while (>= (setq n (1- n)) 0)
  5931. (org-demote))
  5932. (end-of-line 1))))))
  5933. (defun org-convert-to-oddeven-levels ()
  5934. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5935. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5936. section with an even level, conversion would destroy the structure of the file. An error
  5937. is signaled in this case."
  5938. (interactive)
  5939. (goto-char (point-min))
  5940. ;; First check if there are no even levels
  5941. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5942. (org-show-context t)
  5943. (error "Not all levels are odd in this file. Conversion not possible."))
  5944. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5945. (let ((org-odd-levels-only nil) n)
  5946. (save-excursion
  5947. (goto-char (point-min))
  5948. (while (re-search-forward "^\\*\\*+ " nil t)
  5949. (setq n (/ (1- (length (match-string 0))) 2))
  5950. (while (>= (setq n (1- n)) 0)
  5951. (org-promote))
  5952. (end-of-line 1))))))
  5953. (defun org-tr-level (n)
  5954. "Make N odd if required."
  5955. (if org-odd-levels-only (1+ (/ n 2)) n))
  5956. ;;; Vertical tree motion, cutting and pasting of subtrees
  5957. (defun org-move-subtree-up (&optional arg)
  5958. "Move the current subtree up past ARG headlines of the same level."
  5959. (interactive "p")
  5960. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5961. (defun org-move-subtree-down (&optional arg)
  5962. "Move the current subtree down past ARG headlines of the same level."
  5963. (interactive "p")
  5964. (setq arg (prefix-numeric-value arg))
  5965. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5966. 'outline-get-last-sibling))
  5967. (ins-point (make-marker))
  5968. (cnt (abs arg))
  5969. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5970. ;; Select the tree
  5971. (org-back-to-heading)
  5972. (setq beg0 (point))
  5973. (save-excursion
  5974. (setq ne-beg (org-back-over-empty-lines))
  5975. (setq beg (point)))
  5976. (save-match-data
  5977. (save-excursion (outline-end-of-heading)
  5978. (setq folded (org-invisible-p)))
  5979. (outline-end-of-subtree))
  5980. (outline-next-heading)
  5981. (setq ne-end (org-back-over-empty-lines))
  5982. (setq end (point))
  5983. (goto-char beg0)
  5984. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5985. ;; include less whitespace
  5986. (save-excursion
  5987. (goto-char beg)
  5988. (forward-line (- ne-beg ne-end))
  5989. (setq beg (point))))
  5990. ;; Find insertion point, with error handling
  5991. (while (> cnt 0)
  5992. (or (and (funcall movfunc) (looking-at outline-regexp))
  5993. (progn (goto-char beg0)
  5994. (error "Cannot move past superior level or buffer limit")))
  5995. (setq cnt (1- cnt)))
  5996. (if (> arg 0)
  5997. ;; Moving forward - still need to move over subtree
  5998. (progn (org-end-of-subtree t t)
  5999. (save-excursion
  6000. (org-back-over-empty-lines)
  6001. (or (bolp) (newline)))))
  6002. (setq ne-ins (org-back-over-empty-lines))
  6003. (move-marker ins-point (point))
  6004. (setq txt (buffer-substring beg end))
  6005. (delete-region beg end)
  6006. (outline-flag-region (1- beg) beg nil)
  6007. (outline-flag-region (1- (point)) (point) nil)
  6008. (insert txt)
  6009. (or (bolp) (insert "\n"))
  6010. (setq ins-end (point))
  6011. (goto-char ins-point)
  6012. (org-skip-whitespace)
  6013. (when (and (< arg 0)
  6014. (org-first-sibling-p)
  6015. (> ne-ins ne-beg))
  6016. ;; Move whitespace back to beginning
  6017. (save-excursion
  6018. (goto-char ins-end)
  6019. (let ((kill-whole-line t))
  6020. (kill-line (- ne-ins ne-beg)) (point)))
  6021. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6022. (move-marker ins-point nil)
  6023. (org-compact-display-after-subtree-move)
  6024. (unless folded
  6025. (org-show-entry)
  6026. (show-children)
  6027. (org-cycle-hide-drawers 'children))))
  6028. (defvar org-subtree-clip ""
  6029. "Clipboard for cut and paste of subtrees.
  6030. This is actually only a copy of the kill, because we use the normal kill
  6031. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6032. (defvar org-subtree-clip-folded nil
  6033. "Was the last copied subtree folded?
  6034. This is used to fold the tree back after pasting.")
  6035. (defun org-cut-subtree (&optional n)
  6036. "Cut the current subtree into the clipboard.
  6037. With prefix arg N, cut this many sequential subtrees.
  6038. This is a short-hand for marking the subtree and then cutting it."
  6039. (interactive "p")
  6040. (org-copy-subtree n 'cut))
  6041. (defun org-copy-subtree (&optional n cut)
  6042. "Cut the current subtree into the clipboard.
  6043. With prefix arg N, cut this many sequential subtrees.
  6044. This is a short-hand for marking the subtree and then copying it.
  6045. If CUT is non-nil, actually cut the subtree."
  6046. (interactive "p")
  6047. (let (beg end folded (beg0 (point)))
  6048. (if (interactive-p)
  6049. (org-back-to-heading nil) ; take what looks like a subtree
  6050. (org-back-to-heading t)) ; take what is really there
  6051. (org-back-over-empty-lines)
  6052. (setq beg (point))
  6053. (skip-chars-forward " \t\r\n")
  6054. (save-match-data
  6055. (save-excursion (outline-end-of-heading)
  6056. (setq folded (org-invisible-p)))
  6057. (condition-case nil
  6058. (outline-forward-same-level (1- n))
  6059. (error nil))
  6060. (org-end-of-subtree t t))
  6061. (org-back-over-empty-lines)
  6062. (setq end (point))
  6063. (goto-char beg0)
  6064. (when (> end beg)
  6065. (setq org-subtree-clip-folded folded)
  6066. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6067. (setq org-subtree-clip (current-kill 0))
  6068. (message "%s: Subtree(s) with %d characters"
  6069. (if cut "Cut" "Copied")
  6070. (length org-subtree-clip)))))
  6071. (defun org-paste-subtree (&optional level tree)
  6072. "Paste the clipboard as a subtree, with modification of headline level.
  6073. The entire subtree is promoted or demoted in order to match a new headline
  6074. level. By default, the new level is derived from the visible headings
  6075. before and after the insertion point, and taken to be the inferior headline
  6076. level of the two. So if the previous visible heading is level 3 and the
  6077. next is level 4 (or vice versa), level 4 will be used for insertion.
  6078. This makes sure that the subtree remains an independent subtree and does
  6079. not swallow low level entries.
  6080. You can also force a different level, either by using a numeric prefix
  6081. argument, or by inserting the heading marker by hand. For example, if the
  6082. cursor is after \"*****\", then the tree will be shifted to level 5.
  6083. If you want to insert the tree as is, just use \\[yank].
  6084. If optional TREE is given, use this text instead of the kill ring."
  6085. (interactive "P")
  6086. (unless (org-kill-is-subtree-p tree)
  6087. (error "%s"
  6088. (substitute-command-keys
  6089. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6090. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  6091. (^re (concat "^\\(" outline-regexp "\\)"))
  6092. (re (concat "\\(" outline-regexp "\\)"))
  6093. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6094. (old-level (if (string-match ^re txt)
  6095. (- (match-end 0) (match-beginning 0) 1)
  6096. -1))
  6097. (force-level (cond (level (prefix-numeric-value level))
  6098. ((string-match
  6099. ^re_ (buffer-substring (point-at-bol) (point)))
  6100. (- (match-end 1) (match-beginning 1)))
  6101. (t nil)))
  6102. (previous-level (save-excursion
  6103. (condition-case nil
  6104. (progn
  6105. (outline-previous-visible-heading 1)
  6106. (if (looking-at re)
  6107. (- (match-end 0) (match-beginning 0) 1)
  6108. 1))
  6109. (error 1))))
  6110. (next-level (save-excursion
  6111. (condition-case nil
  6112. (progn
  6113. (or (looking-at outline-regexp)
  6114. (outline-next-visible-heading 1))
  6115. (if (looking-at re)
  6116. (- (match-end 0) (match-beginning 0) 1)
  6117. 1))
  6118. (error 1))))
  6119. (new-level (or force-level (max previous-level next-level)))
  6120. (shift (if (or (= old-level -1)
  6121. (= new-level -1)
  6122. (= old-level new-level))
  6123. 0
  6124. (- new-level old-level)))
  6125. (delta (if (> shift 0) -1 1))
  6126. (func (if (> shift 0) 'org-demote 'org-promote))
  6127. (org-odd-levels-only nil)
  6128. beg end)
  6129. ;; Remove the forced level indicator
  6130. (if force-level
  6131. (delete-region (point-at-bol) (point)))
  6132. ;; Paste
  6133. (beginning-of-line 1)
  6134. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6135. (setq beg (point))
  6136. (insert-before-markers txt) ;; FIXME: correct fix????
  6137. (unless (string-match "\n\\'" txt) (insert "\n"))
  6138. (setq end (point))
  6139. (goto-char beg)
  6140. (skip-chars-forward " \t\n\r")
  6141. (setq beg (point))
  6142. ;; Shift if necessary
  6143. (unless (= shift 0)
  6144. (save-restriction
  6145. (narrow-to-region beg end)
  6146. (while (not (= shift 0))
  6147. (org-map-region func (point-min) (point-max))
  6148. (setq shift (+ delta shift)))
  6149. (goto-char (point-min))))
  6150. (when (interactive-p)
  6151. (message "Clipboard pasted as level %d subtree" new-level))
  6152. (if (and kill-ring
  6153. (eq org-subtree-clip (current-kill 0))
  6154. org-subtree-clip-folded)
  6155. ;; The tree was folded before it was killed/copied
  6156. (hide-subtree))))
  6157. (defun org-kill-is-subtree-p (&optional txt)
  6158. "Check if the current kill is an outline subtree, or a set of trees.
  6159. Returns nil if kill does not start with a headline, or if the first
  6160. headline level is not the largest headline level in the tree.
  6161. So this will actually accept several entries of equal levels as well,
  6162. which is OK for `org-paste-subtree'.
  6163. If optional TXT is given, check this string instead of the current kill."
  6164. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6165. (start-level (and kill
  6166. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6167. org-outline-regexp "\\)")
  6168. kill)
  6169. (- (match-end 2) (match-beginning 2) 1)))
  6170. (re (concat "^" org-outline-regexp))
  6171. (start (1+ (match-beginning 2))))
  6172. (if (not start-level)
  6173. (progn
  6174. nil) ;; does not even start with a heading
  6175. (catch 'exit
  6176. (while (setq start (string-match re kill (1+ start)))
  6177. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6178. (throw 'exit nil)))
  6179. t))))
  6180. (defun org-narrow-to-subtree ()
  6181. "Narrow buffer to the current subtree."
  6182. (interactive)
  6183. (save-excursion
  6184. (save-match-data
  6185. (narrow-to-region
  6186. (progn (org-back-to-heading) (point))
  6187. (progn (org-end-of-subtree t t) (point))))))
  6188. ;;; Outline Sorting
  6189. (defun org-sort (with-case)
  6190. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6191. Optional argument WITH-CASE means sort case-sensitively."
  6192. (interactive "P")
  6193. (if (org-at-table-p)
  6194. (org-call-with-arg 'org-table-sort-lines with-case)
  6195. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6196. (defvar org-priority-regexp) ; defined later in the file
  6197. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6198. "Sort entries on a certain level of an outline tree.
  6199. If there is an active region, the entries in the region are sorted.
  6200. Else, if the cursor is before the first entry, sort the top-level items.
  6201. Else, the children of the entry at point are sorted.
  6202. Sorting can be alphabetically, numerically, and by date/time as given by
  6203. the first time stamp in the entry. The command prompts for the sorting
  6204. type unless it has been given to the function through the SORTING-TYPE
  6205. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6206. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6207. called with point at the beginning of the record. It must return either
  6208. a string or a number that should serve as the sorting key for that record.
  6209. Comparing entries ignores case by default. However, with an optional argument
  6210. WITH-CASE, the sorting considers case as well."
  6211. (interactive "P")
  6212. (let ((case-func (if with-case 'identity 'downcase))
  6213. start beg end stars re re2
  6214. txt what tmp plain-list-p)
  6215. ;; Find beginning and end of region to sort
  6216. (cond
  6217. ((org-region-active-p)
  6218. ;; we will sort the region
  6219. (setq end (region-end)
  6220. what "region")
  6221. (goto-char (region-beginning))
  6222. (if (not (org-on-heading-p)) (outline-next-heading))
  6223. (setq start (point)))
  6224. ((org-at-item-p)
  6225. ;; we will sort this plain list
  6226. (org-beginning-of-item-list) (setq start (point))
  6227. (org-end-of-item-list) (setq end (point))
  6228. (goto-char start)
  6229. (setq plain-list-p t
  6230. what "plain list"))
  6231. ((or (org-on-heading-p)
  6232. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6233. ;; we will sort the children of the current headline
  6234. (org-back-to-heading)
  6235. (setq start (point)
  6236. end (progn (org-end-of-subtree t t)
  6237. (org-back-over-empty-lines)
  6238. (point))
  6239. what "children")
  6240. (goto-char start)
  6241. (show-subtree)
  6242. (outline-next-heading))
  6243. (t
  6244. ;; we will sort the top-level entries in this file
  6245. (goto-char (point-min))
  6246. (or (org-on-heading-p) (outline-next-heading))
  6247. (setq start (point) end (point-max) what "top-level")
  6248. (goto-char start)
  6249. (show-all)))
  6250. (setq beg (point))
  6251. (if (>= beg end) (error "Nothing to sort"))
  6252. (unless plain-list-p
  6253. (looking-at "\\(\\*+\\)")
  6254. (setq stars (match-string 1)
  6255. re (concat "^" (regexp-quote stars) " +")
  6256. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6257. txt (buffer-substring beg end))
  6258. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6259. (if (and (not (equal stars "*")) (string-match re2 txt))
  6260. (error "Region to sort contains a level above the first entry")))
  6261. (unless sorting-type
  6262. (message
  6263. (if plain-list-p
  6264. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6265. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6266. what)
  6267. (setq sorting-type (read-char-exclusive))
  6268. (and (= (downcase sorting-type) ?f)
  6269. (setq getkey-func
  6270. (completing-read "Sort using function: "
  6271. obarray 'fboundp t nil nil))
  6272. (setq getkey-func (intern getkey-func)))
  6273. (and (= (downcase sorting-type) ?r)
  6274. (setq property
  6275. (completing-read "Property: "
  6276. (mapcar 'list (org-buffer-property-keys t))
  6277. nil t))))
  6278. (message "Sorting entries...")
  6279. (save-restriction
  6280. (narrow-to-region start end)
  6281. (let ((dcst (downcase sorting-type))
  6282. (now (current-time)))
  6283. (sort-subr
  6284. (/= dcst sorting-type)
  6285. ;; This function moves to the beginning character of the "record" to
  6286. ;; be sorted.
  6287. (if plain-list-p
  6288. (lambda nil
  6289. (if (org-at-item-p) t (goto-char (point-max))))
  6290. (lambda nil
  6291. (if (re-search-forward re nil t)
  6292. (goto-char (match-beginning 0))
  6293. (goto-char (point-max)))))
  6294. ;; This function moves to the last character of the "record" being
  6295. ;; sorted.
  6296. (if plain-list-p
  6297. 'org-end-of-item
  6298. (lambda nil
  6299. (save-match-data
  6300. (condition-case nil
  6301. (outline-forward-same-level 1)
  6302. (error
  6303. (goto-char (point-max)))))))
  6304. ;; This function returns the value that gets sorted against.
  6305. (if plain-list-p
  6306. (lambda nil
  6307. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6308. (cond
  6309. ((= dcst ?n)
  6310. (string-to-number (buffer-substring (match-end 0)
  6311. (point-at-eol))))
  6312. ((= dcst ?a)
  6313. (buffer-substring (match-end 0) (point-at-eol)))
  6314. ((= dcst ?t)
  6315. (if (re-search-forward org-ts-regexp
  6316. (point-at-eol) t)
  6317. (org-time-string-to-time (match-string 0))
  6318. now))
  6319. ((= dcst ?f)
  6320. (if getkey-func
  6321. (progn
  6322. (setq tmp (funcall getkey-func))
  6323. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6324. tmp)
  6325. (error "Invalid key function `%s'" getkey-func)))
  6326. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6327. (lambda nil
  6328. (cond
  6329. ((= dcst ?n)
  6330. (if (looking-at outline-regexp)
  6331. (string-to-number (buffer-substring (match-end 0)
  6332. (point-at-eol)))
  6333. nil))
  6334. ((= dcst ?a)
  6335. (funcall case-func (buffer-substring (point-at-bol)
  6336. (point-at-eol))))
  6337. ((= dcst ?t)
  6338. (if (re-search-forward org-ts-regexp
  6339. (save-excursion
  6340. (forward-line 2)
  6341. (point)) t)
  6342. (org-time-string-to-time (match-string 0))
  6343. now))
  6344. ((= dcst ?p)
  6345. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6346. (string-to-char (match-string 2))
  6347. org-default-priority))
  6348. ((= dcst ?r)
  6349. (or (org-entry-get nil property) ""))
  6350. ((= dcst ?f)
  6351. (if getkey-func
  6352. (progn
  6353. (setq tmp (funcall getkey-func))
  6354. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6355. tmp)
  6356. (error "Invalid key function `%s'" getkey-func)))
  6357. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6358. nil
  6359. (cond
  6360. ((= dcst ?a) 'string<)
  6361. ((= dcst ?t) 'time-less-p)
  6362. (t nil)))))
  6363. (message "Sorting entries...done")))
  6364. (defun org-do-sort (table what &optional with-case sorting-type)
  6365. "Sort TABLE of WHAT according to SORTING-TYPE.
  6366. The user will be prompted for the SORTING-TYPE if the call to this
  6367. function does not specify it. WHAT is only for the prompt, to indicate
  6368. what is being sorted. The sorting key will be extracted from
  6369. the car of the elements of the table.
  6370. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6371. (unless sorting-type
  6372. (message
  6373. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6374. what)
  6375. (setq sorting-type (read-char-exclusive)))
  6376. (let ((dcst (downcase sorting-type))
  6377. extractfun comparefun)
  6378. ;; Define the appropriate functions
  6379. (cond
  6380. ((= dcst ?n)
  6381. (setq extractfun 'string-to-number
  6382. comparefun (if (= dcst sorting-type) '< '>)))
  6383. ((= dcst ?a)
  6384. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6385. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6386. comparefun (if (= dcst sorting-type)
  6387. 'string<
  6388. (lambda (a b) (and (not (string< a b))
  6389. (not (string= a b)))))))
  6390. ((= dcst ?t)
  6391. (setq extractfun
  6392. (lambda (x)
  6393. (if (string-match org-ts-regexp x)
  6394. (time-to-seconds
  6395. (org-time-string-to-time (match-string 0 x)))
  6396. 0))
  6397. comparefun (if (= dcst sorting-type) '< '>)))
  6398. (t (error "Invalid sorting type `%c'" sorting-type)))
  6399. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6400. table)
  6401. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6402. ;;;; Plain list items, including checkboxes
  6403. ;;; Plain list items
  6404. (defun org-at-item-p ()
  6405. "Is point in a line starting a hand-formatted item?"
  6406. (let ((llt org-plain-list-ordered-item-terminator))
  6407. (save-excursion
  6408. (goto-char (point-at-bol))
  6409. (looking-at
  6410. (cond
  6411. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6412. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6413. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6414. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6415. (defun org-in-item-p ()
  6416. "It the cursor inside a plain list item.
  6417. Does not have to be the first line."
  6418. (save-excursion
  6419. (condition-case nil
  6420. (progn
  6421. (org-beginning-of-item)
  6422. (org-at-item-p)
  6423. t)
  6424. (error nil))))
  6425. (defun org-insert-item (&optional checkbox)
  6426. "Insert a new item at the current level.
  6427. Return t when things worked, nil when we are not in an item."
  6428. (when (save-excursion
  6429. (condition-case nil
  6430. (progn
  6431. (org-beginning-of-item)
  6432. (org-at-item-p)
  6433. (if (org-invisible-p) (error "Invisible item"))
  6434. t)
  6435. (error nil)))
  6436. (let* ((bul (match-string 0))
  6437. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6438. (match-end 0)))
  6439. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6440. pos)
  6441. (cond
  6442. ((and (org-at-item-p) (<= (point) eow))
  6443. ;; before the bullet
  6444. (beginning-of-line 1)
  6445. (open-line (if blank 2 1)))
  6446. ((<= (point) eow)
  6447. (beginning-of-line 1))
  6448. (t
  6449. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6450. (end-of-line 1)
  6451. (delete-horizontal-space))
  6452. (newline (if blank 2 1))))
  6453. (insert bul (if checkbox "[ ]" ""))
  6454. (just-one-space)
  6455. (setq pos (point))
  6456. (end-of-line 1)
  6457. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6458. (org-maybe-renumber-ordered-list)
  6459. (and checkbox (org-update-checkbox-count-maybe))
  6460. t))
  6461. ;;; Checkboxes
  6462. (defun org-at-item-checkbox-p ()
  6463. "Is point at a line starting a plain-list item with a checklet?"
  6464. (and (org-at-item-p)
  6465. (save-excursion
  6466. (goto-char (match-end 0))
  6467. (skip-chars-forward " \t")
  6468. (looking-at "\\[[- X]\\]"))))
  6469. (defun org-toggle-checkbox (&optional arg)
  6470. "Toggle the checkbox in the current line."
  6471. (interactive "P")
  6472. (catch 'exit
  6473. (let (beg end status (firstnew 'unknown))
  6474. (cond
  6475. ((org-region-active-p)
  6476. (setq beg (region-beginning) end (region-end)))
  6477. ((org-on-heading-p)
  6478. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6479. ((org-at-item-checkbox-p)
  6480. (let ((pos (point)))
  6481. (replace-match
  6482. (cond (arg "[-]")
  6483. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6484. (t "[ ]"))
  6485. t t)
  6486. (goto-char pos))
  6487. (throw 'exit t))
  6488. (t (error "Not at a checkbox or heading, and no active region")))
  6489. (save-excursion
  6490. (goto-char beg)
  6491. (while (< (point) end)
  6492. (when (org-at-item-checkbox-p)
  6493. (setq status (equal (match-string 0) "[X]"))
  6494. (when (eq firstnew 'unknown)
  6495. (setq firstnew (not status)))
  6496. (replace-match
  6497. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6498. (beginning-of-line 2)))))
  6499. (org-update-checkbox-count-maybe))
  6500. (defun org-update-checkbox-count-maybe ()
  6501. "Update checkbox statistics unless turned off by user."
  6502. (when org-provide-checkbox-statistics
  6503. (org-update-checkbox-count)))
  6504. (defun org-update-checkbox-count (&optional all)
  6505. "Update the checkbox statistics in the current section.
  6506. This will find all statistic cookies like [57%] and [6/12] and update them
  6507. with the current numbers. With optional prefix argument ALL, do this for
  6508. the whole buffer."
  6509. (interactive "P")
  6510. (save-excursion
  6511. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6512. (beg (condition-case nil
  6513. (progn (outline-back-to-heading) (point))
  6514. (error (point-min))))
  6515. (end (move-marker (make-marker)
  6516. (progn (outline-next-heading) (point))))
  6517. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6518. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6519. (re-find (concat re "\\|" re-box))
  6520. beg-cookie end-cookie is-percent c-on c-off lim
  6521. eline curr-ind next-ind continue-from startsearch
  6522. (cstat 0)
  6523. )
  6524. (when all
  6525. (goto-char (point-min))
  6526. (outline-next-heading)
  6527. (setq beg (point) end (point-max)))
  6528. (goto-char end)
  6529. ;; find each statistic cookie
  6530. (while (re-search-backward re-find beg t)
  6531. (setq beg-cookie (match-beginning 1)
  6532. end-cookie (match-end 1)
  6533. cstat (+ cstat (if end-cookie 1 0))
  6534. startsearch (point-at-eol)
  6535. continue-from (point-at-bol)
  6536. is-percent (match-beginning 2)
  6537. lim (cond
  6538. ((org-on-heading-p) (outline-next-heading) (point))
  6539. ((org-at-item-p) (org-end-of-item) (point))
  6540. (t nil))
  6541. c-on 0
  6542. c-off 0)
  6543. (when lim
  6544. ;; find first checkbox for this cookie and gather
  6545. ;; statistics from all that are at this indentation level
  6546. (goto-char startsearch)
  6547. (if (re-search-forward re-box lim t)
  6548. (progn
  6549. (org-beginning-of-item)
  6550. (setq curr-ind (org-get-indentation))
  6551. (setq next-ind curr-ind)
  6552. (while (= curr-ind next-ind)
  6553. (save-excursion (end-of-line) (setq eline (point)))
  6554. (if (re-search-forward re-box eline t)
  6555. (if (member (match-string 2) '("[ ]" "[-]"))
  6556. (setq c-off (1+ c-off))
  6557. (setq c-on (1+ c-on))
  6558. )
  6559. )
  6560. (org-end-of-item)
  6561. (setq next-ind (org-get-indentation))
  6562. )))
  6563. (goto-char continue-from)
  6564. ;; update cookie
  6565. (when end-cookie
  6566. (delete-region beg-cookie end-cookie)
  6567. (goto-char beg-cookie)
  6568. (insert
  6569. (if is-percent
  6570. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6571. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6572. ;; update items checkbox if it has one
  6573. (when (org-at-item-p)
  6574. (org-beginning-of-item)
  6575. (when (and (> (+ c-on c-off) 0)
  6576. (re-search-forward re-box (point-at-eol) t))
  6577. (setq beg-cookie (match-beginning 2)
  6578. end-cookie (match-end 2))
  6579. (delete-region beg-cookie end-cookie)
  6580. (goto-char beg-cookie)
  6581. (cond ((= c-off 0) (insert "[X]"))
  6582. ((= c-on 0) (insert "[ ]"))
  6583. (t (insert "[-]")))
  6584. )))
  6585. (goto-char continue-from))
  6586. (when (interactive-p)
  6587. (message "Checkbox satistics updated %s (%d places)"
  6588. (if all "in entire file" "in current outline entry") cstat)))))
  6589. (defun org-get-checkbox-statistics-face ()
  6590. "Select the face for checkbox statistics.
  6591. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6592. it will be `org-todo'."
  6593. (if (match-end 1)
  6594. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6595. (if (and (> (match-end 2) (match-beginning 2))
  6596. (equal (match-string 2) (match-string 3)))
  6597. 'org-done
  6598. 'org-todo)))
  6599. (defun org-get-indentation (&optional line)
  6600. "Get the indentation of the current line, interpreting tabs.
  6601. When LINE is given, assume it represents a line and compute its indentation."
  6602. (if line
  6603. (if (string-match "^ *" (org-remove-tabs line))
  6604. (match-end 0))
  6605. (save-excursion
  6606. (beginning-of-line 1)
  6607. (skip-chars-forward " \t")
  6608. (current-column))))
  6609. (defun org-remove-tabs (s &optional width)
  6610. "Replace tabulators in S with spaces.
  6611. Assumes that s is a single line, starting in column 0."
  6612. (setq width (or width tab-width))
  6613. (while (string-match "\t" s)
  6614. (setq s (replace-match
  6615. (make-string
  6616. (- (* width (/ (+ (match-beginning 0) width) width))
  6617. (match-beginning 0)) ?\ )
  6618. t t s)))
  6619. s)
  6620. (defun org-fix-indentation (line ind)
  6621. "Fix indentation in LINE.
  6622. IND is a cons cell with target and minimum indentation.
  6623. If the current indenation in LINE is smaller than the minimum,
  6624. leave it alone. If it is larger than ind, set it to the target."
  6625. (let* ((l (org-remove-tabs line))
  6626. (i (org-get-indentation l))
  6627. (i1 (car ind)) (i2 (cdr ind)))
  6628. (if (>= i i2) (setq l (substring line i2)))
  6629. (if (> i1 0)
  6630. (concat (make-string i1 ?\ ) l)
  6631. l)))
  6632. (defcustom org-empty-line-terminates-plain-lists nil
  6633. "Non-nil means, an empty line ends all plain list levels.
  6634. When nil, empty lines are part of the preceeding item."
  6635. :group 'org-plain-lists
  6636. :type 'boolean)
  6637. (defun org-beginning-of-item ()
  6638. "Go to the beginning of the current hand-formatted item.
  6639. If the cursor is not in an item, throw an error."
  6640. (interactive)
  6641. (let ((pos (point))
  6642. (limit (save-excursion
  6643. (condition-case nil
  6644. (progn
  6645. (org-back-to-heading)
  6646. (beginning-of-line 2) (point))
  6647. (error (point-min)))))
  6648. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6649. ind ind1)
  6650. (if (org-at-item-p)
  6651. (beginning-of-line 1)
  6652. (beginning-of-line 1)
  6653. (skip-chars-forward " \t")
  6654. (setq ind (current-column))
  6655. (if (catch 'exit
  6656. (while t
  6657. (beginning-of-line 0)
  6658. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6659. (if (looking-at "[ \t]*$")
  6660. (setq ind1 ind-empty)
  6661. (skip-chars-forward " \t")
  6662. (setq ind1 (current-column)))
  6663. (if (< ind1 ind)
  6664. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6665. nil
  6666. (goto-char pos)
  6667. (error "Not in an item")))))
  6668. (defun org-end-of-item ()
  6669. "Go to the end of the current hand-formatted item.
  6670. If the cursor is not in an item, throw an error."
  6671. (interactive)
  6672. (let* ((pos (point))
  6673. ind1
  6674. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6675. (limit (save-excursion (outline-next-heading) (point)))
  6676. (ind (save-excursion
  6677. (org-beginning-of-item)
  6678. (skip-chars-forward " \t")
  6679. (current-column)))
  6680. (end (catch 'exit
  6681. (while t
  6682. (beginning-of-line 2)
  6683. (if (eobp) (throw 'exit (point)))
  6684. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6685. (if (looking-at "[ \t]*$")
  6686. (setq ind1 ind-empty)
  6687. (skip-chars-forward " \t")
  6688. (setq ind1 (current-column)))
  6689. (if (<= ind1 ind)
  6690. (throw 'exit (point-at-bol)))))))
  6691. (if end
  6692. (goto-char end)
  6693. (goto-char pos)
  6694. (error "Not in an item"))))
  6695. (defun org-next-item ()
  6696. "Move to the beginning of the next item in the current plain list.
  6697. Error if not at a plain list, or if this is the last item in the list."
  6698. (interactive)
  6699. (let (ind ind1 (pos (point)))
  6700. (org-beginning-of-item)
  6701. (setq ind (org-get-indentation))
  6702. (org-end-of-item)
  6703. (setq ind1 (org-get-indentation))
  6704. (unless (and (org-at-item-p) (= ind ind1))
  6705. (goto-char pos)
  6706. (error "On last item"))))
  6707. (defun org-previous-item ()
  6708. "Move to the beginning of the previous item in the current plain list.
  6709. Error if not at a plain list, or if this is the first item in the list."
  6710. (interactive)
  6711. (let (beg ind ind1 (pos (point)))
  6712. (org-beginning-of-item)
  6713. (setq beg (point))
  6714. (setq ind (org-get-indentation))
  6715. (goto-char beg)
  6716. (catch 'exit
  6717. (while t
  6718. (beginning-of-line 0)
  6719. (if (looking-at "[ \t]*$")
  6720. nil
  6721. (if (<= (setq ind1 (org-get-indentation)) ind)
  6722. (throw 'exit t)))))
  6723. (condition-case nil
  6724. (if (or (not (org-at-item-p))
  6725. (< ind1 (1- ind)))
  6726. (error "")
  6727. (org-beginning-of-item))
  6728. (error (goto-char pos)
  6729. (error "On first item")))))
  6730. (defun org-first-list-item-p ()
  6731. "Is this heading the item in a plain list?"
  6732. (unless (org-at-item-p)
  6733. (error "Not at a plain list item"))
  6734. (org-beginning-of-item)
  6735. (= (point) (save-excursion (org-beginning-of-item-list))))
  6736. (defun org-move-item-down ()
  6737. "Move the plain list item at point down, i.e. swap with following item.
  6738. Subitems (items with larger indentation) are considered part of the item,
  6739. so this really moves item trees."
  6740. (interactive)
  6741. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6742. (org-beginning-of-item)
  6743. (setq beg0 (point))
  6744. (save-excursion
  6745. (setq ne-beg (org-back-over-empty-lines))
  6746. (setq beg (point)))
  6747. (goto-char beg0)
  6748. (setq ind (org-get-indentation))
  6749. (org-end-of-item)
  6750. (setq end0 (point))
  6751. (setq ind1 (org-get-indentation))
  6752. (setq ne-end (org-back-over-empty-lines))
  6753. (setq end (point))
  6754. (goto-char beg0)
  6755. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6756. ;; include less whitespace
  6757. (save-excursion
  6758. (goto-char beg)
  6759. (forward-line (- ne-beg ne-end))
  6760. (setq beg (point))))
  6761. (goto-char end0)
  6762. (if (and (org-at-item-p) (= ind ind1))
  6763. (progn
  6764. (org-end-of-item)
  6765. (org-back-over-empty-lines)
  6766. (setq txt (buffer-substring beg end))
  6767. (save-excursion
  6768. (delete-region beg end))
  6769. (setq pos (point))
  6770. (insert txt)
  6771. (goto-char pos) (org-skip-whitespace)
  6772. (org-maybe-renumber-ordered-list))
  6773. (goto-char pos)
  6774. (error "Cannot move this item further down"))))
  6775. (defun org-move-item-up (arg)
  6776. "Move the plain list item at point up, i.e. swap with previous item.
  6777. Subitems (items with larger indentation) are considered part of the item,
  6778. so this really moves item trees."
  6779. (interactive "p")
  6780. (let (beg beg0 end ind ind1 (pos (point)) txt
  6781. ne-beg ne-ins ins-end)
  6782. (org-beginning-of-item)
  6783. (setq beg0 (point))
  6784. (setq ind (org-get-indentation))
  6785. (save-excursion
  6786. (setq ne-beg (org-back-over-empty-lines))
  6787. (setq beg (point)))
  6788. (goto-char beg0)
  6789. (org-end-of-item)
  6790. (setq end (point))
  6791. (goto-char beg0)
  6792. (catch 'exit
  6793. (while t
  6794. (beginning-of-line 0)
  6795. (if (looking-at "[ \t]*$")
  6796. (if org-empty-line-terminates-plain-lists
  6797. (progn
  6798. (goto-char pos)
  6799. (error "Cannot move this item further up"))
  6800. nil)
  6801. (if (<= (setq ind1 (org-get-indentation)) ind)
  6802. (throw 'exit t)))))
  6803. (condition-case nil
  6804. (org-beginning-of-item)
  6805. (error (goto-char beg)
  6806. (error "Cannot move this item further up")))
  6807. (setq ind1 (org-get-indentation))
  6808. (if (and (org-at-item-p) (= ind ind1))
  6809. (progn
  6810. (setq ne-ins (org-back-over-empty-lines))
  6811. (setq txt (buffer-substring beg end))
  6812. (save-excursion
  6813. (delete-region beg end))
  6814. (setq pos (point))
  6815. (insert txt)
  6816. (setq ins-end (point))
  6817. (goto-char pos) (org-skip-whitespace)
  6818. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6819. ;; Move whitespace back to beginning
  6820. (save-excursion
  6821. (goto-char ins-end)
  6822. (let ((kill-whole-line t))
  6823. (kill-line (- ne-ins ne-beg)) (point)))
  6824. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6825. (org-maybe-renumber-ordered-list))
  6826. (goto-char pos)
  6827. (error "Cannot move this item further up"))))
  6828. (defun org-maybe-renumber-ordered-list ()
  6829. "Renumber the ordered list at point if setup allows it.
  6830. This tests the user option `org-auto-renumber-ordered-lists' before
  6831. doing the renumbering."
  6832. (interactive)
  6833. (when (and org-auto-renumber-ordered-lists
  6834. (org-at-item-p))
  6835. (if (match-beginning 3)
  6836. (org-renumber-ordered-list 1)
  6837. (org-fix-bullet-type))))
  6838. (defun org-maybe-renumber-ordered-list-safe ()
  6839. (condition-case nil
  6840. (save-excursion
  6841. (org-maybe-renumber-ordered-list))
  6842. (error nil)))
  6843. (defun org-cycle-list-bullet (&optional which)
  6844. "Cycle through the different itemize/enumerate bullets.
  6845. This cycle the entire list level through the sequence:
  6846. `-' -> `+' -> `*' -> `1.' -> `1)'
  6847. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6848. 0 meand `-', 1 means `+' etc."
  6849. (interactive "P")
  6850. (org-preserve-lc
  6851. (org-beginning-of-item-list)
  6852. (org-at-item-p)
  6853. (beginning-of-line 1)
  6854. (let ((current (match-string 0))
  6855. (prevp (eq which 'previous))
  6856. new)
  6857. (setq new (cond
  6858. ((and (numberp which)
  6859. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6860. ((string-match "-" current) (if prevp "1)" "+"))
  6861. ((string-match "\\+" current)
  6862. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6863. ((string-match "\\*" current) (if prevp "+" "1."))
  6864. ((string-match "\\." current) (if prevp "*" "1)"))
  6865. ((string-match ")" current) (if prevp "1." "-"))
  6866. (t (error "This should not happen"))))
  6867. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6868. (org-fix-bullet-type)
  6869. (org-maybe-renumber-ordered-list))))
  6870. (defun org-get-string-indentation (s)
  6871. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6872. (let ((n -1) (i 0) (w tab-width) c)
  6873. (catch 'exit
  6874. (while (< (setq n (1+ n)) (length s))
  6875. (setq c (aref s n))
  6876. (cond ((= c ?\ ) (setq i (1+ i)))
  6877. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6878. (t (throw 'exit t)))))
  6879. i))
  6880. (defun org-renumber-ordered-list (arg)
  6881. "Renumber an ordered plain list.
  6882. Cursor needs to be in the first line of an item, the line that starts
  6883. with something like \"1.\" or \"2)\"."
  6884. (interactive "p")
  6885. (unless (and (org-at-item-p)
  6886. (match-beginning 3))
  6887. (error "This is not an ordered list"))
  6888. (let ((line (org-current-line))
  6889. (col (current-column))
  6890. (ind (org-get-string-indentation
  6891. (buffer-substring (point-at-bol) (match-beginning 3))))
  6892. ;; (term (substring (match-string 3) -1))
  6893. ind1 (n (1- arg))
  6894. fmt)
  6895. ;; find where this list begins
  6896. (org-beginning-of-item-list)
  6897. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6898. (setq fmt (concat "%d" (match-string 1)))
  6899. (beginning-of-line 0)
  6900. ;; walk forward and replace these numbers
  6901. (catch 'exit
  6902. (while t
  6903. (catch 'next
  6904. (beginning-of-line 2)
  6905. (if (eobp) (throw 'exit nil))
  6906. (if (looking-at "[ \t]*$") (throw 'next nil))
  6907. (skip-chars-forward " \t") (setq ind1 (current-column))
  6908. (if (> ind1 ind) (throw 'next t))
  6909. (if (< ind1 ind) (throw 'exit t))
  6910. (if (not (org-at-item-p)) (throw 'exit nil))
  6911. (delete-region (match-beginning 2) (match-end 2))
  6912. (goto-char (match-beginning 2))
  6913. (insert (format fmt (setq n (1+ n)))))))
  6914. (goto-line line)
  6915. (move-to-column col)))
  6916. (defun org-fix-bullet-type ()
  6917. "Make sure all items in this list have the same bullet as the firsst item."
  6918. (interactive)
  6919. (unless (org-at-item-p) (error "This is not a list"))
  6920. (let ((line (org-current-line))
  6921. (col (current-column))
  6922. (ind (current-indentation))
  6923. ind1 bullet)
  6924. ;; find where this list begins
  6925. (org-beginning-of-item-list)
  6926. (beginning-of-line 1)
  6927. ;; find out what the bullet type is
  6928. (looking-at "[ \t]*\\(\\S-+\\)")
  6929. (setq bullet (match-string 1))
  6930. ;; walk forward and replace these numbers
  6931. (beginning-of-line 0)
  6932. (catch 'exit
  6933. (while t
  6934. (catch 'next
  6935. (beginning-of-line 2)
  6936. (if (eobp) (throw 'exit nil))
  6937. (if (looking-at "[ \t]*$") (throw 'next nil))
  6938. (skip-chars-forward " \t") (setq ind1 (current-column))
  6939. (if (> ind1 ind) (throw 'next t))
  6940. (if (< ind1 ind) (throw 'exit t))
  6941. (if (not (org-at-item-p)) (throw 'exit nil))
  6942. (skip-chars-forward " \t")
  6943. (looking-at "\\S-+")
  6944. (replace-match bullet))))
  6945. (goto-line line)
  6946. (move-to-column col)
  6947. (if (string-match "[0-9]" bullet)
  6948. (org-renumber-ordered-list 1))))
  6949. (defun org-beginning-of-item-list ()
  6950. "Go to the beginning of the current item list.
  6951. I.e. to the first item in this list."
  6952. (interactive)
  6953. (org-beginning-of-item)
  6954. (let ((pos (point-at-bol))
  6955. (ind (org-get-indentation))
  6956. ind1)
  6957. ;; find where this list begins
  6958. (catch 'exit
  6959. (while t
  6960. (catch 'next
  6961. (beginning-of-line 0)
  6962. (if (looking-at "[ \t]*$")
  6963. (throw (if (bobp) 'exit 'next) t))
  6964. (skip-chars-forward " \t") (setq ind1 (current-column))
  6965. (if (or (< ind1 ind)
  6966. (and (= ind1 ind)
  6967. (not (org-at-item-p)))
  6968. (bobp))
  6969. (throw 'exit t)
  6970. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6971. (goto-char pos)))
  6972. (defun org-end-of-item-list ()
  6973. "Go to the end of the current item list.
  6974. I.e. to the text after the last item."
  6975. (interactive)
  6976. (org-beginning-of-item)
  6977. (let ((pos (point-at-bol))
  6978. (ind (org-get-indentation))
  6979. ind1)
  6980. ;; find where this list begins
  6981. (catch 'exit
  6982. (while t
  6983. (catch 'next
  6984. (beginning-of-line 2)
  6985. (if (looking-at "[ \t]*$")
  6986. (throw (if (eobp) 'exit 'next) t))
  6987. (skip-chars-forward " \t") (setq ind1 (current-column))
  6988. (if (or (< ind1 ind)
  6989. (and (= ind1 ind)
  6990. (not (org-at-item-p)))
  6991. (eobp))
  6992. (progn
  6993. (setq pos (point-at-bol))
  6994. (throw 'exit t))))))
  6995. (goto-char pos)))
  6996. (defvar org-last-indent-begin-marker (make-marker))
  6997. (defvar org-last-indent-end-marker (make-marker))
  6998. (defun org-outdent-item (arg)
  6999. "Outdent a local list item."
  7000. (interactive "p")
  7001. (org-indent-item (- arg)))
  7002. (defun org-indent-item (arg)
  7003. "Indent a local list item."
  7004. (interactive "p")
  7005. (unless (org-at-item-p)
  7006. (error "Not on an item"))
  7007. (save-excursion
  7008. (let (beg end ind ind1 tmp delta ind-down ind-up)
  7009. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  7010. (setq beg org-last-indent-begin-marker
  7011. end org-last-indent-end-marker)
  7012. (org-beginning-of-item)
  7013. (setq beg (move-marker org-last-indent-begin-marker (point)))
  7014. (org-end-of-item)
  7015. (setq end (move-marker org-last-indent-end-marker (point))))
  7016. (goto-char beg)
  7017. (setq tmp (org-item-indent-positions)
  7018. ind (car tmp)
  7019. ind-down (nth 2 tmp)
  7020. ind-up (nth 1 tmp)
  7021. delta (if (> arg 0)
  7022. (if ind-down (- ind-down ind) 2)
  7023. (if ind-up (- ind-up ind) -2)))
  7024. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  7025. (while (< (point) end)
  7026. (beginning-of-line 1)
  7027. (skip-chars-forward " \t") (setq ind1 (current-column))
  7028. (delete-region (point-at-bol) (point))
  7029. (or (eolp) (indent-to-column (+ ind1 delta)))
  7030. (beginning-of-line 2))))
  7031. (org-fix-bullet-type)
  7032. (org-maybe-renumber-ordered-list-safe)
  7033. (save-excursion
  7034. (beginning-of-line 0)
  7035. (condition-case nil (org-beginning-of-item) (error nil))
  7036. (org-maybe-renumber-ordered-list-safe)))
  7037. (defun org-item-indent-positions ()
  7038. "Return indentation for plain list items.
  7039. This returns a list with three values: The current indentation, the
  7040. parent indentation and the indentation a child should habe.
  7041. Assumes cursor in item line."
  7042. (let* ((bolpos (point-at-bol))
  7043. (ind (org-get-indentation))
  7044. ind-down ind-up pos)
  7045. (save-excursion
  7046. (org-beginning-of-item-list)
  7047. (skip-chars-backward "\n\r \t")
  7048. (when (org-in-item-p)
  7049. (org-beginning-of-item)
  7050. (setq ind-up (org-get-indentation))))
  7051. (setq pos (point))
  7052. (save-excursion
  7053. (cond
  7054. ((and (condition-case nil (progn (org-previous-item) t)
  7055. (error nil))
  7056. (or (forward-char 1) t)
  7057. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  7058. (setq ind-down (org-get-indentation)))
  7059. ((and (goto-char pos)
  7060. (org-at-item-p))
  7061. (goto-char (match-end 0))
  7062. (skip-chars-forward " \t")
  7063. (setq ind-down (current-column)))))
  7064. (list ind ind-up ind-down)))
  7065. ;;; The orgstruct minor mode
  7066. ;; Define a minor mode which can be used in other modes in order to
  7067. ;; integrate the org-mode structure editing commands.
  7068. ;; This is really a hack, because the org-mode structure commands use
  7069. ;; keys which normally belong to the major mode. Here is how it
  7070. ;; works: The minor mode defines all the keys necessary to operate the
  7071. ;; structure commands, but wraps the commands into a function which
  7072. ;; tests if the cursor is currently at a headline or a plain list
  7073. ;; item. If that is the case, the structure command is used,
  7074. ;; temporarily setting many Org-mode variables like regular
  7075. ;; expressions for filling etc. However, when any of those keys is
  7076. ;; used at a different location, function uses `key-binding' to look
  7077. ;; up if the key has an associated command in another currently active
  7078. ;; keymap (minor modes, major mode, global), and executes that
  7079. ;; command. There might be problems if any of the keys is otherwise
  7080. ;; used as a prefix key.
  7081. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7082. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7083. ;; addresses this by checking explicitly for both bindings.
  7084. (defvar orgstruct-mode-map (make-sparse-keymap)
  7085. "Keymap for the minor `orgstruct-mode'.")
  7086. (defvar org-local-vars nil
  7087. "List of local variables, for use by `orgstruct-mode'")
  7088. ;;;###autoload
  7089. (define-minor-mode orgstruct-mode
  7090. "Toggle the minor more `orgstruct-mode'.
  7091. This mode is for using Org-mode structure commands in other modes.
  7092. The following key behave as if Org-mode was active, if the cursor
  7093. is on a headline, or on a plain list item (both in the definition
  7094. of Org-mode).
  7095. M-up Move entry/item up
  7096. M-down Move entry/item down
  7097. M-left Promote
  7098. M-right Demote
  7099. M-S-up Move entry/item up
  7100. M-S-down Move entry/item down
  7101. M-S-left Promote subtree
  7102. M-S-right Demote subtree
  7103. M-q Fill paragraph and items like in Org-mode
  7104. C-c ^ Sort entries
  7105. C-c - Cycle list bullet
  7106. TAB Cycle item visibility
  7107. M-RET Insert new heading/item
  7108. S-M-RET Insert new TODO heading / Chekbox item
  7109. C-c C-c Set tags / toggle checkbox"
  7110. nil " OrgStruct" nil
  7111. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7112. ;;;###autoload
  7113. (defun turn-on-orgstruct ()
  7114. "Unconditionally turn on `orgstruct-mode'."
  7115. (orgstruct-mode 1))
  7116. ;;;###autoload
  7117. (defun turn-on-orgstruct++ ()
  7118. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7119. In addition to setting orgstruct-mode, this also exports all indentation and
  7120. autofilling variables from org-mode into the buffer. Note that turning
  7121. off orgstruct-mode will *not* remove these additional settings."
  7122. (orgstruct-mode 1)
  7123. (let (var val)
  7124. (mapc
  7125. (lambda (x)
  7126. (when (string-match
  7127. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7128. (symbol-name (car x)))
  7129. (setq var (car x) val (nth 1 x))
  7130. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7131. org-local-vars)))
  7132. (defun orgstruct-error ()
  7133. "Error when there is no default binding for a structure key."
  7134. (interactive)
  7135. (error "This key has no function outside structure elements"))
  7136. (defun orgstruct-setup ()
  7137. "Setup orgstruct keymaps."
  7138. (let ((nfunc 0)
  7139. (bindings
  7140. (list
  7141. '([(meta up)] org-metaup)
  7142. '([(meta down)] org-metadown)
  7143. '([(meta left)] org-metaleft)
  7144. '([(meta right)] org-metaright)
  7145. '([(meta shift up)] org-shiftmetaup)
  7146. '([(meta shift down)] org-shiftmetadown)
  7147. '([(meta shift left)] org-shiftmetaleft)
  7148. '([(meta shift right)] org-shiftmetaright)
  7149. '([(shift up)] org-shiftup)
  7150. '([(shift down)] org-shiftdown)
  7151. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7152. '("\M-q" fill-paragraph)
  7153. '("\C-c^" org-sort)
  7154. '("\C-c-" org-cycle-list-bullet)))
  7155. elt key fun cmd)
  7156. (while (setq elt (pop bindings))
  7157. (setq nfunc (1+ nfunc))
  7158. (setq key (org-key (car elt))
  7159. fun (nth 1 elt)
  7160. cmd (orgstruct-make-binding fun nfunc key))
  7161. (org-defkey orgstruct-mode-map key cmd))
  7162. ;; Special treatment needed for TAB and RET
  7163. (org-defkey orgstruct-mode-map [(tab)]
  7164. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7165. (org-defkey orgstruct-mode-map "\C-i"
  7166. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7167. (org-defkey orgstruct-mode-map "\M-\C-m"
  7168. (orgstruct-make-binding 'org-insert-heading 105
  7169. "\M-\C-m" [(meta return)]))
  7170. (org-defkey orgstruct-mode-map [(meta return)]
  7171. (orgstruct-make-binding 'org-insert-heading 106
  7172. [(meta return)] "\M-\C-m"))
  7173. (org-defkey orgstruct-mode-map [(shift meta return)]
  7174. (orgstruct-make-binding 'org-insert-todo-heading 107
  7175. [(meta return)] "\M-\C-m"))
  7176. (unless org-local-vars
  7177. (setq org-local-vars (org-get-local-variables)))
  7178. t))
  7179. (defun orgstruct-make-binding (fun n &rest keys)
  7180. "Create a function for binding in the structure minor mode.
  7181. FUN is the command to call inside a table. N is used to create a unique
  7182. command name. KEYS are keys that should be checked in for a command
  7183. to execute outside of tables."
  7184. (eval
  7185. (list 'defun
  7186. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7187. '(arg)
  7188. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7189. "Outside of structure, run the binding of `"
  7190. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7191. "'.")
  7192. '(interactive "p")
  7193. (list 'if
  7194. '(org-context-p 'headline 'item)
  7195. (list 'org-run-like-in-org-mode (list 'quote fun))
  7196. (list 'let '(orgstruct-mode)
  7197. (list 'call-interactively
  7198. (append '(or)
  7199. (mapcar (lambda (k)
  7200. (list 'key-binding k))
  7201. keys)
  7202. '('orgstruct-error))))))))
  7203. (defun org-context-p (&rest contexts)
  7204. "Check if local context is and of CONTEXTS.
  7205. Possible values in the list of contexts are `table', `headline', and `item'."
  7206. (let ((pos (point)))
  7207. (goto-char (point-at-bol))
  7208. (prog1 (or (and (memq 'table contexts)
  7209. (looking-at "[ \t]*|"))
  7210. (and (memq 'headline contexts)
  7211. (looking-at "\\*+"))
  7212. (and (memq 'item contexts)
  7213. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7214. (goto-char pos))))
  7215. (defun org-get-local-variables ()
  7216. "Return a list of all local variables in an org-mode buffer."
  7217. (let (varlist)
  7218. (with-current-buffer (get-buffer-create "*Org tmp*")
  7219. (erase-buffer)
  7220. (org-mode)
  7221. (setq varlist (buffer-local-variables)))
  7222. (kill-buffer "*Org tmp*")
  7223. (delq nil
  7224. (mapcar
  7225. (lambda (x)
  7226. (setq x
  7227. (if (symbolp x)
  7228. (list x)
  7229. (list (car x) (list 'quote (cdr x)))))
  7230. (if (string-match
  7231. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7232. (symbol-name (car x)))
  7233. x nil))
  7234. varlist))))
  7235. ;;;###autoload
  7236. (defun org-run-like-in-org-mode (cmd)
  7237. (unless org-local-vars
  7238. (setq org-local-vars (org-get-local-variables)))
  7239. (eval (list 'let org-local-vars
  7240. (list 'call-interactively (list 'quote cmd)))))
  7241. ;;;; Archiving
  7242. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7243. (defun org-archive-subtree (&optional find-done)
  7244. "Move the current subtree to the archive.
  7245. The archive can be a certain top-level heading in the current file, or in
  7246. a different file. The tree will be moved to that location, the subtree
  7247. heading be marked DONE, and the current time will be added.
  7248. When called with prefix argument FIND-DONE, find whole trees without any
  7249. open TODO items and archive them (after getting confirmation from the user).
  7250. If the cursor is not at a headline when this comand is called, try all level
  7251. 1 trees. If the cursor is on a headline, only try the direct children of
  7252. this heading."
  7253. (interactive "P")
  7254. (if find-done
  7255. (org-archive-all-done)
  7256. ;; Save all relevant TODO keyword-relatex variables
  7257. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7258. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7259. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7260. (tr-org-done-keywords org-done-keywords)
  7261. (tr-org-todo-regexp org-todo-regexp)
  7262. (tr-org-todo-line-regexp org-todo-line-regexp)
  7263. (tr-org-odd-levels-only org-odd-levels-only)
  7264. (this-buffer (current-buffer))
  7265. (org-archive-location org-archive-location)
  7266. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7267. ;; start of variables that will be used for saving context
  7268. ;; The compiler complains about them - keep them anyway!
  7269. (file (abbreviate-file-name (buffer-file-name)))
  7270. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7271. (time (format-time-string
  7272. (substring (cdr org-time-stamp-formats) 1 -1)
  7273. (current-time)))
  7274. afile heading buffer level newfile-p
  7275. category todo priority
  7276. ;; start of variables that will be used for savind context
  7277. ltags itags prop)
  7278. ;; Try to find a local archive location
  7279. (save-excursion
  7280. (save-restriction
  7281. (widen)
  7282. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7283. (if (and prop (string-match "\\S-" prop))
  7284. (setq org-archive-location prop)
  7285. (if (or (re-search-backward re nil t)
  7286. (re-search-forward re nil t))
  7287. (setq org-archive-location (match-string 1))))))
  7288. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7289. (progn
  7290. (setq afile (format (match-string 1 org-archive-location)
  7291. (file-name-nondirectory buffer-file-name))
  7292. heading (match-string 2 org-archive-location)))
  7293. (error "Invalid `org-archive-location'"))
  7294. (if (> (length afile) 0)
  7295. (setq newfile-p (not (file-exists-p afile))
  7296. buffer (find-file-noselect afile))
  7297. (setq buffer (current-buffer)))
  7298. (unless buffer
  7299. (error "Cannot access file \"%s\"" afile))
  7300. (if (and (> (length heading) 0)
  7301. (string-match "^\\*+" heading))
  7302. (setq level (match-end 0))
  7303. (setq heading nil level 0))
  7304. (save-excursion
  7305. (org-back-to-heading t)
  7306. ;; Get context information that will be lost by moving the tree
  7307. (org-refresh-category-properties)
  7308. (setq category (org-get-category)
  7309. todo (and (looking-at org-todo-line-regexp)
  7310. (match-string 2))
  7311. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7312. ltags (org-get-tags)
  7313. itags (org-delete-all ltags (org-get-tags-at)))
  7314. (setq ltags (mapconcat 'identity ltags " ")
  7315. itags (mapconcat 'identity itags " "))
  7316. ;; We first only copy, in case something goes wrong
  7317. ;; we need to protect this-command, to avoid kill-region sets it,
  7318. ;; which would lead to duplication of subtrees
  7319. (let (this-command) (org-copy-subtree))
  7320. (set-buffer buffer)
  7321. ;; Enforce org-mode for the archive buffer
  7322. (if (not (org-mode-p))
  7323. ;; Force the mode for future visits.
  7324. (let ((org-insert-mode-line-in-empty-file t)
  7325. (org-inhibit-startup t))
  7326. (call-interactively 'org-mode)))
  7327. (when newfile-p
  7328. (goto-char (point-max))
  7329. (insert (format "\nArchived entries from file %s\n\n"
  7330. (buffer-file-name this-buffer))))
  7331. ;; Force the TODO keywords of the original buffer
  7332. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7333. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7334. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7335. (org-done-keywords tr-org-done-keywords)
  7336. (org-todo-regexp tr-org-todo-regexp)
  7337. (org-todo-line-regexp tr-org-todo-line-regexp)
  7338. (org-odd-levels-only
  7339. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7340. org-odd-levels-only
  7341. tr-org-odd-levels-only)))
  7342. (goto-char (point-min))
  7343. (show-all)
  7344. (if heading
  7345. (progn
  7346. (if (re-search-forward
  7347. (concat "^" (regexp-quote heading)
  7348. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7349. nil t)
  7350. (goto-char (match-end 0))
  7351. ;; Heading not found, just insert it at the end
  7352. (goto-char (point-max))
  7353. (or (bolp) (insert "\n"))
  7354. (insert "\n" heading "\n")
  7355. (end-of-line 0))
  7356. ;; Make the subtree visible
  7357. (show-subtree)
  7358. (org-end-of-subtree t)
  7359. (skip-chars-backward " \t\r\n")
  7360. (and (looking-at "[ \t\r\n]*")
  7361. (replace-match "\n\n")))
  7362. ;; No specific heading, just go to end of file.
  7363. (goto-char (point-max)) (insert "\n"))
  7364. ;; Paste
  7365. (org-paste-subtree (org-get-valid-level level 1))
  7366. ;; Mark the entry as done
  7367. (when (and org-archive-mark-done
  7368. (looking-at org-todo-line-regexp)
  7369. (or (not (match-end 2))
  7370. (not (member (match-string 2) org-done-keywords))))
  7371. (let (org-log-done org-todo-log-states)
  7372. (org-todo
  7373. (car (or (member org-archive-mark-done org-done-keywords)
  7374. org-done-keywords)))))
  7375. ;; Add the context info
  7376. (when org-archive-save-context-info
  7377. (let ((l org-archive-save-context-info) e n v)
  7378. (while (setq e (pop l))
  7379. (when (and (setq v (symbol-value e))
  7380. (stringp v) (string-match "\\S-" v))
  7381. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7382. (org-entry-put (point) n v)))))
  7383. ;; Save and kill the buffer, if it is not the same buffer.
  7384. (if (not (eq this-buffer buffer))
  7385. (progn (save-buffer) (kill-buffer buffer)))))
  7386. ;; Here we are back in the original buffer. Everything seems to have
  7387. ;; worked. So now cut the tree and finish up.
  7388. (let (this-command) (org-cut-subtree))
  7389. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7390. (message "Subtree archived %s"
  7391. (if (eq this-buffer buffer)
  7392. (concat "under heading: " heading)
  7393. (concat "in file: " (abbreviate-file-name afile)))))))
  7394. (defun org-refresh-category-properties ()
  7395. "Refresh category text properties in teh buffer."
  7396. (let ((def-cat (cond
  7397. ((null org-category)
  7398. (if buffer-file-name
  7399. (file-name-sans-extension
  7400. (file-name-nondirectory buffer-file-name))
  7401. "???"))
  7402. ((symbolp org-category) (symbol-name org-category))
  7403. (t org-category)))
  7404. beg end cat pos optionp)
  7405. (org-unmodified
  7406. (save-excursion
  7407. (save-restriction
  7408. (widen)
  7409. (goto-char (point-min))
  7410. (put-text-property (point) (point-max) 'org-category def-cat)
  7411. (while (re-search-forward
  7412. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7413. (setq pos (match-end 0)
  7414. optionp (equal (char-after (match-beginning 0)) ?#)
  7415. cat (org-trim (match-string 2)))
  7416. (if optionp
  7417. (setq beg (point-at-bol) end (point-max))
  7418. (org-back-to-heading t)
  7419. (setq beg (point) end (org-end-of-subtree t t)))
  7420. (put-text-property beg end 'org-category cat)
  7421. (goto-char pos)))))))
  7422. (defun org-archive-all-done (&optional tag)
  7423. "Archive sublevels of the current tree without open TODO items.
  7424. If the cursor is not on a headline, try all level 1 trees. If
  7425. it is on a headline, try all direct children.
  7426. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7427. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7428. (rea (concat ".*:" org-archive-tag ":"))
  7429. (begm (make-marker))
  7430. (endm (make-marker))
  7431. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7432. "Move subtree to archive (no open TODO items)? "))
  7433. beg end (cntarch 0))
  7434. (if (org-on-heading-p)
  7435. (progn
  7436. (setq re1 (concat "^" (regexp-quote
  7437. (make-string
  7438. (1+ (- (match-end 0) (match-beginning 0) 1))
  7439. ?*))
  7440. " "))
  7441. (move-marker begm (point))
  7442. (move-marker endm (org-end-of-subtree t)))
  7443. (setq re1 "^* ")
  7444. (move-marker begm (point-min))
  7445. (move-marker endm (point-max)))
  7446. (save-excursion
  7447. (goto-char begm)
  7448. (while (re-search-forward re1 endm t)
  7449. (setq beg (match-beginning 0)
  7450. end (save-excursion (org-end-of-subtree t) (point)))
  7451. (goto-char beg)
  7452. (if (re-search-forward re end t)
  7453. (goto-char end)
  7454. (goto-char beg)
  7455. (if (and (or (not tag) (not (looking-at rea)))
  7456. (y-or-n-p question))
  7457. (progn
  7458. (if tag
  7459. (org-toggle-tag org-archive-tag 'on)
  7460. (org-archive-subtree))
  7461. (setq cntarch (1+ cntarch)))
  7462. (goto-char end)))))
  7463. (message "%d trees archived" cntarch)))
  7464. (defun org-cycle-hide-drawers (state)
  7465. "Re-hide all drawers after a visibility state change."
  7466. (when (and (org-mode-p)
  7467. (not (memq state '(overview folded))))
  7468. (save-excursion
  7469. (let* ((globalp (memq state '(contents all)))
  7470. (beg (if globalp (point-min) (point)))
  7471. (end (if globalp (point-max) (org-end-of-subtree t))))
  7472. (goto-char beg)
  7473. (while (re-search-forward org-drawer-regexp end t)
  7474. (org-flag-drawer t))))))
  7475. (defun org-flag-drawer (flag)
  7476. (save-excursion
  7477. (beginning-of-line 1)
  7478. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7479. (let ((b (match-end 0))
  7480. (outline-regexp org-outline-regexp))
  7481. (if (re-search-forward
  7482. "^[ \t]*:END:"
  7483. (save-excursion (outline-next-heading) (point)) t)
  7484. (outline-flag-region b (point-at-eol) flag)
  7485. (error ":END: line missing"))))))
  7486. (defun org-cycle-hide-archived-subtrees (state)
  7487. "Re-hide all archived subtrees after a visibility state change."
  7488. (when (and (not org-cycle-open-archived-trees)
  7489. (not (memq state '(overview folded))))
  7490. (save-excursion
  7491. (let* ((globalp (memq state '(contents all)))
  7492. (beg (if globalp (point-min) (point)))
  7493. (end (if globalp (point-max) (org-end-of-subtree t))))
  7494. (org-hide-archived-subtrees beg end)
  7495. (goto-char beg)
  7496. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7497. (message "%s" (substitute-command-keys
  7498. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7499. (defun org-force-cycle-archived ()
  7500. "Cycle subtree even if it is archived."
  7501. (interactive)
  7502. (setq this-command 'org-cycle)
  7503. (let ((org-cycle-open-archived-trees t))
  7504. (call-interactively 'org-cycle)))
  7505. (defun org-hide-archived-subtrees (beg end)
  7506. "Re-hide all archived subtrees after a visibility state change."
  7507. (save-excursion
  7508. (let* ((re (concat ":" org-archive-tag ":")))
  7509. (goto-char beg)
  7510. (while (re-search-forward re end t)
  7511. (and (org-on-heading-p) (hide-subtree))
  7512. (org-end-of-subtree t)))))
  7513. (defun org-toggle-tag (tag &optional onoff)
  7514. "Toggle the tag TAG for the current line.
  7515. If ONOFF is `on' or `off', don't toggle but set to this state."
  7516. (unless (org-on-heading-p t) (error "Not on headling"))
  7517. (let (res current)
  7518. (save-excursion
  7519. (beginning-of-line)
  7520. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7521. (point-at-eol) t)
  7522. (progn
  7523. (setq current (match-string 1))
  7524. (replace-match ""))
  7525. (setq current ""))
  7526. (setq current (nreverse (org-split-string current ":")))
  7527. (cond
  7528. ((eq onoff 'on)
  7529. (setq res t)
  7530. (or (member tag current) (push tag current)))
  7531. ((eq onoff 'off)
  7532. (or (not (member tag current)) (setq current (delete tag current))))
  7533. (t (if (member tag current)
  7534. (setq current (delete tag current))
  7535. (setq res t)
  7536. (push tag current))))
  7537. (end-of-line 1)
  7538. (if current
  7539. (progn
  7540. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7541. (org-set-tags nil t))
  7542. (delete-horizontal-space))
  7543. (run-hooks 'org-after-tags-change-hook))
  7544. res))
  7545. (defun org-toggle-archive-tag (&optional arg)
  7546. "Toggle the archive tag for the current headline.
  7547. With prefix ARG, check all children of current headline and offer tagging
  7548. the children that do not contain any open TODO items."
  7549. (interactive "P")
  7550. (if arg
  7551. (org-archive-all-done 'tag)
  7552. (let (set)
  7553. (save-excursion
  7554. (org-back-to-heading t)
  7555. (setq set (org-toggle-tag org-archive-tag))
  7556. (when set (hide-subtree)))
  7557. (and set (beginning-of-line 1))
  7558. (message "Subtree %s" (if set "archived" "unarchived")))))
  7559. ;;;; Tables
  7560. ;;; The table editor
  7561. ;; Watch out: Here we are talking about two different kind of tables.
  7562. ;; Most of the code is for the tables created with the Org-mode table editor.
  7563. ;; Sometimes, we talk about tables created and edited with the table.el
  7564. ;; Emacs package. We call the former org-type tables, and the latter
  7565. ;; table.el-type tables.
  7566. (defun org-before-change-function (beg end)
  7567. "Every change indicates that a table might need an update."
  7568. (setq org-table-may-need-update t))
  7569. (defconst org-table-line-regexp "^[ \t]*|"
  7570. "Detects an org-type table line.")
  7571. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7572. "Detects an org-type table line.")
  7573. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7574. "Detects a table line marked for automatic recalculation.")
  7575. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7576. "Detects a table line marked for automatic recalculation.")
  7577. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7578. "Detects a table line marked for automatic recalculation.")
  7579. (defconst org-table-hline-regexp "^[ \t]*|-"
  7580. "Detects an org-type table hline.")
  7581. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7582. "Detects a table-type table hline.")
  7583. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7584. "Detects an org-type or table-type table.")
  7585. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7586. "Searching from within a table (any type) this finds the first line
  7587. outside the table.")
  7588. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7589. "Searching from within a table (any type) this finds the first line
  7590. outside the table.")
  7591. (defvar org-table-last-highlighted-reference nil)
  7592. (defvar org-table-formula-history nil)
  7593. (defvar org-table-column-names nil
  7594. "Alist with column names, derived from the `!' line.")
  7595. (defvar org-table-column-name-regexp nil
  7596. "Regular expression matching the current column names.")
  7597. (defvar org-table-local-parameters nil
  7598. "Alist with parameter names, derived from the `$' line.")
  7599. (defvar org-table-named-field-locations nil
  7600. "Alist with locations of named fields.")
  7601. (defvar org-table-current-line-types nil
  7602. "Table row types, non-nil only for the duration of a comand.")
  7603. (defvar org-table-current-begin-line nil
  7604. "Table begin line, non-nil only for the duration of a comand.")
  7605. (defvar org-table-current-begin-pos nil
  7606. "Table begin position, non-nil only for the duration of a comand.")
  7607. (defvar org-table-dlines nil
  7608. "Vector of data line line numbers in the current table.")
  7609. (defvar org-table-hlines nil
  7610. "Vector of hline line numbers in the current table.")
  7611. (defconst org-table-range-regexp
  7612. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7613. ;; 1 2 3 4 5
  7614. "Regular expression for matching ranges in formulas.")
  7615. (defconst org-table-range-regexp2
  7616. (concat
  7617. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7618. "\\.\\."
  7619. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7620. "Match a range for reference display.")
  7621. (defconst org-table-translate-regexp
  7622. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7623. "Match a reference that needs translation, for reference display.")
  7624. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7625. (defun org-table-create-with-table.el ()
  7626. "Use the table.el package to insert a new table.
  7627. If there is already a table at point, convert between Org-mode tables
  7628. and table.el tables."
  7629. (interactive)
  7630. (require 'table)
  7631. (cond
  7632. ((org-at-table.el-p)
  7633. (if (y-or-n-p "Convert table to Org-mode table? ")
  7634. (org-table-convert)))
  7635. ((org-at-table-p)
  7636. (if (y-or-n-p "Convert table to table.el table? ")
  7637. (org-table-convert)))
  7638. (t (call-interactively 'table-insert))))
  7639. (defun org-table-create-or-convert-from-region (arg)
  7640. "Convert region to table, or create an empty table.
  7641. If there is an active region, convert it to a table, using the function
  7642. `org-table-convert-region'. See the documentation of that function
  7643. to learn how the prefix argument is interpreted to determine the field
  7644. separator.
  7645. If there is no such region, create an empty table with `org-table-create'."
  7646. (interactive "P")
  7647. (if (org-region-active-p)
  7648. (org-table-convert-region (region-beginning) (region-end) arg)
  7649. (org-table-create arg)))
  7650. (defun org-table-create (&optional size)
  7651. "Query for a size and insert a table skeleton.
  7652. SIZE is a string Columns x Rows like for example \"3x2\"."
  7653. (interactive "P")
  7654. (unless size
  7655. (setq size (read-string
  7656. (concat "Table size Columns x Rows [e.g. "
  7657. org-table-default-size "]: ")
  7658. "" nil org-table-default-size)))
  7659. (let* ((pos (point))
  7660. (indent (make-string (current-column) ?\ ))
  7661. (split (org-split-string size " *x *"))
  7662. (rows (string-to-number (nth 1 split)))
  7663. (columns (string-to-number (car split)))
  7664. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7665. "\n")))
  7666. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7667. (point-at-bol) (point)))
  7668. (beginning-of-line 1)
  7669. (newline))
  7670. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7671. (dotimes (i rows) (insert line))
  7672. (goto-char pos)
  7673. (if (> rows 1)
  7674. ;; Insert a hline after the first row.
  7675. (progn
  7676. (end-of-line 1)
  7677. (insert "\n|-")
  7678. (goto-char pos)))
  7679. (org-table-align)))
  7680. (defun org-table-convert-region (beg0 end0 &optional separator)
  7681. "Convert region to a table.
  7682. The region goes from BEG0 to END0, but these borders will be moved
  7683. slightly, to make sure a beginning of line in the first line is included.
  7684. SEPARATOR specifies the field separator in the lines. It can have the
  7685. following values:
  7686. '(4) Use the comma as a field separator
  7687. '(16) Use a TAB as field separator
  7688. integer When a number, use that many spaces as field separator
  7689. nil When nil, the command tries to be smart and figure out the
  7690. separator in the following way:
  7691. - when each line contains a TAB, assume TAB-separated material
  7692. - when each line contains a comme, assume CSV material
  7693. - else, assume one or more SPACE charcters as separator."
  7694. (interactive "rP")
  7695. (let* ((beg (min beg0 end0))
  7696. (end (max beg0 end0))
  7697. re)
  7698. (goto-char beg)
  7699. (beginning-of-line 1)
  7700. (setq beg (move-marker (make-marker) (point)))
  7701. (goto-char end)
  7702. (if (bolp) (backward-char 1) (end-of-line 1))
  7703. (setq end (move-marker (make-marker) (point)))
  7704. ;; Get the right field separator
  7705. (unless separator
  7706. (goto-char beg)
  7707. (setq separator
  7708. (cond
  7709. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7710. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7711. (t 1))))
  7712. (setq re (cond
  7713. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7714. ((equal separator '(16)) "^\\|\t")
  7715. ((integerp separator)
  7716. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7717. (t (error "This should not happen"))))
  7718. (goto-char beg)
  7719. (while (re-search-forward re end t)
  7720. (replace-match "| " t t))
  7721. (goto-char beg)
  7722. (insert " ")
  7723. (org-table-align)))
  7724. (defun org-table-import (file arg)
  7725. "Import FILE as a table.
  7726. The file is assumed to be tab-separated. Such files can be produced by most
  7727. spreadsheet and database applications. If no tabs (at least one per line)
  7728. are found, lines will be split on whitespace into fields."
  7729. (interactive "f\nP")
  7730. (or (bolp) (newline))
  7731. (let ((beg (point))
  7732. (pm (point-max)))
  7733. (insert-file-contents file)
  7734. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7735. (defun org-table-export ()
  7736. "Export table as a tab-separated file.
  7737. Such a file can be imported into a spreadsheet program like Excel."
  7738. (interactive)
  7739. (let* ((beg (org-table-begin))
  7740. (end (org-table-end))
  7741. (table (buffer-substring beg end))
  7742. (file (read-file-name "Export table to: "))
  7743. buf)
  7744. (unless (or (not (file-exists-p file))
  7745. (y-or-n-p (format "Overwrite file %s? " file)))
  7746. (error "Abort"))
  7747. (with-current-buffer (find-file-noselect file)
  7748. (setq buf (current-buffer))
  7749. (erase-buffer)
  7750. (fundamental-mode)
  7751. (insert table)
  7752. (goto-char (point-min))
  7753. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7754. (replace-match "" t t)
  7755. (end-of-line 1))
  7756. (goto-char (point-min))
  7757. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7758. (replace-match "" t t)
  7759. (goto-char (min (1+ (point)) (point-max))))
  7760. (goto-char (point-min))
  7761. (while (re-search-forward "^-[-+]*$" nil t)
  7762. (replace-match "")
  7763. (if (looking-at "\n")
  7764. (delete-char 1)))
  7765. (goto-char (point-min))
  7766. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7767. (replace-match "\t" t t))
  7768. (save-buffer))
  7769. (kill-buffer buf)))
  7770. (defvar org-table-aligned-begin-marker (make-marker)
  7771. "Marker at the beginning of the table last aligned.
  7772. Used to check if cursor still is in that table, to minimize realignment.")
  7773. (defvar org-table-aligned-end-marker (make-marker)
  7774. "Marker at the end of the table last aligned.
  7775. Used to check if cursor still is in that table, to minimize realignment.")
  7776. (defvar org-table-last-alignment nil
  7777. "List of flags for flushright alignment, from the last re-alignment.
  7778. This is being used to correctly align a single field after TAB or RET.")
  7779. (defvar org-table-last-column-widths nil
  7780. "List of max width of fields in each column.
  7781. This is being used to correctly align a single field after TAB or RET.")
  7782. (defvar org-table-overlay-coordinates nil
  7783. "Overlay coordinates after each align of a table.")
  7784. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7785. (defvar org-last-recalc-line nil)
  7786. (defconst org-narrow-column-arrow "=>"
  7787. "Used as display property in narrowed table columns.")
  7788. (defun org-table-align ()
  7789. "Align the table at point by aligning all vertical bars."
  7790. (interactive)
  7791. (let* (
  7792. ;; Limits of table
  7793. (beg (org-table-begin))
  7794. (end (org-table-end))
  7795. ;; Current cursor position
  7796. (linepos (org-current-line))
  7797. (colpos (org-table-current-column))
  7798. (winstart (window-start))
  7799. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7800. lines (new "") lengths l typenums ty fields maxfields i
  7801. column
  7802. (indent "") cnt frac
  7803. rfmt hfmt
  7804. (spaces '(1 . 1))
  7805. (sp1 (car spaces))
  7806. (sp2 (cdr spaces))
  7807. (rfmt1 (concat
  7808. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7809. (hfmt1 (concat
  7810. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7811. emptystrings links dates emph narrow fmax f1 len c e)
  7812. (untabify beg end)
  7813. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7814. ;; Check if we have links or dates
  7815. (goto-char beg)
  7816. (setq links (re-search-forward org-bracket-link-regexp end t))
  7817. (goto-char beg)
  7818. (setq emph (and org-hide-emphasis-markers
  7819. (re-search-forward org-emph-re end t)))
  7820. (goto-char beg)
  7821. (setq dates (and org-display-custom-times
  7822. (re-search-forward org-ts-regexp-both end t)))
  7823. ;; Make sure the link properties are right
  7824. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7825. ;; Make sure the date properties are right
  7826. (when dates (goto-char beg) (while (org-activate-dates end)))
  7827. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7828. ;; Check if we are narrowing any columns
  7829. (goto-char beg)
  7830. (setq narrow (and org-format-transports-properties-p
  7831. (re-search-forward "<[0-9]+>" end t)))
  7832. ;; Get the rows
  7833. (setq lines (org-split-string
  7834. (buffer-substring beg end) "\n"))
  7835. ;; Store the indentation of the first line
  7836. (if (string-match "^ *" (car lines))
  7837. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7838. ;; Mark the hlines by setting the corresponding element to nil
  7839. ;; At the same time, we remove trailing space.
  7840. (setq lines (mapcar (lambda (l)
  7841. (if (string-match "^ *|-" l)
  7842. nil
  7843. (if (string-match "[ \t]+$" l)
  7844. (substring l 0 (match-beginning 0))
  7845. l)))
  7846. lines))
  7847. ;; Get the data fields by splitting the lines.
  7848. (setq fields (mapcar
  7849. (lambda (l)
  7850. (org-split-string l " *| *"))
  7851. (delq nil (copy-sequence lines))))
  7852. ;; How many fields in the longest line?
  7853. (condition-case nil
  7854. (setq maxfields (apply 'max (mapcar 'length fields)))
  7855. (error
  7856. (kill-region beg end)
  7857. (org-table-create org-table-default-size)
  7858. (error "Empty table - created default table")))
  7859. ;; A list of empty strings to fill any short rows on output
  7860. (setq emptystrings (make-list maxfields ""))
  7861. ;; Check for special formatting.
  7862. (setq i -1)
  7863. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7864. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7865. ;; Check if there is an explicit width specified
  7866. (when narrow
  7867. (setq c column fmax nil)
  7868. (while c
  7869. (setq e (pop c))
  7870. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7871. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7872. ;; Find fields that are wider than fmax, and shorten them
  7873. (when fmax
  7874. (loop for xx in column do
  7875. (when (and (stringp xx)
  7876. (> (org-string-width xx) fmax))
  7877. (org-add-props xx nil
  7878. 'help-echo
  7879. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7880. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7881. (unless (> f1 1)
  7882. (error "Cannot narrow field starting with wide link \"%s\""
  7883. (match-string 0 xx)))
  7884. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7885. (add-text-properties (- f1 2) f1
  7886. (list 'display org-narrow-column-arrow)
  7887. xx)))))
  7888. ;; Get the maximum width for each column
  7889. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7890. ;; Get the fraction of numbers, to decide about alignment of the column
  7891. (setq cnt 0 frac 0.0)
  7892. (loop for x in column do
  7893. (if (equal x "")
  7894. nil
  7895. (setq frac ( / (+ (* frac cnt)
  7896. (if (string-match org-table-number-regexp x) 1 0))
  7897. (setq cnt (1+ cnt))))))
  7898. (push (>= frac org-table-number-fraction) typenums))
  7899. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7900. ;; Store the alignment of this table, for later editing of single fields
  7901. (setq org-table-last-alignment typenums
  7902. org-table-last-column-widths lengths)
  7903. ;; With invisible characters, `format' does not get the field width right
  7904. ;; So we need to make these fields wide by hand.
  7905. (when (or links emph)
  7906. (loop for i from 0 upto (1- maxfields) do
  7907. (setq len (nth i lengths))
  7908. (loop for j from 0 upto (1- (length fields)) do
  7909. (setq c (nthcdr i (car (nthcdr j fields))))
  7910. (if (and (stringp (car c))
  7911. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7912. ; (string-match org-bracket-link-regexp (car c))
  7913. (< (org-string-width (car c)) len))
  7914. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7915. ;; Compute the formats needed for output of the table
  7916. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7917. (while (setq l (pop lengths))
  7918. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7919. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7920. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7921. (setq rfmt (concat rfmt "\n")
  7922. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7923. (setq new (mapconcat
  7924. (lambda (l)
  7925. (if l (apply 'format rfmt
  7926. (append (pop fields) emptystrings))
  7927. hfmt))
  7928. lines ""))
  7929. ;; Replace the old one
  7930. (delete-region beg end)
  7931. (move-marker end nil)
  7932. (move-marker org-table-aligned-begin-marker (point))
  7933. (insert new)
  7934. (move-marker org-table-aligned-end-marker (point))
  7935. (when (and orgtbl-mode (not (org-mode-p)))
  7936. (goto-char org-table-aligned-begin-marker)
  7937. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7938. ;; Try to move to the old location
  7939. (goto-line winstartline)
  7940. (setq winstart (point-at-bol))
  7941. (goto-line linepos)
  7942. (set-window-start (selected-window) winstart 'noforce)
  7943. (org-table-goto-column colpos)
  7944. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7945. (setq org-table-may-need-update nil)
  7946. ))
  7947. (defun org-string-width (s)
  7948. "Compute width of string, ignoring invisible characters.
  7949. This ignores character with invisibility property `org-link', and also
  7950. characters with property `org-cwidth', because these will become invisible
  7951. upon the next fontification round."
  7952. (let (b l)
  7953. (when (or (eq t buffer-invisibility-spec)
  7954. (assq 'org-link buffer-invisibility-spec))
  7955. (while (setq b (text-property-any 0 (length s)
  7956. 'invisible 'org-link s))
  7957. (setq s (concat (substring s 0 b)
  7958. (substring s (or (next-single-property-change
  7959. b 'invisible s) (length s)))))))
  7960. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7961. (setq s (concat (substring s 0 b)
  7962. (substring s (or (next-single-property-change
  7963. b 'org-cwidth s) (length s))))))
  7964. (setq l (string-width s) b -1)
  7965. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7966. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7967. l))
  7968. (defun org-table-begin (&optional table-type)
  7969. "Find the beginning of the table and return its position.
  7970. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7971. (save-excursion
  7972. (if (not (re-search-backward
  7973. (if table-type org-table-any-border-regexp
  7974. org-table-border-regexp)
  7975. nil t))
  7976. (progn (goto-char (point-min)) (point))
  7977. (goto-char (match-beginning 0))
  7978. (beginning-of-line 2)
  7979. (point))))
  7980. (defun org-table-end (&optional table-type)
  7981. "Find the end of the table and return its position.
  7982. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7983. (save-excursion
  7984. (if (not (re-search-forward
  7985. (if table-type org-table-any-border-regexp
  7986. org-table-border-regexp)
  7987. nil t))
  7988. (goto-char (point-max))
  7989. (goto-char (match-beginning 0)))
  7990. (point-marker)))
  7991. (defun org-table-justify-field-maybe (&optional new)
  7992. "Justify the current field, text to left, number to right.
  7993. Optional argument NEW may specify text to replace the current field content."
  7994. (cond
  7995. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7996. ((org-at-table-hline-p))
  7997. ((and (not new)
  7998. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7999. (current-buffer)))
  8000. (< (point) org-table-aligned-begin-marker)
  8001. (>= (point) org-table-aligned-end-marker)))
  8002. ;; This is not the same table, force a full re-align
  8003. (setq org-table-may-need-update t))
  8004. (t ;; realign the current field, based on previous full realign
  8005. (let* ((pos (point)) s
  8006. (col (org-table-current-column))
  8007. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  8008. l f n o e)
  8009. (when (> col 0)
  8010. (skip-chars-backward "^|\n")
  8011. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  8012. (progn
  8013. (setq s (match-string 1)
  8014. o (match-string 0)
  8015. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  8016. e (not (= (match-beginning 2) (match-end 2))))
  8017. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  8018. l (if e "|" (setq org-table-may-need-update t) ""))
  8019. n (format f s))
  8020. (if new
  8021. (if (<= (length new) l) ;; FIXME: length -> str-width?
  8022. (setq n (format f new))
  8023. (setq n (concat new "|") org-table-may-need-update t)))
  8024. (or (equal n o)
  8025. (let (org-table-may-need-update)
  8026. (replace-match n t t))))
  8027. (setq org-table-may-need-update t))
  8028. (goto-char pos))))))
  8029. (defun org-table-next-field ()
  8030. "Go to the next field in the current table, creating new lines as needed.
  8031. Before doing so, re-align the table if necessary."
  8032. (interactive)
  8033. (org-table-maybe-eval-formula)
  8034. (org-table-maybe-recalculate-line)
  8035. (if (and org-table-automatic-realign
  8036. org-table-may-need-update)
  8037. (org-table-align))
  8038. (let ((end (org-table-end)))
  8039. (if (org-at-table-hline-p)
  8040. (end-of-line 1))
  8041. (condition-case nil
  8042. (progn
  8043. (re-search-forward "|" end)
  8044. (if (looking-at "[ \t]*$")
  8045. (re-search-forward "|" end))
  8046. (if (and (looking-at "-")
  8047. org-table-tab-jumps-over-hlines
  8048. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8049. (goto-char (match-beginning 1)))
  8050. (if (looking-at "-")
  8051. (progn
  8052. (beginning-of-line 0)
  8053. (org-table-insert-row 'below))
  8054. (if (looking-at " ") (forward-char 1))))
  8055. (error
  8056. (org-table-insert-row 'below)))))
  8057. (defun org-table-previous-field ()
  8058. "Go to the previous field in the table.
  8059. Before doing so, re-align the table if necessary."
  8060. (interactive)
  8061. (org-table-justify-field-maybe)
  8062. (org-table-maybe-recalculate-line)
  8063. (if (and org-table-automatic-realign
  8064. org-table-may-need-update)
  8065. (org-table-align))
  8066. (if (org-at-table-hline-p)
  8067. (end-of-line 1))
  8068. (re-search-backward "|" (org-table-begin))
  8069. (re-search-backward "|" (org-table-begin))
  8070. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8071. (re-search-backward "|" (org-table-begin)))
  8072. (if (looking-at "| ?")
  8073. (goto-char (match-end 0))))
  8074. (defun org-table-next-row ()
  8075. "Go to the next row (same column) in the current table.
  8076. Before doing so, re-align the table if necessary."
  8077. (interactive)
  8078. (org-table-maybe-eval-formula)
  8079. (org-table-maybe-recalculate-line)
  8080. (if (or (looking-at "[ \t]*$")
  8081. (save-excursion (skip-chars-backward " \t") (bolp)))
  8082. (newline)
  8083. (if (and org-table-automatic-realign
  8084. org-table-may-need-update)
  8085. (org-table-align))
  8086. (let ((col (org-table-current-column)))
  8087. (beginning-of-line 2)
  8088. (if (or (not (org-at-table-p))
  8089. (org-at-table-hline-p))
  8090. (progn
  8091. (beginning-of-line 0)
  8092. (org-table-insert-row 'below)))
  8093. (org-table-goto-column col)
  8094. (skip-chars-backward "^|\n\r")
  8095. (if (looking-at " ") (forward-char 1)))))
  8096. (defun org-table-copy-down (n)
  8097. "Copy a field down in the current column.
  8098. If the field at the cursor is empty, copy into it the content of the nearest
  8099. non-empty field above. With argument N, use the Nth non-empty field.
  8100. If the current field is not empty, it is copied down to the next row, and
  8101. the cursor is moved with it. Therefore, repeating this command causes the
  8102. column to be filled row-by-row.
  8103. If the variable `org-table-copy-increment' is non-nil and the field is an
  8104. integer or a timestamp, it will be incremented while copying. In the case of
  8105. a timestamp, if the cursor is on the year, change the year. If it is on the
  8106. month or the day, change that. Point will stay on the current date field
  8107. in order to easily repeat the interval."
  8108. (interactive "p")
  8109. (let* ((colpos (org-table-current-column))
  8110. (col (current-column))
  8111. (field (org-table-get-field))
  8112. (non-empty (string-match "[^ \t]" field))
  8113. (beg (org-table-begin))
  8114. txt)
  8115. (org-table-check-inside-data-field)
  8116. (if non-empty
  8117. (progn
  8118. (setq txt (org-trim field))
  8119. (org-table-next-row)
  8120. (org-table-blank-field))
  8121. (save-excursion
  8122. (setq txt
  8123. (catch 'exit
  8124. (while (progn (beginning-of-line 1)
  8125. (re-search-backward org-table-dataline-regexp
  8126. beg t))
  8127. (org-table-goto-column colpos t)
  8128. (if (and (looking-at
  8129. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8130. (= (setq n (1- n)) 0))
  8131. (throw 'exit (match-string 1))))))))
  8132. (if txt
  8133. (progn
  8134. (if (and org-table-copy-increment
  8135. (string-match "^[0-9]+$" txt))
  8136. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8137. (insert txt)
  8138. (move-to-column col)
  8139. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8140. (org-timestamp-up 1)
  8141. (org-table-maybe-recalculate-line))
  8142. (org-table-align)
  8143. (move-to-column col))
  8144. (error "No non-empty field found"))))
  8145. (defun org-table-check-inside-data-field ()
  8146. "Is point inside a table data field?
  8147. I.e. not on a hline or before the first or after the last column?
  8148. This actually throws an error, so it aborts the current command."
  8149. (if (or (not (org-at-table-p))
  8150. (= (org-table-current-column) 0)
  8151. (org-at-table-hline-p)
  8152. (looking-at "[ \t]*$"))
  8153. (error "Not in table data field")))
  8154. (defvar org-table-clip nil
  8155. "Clipboard for table regions.")
  8156. (defun org-table-blank-field ()
  8157. "Blank the current table field or active region."
  8158. (interactive)
  8159. (org-table-check-inside-data-field)
  8160. (if (and (interactive-p) (org-region-active-p))
  8161. (let (org-table-clip)
  8162. (org-table-cut-region (region-beginning) (region-end)))
  8163. (skip-chars-backward "^|")
  8164. (backward-char 1)
  8165. (if (looking-at "|[^|\n]+")
  8166. (let* ((pos (match-beginning 0))
  8167. (match (match-string 0))
  8168. (len (org-string-width match)))
  8169. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8170. (goto-char (+ 2 pos))
  8171. (substring match 1)))))
  8172. (defun org-table-get-field (&optional n replace)
  8173. "Return the value of the field in column N of current row.
  8174. N defaults to current field.
  8175. If REPLACE is a string, replace field with this value. The return value
  8176. is always the old value."
  8177. (and n (org-table-goto-column n))
  8178. (skip-chars-backward "^|\n")
  8179. (backward-char 1)
  8180. (if (looking-at "|[^|\r\n]*")
  8181. (let* ((pos (match-beginning 0))
  8182. (val (buffer-substring (1+ pos) (match-end 0))))
  8183. (if replace
  8184. (replace-match (concat "|" replace) t t))
  8185. (goto-char (min (point-at-eol) (+ 2 pos)))
  8186. val)
  8187. (forward-char 1) ""))
  8188. (defun org-table-field-info (arg)
  8189. "Show info about the current field, and highlight any reference at point."
  8190. (interactive "P")
  8191. (org-table-get-specials)
  8192. (save-excursion
  8193. (let* ((pos (point))
  8194. (col (org-table-current-column))
  8195. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8196. (name (car (rassoc (list (org-current-line) col)
  8197. org-table-named-field-locations)))
  8198. (eql (org-table-get-stored-formulas))
  8199. (dline (org-table-current-dline))
  8200. (ref (format "@%d$%d" dline col))
  8201. (ref1 (org-table-convert-refs-to-an ref))
  8202. (fequation (or (assoc name eql) (assoc ref eql)))
  8203. (cequation (assoc (int-to-string col) eql))
  8204. (eqn (or fequation cequation)))
  8205. (goto-char pos)
  8206. (condition-case nil
  8207. (org-table-show-reference 'local)
  8208. (error nil))
  8209. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8210. dline col
  8211. (if cname (concat " or $" cname) "")
  8212. dline col ref1
  8213. (if name (concat " or $" name) "")
  8214. ;; FIXME: formula info not correct if special table line
  8215. (if eqn
  8216. (concat ", formula: "
  8217. (org-table-formula-to-user
  8218. (concat
  8219. (if (string-match "^[$@]"(car eqn)) "" "$")
  8220. (car eqn) "=" (cdr eqn))))
  8221. "")))))
  8222. (defun org-table-current-column ()
  8223. "Find out which column we are in.
  8224. When called interactively, column is also displayed in echo area."
  8225. (interactive)
  8226. (if (interactive-p) (org-table-check-inside-data-field))
  8227. (save-excursion
  8228. (let ((cnt 0) (pos (point)))
  8229. (beginning-of-line 1)
  8230. (while (search-forward "|" pos t)
  8231. (setq cnt (1+ cnt)))
  8232. (if (interactive-p) (message "This is table column %d" cnt))
  8233. cnt)))
  8234. (defun org-table-current-dline ()
  8235. "Find out what table data line we are in.
  8236. Only datalins count for this."
  8237. (interactive)
  8238. (if (interactive-p) (org-table-check-inside-data-field))
  8239. (save-excursion
  8240. (let ((cnt 0) (pos (point)))
  8241. (goto-char (org-table-begin))
  8242. (while (<= (point) pos)
  8243. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8244. (beginning-of-line 2))
  8245. (if (interactive-p) (message "This is table line %d" cnt))
  8246. cnt)))
  8247. (defun org-table-goto-column (n &optional on-delim force)
  8248. "Move the cursor to the Nth column in the current table line.
  8249. With optional argument ON-DELIM, stop with point before the left delimiter
  8250. of the field.
  8251. If there are less than N fields, just go to after the last delimiter.
  8252. However, when FORCE is non-nil, create new columns if necessary."
  8253. (interactive "p")
  8254. (let ((pos (point-at-eol)))
  8255. (beginning-of-line 1)
  8256. (when (> n 0)
  8257. (while (and (> (setq n (1- n)) -1)
  8258. (or (search-forward "|" pos t)
  8259. (and force
  8260. (progn (end-of-line 1)
  8261. (skip-chars-backward "^|")
  8262. (insert " | "))))))
  8263. ; (backward-char 2) t)))))
  8264. (when (and force (not (looking-at ".*|")))
  8265. (save-excursion (end-of-line 1) (insert " | ")))
  8266. (if on-delim
  8267. (backward-char 1)
  8268. (if (looking-at " ") (forward-char 1))))))
  8269. (defun org-at-table-p (&optional table-type)
  8270. "Return t if the cursor is inside an org-type table.
  8271. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8272. (if org-enable-table-editor
  8273. (save-excursion
  8274. (beginning-of-line 1)
  8275. (looking-at (if table-type org-table-any-line-regexp
  8276. org-table-line-regexp)))
  8277. nil))
  8278. (defun org-at-table.el-p ()
  8279. "Return t if and only if we are at a table.el table."
  8280. (and (org-at-table-p 'any)
  8281. (save-excursion
  8282. (goto-char (org-table-begin 'any))
  8283. (looking-at org-table1-hline-regexp))))
  8284. (defun org-table-recognize-table.el ()
  8285. "If there is a table.el table nearby, recognize it and move into it."
  8286. (if org-table-tab-recognizes-table.el
  8287. (if (org-at-table.el-p)
  8288. (progn
  8289. (beginning-of-line 1)
  8290. (if (looking-at org-table-dataline-regexp)
  8291. nil
  8292. (if (looking-at org-table1-hline-regexp)
  8293. (progn
  8294. (beginning-of-line 2)
  8295. (if (looking-at org-table-any-border-regexp)
  8296. (beginning-of-line -1)))))
  8297. (if (re-search-forward "|" (org-table-end t) t)
  8298. (progn
  8299. (require 'table)
  8300. (if (table--at-cell-p (point))
  8301. t
  8302. (message "recognizing table.el table...")
  8303. (table-recognize-table)
  8304. (message "recognizing table.el table...done")))
  8305. (error "This should not happen..."))
  8306. t)
  8307. nil)
  8308. nil))
  8309. (defun org-at-table-hline-p ()
  8310. "Return t if the cursor is inside a hline in a table."
  8311. (if org-enable-table-editor
  8312. (save-excursion
  8313. (beginning-of-line 1)
  8314. (looking-at org-table-hline-regexp))
  8315. nil))
  8316. (defun org-table-insert-column ()
  8317. "Insert a new column into the table."
  8318. (interactive)
  8319. (if (not (org-at-table-p))
  8320. (error "Not at a table"))
  8321. (org-table-find-dataline)
  8322. (let* ((col (max 1 (org-table-current-column)))
  8323. (beg (org-table-begin))
  8324. (end (org-table-end))
  8325. ;; Current cursor position
  8326. (linepos (org-current-line))
  8327. (colpos col))
  8328. (goto-char beg)
  8329. (while (< (point) end)
  8330. (if (org-at-table-hline-p)
  8331. nil
  8332. (org-table-goto-column col t)
  8333. (insert "| "))
  8334. (beginning-of-line 2))
  8335. (move-marker end nil)
  8336. (goto-line linepos)
  8337. (org-table-goto-column colpos)
  8338. (org-table-align)
  8339. (org-table-fix-formulas "$" nil (1- col) 1)))
  8340. (defun org-table-find-dataline ()
  8341. "Find a dataline in the current table, which is needed for column commands."
  8342. (if (and (org-at-table-p)
  8343. (not (org-at-table-hline-p)))
  8344. t
  8345. (let ((col (current-column))
  8346. (end (org-table-end)))
  8347. (move-to-column col)
  8348. (while (and (< (point) end)
  8349. (or (not (= (current-column) col))
  8350. (org-at-table-hline-p)))
  8351. (beginning-of-line 2)
  8352. (move-to-column col))
  8353. (if (and (org-at-table-p)
  8354. (not (org-at-table-hline-p)))
  8355. t
  8356. (error
  8357. "Please position cursor in a data line for column operations")))))
  8358. (defun org-table-delete-column ()
  8359. "Delete a column from the table."
  8360. (interactive)
  8361. (if (not (org-at-table-p))
  8362. (error "Not at a table"))
  8363. (org-table-find-dataline)
  8364. (org-table-check-inside-data-field)
  8365. (let* ((col (org-table-current-column))
  8366. (beg (org-table-begin))
  8367. (end (org-table-end))
  8368. ;; Current cursor position
  8369. (linepos (org-current-line))
  8370. (colpos col))
  8371. (goto-char beg)
  8372. (while (< (point) end)
  8373. (if (org-at-table-hline-p)
  8374. nil
  8375. (org-table-goto-column col t)
  8376. (and (looking-at "|[^|\n]+|")
  8377. (replace-match "|")))
  8378. (beginning-of-line 2))
  8379. (move-marker end nil)
  8380. (goto-line linepos)
  8381. (org-table-goto-column colpos)
  8382. (org-table-align)
  8383. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8384. col -1 col)))
  8385. (defun org-table-move-column-right ()
  8386. "Move column to the right."
  8387. (interactive)
  8388. (org-table-move-column nil))
  8389. (defun org-table-move-column-left ()
  8390. "Move column to the left."
  8391. (interactive)
  8392. (org-table-move-column 'left))
  8393. (defun org-table-move-column (&optional left)
  8394. "Move the current column to the right. With arg LEFT, move to the left."
  8395. (interactive "P")
  8396. (if (not (org-at-table-p))
  8397. (error "Not at a table"))
  8398. (org-table-find-dataline)
  8399. (org-table-check-inside-data-field)
  8400. (let* ((col (org-table-current-column))
  8401. (col1 (if left (1- col) col))
  8402. (beg (org-table-begin))
  8403. (end (org-table-end))
  8404. ;; Current cursor position
  8405. (linepos (org-current-line))
  8406. (colpos (if left (1- col) (1+ col))))
  8407. (if (and left (= col 1))
  8408. (error "Cannot move column further left"))
  8409. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8410. (error "Cannot move column further right"))
  8411. (goto-char beg)
  8412. (while (< (point) end)
  8413. (if (org-at-table-hline-p)
  8414. nil
  8415. (org-table-goto-column col1 t)
  8416. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8417. (replace-match "|\\2|\\1|")))
  8418. (beginning-of-line 2))
  8419. (move-marker end nil)
  8420. (goto-line linepos)
  8421. (org-table-goto-column colpos)
  8422. (org-table-align)
  8423. (org-table-fix-formulas
  8424. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8425. (cons (number-to-string colpos) (number-to-string col))))))
  8426. (defun org-table-move-row-down ()
  8427. "Move table row down."
  8428. (interactive)
  8429. (org-table-move-row nil))
  8430. (defun org-table-move-row-up ()
  8431. "Move table row up."
  8432. (interactive)
  8433. (org-table-move-row 'up))
  8434. (defun org-table-move-row (&optional up)
  8435. "Move the current table line down. With arg UP, move it up."
  8436. (interactive "P")
  8437. (let* ((col (current-column))
  8438. (pos (point))
  8439. (hline1p (save-excursion (beginning-of-line 1)
  8440. (looking-at org-table-hline-regexp)))
  8441. (dline1 (org-table-current-dline))
  8442. (dline2 (+ dline1 (if up -1 1)))
  8443. (tonew (if up 0 2))
  8444. txt hline2p)
  8445. (beginning-of-line tonew)
  8446. (unless (org-at-table-p)
  8447. (goto-char pos)
  8448. (error "Cannot move row further"))
  8449. (setq hline2p (looking-at org-table-hline-regexp))
  8450. (goto-char pos)
  8451. (beginning-of-line 1)
  8452. (setq pos (point))
  8453. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8454. (delete-region (point) (1+ (point-at-eol)))
  8455. (beginning-of-line tonew)
  8456. (insert txt)
  8457. (beginning-of-line 0)
  8458. (move-to-column col)
  8459. (unless (or hline1p hline2p)
  8460. (org-table-fix-formulas
  8461. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8462. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8463. (defun org-table-insert-row (&optional arg)
  8464. "Insert a new row above the current line into the table.
  8465. With prefix ARG, insert below the current line."
  8466. (interactive "P")
  8467. (if (not (org-at-table-p))
  8468. (error "Not at a table"))
  8469. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8470. (new (org-table-clean-line line)))
  8471. ;; Fix the first field if necessary
  8472. (if (string-match "^[ \t]*| *[#$] *|" line)
  8473. (setq new (replace-match (match-string 0 line) t t new)))
  8474. (beginning-of-line (if arg 2 1))
  8475. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8476. (beginning-of-line 0)
  8477. (re-search-forward "| ?" (point-at-eol) t)
  8478. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8479. (org-table-align))
  8480. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8481. (defun org-table-insert-hline (&optional above)
  8482. "Insert a horizontal-line below the current line into the table.
  8483. With prefix ABOVE, insert above the current line."
  8484. (interactive "P")
  8485. (if (not (org-at-table-p))
  8486. (error "Not at a table"))
  8487. (let ((line (org-table-clean-line
  8488. (buffer-substring (point-at-bol) (point-at-eol))))
  8489. (col (current-column)))
  8490. (while (string-match "|\\( +\\)|" line)
  8491. (setq line (replace-match
  8492. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8493. ?-) "|") t t line)))
  8494. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8495. (beginning-of-line (if above 1 2))
  8496. (insert line "\n")
  8497. (beginning-of-line (if above 1 -1))
  8498. (move-to-column col)
  8499. (and org-table-overlay-coordinates (org-table-align))))
  8500. (defun org-table-hline-and-move (&optional same-column)
  8501. "Insert a hline and move to the row below that line."
  8502. (interactive "P")
  8503. (let ((col (org-table-current-column)))
  8504. (org-table-maybe-eval-formula)
  8505. (org-table-maybe-recalculate-line)
  8506. (org-table-insert-hline)
  8507. (end-of-line 2)
  8508. (if (looking-at "\n[ \t]*|-")
  8509. (progn (insert "\n|") (org-table-align))
  8510. (org-table-next-field))
  8511. (if same-column (org-table-goto-column col))))
  8512. (defun org-table-clean-line (s)
  8513. "Convert a table line S into a string with only \"|\" and space.
  8514. In particular, this does handle wide and invisible characters."
  8515. (if (string-match "^[ \t]*|-" s)
  8516. ;; It's a hline, just map the characters
  8517. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8518. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8519. (setq s (replace-match
  8520. (concat "|" (make-string (org-string-width (match-string 1 s))
  8521. ?\ ) "|")
  8522. t t s)))
  8523. s))
  8524. (defun org-table-kill-row ()
  8525. "Delete the current row or horizontal line from the table."
  8526. (interactive)
  8527. (if (not (org-at-table-p))
  8528. (error "Not at a table"))
  8529. (let ((col (current-column))
  8530. (dline (org-table-current-dline)))
  8531. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8532. (if (not (org-at-table-p)) (beginning-of-line 0))
  8533. (move-to-column col)
  8534. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8535. dline -1 dline)))
  8536. (defun org-table-sort-lines (with-case &optional sorting-type)
  8537. "Sort table lines according to the column at point.
  8538. The position of point indicates the column to be used for
  8539. sorting, and the range of lines is the range between the nearest
  8540. horizontal separator lines, or the entire table of no such lines
  8541. exist. If point is before the first column, you will be prompted
  8542. for the sorting column. If there is an active region, the mark
  8543. specifies the first line and the sorting column, while point
  8544. should be in the last line to be included into the sorting.
  8545. The command then prompts for the sorting type which can be
  8546. alphabetically, numerically, or by time (as given in a time stamp
  8547. in the field). Sorting in reverse order is also possible.
  8548. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8549. If SORTING-TYPE is specified when this function is called from a Lisp
  8550. program, no prompting will take place. SORTING-TYPE must be a character,
  8551. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8552. should be done in reverse order."
  8553. (interactive "P")
  8554. (let* ((thisline (org-current-line))
  8555. (thiscol (org-table-current-column))
  8556. beg end bcol ecol tend tbeg column lns pos)
  8557. (when (equal thiscol 0)
  8558. (if (interactive-p)
  8559. (setq thiscol
  8560. (string-to-number
  8561. (read-string "Use column N for sorting: ")))
  8562. (setq thiscol 1))
  8563. (org-table-goto-column thiscol))
  8564. (org-table-check-inside-data-field)
  8565. (if (org-region-active-p)
  8566. (progn
  8567. (setq beg (region-beginning) end (region-end))
  8568. (goto-char beg)
  8569. (setq column (org-table-current-column)
  8570. beg (point-at-bol))
  8571. (goto-char end)
  8572. (setq end (point-at-bol 2)))
  8573. (setq column (org-table-current-column)
  8574. pos (point)
  8575. tbeg (org-table-begin)
  8576. tend (org-table-end))
  8577. (if (re-search-backward org-table-hline-regexp tbeg t)
  8578. (setq beg (point-at-bol 2))
  8579. (goto-char tbeg)
  8580. (setq beg (point-at-bol 1)))
  8581. (goto-char pos)
  8582. (if (re-search-forward org-table-hline-regexp tend t)
  8583. (setq end (point-at-bol 1))
  8584. (goto-char tend)
  8585. (setq end (point-at-bol))))
  8586. (setq beg (move-marker (make-marker) beg)
  8587. end (move-marker (make-marker) end))
  8588. (untabify beg end)
  8589. (goto-char beg)
  8590. (org-table-goto-column column)
  8591. (skip-chars-backward "^|")
  8592. (setq bcol (current-column))
  8593. (org-table-goto-column (1+ column))
  8594. (skip-chars-backward "^|")
  8595. (setq ecol (1- (current-column)))
  8596. (org-table-goto-column column)
  8597. (setq lns (mapcar (lambda(x) (cons
  8598. (org-sort-remove-invisible
  8599. (nth (1- column)
  8600. (org-split-string x "[ \t]*|[ \t]*")))
  8601. x))
  8602. (org-split-string (buffer-substring beg end) "\n")))
  8603. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8604. (delete-region beg end)
  8605. (move-marker beg nil)
  8606. (move-marker end nil)
  8607. (insert (mapconcat 'cdr lns "\n") "\n")
  8608. (goto-line thisline)
  8609. (org-table-goto-column thiscol)
  8610. (message "%d lines sorted, based on column %d" (length lns) column)))
  8611. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8612. (defun org-sort-remove-invisible (s)
  8613. (remove-text-properties 0 (length s) org-rm-props s)
  8614. (while (string-match org-bracket-link-regexp s)
  8615. (setq s (replace-match (if (match-end 2)
  8616. (match-string 3 s)
  8617. (match-string 1 s)) t t s)))
  8618. s)
  8619. (defun org-table-cut-region (beg end)
  8620. "Copy region in table to the clipboard and blank all relevant fields."
  8621. (interactive "r")
  8622. (org-table-copy-region beg end 'cut))
  8623. (defun org-table-copy-region (beg end &optional cut)
  8624. "Copy rectangular region in table to clipboard.
  8625. A special clipboard is used which can only be accessed
  8626. with `org-table-paste-rectangle'."
  8627. (interactive "rP")
  8628. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8629. region cols
  8630. (rpl (if cut " " nil)))
  8631. (goto-char beg)
  8632. (org-table-check-inside-data-field)
  8633. (setq l01 (org-current-line)
  8634. c01 (org-table-current-column))
  8635. (goto-char end)
  8636. (org-table-check-inside-data-field)
  8637. (setq l02 (org-current-line)
  8638. c02 (org-table-current-column))
  8639. (setq l1 (min l01 l02) l2 (max l01 l02)
  8640. c1 (min c01 c02) c2 (max c01 c02))
  8641. (catch 'exit
  8642. (while t
  8643. (catch 'nextline
  8644. (if (> l1 l2) (throw 'exit t))
  8645. (goto-line l1)
  8646. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8647. (setq cols nil ic1 c1 ic2 c2)
  8648. (while (< ic1 (1+ ic2))
  8649. (push (org-table-get-field ic1 rpl) cols)
  8650. (setq ic1 (1+ ic1)))
  8651. (push (nreverse cols) region)
  8652. (setq l1 (1+ l1)))))
  8653. (setq org-table-clip (nreverse region))
  8654. (if cut (org-table-align))
  8655. org-table-clip))
  8656. (defun org-table-paste-rectangle ()
  8657. "Paste a rectangular region into a table.
  8658. The upper right corner ends up in the current field. All involved fields
  8659. will be overwritten. If the rectangle does not fit into the present table,
  8660. the table is enlarged as needed. The process ignores horizontal separator
  8661. lines."
  8662. (interactive)
  8663. (unless (and org-table-clip (listp org-table-clip))
  8664. (error "First cut/copy a region to paste!"))
  8665. (org-table-check-inside-data-field)
  8666. (let* ((clip org-table-clip)
  8667. (line (org-current-line))
  8668. (col (org-table-current-column))
  8669. (org-enable-table-editor t)
  8670. (org-table-automatic-realign nil)
  8671. c cols field)
  8672. (while (setq cols (pop clip))
  8673. (while (org-at-table-hline-p) (beginning-of-line 2))
  8674. (if (not (org-at-table-p))
  8675. (progn (end-of-line 0) (org-table-next-field)))
  8676. (setq c col)
  8677. (while (setq field (pop cols))
  8678. (org-table-goto-column c nil 'force)
  8679. (org-table-get-field nil field)
  8680. (setq c (1+ c)))
  8681. (beginning-of-line 2))
  8682. (goto-line line)
  8683. (org-table-goto-column col)
  8684. (org-table-align)))
  8685. (defun org-table-convert ()
  8686. "Convert from `org-mode' table to table.el and back.
  8687. Obviously, this only works within limits. When an Org-mode table is
  8688. converted to table.el, all horizontal separator lines get lost, because
  8689. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8690. A table.el table can be converted to an Org-mode table only if it does not
  8691. do row or column spanning. Multiline cells will become multiple cells.
  8692. Beware, Org-mode does not test if the table can be successfully converted - it
  8693. blindly applies a recipe that works for simple tables."
  8694. (interactive)
  8695. (require 'table)
  8696. (if (org-at-table.el-p)
  8697. ;; convert to Org-mode table
  8698. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8699. (end (move-marker (make-marker) (org-table-end t))))
  8700. (table-unrecognize-region beg end)
  8701. (goto-char beg)
  8702. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8703. (replace-match ""))
  8704. (goto-char beg))
  8705. (if (org-at-table-p)
  8706. ;; convert to table.el table
  8707. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8708. (end (move-marker (make-marker) (org-table-end))))
  8709. ;; first, get rid of all horizontal lines
  8710. (goto-char beg)
  8711. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8712. (replace-match ""))
  8713. ;; insert a hline before first
  8714. (goto-char beg)
  8715. (org-table-insert-hline 'above)
  8716. (beginning-of-line -1)
  8717. ;; insert a hline after each line
  8718. (while (progn (beginning-of-line 3) (< (point) end))
  8719. (org-table-insert-hline))
  8720. (goto-char beg)
  8721. (setq end (move-marker end (org-table-end)))
  8722. ;; replace "+" at beginning and ending of hlines
  8723. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8724. (replace-match "\\1+-"))
  8725. (goto-char beg)
  8726. (while (re-search-forward "-|[ \t]*$" end t)
  8727. (replace-match "-+"))
  8728. (goto-char beg)))))
  8729. (defun org-table-wrap-region (arg)
  8730. "Wrap several fields in a column like a paragraph.
  8731. This is useful if you'd like to spread the contents of a field over several
  8732. lines, in order to keep the table compact.
  8733. If there is an active region, and both point and mark are in the same column,
  8734. the text in the column is wrapped to minimum width for the given number of
  8735. lines. Generally, this makes the table more compact. A prefix ARG may be
  8736. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8737. formats the selected text to two lines. If the region was longer than two
  8738. lines, the remaining lines remain empty. A negative prefix argument reduces
  8739. the current number of lines by that amount. The wrapped text is pasted back
  8740. into the table. If you formatted it to more lines than it was before, fields
  8741. further down in the table get overwritten - so you might need to make space in
  8742. the table first.
  8743. If there is no region, the current field is split at the cursor position and
  8744. the text fragment to the right of the cursor is prepended to the field one
  8745. line down.
  8746. If there is no region, but you specify a prefix ARG, the current field gets
  8747. blank, and the content is appended to the field above."
  8748. (interactive "P")
  8749. (org-table-check-inside-data-field)
  8750. (if (org-region-active-p)
  8751. ;; There is a region: fill as a paragraph
  8752. (let* ((beg (region-beginning))
  8753. (cline (save-excursion (goto-char beg) (org-current-line)))
  8754. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8755. nlines)
  8756. (org-table-cut-region (region-beginning) (region-end))
  8757. (if (> (length (car org-table-clip)) 1)
  8758. (error "Region must be limited to single column"))
  8759. (setq nlines (if arg
  8760. (if (< arg 1)
  8761. (+ (length org-table-clip) arg)
  8762. arg)
  8763. (length org-table-clip)))
  8764. (setq org-table-clip
  8765. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8766. nil nlines)))
  8767. (goto-line cline)
  8768. (org-table-goto-column ccol)
  8769. (org-table-paste-rectangle))
  8770. ;; No region, split the current field at point
  8771. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8772. (skip-chars-forward "^\r\n|"))
  8773. (if arg
  8774. ;; combine with field above
  8775. (let ((s (org-table-blank-field))
  8776. (col (org-table-current-column)))
  8777. (beginning-of-line 0)
  8778. (while (org-at-table-hline-p) (beginning-of-line 0))
  8779. (org-table-goto-column col)
  8780. (skip-chars-forward "^|")
  8781. (skip-chars-backward " ")
  8782. (insert " " (org-trim s))
  8783. (org-table-align))
  8784. ;; split field
  8785. (if (looking-at "\\([^|]+\\)+|")
  8786. (let ((s (match-string 1)))
  8787. (replace-match " |")
  8788. (goto-char (match-beginning 0))
  8789. (org-table-next-row)
  8790. (insert (org-trim s) " ")
  8791. (org-table-align))
  8792. (org-table-next-row)))))
  8793. (defvar org-field-marker nil)
  8794. (defun org-table-edit-field (arg)
  8795. "Edit table field in a different window.
  8796. This is mainly useful for fields that contain hidden parts.
  8797. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8798. it can be edited in place."
  8799. (interactive "P")
  8800. (if arg
  8801. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8802. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8803. (remove-text-properties b e '(org-cwidth t invisible t
  8804. display t intangible t))
  8805. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8806. (font-lock-fontify-block)))
  8807. (let ((pos (move-marker (make-marker) (point)))
  8808. (field (org-table-get-field))
  8809. (cw (current-window-configuration))
  8810. p)
  8811. (org-switch-to-buffer-other-window "*Org tmp*")
  8812. (erase-buffer)
  8813. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8814. (let ((org-inhibit-startup t)) (org-mode))
  8815. (goto-char (setq p (point-max)))
  8816. (insert (org-trim field))
  8817. (remove-text-properties p (point-max)
  8818. '(invisible t org-cwidth t display t
  8819. intangible t))
  8820. (goto-char p)
  8821. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8822. (org-set-local 'org-window-configuration cw)
  8823. (org-set-local 'org-field-marker pos)
  8824. (message "Edit and finish with C-c C-c"))))
  8825. (defun org-table-finish-edit-field ()
  8826. "Finish editing a table data field.
  8827. Remove all newline characters, insert the result into the table, realign
  8828. the table and kill the editing buffer."
  8829. (let ((pos org-field-marker)
  8830. (cw org-window-configuration)
  8831. (cb (current-buffer))
  8832. text)
  8833. (goto-char (point-min))
  8834. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8835. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8836. (replace-match " "))
  8837. (setq text (org-trim (buffer-string)))
  8838. (set-window-configuration cw)
  8839. (kill-buffer cb)
  8840. (select-window (get-buffer-window (marker-buffer pos)))
  8841. (goto-char pos)
  8842. (move-marker pos nil)
  8843. (org-table-check-inside-data-field)
  8844. (org-table-get-field nil text)
  8845. (org-table-align)
  8846. (message "New field value inserted")))
  8847. (defun org-trim (s)
  8848. "Remove whitespace at beginning and end of string."
  8849. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8850. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8851. s)
  8852. (defun org-wrap (string &optional width lines)
  8853. "Wrap string to either a number of lines, or a width in characters.
  8854. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8855. that costs. If there is a word longer than WIDTH, the text is actually
  8856. wrapped to the length of that word.
  8857. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8858. many lines, whatever width that takes.
  8859. The return value is a list of lines, without newlines at the end."
  8860. (let* ((words (org-split-string string "[ \t\n]+"))
  8861. (maxword (apply 'max (mapcar 'org-string-width words)))
  8862. w ll)
  8863. (cond (width
  8864. (org-do-wrap words (max maxword width)))
  8865. (lines
  8866. (setq w maxword)
  8867. (setq ll (org-do-wrap words maxword))
  8868. (if (<= (length ll) lines)
  8869. ll
  8870. (setq ll words)
  8871. (while (> (length ll) lines)
  8872. (setq w (1+ w))
  8873. (setq ll (org-do-wrap words w)))
  8874. ll))
  8875. (t (error "Cannot wrap this")))))
  8876. (defun org-do-wrap (words width)
  8877. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8878. (let (lines line)
  8879. (while words
  8880. (setq line (pop words))
  8881. (while (and words (< (+ (length line) (length (car words))) width))
  8882. (setq line (concat line " " (pop words))))
  8883. (setq lines (push line lines)))
  8884. (nreverse lines)))
  8885. (defun org-split-string (string &optional separators)
  8886. "Splits STRING into substrings at SEPARATORS.
  8887. No empty strings are returned if there are matches at the beginning
  8888. and end of string."
  8889. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8890. (start 0)
  8891. notfirst
  8892. (list nil))
  8893. (while (and (string-match rexp string
  8894. (if (and notfirst
  8895. (= start (match-beginning 0))
  8896. (< start (length string)))
  8897. (1+ start) start))
  8898. (< (match-beginning 0) (length string)))
  8899. (setq notfirst t)
  8900. (or (eq (match-beginning 0) 0)
  8901. (and (eq (match-beginning 0) (match-end 0))
  8902. (eq (match-beginning 0) start))
  8903. (setq list
  8904. (cons (substring string start (match-beginning 0))
  8905. list)))
  8906. (setq start (match-end 0)))
  8907. (or (eq start (length string))
  8908. (setq list
  8909. (cons (substring string start)
  8910. list)))
  8911. (nreverse list)))
  8912. (defun org-table-map-tables (function)
  8913. "Apply FUNCTION to the start of all tables in the buffer."
  8914. (save-excursion
  8915. (save-restriction
  8916. (widen)
  8917. (goto-char (point-min))
  8918. (while (re-search-forward org-table-any-line-regexp nil t)
  8919. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8920. (beginning-of-line 1)
  8921. (if (looking-at org-table-line-regexp)
  8922. (save-excursion (funcall function)))
  8923. (re-search-forward org-table-any-border-regexp nil 1))))
  8924. (message "Mapping tables: done"))
  8925. (defvar org-timecnt) ; dynamically scoped parameter
  8926. (defun org-table-sum (&optional beg end nlast)
  8927. "Sum numbers in region of current table column.
  8928. The result will be displayed in the echo area, and will be available
  8929. as kill to be inserted with \\[yank].
  8930. If there is an active region, it is interpreted as a rectangle and all
  8931. numbers in that rectangle will be summed. If there is no active
  8932. region and point is located in a table column, sum all numbers in that
  8933. column.
  8934. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8935. numbers are assumed to be times as well (in decimal hours) and the
  8936. numbers are added as such.
  8937. If NLAST is a number, only the NLAST fields will actually be summed."
  8938. (interactive)
  8939. (save-excursion
  8940. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8941. (cond
  8942. ((and beg end)) ; beg and end given explicitly
  8943. ((org-region-active-p)
  8944. (setq beg (region-beginning) end (region-end)))
  8945. (t
  8946. (setq col (org-table-current-column))
  8947. (goto-char (org-table-begin))
  8948. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8949. (error "No table data"))
  8950. (org-table-goto-column col)
  8951. (setq beg (point))
  8952. (goto-char (org-table-end))
  8953. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8954. (error "No table data"))
  8955. (org-table-goto-column col)
  8956. (setq end (point))))
  8957. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8958. (items1 (cond ((not nlast) items)
  8959. ((>= nlast (length items)) items)
  8960. (t (setq items (reverse items))
  8961. (setcdr (nthcdr (1- nlast) items) nil)
  8962. (nreverse items))))
  8963. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8964. items1)))
  8965. (res (apply '+ numbers))
  8966. (sres (if (= org-timecnt 0)
  8967. (format "%g" res)
  8968. (setq diff (* 3600 res)
  8969. h (floor (/ diff 3600)) diff (mod diff 3600)
  8970. m (floor (/ diff 60)) diff (mod diff 60)
  8971. s diff)
  8972. (format "%d:%02d:%02d" h m s))))
  8973. (kill-new sres)
  8974. (if (interactive-p)
  8975. (message "%s"
  8976. (substitute-command-keys
  8977. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8978. (length numbers) sres))))
  8979. sres))))
  8980. (defun org-table-get-number-for-summing (s)
  8981. (let (n)
  8982. (if (string-match "^ *|? *" s)
  8983. (setq s (replace-match "" nil nil s)))
  8984. (if (string-match " *|? *$" s)
  8985. (setq s (replace-match "" nil nil s)))
  8986. (setq n (string-to-number s))
  8987. (cond
  8988. ((and (string-match "0" s)
  8989. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8990. ((string-match "\\`[ \t]+\\'" s) nil)
  8991. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8992. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8993. (m (string-to-number (or (match-string 2 s) "0")))
  8994. (s (string-to-number (or (match-string 4 s) "0"))))
  8995. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8996. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8997. ((equal n 0) nil)
  8998. (t n))))
  8999. (defun org-table-current-field-formula (&optional key noerror)
  9000. "Return the formula active for the current field.
  9001. Assumes that specials are in place.
  9002. If KEY is given, return the key to this formula.
  9003. Otherwise return the formula preceeded with \"=\" or \":=\"."
  9004. (let* ((name (car (rassoc (list (org-current-line)
  9005. (org-table-current-column))
  9006. org-table-named-field-locations)))
  9007. (col (org-table-current-column))
  9008. (scol (int-to-string col))
  9009. (ref (format "@%d$%d" (org-table-current-dline) col))
  9010. (stored-list (org-table-get-stored-formulas noerror))
  9011. (ass (or (assoc name stored-list)
  9012. (assoc ref stored-list)
  9013. (assoc scol stored-list))))
  9014. (if key
  9015. (car ass)
  9016. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  9017. (cdr ass))))))
  9018. (defun org-table-get-formula (&optional equation named)
  9019. "Read a formula from the minibuffer, offer stored formula as default.
  9020. When NAMED is non-nil, look for a named equation."
  9021. (let* ((stored-list (org-table-get-stored-formulas))
  9022. (name (car (rassoc (list (org-current-line)
  9023. (org-table-current-column))
  9024. org-table-named-field-locations)))
  9025. (ref (format "@%d$%d" (org-table-current-dline)
  9026. (org-table-current-column)))
  9027. (refass (assoc ref stored-list))
  9028. (scol (if named
  9029. (if name name ref)
  9030. (int-to-string (org-table-current-column))))
  9031. (dummy (and (or name refass) (not named)
  9032. (not (y-or-n-p "Replace field formula with column formula? " ))
  9033. (error "Abort")))
  9034. (name (or name ref))
  9035. (org-table-may-need-update nil)
  9036. (stored (cdr (assoc scol stored-list)))
  9037. (eq (cond
  9038. ((and stored equation (string-match "^ *=? *$" equation))
  9039. stored)
  9040. ((stringp equation)
  9041. equation)
  9042. (t (org-table-formula-from-user
  9043. (read-string
  9044. (org-table-formula-to-user
  9045. (format "%s formula %s%s="
  9046. (if named "Field" "Column")
  9047. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  9048. scol))
  9049. (if stored (org-table-formula-to-user stored) "")
  9050. 'org-table-formula-history
  9051. )))))
  9052. mustsave)
  9053. (when (not (string-match "\\S-" eq))
  9054. ;; remove formula
  9055. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9056. (org-table-store-formulas stored-list)
  9057. (error "Formula removed"))
  9058. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9059. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9060. (if (and name (not named))
  9061. ;; We set the column equation, delete the named one.
  9062. (setq stored-list (delq (assoc name stored-list) stored-list)
  9063. mustsave t))
  9064. (if stored
  9065. (setcdr (assoc scol stored-list) eq)
  9066. (setq stored-list (cons (cons scol eq) stored-list)))
  9067. (if (or mustsave (not (equal stored eq)))
  9068. (org-table-store-formulas stored-list))
  9069. eq))
  9070. (defun org-table-store-formulas (alist)
  9071. "Store the list of formulas below the current table."
  9072. (setq alist (sort alist 'org-table-formula-less-p))
  9073. (save-excursion
  9074. (goto-char (org-table-end))
  9075. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  9076. (progn
  9077. ;; don't overwrite TBLFM, we might use text properties to store stuff
  9078. (goto-char (match-beginning 2))
  9079. (delete-region (match-beginning 2) (match-end 0)))
  9080. (insert "#+TBLFM:"))
  9081. (insert " "
  9082. (mapconcat (lambda (x)
  9083. (concat
  9084. (if (equal (string-to-char (car x)) ?@) "" "$")
  9085. (car x) "=" (cdr x)))
  9086. alist "::")
  9087. "\n")))
  9088. (defsubst org-table-formula-make-cmp-string (a)
  9089. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  9090. (concat
  9091. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  9092. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  9093. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  9094. (defun org-table-formula-less-p (a b)
  9095. "Compare two formulas for sorting."
  9096. (let ((as (org-table-formula-make-cmp-string (car a)))
  9097. (bs (org-table-formula-make-cmp-string (car b))))
  9098. (and as bs (string< as bs))))
  9099. (defun org-table-get-stored-formulas (&optional noerror)
  9100. "Return an alist with the stored formulas directly after current table."
  9101. (interactive)
  9102. (let (scol eq eq-alist strings string seen)
  9103. (save-excursion
  9104. (goto-char (org-table-end))
  9105. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9106. (setq strings (org-split-string (match-string 2) " *:: *"))
  9107. (while (setq string (pop strings))
  9108. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9109. (setq scol (if (match-end 2)
  9110. (match-string 2 string)
  9111. (match-string 1 string))
  9112. eq (match-string 3 string)
  9113. eq-alist (cons (cons scol eq) eq-alist))
  9114. (if (member scol seen)
  9115. (if noerror
  9116. (progn
  9117. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9118. (ding)
  9119. (sit-for 2))
  9120. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9121. (push scol seen))))))
  9122. (nreverse eq-alist)))
  9123. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9124. "Modify the equations after the table structure has been edited.
  9125. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9126. For all numbers larger than LIMIT, shift them by DELTA."
  9127. (save-excursion
  9128. (goto-char (org-table-end))
  9129. (when (looking-at "#\\+TBLFM:")
  9130. (let ((re (concat key "\\([0-9]+\\)"))
  9131. (re2
  9132. (when remove
  9133. (if (equal key "$")
  9134. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9135. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9136. s n a)
  9137. (when remove
  9138. (while (re-search-forward re2 (point-at-eol) t)
  9139. (replace-match "")))
  9140. (while (re-search-forward re (point-at-eol) t)
  9141. (setq s (match-string 1) n (string-to-number s))
  9142. (cond
  9143. ((setq a (assoc s replace))
  9144. (replace-match (concat key (cdr a)) t t))
  9145. ((and limit (> n limit))
  9146. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9147. (defun org-table-get-specials ()
  9148. "Get the column names and local parameters for this table."
  9149. (save-excursion
  9150. (let ((beg (org-table-begin)) (end (org-table-end))
  9151. names name fields fields1 field cnt
  9152. c v l line col types dlines hlines)
  9153. (setq org-table-column-names nil
  9154. org-table-local-parameters nil
  9155. org-table-named-field-locations nil
  9156. org-table-current-begin-line nil
  9157. org-table-current-begin-pos nil
  9158. org-table-current-line-types nil)
  9159. (goto-char beg)
  9160. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9161. (setq names (org-split-string (match-string 1) " *| *")
  9162. cnt 1)
  9163. (while (setq name (pop names))
  9164. (setq cnt (1+ cnt))
  9165. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9166. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9167. (setq org-table-column-names (nreverse org-table-column-names))
  9168. (setq org-table-column-name-regexp
  9169. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9170. (goto-char beg)
  9171. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9172. (setq fields (org-split-string (match-string 1) " *| *"))
  9173. (while (setq field (pop fields))
  9174. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9175. (push (cons (match-string 1 field) (match-string 2 field))
  9176. org-table-local-parameters))))
  9177. (goto-char beg)
  9178. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9179. (setq c (match-string 1)
  9180. fields (org-split-string (match-string 2) " *| *"))
  9181. (save-excursion
  9182. (beginning-of-line (if (equal c "_") 2 0))
  9183. (setq line (org-current-line) col 1)
  9184. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9185. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9186. (while (and fields1 (setq field (pop fields)))
  9187. (setq v (pop fields1) col (1+ col))
  9188. (when (and (stringp field) (stringp v)
  9189. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9190. (push (cons field v) org-table-local-parameters)
  9191. (push (list field line col) org-table-named-field-locations))))
  9192. ;; Analyse the line types
  9193. (goto-char beg)
  9194. (setq org-table-current-begin-line (org-current-line)
  9195. org-table-current-begin-pos (point)
  9196. l org-table-current-begin-line)
  9197. (while (looking-at "[ \t]*|\\(-\\)?")
  9198. (push (if (match-end 1) 'hline 'dline) types)
  9199. (if (match-end 1) (push l hlines) (push l dlines))
  9200. (beginning-of-line 2)
  9201. (setq l (1+ l)))
  9202. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9203. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9204. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9205. (defun org-table-maybe-eval-formula ()
  9206. "Check if the current field starts with \"=\" or \":=\".
  9207. If yes, store the formula and apply it."
  9208. ;; We already know we are in a table. Get field will only return a formula
  9209. ;; when appropriate. It might return a separator line, but no problem.
  9210. (when org-table-formula-evaluate-inline
  9211. (let* ((field (org-trim (or (org-table-get-field) "")))
  9212. named eq)
  9213. (when (string-match "^:?=\\(.*\\)" field)
  9214. (setq named (equal (string-to-char field) ?:)
  9215. eq (match-string 1 field))
  9216. (if (or (fboundp 'calc-eval)
  9217. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9218. (org-table-eval-formula (if named '(4) nil)
  9219. (org-table-formula-from-user eq))
  9220. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9221. (defvar org-recalc-commands nil
  9222. "List of commands triggering the recalculation of a line.
  9223. Will be filled automatically during use.")
  9224. (defvar org-recalc-marks
  9225. '((" " . "Unmarked: no special line, no automatic recalculation")
  9226. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9227. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9228. ("!" . "Column name definition line. Reference in formula as $name.")
  9229. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9230. ("_" . "Names for values in row below this one.")
  9231. ("^" . "Names for values in row above this one.")))
  9232. (defun org-table-rotate-recalc-marks (&optional newchar)
  9233. "Rotate the recalculation mark in the first column.
  9234. If in any row, the first field is not consistent with a mark,
  9235. insert a new column for the markers.
  9236. When there is an active region, change all the lines in the region,
  9237. after prompting for the marking character.
  9238. After each change, a message will be displayed indicating the meaning
  9239. of the new mark."
  9240. (interactive)
  9241. (unless (org-at-table-p) (error "Not at a table"))
  9242. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9243. (beg (org-table-begin))
  9244. (end (org-table-end))
  9245. (l (org-current-line))
  9246. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9247. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9248. (have-col
  9249. (save-excursion
  9250. (goto-char beg)
  9251. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9252. (col (org-table-current-column))
  9253. (forcenew (car (assoc newchar org-recalc-marks)))
  9254. epos new)
  9255. (when l1
  9256. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9257. (setq newchar (char-to-string (read-char-exclusive))
  9258. forcenew (car (assoc newchar org-recalc-marks))))
  9259. (if (and newchar (not forcenew))
  9260. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9261. newchar))
  9262. (if l1 (goto-line l1))
  9263. (save-excursion
  9264. (beginning-of-line 1)
  9265. (unless (looking-at org-table-dataline-regexp)
  9266. (error "Not at a table data line")))
  9267. (unless have-col
  9268. (org-table-goto-column 1)
  9269. (org-table-insert-column)
  9270. (org-table-goto-column (1+ col)))
  9271. (setq epos (point-at-eol))
  9272. (save-excursion
  9273. (beginning-of-line 1)
  9274. (org-table-get-field
  9275. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9276. (concat " "
  9277. (setq new (or forcenew
  9278. (cadr (member (match-string 1) marks))))
  9279. " ")
  9280. " # ")))
  9281. (if (and l1 l2)
  9282. (progn
  9283. (goto-line l1)
  9284. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9285. (and (looking-at org-table-dataline-regexp)
  9286. (org-table-get-field 1 (concat " " new " "))))
  9287. (goto-line l1)))
  9288. (if (not (= epos (point-at-eol))) (org-table-align))
  9289. (goto-line l)
  9290. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9291. (defun org-table-maybe-recalculate-line ()
  9292. "Recompute the current line if marked for it, and if we haven't just done it."
  9293. (interactive)
  9294. (and org-table-allow-automatic-line-recalculation
  9295. (not (and (memq last-command org-recalc-commands)
  9296. (equal org-last-recalc-line (org-current-line))))
  9297. (save-excursion (beginning-of-line 1)
  9298. (looking-at org-table-auto-recalculate-regexp))
  9299. (org-table-recalculate) t))
  9300. (defvar org-table-formula-debug nil
  9301. "Non-nil means, debug table formulas.
  9302. When nil, simply write \"#ERROR\" in corrupted fields.")
  9303. (make-variable-buffer-local 'org-table-formula-debug)
  9304. (defvar modes)
  9305. (defsubst org-set-calc-mode (var &optional value)
  9306. (if (stringp var)
  9307. (setq var (assoc var '(("D" calc-angle-mode deg)
  9308. ("R" calc-angle-mode rad)
  9309. ("F" calc-prefer-frac t)
  9310. ("S" calc-symbolic-mode t)))
  9311. value (nth 2 var) var (nth 1 var)))
  9312. (if (memq var modes)
  9313. (setcar (cdr (memq var modes)) value)
  9314. (cons var (cons value modes)))
  9315. modes)
  9316. (defun org-table-eval-formula (&optional arg equation
  9317. suppress-align suppress-const
  9318. suppress-store suppress-analysis)
  9319. "Replace the table field value at the cursor by the result of a calculation.
  9320. This function makes use of Dave Gillespie's Calc package, in my view the
  9321. most exciting program ever written for GNU Emacs. So you need to have Calc
  9322. installed in order to use this function.
  9323. In a table, this command replaces the value in the current field with the
  9324. result of a formula. It also installs the formula as the \"current\" column
  9325. formula, by storing it in a special line below the table. When called
  9326. with a `C-u' prefix, the current field must ba a named field, and the
  9327. formula is installed as valid in only this specific field.
  9328. When called with two `C-u' prefixes, insert the active equation
  9329. for the field back into the current field, so that it can be
  9330. edited there. This is useful in order to use \\[org-table-show-reference]
  9331. to check the referenced fields.
  9332. When called, the command first prompts for a formula, which is read in
  9333. the minibuffer. Previously entered formulas are available through the
  9334. history list, and the last used formula is offered as a default.
  9335. These stored formulas are adapted correctly when moving, inserting, or
  9336. deleting columns with the corresponding commands.
  9337. The formula can be any algebraic expression understood by the Calc package.
  9338. For details, see the Org-mode manual.
  9339. This function can also be called from Lisp programs and offers
  9340. additional arguments: EQUATION can be the formula to apply. If this
  9341. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9342. used to speed-up recursive calls by by-passing unnecessary aligns.
  9343. SUPPRESS-CONST suppresses the interpretation of constants in the
  9344. formula, assuming that this has been done already outside the function.
  9345. SUPPRESS-STORE means the formula should not be stored, either because
  9346. it is already stored, or because it is a modified equation that should
  9347. not overwrite the stored one."
  9348. (interactive "P")
  9349. (org-table-check-inside-data-field)
  9350. (or suppress-analysis (org-table-get-specials))
  9351. (if (equal arg '(16))
  9352. (let ((eq (org-table-current-field-formula)))
  9353. (or eq (error "No equation active for current field"))
  9354. (org-table-get-field nil eq)
  9355. (org-table-align)
  9356. (setq org-table-may-need-update t))
  9357. (let* (fields
  9358. (ndown (if (integerp arg) arg 1))
  9359. (org-table-automatic-realign nil)
  9360. (case-fold-search nil)
  9361. (down (> ndown 1))
  9362. (formula (if (and equation suppress-store)
  9363. equation
  9364. (org-table-get-formula equation (equal arg '(4)))))
  9365. (n0 (org-table-current-column))
  9366. (modes (copy-sequence org-calc-default-modes))
  9367. (numbers nil) ; was a variable, now fixed default
  9368. (keep-empty nil)
  9369. n form form0 bw fmt x ev orig c lispp literal)
  9370. ;; Parse the format string. Since we have a lot of modes, this is
  9371. ;; a lot of work. However, I think calc still uses most of the time.
  9372. (if (string-match ";" formula)
  9373. (let ((tmp (org-split-string formula ";")))
  9374. (setq formula (car tmp)
  9375. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9376. (nth 1 tmp)))
  9377. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9378. (setq c (string-to-char (match-string 1 fmt))
  9379. n (string-to-number (match-string 2 fmt)))
  9380. (if (= c ?p)
  9381. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9382. (setq modes (org-set-calc-mode
  9383. 'calc-float-format
  9384. (list (cdr (assoc c '((?n . float) (?f . fix)
  9385. (?s . sci) (?e . eng))))
  9386. n))))
  9387. (setq fmt (replace-match "" t t fmt)))
  9388. (if (string-match "[NT]" fmt)
  9389. (setq numbers (equal (match-string 0 fmt) "N")
  9390. fmt (replace-match "" t t fmt)))
  9391. (if (string-match "L" fmt)
  9392. (setq literal t
  9393. fmt (replace-match "" t t fmt)))
  9394. (if (string-match "E" fmt)
  9395. (setq keep-empty t
  9396. fmt (replace-match "" t t fmt)))
  9397. (while (string-match "[DRFS]" fmt)
  9398. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9399. (setq fmt (replace-match "" t t fmt)))
  9400. (unless (string-match "\\S-" fmt)
  9401. (setq fmt nil))))
  9402. (if (and (not suppress-const) org-table-formula-use-constants)
  9403. (setq formula (org-table-formula-substitute-names formula)))
  9404. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9405. (while (> ndown 0)
  9406. (setq fields (org-split-string
  9407. (org-no-properties
  9408. (buffer-substring (point-at-bol) (point-at-eol)))
  9409. " *| *"))
  9410. (if (eq numbers t)
  9411. (setq fields (mapcar
  9412. (lambda (x) (number-to-string (string-to-number x)))
  9413. fields)))
  9414. (setq ndown (1- ndown))
  9415. (setq form (copy-sequence formula)
  9416. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9417. (if (and lispp literal) (setq lispp 'literal))
  9418. ;; Check for old vertical references
  9419. (setq form (org-rewrite-old-row-references form))
  9420. ;; Insert complex ranges
  9421. (while (string-match org-table-range-regexp form)
  9422. (setq form
  9423. (replace-match
  9424. (save-match-data
  9425. (org-table-make-reference
  9426. (org-table-get-range (match-string 0 form) nil n0)
  9427. keep-empty numbers lispp))
  9428. t t form)))
  9429. ;; Insert simple ranges
  9430. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9431. (setq form
  9432. (replace-match
  9433. (save-match-data
  9434. (org-table-make-reference
  9435. (org-sublist
  9436. fields (string-to-number (match-string 1 form))
  9437. (string-to-number (match-string 2 form)))
  9438. keep-empty numbers lispp))
  9439. t t form)))
  9440. (setq form0 form)
  9441. ;; Insert the references to fields in same row
  9442. (while (string-match "\\$\\([0-9]+\\)" form)
  9443. (setq n (string-to-number (match-string 1 form))
  9444. x (nth (1- (if (= n 0) n0 n)) fields))
  9445. (unless x (error "Invalid field specifier \"%s\""
  9446. (match-string 0 form)))
  9447. (setq form (replace-match
  9448. (save-match-data
  9449. (org-table-make-reference x nil numbers lispp))
  9450. t t form)))
  9451. (if lispp
  9452. (setq ev (condition-case nil
  9453. (eval (eval (read form)))
  9454. (error "#ERROR"))
  9455. ev (if (numberp ev) (number-to-string ev) ev))
  9456. (or (fboundp 'calc-eval)
  9457. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9458. (setq ev (calc-eval (cons form modes)
  9459. (if numbers 'num))))
  9460. (when org-table-formula-debug
  9461. (with-output-to-temp-buffer "*Substitution History*"
  9462. (princ (format "Substitution history of formula
  9463. Orig: %s
  9464. $xyz-> %s
  9465. @r$c-> %s
  9466. $1-> %s\n" orig formula form0 form))
  9467. (if (listp ev)
  9468. (princ (format " %s^\nError: %s"
  9469. (make-string (car ev) ?\-) (nth 1 ev)))
  9470. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9471. ev (or fmt "NONE")
  9472. (if fmt (format fmt (string-to-number ev)) ev)))))
  9473. (setq bw (get-buffer-window "*Substitution History*"))
  9474. (shrink-window-if-larger-than-buffer bw)
  9475. (unless (and (interactive-p) (not ndown))
  9476. (unless (let (inhibit-redisplay)
  9477. (y-or-n-p "Debugging Formula. Continue to next? "))
  9478. (org-table-align)
  9479. (error "Abort"))
  9480. (delete-window bw)
  9481. (message "")))
  9482. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9483. (org-table-justify-field-maybe
  9484. (if fmt (format fmt (string-to-number ev)) ev))
  9485. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9486. (call-interactively 'org-return)
  9487. (setq ndown 0)))
  9488. (and down (org-table-maybe-recalculate-line))
  9489. (or suppress-align (and org-table-may-need-update
  9490. (org-table-align))))))
  9491. (defun org-table-put-field-property (prop value)
  9492. (save-excursion
  9493. (put-text-property (progn (skip-chars-backward "^|") (point))
  9494. (progn (skip-chars-forward "^|") (point))
  9495. prop value)))
  9496. (defun org-table-get-range (desc &optional tbeg col highlight)
  9497. "Get a calc vector from a column, accorting to descriptor DESC.
  9498. Optional arguments TBEG and COL can give the beginning of the table and
  9499. the current column, to avoid unnecessary parsing.
  9500. HIGHLIGHT means, just highlight the range."
  9501. (if (not (equal (string-to-char desc) ?@))
  9502. (setq desc (concat "@" desc)))
  9503. (save-excursion
  9504. (or tbeg (setq tbeg (org-table-begin)))
  9505. (or col (setq col (org-table-current-column)))
  9506. (let ((thisline (org-current-line))
  9507. beg end c1 c2 r1 r2 rangep tmp)
  9508. (unless (string-match org-table-range-regexp desc)
  9509. (error "Invalid table range specifier `%s'" desc))
  9510. (setq rangep (match-end 3)
  9511. r1 (and (match-end 1) (match-string 1 desc))
  9512. r2 (and (match-end 4) (match-string 4 desc))
  9513. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9514. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9515. (and c1 (setq c1 (+ (string-to-number c1)
  9516. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9517. (and c2 (setq c2 (+ (string-to-number c2)
  9518. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9519. (if (equal r1 "") (setq r1 nil))
  9520. (if (equal r2 "") (setq r2 nil))
  9521. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9522. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9523. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9524. (if (not r1) (setq r1 thisline))
  9525. (if (not r2) (setq r2 thisline))
  9526. (if (not c1) (setq c1 col))
  9527. (if (not c2) (setq c2 col))
  9528. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9529. ;; just one field
  9530. (progn
  9531. (goto-line r1)
  9532. (while (not (looking-at org-table-dataline-regexp))
  9533. (beginning-of-line 2))
  9534. (prog1 (org-trim (org-table-get-field c1))
  9535. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9536. ;; A range, return a vector
  9537. ;; First sort the numbers to get a regular ractangle
  9538. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9539. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9540. (goto-line r1)
  9541. (while (not (looking-at org-table-dataline-regexp))
  9542. (beginning-of-line 2))
  9543. (org-table-goto-column c1)
  9544. (setq beg (point))
  9545. (goto-line r2)
  9546. (while (not (looking-at org-table-dataline-regexp))
  9547. (beginning-of-line 0))
  9548. (org-table-goto-column c2)
  9549. (setq end (point))
  9550. (if highlight
  9551. (org-table-highlight-rectangle
  9552. beg (progn (skip-chars-forward "^|\n") (point))))
  9553. ;; return string representation of calc vector
  9554. (mapcar 'org-trim
  9555. (apply 'append (org-table-copy-region beg end)))))))
  9556. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9557. "Analyze descriptor DESC and retrieve the corresponding line number.
  9558. The cursor is currently in line CLINE, the table begins in line BLINE,
  9559. and TABLE is a vector with line types."
  9560. (if (string-match "^[0-9]+$" desc)
  9561. (aref org-table-dlines (string-to-number desc))
  9562. (setq cline (or cline (org-current-line))
  9563. bline (or bline org-table-current-begin-line)
  9564. table (or table org-table-current-line-types))
  9565. (if (or
  9566. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9567. ;; 1 2 3 4 5 6
  9568. (and (not (match-end 3)) (not (match-end 6)))
  9569. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9570. (error "invalid row descriptor `%s'" desc))
  9571. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9572. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9573. (odir (and (match-end 5) (match-string 5 desc)))
  9574. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9575. (i (- cline bline))
  9576. (rel (and (match-end 6)
  9577. (or (and (match-end 1) (not (match-end 3)))
  9578. (match-end 5)))))
  9579. (if (and hn (not hdir))
  9580. (progn
  9581. (setq i 0 hdir "+")
  9582. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9583. (if (and (not hn) on (not odir))
  9584. (error "should never happen");;(aref org-table-dlines on)
  9585. (if (and hn (> hn 0))
  9586. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9587. (if on
  9588. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9589. (+ bline i)))))
  9590. (defun org-find-row-type (table i type backwards relative n)
  9591. (let ((l (length table)))
  9592. (while (> n 0)
  9593. (while (and (setq i (+ i (if backwards -1 1)))
  9594. (>= i 0) (< i l)
  9595. (not (eq (aref table i) type))
  9596. (if (and relative (eq (aref table i) 'hline))
  9597. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9598. t)))
  9599. (setq n (1- n)))
  9600. (if (or (< i 0) (>= i l))
  9601. (error "Row descriptior leads outside table")
  9602. i)))
  9603. (defun org-rewrite-old-row-references (s)
  9604. (if (string-match "&[-+0-9I]" s)
  9605. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9606. s))
  9607. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9608. "Convert list ELEMENTS to something appropriate to insert into formula.
  9609. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9610. NUMBERS indicates that everything should be converted to numbers.
  9611. LISPP means to return something appropriate for a Lisp list."
  9612. (if (stringp elements) ; just a single val
  9613. (if lispp
  9614. (if (eq lispp 'literal)
  9615. elements
  9616. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9617. (if (equal elements "") (setq elements "0"))
  9618. (if numbers (number-to-string (string-to-number elements)) elements))
  9619. (unless keep-empty
  9620. (setq elements
  9621. (delq nil
  9622. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9623. elements))))
  9624. (setq elements (or elements '("0")))
  9625. (if lispp
  9626. (mapconcat
  9627. (lambda (x)
  9628. (if (eq lispp 'literal)
  9629. x
  9630. (prin1-to-string (if numbers (string-to-number x) x))))
  9631. elements " ")
  9632. (concat "[" (mapconcat
  9633. (lambda (x)
  9634. (if numbers (number-to-string (string-to-number x)) x))
  9635. elements
  9636. ",") "]"))))
  9637. (defun org-table-recalculate (&optional all noalign)
  9638. "Recalculate the current table line by applying all stored formulas.
  9639. With prefix arg ALL, do this for all lines in the table."
  9640. (interactive "P")
  9641. (or (memq this-command org-recalc-commands)
  9642. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9643. (unless (org-at-table-p) (error "Not at a table"))
  9644. (if (equal all '(16))
  9645. (org-table-iterate)
  9646. (org-table-get-specials)
  9647. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9648. (lambda (a b) (string< (car a) (car b)))))
  9649. (inhibit-redisplay (not debug-on-error))
  9650. (line-re org-table-dataline-regexp)
  9651. (thisline (org-current-line))
  9652. (thiscol (org-table-current-column))
  9653. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9654. ;; Insert constants in all formulas
  9655. (setq eqlist
  9656. (mapcar (lambda (x)
  9657. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9658. x)
  9659. eqlist))
  9660. ;; Split the equation list
  9661. (while (setq eq (pop eqlist))
  9662. (if (<= (string-to-char (car eq)) ?9)
  9663. (push eq eqlnum)
  9664. (push eq eqlname)))
  9665. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9666. (if all
  9667. (progn
  9668. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9669. (goto-char (setq beg (org-table-begin)))
  9670. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9671. ;; This is a table with marked lines, compute selected lines
  9672. (setq line-re org-table-recalculate-regexp)
  9673. ;; Move forward to the first non-header line
  9674. (if (and (re-search-forward org-table-dataline-regexp end t)
  9675. (re-search-forward org-table-hline-regexp end t)
  9676. (re-search-forward org-table-dataline-regexp end t))
  9677. (setq beg (match-beginning 0))
  9678. nil))) ;; just leave beg where it is
  9679. (setq beg (point-at-bol)
  9680. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9681. (goto-char beg)
  9682. (and all (message "Re-applying formulas to full table..."))
  9683. ;; First find the named fields, and mark them untouchanble
  9684. (remove-text-properties beg end '(org-untouchable t))
  9685. (while (setq eq (pop eqlname))
  9686. (setq name (car eq)
  9687. a (assoc name org-table-named-field-locations))
  9688. (and (not a)
  9689. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9690. (setq a (list name
  9691. (aref org-table-dlines
  9692. (string-to-number (match-string 1 name)))
  9693. (string-to-number (match-string 2 name)))))
  9694. (when (and a (or all (equal (nth 1 a) thisline)))
  9695. (message "Re-applying formula to field: %s" name)
  9696. (goto-line (nth 1 a))
  9697. (org-table-goto-column (nth 2 a))
  9698. (push (append a (list (cdr eq))) eqlname1)
  9699. (org-table-put-field-property :org-untouchable t)))
  9700. ;; Now evauluate the column formulas, but skip fields covered by
  9701. ;; field formulas
  9702. (goto-char beg)
  9703. (while (re-search-forward line-re end t)
  9704. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9705. ;; Unprotected line, recalculate
  9706. (and all (message "Re-applying formulas to full table...(line %d)"
  9707. (setq cnt (1+ cnt))))
  9708. (setq org-last-recalc-line (org-current-line))
  9709. (setq eql eqlnum)
  9710. (while (setq entry (pop eql))
  9711. (goto-line org-last-recalc-line)
  9712. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9713. (unless (get-text-property (point) :org-untouchable)
  9714. (org-table-eval-formula nil (cdr entry)
  9715. 'noalign 'nocst 'nostore 'noanalysis)))))
  9716. ;; Now evaluate the field formulas
  9717. (while (setq eq (pop eqlname1))
  9718. (message "Re-applying formula to field: %s" (car eq))
  9719. (goto-line (nth 1 eq))
  9720. (org-table-goto-column (nth 2 eq))
  9721. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9722. 'nostore 'noanalysis))
  9723. (goto-line thisline)
  9724. (org-table-goto-column thiscol)
  9725. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9726. (or noalign (and org-table-may-need-update (org-table-align))
  9727. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9728. ;; back to initial position
  9729. (message "Re-applying formulas...done")
  9730. (goto-line thisline)
  9731. (org-table-goto-column thiscol)
  9732. (or noalign (and org-table-may-need-update (org-table-align))
  9733. (and all (message "Re-applying formulas...done"))))))
  9734. (defun org-table-iterate (&optional arg)
  9735. "Recalculate the table until it does not change anymore."
  9736. (interactive "P")
  9737. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9738. (i 0)
  9739. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9740. thistbl)
  9741. (catch 'exit
  9742. (while (< i imax)
  9743. (setq i (1+ i))
  9744. (org-table-recalculate 'all)
  9745. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9746. (if (not (string= lasttbl thistbl))
  9747. (setq lasttbl thistbl)
  9748. (if (> i 1)
  9749. (message "Convergence after %d iterations" i)
  9750. (message "Table was already stable"))
  9751. (throw 'exit t)))
  9752. (error "No convergence after %d iterations" i))))
  9753. (defun org-table-formula-substitute-names (f)
  9754. "Replace $const with values in string F."
  9755. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9756. ;; First, check for column names
  9757. (while (setq start (string-match org-table-column-name-regexp f start))
  9758. (setq start (1+ start))
  9759. (setq a (assoc (match-string 1 f) org-table-column-names))
  9760. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9761. ;; Parameters and constants
  9762. (setq start 0)
  9763. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9764. (setq start (1+ start))
  9765. (if (setq a (save-match-data
  9766. (org-table-get-constant (match-string 1 f))))
  9767. (setq f (replace-match
  9768. (concat (if pp "(") a (if pp ")")) t t f))))
  9769. (if org-table-formula-debug
  9770. (put-text-property 0 (length f) :orig-formula f1 f))
  9771. f))
  9772. (defun org-table-get-constant (const)
  9773. "Find the value for a parameter or constant in a formula.
  9774. Parameters get priority."
  9775. (or (cdr (assoc const org-table-local-parameters))
  9776. (cdr (assoc const org-table-formula-constants-local))
  9777. (cdr (assoc const org-table-formula-constants))
  9778. (and (fboundp 'constants-get) (constants-get const))
  9779. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9780. (org-entry-get nil (substring const 5) 'inherit))
  9781. "#UNDEFINED_NAME"))
  9782. (defvar org-table-fedit-map
  9783. (let ((map (make-sparse-keymap)))
  9784. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9785. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9786. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9787. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9788. (org-defkey map "\C-c?" 'org-table-show-reference)
  9789. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9790. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9791. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9792. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9793. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9794. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9795. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9796. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9797. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9798. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9799. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9800. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9801. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9802. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9803. map))
  9804. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9805. '("Edit-Formulas"
  9806. ["Finish and Install" org-table-fedit-finish t]
  9807. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9808. ["Abort" org-table-fedit-abort t]
  9809. "--"
  9810. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9811. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9812. "--"
  9813. "Shift Reference at Point"
  9814. ["Up" org-table-fedit-ref-up t]
  9815. ["Down" org-table-fedit-ref-down t]
  9816. ["Left" org-table-fedit-ref-left t]
  9817. ["Right" org-table-fedit-ref-right t]
  9818. "-"
  9819. "Change Test Row for Column Formulas"
  9820. ["Up" org-table-fedit-line-up t]
  9821. ["Down" org-table-fedit-line-down t]
  9822. "--"
  9823. ["Scroll Table Window" org-table-fedit-scroll t]
  9824. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9825. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9826. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9827. org-table-overlay-coordinates)]
  9828. "--"
  9829. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9830. :style toggle :selected org-table-buffer-is-an]))
  9831. (defvar org-pos)
  9832. (defun org-table-edit-formulas ()
  9833. "Edit the formulas of the current table in a separate buffer."
  9834. (interactive)
  9835. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9836. (beginning-of-line 0))
  9837. (unless (org-at-table-p) (error "Not at a table"))
  9838. (org-table-get-specials)
  9839. (let ((key (org-table-current-field-formula 'key 'noerror))
  9840. (eql (sort (org-table-get-stored-formulas 'noerror)
  9841. 'org-table-formula-less-p))
  9842. (pos (move-marker (make-marker) (point)))
  9843. (startline 1)
  9844. (wc (current-window-configuration))
  9845. (titles '((column . "# Column Formulas\n")
  9846. (field . "# Field Formulas\n")
  9847. (named . "# Named Field Formulas\n")))
  9848. entry s type title)
  9849. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9850. (erase-buffer)
  9851. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9852. (let ((font-lock-global-modes '(not fundamental-mode)))
  9853. (fundamental-mode))
  9854. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9855. (org-set-local 'org-pos pos)
  9856. (org-set-local 'org-window-configuration wc)
  9857. (use-local-map org-table-fedit-map)
  9858. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9859. (easy-menu-add org-table-fedit-menu)
  9860. (setq startline (org-current-line))
  9861. (while (setq entry (pop eql))
  9862. (setq type (cond
  9863. ((equal (string-to-char (car entry)) ?@) 'field)
  9864. ((string-match "^[0-9]" (car entry)) 'column)
  9865. (t 'named)))
  9866. (when (setq title (assq type titles))
  9867. (or (bobp) (insert "\n"))
  9868. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9869. (setq titles (delq title titles)))
  9870. (if (equal key (car entry)) (setq startline (org-current-line)))
  9871. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9872. (car entry) " = " (cdr entry) "\n"))
  9873. (remove-text-properties 0 (length s) '(face nil) s)
  9874. (insert s))
  9875. (if (eq org-table-use-standard-references t)
  9876. (org-table-fedit-toggle-ref-type))
  9877. (goto-line startline)
  9878. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9879. (defun org-table-fedit-post-command ()
  9880. (when (not (memq this-command '(lisp-complete-symbol)))
  9881. (let ((win (selected-window)))
  9882. (save-excursion
  9883. (condition-case nil
  9884. (org-table-show-reference)
  9885. (error nil))
  9886. (select-window win)))))
  9887. (defun org-table-formula-to-user (s)
  9888. "Convert a formula from internal to user representation."
  9889. (if (eq org-table-use-standard-references t)
  9890. (org-table-convert-refs-to-an s)
  9891. s))
  9892. (defun org-table-formula-from-user (s)
  9893. "Convert a formula from user to internal representation."
  9894. (if org-table-use-standard-references
  9895. (org-table-convert-refs-to-rc s)
  9896. s))
  9897. (defun org-table-convert-refs-to-rc (s)
  9898. "Convert spreadsheet references from AB7 to @7$28.
  9899. Works for single references, but also for entire formulas and even the
  9900. full TBLFM line."
  9901. (let ((start 0))
  9902. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9903. (cond
  9904. ((match-end 3)
  9905. ;; format match, just advance
  9906. (setq start (match-end 0)))
  9907. ((and (> (match-beginning 0) 0)
  9908. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9909. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9910. ;; 3.e5 or something like this.
  9911. (setq start (match-end 0)))
  9912. (t
  9913. (setq start (match-beginning 0)
  9914. s (replace-match
  9915. (if (equal (match-string 2 s) "&")
  9916. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9917. (format "@%d$%d"
  9918. (string-to-number (match-string 2 s))
  9919. (org-letters-to-number (match-string 1 s))))
  9920. t t s)))))
  9921. s))
  9922. (defun org-table-convert-refs-to-an (s)
  9923. "Convert spreadsheet references from to @7$28 to AB7.
  9924. Works for single references, but also for entire formulas and even the
  9925. full TBLFM line."
  9926. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9927. (setq s (replace-match
  9928. (format "%s%d"
  9929. (org-number-to-letters
  9930. (string-to-number (match-string 2 s)))
  9931. (string-to-number (match-string 1 s)))
  9932. t t s)))
  9933. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9934. (setq s (replace-match (concat "\\1"
  9935. (org-number-to-letters
  9936. (string-to-number (match-string 2 s))) "&")
  9937. t nil s)))
  9938. s)
  9939. (defun org-letters-to-number (s)
  9940. "Convert a base 26 number represented by letters into an integer.
  9941. For example: AB -> 28."
  9942. (let ((n 0))
  9943. (setq s (upcase s))
  9944. (while (> (length s) 0)
  9945. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9946. s (substring s 1)))
  9947. n))
  9948. (defun org-number-to-letters (n)
  9949. "Convert an integer into a base 26 number represented by letters.
  9950. For example: 28 -> AB."
  9951. (let ((s ""))
  9952. (while (> n 0)
  9953. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9954. n (/ (1- n) 26)))
  9955. s))
  9956. (defun org-table-fedit-convert-buffer (function)
  9957. "Convert all references in this buffer, using FUNTION."
  9958. (let ((line (org-current-line)))
  9959. (goto-char (point-min))
  9960. (while (not (eobp))
  9961. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9962. (delete-region (point) (point-at-eol))
  9963. (or (eobp) (forward-char 1)))
  9964. (goto-line line)))
  9965. (defun org-table-fedit-toggle-ref-type ()
  9966. "Convert all references in the buffer from B3 to @3$2 and back."
  9967. (interactive)
  9968. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9969. (org-table-fedit-convert-buffer
  9970. (if org-table-buffer-is-an
  9971. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9972. (message "Reference type switched to %s"
  9973. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9974. (defun org-table-fedit-ref-up ()
  9975. "Shift the reference at point one row/hline up."
  9976. (interactive)
  9977. (org-table-fedit-shift-reference 'up))
  9978. (defun org-table-fedit-ref-down ()
  9979. "Shift the reference at point one row/hline down."
  9980. (interactive)
  9981. (org-table-fedit-shift-reference 'down))
  9982. (defun org-table-fedit-ref-left ()
  9983. "Shift the reference at point one field to the left."
  9984. (interactive)
  9985. (org-table-fedit-shift-reference 'left))
  9986. (defun org-table-fedit-ref-right ()
  9987. "Shift the reference at point one field to the right."
  9988. (interactive)
  9989. (org-table-fedit-shift-reference 'right))
  9990. (defun org-table-fedit-shift-reference (dir)
  9991. (cond
  9992. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9993. (if (memq dir '(left right))
  9994. (org-rematch-and-replace 1 (eq dir 'left))
  9995. (error "Cannot shift reference in this direction")))
  9996. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9997. ;; A B3-like reference
  9998. (if (memq dir '(up down))
  9999. (org-rematch-and-replace 2 (eq dir 'up))
  10000. (org-rematch-and-replace 1 (eq dir 'left))))
  10001. ((org-at-regexp-p
  10002. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  10003. ;; An internal reference
  10004. (if (memq dir '(up down))
  10005. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  10006. (org-rematch-and-replace 5 (eq dir 'left))))))
  10007. (defun org-rematch-and-replace (n &optional decr hline)
  10008. "Re-match the group N, and replace it with the shifted refrence."
  10009. (or (match-end n) (error "Cannot shift reference in this direction"))
  10010. (goto-char (match-beginning n))
  10011. (and (looking-at (regexp-quote (match-string n)))
  10012. (replace-match (org-shift-refpart (match-string 0) decr hline)
  10013. t t)))
  10014. (defun org-shift-refpart (ref &optional decr hline)
  10015. "Shift a refrence part REF.
  10016. If DECR is set, decrease the references row/column, else increase.
  10017. If HLINE is set, this may be a hline reference, it certainly is not
  10018. a translation reference."
  10019. (save-match-data
  10020. (let* ((sign (string-match "^[-+]" ref)) n)
  10021. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  10022. (cond
  10023. ((and hline (string-match "^I+" ref))
  10024. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  10025. (setq n (+ n (if decr -1 1)))
  10026. (if (= n 0) (setq n (+ n (if decr -1 1))))
  10027. (if sign
  10028. (setq sign (if (< n 0) "-" "+") n (abs n))
  10029. (setq n (max 1 n)))
  10030. (concat sign (make-string n ?I)))
  10031. ((string-match "^[0-9]+" ref)
  10032. (setq n (string-to-number (concat sign ref)))
  10033. (setq n (+ n (if decr -1 1)))
  10034. (if sign
  10035. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  10036. (number-to-string (max 1 n))))
  10037. ((string-match "^[a-zA-Z]+" ref)
  10038. (org-number-to-letters
  10039. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  10040. (t (error "Cannot shift reference"))))))
  10041. (defun org-table-fedit-toggle-coordinates ()
  10042. "Toggle the display of coordinates in the refrenced table."
  10043. (interactive)
  10044. (let ((pos (marker-position org-pos)))
  10045. (with-current-buffer (marker-buffer org-pos)
  10046. (save-excursion
  10047. (goto-char pos)
  10048. (org-table-toggle-coordinate-overlays)))))
  10049. (defun org-table-fedit-finish (&optional arg)
  10050. "Parse the buffer for formula definitions and install them.
  10051. With prefix ARG, apply the new formulas to the table."
  10052. (interactive "P")
  10053. (org-table-remove-rectangle-highlight)
  10054. (if org-table-use-standard-references
  10055. (progn
  10056. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  10057. (setq org-table-buffer-is-an nil)))
  10058. (let ((pos org-pos) eql var form)
  10059. (goto-char (point-min))
  10060. (while (re-search-forward
  10061. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  10062. nil t)
  10063. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  10064. form (match-string 3))
  10065. (setq form (org-trim form))
  10066. (when (not (equal form ""))
  10067. (while (string-match "[ \t]*\n[ \t]*" form)
  10068. (setq form (replace-match " " t t form)))
  10069. (when (assoc var eql)
  10070. (error "Double formulas for %s" var))
  10071. (push (cons var form) eql)))
  10072. (setq org-pos nil)
  10073. (set-window-configuration org-window-configuration)
  10074. (select-window (get-buffer-window (marker-buffer pos)))
  10075. (goto-char pos)
  10076. (unless (org-at-table-p)
  10077. (error "Lost table position - cannot install formulae"))
  10078. (org-table-store-formulas eql)
  10079. (move-marker pos nil)
  10080. (kill-buffer "*Edit Formulas*")
  10081. (if arg
  10082. (org-table-recalculate 'all)
  10083. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10084. (defun org-table-fedit-abort ()
  10085. "Abort editing formulas, without installing the changes."
  10086. (interactive)
  10087. (org-table-remove-rectangle-highlight)
  10088. (let ((pos org-pos))
  10089. (set-window-configuration org-window-configuration)
  10090. (select-window (get-buffer-window (marker-buffer pos)))
  10091. (goto-char pos)
  10092. (move-marker pos nil)
  10093. (message "Formula editing aborted without installing changes")))
  10094. (defun org-table-fedit-lisp-indent ()
  10095. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  10096. (interactive)
  10097. (let ((pos (point)) beg end ind)
  10098. (beginning-of-line 1)
  10099. (cond
  10100. ((looking-at "[ \t]")
  10101. (goto-char pos)
  10102. (call-interactively 'lisp-indent-line))
  10103. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10104. ((not (fboundp 'pp-buffer))
  10105. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10106. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10107. (goto-char (- (match-end 0) 2))
  10108. (setq beg (point))
  10109. (setq ind (make-string (current-column) ?\ ))
  10110. (condition-case nil (forward-sexp 1)
  10111. (error
  10112. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10113. (setq end (point))
  10114. (save-restriction
  10115. (narrow-to-region beg end)
  10116. (if (eq last-command this-command)
  10117. (progn
  10118. (goto-char (point-min))
  10119. (setq this-command nil)
  10120. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10121. (replace-match " ")))
  10122. (pp-buffer)
  10123. (untabify (point-min) (point-max))
  10124. (goto-char (1+ (point-min)))
  10125. (while (re-search-forward "^." nil t)
  10126. (beginning-of-line 1)
  10127. (insert ind))
  10128. (goto-char (point-max))
  10129. (backward-delete-char 1)))
  10130. (goto-char beg))
  10131. (t nil))))
  10132. (defvar org-show-positions nil)
  10133. (defun org-table-show-reference (&optional local)
  10134. "Show the location/value of the $ expression at point."
  10135. (interactive)
  10136. (org-table-remove-rectangle-highlight)
  10137. (catch 'exit
  10138. (let ((pos (if local (point) org-pos))
  10139. (face2 'highlight)
  10140. (org-inhibit-highlight-removal t)
  10141. (win (selected-window))
  10142. (org-show-positions nil)
  10143. var name e what match dest)
  10144. (if local (org-table-get-specials))
  10145. (setq what (cond
  10146. ((or (org-at-regexp-p org-table-range-regexp2)
  10147. (org-at-regexp-p org-table-translate-regexp)
  10148. (org-at-regexp-p org-table-range-regexp))
  10149. (setq match
  10150. (save-match-data
  10151. (org-table-convert-refs-to-rc (match-string 0))))
  10152. 'range)
  10153. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10154. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10155. ((not local) nil)
  10156. (t (error "No reference at point")))
  10157. match (and what (or match (match-string 0))))
  10158. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10159. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10160. 'secondary-selection))
  10161. (org-add-hook 'before-change-functions
  10162. 'org-table-remove-rectangle-highlight)
  10163. (if (eq what 'name) (setq var (substring match 1)))
  10164. (when (eq what 'range)
  10165. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10166. (setq match (org-table-formula-substitute-names match)))
  10167. (unless local
  10168. (save-excursion
  10169. (end-of-line 1)
  10170. (re-search-backward "^\\S-" nil t)
  10171. (beginning-of-line 1)
  10172. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10173. (setq dest
  10174. (save-match-data
  10175. (org-table-convert-refs-to-rc (match-string 1))))
  10176. (org-table-add-rectangle-overlay
  10177. (match-beginning 1) (match-end 1) face2))))
  10178. (if (and (markerp pos) (marker-buffer pos))
  10179. (if (get-buffer-window (marker-buffer pos))
  10180. (select-window (get-buffer-window (marker-buffer pos)))
  10181. (org-switch-to-buffer-other-window (get-buffer-window
  10182. (marker-buffer pos)))))
  10183. (goto-char pos)
  10184. (org-table-force-dataline)
  10185. (when dest
  10186. (setq name (substring dest 1))
  10187. (cond
  10188. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10189. (setq e (assoc name org-table-named-field-locations))
  10190. (goto-line (nth 1 e))
  10191. (org-table-goto-column (nth 2 e)))
  10192. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10193. (let ((l (string-to-number (match-string 1 dest)))
  10194. (c (string-to-number (match-string 2 dest))))
  10195. (goto-line (aref org-table-dlines l))
  10196. (org-table-goto-column c)))
  10197. (t (org-table-goto-column (string-to-number name))))
  10198. (move-marker pos (point))
  10199. (org-table-highlight-rectangle nil nil face2))
  10200. (cond
  10201. ((equal dest match))
  10202. ((not match))
  10203. ((eq what 'range)
  10204. (condition-case nil
  10205. (save-excursion
  10206. (org-table-get-range match nil nil 'highlight))
  10207. (error nil)))
  10208. ((setq e (assoc var org-table-named-field-locations))
  10209. (goto-line (nth 1 e))
  10210. (org-table-goto-column (nth 2 e))
  10211. (org-table-highlight-rectangle (point) (point))
  10212. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10213. ((setq e (assoc var org-table-column-names))
  10214. (org-table-goto-column (string-to-number (cdr e)))
  10215. (org-table-highlight-rectangle (point) (point))
  10216. (goto-char (org-table-begin))
  10217. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10218. (org-table-end) t)
  10219. (progn
  10220. (goto-char (match-beginning 1))
  10221. (org-table-highlight-rectangle)
  10222. (message "Named column (column %s)" (cdr e)))
  10223. (error "Column name not found")))
  10224. ((eq what 'column)
  10225. ;; column number
  10226. (org-table-goto-column (string-to-number (substring match 1)))
  10227. (org-table-highlight-rectangle (point) (point))
  10228. (message "Column %s" (substring match 1)))
  10229. ((setq e (assoc var org-table-local-parameters))
  10230. (goto-char (org-table-begin))
  10231. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10232. (progn
  10233. (goto-char (match-beginning 1))
  10234. (org-table-highlight-rectangle)
  10235. (message "Local parameter."))
  10236. (error "Parameter not found")))
  10237. (t
  10238. (cond
  10239. ((not var) (error "No reference at point"))
  10240. ((setq e (assoc var org-table-formula-constants-local))
  10241. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10242. var (cdr e)))
  10243. ((setq e (assoc var org-table-formula-constants))
  10244. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10245. var (cdr e)))
  10246. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10247. (message "Constant: $%s=%s, from `constants.el'%s."
  10248. var e (format " (%s units)" constants-unit-system)))
  10249. (t (error "Undefined name $%s" var)))))
  10250. (goto-char pos)
  10251. (when (and org-show-positions
  10252. (not (memq this-command '(org-table-fedit-scroll
  10253. org-table-fedit-scroll-down))))
  10254. (push pos org-show-positions)
  10255. (push org-table-current-begin-pos org-show-positions)
  10256. (let ((min (apply 'min org-show-positions))
  10257. (max (apply 'max org-show-positions)))
  10258. (goto-char min) (recenter 0)
  10259. (goto-char max)
  10260. (or (pos-visible-in-window-p max) (recenter -1))))
  10261. (select-window win))))
  10262. (defun org-table-force-dataline ()
  10263. "Make sure the cursor is in a dataline in a table."
  10264. (unless (save-excursion
  10265. (beginning-of-line 1)
  10266. (looking-at org-table-dataline-regexp))
  10267. (let* ((re org-table-dataline-regexp)
  10268. (p1 (save-excursion (re-search-forward re nil 'move)))
  10269. (p2 (save-excursion (re-search-backward re nil 'move))))
  10270. (cond ((and p1 p2)
  10271. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10272. p1 p2)))
  10273. ((or p1 p2) (goto-char (or p1 p2)))
  10274. (t (error "No table dataline around here"))))))
  10275. (defun org-table-fedit-line-up ()
  10276. "Move cursor one line up in the window showing the table."
  10277. (interactive)
  10278. (org-table-fedit-move 'previous-line))
  10279. (defun org-table-fedit-line-down ()
  10280. "Move cursor one line down in the window showing the table."
  10281. (interactive)
  10282. (org-table-fedit-move 'next-line))
  10283. (defun org-table-fedit-move (command)
  10284. "Move the cursor in the window shoinw the table.
  10285. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10286. (let ((org-table-allow-automatic-line-recalculation nil)
  10287. (pos org-pos) (win (selected-window)) p)
  10288. (select-window (get-buffer-window (marker-buffer org-pos)))
  10289. (setq p (point))
  10290. (call-interactively command)
  10291. (while (and (org-at-table-p)
  10292. (org-at-table-hline-p))
  10293. (call-interactively command))
  10294. (or (org-at-table-p) (goto-char p))
  10295. (move-marker pos (point))
  10296. (select-window win)))
  10297. (defun org-table-fedit-scroll (N)
  10298. (interactive "p")
  10299. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10300. (scroll-other-window N)))
  10301. (defun org-table-fedit-scroll-down (N)
  10302. (interactive "p")
  10303. (org-table-fedit-scroll (- N)))
  10304. (defvar org-table-rectangle-overlays nil)
  10305. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10306. "Add a new overlay."
  10307. (let ((ov (org-make-overlay beg end)))
  10308. (org-overlay-put ov 'face (or face 'secondary-selection))
  10309. (push ov org-table-rectangle-overlays)))
  10310. (defun org-table-highlight-rectangle (&optional beg end face)
  10311. "Highlight rectangular region in a table."
  10312. (setq beg (or beg (point)) end (or end (point)))
  10313. (let ((b (min beg end))
  10314. (e (max beg end))
  10315. l1 c1 l2 c2 tmp)
  10316. (and (boundp 'org-show-positions)
  10317. (setq org-show-positions (cons b (cons e org-show-positions))))
  10318. (goto-char (min beg end))
  10319. (setq l1 (org-current-line)
  10320. c1 (org-table-current-column))
  10321. (goto-char (max beg end))
  10322. (setq l2 (org-current-line)
  10323. c2 (org-table-current-column))
  10324. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10325. (goto-line l1)
  10326. (beginning-of-line 1)
  10327. (loop for line from l1 to l2 do
  10328. (when (looking-at org-table-dataline-regexp)
  10329. (org-table-goto-column c1)
  10330. (skip-chars-backward "^|\n") (setq beg (point))
  10331. (org-table-goto-column c2)
  10332. (skip-chars-forward "^|\n") (setq end (point))
  10333. (org-table-add-rectangle-overlay beg end face))
  10334. (beginning-of-line 2))
  10335. (goto-char b))
  10336. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10337. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10338. "Remove the rectangle overlays."
  10339. (unless org-inhibit-highlight-removal
  10340. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10341. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10342. (setq org-table-rectangle-overlays nil)))
  10343. (defvar org-table-coordinate-overlays nil
  10344. "Collects the cooordinate grid overlays, so that they can be removed.")
  10345. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10346. (defun org-table-overlay-coordinates ()
  10347. "Add overlays to the table at point, to show row/column coordinates."
  10348. (interactive)
  10349. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10350. (setq org-table-coordinate-overlays nil)
  10351. (save-excursion
  10352. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10353. (goto-char (org-table-begin))
  10354. (while (org-at-table-p)
  10355. (setq eol (point-at-eol))
  10356. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10357. (push ov org-table-coordinate-overlays)
  10358. (setq hline (looking-at org-table-hline-regexp))
  10359. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10360. (format "%4d" (setq id (1+ id)))))
  10361. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10362. (when hline
  10363. (setq ic 0)
  10364. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10365. (setq beg (1+ (match-beginning 0))
  10366. ic (1+ ic)
  10367. s1 (concat "$" (int-to-string ic))
  10368. s2 (org-number-to-letters ic)
  10369. str (if (eq org-table-use-standard-references t) s2 s1))
  10370. (setq ov (org-make-overlay beg (+ beg (length str))))
  10371. (push ov org-table-coordinate-overlays)
  10372. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10373. (beginning-of-line 2)))))
  10374. (defun org-table-toggle-coordinate-overlays ()
  10375. "Toggle the display of Row/Column numbers in tables."
  10376. (interactive)
  10377. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10378. (message "Row/Column number display turned %s"
  10379. (if org-table-overlay-coordinates "on" "off"))
  10380. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10381. (org-table-align))
  10382. (unless org-table-overlay-coordinates
  10383. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10384. (setq org-table-coordinate-overlays nil)))
  10385. (defun org-table-toggle-formula-debugger ()
  10386. "Toggle the formula debugger in tables."
  10387. (interactive)
  10388. (setq org-table-formula-debug (not org-table-formula-debug))
  10389. (message "Formula debugging has been turned %s"
  10390. (if org-table-formula-debug "on" "off")))
  10391. ;;; The orgtbl minor mode
  10392. ;; Define a minor mode which can be used in other modes in order to
  10393. ;; integrate the org-mode table editor.
  10394. ;; This is really a hack, because the org-mode table editor uses several
  10395. ;; keys which normally belong to the major mode, for example the TAB and
  10396. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10397. ;; necessary to operate the table editor, but wraps the commands into a
  10398. ;; function which tests if the cursor is currently inside a table. If that
  10399. ;; is the case, the table editor command is executed. However, when any of
  10400. ;; those keys is used outside a table, the function uses `key-binding' to
  10401. ;; look up if the key has an associated command in another currently active
  10402. ;; keymap (minor modes, major mode, global), and executes that command.
  10403. ;; There might be problems if any of the keys used by the table editor is
  10404. ;; otherwise used as a prefix key.
  10405. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10406. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10407. ;; addresses this by checking explicitly for both bindings.
  10408. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10409. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10410. ;; Some modes bind other commands to simple characters, for example
  10411. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10412. ;; active, this binding is ignored inside tables and replaced with a
  10413. ;; modified self-insert.
  10414. (defvar orgtbl-mode nil
  10415. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10416. table editor in arbitrary modes.")
  10417. (make-variable-buffer-local 'orgtbl-mode)
  10418. (defvar orgtbl-mode-map (make-keymap)
  10419. "Keymap for `orgtbl-mode'.")
  10420. ;;;###autoload
  10421. (defun turn-on-orgtbl ()
  10422. "Unconditionally turn on `orgtbl-mode'."
  10423. (orgtbl-mode 1))
  10424. (defvar org-old-auto-fill-inhibit-regexp nil
  10425. "Local variable used by `orgtbl-mode'")
  10426. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10427. "Matches a line belonging to an orgtbl.")
  10428. (defconst orgtbl-extra-font-lock-keywords
  10429. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10430. 0 (quote 'org-table) 'prepend))
  10431. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10432. ;;;###autoload
  10433. (defun orgtbl-mode (&optional arg)
  10434. "The `org-mode' table editor as a minor mode for use in other modes."
  10435. (interactive)
  10436. (if (org-mode-p)
  10437. ;; Exit without error, in case some hook functions calls this
  10438. ;; by accident in org-mode.
  10439. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10440. (setq orgtbl-mode
  10441. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10442. (if orgtbl-mode
  10443. (progn
  10444. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10445. ;; Make sure we are first in minor-mode-map-alist
  10446. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10447. (and c (setq minor-mode-map-alist
  10448. (cons c (delq c minor-mode-map-alist)))))
  10449. (org-set-local (quote org-table-may-need-update) t)
  10450. (org-add-hook 'before-change-functions 'org-before-change-function
  10451. nil 'local)
  10452. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10453. auto-fill-inhibit-regexp)
  10454. (org-set-local 'auto-fill-inhibit-regexp
  10455. (if auto-fill-inhibit-regexp
  10456. (concat orgtbl-line-start-regexp "\\|"
  10457. auto-fill-inhibit-regexp)
  10458. orgtbl-line-start-regexp))
  10459. (org-add-to-invisibility-spec '(org-cwidth))
  10460. (when (fboundp 'font-lock-add-keywords)
  10461. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10462. (org-restart-font-lock))
  10463. (easy-menu-add orgtbl-mode-menu)
  10464. (run-hooks 'orgtbl-mode-hook))
  10465. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10466. (org-cleanup-narrow-column-properties)
  10467. (org-remove-from-invisibility-spec '(org-cwidth))
  10468. (remove-hook 'before-change-functions 'org-before-change-function t)
  10469. (when (fboundp 'font-lock-remove-keywords)
  10470. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10471. (org-restart-font-lock))
  10472. (easy-menu-remove orgtbl-mode-menu)
  10473. (force-mode-line-update 'all))))
  10474. (defun org-cleanup-narrow-column-properties ()
  10475. "Remove all properties related to narrow-column invisibility."
  10476. (let ((s 1))
  10477. (while (setq s (text-property-any s (point-max)
  10478. 'display org-narrow-column-arrow))
  10479. (remove-text-properties s (1+ s) '(display t)))
  10480. (setq s 1)
  10481. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10482. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10483. (setq s 1)
  10484. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10485. (remove-text-properties s (1+ s) '(invisible t)))))
  10486. ;; Install it as a minor mode.
  10487. (put 'orgtbl-mode :included t)
  10488. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10489. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10490. (defun orgtbl-make-binding (fun n &rest keys)
  10491. "Create a function for binding in the table minor mode.
  10492. FUN is the command to call inside a table. N is used to create a unique
  10493. command name. KEYS are keys that should be checked in for a command
  10494. to execute outside of tables."
  10495. (eval
  10496. (list 'defun
  10497. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10498. '(arg)
  10499. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10500. "Outside of tables, run the binding of `"
  10501. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10502. "'.")
  10503. '(interactive "p")
  10504. (list 'if
  10505. '(org-at-table-p)
  10506. (list 'call-interactively (list 'quote fun))
  10507. (list 'let '(orgtbl-mode)
  10508. (list 'call-interactively
  10509. (append '(or)
  10510. (mapcar (lambda (k)
  10511. (list 'key-binding k))
  10512. keys)
  10513. '('orgtbl-error))))))))
  10514. (defun orgtbl-error ()
  10515. "Error when there is no default binding for a table key."
  10516. (interactive)
  10517. (error "This key has no function outside tables"))
  10518. (defun orgtbl-setup ()
  10519. "Setup orgtbl keymaps."
  10520. (let ((nfunc 0)
  10521. (bindings
  10522. (list
  10523. '([(meta shift left)] org-table-delete-column)
  10524. '([(meta left)] org-table-move-column-left)
  10525. '([(meta right)] org-table-move-column-right)
  10526. '([(meta shift right)] org-table-insert-column)
  10527. '([(meta shift up)] org-table-kill-row)
  10528. '([(meta shift down)] org-table-insert-row)
  10529. '([(meta up)] org-table-move-row-up)
  10530. '([(meta down)] org-table-move-row-down)
  10531. '("\C-c\C-w" org-table-cut-region)
  10532. '("\C-c\M-w" org-table-copy-region)
  10533. '("\C-c\C-y" org-table-paste-rectangle)
  10534. '("\C-c-" org-table-insert-hline)
  10535. '("\C-c}" org-table-toggle-coordinate-overlays)
  10536. '("\C-c{" org-table-toggle-formula-debugger)
  10537. '("\C-m" org-table-next-row)
  10538. '([(shift return)] org-table-copy-down)
  10539. '("\C-c\C-q" org-table-wrap-region)
  10540. '("\C-c?" org-table-field-info)
  10541. '("\C-c " org-table-blank-field)
  10542. '("\C-c+" org-table-sum)
  10543. '("\C-c=" org-table-eval-formula)
  10544. '("\C-c'" org-table-edit-formulas)
  10545. '("\C-c`" org-table-edit-field)
  10546. '("\C-c*" org-table-recalculate)
  10547. '("\C-c|" org-table-create-or-convert-from-region)
  10548. '("\C-c^" org-table-sort-lines)
  10549. '([(control ?#)] org-table-rotate-recalc-marks)))
  10550. elt key fun cmd)
  10551. (while (setq elt (pop bindings))
  10552. (setq nfunc (1+ nfunc))
  10553. (setq key (org-key (car elt))
  10554. fun (nth 1 elt)
  10555. cmd (orgtbl-make-binding fun nfunc key))
  10556. (org-defkey orgtbl-mode-map key cmd))
  10557. ;; Special treatment needed for TAB and RET
  10558. (org-defkey orgtbl-mode-map [(return)]
  10559. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10560. (org-defkey orgtbl-mode-map "\C-m"
  10561. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10562. (org-defkey orgtbl-mode-map [(tab)]
  10563. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10564. (org-defkey orgtbl-mode-map "\C-i"
  10565. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10566. (org-defkey orgtbl-mode-map [(shift tab)]
  10567. (orgtbl-make-binding 'org-table-previous-field 104
  10568. [(shift tab)] [(tab)] "\C-i"))
  10569. (org-defkey orgtbl-mode-map "\M-\C-m"
  10570. (orgtbl-make-binding 'org-table-wrap-region 105
  10571. "\M-\C-m" [(meta return)]))
  10572. (org-defkey orgtbl-mode-map [(meta return)]
  10573. (orgtbl-make-binding 'org-table-wrap-region 106
  10574. [(meta return)] "\M-\C-m"))
  10575. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10576. (when orgtbl-optimized
  10577. ;; If the user wants maximum table support, we need to hijack
  10578. ;; some standard editing functions
  10579. (org-remap orgtbl-mode-map
  10580. 'self-insert-command 'orgtbl-self-insert-command
  10581. 'delete-char 'org-delete-char
  10582. 'delete-backward-char 'org-delete-backward-char)
  10583. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10584. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10585. '("OrgTbl"
  10586. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10587. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10588. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10589. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10590. "--"
  10591. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10592. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10593. ["Copy Field from Above"
  10594. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10595. "--"
  10596. ("Column"
  10597. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10598. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10599. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10600. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10601. ("Row"
  10602. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10603. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10604. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10605. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10606. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10607. "--"
  10608. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10609. ("Rectangle"
  10610. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10611. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10612. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10613. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10614. "--"
  10615. ("Radio tables"
  10616. ["Insert table template" orgtbl-insert-radio-table
  10617. (assq major-mode orgtbl-radio-table-templates)]
  10618. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10619. "--"
  10620. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10621. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10622. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10623. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10624. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10625. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10626. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10627. ["Sum Column/Rectangle" org-table-sum
  10628. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10629. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10630. ["Debug Formulas"
  10631. org-table-toggle-formula-debugger :active (org-at-table-p)
  10632. :keys "C-c {"
  10633. :style toggle :selected org-table-formula-debug]
  10634. ["Show Col/Row Numbers"
  10635. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10636. :keys "C-c }"
  10637. :style toggle :selected org-table-overlay-coordinates]
  10638. ))
  10639. t))
  10640. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10641. "If the cursor is inside a table, realign the table.
  10642. It it is a table to be sent away to a receiver, do it.
  10643. With prefix arg, also recompute table."
  10644. (interactive "P")
  10645. (let ((pos (point)) action)
  10646. (save-excursion
  10647. (beginning-of-line 1)
  10648. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10649. ((looking-at "[ \t]*|") pos)
  10650. ((looking-at "#\\+TBLFM:") 'recalc))))
  10651. (cond
  10652. ((integerp action)
  10653. (goto-char action)
  10654. (org-table-maybe-eval-formula)
  10655. (if arg
  10656. (call-interactively 'org-table-recalculate)
  10657. (org-table-maybe-recalculate-line))
  10658. (call-interactively 'org-table-align)
  10659. (orgtbl-send-table 'maybe))
  10660. ((eq action 'recalc)
  10661. (save-excursion
  10662. (beginning-of-line 1)
  10663. (skip-chars-backward " \r\n\t")
  10664. (if (org-at-table-p)
  10665. (org-call-with-arg 'org-table-recalculate t))))
  10666. (t (let (orgtbl-mode)
  10667. (call-interactively (key-binding "\C-c\C-c")))))))
  10668. (defun orgtbl-tab (arg)
  10669. "Justification and field motion for `orgtbl-mode'."
  10670. (interactive "P")
  10671. (if arg (org-table-edit-field t)
  10672. (org-table-justify-field-maybe)
  10673. (org-table-next-field)))
  10674. (defun orgtbl-ret ()
  10675. "Justification and field motion for `orgtbl-mode'."
  10676. (interactive)
  10677. (org-table-justify-field-maybe)
  10678. (org-table-next-row))
  10679. (defun orgtbl-self-insert-command (N)
  10680. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10681. If the cursor is in a table looking at whitespace, the whitespace is
  10682. overwritten, and the table is not marked as requiring realignment."
  10683. (interactive "p")
  10684. (if (and (org-at-table-p)
  10685. (or
  10686. (and org-table-auto-blank-field
  10687. (member last-command
  10688. '(orgtbl-hijacker-command-100
  10689. orgtbl-hijacker-command-101
  10690. orgtbl-hijacker-command-102
  10691. orgtbl-hijacker-command-103
  10692. orgtbl-hijacker-command-104
  10693. orgtbl-hijacker-command-105))
  10694. (org-table-blank-field))
  10695. t)
  10696. (eq N 1)
  10697. (looking-at "[^|\n]* +|"))
  10698. (let (org-table-may-need-update)
  10699. (goto-char (1- (match-end 0)))
  10700. (delete-backward-char 1)
  10701. (goto-char (match-beginning 0))
  10702. (self-insert-command N))
  10703. (setq org-table-may-need-update t)
  10704. (let (orgtbl-mode)
  10705. (call-interactively (key-binding (vector last-input-event))))))
  10706. (defun org-force-self-insert (N)
  10707. "Needed to enforce self-insert under remapping."
  10708. (interactive "p")
  10709. (self-insert-command N))
  10710. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10711. "Regula expression matching exponentials as produced by calc.")
  10712. (defvar org-table-clean-did-remove-column nil)
  10713. (defun orgtbl-export (table target)
  10714. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10715. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10716. org-table-last-alignment org-table-last-column-widths
  10717. maxcol column)
  10718. (if (not (fboundp func))
  10719. (error "Cannot export orgtbl table to %s" target))
  10720. (setq lines (org-table-clean-before-export lines))
  10721. (setq table
  10722. (mapcar
  10723. (lambda (x)
  10724. (if (string-match org-table-hline-regexp x)
  10725. 'hline
  10726. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10727. lines))
  10728. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10729. table)))
  10730. (loop for i from (1- maxcol) downto 0 do
  10731. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10732. (setq column (delq nil column))
  10733. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10734. (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))
  10735. (funcall func table nil)))
  10736. (defun orgtbl-send-table (&optional maybe)
  10737. "Send a tranformed version of this table to the receiver position.
  10738. With argument MAYBE, fail quietly if no transformation is defined for
  10739. this table."
  10740. (interactive)
  10741. (catch 'exit
  10742. (unless (org-at-table-p) (error "Not at a table"))
  10743. ;; when non-interactive, we assume align has just happened.
  10744. (when (interactive-p) (org-table-align))
  10745. (save-excursion
  10746. (goto-char (org-table-begin))
  10747. (beginning-of-line 0)
  10748. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10749. (if maybe
  10750. (throw 'exit nil)
  10751. (error "Don't know how to transform this table."))))
  10752. (let* ((name (match-string 1))
  10753. beg
  10754. (transform (intern (match-string 2)))
  10755. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10756. (skip (plist-get params :skip))
  10757. (skipcols (plist-get params :skipcols))
  10758. (txt (buffer-substring-no-properties
  10759. (org-table-begin) (org-table-end)))
  10760. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10761. (lines (org-table-clean-before-export lines))
  10762. (i0 (if org-table-clean-did-remove-column 2 1))
  10763. (table (mapcar
  10764. (lambda (x)
  10765. (if (string-match org-table-hline-regexp x)
  10766. 'hline
  10767. (org-remove-by-index
  10768. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10769. skipcols i0)))
  10770. lines))
  10771. (fun (if (= i0 2) 'cdr 'identity))
  10772. (org-table-last-alignment
  10773. (org-remove-by-index (funcall fun org-table-last-alignment)
  10774. skipcols i0))
  10775. (org-table-last-column-widths
  10776. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10777. skipcols i0)))
  10778. (unless (fboundp transform)
  10779. (error "No such transformation function %s" transform))
  10780. (setq txt (funcall transform table params))
  10781. ;; Find the insertion place
  10782. (save-excursion
  10783. (goto-char (point-min))
  10784. (unless (re-search-forward
  10785. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10786. (error "Don't know where to insert translated table"))
  10787. (goto-char (match-beginning 0))
  10788. (beginning-of-line 2)
  10789. (setq beg (point))
  10790. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10791. (error "Cannot find end of insertion region"))
  10792. (beginning-of-line 1)
  10793. (delete-region beg (point))
  10794. (goto-char beg)
  10795. (insert txt "\n"))
  10796. (message "Table converted and installed at receiver location"))))
  10797. (defun org-remove-by-index (list indices &optional i0)
  10798. "Remove the elements in LIST with indices in INDICES.
  10799. First element has index 0, or I0 if given."
  10800. (if (not indices)
  10801. list
  10802. (if (integerp indices) (setq indices (list indices)))
  10803. (setq i0 (1- (or i0 0)))
  10804. (delq :rm (mapcar (lambda (x)
  10805. (setq i0 (1+ i0))
  10806. (if (memq i0 indices) :rm x))
  10807. list))))
  10808. (defun orgtbl-toggle-comment ()
  10809. "Comment or uncomment the orgtbl at point."
  10810. (interactive)
  10811. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10812. (re2 (concat "^" orgtbl-line-start-regexp))
  10813. (commented (save-excursion (beginning-of-line 1)
  10814. (cond ((looking-at re1) t)
  10815. ((looking-at re2) nil)
  10816. (t (error "Not at an org table")))))
  10817. (re (if commented re1 re2))
  10818. beg end)
  10819. (save-excursion
  10820. (beginning-of-line 1)
  10821. (while (looking-at re) (beginning-of-line 0))
  10822. (beginning-of-line 2)
  10823. (setq beg (point))
  10824. (while (looking-at re) (beginning-of-line 2))
  10825. (setq end (point)))
  10826. (comment-region beg end (if commented '(4) nil))))
  10827. (defun orgtbl-insert-radio-table ()
  10828. "Insert a radio table template appropriate for this major mode."
  10829. (interactive)
  10830. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10831. (txt (nth 1 e))
  10832. name pos)
  10833. (unless e (error "No radio table setup defined for %s" major-mode))
  10834. (setq name (read-string "Table name: "))
  10835. (while (string-match "%n" txt)
  10836. (setq txt (replace-match name t t txt)))
  10837. (or (bolp) (insert "\n"))
  10838. (setq pos (point))
  10839. (insert txt)
  10840. (goto-char pos)))
  10841. (defun org-get-param (params header i sym &optional hsym)
  10842. "Get parameter value for symbol SYM.
  10843. If this is a header line, actually get the value for the symbol with an
  10844. additional \"h\" inserted after the colon.
  10845. If the value is a protperty list, get the element for the current column.
  10846. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10847. (let ((val (plist-get params sym)))
  10848. (and hsym header (setq val (or (plist-get params hsym) val)))
  10849. (if (consp val) (plist-get val i) val)))
  10850. (defun orgtbl-to-generic (table params)
  10851. "Convert the orgtbl-mode TABLE to some other format.
  10852. This generic routine can be used for many standard cases.
  10853. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10854. separator line, or a list of fields for that line.
  10855. PARAMS is a property list of parameters that can influence the conversion.
  10856. For the generic converter, some parameters are obligatory: You need to
  10857. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10858. :splice, you must have :tstart and :tend.
  10859. Valid parameters are
  10860. :tstart String to start the table. Ignored when :splice is t.
  10861. :tend String to end the table. Ignored when :splice is t.
  10862. :splice When set to t, return only table body lines, don't wrap
  10863. them into :tstart and :tend. Default is nil.
  10864. :hline String to be inserted on horizontal separation lines.
  10865. May be nil to ignore hlines.
  10866. :lstart String to start a new table line.
  10867. :lend String to end a table line
  10868. :sep Separator between two fields
  10869. :lfmt Format for entire line, with enough %s to capture all fields.
  10870. If this is present, :lstart, :lend, and :sep are ignored.
  10871. :fmt A format to be used to wrap the field, should contain
  10872. %s for the original field value. For example, to wrap
  10873. everything in dollars, you could use :fmt \"$%s$\".
  10874. This may also be a property list with column numbers and
  10875. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10876. :hlstart :hlend :hlsep :hlfmt :hfmt
  10877. Same as above, specific for the header lines in the table.
  10878. All lines before the first hline are treated as header.
  10879. If any of these is not present, the data line value is used.
  10880. :efmt Use this format to print numbers with exponentials.
  10881. The format should have %s twice for inserting mantissa
  10882. and exponent, for example \"%s\\\\times10^{%s}\". This
  10883. may also be a property list with column numbers and
  10884. formats. :fmt will still be applied after :efmt.
  10885. In addition to this, the parameters :skip and :skipcols are always handled
  10886. directly by `orgtbl-send-table'. See manual."
  10887. (interactive)
  10888. (let* ((p params)
  10889. (splicep (plist-get p :splice))
  10890. (hline (plist-get p :hline))
  10891. rtn line i fm efm lfmt h)
  10892. ;; Do we have a header?
  10893. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10894. (setq h t))
  10895. ;; Put header
  10896. (unless splicep
  10897. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10898. ;; Now loop over all lines
  10899. (while (setq line (pop table))
  10900. (if (eq line 'hline)
  10901. ;; A horizontal separator line
  10902. (progn (if hline (push hline rtn))
  10903. (setq h nil)) ; no longer in header
  10904. ;; A normal line. Convert the fields, push line onto the result list
  10905. (setq i 0)
  10906. (setq line
  10907. (mapcar
  10908. (lambda (f)
  10909. (setq i (1+ i)
  10910. fm (org-get-param p h i :fmt :hfmt)
  10911. efm (org-get-param p h i :efmt))
  10912. (if (and efm (string-match orgtbl-exp-regexp f))
  10913. (setq f (format
  10914. efm (match-string 1 f) (match-string 2 f))))
  10915. (if fm (setq f (format fm f)))
  10916. f)
  10917. line))
  10918. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10919. (push (apply 'format lfmt line) rtn)
  10920. (push (concat
  10921. (org-get-param p h i :lstart :hlstart)
  10922. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10923. (org-get-param p h i :lend :hlend))
  10924. rtn))))
  10925. (unless splicep
  10926. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10927. (mapconcat 'identity (nreverse rtn) "\n")))
  10928. (defun orgtbl-to-latex (table params)
  10929. "Convert the orgtbl-mode TABLE to LaTeX.
  10930. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10931. separator line, or a list of fields for that line.
  10932. PARAMS is a property list of parameters that can influence the conversion.
  10933. Supports all parameters from `orgtbl-to-generic'. Most important for
  10934. LaTeX are:
  10935. :splice When set to t, return only table body lines, don't wrap
  10936. them into a tabular environment. Default is nil.
  10937. :fmt A format to be used to wrap the field, should contain %s for the
  10938. original field value. For example, to wrap everything in dollars,
  10939. use :fmt \"$%s$\". This may also be a property list with column
  10940. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10941. :efmt Format for transforming numbers with exponentials. The format
  10942. should have %s twice for inserting mantissa and exponent, for
  10943. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10944. This may also be a property list with column numbers and formats.
  10945. The general parameters :skip and :skipcols have already been applied when
  10946. this function is called."
  10947. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10948. org-table-last-alignment ""))
  10949. (params2
  10950. (list
  10951. :tstart (concat "\\begin{tabular}{" alignment "}")
  10952. :tend "\\end{tabular}"
  10953. :lstart "" :lend " \\\\" :sep " & "
  10954. :efmt "%s\\,(%s)" :hline "\\hline")))
  10955. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10956. (defun orgtbl-to-html (table params)
  10957. "Convert the orgtbl-mode TABLE to LaTeX.
  10958. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10959. separator line, or a list of fields for that line.
  10960. PARAMS is a property list of parameters that can influence the conversion.
  10961. Currently this function recognizes the following parameters:
  10962. :splice When set to t, return only table body lines, don't wrap
  10963. them into a <table> environment. Default is nil.
  10964. The general parameters :skip and :skipcols have already been applied when
  10965. this function is called. The function does *not* use `orgtbl-to-generic',
  10966. so you cannot specify parameters for it."
  10967. (let* ((splicep (plist-get params :splice))
  10968. html)
  10969. ;; Just call the formatter we already have
  10970. ;; We need to make text lines for it, so put the fields back together.
  10971. (setq html (org-format-org-table-html
  10972. (mapcar
  10973. (lambda (x)
  10974. (if (eq x 'hline)
  10975. "|----+----|"
  10976. (concat "| " (mapconcat 'identity x " | ") " |")))
  10977. table)
  10978. splicep))
  10979. (if (string-match "\n+\\'" html)
  10980. (setq html (replace-match "" t t html)))
  10981. html))
  10982. (defun orgtbl-to-texinfo (table params)
  10983. "Convert the orgtbl-mode TABLE to TeXInfo.
  10984. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10985. separator line, or a list of fields for that line.
  10986. PARAMS is a property list of parameters that can influence the conversion.
  10987. Supports all parameters from `orgtbl-to-generic'. Most important for
  10988. TeXInfo are:
  10989. :splice nil/t When set to t, return only table body lines, don't wrap
  10990. them into a multitable environment. Default is nil.
  10991. :fmt fmt A format to be used to wrap the field, should contain
  10992. %s for the original field value. For example, to wrap
  10993. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10994. This may also be a property list with column numbers and
  10995. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10996. :cf \"f1 f2..\" The column fractions for the table. By default these
  10997. are computed automatically from the width of the columns
  10998. under org-mode.
  10999. The general parameters :skip and :skipcols have already been applied when
  11000. this function is called."
  11001. (let* ((total (float (apply '+ org-table-last-column-widths)))
  11002. (colfrac (or (plist-get params :cf)
  11003. (mapconcat
  11004. (lambda (x) (format "%.3f" (/ (float x) total)))
  11005. org-table-last-column-widths " ")))
  11006. (params2
  11007. (list
  11008. :tstart (concat "@multitable @columnfractions " colfrac)
  11009. :tend "@end multitable"
  11010. :lstart "@item " :lend "" :sep " @tab "
  11011. :hlstart "@headitem ")))
  11012. (orgtbl-to-generic table (org-combine-plists params2 params))))
  11013. ;;;; Link Stuff
  11014. ;;; Link abbreviations
  11015. (defun org-link-expand-abbrev (link)
  11016. "Apply replacements as defined in `org-link-abbrev-alist."
  11017. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  11018. (let* ((key (match-string 1 link))
  11019. (as (or (assoc key org-link-abbrev-alist-local)
  11020. (assoc key org-link-abbrev-alist)))
  11021. (tag (and (match-end 2) (match-string 3 link)))
  11022. rpl)
  11023. (if (not as)
  11024. link
  11025. (setq rpl (cdr as))
  11026. (cond
  11027. ((symbolp rpl) (funcall rpl tag))
  11028. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  11029. (t (concat rpl tag)))))
  11030. link))
  11031. ;;; Storing and inserting links
  11032. (defvar org-insert-link-history nil
  11033. "Minibuffer history for links inserted with `org-insert-link'.")
  11034. (defvar org-stored-links nil
  11035. "Contains the links stored with `org-store-link'.")
  11036. (defvar org-store-link-plist nil
  11037. "Plist with info about the most recently link created with `org-store-link'.")
  11038. (defvar org-link-protocols nil
  11039. "Link protocols added to Org-mode using `org-add-link-type'.")
  11040. (defvar org-store-link-functions nil
  11041. "List of functions that are called to create and store a link.
  11042. Each function will be called in turn until one returns a non-nil
  11043. value. Each function should check if it is responsible for creating
  11044. this link (for example by looking at the major mode).
  11045. If not, it must exit and return nil.
  11046. If yes, it should return a non-nil value after a calling
  11047. `org-store-link-props' with a list of properties and values.
  11048. Special properties are:
  11049. :type The link prefix. like \"http\". This must be given.
  11050. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  11051. This is obligatory as well.
  11052. :description Optional default description for the second pair
  11053. of brackets in an Org-mode link. The user can still change
  11054. this when inserting this link into an Org-mode buffer.
  11055. In addition to these, any additional properties can be specified
  11056. and then used in remember templates.")
  11057. (defun org-add-link-type (type &optional follow publish)
  11058. "Add TYPE to the list of `org-link-types'.
  11059. Re-compute all regular expressions depending on `org-link-types'
  11060. FOLLOW and PUBLISH are two functions. Both take the link path as
  11061. an argument.
  11062. FOLLOW should do whatever is necessary to follow the link, for example
  11063. to find a file or display a mail message.
  11064. PUBLISH takes the path and retuns the string that should be used when
  11065. this document is published. FIMXE: This is actually not yet implemented."
  11066. (add-to-list 'org-link-types type t)
  11067. (org-make-link-regexps)
  11068. (add-to-list 'org-link-protocols
  11069. (list type follow publish)))
  11070. (defun org-add-agenda-custom-command (entry)
  11071. "Replace or add a command in `org-agenda-custom-commands'.
  11072. This is mostly for hacking and trying a new command - once the command
  11073. works you probably want to add it to `org-agenda-custom-commands' for good."
  11074. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  11075. (if ass
  11076. (setcdr ass (cdr entry))
  11077. (push entry org-agenda-custom-commands))))
  11078. ;;;###autoload
  11079. (defun org-store-link (arg)
  11080. "\\<org-mode-map>Store an org-link to the current location.
  11081. This link is added to `org-stored-links' and can later be inserted
  11082. into an org-buffer with \\[org-insert-link].
  11083. For some link types, a prefix arg is interpreted:
  11084. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  11085. For file links, arg negates `org-context-in-file-links'."
  11086. (interactive "P")
  11087. (setq org-store-link-plist nil) ; reset
  11088. (let (link cpltxt desc description search txt)
  11089. (cond
  11090. ((run-hook-with-args-until-success 'org-store-link-functions)
  11091. (setq link (plist-get org-store-link-plist :link)
  11092. desc (or (plist-get org-store-link-plist :description) link)))
  11093. ((eq major-mode 'bbdb-mode)
  11094. (let ((name (bbdb-record-name (bbdb-current-record)))
  11095. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  11096. (setq cpltxt (concat "bbdb:" (or name company))
  11097. link (org-make-link cpltxt))
  11098. (org-store-link-props :type "bbdb" :name name :company company)))
  11099. ((eq major-mode 'Info-mode)
  11100. (setq link (org-make-link "info:"
  11101. (file-name-nondirectory Info-current-file)
  11102. ":" Info-current-node))
  11103. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  11104. ":" Info-current-node))
  11105. (org-store-link-props :type "info" :file Info-current-file
  11106. :node Info-current-node))
  11107. ((eq major-mode 'calendar-mode)
  11108. (let ((cd (calendar-cursor-to-date)))
  11109. (setq link
  11110. (format-time-string
  11111. (car org-time-stamp-formats)
  11112. (apply 'encode-time
  11113. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11114. nil nil nil))))
  11115. (org-store-link-props :type "calendar" :date cd)))
  11116. ((or (eq major-mode 'vm-summary-mode)
  11117. (eq major-mode 'vm-presentation-mode))
  11118. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11119. (vm-follow-summary-cursor)
  11120. (save-excursion
  11121. (vm-select-folder-buffer)
  11122. (let* ((message (car vm-message-pointer))
  11123. (folder buffer-file-name)
  11124. (subject (vm-su-subject message))
  11125. (to (vm-get-header-contents message "To"))
  11126. (from (vm-get-header-contents message "From"))
  11127. (message-id (vm-su-message-id message)))
  11128. (org-store-link-props :type "vm" :from from :to to :subject subject
  11129. :message-id message-id)
  11130. (setq message-id (org-remove-angle-brackets message-id))
  11131. (setq folder (abbreviate-file-name folder))
  11132. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11133. folder)
  11134. (setq folder (replace-match "" t t folder)))
  11135. (setq cpltxt (org-email-link-description))
  11136. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11137. ((eq major-mode 'wl-summary-mode)
  11138. (let* ((msgnum (wl-summary-message-number))
  11139. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11140. msgnum 'message-id))
  11141. (wl-message-entity
  11142. (if (fboundp 'elmo-message-entity)
  11143. (elmo-message-entity
  11144. wl-summary-buffer-elmo-folder msgnum)
  11145. (elmo-msgdb-overview-get-entity
  11146. msgnum (wl-summary-buffer-msgdb))))
  11147. (from (wl-summary-line-from))
  11148. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11149. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11150. (wl-summary-line-subject))))
  11151. (org-store-link-props :type "wl" :from from :to to
  11152. :subject subject :message-id message-id)
  11153. (setq message-id (org-remove-angle-brackets message-id))
  11154. (setq cpltxt (org-email-link-description))
  11155. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11156. "#" message-id))))
  11157. ((or (equal major-mode 'mh-folder-mode)
  11158. (equal major-mode 'mh-show-mode))
  11159. (let ((from (org-mhe-get-header "From:"))
  11160. (to (org-mhe-get-header "To:"))
  11161. (message-id (org-mhe-get-header "Message-Id:"))
  11162. (subject (org-mhe-get-header "Subject:")))
  11163. (org-store-link-props :type "mh" :from from :to to
  11164. :subject subject :message-id message-id)
  11165. (setq cpltxt (org-email-link-description))
  11166. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11167. (org-remove-angle-brackets message-id)))))
  11168. ((or (eq major-mode 'rmail-mode)
  11169. (eq major-mode 'rmail-summary-mode))
  11170. (save-window-excursion
  11171. (save-restriction
  11172. (when (eq major-mode 'rmail-summary-mode)
  11173. (rmail-show-message rmail-current-message))
  11174. (rmail-narrow-to-non-pruned-header)
  11175. (let ((folder buffer-file-name)
  11176. (message-id (mail-fetch-field "message-id"))
  11177. (from (mail-fetch-field "from"))
  11178. (to (mail-fetch-field "to"))
  11179. (subject (mail-fetch-field "subject")))
  11180. (org-store-link-props
  11181. :type "rmail" :from from :to to
  11182. :subject subject :message-id message-id)
  11183. (setq message-id (org-remove-angle-brackets message-id))
  11184. (setq cpltxt (org-email-link-description))
  11185. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11186. (rmail-show-message rmail-current-message))))
  11187. ((eq major-mode 'gnus-group-mode)
  11188. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11189. (gnus-group-group-name)) ; version
  11190. ((fboundp 'gnus-group-name)
  11191. (gnus-group-name))
  11192. (t "???"))))
  11193. (unless group (error "Not on a group"))
  11194. (org-store-link-props :type "gnus" :group group)
  11195. (setq cpltxt (concat
  11196. (if (org-xor arg org-usenet-links-prefer-google)
  11197. "http://groups.google.com/groups?group="
  11198. "gnus:")
  11199. group)
  11200. link (org-make-link cpltxt))))
  11201. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11202. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11203. (let* ((group gnus-newsgroup-name)
  11204. (article (gnus-summary-article-number))
  11205. (header (gnus-summary-article-header article))
  11206. (from (mail-header-from header))
  11207. (message-id (mail-header-id header))
  11208. (date (mail-header-date header))
  11209. (subject (gnus-summary-subject-string)))
  11210. (org-store-link-props :type "gnus" :from from :subject subject
  11211. :message-id message-id :group group)
  11212. (setq cpltxt (org-email-link-description))
  11213. (if (org-xor arg org-usenet-links-prefer-google)
  11214. (setq link
  11215. (concat
  11216. cpltxt "\n "
  11217. (format "http://groups.google.com/groups?as_umsgid=%s"
  11218. (org-fixup-message-id-for-http message-id))))
  11219. (setq link (org-make-link "gnus:" group
  11220. "#" (number-to-string article))))))
  11221. ((eq major-mode 'w3-mode)
  11222. (setq cpltxt (url-view-url t)
  11223. link (org-make-link cpltxt))
  11224. (org-store-link-props :type "w3" :url (url-view-url t)))
  11225. ((eq major-mode 'w3m-mode)
  11226. (setq cpltxt (or w3m-current-title w3m-current-url)
  11227. link (org-make-link w3m-current-url))
  11228. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11229. ((setq search (run-hook-with-args-until-success
  11230. 'org-create-file-search-functions))
  11231. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11232. "::" search))
  11233. (setq cpltxt (or description link)))
  11234. ((eq major-mode 'image-mode)
  11235. (setq cpltxt (concat "file:"
  11236. (abbreviate-file-name buffer-file-name))
  11237. link (org-make-link cpltxt))
  11238. (org-store-link-props :type "image" :file buffer-file-name))
  11239. ((eq major-mode 'dired-mode)
  11240. ;; link to the file in the current line
  11241. (setq cpltxt (concat "file:"
  11242. (abbreviate-file-name
  11243. (expand-file-name
  11244. (dired-get-filename nil t))))
  11245. link (org-make-link cpltxt)))
  11246. ((and buffer-file-name (org-mode-p))
  11247. ;; Just link to current headline
  11248. (setq cpltxt (concat "file:"
  11249. (abbreviate-file-name buffer-file-name)))
  11250. ;; Add a context search string
  11251. (when (org-xor org-context-in-file-links arg)
  11252. ;; Check if we are on a target
  11253. (if (org-in-regexp "<<\\(.*?\\)>>")
  11254. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11255. (setq txt (cond
  11256. ((org-on-heading-p) nil)
  11257. ((org-region-active-p)
  11258. (buffer-substring (region-beginning) (region-end)))
  11259. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11260. (when (or (null txt) (string-match "\\S-" txt))
  11261. (setq cpltxt
  11262. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11263. desc "NONE"))))
  11264. (if (string-match "::\\'" cpltxt)
  11265. (setq cpltxt (substring cpltxt 0 -2)))
  11266. (setq link (org-make-link cpltxt)))
  11267. ((buffer-file-name (buffer-base-buffer))
  11268. ;; Just link to this file here.
  11269. (setq cpltxt (concat "file:"
  11270. (abbreviate-file-name
  11271. (buffer-file-name (buffer-base-buffer)))))
  11272. ;; Add a context string
  11273. (when (org-xor org-context-in-file-links arg)
  11274. (setq txt (if (org-region-active-p)
  11275. (buffer-substring (region-beginning) (region-end))
  11276. (buffer-substring (point-at-bol) (point-at-eol))))
  11277. ;; Only use search option if there is some text.
  11278. (when (string-match "\\S-" txt)
  11279. (setq cpltxt
  11280. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11281. desc "NONE")))
  11282. (setq link (org-make-link cpltxt)))
  11283. ((interactive-p)
  11284. (error "Cannot link to a buffer which is not visiting a file"))
  11285. (t (setq link nil)))
  11286. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11287. (setq link (or link cpltxt)
  11288. desc (or desc cpltxt))
  11289. (if (equal desc "NONE") (setq desc nil))
  11290. (if (and (interactive-p) link)
  11291. (progn
  11292. (setq org-stored-links
  11293. (cons (list link desc) org-stored-links))
  11294. (message "Stored: %s" (or desc link)))
  11295. (and link (org-make-link-string link desc)))))
  11296. (defun org-store-link-props (&rest plist)
  11297. "Store link properties, extract names and addresses."
  11298. (let (x adr)
  11299. (when (setq x (plist-get plist :from))
  11300. (setq adr (mail-extract-address-components x))
  11301. (plist-put plist :fromname (car adr))
  11302. (plist-put plist :fromaddress (nth 1 adr)))
  11303. (when (setq x (plist-get plist :to))
  11304. (setq adr (mail-extract-address-components x))
  11305. (plist-put plist :toname (car adr))
  11306. (plist-put plist :toaddress (nth 1 adr))))
  11307. (let ((from (plist-get plist :from))
  11308. (to (plist-get plist :to)))
  11309. (when (and from to org-from-is-user-regexp)
  11310. (plist-put plist :fromto
  11311. (if (string-match org-from-is-user-regexp from)
  11312. (concat "to %t")
  11313. (concat "from %f")))))
  11314. (setq org-store-link-plist plist))
  11315. (defun org-email-link-description (&optional fmt)
  11316. "Return the description part of an email link.
  11317. This takes information from `org-store-link-plist' and formats it
  11318. according to FMT (default from `org-email-link-description-format')."
  11319. (setq fmt (or fmt org-email-link-description-format))
  11320. (let* ((p org-store-link-plist)
  11321. (to (plist-get p :toaddress))
  11322. (from (plist-get p :fromaddress))
  11323. (table
  11324. (list
  11325. (cons "%c" (plist-get p :fromto))
  11326. (cons "%F" (plist-get p :from))
  11327. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11328. (cons "%T" (plist-get p :to))
  11329. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11330. (cons "%s" (plist-get p :subject))
  11331. (cons "%m" (plist-get p :message-id)))))
  11332. (when (string-match "%c" fmt)
  11333. ;; Check if the user wrote this message
  11334. (if (and org-from-is-user-regexp from to
  11335. (save-match-data (string-match org-from-is-user-regexp from)))
  11336. (setq fmt (replace-match "to %t" t t fmt))
  11337. (setq fmt (replace-match "from %f" t t fmt))))
  11338. (org-replace-escapes fmt table)))
  11339. (defun org-make-org-heading-search-string (&optional string heading)
  11340. "Make search string for STRING or current headline."
  11341. (interactive)
  11342. (let ((s (or string (org-get-heading))))
  11343. (unless (and string (not heading))
  11344. ;; We are using a headline, clean up garbage in there.
  11345. (if (string-match org-todo-regexp s)
  11346. (setq s (replace-match "" t t s)))
  11347. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11348. (setq s (replace-match "" t t s)))
  11349. (setq s (org-trim s))
  11350. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11351. org-comment-string "\\)") s)
  11352. (setq s (replace-match "" t t s)))
  11353. (while (string-match org-ts-regexp s)
  11354. (setq s (replace-match "" t t s))))
  11355. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11356. (setq s (replace-match " " t t s)))
  11357. (or string (setq s (concat "*" s))) ; Add * for headlines
  11358. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11359. (defun org-make-link (&rest strings)
  11360. "Concatenate STRINGS."
  11361. (apply 'concat strings))
  11362. (defun org-make-link-string (link &optional description)
  11363. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11364. (unless (string-match "\\S-" link)
  11365. (error "Empty link"))
  11366. (when (stringp description)
  11367. ;; Remove brackets from the description, they are fatal.
  11368. (while (string-match "\\[" description)
  11369. (setq description (replace-match "{" t t description)))
  11370. (while (string-match "\\]" description)
  11371. (setq description (replace-match "}" t t description))))
  11372. (when (equal (org-link-escape link) description)
  11373. ;; No description needed, it is identical
  11374. (setq description nil))
  11375. (when (and (not description)
  11376. (not (equal link (org-link-escape link))))
  11377. (setq description link))
  11378. (concat "[[" (org-link-escape link) "]"
  11379. (if description (concat "[" description "]") "")
  11380. "]"))
  11381. (defconst org-link-escape-chars
  11382. '((?\ . "%20")
  11383. (?\[ . "%5B")
  11384. (?\] . "%5D")
  11385. (?\340 . "%E0") ; `a
  11386. (?\342 . "%E2") ; ^a
  11387. (?\347 . "%E7") ; ,c
  11388. (?\350 . "%E8") ; `e
  11389. (?\351 . "%E9") ; 'e
  11390. (?\352 . "%EA") ; ^e
  11391. (?\356 . "%EE") ; ^i
  11392. (?\364 . "%F4") ; ^o
  11393. (?\371 . "%F9") ; `u
  11394. (?\373 . "%FB") ; ^u
  11395. (?\; . "%3B")
  11396. (?? . "%3F")
  11397. (?= . "%3D")
  11398. (?+ . "%2B")
  11399. )
  11400. "Association list of escapes for some characters problematic in links.
  11401. This is the list that is used for internal purposes.")
  11402. (defconst org-link-escape-chars-browser
  11403. '((?\ . "%20")) ; 32 for the SPC char
  11404. "Association list of escapes for some characters problematic in links.
  11405. This is the list that is used before handing over to the browser.")
  11406. (defun org-link-escape (text &optional table)
  11407. "Escape charaters in TEXT that are problematic for links."
  11408. (setq table (or table org-link-escape-chars))
  11409. (when text
  11410. (let ((re (mapconcat (lambda (x) (regexp-quote
  11411. (char-to-string (car x))))
  11412. table "\\|")))
  11413. (while (string-match re text)
  11414. (setq text
  11415. (replace-match
  11416. (cdr (assoc (string-to-char (match-string 0 text))
  11417. table))
  11418. t t text)))
  11419. text)))
  11420. (defun org-link-unescape (text &optional table)
  11421. "Reverse the action of `org-link-escape'."
  11422. (setq table (or table org-link-escape-chars))
  11423. (when text
  11424. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11425. table "\\|")))
  11426. (while (string-match re text)
  11427. (setq text
  11428. (replace-match
  11429. (char-to-string (car (rassoc (match-string 0 text) table)))
  11430. t t text)))
  11431. text)))
  11432. (defun org-xor (a b)
  11433. "Exclusive or."
  11434. (if a (not b) b))
  11435. (defun org-get-header (header)
  11436. "Find a header field in the current buffer."
  11437. (save-excursion
  11438. (goto-char (point-min))
  11439. (let ((case-fold-search t) s)
  11440. (cond
  11441. ((eq header 'from)
  11442. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11443. (setq s (match-string 1)))
  11444. (while (string-match "\"" s)
  11445. (setq s (replace-match "" t t s)))
  11446. (if (string-match "[<(].*" s)
  11447. (setq s (replace-match "" t t s))))
  11448. ((eq header 'message-id)
  11449. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11450. (setq s (match-string 1))))
  11451. ((eq header 'subject)
  11452. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11453. (setq s (match-string 1)))))
  11454. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11455. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11456. s)))
  11457. (defun org-fixup-message-id-for-http (s)
  11458. "Replace special characters in a message id, so it can be used in an http query."
  11459. (while (string-match "<" s)
  11460. (setq s (replace-match "%3C" t t s)))
  11461. (while (string-match ">" s)
  11462. (setq s (replace-match "%3E" t t s)))
  11463. (while (string-match "@" s)
  11464. (setq s (replace-match "%40" t t s)))
  11465. s)
  11466. ;;;###autoload
  11467. (defun org-insert-link-global ()
  11468. "Insert a link like Org-mode does.
  11469. This command can be called in any mode to insert a link in Org-mode syntax."
  11470. (interactive)
  11471. (org-run-like-in-org-mode 'org-insert-link))
  11472. (defun org-insert-link (&optional complete-file)
  11473. "Insert a link. At the prompt, enter the link.
  11474. Completion can be used to select a link previously stored with
  11475. `org-store-link'. When the empty string is entered (i.e. if you just
  11476. press RET at the prompt), the link defaults to the most recently
  11477. stored link. As SPC triggers completion in the minibuffer, you need to
  11478. use M-SPC or C-q SPC to force the insertion of a space character.
  11479. You will also be prompted for a description, and if one is given, it will
  11480. be displayed in the buffer instead of the link.
  11481. If there is already a link at point, this command will allow you to edit link
  11482. and description parts.
  11483. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11484. selected using completion. The path to the file will be relative to
  11485. the current directory if the file is in the current directory or a
  11486. subdirectory. Otherwise, the link will be the absolute path as
  11487. completed in the minibuffer (i.e. normally ~/path/to/file).
  11488. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11489. is in the current directory or below.
  11490. With three \\[universal-argument] prefixes, negate the meaning of
  11491. `org-keep-stored-link-after-insertion'."
  11492. (interactive "P")
  11493. (let* ((wcf (current-window-configuration))
  11494. (region (if (org-region-active-p)
  11495. (buffer-substring (region-beginning) (region-end))))
  11496. (remove (and region (list (region-beginning) (region-end))))
  11497. (desc region)
  11498. tmphist ; byte-compile incorrectly complains about this
  11499. link entry file)
  11500. (cond
  11501. ((org-in-regexp org-bracket-link-regexp 1)
  11502. ;; We do have a link at point, and we are going to edit it.
  11503. (setq remove (list (match-beginning 0) (match-end 0)))
  11504. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11505. (setq link (read-string "Link: "
  11506. (org-link-unescape
  11507. (org-match-string-no-properties 1)))))
  11508. ((or (org-in-regexp org-angle-link-re)
  11509. (org-in-regexp org-plain-link-re))
  11510. ;; Convert to bracket link
  11511. (setq remove (list (match-beginning 0) (match-end 0))
  11512. link (read-string "Link: "
  11513. (org-remove-angle-brackets (match-string 0)))))
  11514. ((equal complete-file '(4))
  11515. ;; Completing read for file names.
  11516. (setq file (read-file-name "File: "))
  11517. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11518. (pwd1 (file-name-as-directory (abbreviate-file-name
  11519. (expand-file-name ".")))))
  11520. (cond
  11521. ((equal complete-file '(16))
  11522. (setq link (org-make-link
  11523. "file:"
  11524. (abbreviate-file-name (expand-file-name file)))))
  11525. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11526. (setq link (org-make-link "file:" (match-string 1 file))))
  11527. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11528. (expand-file-name file))
  11529. (setq link (org-make-link
  11530. "file:" (match-string 1 (expand-file-name file)))))
  11531. (t (setq link (org-make-link "file:" file))))))
  11532. (t
  11533. ;; Read link, with completion for stored links.
  11534. (with-output-to-temp-buffer "*Org Links*"
  11535. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11536. (when org-stored-links
  11537. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11538. (princ (mapconcat
  11539. (lambda (x)
  11540. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11541. (reverse org-stored-links) "\n"))))
  11542. (let ((cw (selected-window)))
  11543. (select-window (get-buffer-window "*Org Links*"))
  11544. (shrink-window-if-larger-than-buffer)
  11545. (setq truncate-lines t)
  11546. (select-window cw))
  11547. ;; Fake a link history, containing the stored links.
  11548. (setq tmphist (append (mapcar 'car org-stored-links)
  11549. org-insert-link-history))
  11550. (unwind-protect
  11551. (setq link (org-completing-read
  11552. "Link: "
  11553. (append
  11554. (mapcar (lambda (x) (list (concat (car x) ":")))
  11555. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11556. (mapcar (lambda (x) (list (concat x ":")))
  11557. org-link-types))
  11558. nil nil nil
  11559. 'tmphist
  11560. (or (car (car org-stored-links)))))
  11561. (set-window-configuration wcf)
  11562. (kill-buffer "*Org Links*"))
  11563. (setq entry (assoc link org-stored-links))
  11564. (or entry (push link org-insert-link-history))
  11565. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11566. (not org-keep-stored-link-after-insertion))
  11567. (setq org-stored-links (delq (assoc link org-stored-links)
  11568. org-stored-links)))
  11569. (setq desc (or desc (nth 1 entry)))))
  11570. (if (string-match org-plain-link-re link)
  11571. ;; URL-like link, normalize the use of angular brackets.
  11572. (setq link (org-make-link (org-remove-angle-brackets link))))
  11573. ;; Check if we are linking to the current file with a search option
  11574. ;; If yes, simplify the link by using only the search option.
  11575. (when (and buffer-file-name
  11576. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11577. (let* ((path (match-string 1 link))
  11578. (case-fold-search nil)
  11579. (search (match-string 2 link)))
  11580. (save-match-data
  11581. (if (equal (file-truename buffer-file-name) (file-truename path))
  11582. ;; We are linking to this same file, with a search option
  11583. (setq link search)))))
  11584. ;; Check if we can/should use a relative path. If yes, simplify the link
  11585. (when (string-match "\\<file:\\(.*\\)" link)
  11586. (let* ((path (match-string 1 link))
  11587. (origpath path)
  11588. (case-fold-search nil))
  11589. (cond
  11590. ((eq org-link-file-path-type 'absolute)
  11591. (setq path (abbreviate-file-name (expand-file-name path))))
  11592. ((eq org-link-file-path-type 'noabbrev)
  11593. (setq path (expand-file-name path)))
  11594. ((eq org-link-file-path-type 'relative)
  11595. (setq path (file-relative-name path)))
  11596. (t
  11597. (save-match-data
  11598. (if (string-match (concat "^" (regexp-quote
  11599. (file-name-as-directory
  11600. (expand-file-name "."))))
  11601. (expand-file-name path))
  11602. ;; We are linking a file with relative path name.
  11603. (setq path (substring (expand-file-name path)
  11604. (match-end 0)))))))
  11605. (setq link (concat "file:" path))
  11606. (if (equal desc origpath)
  11607. (setq desc path))))
  11608. (setq desc (read-string "Description: " desc))
  11609. (unless (string-match "\\S-" desc) (setq desc nil))
  11610. (if remove (apply 'delete-region remove))
  11611. (insert (org-make-link-string link desc))))
  11612. (defun org-completing-read (&rest args)
  11613. (let ((minibuffer-local-completion-map
  11614. (copy-keymap minibuffer-local-completion-map)))
  11615. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11616. (apply 'completing-read args)))
  11617. ;;; Opening/following a link
  11618. (defvar org-link-search-failed nil)
  11619. (defun org-next-link ()
  11620. "Move forward to the next link.
  11621. If the link is in hidden text, expose it."
  11622. (interactive)
  11623. (when (and org-link-search-failed (eq this-command last-command))
  11624. (goto-char (point-min))
  11625. (message "Link search wrapped back to beginning of buffer"))
  11626. (setq org-link-search-failed nil)
  11627. (let* ((pos (point))
  11628. (ct (org-context))
  11629. (a (assoc :link ct)))
  11630. (if a (goto-char (nth 2 a)))
  11631. (if (re-search-forward org-any-link-re nil t)
  11632. (progn
  11633. (goto-char (match-beginning 0))
  11634. (if (org-invisible-p) (org-show-context)))
  11635. (goto-char pos)
  11636. (setq org-link-search-failed t)
  11637. (error "No further link found"))))
  11638. (defun org-previous-link ()
  11639. "Move backward to the previous link.
  11640. If the link is in hidden text, expose it."
  11641. (interactive)
  11642. (when (and org-link-search-failed (eq this-command last-command))
  11643. (goto-char (point-max))
  11644. (message "Link search wrapped back to end of buffer"))
  11645. (setq org-link-search-failed nil)
  11646. (let* ((pos (point))
  11647. (ct (org-context))
  11648. (a (assoc :link ct)))
  11649. (if a (goto-char (nth 1 a)))
  11650. (if (re-search-backward org-any-link-re nil t)
  11651. (progn
  11652. (goto-char (match-beginning 0))
  11653. (if (org-invisible-p) (org-show-context)))
  11654. (goto-char pos)
  11655. (setq org-link-search-failed t)
  11656. (error "No further link found"))))
  11657. (defun org-find-file-at-mouse (ev)
  11658. "Open file link or URL at mouse."
  11659. (interactive "e")
  11660. (mouse-set-point ev)
  11661. (org-open-at-point 'in-emacs))
  11662. (defun org-open-at-mouse (ev)
  11663. "Open file link or URL at mouse."
  11664. (interactive "e")
  11665. (mouse-set-point ev)
  11666. (org-open-at-point))
  11667. (defvar org-window-config-before-follow-link nil
  11668. "The window configuration before following a link.
  11669. This is saved in case the need arises to restore it.")
  11670. (defvar org-open-link-marker (make-marker)
  11671. "Marker pointing to the location where `org-open-at-point; was called.")
  11672. ;;;###autoload
  11673. (defun org-open-at-point-global ()
  11674. "Follow a link like Org-mode does.
  11675. This command can be called in any mode to follow a link that has
  11676. Org-mode syntax."
  11677. (interactive)
  11678. (org-run-like-in-org-mode 'org-open-at-point))
  11679. (defun org-open-at-point (&optional in-emacs)
  11680. "Open link at or after point.
  11681. If there is no link at point, this function will search forward up to
  11682. the end of the current subtree.
  11683. Normally, files will be opened by an appropriate application. If the
  11684. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11685. (interactive "P")
  11686. (move-marker org-open-link-marker (point))
  11687. (setq org-window-config-before-follow-link (current-window-configuration))
  11688. (org-remove-occur-highlights nil nil t)
  11689. (if (org-at-timestamp-p t)
  11690. (org-follow-timestamp-link)
  11691. (let (type path link line search (pos (point)))
  11692. (catch 'match
  11693. (save-excursion
  11694. (skip-chars-forward "^]\n\r")
  11695. (when (org-in-regexp org-bracket-link-regexp)
  11696. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11697. (while (string-match " *\n *" link)
  11698. (setq link (replace-match " " t t link)))
  11699. (setq link (org-link-expand-abbrev link))
  11700. (if (string-match org-link-re-with-space2 link)
  11701. (setq type (match-string 1 link) path (match-string 2 link))
  11702. (setq type "thisfile" path link))
  11703. (throw 'match t)))
  11704. (when (get-text-property (point) 'org-linked-text)
  11705. (setq type "thisfile"
  11706. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11707. (1+ (point)) (point))
  11708. path (buffer-substring
  11709. (previous-single-property-change pos 'org-linked-text)
  11710. (next-single-property-change pos 'org-linked-text)))
  11711. (throw 'match t))
  11712. (save-excursion
  11713. (when (or (org-in-regexp org-angle-link-re)
  11714. (org-in-regexp org-plain-link-re))
  11715. (setq type (match-string 1) path (match-string 2))
  11716. (throw 'match t)))
  11717. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11718. (setq type "tree-match"
  11719. path (match-string 1))
  11720. (throw 'match t))
  11721. (save-excursion
  11722. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11723. (setq type "tags"
  11724. path (match-string 1))
  11725. (while (string-match ":" path)
  11726. (setq path (replace-match "+" t t path)))
  11727. (throw 'match t))))
  11728. (unless path
  11729. (error "No link found"))
  11730. ;; Remove any trailing spaces in path
  11731. (if (string-match " +\\'" path)
  11732. (setq path (replace-match "" t t path)))
  11733. (cond
  11734. ((assoc type org-link-protocols)
  11735. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11736. ((equal type "mailto")
  11737. (let ((cmd (car org-link-mailto-program))
  11738. (args (cdr org-link-mailto-program)) args1
  11739. (address path) (subject "") a)
  11740. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11741. (setq address (match-string 1 path)
  11742. subject (org-link-escape (match-string 2 path))))
  11743. (while args
  11744. (cond
  11745. ((not (stringp (car args))) (push (pop args) args1))
  11746. (t (setq a (pop args))
  11747. (if (string-match "%a" a)
  11748. (setq a (replace-match address t t a)))
  11749. (if (string-match "%s" a)
  11750. (setq a (replace-match subject t t a)))
  11751. (push a args1))))
  11752. (apply cmd (nreverse args1))))
  11753. ((member type '("http" "https" "ftp" "news"))
  11754. (browse-url (concat type ":" (org-link-escape
  11755. path org-link-escape-chars-browser))))
  11756. ((member type '("message"))
  11757. (browse-url (concat type ":" path)))
  11758. ((string= type "tags")
  11759. (org-tags-view in-emacs path))
  11760. ((string= type "thisfile")
  11761. (if in-emacs
  11762. (switch-to-buffer-other-window
  11763. (org-get-buffer-for-internal-link (current-buffer)))
  11764. (org-mark-ring-push))
  11765. (let ((cmd `(org-link-search
  11766. ,path
  11767. ,(cond ((equal in-emacs '(4)) 'occur)
  11768. ((equal in-emacs '(16)) 'org-occur)
  11769. (t nil))
  11770. ,pos)))
  11771. (condition-case nil (eval cmd)
  11772. (error (progn (widen) (eval cmd))))))
  11773. ((string= type "tree-match")
  11774. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11775. ((string= type "file")
  11776. (if (string-match "::\\([0-9]+\\)\\'" path)
  11777. (setq line (string-to-number (match-string 1 path))
  11778. path (substring path 0 (match-beginning 0)))
  11779. (if (string-match "::\\(.+\\)\\'" path)
  11780. (setq search (match-string 1 path)
  11781. path (substring path 0 (match-beginning 0)))))
  11782. (if (string-match "[*?{]" (file-name-nondirectory path))
  11783. (dired path)
  11784. (org-open-file path in-emacs line search)))
  11785. ((string= type "news")
  11786. (org-follow-gnus-link path))
  11787. ((string= type "bbdb")
  11788. (org-follow-bbdb-link path))
  11789. ((string= type "info")
  11790. (org-follow-info-link path))
  11791. ((string= type "gnus")
  11792. (let (group article)
  11793. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11794. (error "Error in Gnus link"))
  11795. (setq group (match-string 1 path)
  11796. article (match-string 3 path))
  11797. (org-follow-gnus-link group article)))
  11798. ((string= type "vm")
  11799. (let (folder article)
  11800. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11801. (error "Error in VM link"))
  11802. (setq folder (match-string 1 path)
  11803. article (match-string 3 path))
  11804. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11805. (org-follow-vm-link folder article in-emacs)))
  11806. ((string= type "wl")
  11807. (let (folder article)
  11808. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11809. (error "Error in Wanderlust link"))
  11810. (setq folder (match-string 1 path)
  11811. article (match-string 3 path))
  11812. (org-follow-wl-link folder article)))
  11813. ((string= type "mhe")
  11814. (let (folder article)
  11815. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11816. (error "Error in MHE link"))
  11817. (setq folder (match-string 1 path)
  11818. article (match-string 3 path))
  11819. (org-follow-mhe-link folder article)))
  11820. ((string= type "rmail")
  11821. (let (folder article)
  11822. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11823. (error "Error in RMAIL link"))
  11824. (setq folder (match-string 1 path)
  11825. article (match-string 3 path))
  11826. (org-follow-rmail-link folder article)))
  11827. ((string= type "shell")
  11828. (let ((cmd path))
  11829. (if (or (not org-confirm-shell-link-function)
  11830. (funcall org-confirm-shell-link-function
  11831. (format "Execute \"%s\" in shell? "
  11832. (org-add-props cmd nil
  11833. 'face 'org-warning))))
  11834. (progn
  11835. (message "Executing %s" cmd)
  11836. (shell-command cmd))
  11837. (error "Abort"))))
  11838. ((string= type "elisp")
  11839. (let ((cmd path))
  11840. (if (or (not org-confirm-elisp-link-function)
  11841. (funcall org-confirm-elisp-link-function
  11842. (format "Execute \"%s\" as elisp? "
  11843. (org-add-props cmd nil
  11844. 'face 'org-warning))))
  11845. (message "%s => %s" cmd (eval (read cmd)))
  11846. (error "Abort"))))
  11847. (t
  11848. (browse-url-at-point)))))
  11849. (move-marker org-open-link-marker nil)
  11850. (run-hook-with-args 'org-follow-link-hook))
  11851. ;;; File search
  11852. (defvar org-create-file-search-functions nil
  11853. "List of functions to construct the right search string for a file link.
  11854. These functions are called in turn with point at the location to
  11855. which the link should point.
  11856. A function in the hook should first test if it would like to
  11857. handle this file type, for example by checking the major-mode or
  11858. the file extension. If it decides not to handle this file, it
  11859. should just return nil to give other functions a chance. If it
  11860. does handle the file, it must return the search string to be used
  11861. when following the link. The search string will be part of the
  11862. file link, given after a double colon, and `org-open-at-point'
  11863. will automatically search for it. If special measures must be
  11864. taken to make the search successful, another function should be
  11865. added to the companion hook `org-execute-file-search-functions',
  11866. which see.
  11867. A function in this hook may also use `setq' to set the variable
  11868. `description' to provide a suggestion for the descriptive text to
  11869. be used for this link when it gets inserted into an Org-mode
  11870. buffer with \\[org-insert-link].")
  11871. (defvar org-execute-file-search-functions nil
  11872. "List of functions to execute a file search triggered by a link.
  11873. Functions added to this hook must accept a single argument, the
  11874. search string that was part of the file link, the part after the
  11875. double colon. The function must first check if it would like to
  11876. handle this search, for example by checking the major-mode or the
  11877. file extension. If it decides not to handle this search, it
  11878. should just return nil to give other functions a chance. If it
  11879. does handle the search, it must return a non-nil value to keep
  11880. other functions from trying.
  11881. Each function can access the current prefix argument through the
  11882. variable `current-prefix-argument'. Note that a single prefix is
  11883. used to force opening a link in Emacs, so it may be good to only
  11884. use a numeric or double prefix to guide the search function.
  11885. In case this is needed, a function in this hook can also restore
  11886. the window configuration before `org-open-at-point' was called using:
  11887. (set-window-configuration org-window-config-before-follow-link)")
  11888. (defun org-link-search (s &optional type avoid-pos)
  11889. "Search for a link search option.
  11890. If S is surrounded by forward slashes, it is interpreted as a
  11891. regular expression. In org-mode files, this will create an `org-occur'
  11892. sparse tree. In ordinary files, `occur' will be used to list matches.
  11893. If the current buffer is in `dired-mode', grep will be used to search
  11894. in all files. If AVOID-POS is given, ignore matches near that position."
  11895. (let ((case-fold-search t)
  11896. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11897. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11898. (append '(("") (" ") ("\t") ("\n"))
  11899. org-emphasis-alist)
  11900. "\\|") "\\)"))
  11901. (pos (point))
  11902. (pre "") (post "")
  11903. words re0 re1 re2 re3 re4 re5 re2a reall)
  11904. (cond
  11905. ;; First check if there are any special
  11906. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11907. ;; Now try the builtin stuff
  11908. ((save-excursion
  11909. (goto-char (point-min))
  11910. (and
  11911. (re-search-forward
  11912. (concat "<<" (regexp-quote s0) ">>") nil t)
  11913. (setq pos (match-beginning 0))))
  11914. ;; There is an exact target for this
  11915. (goto-char pos))
  11916. ((string-match "^/\\(.*\\)/$" s)
  11917. ;; A regular expression
  11918. (cond
  11919. ((org-mode-p)
  11920. (org-occur (match-string 1 s)))
  11921. ;;((eq major-mode 'dired-mode)
  11922. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11923. (t (org-do-occur (match-string 1 s)))))
  11924. (t
  11925. ;; A normal search strings
  11926. (when (equal (string-to-char s) ?*)
  11927. ;; Anchor on headlines, post may include tags.
  11928. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11929. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11930. s (substring s 1)))
  11931. (remove-text-properties
  11932. 0 (length s)
  11933. '(face nil mouse-face nil keymap nil fontified nil) s)
  11934. ;; Make a series of regular expressions to find a match
  11935. (setq words (org-split-string s "[ \n\r\t]+")
  11936. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11937. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11938. "\\)" markers)
  11939. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11940. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11941. re1 (concat pre re2 post)
  11942. re3 (concat pre re4 post)
  11943. re5 (concat pre ".*" re4)
  11944. re2 (concat pre re2)
  11945. re2a (concat pre re2a)
  11946. re4 (concat pre re4)
  11947. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11948. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11949. re5 "\\)"
  11950. ))
  11951. (cond
  11952. ((eq type 'org-occur) (org-occur reall))
  11953. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11954. (t (goto-char (point-min))
  11955. (if (or (org-search-not-self 1 re0 nil t)
  11956. (org-search-not-self 1 re1 nil t)
  11957. (org-search-not-self 1 re2 nil t)
  11958. (org-search-not-self 1 re2a nil t)
  11959. (org-search-not-self 1 re3 nil t)
  11960. (org-search-not-self 1 re4 nil t)
  11961. (org-search-not-self 1 re5 nil t)
  11962. )
  11963. (goto-char (match-beginning 1))
  11964. (goto-char pos)
  11965. (error "No match")))))
  11966. (t
  11967. ;; Normal string-search
  11968. (goto-char (point-min))
  11969. (if (search-forward s nil t)
  11970. (goto-char (match-beginning 0))
  11971. (error "No match"))))
  11972. (and (org-mode-p) (org-show-context 'link-search))))
  11973. (defun org-search-not-self (group &rest args)
  11974. "Execute `re-search-forward', but only accept matches that do not
  11975. enclose the position of `org-open-link-marker'."
  11976. (let ((m org-open-link-marker))
  11977. (catch 'exit
  11978. (while (apply 're-search-forward args)
  11979. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11980. (goto-char (match-end group))
  11981. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11982. (> (match-beginning 0) (marker-position m))
  11983. (< (match-end 0) (marker-position m)))
  11984. (save-match-data
  11985. (or (not (org-in-regexp
  11986. org-bracket-link-analytic-regexp 1))
  11987. (not (match-end 4)) ; no description
  11988. (and (<= (match-beginning 4) (point))
  11989. (>= (match-end 4) (point))))))
  11990. (throw 'exit (point))))))))
  11991. (defun org-get-buffer-for-internal-link (buffer)
  11992. "Return a buffer to be used for displaying the link target of internal links."
  11993. (cond
  11994. ((not org-display-internal-link-with-indirect-buffer)
  11995. buffer)
  11996. ((string-match "(Clone)$" (buffer-name buffer))
  11997. (message "Buffer is already a clone, not making another one")
  11998. ;; we also do not modify visibility in this case
  11999. buffer)
  12000. (t ; make a new indirect buffer for displaying the link
  12001. (let* ((bn (buffer-name buffer))
  12002. (ibn (concat bn "(Clone)"))
  12003. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  12004. (with-current-buffer ib (org-overview))
  12005. ib))))
  12006. (defun org-do-occur (regexp &optional cleanup)
  12007. "Call the Emacs command `occur'.
  12008. If CLEANUP is non-nil, remove the printout of the regular expression
  12009. in the *Occur* buffer. This is useful if the regex is long and not useful
  12010. to read."
  12011. (occur regexp)
  12012. (when cleanup
  12013. (let ((cwin (selected-window)) win beg end)
  12014. (when (setq win (get-buffer-window "*Occur*"))
  12015. (select-window win))
  12016. (goto-char (point-min))
  12017. (when (re-search-forward "match[a-z]+" nil t)
  12018. (setq beg (match-end 0))
  12019. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  12020. (setq end (1- (match-beginning 0)))))
  12021. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  12022. (goto-char (point-min))
  12023. (select-window cwin))))
  12024. ;;; The mark ring for links jumps
  12025. (defvar org-mark-ring nil
  12026. "Mark ring for positions before jumps in Org-mode.")
  12027. (defvar org-mark-ring-last-goto nil
  12028. "Last position in the mark ring used to go back.")
  12029. ;; Fill and close the ring
  12030. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  12031. (loop for i from 1 to org-mark-ring-length do
  12032. (push (make-marker) org-mark-ring))
  12033. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  12034. org-mark-ring)
  12035. (defun org-mark-ring-push (&optional pos buffer)
  12036. "Put the current position or POS into the mark ring and rotate it."
  12037. (interactive)
  12038. (setq pos (or pos (point)))
  12039. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  12040. (move-marker (car org-mark-ring)
  12041. (or pos (point))
  12042. (or buffer (current-buffer)))
  12043. (message "%s"
  12044. (substitute-command-keys
  12045. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  12046. (defun org-mark-ring-goto (&optional n)
  12047. "Jump to the previous position in the mark ring.
  12048. With prefix arg N, jump back that many stored positions. When
  12049. called several times in succession, walk through the entire ring.
  12050. Org-mode commands jumping to a different position in the current file,
  12051. or to another Org-mode file, automatically push the old position
  12052. onto the ring."
  12053. (interactive "p")
  12054. (let (p m)
  12055. (if (eq last-command this-command)
  12056. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  12057. (setq p org-mark-ring))
  12058. (setq org-mark-ring-last-goto p)
  12059. (setq m (car p))
  12060. (switch-to-buffer (marker-buffer m))
  12061. (goto-char m)
  12062. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  12063. (defun org-remove-angle-brackets (s)
  12064. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  12065. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  12066. s)
  12067. (defun org-add-angle-brackets (s)
  12068. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  12069. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  12070. s)
  12071. ;;; Following specific links
  12072. (defun org-follow-timestamp-link ()
  12073. (cond
  12074. ((org-at-date-range-p t)
  12075. (let ((org-agenda-start-on-weekday)
  12076. (t1 (match-string 1))
  12077. (t2 (match-string 2)))
  12078. (setq t1 (time-to-days (org-time-string-to-time t1))
  12079. t2 (time-to-days (org-time-string-to-time t2)))
  12080. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  12081. ((org-at-timestamp-p t)
  12082. (org-agenda-list nil (time-to-days (org-time-string-to-time
  12083. (substring (match-string 1) 0 10)))
  12084. 1))
  12085. (t (error "This should not happen"))))
  12086. (defun org-follow-bbdb-link (name)
  12087. "Follow a BBDB link to NAME."
  12088. (require 'bbdb)
  12089. (let ((inhibit-redisplay (not debug-on-error))
  12090. (bbdb-electric-p nil))
  12091. (catch 'exit
  12092. ;; Exact match on name
  12093. (bbdb-name (concat "\\`" name "\\'") nil)
  12094. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12095. ;; Exact match on name
  12096. (bbdb-company (concat "\\`" name "\\'") nil)
  12097. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12098. ;; Partial match on name
  12099. (bbdb-name name nil)
  12100. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12101. ;; Partial match on company
  12102. (bbdb-company name nil)
  12103. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12104. ;; General match including network address and notes
  12105. (bbdb name nil)
  12106. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  12107. (delete-window (get-buffer-window "*BBDB*"))
  12108. (error "No matching BBDB record")))))
  12109. (defun org-follow-info-link (name)
  12110. "Follow an info file & node link to NAME."
  12111. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12112. (string-match "\\(.*\\)" name))
  12113. (progn
  12114. (require 'info)
  12115. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12116. (Info-find-node (match-string 1 name) (match-string 2 name))
  12117. (Info-find-node (match-string 1 name) "Top")))
  12118. (message "Could not open: %s" name)))
  12119. (defun org-follow-gnus-link (&optional group article)
  12120. "Follow a Gnus link to GROUP and ARTICLE."
  12121. (require 'gnus)
  12122. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12123. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12124. (cond ((and group article)
  12125. (gnus-group-read-group 1 nil group)
  12126. (gnus-summary-goto-article (string-to-number article) nil t))
  12127. (group (gnus-group-jump-to-group group))))
  12128. (defun org-follow-vm-link (&optional folder article readonly)
  12129. "Follow a VM link to FOLDER and ARTICLE."
  12130. (require 'vm)
  12131. (setq article (org-add-angle-brackets article))
  12132. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12133. ;; ange-ftp or efs or tramp access
  12134. (let ((user (or (match-string 1 folder) (user-login-name)))
  12135. (host (match-string 2 folder))
  12136. (file (match-string 3 folder)))
  12137. (cond
  12138. ((featurep 'tramp)
  12139. ;; use tramp to access the file
  12140. (if (featurep 'xemacs)
  12141. (setq folder (format "[%s@%s]%s" user host file))
  12142. (setq folder (format "/%s@%s:%s" user host file))))
  12143. (t
  12144. ;; use ange-ftp or efs
  12145. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12146. (setq folder (format "/%s@%s:%s" user host file))))))
  12147. (when folder
  12148. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12149. (sit-for 0.1)
  12150. (when article
  12151. (vm-select-folder-buffer)
  12152. (widen)
  12153. (let ((case-fold-search t))
  12154. (goto-char (point-min))
  12155. (if (not (re-search-forward
  12156. (concat "^" "message-id: *" (regexp-quote article))))
  12157. (error "Could not find the specified message in this folder"))
  12158. (vm-isearch-update)
  12159. (vm-isearch-narrow)
  12160. (vm-beginning-of-message)
  12161. (vm-summarize)))))
  12162. (defun org-follow-wl-link (folder article)
  12163. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12164. (if (and (string= folder "%")
  12165. article
  12166. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12167. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12168. ;; Thus, we recompose folder and article ids.
  12169. (setq folder (format "%s#%s" folder (match-string 1 article))
  12170. article (match-string 3 article)))
  12171. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12172. (error "No such folder: %s" folder))
  12173. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12174. (and article
  12175. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12176. (wl-summary-redisplay)))
  12177. (defun org-follow-rmail-link (folder article)
  12178. "Follow an RMAIL link to FOLDER and ARTICLE."
  12179. (setq article (org-add-angle-brackets article))
  12180. (let (message-number)
  12181. (save-excursion
  12182. (save-window-excursion
  12183. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12184. (setq message-number
  12185. (save-restriction
  12186. (widen)
  12187. (goto-char (point-max))
  12188. (if (re-search-backward
  12189. (concat "^Message-ID:\\s-+" (regexp-quote
  12190. (or article "")))
  12191. nil t)
  12192. (rmail-what-message))))))
  12193. (if message-number
  12194. (progn
  12195. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12196. (rmail-show-message message-number)
  12197. message-number)
  12198. (error "Message not found"))))
  12199. ;;; mh-e integration based on planner-mode
  12200. (defun org-mhe-get-message-real-folder ()
  12201. "Return the name of the current message real folder, so if you use
  12202. sequences, it will now work."
  12203. (save-excursion
  12204. (let* ((folder
  12205. (if (equal major-mode 'mh-folder-mode)
  12206. mh-current-folder
  12207. ;; Refer to the show buffer
  12208. mh-show-folder-buffer))
  12209. (end-index
  12210. (if (boundp 'mh-index-folder)
  12211. (min (length mh-index-folder) (length folder))))
  12212. )
  12213. ;; a simple test on mh-index-data does not work, because
  12214. ;; mh-index-data is always nil in a show buffer.
  12215. (if (and (boundp 'mh-index-folder)
  12216. (string= mh-index-folder (substring folder 0 end-index)))
  12217. (if (equal major-mode 'mh-show-mode)
  12218. (save-window-excursion
  12219. (let (pop-up-frames)
  12220. (when (buffer-live-p (get-buffer folder))
  12221. (progn
  12222. (pop-to-buffer folder)
  12223. (org-mhe-get-message-folder-from-index)
  12224. )
  12225. )))
  12226. (org-mhe-get-message-folder-from-index)
  12227. )
  12228. folder
  12229. )
  12230. )))
  12231. (defun org-mhe-get-message-folder-from-index ()
  12232. "Returns the name of the message folder in a index folder buffer."
  12233. (save-excursion
  12234. (mh-index-previous-folder)
  12235. (re-search-forward "^\\(+.*\\)$" nil t)
  12236. (message "%s" (match-string 1))))
  12237. (defun org-mhe-get-message-folder ()
  12238. "Return the name of the current message folder. Be careful if you
  12239. use sequences."
  12240. (save-excursion
  12241. (if (equal major-mode 'mh-folder-mode)
  12242. mh-current-folder
  12243. ;; Refer to the show buffer
  12244. mh-show-folder-buffer)))
  12245. (defun org-mhe-get-message-num ()
  12246. "Return the number of the current message. Be careful if you
  12247. use sequences."
  12248. (save-excursion
  12249. (if (equal major-mode 'mh-folder-mode)
  12250. (mh-get-msg-num nil)
  12251. ;; Refer to the show buffer
  12252. (mh-show-buffer-message-number))))
  12253. (defun org-mhe-get-header (header)
  12254. "Return a header of the message in folder mode. This will create a
  12255. show buffer for the corresponding message. If you have a more clever
  12256. idea..."
  12257. (let* ((folder (org-mhe-get-message-folder))
  12258. (num (org-mhe-get-message-num))
  12259. (buffer (get-buffer-create (concat "show-" folder)))
  12260. (header-field))
  12261. (with-current-buffer buffer
  12262. (mh-display-msg num folder)
  12263. (if (equal major-mode 'mh-folder-mode)
  12264. (mh-header-display)
  12265. (mh-show-header-display))
  12266. (set-buffer buffer)
  12267. (setq header-field (mh-get-header-field header))
  12268. (if (equal major-mode 'mh-folder-mode)
  12269. (mh-show)
  12270. (mh-show-show))
  12271. header-field)))
  12272. (defun org-follow-mhe-link (folder article)
  12273. "Follow an MHE link to FOLDER and ARTICLE.
  12274. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12275. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12276. ARTICLE is searched in all folders. Indexed searches (swish++,
  12277. namazu, and others supported by MH-E) will always search in all
  12278. folders."
  12279. (require 'mh-e)
  12280. (require 'mh-search)
  12281. (require 'mh-utils)
  12282. (mh-find-path)
  12283. (if (not article)
  12284. (mh-visit-folder (mh-normalize-folder-name folder))
  12285. (setq article (org-add-angle-brackets article))
  12286. (mh-search-choose)
  12287. (if (equal mh-searcher 'pick)
  12288. (progn
  12289. (mh-search folder (list "--message-id" article))
  12290. (when (and org-mhe-search-all-folders
  12291. (not (org-mhe-get-message-real-folder)))
  12292. (kill-this-buffer)
  12293. (mh-search "+" (list "--message-id" article))))
  12294. (mh-search "+" article))
  12295. (if (org-mhe-get-message-real-folder)
  12296. (mh-show-msg 1)
  12297. (kill-this-buffer)
  12298. (error "Message not found"))))
  12299. ;;; BibTeX links
  12300. ;; Use the custom search meachnism to construct and use search strings for
  12301. ;; file links to BibTeX database entries.
  12302. (defun org-create-file-search-in-bibtex ()
  12303. "Create the search string and description for a BibTeX database entry."
  12304. (when (eq major-mode 'bibtex-mode)
  12305. ;; yes, we want to construct this search string.
  12306. ;; Make a good description for this entry, using names, year and the title
  12307. ;; Put it into the `description' variable which is dynamically scoped.
  12308. (let ((bibtex-autokey-names 1)
  12309. (bibtex-autokey-names-stretch 1)
  12310. (bibtex-autokey-name-case-convert-function 'identity)
  12311. (bibtex-autokey-name-separator " & ")
  12312. (bibtex-autokey-additional-names " et al.")
  12313. (bibtex-autokey-year-length 4)
  12314. (bibtex-autokey-name-year-separator " ")
  12315. (bibtex-autokey-titlewords 3)
  12316. (bibtex-autokey-titleword-separator " ")
  12317. (bibtex-autokey-titleword-case-convert-function 'identity)
  12318. (bibtex-autokey-titleword-length 'infty)
  12319. (bibtex-autokey-year-title-separator ": "))
  12320. (setq description (bibtex-generate-autokey)))
  12321. ;; Now parse the entry, get the key and return it.
  12322. (save-excursion
  12323. (bibtex-beginning-of-entry)
  12324. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12325. (defun org-execute-file-search-in-bibtex (s)
  12326. "Find the link search string S as a key for a database entry."
  12327. (when (eq major-mode 'bibtex-mode)
  12328. ;; Yes, we want to do the search in this file.
  12329. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12330. (goto-char (point-min))
  12331. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12332. (regexp-quote s) "[ \t\n]*,") nil t)
  12333. (goto-char (match-beginning 0)))
  12334. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12335. ;; Use double prefix to indicate that any web link should be browsed
  12336. (let ((b (current-buffer)) (p (point)))
  12337. ;; Restore the window configuration because we just use the web link
  12338. (set-window-configuration org-window-config-before-follow-link)
  12339. (save-excursion (set-buffer b) (goto-char p)
  12340. (bibtex-url)))
  12341. (recenter 0)) ; Move entry start to beginning of window
  12342. ;; return t to indicate that the search is done.
  12343. t))
  12344. ;; Finally add the functions to the right hooks.
  12345. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12346. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12347. ;; end of Bibtex link setup
  12348. ;;; Following file links
  12349. (defun org-open-file (path &optional in-emacs line search)
  12350. "Open the file at PATH.
  12351. First, this expands any special file name abbreviations. Then the
  12352. configuration variable `org-file-apps' is checked if it contains an
  12353. entry for this file type, and if yes, the corresponding command is launched.
  12354. If no application is found, Emacs simply visits the file.
  12355. With optional argument IN-EMACS, Emacs will visit the file.
  12356. Optional LINE specifies a line to go to, optional SEARCH a string to
  12357. search for. If LINE or SEARCH is given, the file will always be
  12358. opened in Emacs.
  12359. If the file does not exist, an error is thrown."
  12360. (setq in-emacs (or in-emacs line search))
  12361. (let* ((file (if (equal path "")
  12362. buffer-file-name
  12363. (substitute-in-file-name (expand-file-name path))))
  12364. (apps (append org-file-apps (org-default-apps)))
  12365. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12366. (dirp (if remp nil (file-directory-p file)))
  12367. (dfile (downcase file))
  12368. (old-buffer (current-buffer))
  12369. (old-pos (point))
  12370. (old-mode major-mode)
  12371. ext cmd)
  12372. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12373. (setq ext (match-string 1 dfile))
  12374. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12375. (setq ext (match-string 1 dfile))))
  12376. (if in-emacs
  12377. (setq cmd 'emacs)
  12378. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12379. (and dirp (cdr (assoc 'directory apps)))
  12380. (cdr (assoc ext apps))
  12381. (cdr (assoc t apps)))))
  12382. (when (eq cmd 'mailcap)
  12383. (require 'mailcap)
  12384. (mailcap-parse-mailcaps)
  12385. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12386. (command (mailcap-mime-info mime-type)))
  12387. (if (stringp command)
  12388. (setq cmd command)
  12389. (setq cmd 'emacs))))
  12390. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12391. (not (file-exists-p file))
  12392. (not org-open-non-existing-files))
  12393. (error "No such file: %s" file))
  12394. (cond
  12395. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12396. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12397. (while (string-match "['\"]%s['\"]" cmd)
  12398. (setq cmd (replace-match "%s" t t cmd)))
  12399. (while (string-match "%s" cmd)
  12400. (setq cmd (replace-match
  12401. (save-match-data (shell-quote-argument file))
  12402. t t cmd)))
  12403. (save-window-excursion
  12404. (start-process-shell-command cmd nil cmd)))
  12405. ((or (stringp cmd)
  12406. (eq cmd 'emacs))
  12407. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12408. (widen)
  12409. (if line (goto-line line)
  12410. (if search (org-link-search search))))
  12411. ((consp cmd)
  12412. (eval cmd))
  12413. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12414. (and (org-mode-p) (eq old-mode 'org-mode)
  12415. (or (not (equal old-buffer (current-buffer)))
  12416. (not (equal old-pos (point))))
  12417. (org-mark-ring-push old-pos old-buffer))))
  12418. (defun org-default-apps ()
  12419. "Return the default applications for this operating system."
  12420. (cond
  12421. ((eq system-type 'darwin)
  12422. org-file-apps-defaults-macosx)
  12423. ((eq system-type 'windows-nt)
  12424. org-file-apps-defaults-windowsnt)
  12425. (t org-file-apps-defaults-gnu)))
  12426. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12427. (defun org-file-remote-p (file)
  12428. "Test whether FILE specifies a location on a remote system.
  12429. Return non-nil if the location is indeed remote.
  12430. For example, the filename \"/user@host:/foo\" specifies a location
  12431. on the system \"/user@host:\"."
  12432. (cond ((fboundp 'file-remote-p)
  12433. (file-remote-p file))
  12434. ((fboundp 'tramp-handle-file-remote-p)
  12435. (tramp-handle-file-remote-p file))
  12436. ((and (boundp 'ange-ftp-name-format)
  12437. (string-match (car ange-ftp-name-format) file))
  12438. t)
  12439. (t nil)))
  12440. ;;;; Hooks for remember.el, and refiling
  12441. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12442. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12443. ;;;###autoload
  12444. (defun org-remember-insinuate ()
  12445. "Setup remember.el for use wiht Org-mode."
  12446. (require 'remember)
  12447. (setq remember-annotation-functions '(org-remember-annotation))
  12448. (setq remember-handler-functions '(org-remember-handler))
  12449. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12450. ;;;###autoload
  12451. (defun org-remember-annotation ()
  12452. "Return a link to the current location as an annotation for remember.el.
  12453. If you are using Org-mode files as target for data storage with
  12454. remember.el, then the annotations should include a link compatible with the
  12455. conventions in Org-mode. This function returns such a link."
  12456. (org-store-link nil))
  12457. (defconst org-remember-help
  12458. "Select a destination location for the note.
  12459. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12460. RET on headline -> Store as sublevel entry to current headline
  12461. RET at beg-of-buf -> Append to file as level 2 headline
  12462. <left>/<right> -> before/after current headline, same headings level")
  12463. (defvar org-remember-previous-location nil)
  12464. (defvar org-force-remember-template-char) ;; dynamically scoped
  12465. ;; Save the major mode of the buffer we called remember from
  12466. (defvar org-select-template-temp-major-mode nil)
  12467. ;; Temporary store the buffer where remember was called from
  12468. (defvar org-select-template-original-buffer nil)
  12469. (defun org-select-remember-template (&optional use-char)
  12470. (when org-remember-templates
  12471. (let* ((pre-selected-templates
  12472. (mapcar
  12473. (lambda (tpl)
  12474. (let ((ctxt (nth 5 tpl))
  12475. (mode org-select-template-temp-major-mode)
  12476. (buf org-select-template-original-buffer))
  12477. (if (or (and (functionp ctxt)
  12478. (save-excursion
  12479. (set-buffer buf)
  12480. ;; Protect the user-defined function from error
  12481. (condition-case nil (funcall ctxt) (error nil))))
  12482. (and ctxt (listp ctxt)
  12483. (delq nil (mapcar (lambda(x) (eq mode x)) ctxt))))
  12484. tpl)))
  12485. org-remember-templates))
  12486. ;; If no template at this point, add the default templates:
  12487. (pre-selected-templates1
  12488. (if (not (delq nil pre-selected-templates))
  12489. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  12490. org-remember-templates)
  12491. pre-selected-templates))
  12492. ;; Then unconditionnally add template for any contexts
  12493. (pre-selected-templates2
  12494. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  12495. org-remember-templates)
  12496. (delq nil pre-selected-templates1)))
  12497. (templates (mapcar (lambda (x)
  12498. (if (stringp (car x))
  12499. (append (list (nth 1 x) (car x)) (cddr x))
  12500. (append (list (car x) "") (cdr x))))
  12501. (delq nil pre-selected-templates2)))
  12502. (char (or use-char
  12503. (cond
  12504. ((= (length templates) 1)
  12505. (caar templates))
  12506. ((and (boundp 'org-force-remember-template-char)
  12507. org-force-remember-template-char)
  12508. (if (stringp org-force-remember-template-char)
  12509. (string-to-char org-force-remember-template-char)
  12510. org-force-remember-template-char))
  12511. (t
  12512. (message "Select template: %s"
  12513. (mapconcat
  12514. (lambda (x)
  12515. (cond
  12516. ((not (string-match "\\S-" (nth 1 x)))
  12517. (format "[%c]" (car x)))
  12518. ((equal (downcase (car x))
  12519. (downcase (aref (nth 1 x) 0)))
  12520. (format "[%c]%s" (car x)
  12521. (substring (nth 1 x) 1)))
  12522. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12523. templates " "))
  12524. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12525. (when (equal char0 ?\C-g)
  12526. (jump-to-register remember-register)
  12527. (kill-buffer remember-buffer))
  12528. char0))))))
  12529. (cddr (assoc char templates)))))
  12530. (defvar x-last-selected-text)
  12531. (defvar x-last-selected-text-primary)
  12532. ;;;###autoload
  12533. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12534. "Initialize *remember* buffer with template, invoke `org-mode'.
  12535. This function should be placed into `remember-mode-hook' and in fact requires
  12536. to be run from that hook to function properly."
  12537. (if org-remember-templates
  12538. (let* ((entry (org-select-remember-template use-char))
  12539. (tpl (car entry))
  12540. (plist-p (if org-store-link-plist t nil))
  12541. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12542. (string-match "\\S-" (nth 1 entry)))
  12543. (nth 1 entry)
  12544. org-default-notes-file))
  12545. (headline (nth 2 entry))
  12546. (v-c (or (and (eq window-system 'x)
  12547. (fboundp 'x-cut-buffer-or-selection-value)
  12548. (x-cut-buffer-or-selection-value))
  12549. (org-bound-and-true-p x-last-selected-text)
  12550. (org-bound-and-true-p x-last-selected-text-primary)
  12551. (and (> (length kill-ring) 0) (current-kill 0))))
  12552. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12553. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12554. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12555. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12556. ;; `initial' and `annotation' are bound in `remember'
  12557. (v-i (if (boundp 'initial) initial))
  12558. (v-a (if (and (boundp 'annotation) annotation)
  12559. (if (equal annotation "[[]]") "" annotation)
  12560. ""))
  12561. (v-A (if (and v-a
  12562. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12563. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12564. v-a))
  12565. (v-n user-full-name)
  12566. (org-startup-folded nil)
  12567. org-time-was-given org-end-time-was-given x
  12568. prompt completions char time pos default histvar)
  12569. (setq org-store-link-plist
  12570. (append (list :annotation v-a :initial v-i)
  12571. org-store-link-plist))
  12572. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12573. (erase-buffer)
  12574. (insert (substitute-command-keys
  12575. (format
  12576. "## Filing location: Select interactively, default, or last used:
  12577. ## %s to select file and header location interactively.
  12578. ## %s \"%s\" -> \"* %s\"
  12579. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12580. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12581. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12582. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12583. (abbreviate-file-name (or file org-default-notes-file))
  12584. (or headline "")
  12585. (or (car org-remember-previous-location) "???")
  12586. (or (cdr org-remember-previous-location) "???"))))
  12587. (insert tpl) (goto-char (point-min))
  12588. ;; Simple %-escapes
  12589. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12590. (when (and initial (equal (match-string 0) "%i"))
  12591. (save-match-data
  12592. (let* ((lead (buffer-substring
  12593. (point-at-bol) (match-beginning 0))))
  12594. (setq v-i (mapconcat 'identity
  12595. (org-split-string initial "\n")
  12596. (concat "\n" lead))))))
  12597. (replace-match
  12598. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12599. t t))
  12600. ;; %[] Insert contents of a file.
  12601. (goto-char (point-min))
  12602. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12603. (let ((start (match-beginning 0))
  12604. (end (match-end 0))
  12605. (filename (expand-file-name (match-string 1))))
  12606. (goto-char start)
  12607. (delete-region start end)
  12608. (condition-case error
  12609. (insert-file-contents filename)
  12610. (error (insert (format "%%![Couldn't insert %s: %s]"
  12611. filename error))))))
  12612. ;; %() embedded elisp
  12613. (goto-char (point-min))
  12614. (while (re-search-forward "%\\((.+)\\)" nil t)
  12615. (goto-char (match-beginning 0))
  12616. (let ((template-start (point)))
  12617. (forward-char 1)
  12618. (let ((result
  12619. (condition-case error
  12620. (eval (read (current-buffer)))
  12621. (error (format "%%![Error: %s]" error)))))
  12622. (delete-region template-start (point))
  12623. (insert result))))
  12624. ;; From the property list
  12625. (when plist-p
  12626. (goto-char (point-min))
  12627. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12628. (and (setq x (or (plist-get org-store-link-plist
  12629. (intern (match-string 1))) ""))
  12630. (replace-match x t t))))
  12631. ;; Turn on org-mode in the remember buffer, set local variables
  12632. (org-mode)
  12633. (org-set-local 'org-finish-function 'org-remember-finalize)
  12634. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12635. (org-set-local 'org-default-notes-file file))
  12636. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12637. (org-set-local 'org-remember-default-headline headline))
  12638. ;; Interactive template entries
  12639. (goto-char (point-min))
  12640. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12641. (setq char (if (match-end 3) (match-string 3))
  12642. prompt (if (match-end 2) (match-string 2)))
  12643. (goto-char (match-beginning 0))
  12644. (replace-match "")
  12645. (setq completions nil default nil)
  12646. (when prompt
  12647. (setq completions (org-split-string prompt "|")
  12648. prompt (pop completions)
  12649. default (car completions)
  12650. histvar (intern (concat
  12651. "org-remember-template-prompt-history::"
  12652. (or prompt "")))
  12653. completions (mapcar 'list completions)))
  12654. (cond
  12655. ((member char '("G" "g"))
  12656. (let* ((org-last-tags-completion-table
  12657. (org-global-tags-completion-table
  12658. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12659. (org-add-colon-after-tag-completion t)
  12660. (ins (completing-read
  12661. (if prompt (concat prompt ": ") "Tags: ")
  12662. 'org-tags-completion-function nil nil nil
  12663. 'org-tags-history)))
  12664. (setq ins (mapconcat 'identity
  12665. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12666. ":"))
  12667. (when (string-match "\\S-" ins)
  12668. (or (equal (char-before) ?:) (insert ":"))
  12669. (insert ins)
  12670. (or (equal (char-after) ?:) (insert ":")))))
  12671. (char
  12672. (setq org-time-was-given (equal (upcase char) char))
  12673. (setq time (org-read-date (equal (upcase char) "U") t nil
  12674. prompt))
  12675. (org-insert-time-stamp time org-time-was-given
  12676. (member char '("u" "U"))
  12677. nil nil (list org-end-time-was-given)))
  12678. (t
  12679. (insert (org-completing-read
  12680. (concat (if prompt prompt "Enter string")
  12681. (if default (concat " [" default "]"))
  12682. ": ")
  12683. completions nil nil nil histvar default)))))
  12684. (goto-char (point-min))
  12685. (if (re-search-forward "%\\?" nil t)
  12686. (replace-match "")
  12687. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12688. (org-mode)
  12689. (org-set-local 'org-finish-function 'org-remember-finalize))
  12690. (when (save-excursion
  12691. (goto-char (point-min))
  12692. (re-search-forward "%!" nil t))
  12693. (replace-match "")
  12694. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12695. (defun org-remember-finish-immediately ()
  12696. "File remember note immediately.
  12697. This should be run in `post-command-hook' and will remove itself
  12698. from that hook."
  12699. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12700. (when org-finish-function
  12701. (funcall org-finish-function)))
  12702. (defvar org-clock-marker) ; Defined below
  12703. (defun org-remember-finalize ()
  12704. "Finalize the remember process."
  12705. (unless (fboundp 'remember-finalize)
  12706. (defalias 'remember-finalize 'remember-buffer))
  12707. (when (and org-clock-marker
  12708. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12709. ;; FIXME: test this, this is w/o notetaking!
  12710. (let (org-log-note-clock-out) (org-clock-out)))
  12711. (when buffer-file-name
  12712. (save-buffer)
  12713. (setq buffer-file-name nil))
  12714. (remember-finalize))
  12715. ;;;###autoload
  12716. (defun org-remember (&optional goto org-force-remember-template-char)
  12717. "Call `remember'. If this is already a remember buffer, re-apply template.
  12718. If there is an active region, make sure remember uses it as initial content
  12719. of the remember buffer.
  12720. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12721. anything, just go to the file/headline where the selected template usually
  12722. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12723. note stored by remember.
  12724. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12725. associated with a template in `org-remember-templates'."
  12726. (interactive "P")
  12727. (cond
  12728. ((equal goto '(4)) (org-go-to-remember-target))
  12729. ((equal goto '(16)) (org-remember-goto-last-stored))
  12730. (t
  12731. ;; set temporary variables that will be needed in
  12732. ;; `org-select-remember-template'
  12733. (setq org-select-template-temp-major-mode major-mode)
  12734. (setq org-select-template-original-buffer (current-buffer))
  12735. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12736. (progn
  12737. (when (< (length org-remember-templates) 2)
  12738. (error "No other template available"))
  12739. (erase-buffer)
  12740. (let ((annotation (plist-get org-store-link-plist :annotation))
  12741. (initial (plist-get org-store-link-plist :initial)))
  12742. (org-remember-apply-template))
  12743. (message "Press C-c C-c to remember data"))
  12744. (if (org-region-active-p)
  12745. (remember (buffer-substring (point) (mark)))
  12746. (call-interactively 'remember))))))
  12747. (defun org-remember-goto-last-stored ()
  12748. "Go to the location where the last remember note was stored."
  12749. (interactive)
  12750. (bookmark-jump "org-remember-last-stored")
  12751. (message "This is the last note stored by remember"))
  12752. (defun org-go-to-remember-target (&optional template-key)
  12753. "Go to the target location of a remember template.
  12754. The user is queried for the template."
  12755. (interactive)
  12756. (let* (org-select-template-temp-major-mode
  12757. (entry (org-select-remember-template template-key))
  12758. (file (nth 1 entry))
  12759. (heading (nth 2 entry))
  12760. visiting)
  12761. (unless (and file (stringp file) (string-match "\\S-" file))
  12762. (setq file org-default-notes-file))
  12763. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12764. (setq heading org-remember-default-headline))
  12765. (setq visiting (org-find-base-buffer-visiting file))
  12766. (if (not visiting) (find-file-noselect file))
  12767. (switch-to-buffer (or visiting (get-file-buffer file)))
  12768. (widen)
  12769. (goto-char (point-min))
  12770. (if (re-search-forward
  12771. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12772. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12773. nil t)
  12774. (goto-char (match-beginning 0))
  12775. (error "Target headline not found: %s" heading))))
  12776. (defvar org-note-abort nil) ; dynamically scoped
  12777. ;;;###autoload
  12778. (defun org-remember-handler ()
  12779. "Store stuff from remember.el into an org file.
  12780. First prompts for an org file. If the user just presses return, the value
  12781. of `org-default-notes-file' is used.
  12782. Then the command offers the headings tree of the selected file in order to
  12783. file the text at a specific location.
  12784. You can either immediately press RET to get the note appended to the
  12785. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12786. find a better place. Then press RET or <left> or <right> in insert the note.
  12787. Key Cursor position Note gets inserted
  12788. -----------------------------------------------------------------------------
  12789. RET buffer-start as level 1 heading at end of file
  12790. RET on headline as sublevel of the heading at cursor
  12791. RET no heading at cursor position, level taken from context.
  12792. Or use prefix arg to specify level manually.
  12793. <left> on headline as same level, before current heading
  12794. <right> on headline as same level, after current heading
  12795. So the fastest way to store the note is to press RET RET to append it to
  12796. the default file. This way your current train of thought is not
  12797. interrupted, in accordance with the principles of remember.el.
  12798. You can also get the fast execution without prompting by using
  12799. C-u C-c C-c to exit the remember buffer. See also the variable
  12800. `org-remember-store-without-prompt'.
  12801. Before being stored away, the function ensures that the text has a
  12802. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12803. is constructed from the current date and some additional data.
  12804. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12805. also indented so that it starts in the same column as the headline
  12806. \(i.e. after the stars).
  12807. See also the variable `org-reverse-note-order'."
  12808. (goto-char (point-min))
  12809. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12810. (replace-match ""))
  12811. (goto-char (point-max))
  12812. (beginning-of-line 1)
  12813. (while (looking-at "[ \t]*$\\|##.*")
  12814. (delete-region (1- (point)) (point-max))
  12815. (beginning-of-line 1))
  12816. (catch 'quit
  12817. (if org-note-abort (throw 'quit nil))
  12818. (let* ((txt (buffer-substring (point-min) (point-max)))
  12819. (fastp (org-xor (equal current-prefix-arg '(4))
  12820. org-remember-store-without-prompt))
  12821. (file (cond
  12822. (fastp org-default-notes-file)
  12823. ((and (eq org-remember-interactive-interface 'refile)
  12824. org-refile-targets)
  12825. org-default-notes-file)
  12826. ((not (and (equal current-prefix-arg '(16))
  12827. org-remember-previous-location))
  12828. (org-get-org-file))))
  12829. (heading org-remember-default-headline)
  12830. (visiting (and file (org-find-base-buffer-visiting file)))
  12831. (org-startup-folded nil)
  12832. (org-startup-align-all-tables nil)
  12833. (org-goto-start-pos 1)
  12834. spos exitcmd level indent reversed)
  12835. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12836. (setq file (car org-remember-previous-location)
  12837. heading (cdr org-remember-previous-location)
  12838. fastp t))
  12839. (setq current-prefix-arg nil)
  12840. (if (string-match "[ \t\n]+\\'" txt)
  12841. (setq txt (replace-match "" t t txt)))
  12842. ;; Modify text so that it becomes a nice subtree which can be inserted
  12843. ;; into an org tree.
  12844. (let* ((lines (split-string txt "\n"))
  12845. first)
  12846. (setq first (car lines) lines (cdr lines))
  12847. (if (string-match "^\\*+ " first)
  12848. ;; Is already a headline
  12849. (setq indent nil)
  12850. ;; We need to add a headline: Use time and first buffer line
  12851. (setq lines (cons first lines)
  12852. first (concat "* " (current-time-string)
  12853. " (" (remember-buffer-desc) ")")
  12854. indent " "))
  12855. (if (and org-adapt-indentation indent)
  12856. (setq lines (mapcar
  12857. (lambda (x)
  12858. (if (string-match "\\S-" x)
  12859. (concat indent x) x))
  12860. lines)))
  12861. (setq txt (concat first "\n"
  12862. (mapconcat 'identity lines "\n"))))
  12863. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12864. (setq txt (replace-match "\n\n" t t txt))
  12865. (if (string-match "[ \t\n]*\\'" txt)
  12866. (setq txt (replace-match "\n" t t txt))))
  12867. ;; Put the modified text back into the remember buffer, for refile.
  12868. (erase-buffer)
  12869. (insert txt)
  12870. (goto-char (point-min))
  12871. (when (and (eq org-remember-interactive-interface 'refile)
  12872. (not fastp))
  12873. (org-refile nil (or visiting (find-file-noselect file)))
  12874. (throw 'quit t))
  12875. ;; Find the file
  12876. (if (not visiting) (find-file-noselect file))
  12877. (with-current-buffer (or visiting (get-file-buffer file))
  12878. (unless (org-mode-p)
  12879. (error "Target files for remember notes must be in Org-mode"))
  12880. (save-excursion
  12881. (save-restriction
  12882. (widen)
  12883. (and (goto-char (point-min))
  12884. (not (re-search-forward "^\\* " nil t))
  12885. (insert "\n* " (or heading "Notes") "\n"))
  12886. (setq reversed (org-notes-order-reversed-p))
  12887. ;; Find the default location
  12888. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12889. (goto-char (point-min))
  12890. (if (re-search-forward
  12891. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12892. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12893. nil t)
  12894. (setq org-goto-start-pos (match-beginning 0))
  12895. (when fastp
  12896. (goto-char (point-max))
  12897. (unless (bolp) (newline))
  12898. (insert "* " heading "\n")
  12899. (setq org-goto-start-pos (point-at-bol 0)))))
  12900. ;; Ask the User for a location, using the appropriate interface
  12901. (cond
  12902. (fastp (setq spos org-goto-start-pos
  12903. exitcmd 'return))
  12904. ((eq org-remember-interactive-interface 'outline)
  12905. (setq spos (org-get-location (current-buffer)
  12906. org-remember-help)
  12907. exitcmd (cdr spos)
  12908. spos (car spos)))
  12909. ((eq org-remember-interactive-interface 'outline-path-completion)
  12910. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12911. (org-refile-use-outline-path t))
  12912. (setq spos (org-refile-get-location "Heading: ")
  12913. exitcmd 'return
  12914. spos (nth 3 spos))))
  12915. (t (error "this should not hapen")))
  12916. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12917. ; not handle this note
  12918. (goto-char spos)
  12919. (cond ((org-on-heading-p t)
  12920. (org-back-to-heading t)
  12921. (setq level (funcall outline-level))
  12922. (cond
  12923. ((eq exitcmd 'return)
  12924. ;; sublevel of current
  12925. (setq org-remember-previous-location
  12926. (cons (abbreviate-file-name file)
  12927. (org-get-heading 'notags)))
  12928. (if reversed
  12929. (outline-next-heading)
  12930. (org-end-of-subtree t)
  12931. (if (not (bolp))
  12932. (if (looking-at "[ \t]*\n")
  12933. (beginning-of-line 2)
  12934. (end-of-line 1)
  12935. (insert "\n"))))
  12936. (bookmark-set "org-remember-last-stored")
  12937. (org-paste-subtree (org-get-valid-level level 1) txt))
  12938. ((eq exitcmd 'left)
  12939. ;; before current
  12940. (bookmark-set "org-remember-last-stored")
  12941. (org-paste-subtree level txt))
  12942. ((eq exitcmd 'right)
  12943. ;; after current
  12944. (org-end-of-subtree t)
  12945. (bookmark-set "org-remember-last-stored")
  12946. (org-paste-subtree level txt))
  12947. (t (error "This should not happen"))))
  12948. ((and (bobp) (not reversed))
  12949. ;; Put it at the end, one level below level 1
  12950. (save-restriction
  12951. (widen)
  12952. (goto-char (point-max))
  12953. (if (not (bolp)) (newline))
  12954. (bookmark-set "org-remember-last-stored")
  12955. (org-paste-subtree (org-get-valid-level 1 1) txt)))
  12956. ((and (bobp) reversed)
  12957. ;; Put it at the start, as level 1
  12958. (save-restriction
  12959. (widen)
  12960. (goto-char (point-min))
  12961. (re-search-forward "^\\*+ " nil t)
  12962. (beginning-of-line 1)
  12963. (bookmark-set "org-remember-last-stored")
  12964. (org-paste-subtree 1 txt)))
  12965. (t
  12966. ;; Put it right there, with automatic level determined by
  12967. ;; org-paste-subtree or from prefix arg
  12968. (bookmark-set "org-remember-last-stored")
  12969. (org-paste-subtree
  12970. (if (numberp current-prefix-arg) current-prefix-arg)
  12971. txt)))
  12972. (when remember-save-after-remembering
  12973. (save-buffer)
  12974. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12975. t) ;; return t to indicate that we took care of this note.
  12976. (defun org-get-org-file ()
  12977. "Read a filename, with default directory `org-directory'."
  12978. (let ((default (or org-default-notes-file remember-data-file)))
  12979. (read-file-name (format "File name [%s]: " default)
  12980. (file-name-as-directory org-directory)
  12981. default)))
  12982. (defun org-notes-order-reversed-p ()
  12983. "Check if the current file should receive notes in reversed order."
  12984. (cond
  12985. ((not org-reverse-note-order) nil)
  12986. ((eq t org-reverse-note-order) t)
  12987. ((not (listp org-reverse-note-order)) nil)
  12988. (t (catch 'exit
  12989. (let ((all org-reverse-note-order)
  12990. entry)
  12991. (while (setq entry (pop all))
  12992. (if (string-match (car entry) buffer-file-name)
  12993. (throw 'exit (cdr entry))))
  12994. nil)))))
  12995. ;;; Refiling
  12996. (defvar org-refile-target-table nil
  12997. "The list of refile targets, created by `org-refile'.")
  12998. (defvar org-agenda-new-buffers nil
  12999. "Buffers created to visit agenda files.")
  13000. (defun org-get-refile-targets (&optional default-buffer)
  13001. "Produce a table with refile targets."
  13002. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  13003. targets txt re files f desc descre)
  13004. (with-current-buffer (or default-buffer (current-buffer))
  13005. (while (setq entry (pop entries))
  13006. (setq files (car entry) desc (cdr entry))
  13007. (cond
  13008. ((null files) (setq files (list (current-buffer))))
  13009. ((eq files 'org-agenda-files)
  13010. (setq files (org-agenda-files 'unrestricted)))
  13011. ((and (symbolp files) (fboundp files))
  13012. (setq files (funcall files)))
  13013. ((and (symbolp files) (boundp files))
  13014. (setq files (symbol-value files))))
  13015. (if (stringp files) (setq files (list files)))
  13016. (cond
  13017. ((eq (car desc) :tag)
  13018. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  13019. ((eq (car desc) :todo)
  13020. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  13021. ((eq (car desc) :regexp)
  13022. (setq descre (cdr desc)))
  13023. ((eq (car desc) :level)
  13024. (setq descre (concat "^\\*\\{" (number-to-string
  13025. (if org-odd-levels-only
  13026. (1- (* 2 (cdr desc)))
  13027. (cdr desc)))
  13028. "\\}[ \t]")))
  13029. ((eq (car desc) :maxlevel)
  13030. (setq descre (concat "^\\*\\{1," (number-to-string
  13031. (if org-odd-levels-only
  13032. (1- (* 2 (cdr desc)))
  13033. (cdr desc)))
  13034. "\\}[ \t]")))
  13035. (t (error "Bad refiling target description %s" desc)))
  13036. (while (setq f (pop files))
  13037. (save-excursion
  13038. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  13039. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  13040. (save-excursion
  13041. (save-restriction
  13042. (widen)
  13043. (goto-char (point-min))
  13044. (while (re-search-forward descre nil t)
  13045. (goto-char (point-at-bol))
  13046. (when (looking-at org-complex-heading-regexp)
  13047. (setq txt (match-string 4)
  13048. re (concat "^" (regexp-quote
  13049. (buffer-substring (match-beginning 1)
  13050. (match-end 4)))))
  13051. (if (match-end 5) (setq re (concat re "[ \t]+"
  13052. (regexp-quote
  13053. (match-string 5)))))
  13054. (setq re (concat re "[ \t]*$"))
  13055. (when org-refile-use-outline-path
  13056. (setq txt (mapconcat 'identity
  13057. (append
  13058. (if (eq org-refile-use-outline-path 'file)
  13059. (list (file-name-nondirectory
  13060. (buffer-file-name (buffer-base-buffer))))
  13061. (if (eq org-refile-use-outline-path 'full-file-path)
  13062. (list (buffer-file-name (buffer-base-buffer)))))
  13063. (org-get-outline-path)
  13064. (list txt))
  13065. "/")))
  13066. (push (list txt f re (point)) targets))
  13067. (goto-char (point-at-eol))))))))
  13068. (nreverse targets))))
  13069. (defun org-get-outline-path ()
  13070. "Return the outline path to the current entry, as a list."
  13071. (let (rtn)
  13072. (save-excursion
  13073. (while (org-up-heading-safe)
  13074. (when (looking-at org-complex-heading-regexp)
  13075. (push (org-match-string-no-properties 4) rtn)))
  13076. rtn)))
  13077. (defvar org-refile-history nil
  13078. "History for refiling operations.")
  13079. (defun org-refile (&optional goto default-buffer)
  13080. "Move the entry at point to another heading.
  13081. The list of target headings is compiled using the information in
  13082. `org-refile-targets', which see. This list is created upon first use, and
  13083. you can update it by calling this command with a double prefix (`C-u C-u').
  13084. FIXME: Can we find a better way of updating?
  13085. At the target location, the entry is filed as a subitem of the target heading.
  13086. Depending on `org-reverse-note-order', the new subitem will either be the
  13087. first of the last subitem.
  13088. With prefix arg GOTO, the command will only visit the target location,
  13089. not actually move anything.
  13090. With a double prefix `C-c C-c', go to the location where the last refiling
  13091. operation has put the subtree.
  13092. With a double prefix argument, the command can be used to jump to any
  13093. heading in the current buffer."
  13094. (interactive "P")
  13095. (let* ((cbuf (current-buffer))
  13096. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13097. pos it nbuf file re level reversed)
  13098. (if (equal goto '(16))
  13099. (org-refile-goto-last-stored)
  13100. (when (setq it (org-refile-get-location
  13101. (if goto "Goto: " "Refile to: ") default-buffer))
  13102. (setq file (nth 1 it)
  13103. re (nth 2 it)
  13104. pos (nth 3 it))
  13105. (setq nbuf (or (find-buffer-visiting file)
  13106. (find-file-noselect file)))
  13107. (if goto
  13108. (progn
  13109. (switch-to-buffer nbuf)
  13110. (goto-char pos)
  13111. (org-show-context 'org-goto))
  13112. (org-copy-special)
  13113. (save-excursion
  13114. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  13115. (find-file-noselect file))))
  13116. (setq reversed (org-notes-order-reversed-p))
  13117. (save-excursion
  13118. (save-restriction
  13119. (widen)
  13120. (goto-char pos)
  13121. (looking-at outline-regexp)
  13122. (setq level (org-get-valid-level (funcall outline-level) 1))
  13123. (goto-char
  13124. (if reversed
  13125. (outline-next-heading)
  13126. (or (save-excursion (outline-get-next-sibling))
  13127. (org-end-of-subtree t t)
  13128. (point-max))))
  13129. (bookmark-set "org-refile-last-stored")
  13130. (org-paste-subtree level))))
  13131. (org-cut-special)
  13132. (message "Entry refiled to \"%s\"" (car it)))))))
  13133. (defun org-refile-goto-last-stored ()
  13134. "Go to the location where the last refile was stored."
  13135. (interactive)
  13136. (bookmark-jump "org-refile-last-stored")
  13137. (message "This is the location of the last refile"))
  13138. (defun org-refile-get-location (&optional prompt default-buffer)
  13139. "Prompt the user for a refile location, using PROMPT."
  13140. (let ((org-refile-targets org-refile-targets)
  13141. (org-refile-use-outline-path org-refile-use-outline-path))
  13142. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  13143. (unless org-refile-target-table
  13144. (error "No refile targets"))
  13145. (let* ((cbuf (current-buffer))
  13146. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13147. (fname (and filename (file-truename filename)))
  13148. (tbl (mapcar
  13149. (lambda (x)
  13150. (if (not (equal fname (file-truename (nth 1 x))))
  13151. (cons (concat (car x) " (" (file-name-nondirectory
  13152. (nth 1 x)) ")")
  13153. (cdr x))
  13154. x))
  13155. org-refile-target-table))
  13156. (completion-ignore-case t))
  13157. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13158. tbl)))
  13159. ;;;; Dynamic blocks
  13160. (defun org-find-dblock (name)
  13161. "Find the first dynamic block with name NAME in the buffer.
  13162. If not found, stay at current position and return nil."
  13163. (let (pos)
  13164. (save-excursion
  13165. (goto-char (point-min))
  13166. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13167. nil t)
  13168. (match-beginning 0))))
  13169. (if pos (goto-char pos))
  13170. pos))
  13171. (defconst org-dblock-start-re
  13172. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13173. "Matches the startline of a dynamic block, with parameters.")
  13174. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13175. "Matches the end of a dyhamic block.")
  13176. (defun org-create-dblock (plist)
  13177. "Create a dynamic block section, with parameters taken from PLIST.
  13178. PLIST must containe a :name entry which is used as name of the block."
  13179. (unless (bolp) (newline))
  13180. (let ((name (plist-get plist :name)))
  13181. (insert "#+BEGIN: " name)
  13182. (while plist
  13183. (if (eq (car plist) :name)
  13184. (setq plist (cddr plist))
  13185. (insert " " (prin1-to-string (pop plist)))))
  13186. (insert "\n\n#+END:\n")
  13187. (beginning-of-line -2)))
  13188. (defun org-prepare-dblock ()
  13189. "Prepare dynamic block for refresh.
  13190. This empties the block, puts the cursor at the insert position and returns
  13191. the property list including an extra property :name with the block name."
  13192. (unless (looking-at org-dblock-start-re)
  13193. (error "Not at a dynamic block"))
  13194. (let* ((begdel (1+ (match-end 0)))
  13195. (name (org-no-properties (match-string 1)))
  13196. (params (append (list :name name)
  13197. (read (concat "(" (match-string 3) ")")))))
  13198. (unless (re-search-forward org-dblock-end-re nil t)
  13199. (error "Dynamic block not terminated"))
  13200. (delete-region begdel (match-beginning 0))
  13201. (goto-char begdel)
  13202. (open-line 1)
  13203. params))
  13204. (defun org-map-dblocks (&optional command)
  13205. "Apply COMMAND to all dynamic blocks in the current buffer.
  13206. If COMMAND is not given, use `org-update-dblock'."
  13207. (let ((cmd (or command 'org-update-dblock))
  13208. pos)
  13209. (save-excursion
  13210. (goto-char (point-min))
  13211. (while (re-search-forward org-dblock-start-re nil t)
  13212. (goto-char (setq pos (match-beginning 0)))
  13213. (condition-case nil
  13214. (funcall cmd)
  13215. (error (message "Error during update of dynamic block")))
  13216. (goto-char pos)
  13217. (unless (re-search-forward org-dblock-end-re nil t)
  13218. (error "Dynamic block not terminated"))))))
  13219. (defun org-dblock-update (&optional arg)
  13220. "User command for updating dynamic blocks.
  13221. Update the dynamic block at point. With prefix ARG, update all dynamic
  13222. blocks in the buffer."
  13223. (interactive "P")
  13224. (if arg
  13225. (org-update-all-dblocks)
  13226. (or (looking-at org-dblock-start-re)
  13227. (org-beginning-of-dblock))
  13228. (org-update-dblock)))
  13229. (defun org-update-dblock ()
  13230. "Update the dynamic block at point
  13231. This means to empty the block, parse for parameters and then call
  13232. the correct writing function."
  13233. (save-window-excursion
  13234. (let* ((pos (point))
  13235. (line (org-current-line))
  13236. (params (org-prepare-dblock))
  13237. (name (plist-get params :name))
  13238. (cmd (intern (concat "org-dblock-write:" name))))
  13239. (message "Updating dynamic block `%s' at line %d..." name line)
  13240. (funcall cmd params)
  13241. (message "Updating dynamic block `%s' at line %d...done" name line)
  13242. (goto-char pos))))
  13243. (defun org-beginning-of-dblock ()
  13244. "Find the beginning of the dynamic block at point.
  13245. Error if there is no scuh block at point."
  13246. (let ((pos (point))
  13247. beg)
  13248. (end-of-line 1)
  13249. (if (and (re-search-backward org-dblock-start-re nil t)
  13250. (setq beg (match-beginning 0))
  13251. (re-search-forward org-dblock-end-re nil t)
  13252. (> (match-end 0) pos))
  13253. (goto-char beg)
  13254. (goto-char pos)
  13255. (error "Not in a dynamic block"))))
  13256. (defun org-update-all-dblocks ()
  13257. "Update all dynamic blocks in the buffer.
  13258. This function can be used in a hook."
  13259. (when (org-mode-p)
  13260. (org-map-dblocks 'org-update-dblock)))
  13261. ;;;; Completion
  13262. (defconst org-additional-option-like-keywords
  13263. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13264. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13265. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13266. (defun org-complete (&optional arg)
  13267. "Perform completion on word at point.
  13268. At the beginning of a headline, this completes TODO keywords as given in
  13269. `org-todo-keywords'.
  13270. If the current word is preceded by a backslash, completes the TeX symbols
  13271. that are supported for HTML support.
  13272. If the current word is preceded by \"#+\", completes special words for
  13273. setting file options.
  13274. In the line after \"#+STARTUP:, complete valid keywords.\"
  13275. At all other locations, this simply calls the value of
  13276. `org-completion-fallback-command'."
  13277. (interactive "P")
  13278. (org-without-partial-completion
  13279. (catch 'exit
  13280. (let* ((end (point))
  13281. (beg1 (save-excursion
  13282. (skip-chars-backward (org-re "[:alnum:]_@"))
  13283. (point)))
  13284. (beg (save-excursion
  13285. (skip-chars-backward "a-zA-Z0-9_:$")
  13286. (point)))
  13287. (confirm (lambda (x) (stringp (car x))))
  13288. (searchhead (equal (char-before beg) ?*))
  13289. (tag (and (equal (char-before beg1) ?:)
  13290. (equal (char-after (point-at-bol)) ?*)))
  13291. (prop (and (equal (char-before beg1) ?:)
  13292. (not (equal (char-after (point-at-bol)) ?*))))
  13293. (texp (equal (char-before beg) ?\\))
  13294. (link (equal (char-before beg) ?\[))
  13295. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13296. beg)
  13297. "#+"))
  13298. (startup (string-match "^#\\+STARTUP:.*"
  13299. (buffer-substring (point-at-bol) (point))))
  13300. (completion-ignore-case opt)
  13301. (type nil)
  13302. (tbl nil)
  13303. (table (cond
  13304. (opt
  13305. (setq type :opt)
  13306. (append
  13307. (mapcar
  13308. (lambda (x)
  13309. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13310. (cons (match-string 2 x) (match-string 1 x)))
  13311. (org-split-string (org-get-current-options) "\n"))
  13312. (mapcar 'list org-additional-option-like-keywords)))
  13313. (startup
  13314. (setq type :startup)
  13315. org-startup-options)
  13316. (link (append org-link-abbrev-alist-local
  13317. org-link-abbrev-alist))
  13318. (texp
  13319. (setq type :tex)
  13320. org-html-entities)
  13321. ((string-match "\\`\\*+[ \t]+\\'"
  13322. (buffer-substring (point-at-bol) beg))
  13323. (setq type :todo)
  13324. (mapcar 'list org-todo-keywords-1))
  13325. (searchhead
  13326. (setq type :searchhead)
  13327. (save-excursion
  13328. (goto-char (point-min))
  13329. (while (re-search-forward org-todo-line-regexp nil t)
  13330. (push (list
  13331. (org-make-org-heading-search-string
  13332. (match-string 3) t))
  13333. tbl)))
  13334. tbl)
  13335. (tag (setq type :tag beg beg1)
  13336. (or org-tag-alist (org-get-buffer-tags)))
  13337. (prop (setq type :prop beg beg1)
  13338. (mapcar 'list (org-buffer-property-keys nil t t)))
  13339. (t (progn
  13340. (call-interactively org-completion-fallback-command)
  13341. (throw 'exit nil)))))
  13342. (pattern (buffer-substring-no-properties beg end))
  13343. (completion (try-completion pattern table confirm)))
  13344. (cond ((eq completion t)
  13345. (if (not (assoc (upcase pattern) table))
  13346. (message "Already complete")
  13347. (if (equal type :opt)
  13348. (insert (substring (cdr (assoc (upcase pattern) table))
  13349. (length pattern)))
  13350. (if (memq type '(:tag :prop)) (insert ":")))))
  13351. ((null completion)
  13352. (message "Can't find completion for \"%s\"" pattern)
  13353. (ding))
  13354. ((not (string= pattern completion))
  13355. (delete-region beg end)
  13356. (if (string-match " +$" completion)
  13357. (setq completion (replace-match "" t t completion)))
  13358. (insert completion)
  13359. (if (get-buffer-window "*Completions*")
  13360. (delete-window (get-buffer-window "*Completions*")))
  13361. (if (assoc completion table)
  13362. (if (eq type :todo) (insert " ")
  13363. (if (memq type '(:tag :prop)) (insert ":"))))
  13364. (if (and (equal type :opt) (assoc completion table))
  13365. (message "%s" (substitute-command-keys
  13366. "Press \\[org-complete] again to insert example settings"))))
  13367. (t
  13368. (message "Making completion list...")
  13369. (let ((list (sort (all-completions pattern table confirm)
  13370. 'string<)))
  13371. (with-output-to-temp-buffer "*Completions*"
  13372. (condition-case nil
  13373. ;; Protection needed for XEmacs and emacs 21
  13374. (display-completion-list list pattern)
  13375. (error (display-completion-list list)))))
  13376. (message "Making completion list...%s" "done")))))))
  13377. ;;;; TODO, DEADLINE, Comments
  13378. (defun org-toggle-comment ()
  13379. "Change the COMMENT state of an entry."
  13380. (interactive)
  13381. (save-excursion
  13382. (org-back-to-heading)
  13383. (let (case-fold-search)
  13384. (if (looking-at (concat outline-regexp
  13385. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13386. (replace-match "" t t nil 1)
  13387. (if (looking-at outline-regexp)
  13388. (progn
  13389. (goto-char (match-end 0))
  13390. (insert org-comment-string " ")))))))
  13391. (defvar org-last-todo-state-is-todo nil
  13392. "This is non-nil when the last TODO state change led to a TODO state.
  13393. If the last change removed the TODO tag or switched to DONE, then
  13394. this is nil.")
  13395. (defvar org-setting-tags nil) ; dynamically skiped
  13396. ;; FIXME: better place
  13397. (defun org-property-or-variable-value (var &optional inherit)
  13398. "Check if there is a property fixing the value of VAR.
  13399. If yes, return this value. If not, return the current value of the variable."
  13400. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13401. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13402. (read prop)
  13403. (symbol-value var))))
  13404. (defun org-parse-local-options (string var)
  13405. "Parse STRING for startup setting relevant for variable VAR."
  13406. (let ((rtn (symbol-value var))
  13407. e opts)
  13408. (save-match-data
  13409. (if (or (not string) (not (string-match "\\S-" string)))
  13410. rtn
  13411. (setq opts (delq nil (mapcar (lambda (x)
  13412. (setq e (assoc x org-startup-options))
  13413. (if (eq (nth 1 e) var) e nil))
  13414. (org-split-string string "[ \t]+"))))
  13415. (if (not opts)
  13416. rtn
  13417. (setq rtn nil)
  13418. (while (setq e (pop opts))
  13419. (if (not (nth 3 e))
  13420. (setq rtn (nth 2 e))
  13421. (if (not (listp rtn)) (setq rtn nil))
  13422. (push (nth 2 e) rtn)))
  13423. rtn)))))
  13424. (defvar org-blocker-hook nil
  13425. "Hook for functions that are allowed to block a state change.
  13426. Each function gets as its single argument a property list, see
  13427. `org-trigger-hook' for more information about this list.
  13428. If any of the functions in this hook returns nil, the state change
  13429. is blocked.")
  13430. (defvar org-trigger-hook nil
  13431. "Hook for functions that are triggered by a state change.
  13432. Each function gets as its single argument a property list with at least
  13433. the following elements:
  13434. (:type type-of-change :position pos-at-entry-start
  13435. :from old-state :to new-state)
  13436. Depending on the type, more properties may be present.
  13437. This mechanism is currently implemented for:
  13438. TODO state changes
  13439. ------------------
  13440. :type todo-state-change
  13441. :from previous state (keyword as a string), or nil
  13442. :to new state (keyword as a string), or nil")
  13443. (defun org-todo (&optional arg)
  13444. "Change the TODO state of an item.
  13445. The state of an item is given by a keyword at the start of the heading,
  13446. like
  13447. *** TODO Write paper
  13448. *** DONE Call mom
  13449. The different keywords are specified in the variable `org-todo-keywords'.
  13450. By default the available states are \"TODO\" and \"DONE\".
  13451. So for this example: when the item starts with TODO, it is changed to DONE.
  13452. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13453. DONE are present, add TODO at the beginning of the heading.
  13454. With C-u prefix arg, use completion to determine the new state.
  13455. With numeric prefix arg, switch to that state.
  13456. For calling through lisp, arg is also interpreted in the following way:
  13457. 'none -> empty state
  13458. \"\"(empty string) -> switch to empty state
  13459. 'done -> switch to DONE
  13460. 'nextset -> switch to the next set of keywords
  13461. 'previousset -> switch to the previous set of keywords
  13462. \"WAITING\" -> switch to the specified keyword, but only if it
  13463. really is a member of `org-todo-keywords'."
  13464. (interactive "P")
  13465. (save-excursion
  13466. (catch 'exit
  13467. (org-back-to-heading)
  13468. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13469. (or (looking-at (concat " +" org-todo-regexp " *"))
  13470. (looking-at " *"))
  13471. (let* ((match-data (match-data))
  13472. (startpos (point-at-bol))
  13473. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13474. (org-log-done org-log-done)
  13475. (org-log-repeat org-log-repeat)
  13476. (org-todo-log-states org-todo-log-states)
  13477. (this (match-string 1))
  13478. (hl-pos (match-beginning 0))
  13479. (head (org-get-todo-sequence-head this))
  13480. (ass (assoc head org-todo-kwd-alist))
  13481. (interpret (nth 1 ass))
  13482. (done-word (nth 3 ass))
  13483. (final-done-word (nth 4 ass))
  13484. (last-state (or this ""))
  13485. (completion-ignore-case t)
  13486. (member (member this org-todo-keywords-1))
  13487. (tail (cdr member))
  13488. (state (cond
  13489. ((and org-todo-key-trigger
  13490. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13491. (and (not arg) org-use-fast-todo-selection
  13492. (not (eq org-use-fast-todo-selection 'prefix)))))
  13493. ;; Use fast selection
  13494. (org-fast-todo-selection))
  13495. ((and (equal arg '(4))
  13496. (or (not org-use-fast-todo-selection)
  13497. (not org-todo-key-trigger)))
  13498. ;; Read a state with completion
  13499. (completing-read "State: " (mapcar (lambda(x) (list x))
  13500. org-todo-keywords-1)
  13501. nil t))
  13502. ((eq arg 'right)
  13503. (if this
  13504. (if tail (car tail) nil)
  13505. (car org-todo-keywords-1)))
  13506. ((eq arg 'left)
  13507. (if (equal member org-todo-keywords-1)
  13508. nil
  13509. (if this
  13510. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13511. org-todo-keywords-1)
  13512. (org-last org-todo-keywords-1))))
  13513. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13514. (setq arg nil))) ; hack to fall back to cycling
  13515. (arg
  13516. ;; user or caller requests a specific state
  13517. (cond
  13518. ((equal arg "") nil)
  13519. ((eq arg 'none) nil)
  13520. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13521. ((eq arg 'nextset)
  13522. (or (car (cdr (member head org-todo-heads)))
  13523. (car org-todo-heads)))
  13524. ((eq arg 'previousset)
  13525. (let ((org-todo-heads (reverse org-todo-heads)))
  13526. (or (car (cdr (member head org-todo-heads)))
  13527. (car org-todo-heads))))
  13528. ((car (member arg org-todo-keywords-1)))
  13529. ((nth (1- (prefix-numeric-value arg))
  13530. org-todo-keywords-1))))
  13531. ((null member) (or head (car org-todo-keywords-1)))
  13532. ((equal this final-done-word) nil) ;; -> make empty
  13533. ((null tail) nil) ;; -> first entry
  13534. ((eq interpret 'sequence)
  13535. (car tail))
  13536. ((memq interpret '(type priority))
  13537. (if (eq this-command last-command)
  13538. (car tail)
  13539. (if (> (length tail) 0)
  13540. (or done-word (car org-done-keywords))
  13541. nil)))
  13542. (t nil)))
  13543. (next (if state (concat " " state " ") " "))
  13544. (change-plist (list :type 'todo-state-change :from this :to state
  13545. :position startpos))
  13546. dolog now-done-p)
  13547. (when org-blocker-hook
  13548. (unless (save-excursion
  13549. (save-match-data
  13550. (run-hook-with-args-until-failure
  13551. 'org-blocker-hook change-plist)))
  13552. (if (interactive-p)
  13553. (error "TODO state change from %s to %s blocked" this state)
  13554. ;; fail silently
  13555. (message "TODO state change from %s to %s blocked" this state)
  13556. (throw 'exit nil))))
  13557. (store-match-data match-data)
  13558. (replace-match next t t)
  13559. (unless (pos-visible-in-window-p hl-pos)
  13560. (message "TODO state changed to %s" (org-trim next)))
  13561. (unless head
  13562. (setq head (org-get-todo-sequence-head state)
  13563. ass (assoc head org-todo-kwd-alist)
  13564. interpret (nth 1 ass)
  13565. done-word (nth 3 ass)
  13566. final-done-word (nth 4 ass)))
  13567. (when (memq arg '(nextset previousset))
  13568. (message "Keyword-Set %d/%d: %s"
  13569. (- (length org-todo-sets) -1
  13570. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13571. (length org-todo-sets)
  13572. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13573. (setq org-last-todo-state-is-todo
  13574. (not (member state org-done-keywords)))
  13575. (setq now-done-p (and (member state org-done-keywords)
  13576. (not (member this org-done-keywords))))
  13577. (and logging (org-local-logging logging))
  13578. (when (and (or org-todo-log-states org-log-done)
  13579. (not (memq arg '(nextset previousset))))
  13580. ;; we need to look at recording a time and note
  13581. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13582. (nth 2 (assoc this org-todo-log-states))))
  13583. (when (and state
  13584. (member state org-not-done-keywords)
  13585. (not (member this org-not-done-keywords)))
  13586. ;; This is now a todo state and was not one before
  13587. ;; If there was a CLOSED time stamp, get rid of it.
  13588. (org-add-planning-info nil nil 'closed))
  13589. (when (and now-done-p org-log-done)
  13590. ;; It is now done, and it was not done before
  13591. (org-add-planning-info 'closed (org-current-time))
  13592. (if (and (not dolog) (eq 'note org-log-done))
  13593. (org-add-log-maybe 'done state 'findpos 'note)))
  13594. (when (and state dolog)
  13595. ;; This is a non-nil state, and we need to log it
  13596. (org-add-log-maybe 'state state 'findpos dolog)))
  13597. ;; Fixup tag positioning
  13598. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13599. (run-hooks 'org-after-todo-state-change-hook)
  13600. (if (and arg (not (member state org-done-keywords)))
  13601. (setq head (org-get-todo-sequence-head state)))
  13602. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13603. ;; Do we need to trigger a repeat?
  13604. (when now-done-p (org-auto-repeat-maybe state))
  13605. ;; Fixup cursor location if close to the keyword
  13606. (if (and (outline-on-heading-p)
  13607. (not (bolp))
  13608. (save-excursion (beginning-of-line 1)
  13609. (looking-at org-todo-line-regexp))
  13610. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13611. (progn
  13612. (goto-char (or (match-end 2) (match-end 1)))
  13613. (just-one-space)))
  13614. (when org-trigger-hook
  13615. (save-excursion
  13616. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13617. (defun org-local-logging (value)
  13618. "Get logging settings from a property VALUE."
  13619. (let* (words w a)
  13620. ;; directly set the variables, they are already local.
  13621. (setq org-log-done nil
  13622. org-log-repeat nil
  13623. org-todo-log-states nil)
  13624. (setq words (org-split-string value))
  13625. (while (setq w (pop words))
  13626. (cond
  13627. ((setq a (assoc w org-startup-options))
  13628. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13629. (set (nth 1 a) (nth 2 a))))
  13630. ((setq a (org-extract-log-state-settings w))
  13631. (and (member (car a) org-todo-keywords-1)
  13632. (push a org-todo-log-states)))))))
  13633. (defun org-get-todo-sequence-head (kwd)
  13634. "Return the head of the TODO sequence to which KWD belongs.
  13635. If KWD is not set, check if there is a text property remembering the
  13636. right sequence."
  13637. (let (p)
  13638. (cond
  13639. ((not kwd)
  13640. (or (get-text-property (point-at-bol) 'org-todo-head)
  13641. (progn
  13642. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13643. nil (point-at-eol)))
  13644. (get-text-property p 'org-todo-head))))
  13645. ((not (member kwd org-todo-keywords-1))
  13646. (car org-todo-keywords-1))
  13647. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13648. (defun org-fast-todo-selection ()
  13649. "Fast TODO keyword selection with single keys.
  13650. Returns the new TODO keyword, or nil if no state change should occur."
  13651. (let* ((fulltable org-todo-key-alist)
  13652. (done-keywords org-done-keywords) ;; needed for the faces.
  13653. (maxlen (apply 'max (mapcar
  13654. (lambda (x)
  13655. (if (stringp (car x)) (string-width (car x)) 0))
  13656. fulltable)))
  13657. (expert nil)
  13658. (fwidth (+ maxlen 3 1 3))
  13659. (ncol (/ (- (window-width) 4) fwidth))
  13660. tg cnt e c tbl
  13661. groups ingroup)
  13662. (save-window-excursion
  13663. (if expert
  13664. (set-buffer (get-buffer-create " *Org todo*"))
  13665. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13666. (erase-buffer)
  13667. (org-set-local 'org-done-keywords done-keywords)
  13668. (setq tbl fulltable cnt 0)
  13669. (while (setq e (pop tbl))
  13670. (cond
  13671. ((equal e '(:startgroup))
  13672. (push '() groups) (setq ingroup t)
  13673. (when (not (= cnt 0))
  13674. (setq cnt 0)
  13675. (insert "\n"))
  13676. (insert "{ "))
  13677. ((equal e '(:endgroup))
  13678. (setq ingroup nil cnt 0)
  13679. (insert "}\n"))
  13680. (t
  13681. (setq tg (car e) c (cdr e))
  13682. (if ingroup (push tg (car groups)))
  13683. (setq tg (org-add-props tg nil 'face
  13684. (org-get-todo-face tg)))
  13685. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13686. (insert "[" c "] " tg (make-string
  13687. (- fwidth 4 (length tg)) ?\ ))
  13688. (when (= (setq cnt (1+ cnt)) ncol)
  13689. (insert "\n")
  13690. (if ingroup (insert " "))
  13691. (setq cnt 0)))))
  13692. (insert "\n")
  13693. (goto-char (point-min))
  13694. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13695. (fit-window-to-buffer))
  13696. (message "[a-z..]:Set [SPC]:clear")
  13697. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13698. (cond
  13699. ((or (= c ?\C-g)
  13700. (and (= c ?q) (not (rassoc c fulltable))))
  13701. (setq quit-flag t))
  13702. ((= c ?\ ) nil)
  13703. ((setq e (rassoc c fulltable) tg (car e))
  13704. tg)
  13705. (t (setq quit-flag t))))))
  13706. (defun org-get-repeat ()
  13707. "Check if tere is a deadline/schedule with repeater in this entry."
  13708. (save-match-data
  13709. (save-excursion
  13710. (org-back-to-heading t)
  13711. (if (re-search-forward
  13712. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13713. (match-string 1)))))
  13714. (defvar org-last-changed-timestamp)
  13715. (defvar org-log-post-message)
  13716. (defvar org-log-note-purpose)
  13717. (defun org-auto-repeat-maybe (done-word)
  13718. "Check if the current headline contains a repeated deadline/schedule.
  13719. If yes, set TODO state back to what it was and change the base date
  13720. of repeating deadline/scheduled time stamps to new date.
  13721. This function is run automatically after each state change to a DONE state."
  13722. ;; last-state is dynamically scoped into this function
  13723. (let* ((repeat (org-get-repeat))
  13724. (aa (assoc last-state org-todo-kwd-alist))
  13725. (interpret (nth 1 aa))
  13726. (head (nth 2 aa))
  13727. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13728. (msg "Entry repeats: ")
  13729. (org-log-done nil)
  13730. (org-todo-log-states nil)
  13731. re type n what ts)
  13732. (when repeat
  13733. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13734. (org-todo (if (eq interpret 'type) last-state head))
  13735. (when (and org-log-repeat
  13736. (or (not (memq 'org-add-log-note
  13737. (default-value 'post-command-hook)))
  13738. (eq org-log-note-purpose 'done)))
  13739. ;; Make sure a note is taken;
  13740. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13741. 'findpos org-log-repeat))
  13742. (org-back-to-heading t)
  13743. (org-add-planning-info nil nil 'closed)
  13744. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13745. org-deadline-time-regexp "\\)\\|\\("
  13746. org-ts-regexp "\\)"))
  13747. (while (re-search-forward
  13748. re (save-excursion (outline-next-heading) (point)) t)
  13749. (setq type (if (match-end 1) org-scheduled-string
  13750. (if (match-end 3) org-deadline-string "Plain:"))
  13751. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13752. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13753. (setq n (string-to-number (match-string 1 ts))
  13754. what (match-string 2 ts))
  13755. (if (equal what "w") (setq n (* n 7) what "d"))
  13756. (org-timestamp-change n (cdr (assoc what whata)))
  13757. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13758. (setq org-log-post-message msg)
  13759. (message "%s" msg))))
  13760. (defun org-show-todo-tree (arg)
  13761. "Make a compact tree which shows all headlines marked with TODO.
  13762. The tree will show the lines where the regexp matches, and all higher
  13763. headlines above the match.
  13764. With a \\[universal-argument] prefix, also show the DONE entries.
  13765. With a numeric prefix N, construct a sparse tree for the Nth element
  13766. of `org-todo-keywords-1'."
  13767. (interactive "P")
  13768. (let ((case-fold-search nil)
  13769. (kwd-re
  13770. (cond ((null arg) org-not-done-regexp)
  13771. ((equal arg '(4))
  13772. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13773. (mapcar 'list org-todo-keywords-1))))
  13774. (concat "\\("
  13775. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13776. "\\)\\>")))
  13777. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13778. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13779. org-todo-keywords-1)))
  13780. (t (error "Invalid prefix argument: %s" arg)))))
  13781. (message "%d TODO entries found"
  13782. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13783. (defun org-deadline (&optional remove)
  13784. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13785. With argument REMOVE, remove any deadline from the item."
  13786. (interactive "P")
  13787. (if remove
  13788. (progn
  13789. (org-remove-timestamp-with-keyword org-deadline-string)
  13790. (message "Item no longer has a deadline."))
  13791. (org-add-planning-info 'deadline nil 'closed)))
  13792. (defun org-schedule (&optional remove)
  13793. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13794. With argument REMOVE, remove any scheduling date from the item."
  13795. (interactive "P")
  13796. (if remove
  13797. (progn
  13798. (org-remove-timestamp-with-keyword org-scheduled-string)
  13799. (message "Item is no longer scheduled."))
  13800. (org-add-planning-info 'scheduled nil 'closed)))
  13801. (defun org-remove-timestamp-with-keyword (keyword)
  13802. "Remove all time stamps with KEYWORD in the current entry."
  13803. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13804. beg)
  13805. (save-excursion
  13806. (org-back-to-heading t)
  13807. (setq beg (point))
  13808. (org-end-of-subtree t t)
  13809. (while (re-search-backward re beg t)
  13810. (replace-match "")
  13811. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13812. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13813. (defun org-add-planning-info (what &optional time &rest remove)
  13814. "Insert new timestamp with keyword in the line directly after the headline.
  13815. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13816. If non is given, the user is prompted for a date.
  13817. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13818. be removed."
  13819. (interactive)
  13820. (let (org-time-was-given org-end-time-was-given)
  13821. (when what (setq time (or time (org-read-date nil 'to-time))))
  13822. (when (and org-insert-labeled-timestamps-at-point
  13823. (member what '(scheduled deadline)))
  13824. (insert
  13825. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13826. (org-insert-time-stamp time org-time-was-given
  13827. nil nil nil (list org-end-time-was-given))
  13828. (setq what nil))
  13829. (save-excursion
  13830. (save-restriction
  13831. (let (col list elt ts buffer-invisibility-spec)
  13832. (org-back-to-heading t)
  13833. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13834. (goto-char (match-end 1))
  13835. (setq col (current-column))
  13836. (goto-char (match-end 0))
  13837. (if (eobp) (insert "\n") (forward-char 1))
  13838. (if (and (not (looking-at outline-regexp))
  13839. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13840. "[^\r\n]*"))
  13841. (not (equal (match-string 1) org-clock-string)))
  13842. (narrow-to-region (match-beginning 0) (match-end 0))
  13843. (insert-before-markers "\n")
  13844. (backward-char 1)
  13845. (narrow-to-region (point) (point))
  13846. (indent-to-column col))
  13847. ;; Check if we have to remove something.
  13848. (setq list (cons what remove))
  13849. (while list
  13850. (setq elt (pop list))
  13851. (goto-char (point-min))
  13852. (when (or (and (eq elt 'scheduled)
  13853. (re-search-forward org-scheduled-time-regexp nil t))
  13854. (and (eq elt 'deadline)
  13855. (re-search-forward org-deadline-time-regexp nil t))
  13856. (and (eq elt 'closed)
  13857. (re-search-forward org-closed-time-regexp nil t)))
  13858. (replace-match "")
  13859. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13860. (if (looking-at " +") (replace-match ""))))
  13861. (goto-char (point-max))
  13862. (when what
  13863. (insert
  13864. (if (not (equal (char-before) ?\ )) " " "")
  13865. (cond ((eq what 'scheduled) org-scheduled-string)
  13866. ((eq what 'deadline) org-deadline-string)
  13867. ((eq what 'closed) org-closed-string))
  13868. " ")
  13869. (setq ts (org-insert-time-stamp
  13870. time
  13871. (or org-time-was-given
  13872. (and (eq what 'closed) org-log-done-with-time))
  13873. (eq what 'closed)
  13874. nil nil (list org-end-time-was-given)))
  13875. (end-of-line 1))
  13876. (goto-char (point-min))
  13877. (widen)
  13878. (if (looking-at "[ \t]+\r?\n")
  13879. (replace-match ""))
  13880. ts)))))
  13881. (defvar org-log-note-marker (make-marker))
  13882. (defvar org-log-note-purpose nil)
  13883. (defvar org-log-note-state nil)
  13884. (defvar org-log-note-how nil)
  13885. (defvar org-log-note-window-configuration nil)
  13886. (defvar org-log-note-return-to (make-marker))
  13887. (defvar org-log-post-message nil
  13888. "Message to be displayed after a log note has been stored.
  13889. The auto-repeater uses this.")
  13890. (defun org-add-log-maybe (&optional purpose state findpos how)
  13891. "Set up the post command hook to take a note.
  13892. If this is about to TODO state change, the new state is expected in STATE.
  13893. When FINDPOS is non-nil, find the correct position for the note in
  13894. the current entry. If not, assume that it can be inserted at point."
  13895. (save-excursion
  13896. (when findpos
  13897. (org-back-to-heading t)
  13898. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13899. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13900. "[^\r\n]*\\)?"))
  13901. (goto-char (match-end 0))
  13902. (unless org-log-states-order-reversed
  13903. (and (= (char-after) ?\n) (forward-char 1))
  13904. (org-skip-over-state-notes)
  13905. (skip-chars-backward " \t\n\r")))
  13906. (move-marker org-log-note-marker (point))
  13907. (setq org-log-note-purpose purpose
  13908. org-log-note-state state
  13909. org-log-note-how how)
  13910. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13911. (defun org-skip-over-state-notes ()
  13912. "Skip past the list of State notes in an entry."
  13913. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13914. (while (looking-at "[ \t]*- State")
  13915. (condition-case nil
  13916. (org-next-item)
  13917. (error (org-end-of-item)))))
  13918. (defun org-add-log-note (&optional purpose)
  13919. "Pop up a window for taking a note, and add this note later at point."
  13920. (remove-hook 'post-command-hook 'org-add-log-note)
  13921. (setq org-log-note-window-configuration (current-window-configuration))
  13922. (delete-other-windows)
  13923. (move-marker org-log-note-return-to (point))
  13924. (switch-to-buffer (marker-buffer org-log-note-marker))
  13925. (goto-char org-log-note-marker)
  13926. (org-switch-to-buffer-other-window "*Org Note*")
  13927. (erase-buffer)
  13928. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13929. (org-store-log-note)
  13930. (let ((org-inhibit-startup t)) (org-mode))
  13931. (insert (format "# Insert note for %s.
  13932. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13933. (cond
  13934. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13935. ((eq org-log-note-purpose 'done) "closed todo item")
  13936. ((eq org-log-note-purpose 'state)
  13937. (format "state change to \"%s\"" org-log-note-state))
  13938. (t (error "This should not happen")))))
  13939. (org-set-local 'org-finish-function 'org-store-log-note)))
  13940. (defun org-store-log-note ()
  13941. "Finish taking a log note, and insert it to where it belongs."
  13942. (let ((txt (buffer-string))
  13943. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13944. lines ind)
  13945. (kill-buffer (current-buffer))
  13946. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13947. (setq txt (replace-match "" t t txt)))
  13948. (if (string-match "\\s-+\\'" txt)
  13949. (setq txt (replace-match "" t t txt)))
  13950. (setq lines (org-split-string txt "\n"))
  13951. (when (and note (string-match "\\S-" note))
  13952. (setq note
  13953. (org-replace-escapes
  13954. note
  13955. (list (cons "%u" (user-login-name))
  13956. (cons "%U" user-full-name)
  13957. (cons "%t" (format-time-string
  13958. (org-time-stamp-format 'long 'inactive)
  13959. (current-time)))
  13960. (cons "%s" (if org-log-note-state
  13961. (concat "\"" org-log-note-state "\"")
  13962. "")))))
  13963. (if lines (setq note (concat note " \\\\")))
  13964. (push note lines))
  13965. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13966. (when lines
  13967. (save-excursion
  13968. (set-buffer (marker-buffer org-log-note-marker))
  13969. (save-excursion
  13970. (goto-char org-log-note-marker)
  13971. (move-marker org-log-note-marker nil)
  13972. (end-of-line 1)
  13973. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13974. (indent-relative nil)
  13975. (insert "- " (pop lines))
  13976. (org-indent-line-function)
  13977. (beginning-of-line 1)
  13978. (looking-at "[ \t]*")
  13979. (setq ind (concat (match-string 0) " "))
  13980. (end-of-line 1)
  13981. (while lines (insert "\n" ind (pop lines)))))))
  13982. (set-window-configuration org-log-note-window-configuration)
  13983. (with-current-buffer (marker-buffer org-log-note-return-to)
  13984. (goto-char org-log-note-return-to))
  13985. (move-marker org-log-note-return-to nil)
  13986. (and org-log-post-message (message "%s" org-log-post-message)))
  13987. ;; FIXME: what else would be useful?
  13988. ;; - priority
  13989. ;; - date
  13990. (defun org-sparse-tree (&optional arg)
  13991. "Create a sparse tree, prompt for the details.
  13992. This command can create sparse trees. You first need to select the type
  13993. of match used to create the tree:
  13994. t Show entries with a specific TODO keyword.
  13995. T Show entries selected by a tags match.
  13996. p Enter a property name and its value (both with completion on existing
  13997. names/values) and show entries with that property.
  13998. r Show entries matching a regular expression
  13999. d Show deadlines due within `org-deadline-warning-days'."
  14000. (interactive "P")
  14001. (let (ans kwd value)
  14002. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  14003. (setq ans (read-char-exclusive))
  14004. (cond
  14005. ((equal ans ?d)
  14006. (call-interactively 'org-check-deadlines))
  14007. ((equal ans ?b)
  14008. (call-interactively 'org-check-before-date))
  14009. ((equal ans ?t)
  14010. (org-show-todo-tree '(4)))
  14011. ((equal ans ?T)
  14012. (call-interactively 'org-tags-sparse-tree))
  14013. ((member ans '(?p ?P))
  14014. (setq kwd (completing-read "Property: "
  14015. (mapcar 'list (org-buffer-property-keys))))
  14016. (setq value (completing-read "Value: "
  14017. (mapcar 'list (org-property-values kwd))))
  14018. (unless (string-match "\\`{.*}\\'" value)
  14019. (setq value (concat "\"" value "\"")))
  14020. (org-tags-sparse-tree arg (concat kwd "=" value)))
  14021. ((member ans '(?r ?R ?/))
  14022. (call-interactively 'org-occur))
  14023. (t (error "No such sparse tree command \"%c\"" ans)))))
  14024. (defvar org-occur-highlights nil
  14025. "List of overlays used for occur matches.")
  14026. (make-variable-buffer-local 'org-occur-highlights)
  14027. (defvar org-occur-parameters nil
  14028. "Parameters of the active org-occur calls.
  14029. This is a list, each call to org-occur pushes as cons cell,
  14030. containing the regular expression and the callback, onto the list.
  14031. The list can contain several entries if `org-occur' has been called
  14032. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  14033. will only contain one set of parameters. When the highlights are
  14034. removed (for example with `C-c C-c', or with the next edit (depending
  14035. on `org-remove-highlights-with-change'), this variable is emptied
  14036. as well.")
  14037. (make-variable-buffer-local 'org-occur-parameters)
  14038. (defun org-occur (regexp &optional keep-previous callback)
  14039. "Make a compact tree which shows all matches of REGEXP.
  14040. The tree will show the lines where the regexp matches, and all higher
  14041. headlines above the match. It will also show the heading after the match,
  14042. to make sure editing the matching entry is easy.
  14043. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  14044. call to `org-occur' will be kept, to allow stacking of calls to this
  14045. command.
  14046. If CALLBACK is non-nil, it is a function which is called to confirm
  14047. that the match should indeed be shown."
  14048. (interactive "sRegexp: \nP")
  14049. (unless keep-previous
  14050. (org-remove-occur-highlights nil nil t))
  14051. (push (cons regexp callback) org-occur-parameters)
  14052. (let ((cnt 0))
  14053. (save-excursion
  14054. (goto-char (point-min))
  14055. (if (or (not keep-previous) ; do not want to keep
  14056. (not org-occur-highlights)) ; no previous matches
  14057. ;; hide everything
  14058. (org-overview))
  14059. (while (re-search-forward regexp nil t)
  14060. (when (or (not callback)
  14061. (save-match-data (funcall callback)))
  14062. (setq cnt (1+ cnt))
  14063. (when org-highlight-sparse-tree-matches
  14064. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  14065. (org-show-context 'occur-tree))))
  14066. (when org-remove-highlights-with-change
  14067. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  14068. nil 'local))
  14069. (unless org-sparse-tree-open-archived-trees
  14070. (org-hide-archived-subtrees (point-min) (point-max)))
  14071. (run-hooks 'org-occur-hook)
  14072. (if (interactive-p)
  14073. (message "%d match(es) for regexp %s" cnt regexp))
  14074. cnt))
  14075. (defun org-show-context (&optional key)
  14076. "Make sure point and context and visible.
  14077. How much context is shown depends upon the variables
  14078. `org-show-hierarchy-above', `org-show-following-heading'. and
  14079. `org-show-siblings'."
  14080. (let ((heading-p (org-on-heading-p t))
  14081. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  14082. (following-p (org-get-alist-option org-show-following-heading key))
  14083. (entry-p (org-get-alist-option org-show-entry-below key))
  14084. (siblings-p (org-get-alist-option org-show-siblings key)))
  14085. (catch 'exit
  14086. ;; Show heading or entry text
  14087. (if (and heading-p (not entry-p))
  14088. (org-flag-heading nil) ; only show the heading
  14089. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  14090. (org-show-hidden-entry))) ; show entire entry
  14091. (when following-p
  14092. ;; Show next sibling, or heading below text
  14093. (save-excursion
  14094. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  14095. (org-flag-heading nil))))
  14096. (when siblings-p (org-show-siblings))
  14097. (when hierarchy-p
  14098. ;; show all higher headings, possibly with siblings
  14099. (save-excursion
  14100. (while (and (condition-case nil
  14101. (progn (org-up-heading-all 1) t)
  14102. (error nil))
  14103. (not (bobp)))
  14104. (org-flag-heading nil)
  14105. (when siblings-p (org-show-siblings))))))))
  14106. (defun org-reveal (&optional siblings)
  14107. "Show current entry, hierarchy above it, and the following headline.
  14108. This can be used to show a consistent set of context around locations
  14109. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  14110. not t for the search context.
  14111. With optional argument SIBLINGS, on each level of the hierarchy all
  14112. siblings are shown. This repairs the tree structure to what it would
  14113. look like when opened with hierarchical calls to `org-cycle'."
  14114. (interactive "P")
  14115. (let ((org-show-hierarchy-above t)
  14116. (org-show-following-heading t)
  14117. (org-show-siblings (if siblings t org-show-siblings)))
  14118. (org-show-context nil)))
  14119. (defun org-highlight-new-match (beg end)
  14120. "Highlight from BEG to END and mark the highlight is an occur headline."
  14121. (let ((ov (org-make-overlay beg end)))
  14122. (org-overlay-put ov 'face 'secondary-selection)
  14123. (push ov org-occur-highlights)))
  14124. (defun org-remove-occur-highlights (&optional beg end noremove)
  14125. "Remove the occur highlights from the buffer.
  14126. BEG and END are ignored. If NOREMOVE is nil, remove this function
  14127. from the `before-change-functions' in the current buffer."
  14128. (interactive)
  14129. (unless org-inhibit-highlight-removal
  14130. (mapc 'org-delete-overlay org-occur-highlights)
  14131. (setq org-occur-highlights nil)
  14132. (setq org-occur-parameters nil)
  14133. (unless noremove
  14134. (remove-hook 'before-change-functions
  14135. 'org-remove-occur-highlights 'local))))
  14136. ;;;; Priorities
  14137. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  14138. "Regular expression matching the priority indicator.")
  14139. (defvar org-remove-priority-next-time nil)
  14140. (defun org-priority-up ()
  14141. "Increase the priority of the current item."
  14142. (interactive)
  14143. (org-priority 'up))
  14144. (defun org-priority-down ()
  14145. "Decrease the priority of the current item."
  14146. (interactive)
  14147. (org-priority 'down))
  14148. (defun org-priority (&optional action)
  14149. "Change the priority of an item by ARG.
  14150. ACTION can be `set', `up', `down', or a character."
  14151. (interactive)
  14152. (setq action (or action 'set))
  14153. (let (current new news have remove)
  14154. (save-excursion
  14155. (org-back-to-heading)
  14156. (if (looking-at org-priority-regexp)
  14157. (setq current (string-to-char (match-string 2))
  14158. have t)
  14159. (setq current org-default-priority))
  14160. (cond
  14161. ((or (eq action 'set) (integerp action))
  14162. (if (integerp action)
  14163. (setq new action)
  14164. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14165. (setq new (read-char-exclusive)))
  14166. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14167. (= (upcase org-lowest-priority) org-lowest-priority))
  14168. (setq new (upcase new)))
  14169. (cond ((equal new ?\ ) (setq remove t))
  14170. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14171. (error "Priority must be between `%c' and `%c'"
  14172. org-highest-priority org-lowest-priority))))
  14173. ((eq action 'up)
  14174. (if (and (not have) (eq last-command this-command))
  14175. (setq new org-lowest-priority)
  14176. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14177. org-default-priority (1- current)))))
  14178. ((eq action 'down)
  14179. (if (and (not have) (eq last-command this-command))
  14180. (setq new org-highest-priority)
  14181. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14182. org-default-priority (1+ current)))))
  14183. (t (error "Invalid action")))
  14184. (if (or (< (upcase new) org-highest-priority)
  14185. (> (upcase new) org-lowest-priority))
  14186. (setq remove t))
  14187. (setq news (format "%c" new))
  14188. (if have
  14189. (if remove
  14190. (replace-match "" t t nil 1)
  14191. (replace-match news t t nil 2))
  14192. (if remove
  14193. (error "No priority cookie found in line")
  14194. (looking-at org-todo-line-regexp)
  14195. (if (match-end 2)
  14196. (progn
  14197. (goto-char (match-end 2))
  14198. (insert " [#" news "]"))
  14199. (goto-char (match-beginning 3))
  14200. (insert "[#" news "] ")))))
  14201. (org-preserve-lc (org-set-tags nil 'align))
  14202. (if remove
  14203. (message "Priority removed")
  14204. (message "Priority of current item set to %s" news))))
  14205. (defun org-get-priority (s)
  14206. "Find priority cookie and return priority."
  14207. (save-match-data
  14208. (if (not (string-match org-priority-regexp s))
  14209. (* 1000 (- org-lowest-priority org-default-priority))
  14210. (* 1000 (- org-lowest-priority
  14211. (string-to-char (match-string 2 s)))))))
  14212. ;;;; Tags
  14213. (defun org-scan-tags (action matcher &optional todo-only)
  14214. "Scan headline tags with inheritance and produce output ACTION.
  14215. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14216. evaluated, testing if a given set of tags qualifies a headline for
  14217. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14218. are included in the output."
  14219. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14220. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14221. (org-re
  14222. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14223. (props (list 'face nil
  14224. 'done-face 'org-done
  14225. 'undone-face nil
  14226. 'mouse-face 'highlight
  14227. 'org-not-done-regexp org-not-done-regexp
  14228. 'org-todo-regexp org-todo-regexp
  14229. 'keymap org-agenda-keymap
  14230. 'help-echo
  14231. (format "mouse-2 or RET jump to org file %s"
  14232. (abbreviate-file-name
  14233. (or (buffer-file-name (buffer-base-buffer))
  14234. (buffer-name (buffer-base-buffer)))))))
  14235. (case-fold-search nil)
  14236. lspos
  14237. tags tags-list tags-alist (llast 0) rtn level category i txt
  14238. todo marker entry priority)
  14239. (save-excursion
  14240. (goto-char (point-min))
  14241. (when (eq action 'sparse-tree)
  14242. (org-overview)
  14243. (org-remove-occur-highlights))
  14244. (while (re-search-forward re nil t)
  14245. (catch :skip
  14246. (setq todo (if (match-end 1) (match-string 2))
  14247. tags (if (match-end 4) (match-string 4)))
  14248. (goto-char (setq lspos (1+ (match-beginning 0))))
  14249. (setq level (org-reduced-level (funcall outline-level))
  14250. category (org-get-category))
  14251. (setq i llast llast level)
  14252. ;; remove tag lists from same and sublevels
  14253. (while (>= i level)
  14254. (when (setq entry (assoc i tags-alist))
  14255. (setq tags-alist (delete entry tags-alist)))
  14256. (setq i (1- i)))
  14257. ;; add the nex tags
  14258. (when tags
  14259. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14260. tags-alist
  14261. (cons (cons level tags) tags-alist)))
  14262. ;; compile tags for current headline
  14263. (setq tags-list
  14264. (if org-use-tag-inheritance
  14265. (apply 'append (mapcar 'cdr tags-alist))
  14266. tags))
  14267. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14268. (eval matcher)
  14269. (or (not org-agenda-skip-archived-trees)
  14270. (not (member org-archive-tag tags-list))))
  14271. (and (eq action 'agenda) (org-agenda-skip))
  14272. ;; list this headline
  14273. (if (eq action 'sparse-tree)
  14274. (progn
  14275. (and org-highlight-sparse-tree-matches
  14276. (org-get-heading) (match-end 0)
  14277. (org-highlight-new-match
  14278. (match-beginning 0) (match-beginning 1)))
  14279. (org-show-context 'tags-tree))
  14280. (setq txt (org-format-agenda-item
  14281. ""
  14282. (concat
  14283. (if org-tags-match-list-sublevels
  14284. (make-string (1- level) ?.) "")
  14285. (org-get-heading))
  14286. category tags-list)
  14287. priority (org-get-priority txt))
  14288. (goto-char lspos)
  14289. (setq marker (org-agenda-new-marker))
  14290. (org-add-props txt props
  14291. 'org-marker marker 'org-hd-marker marker 'org-category category
  14292. 'priority priority 'type "tagsmatch")
  14293. (push txt rtn))
  14294. ;; if we are to skip sublevels, jump to end of subtree
  14295. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14296. (when (and (eq action 'sparse-tree)
  14297. (not org-sparse-tree-open-archived-trees))
  14298. (org-hide-archived-subtrees (point-min) (point-max)))
  14299. (nreverse rtn)))
  14300. (defvar todo-only) ;; dynamically scoped
  14301. (defun org-tags-sparse-tree (&optional todo-only match)
  14302. "Create a sparse tree according to tags string MATCH.
  14303. MATCH can contain positive and negative selection of tags, like
  14304. \"+WORK+URGENT-WITHBOSS\".
  14305. If optional argument TODO_ONLY is non-nil, only select lines that are
  14306. also TODO lines."
  14307. (interactive "P")
  14308. (org-prepare-agenda-buffers (list (current-buffer)))
  14309. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14310. (defvar org-cached-props nil)
  14311. (defun org-cached-entry-get (pom property)
  14312. (if (or (eq t org-use-property-inheritance)
  14313. (member property org-use-property-inheritance))
  14314. ;; Caching is not possible, check it directly
  14315. (org-entry-get pom property 'inherit)
  14316. ;; Get all properties, so that we can do complicated checks easily
  14317. (cdr (assoc property (or org-cached-props
  14318. (setq org-cached-props
  14319. (org-entry-properties pom)))))))
  14320. (defun org-global-tags-completion-table (&optional files)
  14321. "Return the list of all tags in all agenda buffer/files."
  14322. (save-excursion
  14323. (org-uniquify
  14324. (delq nil
  14325. (apply 'append
  14326. (mapcar
  14327. (lambda (file)
  14328. (set-buffer (find-file-noselect file))
  14329. (append (org-get-buffer-tags)
  14330. (mapcar (lambda (x) (if (stringp (car-safe x))
  14331. (list (car-safe x)) nil))
  14332. org-tag-alist)))
  14333. (if (and files (car files))
  14334. files
  14335. (org-agenda-files))))))))
  14336. (defun org-make-tags-matcher (match)
  14337. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14338. ;; todo-only is scoped dynamically into this function, and the function
  14339. ;; may change it it the matcher asksk for it.
  14340. (unless match
  14341. ;; Get a new match request, with completion
  14342. (let ((org-last-tags-completion-table
  14343. (org-global-tags-completion-table)))
  14344. (setq match (completing-read
  14345. "Match: " 'org-tags-completion-function nil nil nil
  14346. 'org-tags-history))))
  14347. ;; Parse the string and create a lisp form
  14348. (let ((match0 match)
  14349. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14350. minus tag mm
  14351. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14352. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14353. (if (string-match "/+" match)
  14354. ;; match contains also a todo-matching request
  14355. (progn
  14356. (setq tagsmatch (substring match 0 (match-beginning 0))
  14357. todomatch (substring match (match-end 0)))
  14358. (if (string-match "^!" todomatch)
  14359. (setq todo-only t todomatch (substring todomatch 1)))
  14360. (if (string-match "^\\s-*$" todomatch)
  14361. (setq todomatch nil)))
  14362. ;; only matching tags
  14363. (setq tagsmatch match todomatch nil))
  14364. ;; Make the tags matcher
  14365. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14366. (setq tagsmatcher t)
  14367. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14368. (while (setq term (pop orterms))
  14369. (while (and (equal (substring term -1) "\\") orterms)
  14370. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14371. (while (string-match re term)
  14372. (setq minus (and (match-end 1)
  14373. (equal (match-string 1 term) "-"))
  14374. tag (match-string 2 term)
  14375. re-p (equal (string-to-char tag) ?{)
  14376. level-p (match-end 3)
  14377. prop-p (match-end 4)
  14378. mm (cond
  14379. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14380. (level-p `(= level ,(string-to-number
  14381. (match-string 3 term))))
  14382. (prop-p
  14383. (setq pn (match-string 4 term)
  14384. pv (match-string 5 term)
  14385. cat-p (equal pn "CATEGORY")
  14386. re-p (equal (string-to-char pv) ?{)
  14387. pv (substring pv 1 -1))
  14388. (if (equal pn "CATEGORY")
  14389. (setq gv '(get-text-property (point) 'org-category))
  14390. (setq gv `(org-cached-entry-get nil ,pn)))
  14391. (if re-p
  14392. `(string-match ,pv (or ,gv ""))
  14393. `(equal ,pv (or ,gv ""))))
  14394. (t `(member ,(downcase tag) tags-list)))
  14395. mm (if minus (list 'not mm) mm)
  14396. term (substring term (match-end 0)))
  14397. (push mm tagsmatcher))
  14398. (push (if (> (length tagsmatcher) 1)
  14399. (cons 'and tagsmatcher)
  14400. (car tagsmatcher))
  14401. orlist)
  14402. (setq tagsmatcher nil))
  14403. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14404. (setq tagsmatcher
  14405. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14406. ;; Make the todo matcher
  14407. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14408. (setq todomatcher t)
  14409. (setq orterms (org-split-string todomatch "|") orlist nil)
  14410. (while (setq term (pop orterms))
  14411. (while (string-match re term)
  14412. (setq minus (and (match-end 1)
  14413. (equal (match-string 1 term) "-"))
  14414. kwd (match-string 2 term)
  14415. re-p (equal (string-to-char kwd) ?{)
  14416. term (substring term (match-end 0))
  14417. mm (if re-p
  14418. `(string-match ,(substring kwd 1 -1) todo)
  14419. (list 'equal 'todo kwd))
  14420. mm (if minus (list 'not mm) mm))
  14421. (push mm todomatcher))
  14422. (push (if (> (length todomatcher) 1)
  14423. (cons 'and todomatcher)
  14424. (car todomatcher))
  14425. orlist)
  14426. (setq todomatcher nil))
  14427. (setq todomatcher (if (> (length orlist) 1)
  14428. (cons 'or orlist) (car orlist))))
  14429. ;; Return the string and lisp forms of the matcher
  14430. (setq matcher (if todomatcher
  14431. (list 'and tagsmatcher todomatcher)
  14432. tagsmatcher))
  14433. (cons match0 matcher)))
  14434. (defun org-match-any-p (re list)
  14435. "Does re match any element of list?"
  14436. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14437. (delq nil list))
  14438. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14439. (defvar org-tags-overlay (org-make-overlay 1 1))
  14440. (org-detach-overlay org-tags-overlay)
  14441. (defun org-align-tags-here (to-col)
  14442. ;; Assumes that this is a headline
  14443. (let ((pos (point)) (col (current-column)) tags)
  14444. (beginning-of-line 1)
  14445. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14446. (< pos (match-beginning 2)))
  14447. (progn
  14448. (setq tags (match-string 2))
  14449. (goto-char (match-beginning 1))
  14450. (insert " ")
  14451. (delete-region (point) (1+ (match-end 0)))
  14452. (backward-char 1)
  14453. (move-to-column
  14454. (max (1+ (current-column))
  14455. (1+ col)
  14456. (if (> to-col 0)
  14457. to-col
  14458. (- (abs to-col) (length tags))))
  14459. t)
  14460. (insert tags)
  14461. (move-to-column (min (current-column) col) t))
  14462. (goto-char pos))))
  14463. (defun org-set-tags (&optional arg just-align)
  14464. "Set the tags for the current headline.
  14465. With prefix ARG, realign all tags in headings in the current buffer."
  14466. (interactive "P")
  14467. (let* ((re (concat "^" outline-regexp))
  14468. (current (org-get-tags-string))
  14469. (col (current-column))
  14470. (org-setting-tags t)
  14471. table current-tags inherited-tags ; computed below when needed
  14472. tags p0 c0 c1 rpl)
  14473. (if arg
  14474. (save-excursion
  14475. (goto-char (point-min))
  14476. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14477. (while (re-search-forward re nil t)
  14478. (org-set-tags nil t)
  14479. (end-of-line 1)))
  14480. (message "All tags realigned to column %d" org-tags-column))
  14481. (if just-align
  14482. (setq tags current)
  14483. ;; Get a new set of tags from the user
  14484. (save-excursion
  14485. (setq table (or org-tag-alist (org-get-buffer-tags))
  14486. org-last-tags-completion-table table
  14487. current-tags (org-split-string current ":")
  14488. inherited-tags (nreverse
  14489. (nthcdr (length current-tags)
  14490. (nreverse (org-get-tags-at))))
  14491. tags
  14492. (if (or (eq t org-use-fast-tag-selection)
  14493. (and org-use-fast-tag-selection
  14494. (delq nil (mapcar 'cdr table))))
  14495. (org-fast-tag-selection
  14496. current-tags inherited-tags table
  14497. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14498. (let ((org-add-colon-after-tag-completion t))
  14499. (org-trim
  14500. (org-without-partial-completion
  14501. (completing-read "Tags: " 'org-tags-completion-function
  14502. nil nil current 'org-tags-history)))))))
  14503. (while (string-match "[-+&]+" tags)
  14504. ;; No boolean logic, just a list
  14505. (setq tags (replace-match ":" t t tags))))
  14506. (if (string-match "\\`[\t ]*\\'" tags)
  14507. (setq tags "")
  14508. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14509. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14510. ;; Insert new tags at the correct column
  14511. (beginning-of-line 1)
  14512. (cond
  14513. ((and (equal current "") (equal tags "")))
  14514. ((re-search-forward
  14515. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14516. (point-at-eol) t)
  14517. (if (equal tags "")
  14518. (setq rpl "")
  14519. (goto-char (match-beginning 0))
  14520. (setq c0 (current-column) p0 (point)
  14521. c1 (max (1+ c0) (if (> org-tags-column 0)
  14522. org-tags-column
  14523. (- (- org-tags-column) (length tags))))
  14524. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14525. (replace-match rpl t t)
  14526. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14527. tags)
  14528. (t (error "Tags alignment failed")))
  14529. (move-to-column col)
  14530. (unless just-align
  14531. (run-hooks 'org-after-tags-change-hook)))))
  14532. (defun org-change-tag-in-region (beg end tag off)
  14533. "Add or remove TAG for each entry in the region.
  14534. This works in the agenda, and also in an org-mode buffer."
  14535. (interactive
  14536. (list (region-beginning) (region-end)
  14537. (let ((org-last-tags-completion-table
  14538. (if (org-mode-p)
  14539. (org-get-buffer-tags)
  14540. (org-global-tags-completion-table))))
  14541. (completing-read
  14542. "Tag: " 'org-tags-completion-function nil nil nil
  14543. 'org-tags-history))
  14544. (progn
  14545. (message "[s]et or [r]emove? ")
  14546. (equal (read-char-exclusive) ?r))))
  14547. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14548. (let ((agendap (equal major-mode 'org-agenda-mode))
  14549. l1 l2 m buf pos newhead (cnt 0))
  14550. (goto-char end)
  14551. (setq l2 (1- (org-current-line)))
  14552. (goto-char beg)
  14553. (setq l1 (org-current-line))
  14554. (loop for l from l1 to l2 do
  14555. (goto-line l)
  14556. (setq m (get-text-property (point) 'org-hd-marker))
  14557. (when (or (and (org-mode-p) (org-on-heading-p))
  14558. (and agendap m))
  14559. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14560. pos (if agendap m (point)))
  14561. (with-current-buffer buf
  14562. (save-excursion
  14563. (save-restriction
  14564. (goto-char pos)
  14565. (setq cnt (1+ cnt))
  14566. (org-toggle-tag tag (if off 'off 'on))
  14567. (setq newhead (org-get-heading)))))
  14568. (and agendap (org-agenda-change-all-lines newhead m))))
  14569. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14570. (defun org-tags-completion-function (string predicate &optional flag)
  14571. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14572. (confirm (lambda (x) (stringp (car x)))))
  14573. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14574. (setq s1 (match-string 1 string)
  14575. s2 (match-string 2 string))
  14576. (setq s1 "" s2 string))
  14577. (cond
  14578. ((eq flag nil)
  14579. ;; try completion
  14580. (setq rtn (try-completion s2 ctable confirm))
  14581. (if (stringp rtn)
  14582. (setq rtn
  14583. (concat s1 s2 (substring rtn (length s2))
  14584. (if (and org-add-colon-after-tag-completion
  14585. (assoc rtn ctable))
  14586. ":" ""))))
  14587. rtn)
  14588. ((eq flag t)
  14589. ;; all-completions
  14590. (all-completions s2 ctable confirm)
  14591. )
  14592. ((eq flag 'lambda)
  14593. ;; exact match?
  14594. (assoc s2 ctable)))
  14595. ))
  14596. (defun org-fast-tag-insert (kwd tags face &optional end)
  14597. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14598. (insert (format "%-12s" (concat kwd ":"))
  14599. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14600. (or end "")))
  14601. (defun org-fast-tag-show-exit (flag)
  14602. (save-excursion
  14603. (goto-line 3)
  14604. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14605. (replace-match ""))
  14606. (when flag
  14607. (end-of-line 1)
  14608. (move-to-column (- (window-width) 19) t)
  14609. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14610. (defun org-set-current-tags-overlay (current prefix)
  14611. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14612. (if (featurep 'xemacs)
  14613. (org-overlay-display org-tags-overlay (concat prefix s)
  14614. 'secondary-selection)
  14615. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14616. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14617. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14618. "Fast tag selection with single keys.
  14619. CURRENT is the current list of tags in the headline, INHERITED is the
  14620. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14621. possibly with grouping information. TODO-TABLE is a similar table with
  14622. TODO keywords, should these have keys assigned to them.
  14623. If the keys are nil, a-z are automatically assigned.
  14624. Returns the new tags string, or nil to not change the current settings."
  14625. (let* ((fulltable (append table todo-table))
  14626. (maxlen (apply 'max (mapcar
  14627. (lambda (x)
  14628. (if (stringp (car x)) (string-width (car x)) 0))
  14629. fulltable)))
  14630. (buf (current-buffer))
  14631. (expert (eq org-fast-tag-selection-single-key 'expert))
  14632. (buffer-tags nil)
  14633. (fwidth (+ maxlen 3 1 3))
  14634. (ncol (/ (- (window-width) 4) fwidth))
  14635. (i-face 'org-done)
  14636. (c-face 'org-todo)
  14637. tg cnt e c char c1 c2 ntable tbl rtn
  14638. ov-start ov-end ov-prefix
  14639. (exit-after-next org-fast-tag-selection-single-key)
  14640. (done-keywords org-done-keywords)
  14641. groups ingroup)
  14642. (save-excursion
  14643. (beginning-of-line 1)
  14644. (if (looking-at
  14645. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14646. (setq ov-start (match-beginning 1)
  14647. ov-end (match-end 1)
  14648. ov-prefix "")
  14649. (setq ov-start (1- (point-at-eol))
  14650. ov-end (1+ ov-start))
  14651. (skip-chars-forward "^\n\r")
  14652. (setq ov-prefix
  14653. (concat
  14654. (buffer-substring (1- (point)) (point))
  14655. (if (> (current-column) org-tags-column)
  14656. " "
  14657. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14658. (org-move-overlay org-tags-overlay ov-start ov-end)
  14659. (save-window-excursion
  14660. (if expert
  14661. (set-buffer (get-buffer-create " *Org tags*"))
  14662. (delete-other-windows)
  14663. (split-window-vertically)
  14664. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14665. (erase-buffer)
  14666. (org-set-local 'org-done-keywords done-keywords)
  14667. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14668. (org-fast-tag-insert "Current" current c-face "\n\n")
  14669. (org-fast-tag-show-exit exit-after-next)
  14670. (org-set-current-tags-overlay current ov-prefix)
  14671. (setq tbl fulltable char ?a cnt 0)
  14672. (while (setq e (pop tbl))
  14673. (cond
  14674. ((equal e '(:startgroup))
  14675. (push '() groups) (setq ingroup t)
  14676. (when (not (= cnt 0))
  14677. (setq cnt 0)
  14678. (insert "\n"))
  14679. (insert "{ "))
  14680. ((equal e '(:endgroup))
  14681. (setq ingroup nil cnt 0)
  14682. (insert "}\n"))
  14683. (t
  14684. (setq tg (car e) c2 nil)
  14685. (if (cdr e)
  14686. (setq c (cdr e))
  14687. ;; automatically assign a character.
  14688. (setq c1 (string-to-char
  14689. (downcase (substring
  14690. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14691. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14692. (while (or (rassoc char ntable) (rassoc char table))
  14693. (setq char (1+ char)))
  14694. (setq c2 c1))
  14695. (setq c (or c2 char)))
  14696. (if ingroup (push tg (car groups)))
  14697. (setq tg (org-add-props tg nil 'face
  14698. (cond
  14699. ((not (assoc tg table))
  14700. (org-get-todo-face tg))
  14701. ((member tg current) c-face)
  14702. ((member tg inherited) i-face)
  14703. (t nil))))
  14704. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14705. (insert "[" c "] " tg (make-string
  14706. (- fwidth 4 (length tg)) ?\ ))
  14707. (push (cons tg c) ntable)
  14708. (when (= (setq cnt (1+ cnt)) ncol)
  14709. (insert "\n")
  14710. (if ingroup (insert " "))
  14711. (setq cnt 0)))))
  14712. (setq ntable (nreverse ntable))
  14713. (insert "\n")
  14714. (goto-char (point-min))
  14715. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14716. (fit-window-to-buffer))
  14717. (setq rtn
  14718. (catch 'exit
  14719. (while t
  14720. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14721. (if groups " [!] no groups" " [!]groups")
  14722. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14723. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14724. (cond
  14725. ((= c ?\r) (throw 'exit t))
  14726. ((= c ?!)
  14727. (setq groups (not groups))
  14728. (goto-char (point-min))
  14729. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14730. ((= c ?\C-c)
  14731. (if (not expert)
  14732. (org-fast-tag-show-exit
  14733. (setq exit-after-next (not exit-after-next)))
  14734. (setq expert nil)
  14735. (delete-other-windows)
  14736. (split-window-vertically)
  14737. (org-switch-to-buffer-other-window " *Org tags*")
  14738. (and (fboundp 'fit-window-to-buffer)
  14739. (fit-window-to-buffer))))
  14740. ((or (= c ?\C-g)
  14741. (and (= c ?q) (not (rassoc c ntable))))
  14742. (org-detach-overlay org-tags-overlay)
  14743. (setq quit-flag t))
  14744. ((= c ?\ )
  14745. (setq current nil)
  14746. (if exit-after-next (setq exit-after-next 'now)))
  14747. ((= c ?\t)
  14748. (condition-case nil
  14749. (setq tg (completing-read
  14750. "Tag: "
  14751. (or buffer-tags
  14752. (with-current-buffer buf
  14753. (org-get-buffer-tags)))))
  14754. (quit (setq tg "")))
  14755. (when (string-match "\\S-" tg)
  14756. (add-to-list 'buffer-tags (list tg))
  14757. (if (member tg current)
  14758. (setq current (delete tg current))
  14759. (push tg current)))
  14760. (if exit-after-next (setq exit-after-next 'now)))
  14761. ((setq e (rassoc c todo-table) tg (car e))
  14762. (with-current-buffer buf
  14763. (save-excursion (org-todo tg)))
  14764. (if exit-after-next (setq exit-after-next 'now)))
  14765. ((setq e (rassoc c ntable) tg (car e))
  14766. (if (member tg current)
  14767. (setq current (delete tg current))
  14768. (loop for g in groups do
  14769. (if (member tg g)
  14770. (mapc (lambda (x)
  14771. (setq current (delete x current)))
  14772. g)))
  14773. (push tg current))
  14774. (if exit-after-next (setq exit-after-next 'now))))
  14775. ;; Create a sorted list
  14776. (setq current
  14777. (sort current
  14778. (lambda (a b)
  14779. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14780. (if (eq exit-after-next 'now) (throw 'exit t))
  14781. (goto-char (point-min))
  14782. (beginning-of-line 2)
  14783. (delete-region (point) (point-at-eol))
  14784. (org-fast-tag-insert "Current" current c-face)
  14785. (org-set-current-tags-overlay current ov-prefix)
  14786. (while (re-search-forward
  14787. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14788. (setq tg (match-string 1))
  14789. (add-text-properties
  14790. (match-beginning 1) (match-end 1)
  14791. (list 'face
  14792. (cond
  14793. ((member tg current) c-face)
  14794. ((member tg inherited) i-face)
  14795. (t (get-text-property (match-beginning 1) 'face))))))
  14796. (goto-char (point-min)))))
  14797. (org-detach-overlay org-tags-overlay)
  14798. (if rtn
  14799. (mapconcat 'identity current ":")
  14800. nil))))
  14801. (defun org-get-tags-string ()
  14802. "Get the TAGS string in the current headline."
  14803. (unless (org-on-heading-p t)
  14804. (error "Not on a heading"))
  14805. (save-excursion
  14806. (beginning-of-line 1)
  14807. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14808. (org-match-string-no-properties 1)
  14809. "")))
  14810. (defun org-get-tags ()
  14811. "Get the list of tags specified in the current headline."
  14812. (org-split-string (org-get-tags-string) ":"))
  14813. (defun org-get-buffer-tags ()
  14814. "Get a table of all tags used in the buffer, for completion."
  14815. (let (tags)
  14816. (save-excursion
  14817. (goto-char (point-min))
  14818. (while (re-search-forward
  14819. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14820. (when (equal (char-after (point-at-bol 0)) ?*)
  14821. (mapc (lambda (x) (add-to-list 'tags x))
  14822. (org-split-string (org-match-string-no-properties 1) ":")))))
  14823. (mapcar 'list tags)))
  14824. ;;;; Properties
  14825. ;;; Setting and retrieving properties
  14826. (defconst org-special-properties
  14827. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14828. "TIMESTAMP" "TIMESTAMP_IA")
  14829. "The special properties valid in Org-mode.
  14830. These are properties that are not defined in the property drawer,
  14831. but in some other way.")
  14832. (defconst org-default-properties
  14833. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14834. "LOCATION" "LOGGING" "COLUMNS")
  14835. "Some properties that are used by Org-mode for various purposes.
  14836. Being in this list makes sure that they are offered for completion.")
  14837. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14838. "Regular expression matching the first line of a property drawer.")
  14839. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14840. "Regular expression matching the first line of a property drawer.")
  14841. (defun org-property-action ()
  14842. "Do an action on properties."
  14843. (interactive)
  14844. (let (c)
  14845. (org-at-property-p)
  14846. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14847. (setq c (read-char-exclusive))
  14848. (cond
  14849. ((equal c ?s)
  14850. (call-interactively 'org-set-property))
  14851. ((equal c ?d)
  14852. (call-interactively 'org-delete-property))
  14853. ((equal c ?D)
  14854. (call-interactively 'org-delete-property-globally))
  14855. ((equal c ?c)
  14856. (call-interactively 'org-compute-property-at-point))
  14857. (t (error "No such property action %c" c)))))
  14858. (defun org-at-property-p ()
  14859. "Is the cursor in a property line?"
  14860. ;; FIXME: Does not check if we are actually in the drawer.
  14861. ;; FIXME: also returns true on any drawers.....
  14862. ;; This is used by C-c C-c for property action.
  14863. (save-excursion
  14864. (beginning-of-line 1)
  14865. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14866. (defmacro org-with-point-at (pom &rest body)
  14867. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14868. (declare (indent 1) (debug t))
  14869. `(save-excursion
  14870. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14871. (save-excursion
  14872. (goto-char (or pom (point)))
  14873. ,@body)))
  14874. (defun org-get-property-block (&optional beg end force)
  14875. "Return the (beg . end) range of the body of the property drawer.
  14876. BEG and END can be beginning and end of subtree, if not given
  14877. they will be found.
  14878. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14879. (catch 'exit
  14880. (save-excursion
  14881. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14882. (end (or end (progn (outline-next-heading) (point)))))
  14883. (goto-char beg)
  14884. (if (re-search-forward org-property-start-re end t)
  14885. (setq beg (1+ (match-end 0)))
  14886. (if force
  14887. (save-excursion
  14888. (org-insert-property-drawer)
  14889. (setq end (progn (outline-next-heading) (point))))
  14890. (throw 'exit nil))
  14891. (goto-char beg)
  14892. (if (re-search-forward org-property-start-re end t)
  14893. (setq beg (1+ (match-end 0)))))
  14894. (if (re-search-forward org-property-end-re end t)
  14895. (setq end (match-beginning 0))
  14896. (or force (throw 'exit nil))
  14897. (goto-char beg)
  14898. (setq end beg)
  14899. (org-indent-line-function)
  14900. (insert ":END:\n"))
  14901. (cons beg end)))))
  14902. (defun org-entry-properties (&optional pom which)
  14903. "Get all properties of the entry at point-or-marker POM.
  14904. This includes the TODO keyword, the tags, time strings for deadline,
  14905. scheduled, and clocking, and any additional properties defined in the
  14906. entry. The return value is an alist, keys may occur multiple times
  14907. if the property key was used several times.
  14908. POM may also be nil, in which case the current entry is used.
  14909. If WHICH is nil or `all', get all properties. If WHICH is
  14910. `special' or `standard', only get that subclass."
  14911. (setq which (or which 'all))
  14912. (org-with-point-at pom
  14913. (let ((clockstr (substring org-clock-string 0 -1))
  14914. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14915. beg end range props sum-props key value string clocksum)
  14916. (save-excursion
  14917. (when (condition-case nil (org-back-to-heading t) (error nil))
  14918. (setq beg (point))
  14919. (setq sum-props (get-text-property (point) 'org-summaries))
  14920. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14921. (outline-next-heading)
  14922. (setq end (point))
  14923. (when (memq which '(all special))
  14924. ;; Get the special properties, like TODO and tags
  14925. (goto-char beg)
  14926. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14927. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14928. (when (looking-at org-priority-regexp)
  14929. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14930. (when (and (setq value (org-get-tags-string))
  14931. (string-match "\\S-" value))
  14932. (push (cons "TAGS" value) props))
  14933. (when (setq value (org-get-tags-at))
  14934. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14935. props))
  14936. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14937. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14938. string (if (equal key clockstr)
  14939. (org-no-properties
  14940. (org-trim
  14941. (buffer-substring
  14942. (match-beginning 3) (goto-char (point-at-eol)))))
  14943. (substring (org-match-string-no-properties 3) 1 -1)))
  14944. (unless key
  14945. (if (= (char-after (match-beginning 3)) ?\[)
  14946. (setq key "TIMESTAMP_IA")
  14947. (setq key "TIMESTAMP")))
  14948. (when (or (equal key clockstr) (not (assoc key props)))
  14949. (push (cons key string) props)))
  14950. )
  14951. (when (memq which '(all standard))
  14952. ;; Get the standard properties, like :PORP: ...
  14953. (setq range (org-get-property-block beg end))
  14954. (when range
  14955. (goto-char (car range))
  14956. (while (re-search-forward
  14957. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14958. (cdr range) t)
  14959. (setq key (org-match-string-no-properties 1)
  14960. value (org-trim (or (org-match-string-no-properties 2) "")))
  14961. (unless (member key excluded)
  14962. (push (cons key (or value "")) props)))))
  14963. (if clocksum
  14964. (push (cons "CLOCKSUM"
  14965. (org-column-number-to-string (/ (float clocksum) 60.)
  14966. 'add_times))
  14967. props))
  14968. (append sum-props (nreverse props)))))))
  14969. (defun org-entry-get (pom property &optional inherit)
  14970. "Get value of PROPERTY for entry at point-or-marker POM.
  14971. If INHERIT is non-nil and the entry does not have the property,
  14972. then also check higher levels of the hierarchy.
  14973. If the property is present but empty, the return value is the empty string.
  14974. If the property is not present at all, nil is returned."
  14975. (org-with-point-at pom
  14976. (if inherit
  14977. (org-entry-get-with-inheritance property)
  14978. (if (member property org-special-properties)
  14979. ;; We need a special property. Use brute force, get all properties.
  14980. (cdr (assoc property (org-entry-properties nil 'special)))
  14981. (let ((range (org-get-property-block)))
  14982. (if (and range
  14983. (goto-char (car range))
  14984. (re-search-forward
  14985. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14986. (cdr range) t))
  14987. ;; Found the property, return it.
  14988. (if (match-end 1)
  14989. (org-match-string-no-properties 1)
  14990. "")))))))
  14991. (defun org-entry-delete (pom property)
  14992. "Delete the property PROPERTY from entry at point-or-marker POM."
  14993. (org-with-point-at pom
  14994. (if (member property org-special-properties)
  14995. nil ; cannot delete these properties.
  14996. (let ((range (org-get-property-block)))
  14997. (if (and range
  14998. (goto-char (car range))
  14999. (re-search-forward
  15000. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  15001. (cdr range) t))
  15002. (progn
  15003. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  15004. t)
  15005. nil)))))
  15006. ;; Multi-values properties are properties that contain multiple values
  15007. ;; These values are assumed to be single words, separated by whitespace.
  15008. (defun org-entry-add-to-multivalued-property (pom property value)
  15009. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  15010. (let* ((old (org-entry-get pom property))
  15011. (values (and old (org-split-string old "[ \t]"))))
  15012. (unless (member value values)
  15013. (setq values (cons value values))
  15014. (org-entry-put pom property
  15015. (mapconcat 'identity values " ")))))
  15016. (defun org-entry-remove-from-multivalued-property (pom property value)
  15017. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  15018. (let* ((old (org-entry-get pom property))
  15019. (values (and old (org-split-string old "[ \t]"))))
  15020. (when (member value values)
  15021. (setq values (delete value values))
  15022. (org-entry-put pom property
  15023. (mapconcat 'identity values " ")))))
  15024. (defun org-entry-member-in-multivalued-property (pom property value)
  15025. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  15026. (let* ((old (org-entry-get pom property))
  15027. (values (and old (org-split-string old "[ \t]"))))
  15028. (member value values)))
  15029. (defvar org-entry-property-inherited-from (make-marker))
  15030. (defun org-entry-get-with-inheritance (property)
  15031. "Get entry property, and search higher levels if not present."
  15032. (let (tmp)
  15033. (save-excursion
  15034. (save-restriction
  15035. (widen)
  15036. (catch 'ex
  15037. (while t
  15038. (when (setq tmp (org-entry-get nil property))
  15039. (org-back-to-heading t)
  15040. (move-marker org-entry-property-inherited-from (point))
  15041. (throw 'ex tmp))
  15042. (or (org-up-heading-safe) (throw 'ex nil)))))
  15043. (or tmp (cdr (assoc property org-local-properties))
  15044. (cdr (assoc property org-global-properties))))))
  15045. (defun org-entry-put (pom property value)
  15046. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  15047. (org-with-point-at pom
  15048. (org-back-to-heading t)
  15049. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  15050. range)
  15051. (cond
  15052. ((equal property "TODO")
  15053. (when (and (stringp value) (string-match "\\S-" value)
  15054. (not (member value org-todo-keywords-1)))
  15055. (error "\"%s\" is not a valid TODO state" value))
  15056. (if (or (not value)
  15057. (not (string-match "\\S-" value)))
  15058. (setq value 'none))
  15059. (org-todo value)
  15060. (org-set-tags nil 'align))
  15061. ((equal property "PRIORITY")
  15062. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  15063. (string-to-char value) ?\ ))
  15064. (org-set-tags nil 'align))
  15065. ((equal property "SCHEDULED")
  15066. (if (re-search-forward org-scheduled-time-regexp end t)
  15067. (cond
  15068. ((eq value 'earlier) (org-timestamp-change -1 'day))
  15069. ((eq value 'later) (org-timestamp-change 1 'day))
  15070. (t (call-interactively 'org-schedule)))
  15071. (call-interactively 'org-schedule)))
  15072. ((equal property "DEADLINE")
  15073. (if (re-search-forward org-deadline-time-regexp end t)
  15074. (cond
  15075. ((eq value 'earlier) (org-timestamp-change -1 'day))
  15076. ((eq value 'later) (org-timestamp-change 1 'day))
  15077. (t (call-interactively 'org-deadline)))
  15078. (call-interactively 'org-deadline)))
  15079. ((member property org-special-properties)
  15080. (error "The %s property can not yet be set with `org-entry-put'"
  15081. property))
  15082. (t ; a non-special property
  15083. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  15084. (setq range (org-get-property-block beg end 'force))
  15085. (goto-char (car range))
  15086. (if (re-search-forward
  15087. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  15088. (progn
  15089. (delete-region (match-beginning 1) (match-end 1))
  15090. (goto-char (match-beginning 1)))
  15091. (goto-char (cdr range))
  15092. (insert "\n")
  15093. (backward-char 1)
  15094. (org-indent-line-function)
  15095. (insert ":" property ":"))
  15096. (and value (insert " " value))
  15097. (org-indent-line-function)))))))
  15098. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  15099. "Get all property keys in the current buffer.
  15100. With INCLUDE-SPECIALS, also list the special properties that relect things
  15101. like tags and TODO state.
  15102. With INCLUDE-DEFAULTS, also include properties that has special meaning
  15103. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  15104. With INCLUDE-COLUMNS, also include property names given in COLUMN
  15105. formats in the current buffer."
  15106. (let (rtn range cfmt cols s p)
  15107. (save-excursion
  15108. (save-restriction
  15109. (widen)
  15110. (goto-char (point-min))
  15111. (while (re-search-forward org-property-start-re nil t)
  15112. (setq range (org-get-property-block))
  15113. (goto-char (car range))
  15114. (while (re-search-forward
  15115. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  15116. (cdr range) t)
  15117. (add-to-list 'rtn (org-match-string-no-properties 1)))
  15118. (outline-next-heading))))
  15119. (when include-specials
  15120. (setq rtn (append org-special-properties rtn)))
  15121. (when include-defaults
  15122. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  15123. (when include-columns
  15124. (save-excursion
  15125. (save-restriction
  15126. (widen)
  15127. (goto-char (point-min))
  15128. (while (re-search-forward
  15129. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  15130. nil t)
  15131. (setq cfmt (match-string 2) s 0)
  15132. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  15133. cfmt s)
  15134. (setq s (match-end 0)
  15135. p (match-string 1 cfmt))
  15136. (unless (or (equal p "ITEM")
  15137. (member p org-special-properties))
  15138. (add-to-list 'rtn (match-string 1 cfmt))))))))
  15139. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  15140. (defun org-property-values (key)
  15141. "Return a list of all values of property KEY."
  15142. (save-excursion
  15143. (save-restriction
  15144. (widen)
  15145. (goto-char (point-min))
  15146. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  15147. values)
  15148. (while (re-search-forward re nil t)
  15149. (add-to-list 'values (org-trim (match-string 1))))
  15150. (delete "" values)))))
  15151. (defun org-insert-property-drawer ()
  15152. "Insert a property drawer into the current entry."
  15153. (interactive)
  15154. (org-back-to-heading t)
  15155. (looking-at outline-regexp)
  15156. (let ((indent (- (match-end 0)(match-beginning 0)))
  15157. (beg (point))
  15158. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15159. end hiddenp)
  15160. (outline-next-heading)
  15161. (setq end (point))
  15162. (goto-char beg)
  15163. (while (re-search-forward re end t))
  15164. (setq hiddenp (org-invisible-p))
  15165. (end-of-line 1)
  15166. (and (equal (char-after) ?\n) (forward-char 1))
  15167. (org-skip-over-state-notes)
  15168. (skip-chars-backward " \t\n\r")
  15169. (if (eq (char-before) ?*) (forward-char 1))
  15170. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15171. (beginning-of-line 0)
  15172. (indent-to-column indent)
  15173. (beginning-of-line 2)
  15174. (indent-to-column indent)
  15175. (beginning-of-line 0)
  15176. (if hiddenp
  15177. (save-excursion
  15178. (org-back-to-heading t)
  15179. (hide-entry))
  15180. (org-flag-drawer t))))
  15181. (defun org-set-property (property value)
  15182. "In the current entry, set PROPERTY to VALUE.
  15183. When called interactively, this will prompt for a property name, offering
  15184. completion on existing and default properties. And then it will prompt
  15185. for a value, offering competion either on allowed values (via an inherited
  15186. xxx_ALL property) or on existing values in other instances of this property
  15187. in the current file."
  15188. (interactive
  15189. (let* ((prop (completing-read
  15190. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15191. (cur (org-entry-get nil prop))
  15192. (allowed (org-property-get-allowed-values nil prop 'table))
  15193. (existing (mapcar 'list (org-property-values prop)))
  15194. (val (if allowed
  15195. (completing-read "Value: " allowed nil 'req-match)
  15196. (completing-read
  15197. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15198. (concat "[" cur "]") "")
  15199. ": ")
  15200. existing nil nil "" nil cur))))
  15201. (list prop (if (equal val "") cur val))))
  15202. (unless (equal (org-entry-get nil property) value)
  15203. (org-entry-put nil property value)))
  15204. (defun org-delete-property (property)
  15205. "In the current entry, delete PROPERTY."
  15206. (interactive
  15207. (let* ((prop (completing-read
  15208. "Property: " (org-entry-properties nil 'standard))))
  15209. (list prop)))
  15210. (message "Property %s %s" property
  15211. (if (org-entry-delete nil property)
  15212. "deleted"
  15213. "was not present in the entry")))
  15214. (defun org-delete-property-globally (property)
  15215. "Remove PROPERTY globally, from all entries."
  15216. (interactive
  15217. (let* ((prop (completing-read
  15218. "Globally remove property: "
  15219. (mapcar 'list (org-buffer-property-keys)))))
  15220. (list prop)))
  15221. (save-excursion
  15222. (save-restriction
  15223. (widen)
  15224. (goto-char (point-min))
  15225. (let ((cnt 0))
  15226. (while (re-search-forward
  15227. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15228. nil t)
  15229. (setq cnt (1+ cnt))
  15230. (replace-match ""))
  15231. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15232. (defvar org-columns-current-fmt-compiled) ; defined below
  15233. (defun org-compute-property-at-point ()
  15234. "Compute the property at point.
  15235. This looks for an enclosing column format, extracts the operator and
  15236. then applies it to the proerty in the column format's scope."
  15237. (interactive)
  15238. (unless (org-at-property-p)
  15239. (error "Not at a property"))
  15240. (let ((prop (org-match-string-no-properties 2)))
  15241. (org-columns-get-format-and-top-level)
  15242. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15243. (error "No operator defined for property %s" prop))
  15244. (org-columns-compute prop)))
  15245. (defun org-property-get-allowed-values (pom property &optional table)
  15246. "Get allowed values for the property PROPERTY.
  15247. When TABLE is non-nil, return an alist that can directly be used for
  15248. completion."
  15249. (let (vals)
  15250. (cond
  15251. ((equal property "TODO")
  15252. (setq vals (org-with-point-at pom
  15253. (append org-todo-keywords-1 '("")))))
  15254. ((equal property "PRIORITY")
  15255. (let ((n org-lowest-priority))
  15256. (while (>= n org-highest-priority)
  15257. (push (char-to-string n) vals)
  15258. (setq n (1- n)))))
  15259. ((member property org-special-properties))
  15260. (t
  15261. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15262. (when (and vals (string-match "\\S-" vals))
  15263. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15264. (setq vals (mapcar (lambda (x)
  15265. (cond ((stringp x) x)
  15266. ((numberp x) (number-to-string x))
  15267. ((symbolp x) (symbol-name x))
  15268. (t "???")))
  15269. vals)))))
  15270. (if table (mapcar 'list vals) vals)))
  15271. (defun org-property-previous-allowed-value (&optional previous)
  15272. "Switch to the next allowed value for this property."
  15273. (interactive)
  15274. (org-property-next-allowed-value t))
  15275. (defun org-property-next-allowed-value (&optional previous)
  15276. "Switch to the next allowed value for this property."
  15277. (interactive)
  15278. (unless (org-at-property-p)
  15279. (error "Not at a property"))
  15280. (let* ((key (match-string 2))
  15281. (value (match-string 3))
  15282. (allowed (or (org-property-get-allowed-values (point) key)
  15283. (and (member value '("[ ]" "[-]" "[X]"))
  15284. '("[ ]" "[X]"))))
  15285. nval)
  15286. (unless allowed
  15287. (error "Allowed values for this property have not been defined"))
  15288. (if previous (setq allowed (reverse allowed)))
  15289. (if (member value allowed)
  15290. (setq nval (car (cdr (member value allowed)))))
  15291. (setq nval (or nval (car allowed)))
  15292. (if (equal nval value)
  15293. (error "Only one allowed value for this property"))
  15294. (org-at-property-p)
  15295. (replace-match (concat " :" key ": " nval) t t)
  15296. (org-indent-line-function)
  15297. (beginning-of-line 1)
  15298. (skip-chars-forward " \t")))
  15299. (defun org-find-entry-with-id (ident)
  15300. "Locate the entry that contains the ID property with exact value IDENT.
  15301. IDENT can be a string, a symbol or a number, this function will search for
  15302. the string representation of it.
  15303. Return the position where this entry starts, or nil if there is no such entry."
  15304. (let ((id (cond
  15305. ((stringp ident) ident)
  15306. ((symbol-name ident) (symbol-name ident))
  15307. ((numberp ident) (number-to-string ident))
  15308. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15309. (case-fold-search nil))
  15310. (save-excursion
  15311. (save-restriction
  15312. (widen)
  15313. (goto-char (point-min))
  15314. (when (re-search-forward
  15315. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15316. nil t)
  15317. (org-back-to-heading)
  15318. (point))))))
  15319. ;;; Column View
  15320. (defvar org-columns-overlays nil
  15321. "Holds the list of current column overlays.")
  15322. (defvar org-columns-current-fmt nil
  15323. "Local variable, holds the currently active column format.")
  15324. (defvar org-columns-current-fmt-compiled nil
  15325. "Local variable, holds the currently active column format.
  15326. This is the compiled version of the format.")
  15327. (defvar org-columns-current-widths nil
  15328. "Loval variable, holds the currently widths of fields.")
  15329. (defvar org-columns-current-maxwidths nil
  15330. "Loval variable, holds the currently active maximum column widths.")
  15331. (defvar org-columns-begin-marker (make-marker)
  15332. "Points to the position where last a column creation command was called.")
  15333. (defvar org-columns-top-level-marker (make-marker)
  15334. "Points to the position where current columns region starts.")
  15335. (defvar org-columns-map (make-sparse-keymap)
  15336. "The keymap valid in column display.")
  15337. (defun org-columns-content ()
  15338. "Switch to contents view while in columns view."
  15339. (interactive)
  15340. (org-overview)
  15341. (org-content))
  15342. (org-defkey org-columns-map "c" 'org-columns-content)
  15343. (org-defkey org-columns-map "o" 'org-overview)
  15344. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15345. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15346. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15347. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15348. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15349. (org-defkey org-columns-map "q" 'org-columns-quit)
  15350. (org-defkey org-columns-map "r" 'org-columns-redo)
  15351. (org-defkey org-columns-map "g" 'org-columns-redo)
  15352. (org-defkey org-columns-map [left] 'backward-char)
  15353. (org-defkey org-columns-map "\M-b" 'backward-char)
  15354. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15355. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15356. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15357. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15358. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15359. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15360. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15361. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15362. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15363. (org-defkey org-columns-map ">" 'org-columns-widen)
  15364. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15365. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15366. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15367. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15368. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15369. '("Column"
  15370. ["Edit property" org-columns-edit-value t]
  15371. ["Next allowed value" org-columns-next-allowed-value t]
  15372. ["Previous allowed value" org-columns-previous-allowed-value t]
  15373. ["Show full value" org-columns-show-value t]
  15374. ["Edit allowed values" org-columns-edit-allowed t]
  15375. "--"
  15376. ["Edit column attributes" org-columns-edit-attributes t]
  15377. ["Increase column width" org-columns-widen t]
  15378. ["Decrease column width" org-columns-narrow t]
  15379. "--"
  15380. ["Move column right" org-columns-move-right t]
  15381. ["Move column left" org-columns-move-left t]
  15382. ["Add column" org-columns-new t]
  15383. ["Delete column" org-columns-delete t]
  15384. "--"
  15385. ["CONTENTS" org-columns-content t]
  15386. ["OVERVIEW" org-overview t]
  15387. ["Refresh columns display" org-columns-redo t]
  15388. "--"
  15389. ["Open link" org-columns-open-link t]
  15390. "--"
  15391. ["Quit" org-columns-quit t]))
  15392. (defun org-columns-new-overlay (beg end &optional string face)
  15393. "Create a new column overlay and add it to the list."
  15394. (let ((ov (org-make-overlay beg end)))
  15395. (org-overlay-put ov 'face (or face 'secondary-selection))
  15396. (org-overlay-display ov string face)
  15397. (push ov org-columns-overlays)
  15398. ov))
  15399. (defun org-columns-display-here (&optional props)
  15400. "Overlay the current line with column display."
  15401. (interactive)
  15402. (let* ((fmt org-columns-current-fmt-compiled)
  15403. (beg (point-at-bol))
  15404. (level-face (save-excursion
  15405. (beginning-of-line 1)
  15406. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15407. (org-get-level-face 2))))
  15408. (color (list :foreground
  15409. (face-attribute (or level-face 'default) :foreground)))
  15410. props pom property ass width f string ov column val modval)
  15411. ;; Check if the entry is in another buffer.
  15412. (unless props
  15413. (if (eq major-mode 'org-agenda-mode)
  15414. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15415. (get-text-property (point) 'org-marker))
  15416. props (if pom (org-entry-properties pom) nil))
  15417. (setq props (org-entry-properties nil))))
  15418. ;; Walk the format
  15419. (while (setq column (pop fmt))
  15420. (setq property (car column)
  15421. ass (if (equal property "ITEM")
  15422. (cons "ITEM"
  15423. (save-match-data
  15424. (org-no-properties
  15425. (org-remove-tabs
  15426. (buffer-substring-no-properties
  15427. (point-at-bol) (point-at-eol))))))
  15428. (assoc property props))
  15429. width (or (cdr (assoc property org-columns-current-maxwidths))
  15430. (nth 2 column)
  15431. (length property))
  15432. f (format "%%-%d.%ds | " width width)
  15433. val (or (cdr ass) "")
  15434. modval (if (equal property "ITEM")
  15435. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15436. string (format f (or modval val)))
  15437. ;; Create the overlay
  15438. (org-unmodified
  15439. (setq ov (org-columns-new-overlay
  15440. beg (setq beg (1+ beg)) string
  15441. (list color 'org-column)))
  15442. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15443. (org-overlay-put ov 'keymap org-columns-map)
  15444. (org-overlay-put ov 'org-columns-key property)
  15445. (org-overlay-put ov 'org-columns-value (cdr ass))
  15446. (org-overlay-put ov 'org-columns-value-modified modval)
  15447. (org-overlay-put ov 'org-columns-pom pom)
  15448. (org-overlay-put ov 'org-columns-format f))
  15449. (if (or (not (char-after beg))
  15450. (equal (char-after beg) ?\n))
  15451. (let ((inhibit-read-only t))
  15452. (save-excursion
  15453. (goto-char beg)
  15454. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15455. ;; Make the rest of the line disappear.
  15456. (org-unmodified
  15457. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15458. (org-overlay-put ov 'invisible t)
  15459. (org-overlay-put ov 'keymap org-columns-map)
  15460. (org-overlay-put ov 'intangible t)
  15461. (push ov org-columns-overlays)
  15462. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15463. (org-overlay-put ov 'keymap org-columns-map)
  15464. (push ov org-columns-overlays)
  15465. (let ((inhibit-read-only t))
  15466. (put-text-property (max (point-min) (1- (point-at-bol)))
  15467. (min (point-max) (1+ (point-at-eol)))
  15468. 'read-only "Type `e' to edit property")))))
  15469. (defvar org-previous-header-line-format nil
  15470. "The header line format before column view was turned on.")
  15471. (defvar org-columns-inhibit-recalculation nil
  15472. "Inhibit recomputing of columns on column view startup.")
  15473. (defvar header-line-format)
  15474. (defun org-columns-display-here-title ()
  15475. "Overlay the newline before the current line with the table title."
  15476. (interactive)
  15477. (let ((fmt org-columns-current-fmt-compiled)
  15478. string (title "")
  15479. property width f column str widths)
  15480. (while (setq column (pop fmt))
  15481. (setq property (car column)
  15482. str (or (nth 1 column) property)
  15483. width (or (cdr (assoc property org-columns-current-maxwidths))
  15484. (nth 2 column)
  15485. (length str))
  15486. widths (push width widths)
  15487. f (format "%%-%d.%ds | " width width)
  15488. string (format f str)
  15489. title (concat title string)))
  15490. (setq title (concat
  15491. (org-add-props " " nil 'display '(space :align-to 0))
  15492. (org-add-props title nil 'face '(:weight bold :underline t))))
  15493. (org-set-local 'org-previous-header-line-format header-line-format)
  15494. (org-set-local 'org-columns-current-widths (nreverse widths))
  15495. (setq header-line-format title)))
  15496. (defun org-columns-remove-overlays ()
  15497. "Remove all currently active column overlays."
  15498. (interactive)
  15499. (when (marker-buffer org-columns-begin-marker)
  15500. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15501. (when (local-variable-p 'org-previous-header-line-format)
  15502. (setq header-line-format org-previous-header-line-format)
  15503. (kill-local-variable 'org-previous-header-line-format))
  15504. (move-marker org-columns-begin-marker nil)
  15505. (move-marker org-columns-top-level-marker nil)
  15506. (org-unmodified
  15507. (mapc 'org-delete-overlay org-columns-overlays)
  15508. (setq org-columns-overlays nil)
  15509. (let ((inhibit-read-only t))
  15510. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15511. (defun org-columns-cleanup-item (item fmt)
  15512. "Remove from ITEM what is a column in the format FMT."
  15513. (if (not org-complex-heading-regexp)
  15514. item
  15515. (when (string-match org-complex-heading-regexp item)
  15516. (concat
  15517. (org-add-props (concat (match-string 1 item) " ") nil
  15518. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15519. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15520. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15521. " " (match-string 4 item)
  15522. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15523. (defun org-columns-show-value ()
  15524. "Show the full value of the property."
  15525. (interactive)
  15526. (let ((value (get-char-property (point) 'org-columns-value)))
  15527. (message "Value is: %s" (or value ""))))
  15528. (defun org-columns-quit ()
  15529. "Remove the column overlays and in this way exit column editing."
  15530. (interactive)
  15531. (org-unmodified
  15532. (org-columns-remove-overlays)
  15533. (let ((inhibit-read-only t))
  15534. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15535. (when (eq major-mode 'org-agenda-mode)
  15536. (message
  15537. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15538. (defun org-columns-check-computed ()
  15539. "Check if this column value is computed.
  15540. If yes, throw an error indicating that changing it does not make sense."
  15541. (let ((val (get-char-property (point) 'org-columns-value)))
  15542. (when (and (stringp val)
  15543. (get-char-property 0 'org-computed val))
  15544. (error "This value is computed from the entry's children"))))
  15545. (defun org-columns-todo (&optional arg)
  15546. "Change the TODO state during column view."
  15547. (interactive "P")
  15548. (org-columns-edit-value "TODO"))
  15549. (defun org-columns-set-tags-or-toggle (&optional arg)
  15550. "Toggle checkbox at point, or set tags for current headline."
  15551. (interactive "P")
  15552. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15553. (get-char-property (point) 'org-columns-value))
  15554. (org-columns-next-allowed-value)
  15555. (org-columns-edit-value "TAGS")))
  15556. (defun org-columns-edit-value (&optional key)
  15557. "Edit the value of the property at point in column view.
  15558. Where possible, use the standard interface for changing this line."
  15559. (interactive)
  15560. (org-columns-check-computed)
  15561. (let* ((external-key key)
  15562. (col (current-column))
  15563. (key (or key (get-char-property (point) 'org-columns-key)))
  15564. (value (get-char-property (point) 'org-columns-value))
  15565. (bol (point-at-bol)) (eol (point-at-eol))
  15566. (pom (or (get-text-property bol 'org-hd-marker)
  15567. (point))) ; keep despite of compiler waring
  15568. (line-overlays
  15569. (delq nil (mapcar (lambda (x)
  15570. (and (eq (overlay-buffer x) (current-buffer))
  15571. (>= (overlay-start x) bol)
  15572. (<= (overlay-start x) eol)
  15573. x))
  15574. org-columns-overlays)))
  15575. nval eval allowed)
  15576. (cond
  15577. ((equal key "CLOCKSUM")
  15578. (error "This special column cannot be edited"))
  15579. ((equal key "ITEM")
  15580. (setq eval '(org-with-point-at pom
  15581. (org-edit-headline))))
  15582. ((equal key "TODO")
  15583. (setq eval '(org-with-point-at pom
  15584. (let ((current-prefix-arg
  15585. (if external-key current-prefix-arg '(4))))
  15586. (call-interactively 'org-todo)))))
  15587. ((equal key "PRIORITY")
  15588. (setq eval '(org-with-point-at pom
  15589. (call-interactively 'org-priority))))
  15590. ((equal key "TAGS")
  15591. (setq eval '(org-with-point-at pom
  15592. (let ((org-fast-tag-selection-single-key
  15593. (if (eq org-fast-tag-selection-single-key 'expert)
  15594. t org-fast-tag-selection-single-key)))
  15595. (call-interactively 'org-set-tags)))))
  15596. ((equal key "DEADLINE")
  15597. (setq eval '(org-with-point-at pom
  15598. (call-interactively 'org-deadline))))
  15599. ((equal key "SCHEDULED")
  15600. (setq eval '(org-with-point-at pom
  15601. (call-interactively 'org-schedule))))
  15602. (t
  15603. (setq allowed (org-property-get-allowed-values pom key 'table))
  15604. (if allowed
  15605. (setq nval (completing-read "Value: " allowed nil t))
  15606. (setq nval (read-string "Edit: " value)))
  15607. (setq nval (org-trim nval))
  15608. (when (not (equal nval value))
  15609. (setq eval '(org-entry-put pom key nval)))))
  15610. (when eval
  15611. (let ((inhibit-read-only t))
  15612. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15613. (unwind-protect
  15614. (progn
  15615. (setq org-columns-overlays
  15616. (org-delete-all line-overlays org-columns-overlays))
  15617. (mapc 'org-delete-overlay line-overlays)
  15618. (org-columns-eval eval))
  15619. (org-columns-display-here))))
  15620. (move-to-column col)
  15621. (if (and (org-mode-p)
  15622. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15623. (org-columns-update key))))
  15624. (defun org-edit-headline () ; FIXME: this is not columns specific
  15625. "Edit the current headline, the part without TODO keyword, TAGS."
  15626. (org-back-to-heading)
  15627. (when (looking-at org-todo-line-regexp)
  15628. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15629. (txt (match-string 3))
  15630. (post "")
  15631. txt2)
  15632. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15633. (setq post (match-string 0 txt)
  15634. txt (substring txt 0 (match-beginning 0))))
  15635. (setq txt2 (read-string "Edit: " txt))
  15636. (when (not (equal txt txt2))
  15637. (beginning-of-line 1)
  15638. (insert pre txt2 post)
  15639. (delete-region (point) (point-at-eol))
  15640. (org-set-tags nil t)))))
  15641. (defun org-columns-edit-allowed ()
  15642. "Edit the list of allowed values for the current property."
  15643. (interactive)
  15644. (let* ((key (get-char-property (point) 'org-columns-key))
  15645. (key1 (concat key "_ALL"))
  15646. (allowed (org-entry-get (point) key1 t))
  15647. nval)
  15648. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15649. (setq nval (read-string "Allowed: " allowed))
  15650. (org-entry-put
  15651. (cond ((marker-position org-entry-property-inherited-from)
  15652. org-entry-property-inherited-from)
  15653. ((marker-position org-columns-top-level-marker)
  15654. org-columns-top-level-marker))
  15655. key1 nval)))
  15656. (defmacro org-no-warnings (&rest body)
  15657. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15658. (defun org-columns-eval (form)
  15659. (let (hidep)
  15660. (save-excursion
  15661. (beginning-of-line 1)
  15662. ;; `next-line' is needed here, because it skips invisible line.
  15663. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15664. (setq hidep (org-on-heading-p 1)))
  15665. (eval form)
  15666. (and hidep (hide-entry))))
  15667. (defun org-columns-previous-allowed-value ()
  15668. "Switch to the previous allowed value for this column."
  15669. (interactive)
  15670. (org-columns-next-allowed-value t))
  15671. (defun org-columns-next-allowed-value (&optional previous)
  15672. "Switch to the next allowed value for this column."
  15673. (interactive)
  15674. (org-columns-check-computed)
  15675. (let* ((col (current-column))
  15676. (key (get-char-property (point) 'org-columns-key))
  15677. (value (get-char-property (point) 'org-columns-value))
  15678. (bol (point-at-bol)) (eol (point-at-eol))
  15679. (pom (or (get-text-property bol 'org-hd-marker)
  15680. (point))) ; keep despite of compiler waring
  15681. (line-overlays
  15682. (delq nil (mapcar (lambda (x)
  15683. (and (eq (overlay-buffer x) (current-buffer))
  15684. (>= (overlay-start x) bol)
  15685. (<= (overlay-start x) eol)
  15686. x))
  15687. org-columns-overlays)))
  15688. (allowed (or (org-property-get-allowed-values pom key)
  15689. (and (memq
  15690. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15691. '(checkbox checkbox-n-of-m checkbox-percent))
  15692. '("[ ]" "[X]"))))
  15693. nval)
  15694. (when (equal key "ITEM")
  15695. (error "Cannot edit item headline from here"))
  15696. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15697. (error "Allowed values for this property have not been defined"))
  15698. (if (member key '("SCHEDULED" "DEADLINE"))
  15699. (setq nval (if previous 'earlier 'later))
  15700. (if previous (setq allowed (reverse allowed)))
  15701. (if (member value allowed)
  15702. (setq nval (car (cdr (member value allowed)))))
  15703. (setq nval (or nval (car allowed)))
  15704. (if (equal nval value)
  15705. (error "Only one allowed value for this property")))
  15706. (let ((inhibit-read-only t))
  15707. (remove-text-properties (1- bol) eol '(read-only t))
  15708. (unwind-protect
  15709. (progn
  15710. (setq org-columns-overlays
  15711. (org-delete-all line-overlays org-columns-overlays))
  15712. (mapc 'org-delete-overlay line-overlays)
  15713. (org-columns-eval '(org-entry-put pom key nval)))
  15714. (org-columns-display-here)))
  15715. (move-to-column col)
  15716. (if (and (org-mode-p)
  15717. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15718. (org-columns-update key))))
  15719. (defun org-verify-version (task)
  15720. (cond
  15721. ((eq task 'columns)
  15722. (if (or (featurep 'xemacs)
  15723. (< emacs-major-version 22))
  15724. (error "Emacs 22 is required for the columns feature")))))
  15725. (defun org-columns-open-link (&optional arg)
  15726. (interactive "P")
  15727. (let ((value (get-char-property (point) 'org-columns-value)))
  15728. (org-open-link-from-string value arg)))
  15729. (defun org-open-link-from-string (s &optional arg)
  15730. "Open a link in the string S, as if it was in Org-mode."
  15731. (interactive)
  15732. (with-temp-buffer
  15733. (let ((org-inhibit-startup t))
  15734. (org-mode)
  15735. (insert s)
  15736. (goto-char (point-min))
  15737. (org-open-at-point arg))))
  15738. (defun org-columns-get-format-and-top-level ()
  15739. (let (fmt)
  15740. (when (condition-case nil (org-back-to-heading) (error nil))
  15741. (move-marker org-entry-property-inherited-from nil)
  15742. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15743. (setq fmt (or fmt org-columns-default-format))
  15744. (org-set-local 'org-columns-current-fmt fmt)
  15745. (org-columns-compile-format fmt)
  15746. (if (marker-position org-entry-property-inherited-from)
  15747. (move-marker org-columns-top-level-marker
  15748. org-entry-property-inherited-from)
  15749. (move-marker org-columns-top-level-marker (point)))
  15750. fmt))
  15751. (defun org-columns ()
  15752. "Turn on column view on an org-mode file."
  15753. (interactive)
  15754. (org-verify-version 'columns)
  15755. (org-columns-remove-overlays)
  15756. (move-marker org-columns-begin-marker (point))
  15757. (let (beg end fmt cache maxwidths)
  15758. (setq fmt (org-columns-get-format-and-top-level))
  15759. (save-excursion
  15760. (goto-char org-columns-top-level-marker)
  15761. (setq beg (point))
  15762. (unless org-columns-inhibit-recalculation
  15763. (org-columns-compute-all))
  15764. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15765. (point-max)))
  15766. ;; Get and cache the properties
  15767. (goto-char beg)
  15768. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15769. (save-excursion
  15770. (save-restriction
  15771. (narrow-to-region beg end)
  15772. (org-clock-sum))))
  15773. (while (re-search-forward (concat "^" outline-regexp) end t)
  15774. (push (cons (org-current-line) (org-entry-properties)) cache))
  15775. (when cache
  15776. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15777. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15778. (org-columns-display-here-title)
  15779. (mapc (lambda (x)
  15780. (goto-line (car x))
  15781. (org-columns-display-here (cdr x)))
  15782. cache)))))
  15783. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15784. "Insert a new column, to the leeft o the current column."
  15785. (interactive)
  15786. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15787. cell)
  15788. (setq prop (completing-read
  15789. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15790. nil nil prop))
  15791. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15792. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15793. (if (string-match "\\S-" width)
  15794. (setq width (string-to-number width))
  15795. (setq width nil))
  15796. (setq fmt (completing-read "Summary [none]: "
  15797. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15798. nil t))
  15799. (if (string-match "\\S-" fmt)
  15800. (setq fmt (intern fmt))
  15801. (setq fmt nil))
  15802. (if (eq fmt 'none) (setq fmt nil))
  15803. (if editp
  15804. (progn
  15805. (setcar editp prop)
  15806. (setcdr editp (list title width nil fmt)))
  15807. (setq cell (nthcdr (1- (current-column))
  15808. org-columns-current-fmt-compiled))
  15809. (setcdr cell (cons (list prop title width nil fmt)
  15810. (cdr cell))))
  15811. (org-columns-store-format)
  15812. (org-columns-redo)))
  15813. (defun org-columns-delete ()
  15814. "Delete the column at point from columns view."
  15815. (interactive)
  15816. (let* ((n (current-column))
  15817. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15818. (when (y-or-n-p
  15819. (format "Are you sure you want to remove column \"%s\"? " title))
  15820. (setq org-columns-current-fmt-compiled
  15821. (delq (nth n org-columns-current-fmt-compiled)
  15822. org-columns-current-fmt-compiled))
  15823. (org-columns-store-format)
  15824. (org-columns-redo)
  15825. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15826. (backward-char 1)))))
  15827. (defun org-columns-edit-attributes ()
  15828. "Edit the attributes of the current column."
  15829. (interactive)
  15830. (let* ((n (current-column))
  15831. (info (nth n org-columns-current-fmt-compiled)))
  15832. (apply 'org-columns-new info)))
  15833. (defun org-columns-widen (arg)
  15834. "Make the column wider by ARG characters."
  15835. (interactive "p")
  15836. (let* ((n (current-column))
  15837. (entry (nth n org-columns-current-fmt-compiled))
  15838. (width (or (nth 2 entry)
  15839. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15840. (setq width (max 1 (+ width arg)))
  15841. (setcar (nthcdr 2 entry) width)
  15842. (org-columns-store-format)
  15843. (org-columns-redo)))
  15844. (defun org-columns-narrow (arg)
  15845. "Make the column nrrower by ARG characters."
  15846. (interactive "p")
  15847. (org-columns-widen (- arg)))
  15848. (defun org-columns-move-right ()
  15849. "Swap this column with the one to the right."
  15850. (interactive)
  15851. (let* ((n (current-column))
  15852. (cell (nthcdr n org-columns-current-fmt-compiled))
  15853. e)
  15854. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15855. (error "Cannot shift this column further to the right"))
  15856. (setq e (car cell))
  15857. (setcar cell (car (cdr cell)))
  15858. (setcdr cell (cons e (cdr (cdr cell))))
  15859. (org-columns-store-format)
  15860. (org-columns-redo)
  15861. (forward-char 1)))
  15862. (defun org-columns-move-left ()
  15863. "Swap this column with the one to the left."
  15864. (interactive)
  15865. (let* ((n (current-column)))
  15866. (when (= n 0)
  15867. (error "Cannot shift this column further to the left"))
  15868. (backward-char 1)
  15869. (org-columns-move-right)
  15870. (backward-char 1)))
  15871. (defun org-columns-store-format ()
  15872. "Store the text version of the current columns format in appropriate place.
  15873. This is either in the COLUMNS property of the node starting the current column
  15874. display, or in the #+COLUMNS line of the current buffer."
  15875. (let (fmt (cnt 0))
  15876. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15877. (org-set-local 'org-columns-current-fmt fmt)
  15878. (if (marker-position org-columns-top-level-marker)
  15879. (save-excursion
  15880. (goto-char org-columns-top-level-marker)
  15881. (if (and (org-at-heading-p)
  15882. (org-entry-get nil "COLUMNS"))
  15883. (org-entry-put nil "COLUMNS" fmt)
  15884. (goto-char (point-min))
  15885. ;; Overwrite all #+COLUMNS lines....
  15886. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15887. (setq cnt (1+ cnt))
  15888. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15889. (unless (> cnt 0)
  15890. (goto-char (point-min))
  15891. (or (org-on-heading-p t) (outline-next-heading))
  15892. (let ((inhibit-read-only t))
  15893. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15894. (org-set-local 'org-columns-default-format fmt))))))
  15895. (defvar org-overriding-columns-format nil
  15896. "When set, overrides any other definition.")
  15897. (defvar org-agenda-view-columns-initially nil
  15898. "When set, switch to columns view immediately after creating the agenda.")
  15899. (defun org-agenda-columns ()
  15900. "Turn on column view in the agenda."
  15901. (interactive)
  15902. (org-verify-version 'columns)
  15903. (org-columns-remove-overlays)
  15904. (move-marker org-columns-begin-marker (point))
  15905. (let (fmt cache maxwidths m)
  15906. (cond
  15907. ((and (local-variable-p 'org-overriding-columns-format)
  15908. org-overriding-columns-format)
  15909. (setq fmt org-overriding-columns-format))
  15910. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15911. (setq fmt (org-entry-get m "COLUMNS" t)))
  15912. ((and (boundp 'org-columns-current-fmt)
  15913. (local-variable-p 'org-columns-current-fmt)
  15914. org-columns-current-fmt)
  15915. (setq fmt org-columns-current-fmt))
  15916. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15917. (setq m (get-text-property m 'org-hd-marker))
  15918. (setq fmt (org-entry-get m "COLUMNS" t))))
  15919. (setq fmt (or fmt org-columns-default-format))
  15920. (org-set-local 'org-columns-current-fmt fmt)
  15921. (org-columns-compile-format fmt)
  15922. (save-excursion
  15923. ;; Get and cache the properties
  15924. (goto-char (point-min))
  15925. (while (not (eobp))
  15926. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15927. (get-text-property (point) 'org-marker)))
  15928. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15929. (beginning-of-line 2))
  15930. (when cache
  15931. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15932. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15933. (org-columns-display-here-title)
  15934. (mapc (lambda (x)
  15935. (goto-line (car x))
  15936. (org-columns-display-here (cdr x)))
  15937. cache)))))
  15938. (defun org-columns-get-autowidth-alist (s cache)
  15939. "Derive the maximum column widths from the format and the cache."
  15940. (let ((start 0) rtn)
  15941. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15942. (push (cons (match-string 1 s) 1) rtn)
  15943. (setq start (match-end 0)))
  15944. (mapc (lambda (x)
  15945. (setcdr x (apply 'max
  15946. (mapcar
  15947. (lambda (y)
  15948. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15949. cache))))
  15950. rtn)
  15951. rtn))
  15952. (defun org-columns-compute-all ()
  15953. "Compute all columns that have operators defined."
  15954. (org-unmodified
  15955. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15956. (let ((columns org-columns-current-fmt-compiled) col)
  15957. (while (setq col (pop columns))
  15958. (when (nth 3 col)
  15959. (save-excursion
  15960. (org-columns-compute (car col)))))))
  15961. (defun org-columns-update (property)
  15962. "Recompute PROPERTY, and update the columns display for it."
  15963. (org-columns-compute property)
  15964. (let (fmt val pos)
  15965. (save-excursion
  15966. (mapc (lambda (ov)
  15967. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15968. (setq pos (org-overlay-start ov))
  15969. (goto-char pos)
  15970. (when (setq val (cdr (assoc property
  15971. (get-text-property
  15972. (point-at-bol) 'org-summaries))))
  15973. (setq fmt (org-overlay-get ov 'org-columns-format))
  15974. (org-overlay-put ov 'org-columns-value val)
  15975. (org-overlay-put ov 'display (format fmt val)))))
  15976. org-columns-overlays))))
  15977. (defun org-columns-compute (property)
  15978. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15979. (interactive)
  15980. (let* ((re (concat "^" outline-regexp))
  15981. (lmax 30) ; Does anyone use deeper levels???
  15982. (lsum (make-vector lmax 0))
  15983. (lflag (make-vector lmax nil))
  15984. (level 0)
  15985. (ass (assoc property org-columns-current-fmt-compiled))
  15986. (format (nth 4 ass))
  15987. (printf (nth 5 ass))
  15988. (beg org-columns-top-level-marker)
  15989. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15990. (save-excursion
  15991. ;; Find the region to compute
  15992. (goto-char beg)
  15993. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15994. (goto-char end)
  15995. ;; Walk the tree from the back and do the computations
  15996. (while (re-search-backward re beg t)
  15997. (setq sumpos (match-beginning 0)
  15998. last-level level
  15999. level (org-outline-level)
  16000. val (org-entry-get nil property)
  16001. valflag (and val (string-match "\\S-" val)))
  16002. (cond
  16003. ((< level last-level)
  16004. ;; put the sum of lower levels here as a property
  16005. (setq sum (aref lsum last-level) ; current sum
  16006. flag (aref lflag last-level) ; any valid entries from children?
  16007. str (org-column-number-to-string sum format printf)
  16008. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  16009. useval (if flag str1 (if valflag val ""))
  16010. sum-alist (get-text-property sumpos 'org-summaries))
  16011. (if (assoc property sum-alist)
  16012. (setcdr (assoc property sum-alist) useval)
  16013. (push (cons property useval) sum-alist)
  16014. (org-unmodified
  16015. (add-text-properties sumpos (1+ sumpos)
  16016. (list 'org-summaries sum-alist))))
  16017. (when val
  16018. (org-entry-put nil property (if flag str val)))
  16019. ;; add current to current level accumulator
  16020. (when (or flag valflag)
  16021. (aset lsum level (+ (aref lsum level)
  16022. (if flag sum (org-column-string-to-number
  16023. (if flag str val) format))))
  16024. (aset lflag level t))
  16025. ;; clear accumulators for deeper levels
  16026. (loop for l from (1+ level) to (1- lmax) do
  16027. (aset lsum l 0)
  16028. (aset lflag l nil)))
  16029. ((>= level last-level)
  16030. ;; add what we have here to the accumulator for this level
  16031. (aset lsum level (+ (aref lsum level)
  16032. (org-column-string-to-number (or val "0") format)))
  16033. (and valflag (aset lflag level t)))
  16034. (t (error "This should not happen")))))))
  16035. (defun org-columns-redo ()
  16036. "Construct the column display again."
  16037. (interactive)
  16038. (message "Recomputing columns...")
  16039. (save-excursion
  16040. (if (marker-position org-columns-begin-marker)
  16041. (goto-char org-columns-begin-marker))
  16042. (org-columns-remove-overlays)
  16043. (if (org-mode-p)
  16044. (call-interactively 'org-columns)
  16045. (call-interactively 'org-agenda-columns)))
  16046. (message "Recomputing columns...done"))
  16047. (defun org-columns-not-in-agenda ()
  16048. (if (eq major-mode 'org-agenda-mode)
  16049. (error "This command is only allowed in Org-mode buffers")))
  16050. (defun org-string-to-number (s)
  16051. "Convert string to number, and interpret hh:mm:ss."
  16052. (if (not (string-match ":" s))
  16053. (string-to-number s)
  16054. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  16055. (while l
  16056. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  16057. sum)))
  16058. (defun org-column-number-to-string (n fmt &optional printf)
  16059. "Convert a computed column number to a string value, according to FMT."
  16060. (cond
  16061. ((eq fmt 'add_times)
  16062. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  16063. (format "%d:%02d" h m)))
  16064. ((eq fmt 'checkbox)
  16065. (cond ((= n (floor n)) "[X]")
  16066. ((> n 1.) "[-]")
  16067. (t "[ ]")))
  16068. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  16069. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  16070. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  16071. (printf (format printf n))
  16072. ((eq fmt 'currency)
  16073. (format "%.2f" n))
  16074. (t (number-to-string n))))
  16075. (defun org-nofm-to-completion (n m &optional percent)
  16076. (if (not percent)
  16077. (format "[%d/%d]" n m)
  16078. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  16079. (defun org-column-string-to-number (s fmt)
  16080. "Convert a column value to a number that can be used for column computing."
  16081. (cond
  16082. ((string-match ":" s)
  16083. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  16084. (while l
  16085. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  16086. sum))
  16087. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  16088. (if (equal s "[X]") 1. 0.000001))
  16089. (t (string-to-number s))))
  16090. (defun org-columns-uncompile-format (cfmt)
  16091. "Turn the compiled columns format back into a string representation."
  16092. (let ((rtn "") e s prop title op width fmt printf)
  16093. (while (setq e (pop cfmt))
  16094. (setq prop (car e)
  16095. title (nth 1 e)
  16096. width (nth 2 e)
  16097. op (nth 3 e)
  16098. fmt (nth 4 e)
  16099. printf (nth 5 e))
  16100. (cond
  16101. ((eq fmt 'add_times) (setq op ":"))
  16102. ((eq fmt 'checkbox) (setq op "X"))
  16103. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  16104. ((eq fmt 'checkbox-percent) (setq op "X%"))
  16105. ((eq fmt 'add_numbers) (setq op "+"))
  16106. ((eq fmt 'currency) (setq op "$")))
  16107. (if (and op printf) (setq op (concat op ";" printf)))
  16108. (if (equal title prop) (setq title nil))
  16109. (setq s (concat "%" (if width (number-to-string width))
  16110. prop
  16111. (if title (concat "(" title ")"))
  16112. (if op (concat "{" op "}"))))
  16113. (setq rtn (concat rtn " " s)))
  16114. (org-trim rtn)))
  16115. (defun org-columns-compile-format (fmt)
  16116. "Turn a column format string into an alist of specifications.
  16117. The alist has one entry for each column in the format. The elements of
  16118. that list are:
  16119. property the property
  16120. title the title field for the columns
  16121. width the column width in characters, can be nil for automatic
  16122. operator the operator if any
  16123. format the output format for computed results, derived from operator
  16124. printf a printf format for computed values"
  16125. (let ((start 0) width prop title op f printf)
  16126. (setq org-columns-current-fmt-compiled nil)
  16127. (while (string-match
  16128. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  16129. fmt start)
  16130. (setq start (match-end 0)
  16131. width (match-string 1 fmt)
  16132. prop (match-string 2 fmt)
  16133. title (or (match-string 3 fmt) prop)
  16134. op (match-string 4 fmt)
  16135. f nil
  16136. printf nil)
  16137. (if width (setq width (string-to-number width)))
  16138. (when (and op (string-match ";" op))
  16139. (setq printf (substring op (match-end 0))
  16140. op (substring op 0 (match-beginning 0))))
  16141. (cond
  16142. ((equal op "+") (setq f 'add_numbers))
  16143. ((equal op "$") (setq f 'currency))
  16144. ((equal op ":") (setq f 'add_times))
  16145. ((equal op "X") (setq f 'checkbox))
  16146. ((equal op "X/") (setq f 'checkbox-n-of-m))
  16147. ((equal op "X%") (setq f 'checkbox-percent))
  16148. )
  16149. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  16150. (setq org-columns-current-fmt-compiled
  16151. (nreverse org-columns-current-fmt-compiled))))
  16152. ;;; Dynamic block for Column view
  16153. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  16154. "Get the column view of the current buffer or subtree.
  16155. The first optional argument MAXLEVEL sets the level limit. A
  16156. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  16157. empty rows, an empty row being one where all the column view
  16158. specifiers except ITEM are empty. This function returns a list
  16159. containing the title row and all other rows. Each row is a list
  16160. of fields."
  16161. (save-excursion
  16162. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16163. (n (length title)) row tbl)
  16164. (goto-char (point-min))
  16165. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  16166. (or (null maxlevel)
  16167. (>= maxlevel
  16168. (if org-odd-levels-only
  16169. (/ (1+ (length (match-string 1))) 2)
  16170. (length (match-string 1))))))
  16171. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16172. (setq row nil)
  16173. (loop for i from 0 to (1- n) do
  16174. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16175. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16176. "")
  16177. row))
  16178. (setq row (nreverse row))
  16179. (unless (and skip-empty-rows
  16180. (eq 1 (length (delete "" (delete-dups row)))))
  16181. (push row tbl))))
  16182. (append (list title 'hline) (nreverse tbl)))))
  16183. (defun org-dblock-write:columnview (params)
  16184. "Write the column view table.
  16185. PARAMS is a property list of parameters:
  16186. :width enforce same column widths with <N> specifiers.
  16187. :id the :ID: property of the entry where the columns view
  16188. should be built, as a string. When `local', call locally.
  16189. When `global' call column view with the cursor at the beginning
  16190. of the buffer (usually this means that the whole buffer switches
  16191. to column view).
  16192. :hlines When t, insert a hline before each item. When a number, insert
  16193. a hline before each level <= that number.
  16194. :vlines When t, make each column a colgroup to enforce vertical lines.
  16195. :maxlevel When set to a number, don't capture headlines below this level.
  16196. :skip-empty-rows
  16197. When t, skip rows where all specifiers other than ITEM are empty."
  16198. (let ((pos (move-marker (make-marker) (point)))
  16199. (hlines (plist-get params :hlines))
  16200. (vlines (plist-get params :vlines))
  16201. (maxlevel (plist-get params :maxlevel))
  16202. (skip-empty-rows (plist-get params :skip-empty-rows))
  16203. tbl id idpos nfields tmp)
  16204. (save-excursion
  16205. (save-restriction
  16206. (when (setq id (plist-get params :id))
  16207. (cond ((not id) nil)
  16208. ((eq id 'global) (goto-char (point-min)))
  16209. ((eq id 'local) nil)
  16210. ((setq idpos (org-find-entry-with-id id))
  16211. (goto-char idpos))
  16212. (t (error "Cannot find entry with :ID: %s" id))))
  16213. (org-columns)
  16214. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  16215. (setq nfields (length (car tbl)))
  16216. (org-columns-quit)))
  16217. (goto-char pos)
  16218. (move-marker pos nil)
  16219. (when tbl
  16220. (when (plist-get params :hlines)
  16221. (setq tmp nil)
  16222. (while tbl
  16223. (if (eq (car tbl) 'hline)
  16224. (push (pop tbl) tmp)
  16225. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16226. (if (and (not (eq (car tmp) 'hline))
  16227. (or (eq hlines t)
  16228. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16229. (push 'hline tmp)))
  16230. (push (pop tbl) tmp)))
  16231. (setq tbl (nreverse tmp)))
  16232. (when vlines
  16233. (setq tbl (mapcar (lambda (x)
  16234. (if (eq 'hline x) x (cons "" x)))
  16235. tbl))
  16236. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16237. (setq pos (point))
  16238. (insert (org-listtable-to-string tbl))
  16239. (when (plist-get params :width)
  16240. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16241. org-columns-current-widths "|")))
  16242. (goto-char pos)
  16243. (org-table-align))))
  16244. (defun org-listtable-to-string (tbl)
  16245. "Convert a listtable TBL to a string that contains the Org-mode table.
  16246. The table still need to be alligned. The resulting string has no leading
  16247. and tailing newline characters."
  16248. (mapconcat
  16249. (lambda (x)
  16250. (cond
  16251. ((listp x)
  16252. (concat "|" (mapconcat 'identity x "|") "|"))
  16253. ((eq x 'hline) "|-|")
  16254. (t (error "Garbage in listtable: %s" x))))
  16255. tbl "\n"))
  16256. (defun org-insert-columns-dblock ()
  16257. "Create a dynamic block capturing a column view table."
  16258. (interactive)
  16259. (let ((defaults '(:name "columnview" :hlines 1))
  16260. (id (completing-read
  16261. "Capture columns (local, global, entry with :ID: property) [local]: "
  16262. (append '(("global") ("local"))
  16263. (mapcar 'list (org-property-values "ID"))))))
  16264. (if (equal id "") (setq id 'local))
  16265. (if (equal id "global") (setq id 'global))
  16266. (setq defaults (append defaults (list :id id)))
  16267. (org-create-dblock defaults)
  16268. (org-update-dblock)))
  16269. ;;;; Timestamps
  16270. (defvar org-last-changed-timestamp nil)
  16271. (defvar org-time-was-given) ; dynamically scoped parameter
  16272. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16273. (defvar org-ts-what) ; dynamically scoped parameter
  16274. (defun org-time-stamp (arg)
  16275. "Prompt for a date/time and insert a time stamp.
  16276. If the user specifies a time like HH:MM, or if this command is called
  16277. with a prefix argument, the time stamp will contain date and time.
  16278. Otherwise, only the date will be included. All parts of a date not
  16279. specified by the user will be filled in from the current date/time.
  16280. So if you press just return without typing anything, the time stamp
  16281. will represent the current date/time. If there is already a timestamp
  16282. at the cursor, it will be modified."
  16283. (interactive "P")
  16284. (let* ((ts nil)
  16285. (default-time
  16286. ;; Default time is either today, or, when entering a range,
  16287. ;; the range start.
  16288. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16289. (save-excursion
  16290. (re-search-backward
  16291. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16292. (- (point) 20) t)))
  16293. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16294. (current-time)))
  16295. (default-input (and ts (org-get-compact-tod ts)))
  16296. org-time-was-given org-end-time-was-given time)
  16297. (cond
  16298. ((and (org-at-timestamp-p)
  16299. (eq last-command 'org-time-stamp)
  16300. (eq this-command 'org-time-stamp))
  16301. (insert "--")
  16302. (setq time (let ((this-command this-command))
  16303. (org-read-date arg 'totime nil nil default-time default-input)))
  16304. (org-insert-time-stamp time (or org-time-was-given arg)))
  16305. ((org-at-timestamp-p)
  16306. (setq time (let ((this-command this-command))
  16307. (org-read-date arg 'totime nil nil default-time default-input)))
  16308. (when (org-at-timestamp-p) ; just to get the match data
  16309. (replace-match "")
  16310. (setq org-last-changed-timestamp
  16311. (org-insert-time-stamp
  16312. time (or org-time-was-given arg)
  16313. nil nil nil (list org-end-time-was-given))))
  16314. (message "Timestamp updated"))
  16315. (t
  16316. (setq time (let ((this-command this-command))
  16317. (org-read-date arg 'totime nil nil default-time default-input)))
  16318. (org-insert-time-stamp time (or org-time-was-given arg)
  16319. nil nil nil (list org-end-time-was-given))))))
  16320. ;; FIXME: can we use this for something else????
  16321. ;; like computing time differences?????
  16322. (defun org-get-compact-tod (s)
  16323. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16324. (let* ((t1 (match-string 1 s))
  16325. (h1 (string-to-number (match-string 2 s)))
  16326. (m1 (string-to-number (match-string 3 s)))
  16327. (t2 (and (match-end 4) (match-string 5 s)))
  16328. (h2 (and t2 (string-to-number (match-string 6 s))))
  16329. (m2 (and t2 (string-to-number (match-string 7 s))))
  16330. dh dm)
  16331. (if (not t2)
  16332. t1
  16333. (setq dh (- h2 h1) dm (- m2 m1))
  16334. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16335. (concat t1 "+" (number-to-string dh)
  16336. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16337. (defun org-time-stamp-inactive (&optional arg)
  16338. "Insert an inactive time stamp.
  16339. An inactive time stamp is enclosed in square brackets instead of angle
  16340. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16341. does not link to the calendar and cannot be changed with the S-cursor keys.
  16342. So these are more for recording a certain time/date."
  16343. (interactive "P")
  16344. (let (org-time-was-given org-end-time-was-given time)
  16345. (setq time (org-read-date arg 'totime))
  16346. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16347. nil nil (list org-end-time-was-given))))
  16348. (defvar org-date-ovl (org-make-overlay 1 1))
  16349. (org-overlay-put org-date-ovl 'face 'org-warning)
  16350. (org-detach-overlay org-date-ovl)
  16351. (defvar org-ans1) ; dynamically scoped parameter
  16352. (defvar org-ans2) ; dynamically scoped parameter
  16353. (defvar org-plain-time-of-day-regexp) ; defined below
  16354. (defvar org-read-date-overlay nil)
  16355. (defvar org-dcst nil) ; dynamically scoped
  16356. (defun org-read-date (&optional with-time to-time from-string prompt
  16357. default-time default-input)
  16358. "Read a date, possibly a time, and make things smooth for the user.
  16359. The prompt will suggest to enter an ISO date, but you can also enter anything
  16360. which will at least partially be understood by `parse-time-string'.
  16361. Unrecognized parts of the date will default to the current day, month, year,
  16362. hour and minute. If this command is called to replace a timestamp at point,
  16363. of to enter the second timestamp of a range, the default time is taken from the
  16364. existing stamp. For example,
  16365. 3-2-5 --> 2003-02-05
  16366. feb 15 --> currentyear-02-15
  16367. sep 12 9 --> 2009-09-12
  16368. 12:45 --> today 12:45
  16369. 22 sept 0:34 --> currentyear-09-22 0:34
  16370. 12 --> currentyear-currentmonth-12
  16371. Fri --> nearest Friday (today or later)
  16372. etc.
  16373. Furthermore you can specify a relative date by giving, as the *first* thing
  16374. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16375. change in days weeks, months, years.
  16376. With a single plus or minus, the date is relative to today. With a double
  16377. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16378. +4d --> four days from today
  16379. +4 --> same as above
  16380. +2w --> two weeks from today
  16381. ++5 --> five days from default date
  16382. The function understands only English month and weekday abbreviations,
  16383. but this can be configured with the variables `parse-time-months' and
  16384. `parse-time-weekdays'.
  16385. While prompting, a calendar is popped up - you can also select the
  16386. date with the mouse (button 1). The calendar shows a period of three
  16387. months. To scroll it to other months, use the keys `>' and `<'.
  16388. If you don't like the calendar, turn it off with
  16389. \(setq org-read-date-popup-calendar nil)
  16390. With optional argument TO-TIME, the date will immediately be converted
  16391. to an internal time.
  16392. With an optional argument WITH-TIME, the prompt will suggest to also
  16393. insert a time. Note that when WITH-TIME is not set, you can still
  16394. enter a time, and this function will inform the calling routine about
  16395. this change. The calling routine may then choose to change the format
  16396. used to insert the time stamp into the buffer to include the time.
  16397. With optional argument FROM-STRING, read from this string instead from
  16398. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16399. the time/date that is used for everything that is not specified by the
  16400. user."
  16401. (require 'parse-time)
  16402. (let* ((org-time-stamp-rounding-minutes
  16403. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  16404. (org-dcst org-display-custom-times)
  16405. (ct (org-current-time))
  16406. (def (or default-time ct))
  16407. (defdecode (decode-time def))
  16408. (dummy (progn
  16409. (when (< (nth 2 defdecode) org-extend-today-until)
  16410. (setcar (nthcdr 2 defdecode) -1)
  16411. (setcar (nthcdr 1 defdecode) 59)
  16412. (setq def (apply 'encode-time defdecode)
  16413. defdecode (decode-time def)))))
  16414. (calendar-move-hook nil)
  16415. (view-diary-entries-initially nil)
  16416. (view-calendar-holidays-initially nil)
  16417. (timestr (format-time-string
  16418. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16419. (prompt (concat (if prompt (concat prompt " ") "")
  16420. (format "Date+time [%s]: " timestr)))
  16421. ans (org-ans0 "") org-ans1 org-ans2 final)
  16422. (cond
  16423. (from-string (setq ans from-string))
  16424. (org-read-date-popup-calendar
  16425. (save-excursion
  16426. (save-window-excursion
  16427. (calendar)
  16428. (calendar-forward-day (- (time-to-days def)
  16429. (calendar-absolute-from-gregorian
  16430. (calendar-current-date))))
  16431. (org-eval-in-calendar nil t)
  16432. (let* ((old-map (current-local-map))
  16433. (map (copy-keymap calendar-mode-map))
  16434. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16435. (org-defkey map (kbd "RET") 'org-calendar-select)
  16436. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16437. 'org-calendar-select-mouse)
  16438. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16439. 'org-calendar-select-mouse)
  16440. (org-defkey minibuffer-local-map [(meta shift left)]
  16441. (lambda () (interactive)
  16442. (org-eval-in-calendar '(calendar-backward-month 1))))
  16443. (org-defkey minibuffer-local-map [(meta shift right)]
  16444. (lambda () (interactive)
  16445. (org-eval-in-calendar '(calendar-forward-month 1))))
  16446. (org-defkey minibuffer-local-map [(meta shift up)]
  16447. (lambda () (interactive)
  16448. (org-eval-in-calendar '(calendar-backward-year 1))))
  16449. (org-defkey minibuffer-local-map [(meta shift down)]
  16450. (lambda () (interactive)
  16451. (org-eval-in-calendar '(calendar-forward-year 1))))
  16452. (org-defkey minibuffer-local-map [(shift up)]
  16453. (lambda () (interactive)
  16454. (org-eval-in-calendar '(calendar-backward-week 1))))
  16455. (org-defkey minibuffer-local-map [(shift down)]
  16456. (lambda () (interactive)
  16457. (org-eval-in-calendar '(calendar-forward-week 1))))
  16458. (org-defkey minibuffer-local-map [(shift left)]
  16459. (lambda () (interactive)
  16460. (org-eval-in-calendar '(calendar-backward-day 1))))
  16461. (org-defkey minibuffer-local-map [(shift right)]
  16462. (lambda () (interactive)
  16463. (org-eval-in-calendar '(calendar-forward-day 1))))
  16464. (org-defkey minibuffer-local-map ">"
  16465. (lambda () (interactive)
  16466. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16467. (org-defkey minibuffer-local-map "<"
  16468. (lambda () (interactive)
  16469. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16470. (unwind-protect
  16471. (progn
  16472. (use-local-map map)
  16473. (add-hook 'post-command-hook 'org-read-date-display)
  16474. (setq org-ans0 (read-string prompt default-input nil nil))
  16475. ;; org-ans0: from prompt
  16476. ;; org-ans1: from mouse click
  16477. ;; org-ans2: from calendar motion
  16478. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16479. (remove-hook 'post-command-hook 'org-read-date-display)
  16480. (use-local-map old-map)
  16481. (when org-read-date-overlay
  16482. (org-delete-overlay org-read-date-overlay)
  16483. (setq org-read-date-overlay nil)))))))
  16484. (t ; Naked prompt only
  16485. (unwind-protect
  16486. (setq ans (read-string prompt default-input nil timestr))
  16487. (when org-read-date-overlay
  16488. (org-delete-overlay org-read-date-overlay)
  16489. (setq org-read-date-overlay nil)))))
  16490. (setq final (org-read-date-analyze ans def defdecode))
  16491. (if to-time
  16492. (apply 'encode-time final)
  16493. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16494. (format "%04d-%02d-%02d %02d:%02d"
  16495. (nth 5 final) (nth 4 final) (nth 3 final)
  16496. (nth 2 final) (nth 1 final))
  16497. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16498. (defvar def)
  16499. (defvar defdecode)
  16500. (defvar with-time)
  16501. (defun org-read-date-display ()
  16502. "Display the currrent date prompt interpretation in the minibuffer."
  16503. (when org-read-date-display-live
  16504. (when org-read-date-overlay
  16505. (org-delete-overlay org-read-date-overlay))
  16506. (let ((p (point)))
  16507. (end-of-line 1)
  16508. (while (not (equal (buffer-substring
  16509. (max (point-min) (- (point) 4)) (point))
  16510. " "))
  16511. (insert " "))
  16512. (goto-char p))
  16513. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16514. " " (or org-ans1 org-ans2)))
  16515. (org-end-time-was-given nil)
  16516. (f (org-read-date-analyze ans def defdecode))
  16517. (fmts (if org-dcst
  16518. org-time-stamp-custom-formats
  16519. org-time-stamp-formats))
  16520. (fmt (if (or with-time
  16521. (and (boundp 'org-time-was-given) org-time-was-given))
  16522. (cdr fmts)
  16523. (car fmts)))
  16524. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16525. (when (and org-end-time-was-given
  16526. (string-match org-plain-time-of-day-regexp txt))
  16527. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16528. org-end-time-was-given
  16529. (substring txt (match-end 0)))))
  16530. (setq org-read-date-overlay
  16531. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16532. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16533. (defun org-read-date-analyze (ans def defdecode)
  16534. "Analyze the combined answer of the date prompt."
  16535. ;; FIXME: cleanup and comment
  16536. (let (delta deltan deltaw deltadef year month day
  16537. hour minute second wday pm h2 m2 tl wday1)
  16538. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16539. (setq ans (replace-match "" t t ans)
  16540. deltan (car delta)
  16541. deltaw (nth 1 delta)
  16542. deltadef (nth 2 delta)))
  16543. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16544. (when (string-match
  16545. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16546. (setq year (if (match-end 2)
  16547. (string-to-number (match-string 2 ans))
  16548. (string-to-number (format-time-string "%Y")))
  16549. month (string-to-number (match-string 3 ans))
  16550. day (string-to-number (match-string 4 ans)))
  16551. (if (< year 100) (setq year (+ 2000 year)))
  16552. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16553. t nil ans)))
  16554. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16555. ;; If there is a time with am/pm, and *no* time without it, we convert
  16556. ;; so that matching will be successful.
  16557. (loop for i from 1 to 2 do ; twice, for end time as well
  16558. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16559. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16560. (setq hour (string-to-number (match-string 1 ans))
  16561. minute (if (match-end 3)
  16562. (string-to-number (match-string 3 ans))
  16563. 0)
  16564. pm (equal ?p
  16565. (string-to-char (downcase (match-string 4 ans)))))
  16566. (if (and (= hour 12) (not pm))
  16567. (setq hour 0)
  16568. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16569. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16570. t t ans))))
  16571. ;; Check if a time range is given as a duration
  16572. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16573. (setq hour (string-to-number (match-string 1 ans))
  16574. h2 (+ hour (string-to-number (match-string 3 ans)))
  16575. minute (string-to-number (match-string 2 ans))
  16576. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16577. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16578. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16579. ;; Check if there is a time range
  16580. (when (boundp 'org-end-time-was-given)
  16581. (setq org-time-was-given nil)
  16582. (when (and (string-match org-plain-time-of-day-regexp ans)
  16583. (match-end 8))
  16584. (setq org-end-time-was-given (match-string 8 ans))
  16585. (setq ans (concat (substring ans 0 (match-beginning 7))
  16586. (substring ans (match-end 7))))))
  16587. (setq tl (parse-time-string ans)
  16588. day (or (nth 3 tl) (nth 3 defdecode))
  16589. month (or (nth 4 tl)
  16590. (if (and org-read-date-prefer-future
  16591. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16592. (1+ (nth 4 defdecode))
  16593. (nth 4 defdecode)))
  16594. year (or (nth 5 tl)
  16595. (if (and org-read-date-prefer-future
  16596. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16597. (1+ (nth 5 defdecode))
  16598. (nth 5 defdecode)))
  16599. hour (or (nth 2 tl) (nth 2 defdecode))
  16600. minute (or (nth 1 tl) (nth 1 defdecode))
  16601. second (or (nth 0 tl) 0)
  16602. wday (nth 6 tl))
  16603. (when deltan
  16604. (unless deltadef
  16605. (let ((now (decode-time (current-time))))
  16606. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16607. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16608. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16609. ((equal deltaw "m") (setq month (+ month deltan)))
  16610. ((equal deltaw "y") (setq year (+ year deltan)))))
  16611. (when (and wday (not (nth 3 tl)))
  16612. ;; Weekday was given, but no day, so pick that day in the week
  16613. ;; on or after the derived date.
  16614. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16615. (unless (equal wday wday1)
  16616. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16617. (if (and (boundp 'org-time-was-given)
  16618. (nth 2 tl))
  16619. (setq org-time-was-given t))
  16620. (if (< year 100) (setq year (+ 2000 year)))
  16621. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16622. (list second minute hour day month year)))
  16623. (defvar parse-time-weekdays)
  16624. (defun org-read-date-get-relative (s today default)
  16625. "Check string S for special relative date string.
  16626. TODAY and DEFAULT are internal times, for today and for a default.
  16627. Return shift list (N what def-flag)
  16628. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16629. N is the number of WHATs to shift.
  16630. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16631. the DEFAULT date rather than TODAY."
  16632. (when (string-match
  16633. (concat
  16634. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16635. "\\([0-9]+\\)?"
  16636. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16637. "\\([ \t]\\|$\\)") s)
  16638. (let* ((dir (if (match-end 1)
  16639. (string-to-char (substring (match-string 1 s) -1))
  16640. ?+))
  16641. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16642. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16643. (what (if (match-end 3) (match-string 3 s) "d"))
  16644. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16645. (date (if rel default today))
  16646. (wday (nth 6 (decode-time date)))
  16647. delta)
  16648. (if wday1
  16649. (progn
  16650. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16651. (if (= dir ?-) (setq delta (- delta 7)))
  16652. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16653. (list delta "d" rel))
  16654. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16655. (defun org-eval-in-calendar (form &optional keepdate)
  16656. "Eval FORM in the calendar window and return to current window.
  16657. Also, store the cursor date in variable org-ans2."
  16658. (let ((sw (selected-window)))
  16659. (select-window (get-buffer-window "*Calendar*"))
  16660. (eval form)
  16661. (when (and (not keepdate) (calendar-cursor-to-date))
  16662. (let* ((date (calendar-cursor-to-date))
  16663. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16664. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16665. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16666. (select-window sw)))
  16667. ; ;; Update the prompt to show new default date
  16668. ; (save-excursion
  16669. ; (goto-char (point-min))
  16670. ; (when (and org-ans2
  16671. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16672. ; (get-text-property (match-end 0) 'field))
  16673. ; (let ((inhibit-read-only t))
  16674. ; (replace-match (concat "[" org-ans2 "]") t t)
  16675. ; (add-text-properties (point-min) (1+ (match-end 0))
  16676. ; (text-properties-at (1+ (point-min)))))))))
  16677. (defun org-calendar-select ()
  16678. "Return to `org-read-date' with the date currently selected.
  16679. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16680. (interactive)
  16681. (when (calendar-cursor-to-date)
  16682. (let* ((date (calendar-cursor-to-date))
  16683. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16684. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16685. (if (active-minibuffer-window) (exit-minibuffer))))
  16686. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16687. "Insert a date stamp for the date given by the internal TIME.
  16688. WITH-HM means, use the stamp format that includes the time of the day.
  16689. INACTIVE means use square brackets instead of angular ones, so that the
  16690. stamp will not contribute to the agenda.
  16691. PRE and POST are optional strings to be inserted before and after the
  16692. stamp.
  16693. The command returns the inserted time stamp."
  16694. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16695. stamp)
  16696. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16697. (insert-before-markers (or pre ""))
  16698. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16699. (when (listp extra)
  16700. (setq extra (car extra))
  16701. (if (and (stringp extra)
  16702. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16703. (setq extra (format "-%02d:%02d"
  16704. (string-to-number (match-string 1 extra))
  16705. (string-to-number (match-string 2 extra))))
  16706. (setq extra nil)))
  16707. (when extra
  16708. (backward-char 1)
  16709. (insert-before-markers extra)
  16710. (forward-char 1))
  16711. (insert-before-markers (or post ""))
  16712. stamp))
  16713. (defun org-toggle-time-stamp-overlays ()
  16714. "Toggle the use of custom time stamp formats."
  16715. (interactive)
  16716. (setq org-display-custom-times (not org-display-custom-times))
  16717. (unless org-display-custom-times
  16718. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16719. (while (setq p (next-single-property-change p 'display))
  16720. (if (and (get-text-property p 'display)
  16721. (eq (get-text-property p 'face) 'org-date))
  16722. (remove-text-properties
  16723. p (setq p (next-single-property-change p 'display))
  16724. '(display t))))
  16725. (set-buffer-modified-p bmp)))
  16726. (if (featurep 'xemacs)
  16727. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16728. (org-restart-font-lock)
  16729. (setq org-table-may-need-update t)
  16730. (if org-display-custom-times
  16731. (message "Time stamps are overlayed with custom format")
  16732. (message "Time stamp overlays removed")))
  16733. (defun org-display-custom-time (beg end)
  16734. "Overlay modified time stamp format over timestamp between BED and END."
  16735. (let* ((ts (buffer-substring beg end))
  16736. t1 w1 with-hm tf time str w2 (off 0))
  16737. (save-match-data
  16738. (setq t1 (org-parse-time-string ts t))
  16739. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16740. (setq off (- (match-end 0) (match-beginning 0)))))
  16741. (setq end (- end off))
  16742. (setq w1 (- end beg)
  16743. with-hm (and (nth 1 t1) (nth 2 t1))
  16744. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16745. time (org-fix-decoded-time t1)
  16746. str (org-add-props
  16747. (format-time-string
  16748. (substring tf 1 -1) (apply 'encode-time time))
  16749. nil 'mouse-face 'highlight)
  16750. w2 (length str))
  16751. (if (not (= w2 w1))
  16752. (add-text-properties (1+ beg) (+ 2 beg)
  16753. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16754. (if (featurep 'xemacs)
  16755. (progn
  16756. (put-text-property beg end 'invisible t)
  16757. (put-text-property beg end 'end-glyph (make-glyph str)))
  16758. (put-text-property beg end 'display str))))
  16759. (defun org-translate-time (string)
  16760. "Translate all timestamps in STRING to custom format.
  16761. But do this only if the variable `org-display-custom-times' is set."
  16762. (when org-display-custom-times
  16763. (save-match-data
  16764. (let* ((start 0)
  16765. (re org-ts-regexp-both)
  16766. t1 with-hm inactive tf time str beg end)
  16767. (while (setq start (string-match re string start))
  16768. (setq beg (match-beginning 0)
  16769. end (match-end 0)
  16770. t1 (save-match-data
  16771. (org-parse-time-string (substring string beg end) t))
  16772. with-hm (and (nth 1 t1) (nth 2 t1))
  16773. inactive (equal (substring string beg (1+ beg)) "[")
  16774. tf (funcall (if with-hm 'cdr 'car)
  16775. org-time-stamp-custom-formats)
  16776. time (org-fix-decoded-time t1)
  16777. str (format-time-string
  16778. (concat
  16779. (if inactive "[" "<") (substring tf 1 -1)
  16780. (if inactive "]" ">"))
  16781. (apply 'encode-time time))
  16782. string (replace-match str t t string)
  16783. start (+ start (length str)))))))
  16784. string)
  16785. (defun org-fix-decoded-time (time)
  16786. "Set 0 instead of nil for the first 6 elements of time.
  16787. Don't touch the rest."
  16788. (let ((n 0))
  16789. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16790. (defun org-days-to-time (timestamp-string)
  16791. "Difference between TIMESTAMP-STRING and now in days."
  16792. (- (time-to-days (org-time-string-to-time timestamp-string))
  16793. (time-to-days (current-time))))
  16794. (defun org-deadline-close (timestamp-string &optional ndays)
  16795. "Is the time in TIMESTAMP-STRING close to the current date?"
  16796. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16797. (and (< (org-days-to-time timestamp-string) ndays)
  16798. (not (org-entry-is-done-p))))
  16799. (defun org-get-wdays (ts)
  16800. "Get the deadline lead time appropriate for timestring TS."
  16801. (cond
  16802. ((<= org-deadline-warning-days 0)
  16803. ;; 0 or negative, enforce this value no matter what
  16804. (- org-deadline-warning-days))
  16805. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16806. ;; lead time is specified.
  16807. (floor (* (string-to-number (match-string 1 ts))
  16808. (cdr (assoc (match-string 2 ts)
  16809. '(("d" . 1) ("w" . 7)
  16810. ("m" . 30.4) ("y" . 365.25)))))))
  16811. ;; go for the default.
  16812. (t org-deadline-warning-days)))
  16813. (defun org-calendar-select-mouse (ev)
  16814. "Return to `org-read-date' with the date currently selected.
  16815. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16816. (interactive "e")
  16817. (mouse-set-point ev)
  16818. (when (calendar-cursor-to-date)
  16819. (let* ((date (calendar-cursor-to-date))
  16820. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16821. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16822. (if (active-minibuffer-window) (exit-minibuffer))))
  16823. (defun org-check-deadlines (ndays)
  16824. "Check if there are any deadlines due or past due.
  16825. A deadline is considered due if it happens within `org-deadline-warning-days'
  16826. days from today's date. If the deadline appears in an entry marked DONE,
  16827. it is not shown. The prefix arg NDAYS can be used to test that many
  16828. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16829. (interactive "P")
  16830. (let* ((org-warn-days
  16831. (cond
  16832. ((equal ndays '(4)) 100000)
  16833. (ndays (prefix-numeric-value ndays))
  16834. (t (abs org-deadline-warning-days))))
  16835. (case-fold-search nil)
  16836. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16837. (callback
  16838. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16839. (message "%d deadlines past-due or due within %d days"
  16840. (org-occur regexp nil callback)
  16841. org-warn-days)))
  16842. (defun org-check-before-date (date)
  16843. "Check if there are deadlines or scheduled entries before DATE."
  16844. (interactive (list (org-read-date)))
  16845. (let ((case-fold-search nil)
  16846. (regexp (concat "\\<\\(" org-deadline-string
  16847. "\\|" org-scheduled-string
  16848. "\\) *<\\([^>]+\\)>"))
  16849. (callback
  16850. (lambda () (time-less-p
  16851. (org-time-string-to-time (match-string 2))
  16852. (org-time-string-to-time date)))))
  16853. (message "%d entries before %s"
  16854. (org-occur regexp nil callback) date)))
  16855. (defun org-evaluate-time-range (&optional to-buffer)
  16856. "Evaluate a time range by computing the difference between start and end.
  16857. Normally the result is just printed in the echo area, but with prefix arg
  16858. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16859. If the time range is actually in a table, the result is inserted into the
  16860. next column.
  16861. For time difference computation, a year is assumed to be exactly 365
  16862. days in order to avoid rounding problems."
  16863. (interactive "P")
  16864. (or
  16865. (org-clock-update-time-maybe)
  16866. (save-excursion
  16867. (unless (org-at-date-range-p t)
  16868. (goto-char (point-at-bol))
  16869. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16870. (if (not (org-at-date-range-p t))
  16871. (error "Not at a time-stamp range, and none found in current line")))
  16872. (let* ((ts1 (match-string 1))
  16873. (ts2 (match-string 2))
  16874. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16875. (match-end (match-end 0))
  16876. (time1 (org-time-string-to-time ts1))
  16877. (time2 (org-time-string-to-time ts2))
  16878. (t1 (time-to-seconds time1))
  16879. (t2 (time-to-seconds time2))
  16880. (diff (abs (- t2 t1)))
  16881. (negative (< (- t2 t1) 0))
  16882. ;; (ys (floor (* 365 24 60 60)))
  16883. (ds (* 24 60 60))
  16884. (hs (* 60 60))
  16885. (fy "%dy %dd %02d:%02d")
  16886. (fy1 "%dy %dd")
  16887. (fd "%dd %02d:%02d")
  16888. (fd1 "%dd")
  16889. (fh "%02d:%02d")
  16890. y d h m align)
  16891. (if havetime
  16892. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16893. y 0
  16894. d (floor (/ diff ds)) diff (mod diff ds)
  16895. h (floor (/ diff hs)) diff (mod diff hs)
  16896. m (floor (/ diff 60)))
  16897. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16898. y 0
  16899. d (floor (+ (/ diff ds) 0.5))
  16900. h 0 m 0))
  16901. (if (not to-buffer)
  16902. (message "%s" (org-make-tdiff-string y d h m))
  16903. (if (org-at-table-p)
  16904. (progn
  16905. (goto-char match-end)
  16906. (setq align t)
  16907. (and (looking-at " *|") (goto-char (match-end 0))))
  16908. (goto-char match-end))
  16909. (if (looking-at
  16910. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16911. (replace-match ""))
  16912. (if negative (insert " -"))
  16913. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16914. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16915. (insert " " (format fh h m))))
  16916. (if align (org-table-align))
  16917. (message "Time difference inserted")))))
  16918. (defun org-make-tdiff-string (y d h m)
  16919. (let ((fmt "")
  16920. (l nil))
  16921. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16922. l (push y l)))
  16923. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16924. l (push d l)))
  16925. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16926. l (push h l)))
  16927. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16928. l (push m l)))
  16929. (apply 'format fmt (nreverse l))))
  16930. (defun org-time-string-to-time (s)
  16931. (apply 'encode-time (org-parse-time-string s)))
  16932. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16933. "Convert a time stamp to an absolute day number.
  16934. If there is a specifyer for a cyclic time stamp, get the closest date to
  16935. DAYNR."
  16936. (cond
  16937. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16938. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16939. daynr
  16940. (+ daynr 1000)))
  16941. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16942. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16943. (time-to-days (current-time))) (match-string 0 s)
  16944. prefer))
  16945. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16946. (defun org-time-from-absolute (d)
  16947. "Return the time corresponding to date D.
  16948. D may be an absolute day number, or a calendar-type list (month day year)."
  16949. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16950. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16951. (defun org-calendar-holiday ()
  16952. "List of holidays, for Diary display in Org-mode."
  16953. (require 'holidays)
  16954. (let ((hl (funcall
  16955. (if (fboundp 'calendar-check-holidays)
  16956. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16957. (if hl (mapconcat 'identity hl "; "))))
  16958. (defun org-diary-sexp-entry (sexp entry date)
  16959. "Process a SEXP diary ENTRY for DATE."
  16960. (require 'diary-lib)
  16961. (let ((result (if calendar-debug-sexp
  16962. (let ((stack-trace-on-error t))
  16963. (eval (car (read-from-string sexp))))
  16964. (condition-case nil
  16965. (eval (car (read-from-string sexp)))
  16966. (error
  16967. (beep)
  16968. (message "Bad sexp at line %d in %s: %s"
  16969. (org-current-line)
  16970. (buffer-file-name) sexp)
  16971. (sleep-for 2))))))
  16972. (cond ((stringp result) result)
  16973. ((and (consp result)
  16974. (stringp (cdr result))) (cdr result))
  16975. (result entry)
  16976. (t nil))))
  16977. (defun org-diary-to-ical-string (frombuf)
  16978. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16979. This uses the icalendar.el library."
  16980. (let* ((tmpdir (if (featurep 'xemacs)
  16981. (temp-directory)
  16982. temporary-file-directory))
  16983. (tmpfile (make-temp-name
  16984. (expand-file-name "orgics" tmpdir)))
  16985. buf rtn b e)
  16986. (save-excursion
  16987. (set-buffer frombuf)
  16988. (icalendar-export-region (point-min) (point-max) tmpfile)
  16989. (setq buf (find-buffer-visiting tmpfile))
  16990. (set-buffer buf)
  16991. (goto-char (point-min))
  16992. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16993. (setq b (match-beginning 0)))
  16994. (goto-char (point-max))
  16995. (if (re-search-backward "^END:VEVENT" nil t)
  16996. (setq e (match-end 0)))
  16997. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16998. (kill-buffer buf)
  16999. (kill-buffer frombuf)
  17000. (delete-file tmpfile)
  17001. rtn))
  17002. (defun org-closest-date (start current change prefer)
  17003. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  17004. When PREFER is `past' return a date that is either CURRENT or past.
  17005. When PREFER is `future', return a date that is either CURRENT or future."
  17006. ;; Make the proper lists from the dates
  17007. (catch 'exit
  17008. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  17009. dn dw sday cday n1 n2
  17010. d m y y1 y2 date1 date2 nmonths nm ny m2)
  17011. (setq start (org-date-to-gregorian start)
  17012. current (org-date-to-gregorian
  17013. (if org-agenda-repeating-timestamp-show-all
  17014. current
  17015. (time-to-days (current-time))))
  17016. sday (calendar-absolute-from-gregorian start)
  17017. cday (calendar-absolute-from-gregorian current))
  17018. (if (<= cday sday) (throw 'exit sday))
  17019. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  17020. (setq dn (string-to-number (match-string 1 change))
  17021. dw (cdr (assoc (match-string 2 change) a1)))
  17022. (error "Invalid change specifyer: %s" change))
  17023. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  17024. (cond
  17025. ((eq dw 'day)
  17026. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  17027. n2 (+ n1 dn)))
  17028. ((eq dw 'year)
  17029. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  17030. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  17031. (setq date1 (list m d y1)
  17032. n1 (calendar-absolute-from-gregorian date1)
  17033. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  17034. n2 (calendar-absolute-from-gregorian date2)))
  17035. ((eq dw 'month)
  17036. ;; approx number of month between the tow dates
  17037. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  17038. ;; How often does dn fit in there?
  17039. (setq d (nth 1 start) m (car start) y (nth 2 start)
  17040. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  17041. m (+ m nm)
  17042. ny (floor (/ m 12))
  17043. y (+ y ny)
  17044. m (- m (* ny 12)))
  17045. (while (> m 12) (setq m (- m 12) y (1+ y)))
  17046. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  17047. (setq m2 (+ m dn) y2 y)
  17048. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  17049. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  17050. (while (< n2 cday)
  17051. (setq n1 n2 m m2 y y2)
  17052. (setq m2 (+ m dn) y2 y)
  17053. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  17054. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  17055. (if org-agenda-repeating-timestamp-show-all
  17056. (cond
  17057. ((eq prefer 'past) n1)
  17058. ((eq prefer 'future) (if (= cday n1) n1 n2))
  17059. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  17060. (cond
  17061. ((eq prefer 'past) n1)
  17062. ((eq prefer 'future) (if (= cday n1) n1 n2))
  17063. (t (if (= cday n1) n1 n2)))))))
  17064. (defun org-date-to-gregorian (date)
  17065. "Turn any specification of DATE into a gregorian date for the calendar."
  17066. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  17067. ((and (listp date) (= (length date) 3)) date)
  17068. ((stringp date)
  17069. (setq date (org-parse-time-string date))
  17070. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  17071. ((listp date)
  17072. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  17073. (defun org-parse-time-string (s &optional nodefault)
  17074. "Parse the standard Org-mode time string.
  17075. This should be a lot faster than the normal `parse-time-string'.
  17076. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  17077. hour and minute fields will be nil if not given."
  17078. (if (string-match org-ts-regexp0 s)
  17079. (list 0
  17080. (if (or (match-beginning 8) (not nodefault))
  17081. (string-to-number (or (match-string 8 s) "0")))
  17082. (if (or (match-beginning 7) (not nodefault))
  17083. (string-to-number (or (match-string 7 s) "0")))
  17084. (string-to-number (match-string 4 s))
  17085. (string-to-number (match-string 3 s))
  17086. (string-to-number (match-string 2 s))
  17087. nil nil nil)
  17088. (make-list 9 0)))
  17089. (defun org-timestamp-up (&optional arg)
  17090. "Increase the date item at the cursor by one.
  17091. If the cursor is on the year, change the year. If it is on the month or
  17092. the day, change that.
  17093. With prefix ARG, change by that many units."
  17094. (interactive "p")
  17095. (org-timestamp-change (prefix-numeric-value arg)))
  17096. (defun org-timestamp-down (&optional arg)
  17097. "Decrease the date item at the cursor by one.
  17098. If the cursor is on the year, change the year. If it is on the month or
  17099. the day, change that.
  17100. With prefix ARG, change by that many units."
  17101. (interactive "p")
  17102. (org-timestamp-change (- (prefix-numeric-value arg))))
  17103. (defun org-timestamp-up-day (&optional arg)
  17104. "Increase the date in the time stamp by one day.
  17105. With prefix ARG, change that many days."
  17106. (interactive "p")
  17107. (if (and (not (org-at-timestamp-p t))
  17108. (org-on-heading-p))
  17109. (org-todo 'up)
  17110. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  17111. (defun org-timestamp-down-day (&optional arg)
  17112. "Decrease the date in the time stamp by one day.
  17113. With prefix ARG, change that many days."
  17114. (interactive "p")
  17115. (if (and (not (org-at-timestamp-p t))
  17116. (org-on-heading-p))
  17117. (org-todo 'down)
  17118. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  17119. (defsubst org-pos-in-match-range (pos n)
  17120. (and (match-beginning n)
  17121. (<= (match-beginning n) pos)
  17122. (>= (match-end n) pos)))
  17123. (defun org-at-timestamp-p (&optional inactive-ok)
  17124. "Determine if the cursor is in or at a timestamp."
  17125. (interactive)
  17126. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  17127. (pos (point))
  17128. (ans (or (looking-at tsr)
  17129. (save-excursion
  17130. (skip-chars-backward "^[<\n\r\t")
  17131. (if (> (point) (point-min)) (backward-char 1))
  17132. (and (looking-at tsr)
  17133. (> (- (match-end 0) pos) -1))))))
  17134. (and ans
  17135. (boundp 'org-ts-what)
  17136. (setq org-ts-what
  17137. (cond
  17138. ((= pos (match-beginning 0)) 'bracket)
  17139. ((= pos (1- (match-end 0))) 'bracket)
  17140. ((org-pos-in-match-range pos 2) 'year)
  17141. ((org-pos-in-match-range pos 3) 'month)
  17142. ((org-pos-in-match-range pos 7) 'hour)
  17143. ((org-pos-in-match-range pos 8) 'minute)
  17144. ((or (org-pos-in-match-range pos 4)
  17145. (org-pos-in-match-range pos 5)) 'day)
  17146. ((and (> pos (or (match-end 8) (match-end 5)))
  17147. (< pos (match-end 0)))
  17148. (- pos (or (match-end 8) (match-end 5))))
  17149. (t 'day))))
  17150. ans))
  17151. (defun org-toggle-timestamp-type ()
  17152. "Toggle the type (<active> or [inactive]) of a time stamp."
  17153. (interactive)
  17154. (when (org-at-timestamp-p t)
  17155. (save-excursion
  17156. (goto-char (match-beginning 0))
  17157. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  17158. (goto-char (1- (match-end 0)))
  17159. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  17160. (message "Timestamp is now %sactive"
  17161. (if (equal (char-before) ?>) "in" ""))))
  17162. (defun org-timestamp-change (n &optional what)
  17163. "Change the date in the time stamp at point.
  17164. The date will be changed by N times WHAT. WHAT can be `day', `month',
  17165. `year', `minute', `second'. If WHAT is not given, the cursor position
  17166. in the timestamp determines what will be changed."
  17167. (let ((pos (point))
  17168. with-hm inactive
  17169. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  17170. org-ts-what
  17171. extra rem
  17172. ts time time0)
  17173. (if (not (org-at-timestamp-p t))
  17174. (error "Not at a timestamp"))
  17175. (if (and (not what) (eq org-ts-what 'bracket))
  17176. (org-toggle-timestamp-type)
  17177. (if (and (not what) (not (eq org-ts-what 'day))
  17178. org-display-custom-times
  17179. (get-text-property (point) 'display)
  17180. (not (get-text-property (1- (point)) 'display)))
  17181. (setq org-ts-what 'day))
  17182. (setq org-ts-what (or what org-ts-what)
  17183. inactive (= (char-after (match-beginning 0)) ?\[)
  17184. ts (match-string 0))
  17185. (replace-match "")
  17186. (if (string-match
  17187. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[-+][0-9]+[dwmy]\\)*\\)[]>]"
  17188. ts)
  17189. (setq extra (match-string 1 ts)))
  17190. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17191. (setq with-hm t))
  17192. (setq time0 (org-parse-time-string ts))
  17193. (when (and (eq org-ts-what 'minute)
  17194. (eq current-prefix-arg nil))
  17195. (setq n (* dm (org-no-warnings (signum n))))
  17196. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  17197. (setcar (cdr time0) (+ (nth 1 time0)
  17198. (if (> n 0) (- rem) (- dm rem))))))
  17199. (setq time
  17200. (encode-time (or (car time0) 0)
  17201. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17202. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17203. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17204. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17205. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17206. (nthcdr 6 time0)))
  17207. (when (integerp org-ts-what)
  17208. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  17209. (if (eq what 'calendar)
  17210. (let ((cal-date (org-get-date-from-calendar)))
  17211. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17212. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17213. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17214. (setcar time0 (or (car time0) 0))
  17215. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17216. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17217. (setq time (apply 'encode-time time0))))
  17218. (setq org-last-changed-timestamp
  17219. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17220. (org-clock-update-time-maybe)
  17221. (goto-char pos)
  17222. ;; Try to recenter the calendar window, if any
  17223. (if (and org-calendar-follow-timestamp-change
  17224. (get-buffer-window "*Calendar*" t)
  17225. (memq org-ts-what '(day month year)))
  17226. (org-recenter-calendar (time-to-days time))))))
  17227. ;; FIXME: does not yet work for lead times
  17228. (defun org-modify-ts-extra (s pos n dm)
  17229. "Change the different parts of the lead-time and repeat fields in timestamp."
  17230. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17231. ng h m new rem)
  17232. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17233. (cond
  17234. ((or (org-pos-in-match-range pos 2)
  17235. (org-pos-in-match-range pos 3))
  17236. (setq m (string-to-number (match-string 3 s))
  17237. h (string-to-number (match-string 2 s)))
  17238. (if (org-pos-in-match-range pos 2)
  17239. (setq h (+ h n))
  17240. (setq n (* dm (org-no-warnings (signum n))))
  17241. (when (not (= 0 (setq rem (% m dm))))
  17242. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  17243. (setq m (+ m n)))
  17244. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17245. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17246. (setq h (min 24 (max 0 h)))
  17247. (setq ng 1 new (format "-%02d:%02d" h m)))
  17248. ((org-pos-in-match-range pos 6)
  17249. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17250. ((org-pos-in-match-range pos 5)
  17251. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  17252. ((org-pos-in-match-range pos 9)
  17253. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  17254. ((org-pos-in-match-range pos 8)
  17255. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  17256. (when ng
  17257. (setq s (concat
  17258. (substring s 0 (match-beginning ng))
  17259. new
  17260. (substring s (match-end ng))))))
  17261. s))
  17262. (defun org-recenter-calendar (date)
  17263. "If the calendar is visible, recenter it to DATE."
  17264. (let* ((win (selected-window))
  17265. (cwin (get-buffer-window "*Calendar*" t))
  17266. (calendar-move-hook nil))
  17267. (when cwin
  17268. (select-window cwin)
  17269. (calendar-goto-date (if (listp date) date
  17270. (calendar-gregorian-from-absolute date)))
  17271. (select-window win))))
  17272. (defun org-goto-calendar (&optional arg)
  17273. "Go to the Emacs calendar at the current date.
  17274. If there is a time stamp in the current line, go to that date.
  17275. A prefix ARG can be used to force the current date."
  17276. (interactive "P")
  17277. (let ((tsr org-ts-regexp) diff
  17278. (calendar-move-hook nil)
  17279. (view-calendar-holidays-initially nil)
  17280. (view-diary-entries-initially nil))
  17281. (if (or (org-at-timestamp-p)
  17282. (save-excursion
  17283. (beginning-of-line 1)
  17284. (looking-at (concat ".*" tsr))))
  17285. (let ((d1 (time-to-days (current-time)))
  17286. (d2 (time-to-days
  17287. (org-time-string-to-time (match-string 1)))))
  17288. (setq diff (- d2 d1))))
  17289. (calendar)
  17290. (calendar-goto-today)
  17291. (if (and diff (not arg)) (calendar-forward-day diff))))
  17292. (defun org-get-date-from-calendar ()
  17293. "Return a list (month day year) of date at point in calendar."
  17294. (with-current-buffer "*Calendar*"
  17295. (save-match-data
  17296. (calendar-cursor-to-date))))
  17297. (defun org-date-from-calendar ()
  17298. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17299. If there is already a time stamp at the cursor position, update it."
  17300. (interactive)
  17301. (if (org-at-timestamp-p t)
  17302. (org-timestamp-change 0 'calendar)
  17303. (let ((cal-date (org-get-date-from-calendar)))
  17304. (org-insert-time-stamp
  17305. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17306. (defvar appt-time-msg-list)
  17307. ;;;###autoload
  17308. (defun org-agenda-to-appt (&optional refresh filter)
  17309. "Activate appointments found in `org-agenda-files'.
  17310. With a \\[universal-argument] prefix, refresh the list of
  17311. appointements.
  17312. If FILTER is t, interactively prompt the user for a regular
  17313. expression, and filter out entries that don't match it.
  17314. If FILTER is a string, use this string as a regular expression
  17315. for filtering entries out.
  17316. FILTER can also be an alist with the car of each cell being
  17317. either 'headline or 'category. For example:
  17318. '((headline \"IMPORTANT\")
  17319. (category \"Work\"))
  17320. will only add headlines containing IMPORTANT or headlines
  17321. belonging to the \"Work\" category."
  17322. (interactive "P")
  17323. (require 'calendar)
  17324. (if refresh (setq appt-time-msg-list nil))
  17325. (if (eq filter t)
  17326. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17327. (let* ((cnt 0) ; count added events
  17328. (org-agenda-new-buffers nil)
  17329. (org-deadline-warning-days 0)
  17330. (today (org-date-to-gregorian
  17331. (time-to-days (current-time))))
  17332. (files (org-agenda-files)) entries file)
  17333. ;; Get all entries which may contain an appt
  17334. (while (setq file (pop files))
  17335. (setq entries
  17336. (append entries
  17337. (org-agenda-get-day-entries
  17338. file today :timestamp :scheduled :deadline))))
  17339. (setq entries (delq nil entries))
  17340. ;; Map thru entries and find if we should filter them out
  17341. (mapc
  17342. (lambda(x)
  17343. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17344. (cat (get-text-property 1 'org-category x))
  17345. (tod (get-text-property 1 'time-of-day x))
  17346. (ok (or (null filter)
  17347. (and (stringp filter) (string-match filter evt))
  17348. (and (listp filter)
  17349. (or (string-match
  17350. (cadr (assoc 'category filter)) cat)
  17351. (string-match
  17352. (cadr (assoc 'headline filter)) evt))))))
  17353. ;; FIXME: Shall we remove text-properties for the appt text?
  17354. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17355. (when (and ok tod)
  17356. (setq tod (number-to-string tod)
  17357. tod (when (string-match
  17358. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17359. (concat (match-string 1 tod) ":"
  17360. (match-string 2 tod))))
  17361. (appt-add tod evt)
  17362. (setq cnt (1+ cnt))))) entries)
  17363. (org-release-buffers org-agenda-new-buffers)
  17364. (if (eq cnt 0)
  17365. (message "No event to add")
  17366. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17367. ;;; The clock for measuring work time.
  17368. (defvar org-mode-line-string "")
  17369. (put 'org-mode-line-string 'risky-local-variable t)
  17370. (defvar org-mode-line-timer nil)
  17371. (defvar org-clock-heading "")
  17372. (defvar org-clock-start-time "")
  17373. (defun org-update-mode-line ()
  17374. (let* ((delta (- (time-to-seconds (current-time))
  17375. (time-to-seconds org-clock-start-time)))
  17376. (h (floor delta 3600))
  17377. (m (floor (- delta (* 3600 h)) 60)))
  17378. (setq org-mode-line-string
  17379. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17380. 'help-echo "Org-mode clock is running"))
  17381. (force-mode-line-update)))
  17382. (defvar org-clock-marker (make-marker)
  17383. "Marker recording the last clock-in.")
  17384. (defvar org-clock-mode-line-entry nil
  17385. "Information for the modeline about the running clock.")
  17386. (defun org-clock-in ()
  17387. "Start the clock on the current item.
  17388. If necessary, clock-out of the currently active clock."
  17389. (interactive)
  17390. (org-clock-out t)
  17391. (let (ts)
  17392. (save-excursion
  17393. (org-back-to-heading t)
  17394. (when (and org-clock-in-switch-to-state
  17395. (not (looking-at (concat outline-regexp "[ \t]*"
  17396. org-clock-in-switch-to-state
  17397. "\\>"))))
  17398. (org-todo org-clock-in-switch-to-state))
  17399. (if (and org-clock-heading-function
  17400. (functionp org-clock-heading-function))
  17401. (setq org-clock-heading (funcall org-clock-heading-function))
  17402. (if (looking-at org-complex-heading-regexp)
  17403. (setq org-clock-heading (match-string 4))
  17404. (setq org-clock-heading "???")))
  17405. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17406. (org-clock-find-position)
  17407. (insert "\n") (backward-char 1)
  17408. (indent-relative)
  17409. (insert org-clock-string " ")
  17410. (setq org-clock-start-time (current-time))
  17411. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17412. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17413. (or global-mode-string (setq global-mode-string '("")))
  17414. (or (memq 'org-mode-line-string global-mode-string)
  17415. (setq global-mode-string
  17416. (append global-mode-string '(org-mode-line-string))))
  17417. (org-update-mode-line)
  17418. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17419. (message "Clock started at %s" ts))))
  17420. (defun org-clock-find-position ()
  17421. "Find the location where the next clock line should be inserted."
  17422. (org-back-to-heading t)
  17423. (catch 'exit
  17424. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17425. (re (concat "^[ \t]*" org-clock-string))
  17426. (cnt 0)
  17427. first last)
  17428. (goto-char beg)
  17429. (when (eobp) (newline) (setq end (max (point) end)))
  17430. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17431. ;; we seem to have a CLOCK drawer, so go there.
  17432. (beginning-of-line 2)
  17433. (throw 'exit t))
  17434. ;; Lets count the CLOCK lines
  17435. (goto-char beg)
  17436. (while (re-search-forward re end t)
  17437. (setq first (or first (match-beginning 0))
  17438. last (match-beginning 0)
  17439. cnt (1+ cnt)))
  17440. (when (and (integerp org-clock-into-drawer)
  17441. (>= (1+ cnt) org-clock-into-drawer))
  17442. ;; Wrap current entries into a new drawer
  17443. (goto-char last)
  17444. (beginning-of-line 2)
  17445. (if (org-at-item-p) (org-end-of-item))
  17446. (insert ":END:\n")
  17447. (beginning-of-line 0)
  17448. (org-indent-line-function)
  17449. (goto-char first)
  17450. (insert ":CLOCK:\n")
  17451. (beginning-of-line 0)
  17452. (org-indent-line-function)
  17453. (org-flag-drawer t)
  17454. (beginning-of-line 2)
  17455. (throw 'exit nil))
  17456. (goto-char beg)
  17457. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17458. (not (equal (match-string 1) org-clock-string)))
  17459. ;; Planning info, skip to after it
  17460. (beginning-of-line 2)
  17461. (or (bolp) (newline)))
  17462. (when (eq t org-clock-into-drawer)
  17463. (insert ":CLOCK:\n:END:\n")
  17464. (beginning-of-line -1)
  17465. (org-indent-line-function)
  17466. (org-flag-drawer t)
  17467. (beginning-of-line 2)
  17468. (org-indent-line-function)))))
  17469. (defun org-clock-out (&optional fail-quietly)
  17470. "Stop the currently running clock.
  17471. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17472. (interactive)
  17473. (catch 'exit
  17474. (if (not (marker-buffer org-clock-marker))
  17475. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17476. (let (ts te s h m)
  17477. (save-excursion
  17478. (set-buffer (marker-buffer org-clock-marker))
  17479. (goto-char org-clock-marker)
  17480. (beginning-of-line 1)
  17481. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17482. (equal (match-string 1) org-clock-string))
  17483. (setq ts (match-string 2))
  17484. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17485. (goto-char (match-end 0))
  17486. (delete-region (point) (point-at-eol))
  17487. (insert "--")
  17488. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17489. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17490. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17491. h (floor (/ s 3600))
  17492. s (- s (* 3600 h))
  17493. m (floor (/ s 60))
  17494. s (- s (* 60 s)))
  17495. (insert " => " (format "%2d:%02d" h m))
  17496. (move-marker org-clock-marker nil)
  17497. (when org-log-note-clock-out
  17498. (org-add-log-maybe 'clock-out))
  17499. (when org-mode-line-timer
  17500. (cancel-timer org-mode-line-timer)
  17501. (setq org-mode-line-timer nil))
  17502. (setq global-mode-string
  17503. (delq 'org-mode-line-string global-mode-string))
  17504. (force-mode-line-update)
  17505. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17506. (defun org-clock-cancel ()
  17507. "Cancel the running clock be removing the start timestamp."
  17508. (interactive)
  17509. (if (not (marker-buffer org-clock-marker))
  17510. (error "No active clock"))
  17511. (save-excursion
  17512. (set-buffer (marker-buffer org-clock-marker))
  17513. (goto-char org-clock-marker)
  17514. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17515. (setq global-mode-string
  17516. (delq 'org-mode-line-string global-mode-string))
  17517. (force-mode-line-update)
  17518. (message "Clock canceled"))
  17519. (defun org-clock-goto (&optional delete-windows)
  17520. "Go to the currently clocked-in entry."
  17521. (interactive "P")
  17522. (if (not (marker-buffer org-clock-marker))
  17523. (error "No active clock"))
  17524. (switch-to-buffer-other-window
  17525. (marker-buffer org-clock-marker))
  17526. (if delete-windows (delete-other-windows))
  17527. (goto-char org-clock-marker)
  17528. (org-show-entry)
  17529. (org-back-to-heading)
  17530. (recenter))
  17531. (defvar org-clock-file-total-minutes nil
  17532. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17533. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17534. (defun org-clock-sum (&optional tstart tend)
  17535. "Sum the times for each subtree.
  17536. Puts the resulting times in minutes as a text property on each headline."
  17537. (interactive)
  17538. (let* ((bmp (buffer-modified-p))
  17539. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17540. org-clock-string
  17541. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17542. (lmax 30)
  17543. (ltimes (make-vector lmax 0))
  17544. (t1 0)
  17545. (level 0)
  17546. ts te dt
  17547. time)
  17548. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17549. (save-excursion
  17550. (goto-char (point-max))
  17551. (while (re-search-backward re nil t)
  17552. (cond
  17553. ((match-end 2)
  17554. ;; Two time stamps
  17555. (setq ts (match-string 2)
  17556. te (match-string 3)
  17557. ts (time-to-seconds
  17558. (apply 'encode-time (org-parse-time-string ts)))
  17559. te (time-to-seconds
  17560. (apply 'encode-time (org-parse-time-string te)))
  17561. ts (if tstart (max ts tstart) ts)
  17562. te (if tend (min te tend) te)
  17563. dt (- te ts)
  17564. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17565. ((match-end 4)
  17566. ;; A naket time
  17567. (setq t1 (+ t1 (string-to-number (match-string 5))
  17568. (* 60 (string-to-number (match-string 4))))))
  17569. (t ;; A headline
  17570. (setq level (- (match-end 1) (match-beginning 1)))
  17571. (when (or (> t1 0) (> (aref ltimes level) 0))
  17572. (loop for l from 0 to level do
  17573. (aset ltimes l (+ (aref ltimes l) t1)))
  17574. (setq t1 0 time (aref ltimes level))
  17575. (loop for l from level to (1- lmax) do
  17576. (aset ltimes l 0))
  17577. (goto-char (match-beginning 0))
  17578. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17579. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17580. (set-buffer-modified-p bmp)))
  17581. (defun org-clock-display (&optional total-only)
  17582. "Show subtree times in the entire buffer.
  17583. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17584. in the echo area."
  17585. (interactive)
  17586. (org-remove-clock-overlays)
  17587. (let (time h m p)
  17588. (org-clock-sum)
  17589. (unless total-only
  17590. (save-excursion
  17591. (goto-char (point-min))
  17592. (while (or (and (equal (setq p (point)) (point-min))
  17593. (get-text-property p :org-clock-minutes))
  17594. (setq p (next-single-property-change
  17595. (point) :org-clock-minutes)))
  17596. (goto-char p)
  17597. (when (setq time (get-text-property p :org-clock-minutes))
  17598. (org-put-clock-overlay time (funcall outline-level))))
  17599. (setq h (/ org-clock-file-total-minutes 60)
  17600. m (- org-clock-file-total-minutes (* 60 h)))
  17601. ;; Arrange to remove the overlays upon next change.
  17602. (when org-remove-highlights-with-change
  17603. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17604. nil 'local))))
  17605. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17606. (defvar org-clock-overlays nil)
  17607. (make-variable-buffer-local 'org-clock-overlays)
  17608. (defun org-put-clock-overlay (time &optional level)
  17609. "Put an overlays on the current line, displaying TIME.
  17610. If LEVEL is given, prefix time with a corresponding number of stars.
  17611. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17612. will be easy to remove."
  17613. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17614. (l (if level (org-get-valid-level level 0) 0))
  17615. (off 0)
  17616. ov tx)
  17617. (move-to-column c)
  17618. (unless (eolp) (skip-chars-backward "^ \t"))
  17619. (skip-chars-backward " \t")
  17620. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17621. tx (concat (buffer-substring (1- (point)) (point))
  17622. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17623. (org-add-props (format "%s %2d:%02d%s"
  17624. (make-string l ?*) h m
  17625. (make-string (- 16 l) ?\ ))
  17626. '(face secondary-selection))
  17627. ""))
  17628. (if (not (featurep 'xemacs))
  17629. (org-overlay-put ov 'display tx)
  17630. (org-overlay-put ov 'invisible t)
  17631. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17632. (push ov org-clock-overlays)))
  17633. (defun org-remove-clock-overlays (&optional beg end noremove)
  17634. "Remove the occur highlights from the buffer.
  17635. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17636. from the `before-change-functions' in the current buffer."
  17637. (interactive)
  17638. (unless org-inhibit-highlight-removal
  17639. (mapc 'org-delete-overlay org-clock-overlays)
  17640. (setq org-clock-overlays nil)
  17641. (unless noremove
  17642. (remove-hook 'before-change-functions
  17643. 'org-remove-clock-overlays 'local))))
  17644. (defun org-clock-out-if-current ()
  17645. "Clock out if the current entry contains the running clock.
  17646. This is used to stop the clock after a TODO entry is marked DONE,
  17647. and is only done if the variable `org-clock-out-when-done' is not nil."
  17648. (when (and org-clock-out-when-done
  17649. (member state org-done-keywords)
  17650. (equal (marker-buffer org-clock-marker) (current-buffer))
  17651. (< (point) org-clock-marker)
  17652. (> (save-excursion (outline-next-heading) (point))
  17653. org-clock-marker))
  17654. ;; Clock out, but don't accept a logging message for this.
  17655. (let ((org-log-note-clock-out nil))
  17656. (org-clock-out))))
  17657. (add-hook 'org-after-todo-state-change-hook
  17658. 'org-clock-out-if-current)
  17659. (defun org-check-running-clock ()
  17660. "Check if the current buffer contains the running clock.
  17661. If yes, offer to stop it and to save the buffer with the changes."
  17662. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17663. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17664. (buffer-name))))
  17665. (org-clock-out)
  17666. (when (y-or-n-p "Save changed buffer?")
  17667. (save-buffer))))
  17668. (defun org-clock-report (&optional arg)
  17669. "Create a table containing a report about clocked time.
  17670. If the cursor is inside an existing clocktable block, then the table
  17671. will be updated. If not, a new clocktable will be inserted.
  17672. When called with a prefix argument, move to the first clock table in the
  17673. buffer and update it."
  17674. (interactive "P")
  17675. (org-remove-clock-overlays)
  17676. (when arg
  17677. (org-find-dblock "clocktable")
  17678. (org-show-entry))
  17679. (if (org-in-clocktable-p)
  17680. (goto-char (org-in-clocktable-p))
  17681. (org-create-dblock (list :name "clocktable"
  17682. :maxlevel 2 :scope 'file)))
  17683. (org-update-dblock))
  17684. (defun org-in-clocktable-p ()
  17685. "Check if the cursor is in a clocktable."
  17686. (let ((pos (point)) start)
  17687. (save-excursion
  17688. (end-of-line 1)
  17689. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17690. (setq start (match-beginning 0))
  17691. (re-search-forward "^#\\+END:.*" nil t)
  17692. (>= (match-end 0) pos)
  17693. start))))
  17694. (defun org-clock-update-time-maybe ()
  17695. "If this is a CLOCK line, update it and return t.
  17696. Otherwise, return nil."
  17697. (interactive)
  17698. (save-excursion
  17699. (beginning-of-line 1)
  17700. (skip-chars-forward " \t")
  17701. (when (looking-at org-clock-string)
  17702. (let ((re (concat "[ \t]*" org-clock-string
  17703. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17704. "\\([ \t]*=>.*\\)?"))
  17705. ts te h m s)
  17706. (if (not (looking-at re))
  17707. nil
  17708. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17709. (end-of-line 1)
  17710. (setq ts (match-string 1)
  17711. te (match-string 2))
  17712. (setq s (- (time-to-seconds
  17713. (apply 'encode-time (org-parse-time-string te)))
  17714. (time-to-seconds
  17715. (apply 'encode-time (org-parse-time-string ts))))
  17716. h (floor (/ s 3600))
  17717. s (- s (* 3600 h))
  17718. m (floor (/ s 60))
  17719. s (- s (* 60 s)))
  17720. (insert " => " (format "%2d:%02d" h m))
  17721. t)))))
  17722. (defun org-clock-special-range (key &optional time as-strings)
  17723. "Return two times bordering a special time range.
  17724. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17725. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17726. A week starts Monday 0:00 and ends Sunday 24:00.
  17727. The range is determined relative to TIME. TIME defaults to the current time.
  17728. The return value is a cons cell with two internal times like the ones
  17729. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17730. the returned times will be formatted strings."
  17731. (let* ((tm (decode-time (or time (current-time))))
  17732. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17733. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17734. (dow (nth 6 tm))
  17735. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17736. (cond
  17737. ((eq key 'today)
  17738. (setq h 0 m 0 h1 24 m1 0))
  17739. ((eq key 'yesterday)
  17740. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17741. ((eq key 'thisweek)
  17742. (setq diff (if (= dow 0) 6 (1- dow))
  17743. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17744. ((eq key 'lastweek)
  17745. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17746. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17747. ((eq key 'thismonth)
  17748. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17749. ((eq key 'lastmonth)
  17750. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17751. ((eq key 'thisyear)
  17752. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17753. ((eq key 'lastyear)
  17754. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17755. (t (error "No such time block %s" key)))
  17756. (setq ts (encode-time s m h d month y)
  17757. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17758. (or d1 d) (or month1 month) (or y1 y)))
  17759. (setq fm (cdr org-time-stamp-formats))
  17760. (if as-strings
  17761. (cons (format-time-string fm ts) (format-time-string fm te))
  17762. (cons ts te))))
  17763. (defun org-dblock-write:clocktable (params)
  17764. "Write the standard clocktable."
  17765. (catch 'exit
  17766. (let* ((hlchars '((1 . "*") (2 . "/")))
  17767. (ins (make-marker))
  17768. (total-time nil)
  17769. (scope (plist-get params :scope))
  17770. (tostring (plist-get params :tostring))
  17771. (multifile (plist-get params :multifile))
  17772. (header (plist-get params :header))
  17773. (maxlevel (or (plist-get params :maxlevel) 3))
  17774. (step (plist-get params :step))
  17775. (emph (plist-get params :emphasize))
  17776. (ts (plist-get params :tstart))
  17777. (te (plist-get params :tend))
  17778. (block (plist-get params :block))
  17779. (link (plist-get params :link))
  17780. ipos time h m p level hlc hdl
  17781. cc beg end pos tbl)
  17782. (when step
  17783. (org-clocktable-steps params)
  17784. (throw 'exit nil))
  17785. (when block
  17786. (setq cc (org-clock-special-range block nil t)
  17787. ts (car cc) te (cdr cc)))
  17788. (if ts (setq ts (time-to-seconds
  17789. (apply 'encode-time (org-parse-time-string ts)))))
  17790. (if te (setq te (time-to-seconds
  17791. (apply 'encode-time (org-parse-time-string te)))))
  17792. (move-marker ins (point))
  17793. (setq ipos (point))
  17794. ;; Get the right scope
  17795. (setq pos (point))
  17796. (save-restriction
  17797. (cond
  17798. ((not scope))
  17799. ((eq scope 'file) (widen))
  17800. ((eq scope 'subtree) (org-narrow-to-subtree))
  17801. ((eq scope 'tree)
  17802. (while (org-up-heading-safe))
  17803. (org-narrow-to-subtree))
  17804. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17805. (symbol-name scope)))
  17806. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17807. (catch 'exit
  17808. (while (org-up-heading-safe)
  17809. (looking-at outline-regexp)
  17810. (if (<= (org-reduced-level (funcall outline-level)) level)
  17811. (throw 'exit nil))))
  17812. (org-narrow-to-subtree))
  17813. ((or (listp scope) (eq scope 'agenda))
  17814. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17815. (scope 'agenda)
  17816. (p1 (copy-sequence params))
  17817. file)
  17818. (plist-put p1 :tostring t)
  17819. (plist-put p1 :multifile t)
  17820. (plist-put p1 :scope 'file)
  17821. (org-prepare-agenda-buffers files)
  17822. (while (setq file (pop files))
  17823. (with-current-buffer (find-buffer-visiting file)
  17824. (push (org-clocktable-add-file
  17825. file (org-dblock-write:clocktable p1)) tbl)
  17826. (setq total-time (+ (or total-time 0)
  17827. org-clock-file-total-minutes)))))))
  17828. (goto-char pos)
  17829. (unless (eq scope 'agenda)
  17830. (org-clock-sum ts te)
  17831. (goto-char (point-min))
  17832. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17833. (goto-char p)
  17834. (when (setq time (get-text-property p :org-clock-minutes))
  17835. (save-excursion
  17836. (beginning-of-line 1)
  17837. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17838. (setq level (org-reduced-level
  17839. (- (match-end 1) (match-beginning 1))))
  17840. (<= level maxlevel))
  17841. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17842. hdl (if (not link)
  17843. (match-string 2)
  17844. (org-make-link-string
  17845. (format "file:%s::%s"
  17846. (buffer-file-name)
  17847. (save-match-data
  17848. (org-make-org-heading-search-string
  17849. (match-string 2))))
  17850. (match-string 2)))
  17851. h (/ time 60)
  17852. m (- time (* 60 h)))
  17853. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17854. (push (concat
  17855. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17856. (make-string (1- level) ?|)
  17857. hlc (format "%d:%02d" h m) hlc
  17858. " |") tbl))))))
  17859. (setq tbl (nreverse tbl))
  17860. (if tostring
  17861. (if tbl (mapconcat 'identity tbl "\n") nil)
  17862. (goto-char ins)
  17863. (insert-before-markers
  17864. (or header
  17865. (concat
  17866. "Clock summary at ["
  17867. (substring
  17868. (format-time-string (cdr org-time-stamp-formats))
  17869. 1 -1)
  17870. "]."
  17871. (if block
  17872. (format " Considered range is /%s/." block)
  17873. "")
  17874. "\n\n"))
  17875. (if (eq scope 'agenda) "|File" "")
  17876. "|L|Headline|Time|\n")
  17877. (setq total-time (or total-time org-clock-file-total-minutes)
  17878. h (/ total-time 60)
  17879. m (- total-time (* 60 h)))
  17880. (insert-before-markers
  17881. "|-\n|"
  17882. (if (eq scope 'agenda) "|" "")
  17883. "|"
  17884. "*Total time*| "
  17885. (format "*%d:%02d*" h m)
  17886. "|\n|-\n")
  17887. (setq tbl (delq nil tbl))
  17888. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17889. (equal (substring (car tbl) 0 2) "|-"))
  17890. (pop tbl))
  17891. (insert-before-markers (mapconcat
  17892. 'identity (delq nil tbl)
  17893. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17894. (backward-delete-char 1)
  17895. (goto-char ipos)
  17896. (skip-chars-forward "^|")
  17897. (org-table-align))))))
  17898. (defun org-clocktable-steps (params)
  17899. (let* ((p1 (copy-sequence params))
  17900. (ts (plist-get p1 :tstart))
  17901. (te (plist-get p1 :tend))
  17902. (step0 (plist-get p1 :step))
  17903. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17904. (block (plist-get p1 :block))
  17905. cc)
  17906. (when block
  17907. (setq cc (org-clock-special-range block nil t)
  17908. ts (car cc) te (cdr cc)))
  17909. (if ts (setq ts (time-to-seconds
  17910. (apply 'encode-time (org-parse-time-string ts)))))
  17911. (if te (setq te (time-to-seconds
  17912. (apply 'encode-time (org-parse-time-string te)))))
  17913. (plist-put p1 :header "")
  17914. (plist-put p1 :step nil)
  17915. (plist-put p1 :block nil)
  17916. (while (< ts te)
  17917. (or (bolp) (insert "\n"))
  17918. (plist-put p1 :tstart (format-time-string
  17919. (car org-time-stamp-formats)
  17920. (seconds-to-time ts)))
  17921. (plist-put p1 :tend (format-time-string
  17922. (car org-time-stamp-formats)
  17923. (seconds-to-time (setq ts (+ ts step)))))
  17924. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17925. (plist-get p1 :tstart) "\n")
  17926. (org-dblock-write:clocktable p1)
  17927. (re-search-forward "#\\+END:")
  17928. (end-of-line 0))))
  17929. (defun org-clocktable-add-file (file table)
  17930. (if table
  17931. (let ((lines (org-split-string table "\n"))
  17932. (ff (file-name-nondirectory file)))
  17933. (mapconcat 'identity
  17934. (mapcar (lambda (x)
  17935. (if (string-match org-table-dataline-regexp x)
  17936. (concat "|" ff x)
  17937. x))
  17938. lines)
  17939. "\n"))))
  17940. ;; FIXME: I don't think anybody uses this, ask David
  17941. (defun org-collect-clock-time-entries ()
  17942. "Return an internal list with clocking information.
  17943. This list has one entry for each CLOCK interval.
  17944. FIXME: describe the elements."
  17945. (interactive)
  17946. (let ((re (concat "^[ \t]*" org-clock-string
  17947. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17948. rtn beg end next cont level title total closedp leafp
  17949. clockpos titlepos h m donep)
  17950. (save-excursion
  17951. (org-clock-sum)
  17952. (goto-char (point-min))
  17953. (while (re-search-forward re nil t)
  17954. (setq clockpos (match-beginning 0)
  17955. beg (match-string 1) end (match-string 2)
  17956. cont (match-end 0))
  17957. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17958. end (apply 'encode-time (org-parse-time-string end)))
  17959. (org-back-to-heading t)
  17960. (setq donep (org-entry-is-done-p))
  17961. (setq titlepos (point)
  17962. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17963. h (/ total 60) m (- total (* 60 h))
  17964. total (cons h m))
  17965. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17966. (setq level (- (match-end 1) (match-beginning 1))
  17967. title (org-match-string-no-properties 2))
  17968. (save-excursion (outline-next-heading) (setq next (point)))
  17969. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17970. (goto-char next)
  17971. (setq leafp (and (looking-at "^\\*+ ")
  17972. (<= (- (match-end 0) (point)) level)))
  17973. (push (list beg end clockpos closedp donep
  17974. total title titlepos level leafp)
  17975. rtn)
  17976. (goto-char cont)))
  17977. (nreverse rtn)))
  17978. ;;;; Agenda, and Diary Integration
  17979. ;;; Define the Org-agenda-mode
  17980. (defvar org-agenda-mode-map (make-sparse-keymap)
  17981. "Keymap for `org-agenda-mode'.")
  17982. (defvar org-agenda-menu) ; defined later in this file.
  17983. (defvar org-agenda-follow-mode nil)
  17984. (defvar org-agenda-show-log nil)
  17985. (defvar org-agenda-redo-command nil)
  17986. (defvar org-agenda-query-string nil)
  17987. (defvar org-agenda-mode-hook nil)
  17988. (defvar org-agenda-type nil)
  17989. (defvar org-agenda-force-single-file nil)
  17990. (defun org-agenda-mode ()
  17991. "Mode for time-sorted view on action items in Org-mode files.
  17992. The following commands are available:
  17993. \\{org-agenda-mode-map}"
  17994. (interactive)
  17995. (kill-all-local-variables)
  17996. (setq org-agenda-undo-list nil
  17997. org-agenda-pending-undo-list nil)
  17998. (setq major-mode 'org-agenda-mode)
  17999. ;; Keep global-font-lock-mode from turning on font-lock-mode
  18000. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  18001. (setq mode-name "Org-Agenda")
  18002. (use-local-map org-agenda-mode-map)
  18003. (easy-menu-add org-agenda-menu)
  18004. (if org-startup-truncated (setq truncate-lines t))
  18005. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  18006. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  18007. ;; Make sure properties are removed when copying text
  18008. (when (boundp 'buffer-substring-filters)
  18009. (org-set-local 'buffer-substring-filters
  18010. (cons (lambda (x)
  18011. (set-text-properties 0 (length x) nil x) x)
  18012. buffer-substring-filters)))
  18013. (unless org-agenda-keep-modes
  18014. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  18015. org-agenda-show-log nil))
  18016. (easy-menu-change
  18017. '("Agenda") "Agenda Files"
  18018. (append
  18019. (list
  18020. (vector
  18021. (if (get 'org-agenda-files 'org-restrict)
  18022. "Restricted to single file"
  18023. "Edit File List")
  18024. '(org-edit-agenda-file-list)
  18025. (not (get 'org-agenda-files 'org-restrict)))
  18026. "--")
  18027. (mapcar 'org-file-menu-entry (org-agenda-files))))
  18028. (org-agenda-set-mode-name)
  18029. (apply
  18030. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  18031. (list 'org-agenda-mode-hook)))
  18032. (substitute-key-definition 'undo 'org-agenda-undo
  18033. org-agenda-mode-map global-map)
  18034. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  18035. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  18036. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  18037. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  18038. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  18039. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  18040. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  18041. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  18042. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  18043. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  18044. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  18045. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  18046. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  18047. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  18048. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  18049. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  18050. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  18051. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  18052. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  18053. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  18054. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  18055. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  18056. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  18057. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  18058. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  18059. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  18060. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  18061. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  18062. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  18063. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  18064. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  18065. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  18066. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  18067. (while l (org-defkey org-agenda-mode-map
  18068. (int-to-string (pop l)) 'digit-argument)))
  18069. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  18070. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  18071. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  18072. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  18073. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  18074. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  18075. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  18076. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  18077. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  18078. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  18079. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  18080. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  18081. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  18082. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  18083. (org-defkey org-agenda-mode-map "n" 'next-line)
  18084. (org-defkey org-agenda-mode-map "p" 'previous-line)
  18085. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  18086. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  18087. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  18088. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  18089. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  18090. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  18091. (eval-after-load "calendar"
  18092. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  18093. 'org-calendar-goto-agenda))
  18094. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  18095. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  18096. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  18097. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  18098. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  18099. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  18100. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  18101. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  18102. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  18103. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  18104. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  18105. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18106. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  18107. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  18108. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  18109. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  18110. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  18111. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  18112. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  18113. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  18114. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  18115. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  18116. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  18117. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  18118. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  18119. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  18120. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  18121. "Local keymap for agenda entries from Org-mode.")
  18122. (org-defkey org-agenda-keymap
  18123. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  18124. (org-defkey org-agenda-keymap
  18125. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  18126. (when org-agenda-mouse-1-follows-link
  18127. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  18128. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  18129. '("Agenda"
  18130. ("Agenda Files")
  18131. "--"
  18132. ["Show" org-agenda-show t]
  18133. ["Go To (other window)" org-agenda-goto t]
  18134. ["Go To (this window)" org-agenda-switch-to t]
  18135. ["Follow Mode" org-agenda-follow-mode
  18136. :style toggle :selected org-agenda-follow-mode :active t]
  18137. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  18138. "--"
  18139. ["Cycle TODO" org-agenda-todo t]
  18140. ["Archive subtree" org-agenda-archive t]
  18141. ["Delete subtree" org-agenda-kill t]
  18142. "--"
  18143. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  18144. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  18145. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  18146. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  18147. "--"
  18148. ("Tags and Properties"
  18149. ["Show all Tags" org-agenda-show-tags t]
  18150. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  18151. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  18152. "--"
  18153. ["Column View" org-columns t])
  18154. ("Date/Schedule"
  18155. ["Schedule" org-agenda-schedule t]
  18156. ["Set Deadline" org-agenda-deadline t]
  18157. "--"
  18158. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  18159. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  18160. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  18161. ("Clock"
  18162. ["Clock in" org-agenda-clock-in t]
  18163. ["Clock out" org-agenda-clock-out t]
  18164. ["Clock cancel" org-agenda-clock-cancel t]
  18165. ["Goto running clock" org-clock-goto t])
  18166. ("Priority"
  18167. ["Set Priority" org-agenda-priority t]
  18168. ["Increase Priority" org-agenda-priority-up t]
  18169. ["Decrease Priority" org-agenda-priority-down t]
  18170. ["Show Priority" org-agenda-show-priority t])
  18171. ("Calendar/Diary"
  18172. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  18173. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  18174. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  18175. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  18176. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  18177. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  18178. "--"
  18179. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  18180. "--"
  18181. ("View"
  18182. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  18183. :style radio :selected (equal org-agenda-ndays 1)]
  18184. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  18185. :style radio :selected (equal org-agenda-ndays 7)]
  18186. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  18187. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  18188. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  18189. :style radio :selected (member org-agenda-ndays '(365 366))]
  18190. "--"
  18191. ["Show Logbook entries" org-agenda-log-mode
  18192. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  18193. ["Include Diary" org-agenda-toggle-diary
  18194. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  18195. ["Use Time Grid" org-agenda-toggle-time-grid
  18196. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  18197. ["Write view to file" org-write-agenda t]
  18198. ["Rebuild buffer" org-agenda-redo t]
  18199. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18200. "--"
  18201. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18202. "--"
  18203. ["Quit" org-agenda-quit t]
  18204. ["Exit and Release Buffers" org-agenda-exit t]
  18205. ))
  18206. ;;; Agenda undo
  18207. (defvar org-agenda-allow-remote-undo t
  18208. "Non-nil means, allow remote undo from the agenda buffer.")
  18209. (defvar org-agenda-undo-list nil
  18210. "List of undoable operations in the agenda since last refresh.")
  18211. (defvar org-agenda-undo-has-started-in nil
  18212. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18213. (defvar org-agenda-pending-undo-list nil
  18214. "In a series of undo commands, this is the list of remaning undo items.")
  18215. (defmacro org-if-unprotected (&rest body)
  18216. "Execute BODY if there is no `org-protected' text property at point."
  18217. (declare (debug t))
  18218. `(unless (get-text-property (point) 'org-protected)
  18219. ,@body))
  18220. (defmacro org-with-remote-undo (_buffer &rest _body)
  18221. "Execute BODY while recording undo information in two buffers."
  18222. (declare (indent 1) (debug t))
  18223. `(let ((_cline (org-current-line))
  18224. (_cmd this-command)
  18225. (_buf1 (current-buffer))
  18226. (_buf2 ,_buffer)
  18227. (_undo1 buffer-undo-list)
  18228. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18229. _c1 _c2)
  18230. ,@_body
  18231. (when org-agenda-allow-remote-undo
  18232. (setq _c1 (org-verify-change-for-undo
  18233. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18234. _c2 (org-verify-change-for-undo
  18235. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18236. (when (or _c1 _c2)
  18237. ;; make sure there are undo boundaries
  18238. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18239. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18240. ;; remember which buffer to undo
  18241. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18242. org-agenda-undo-list)))))
  18243. (defun org-agenda-undo ()
  18244. "Undo a remote editing step in the agenda.
  18245. This undoes changes both in the agenda buffer and in the remote buffer
  18246. that have been changed along."
  18247. (interactive)
  18248. (or org-agenda-allow-remote-undo
  18249. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18250. (if (not (eq this-command last-command))
  18251. (setq org-agenda-undo-has-started-in nil
  18252. org-agenda-pending-undo-list org-agenda-undo-list))
  18253. (if (not org-agenda-pending-undo-list)
  18254. (error "No further undo information"))
  18255. (let* ((entry (pop org-agenda-pending-undo-list))
  18256. buf line cmd rembuf)
  18257. (setq cmd (pop entry) line (pop entry))
  18258. (setq rembuf (nth 2 entry))
  18259. (org-with-remote-undo rembuf
  18260. (while (bufferp (setq buf (pop entry)))
  18261. (if (pop entry)
  18262. (with-current-buffer buf
  18263. (let ((last-undo-buffer buf)
  18264. (inhibit-read-only t))
  18265. (unless (memq buf org-agenda-undo-has-started-in)
  18266. (push buf org-agenda-undo-has-started-in)
  18267. (make-local-variable 'pending-undo-list)
  18268. (undo-start))
  18269. (while (and pending-undo-list
  18270. (listp pending-undo-list)
  18271. (not (car pending-undo-list)))
  18272. (pop pending-undo-list))
  18273. (undo-more 1))))))
  18274. (goto-line line)
  18275. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18276. (defun org-verify-change-for-undo (l1 l2)
  18277. "Verify that a real change occurred between the undo lists L1 and L2."
  18278. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18279. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18280. (not (eq l1 l2)))
  18281. ;;; Agenda dispatch
  18282. (defvar org-agenda-restrict nil)
  18283. (defvar org-agenda-restrict-begin (make-marker))
  18284. (defvar org-agenda-restrict-end (make-marker))
  18285. (defvar org-agenda-last-dispatch-buffer nil)
  18286. (defvar org-agenda-overriding-restriction nil)
  18287. ;;;###autoload
  18288. (defun org-agenda (arg &optional keys restriction)
  18289. "Dispatch agenda commands to collect entries to the agenda buffer.
  18290. Prompts for a command to execute. Any prefix arg will be passed
  18291. on to the selected command. The default selections are:
  18292. a Call `org-agenda-list' to display the agenda for current day or week.
  18293. t Call `org-todo-list' to display the global todo list.
  18294. T Call `org-todo-list' to display the global todo list, select only
  18295. entries with a specific TODO keyword (the user gets a prompt).
  18296. m Call `org-tags-view' to display headlines with tags matching
  18297. a condition (the user is prompted for the condition).
  18298. M Like `m', but select only TODO entries, no ordinary headlines.
  18299. L Create a timeline for the current buffer.
  18300. e Export views to associated files.
  18301. More commands can be added by configuring the variable
  18302. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18303. searches can be pre-defined in this way.
  18304. If the current buffer is in Org-mode and visiting a file, you can also
  18305. first press `<' once to indicate that the agenda should be temporarily
  18306. \(until the next use of \\[org-agenda]) restricted to the current file.
  18307. Pressing `<' twice means to restrict to the current subtree or region
  18308. \(if active)."
  18309. (interactive "P")
  18310. (catch 'exit
  18311. (let* ((prefix-descriptions nil)
  18312. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18313. (org-agenda-custom-commands
  18314. ;; normalize different versions
  18315. (delq nil
  18316. (mapcar
  18317. (lambda (x)
  18318. (cond ((stringp (cdr x))
  18319. (push x prefix-descriptions)
  18320. nil)
  18321. ((stringp (nth 1 x)) x)
  18322. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18323. (t (cons (car x) (cons "" (cdr x))))))
  18324. org-agenda-custom-commands)))
  18325. (buf (current-buffer))
  18326. (bfn (buffer-file-name (buffer-base-buffer)))
  18327. entry key type match lprops ans)
  18328. ;; Turn off restriction unless there is an overriding one
  18329. (unless org-agenda-overriding-restriction
  18330. (put 'org-agenda-files 'org-restrict nil)
  18331. (setq org-agenda-restrict nil)
  18332. (move-marker org-agenda-restrict-begin nil)
  18333. (move-marker org-agenda-restrict-end nil))
  18334. ;; Delete old local properties
  18335. (put 'org-agenda-redo-command 'org-lprops nil)
  18336. ;; Remember where this call originated
  18337. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18338. (unless keys
  18339. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18340. keys (car ans)
  18341. restriction (cdr ans)))
  18342. ;; Estabish the restriction, if any
  18343. (when (and (not org-agenda-overriding-restriction) restriction)
  18344. (put 'org-agenda-files 'org-restrict (list bfn))
  18345. (cond
  18346. ((eq restriction 'region)
  18347. (setq org-agenda-restrict t)
  18348. (move-marker org-agenda-restrict-begin (region-beginning))
  18349. (move-marker org-agenda-restrict-end (region-end)))
  18350. ((eq restriction 'subtree)
  18351. (save-excursion
  18352. (setq org-agenda-restrict t)
  18353. (org-back-to-heading t)
  18354. (move-marker org-agenda-restrict-begin (point))
  18355. (move-marker org-agenda-restrict-end
  18356. (progn (org-end-of-subtree t)))))))
  18357. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18358. ;; For example the todo list should not need it (but does...)
  18359. (cond
  18360. ((setq entry (assoc keys org-agenda-custom-commands))
  18361. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18362. (progn
  18363. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18364. (put 'org-agenda-redo-command 'org-lprops lprops)
  18365. (cond
  18366. ((eq type 'agenda)
  18367. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18368. ((eq type 'alltodo)
  18369. (org-let lprops '(org-todo-list current-prefix-arg)))
  18370. ((eq type 'search)
  18371. (org-let lprops '(org-search-view current-prefix-arg match)))
  18372. ((eq type 'stuck)
  18373. (org-let lprops '(org-agenda-list-stuck-projects
  18374. current-prefix-arg)))
  18375. ((eq type 'tags)
  18376. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18377. ((eq type 'tags-todo)
  18378. (org-let lprops '(org-tags-view '(4) match)))
  18379. ((eq type 'todo)
  18380. (org-let lprops '(org-todo-list match)))
  18381. ((eq type 'tags-tree)
  18382. (org-check-for-org-mode)
  18383. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18384. ((eq type 'todo-tree)
  18385. (org-check-for-org-mode)
  18386. (org-let lprops
  18387. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18388. (regexp-quote match) "\\>"))))
  18389. ((eq type 'occur-tree)
  18390. (org-check-for-org-mode)
  18391. (org-let lprops '(org-occur match)))
  18392. ((functionp type)
  18393. (org-let lprops '(funcall type match)))
  18394. ((fboundp type)
  18395. (org-let lprops '(funcall type match)))
  18396. (t (error "Invalid custom agenda command type %s" type))))
  18397. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18398. ((equal keys "C")
  18399. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18400. (customize-variable 'org-agenda-custom-commands))
  18401. ((equal keys "a") (call-interactively 'org-agenda-list))
  18402. ((equal keys "s") (call-interactively 'org-search-view))
  18403. ((equal keys "t") (call-interactively 'org-todo-list))
  18404. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18405. ((equal keys "m") (call-interactively 'org-tags-view))
  18406. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18407. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18408. ((equal keys "L")
  18409. (unless (org-mode-p)
  18410. (error "This is not an Org-mode file"))
  18411. (unless restriction
  18412. (put 'org-agenda-files 'org-restrict (list bfn))
  18413. (org-call-with-arg 'org-timeline arg)))
  18414. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18415. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18416. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18417. (t (error "Invalid agenda key"))))))
  18418. (defun org-agenda-normalize-custom-commands (cmds)
  18419. (delq nil
  18420. (mapcar
  18421. (lambda (x)
  18422. (cond ((stringp (cdr x)) nil)
  18423. ((stringp (nth 1 x)) x)
  18424. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18425. (t (cons (car x) (cons "" (cdr x))))))
  18426. cmds)))
  18427. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18428. "The user interface for selecting an agenda command."
  18429. (catch 'exit
  18430. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18431. (restrict-ok (and bfn (org-mode-p)))
  18432. (region-p (org-region-active-p))
  18433. (custom org-agenda-custom-commands)
  18434. (selstring "")
  18435. restriction second-time
  18436. c entry key type match prefixes rmheader header-end custom1 desc)
  18437. (save-window-excursion
  18438. (delete-other-windows)
  18439. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18440. (erase-buffer)
  18441. (insert (eval-when-compile
  18442. (let ((header
  18443. "
  18444. Press key for an agenda command: < Buffer,subtree/region restriction
  18445. -------------------------------- > Remove restriction
  18446. a Agenda for current week or day e Export agenda views
  18447. t List of all TODO entries T Entries with special TODO kwd
  18448. m Match a TAGS query M Like m, but only TODO entries
  18449. L Timeline for current buffer # List stuck projects (!=configure)
  18450. s Search for keywords C Configure custom agenda commands
  18451. / Multi-occur
  18452. ")
  18453. (start 0))
  18454. (while (string-match
  18455. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18456. header start)
  18457. (setq start (match-end 0))
  18458. (add-text-properties (match-beginning 2) (match-end 2)
  18459. '(face bold) header))
  18460. header)))
  18461. (setq header-end (move-marker (make-marker) (point)))
  18462. (while t
  18463. (setq custom1 custom)
  18464. (when (eq rmheader t)
  18465. (goto-line 1)
  18466. (re-search-forward ":" nil t)
  18467. (delete-region (match-end 0) (point-at-eol))
  18468. (forward-char 1)
  18469. (looking-at "-+")
  18470. (delete-region (match-end 0) (point-at-eol))
  18471. (move-marker header-end (match-end 0)))
  18472. (goto-char header-end)
  18473. (delete-region (point) (point-max))
  18474. (while (setq entry (pop custom1))
  18475. (setq key (car entry) desc (nth 1 entry)
  18476. type (nth 2 entry) match (nth 3 entry))
  18477. (if (> (length key) 1)
  18478. (add-to-list 'prefixes (string-to-char key))
  18479. (insert
  18480. (format
  18481. "\n%-4s%-14s: %s"
  18482. (org-add-props (copy-sequence key)
  18483. '(face bold))
  18484. (cond
  18485. ((string-match "\\S-" desc) desc)
  18486. ((eq type 'agenda) "Agenda for current week or day")
  18487. ((eq type 'alltodo) "List of all TODO entries")
  18488. ((eq type 'search) "Word search")
  18489. ((eq type 'stuck) "List of stuck projects")
  18490. ((eq type 'todo) "TODO keyword")
  18491. ((eq type 'tags) "Tags query")
  18492. ((eq type 'tags-todo) "Tags (TODO)")
  18493. ((eq type 'tags-tree) "Tags tree")
  18494. ((eq type 'todo-tree) "TODO kwd tree")
  18495. ((eq type 'occur-tree) "Occur tree")
  18496. ((functionp type) (if (symbolp type)
  18497. (symbol-name type)
  18498. "Lambda expression"))
  18499. (t "???"))
  18500. (cond
  18501. ((stringp match)
  18502. (org-add-props match nil 'face 'org-warning))
  18503. (match
  18504. (format "set of %d commands" (length match)))
  18505. (t ""))))))
  18506. (when prefixes
  18507. (mapc (lambda (x)
  18508. (insert
  18509. (format "\n%s %s"
  18510. (org-add-props (char-to-string x)
  18511. nil 'face 'bold)
  18512. (or (cdr (assoc (concat selstring (char-to-string x))
  18513. prefix-descriptions))
  18514. "Prefix key"))))
  18515. prefixes))
  18516. (goto-char (point-min))
  18517. (when (fboundp 'fit-window-to-buffer)
  18518. (if second-time
  18519. (if (not (pos-visible-in-window-p (point-max)))
  18520. (fit-window-to-buffer))
  18521. (setq second-time t)
  18522. (fit-window-to-buffer)))
  18523. (message "Press key for agenda command%s:"
  18524. (if (or restrict-ok org-agenda-overriding-restriction)
  18525. (if org-agenda-overriding-restriction
  18526. " (restriction lock active)"
  18527. (if restriction
  18528. (format " (restricted to %s)" restriction)
  18529. " (unrestricted)"))
  18530. ""))
  18531. (setq c (read-char-exclusive))
  18532. (message "")
  18533. (cond
  18534. ((assoc (char-to-string c) custom)
  18535. (setq selstring (concat selstring (char-to-string c)))
  18536. (throw 'exit (cons selstring restriction)))
  18537. ((memq c prefixes)
  18538. (setq selstring (concat selstring (char-to-string c))
  18539. prefixes nil
  18540. rmheader (or rmheader t)
  18541. custom (delq nil (mapcar
  18542. (lambda (x)
  18543. (if (or (= (length (car x)) 1)
  18544. (/= (string-to-char (car x)) c))
  18545. nil
  18546. (cons (substring (car x) 1) (cdr x))))
  18547. custom))))
  18548. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18549. (message "Restriction is only possible in Org-mode buffers")
  18550. (ding) (sit-for 1))
  18551. ((eq c ?1)
  18552. (org-agenda-remove-restriction-lock 'noupdate)
  18553. (setq restriction 'buffer))
  18554. ((eq c ?0)
  18555. (org-agenda-remove-restriction-lock 'noupdate)
  18556. (setq restriction (if region-p 'region 'subtree)))
  18557. ((eq c ?<)
  18558. (org-agenda-remove-restriction-lock 'noupdate)
  18559. (setq restriction
  18560. (cond
  18561. ((eq restriction 'buffer)
  18562. (if region-p 'region 'subtree))
  18563. ((memq restriction '(subtree region))
  18564. nil)
  18565. (t 'buffer))))
  18566. ((eq c ?>)
  18567. (org-agenda-remove-restriction-lock 'noupdate)
  18568. (setq restriction nil))
  18569. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18570. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18571. ((and (> (length selstring) 0) (eq c ?\d))
  18572. (delete-window)
  18573. (org-agenda-get-restriction-and-command prefix-descriptions))
  18574. ((equal c ?q) (error "Abort"))
  18575. (t (error "Invalid key %c" c))))))))
  18576. (defun org-run-agenda-series (name series)
  18577. (org-prepare-agenda name)
  18578. (let* ((org-agenda-multi t)
  18579. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18580. (cmds (car series))
  18581. (gprops (nth 1 series))
  18582. match ;; The byte compiler incorrectly complains about this. Keep it!
  18583. cmd type lprops)
  18584. (while (setq cmd (pop cmds))
  18585. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18586. (cond
  18587. ((eq type 'agenda)
  18588. (org-let2 gprops lprops
  18589. '(call-interactively 'org-agenda-list)))
  18590. ((eq type 'alltodo)
  18591. (org-let2 gprops lprops
  18592. '(call-interactively 'org-todo-list)))
  18593. ((eq type 'search)
  18594. (org-let2 gprops lprops
  18595. '(org-search-view current-prefix-arg match)))
  18596. ((eq type 'stuck)
  18597. (org-let2 gprops lprops
  18598. '(call-interactively 'org-agenda-list-stuck-projects)))
  18599. ((eq type 'tags)
  18600. (org-let2 gprops lprops
  18601. '(org-tags-view current-prefix-arg match)))
  18602. ((eq type 'tags-todo)
  18603. (org-let2 gprops lprops
  18604. '(org-tags-view '(4) match)))
  18605. ((eq type 'todo)
  18606. (org-let2 gprops lprops
  18607. '(org-todo-list match)))
  18608. ((fboundp type)
  18609. (org-let2 gprops lprops
  18610. '(funcall type match)))
  18611. (t (error "Invalid type in command series"))))
  18612. (widen)
  18613. (setq org-agenda-redo-command redo)
  18614. (goto-char (point-min)))
  18615. (org-finalize-agenda))
  18616. ;;;###autoload
  18617. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18618. "Run an agenda command in batch mode and send the result to STDOUT.
  18619. If CMD-KEY is a string of length 1, it is used as a key in
  18620. `org-agenda-custom-commands' and triggers this command. If it is a
  18621. longer string it is used as a tags/todo match string.
  18622. Paramters are alternating variable names and values that will be bound
  18623. before running the agenda command."
  18624. (let (pars)
  18625. (while parameters
  18626. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18627. (if (> (length cmd-key) 2)
  18628. (eval (list 'let (nreverse pars)
  18629. (list 'org-tags-view nil cmd-key)))
  18630. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18631. (set-buffer org-agenda-buffer-name)
  18632. (princ (org-encode-for-stdout (buffer-string)))))
  18633. (defun org-encode-for-stdout (string)
  18634. (if (fboundp 'encode-coding-string)
  18635. (encode-coding-string string buffer-file-coding-system)
  18636. string))
  18637. (defvar org-agenda-info nil)
  18638. ;;;###autoload
  18639. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18640. "Run an agenda command in batch mode and send the result to STDOUT.
  18641. If CMD-KEY is a string of length 1, it is used as a key in
  18642. `org-agenda-custom-commands' and triggers this command. If it is a
  18643. longer string it is used as a tags/todo match string.
  18644. Paramters are alternating variable names and values that will be bound
  18645. before running the agenda command.
  18646. The output gives a line for each selected agenda item. Each
  18647. item is a list of comma-separated values, like this:
  18648. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18649. category The category of the item
  18650. head The headline, without TODO kwd, TAGS and PRIORITY
  18651. type The type of the agenda entry, can be
  18652. todo selected in TODO match
  18653. tagsmatch selected in tags match
  18654. diary imported from diary
  18655. deadline a deadline on given date
  18656. scheduled scheduled on given date
  18657. timestamp entry has timestamp on given date
  18658. closed entry was closed on given date
  18659. upcoming-deadline warning about deadline
  18660. past-scheduled forwarded scheduled item
  18661. block entry has date block including g. date
  18662. todo The todo keyword, if any
  18663. tags All tags including inherited ones, separated by colons
  18664. date The relevant date, like 2007-2-14
  18665. time The time, like 15:00-16:50
  18666. extra Sting with extra planning info
  18667. priority-l The priority letter if any was given
  18668. priority-n The computed numerical priority
  18669. agenda-day The day in the agenda where this is listed"
  18670. (let (pars)
  18671. (while parameters
  18672. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18673. (push (list 'org-agenda-remove-tags t) pars)
  18674. (if (> (length cmd-key) 2)
  18675. (eval (list 'let (nreverse pars)
  18676. (list 'org-tags-view nil cmd-key)))
  18677. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18678. (set-buffer org-agenda-buffer-name)
  18679. (let* ((lines (org-split-string (buffer-string) "\n"))
  18680. line)
  18681. (while (setq line (pop lines))
  18682. (catch 'next
  18683. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18684. (setq org-agenda-info
  18685. (org-fix-agenda-info (text-properties-at 0 line)))
  18686. (princ
  18687. (org-encode-for-stdout
  18688. (mapconcat 'org-agenda-export-csv-mapper
  18689. '(org-category txt type todo tags date time-of-day extra
  18690. priority-letter priority agenda-day)
  18691. ",")))
  18692. (princ "\n"))))))
  18693. (defun org-fix-agenda-info (props)
  18694. "Make sure all properties on an agenda item have a canonical form,
  18695. so the export commands can easily use it."
  18696. (let (tmp re)
  18697. (when (setq tmp (plist-get props 'tags))
  18698. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18699. (when (setq tmp (plist-get props 'date))
  18700. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18701. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18702. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18703. (setq tmp (calendar-date-string tmp)))
  18704. (setq props (plist-put props 'date tmp)))
  18705. (when (setq tmp (plist-get props 'day))
  18706. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18707. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18708. (setq tmp (calendar-date-string tmp)))
  18709. (setq props (plist-put props 'day tmp))
  18710. (setq props (plist-put props 'agenda-day tmp)))
  18711. (when (setq tmp (plist-get props 'txt))
  18712. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18713. (plist-put props 'priority-letter (match-string 1 tmp))
  18714. (setq tmp (replace-match "" t t tmp)))
  18715. (when (and (setq re (plist-get props 'org-todo-regexp))
  18716. (setq re (concat "\\`\\.*" re " ?"))
  18717. (string-match re tmp))
  18718. (plist-put props 'todo (match-string 1 tmp))
  18719. (setq tmp (replace-match "" t t tmp)))
  18720. (plist-put props 'txt tmp)))
  18721. props)
  18722. (defun org-agenda-export-csv-mapper (prop)
  18723. (let ((res (plist-get org-agenda-info prop)))
  18724. (setq res
  18725. (cond
  18726. ((not res) "")
  18727. ((stringp res) res)
  18728. (t (prin1-to-string res))))
  18729. (while (string-match "," res)
  18730. (setq res (replace-match ";" t t res)))
  18731. (org-trim res)))
  18732. ;;;###autoload
  18733. (defun org-store-agenda-views (&rest parameters)
  18734. (interactive)
  18735. (eval (list 'org-batch-store-agenda-views)))
  18736. ;; FIXME, why is this a macro?????
  18737. ;;;###autoload
  18738. (defmacro org-batch-store-agenda-views (&rest parameters)
  18739. "Run all custom agenda commands that have a file argument."
  18740. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18741. (pop-up-frames nil)
  18742. (dir default-directory)
  18743. pars cmd thiscmdkey files opts)
  18744. (while parameters
  18745. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18746. (setq pars (reverse pars))
  18747. (save-window-excursion
  18748. (while cmds
  18749. (setq cmd (pop cmds)
  18750. thiscmdkey (car cmd)
  18751. opts (nth 4 cmd)
  18752. files (nth 5 cmd))
  18753. (if (stringp files) (setq files (list files)))
  18754. (when files
  18755. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18756. (list 'org-agenda nil thiscmdkey)))
  18757. (set-buffer org-agenda-buffer-name)
  18758. (while files
  18759. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18760. (list 'org-write-agenda
  18761. (expand-file-name (pop files) dir) t))))
  18762. (and (get-buffer org-agenda-buffer-name)
  18763. (kill-buffer org-agenda-buffer-name)))))))
  18764. (defun org-write-agenda (file &optional nosettings)
  18765. "Write the current buffer (an agenda view) as a file.
  18766. Depending on the extension of the file name, plain text (.txt),
  18767. HTML (.html or .htm) or Postscript (.ps) is produced.
  18768. If NOSETTINGS is given, do not scope the settings of
  18769. `org-agenda-exporter-settings' into the export commands. This is used when
  18770. the settings have already been scoped and we do not wish to overrule other,
  18771. higher priority settings."
  18772. (interactive "FWrite agenda to file: ")
  18773. (if (not (file-writable-p file))
  18774. (error "Cannot write agenda to file %s" file))
  18775. (cond
  18776. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18777. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18778. (org-let (if nosettings nil org-agenda-exporter-settings)
  18779. '(save-excursion
  18780. (save-window-excursion
  18781. (cond
  18782. ((string-match "\\.html?\\'" file)
  18783. (set-buffer (htmlize-buffer (current-buffer)))
  18784. (when (and org-agenda-export-html-style
  18785. (string-match "<style>" org-agenda-export-html-style))
  18786. ;; replace <style> section with org-agenda-export-html-style
  18787. (goto-char (point-min))
  18788. (kill-region (- (search-forward "<style") 6)
  18789. (search-forward "</style>"))
  18790. (insert org-agenda-export-html-style))
  18791. (write-file file)
  18792. (kill-buffer (current-buffer))
  18793. (message "HTML written to %s" file))
  18794. ((string-match "\\.ps\\'" file)
  18795. (ps-print-buffer-with-faces file)
  18796. (message "Postscript written to %s" file))
  18797. (t
  18798. (let ((bs (buffer-string)))
  18799. (find-file file)
  18800. (insert bs)
  18801. (save-buffer 0)
  18802. (kill-buffer (current-buffer))
  18803. (message "Plain text written to %s" file))))))
  18804. (set-buffer org-agenda-buffer-name)))
  18805. (defmacro org-no-read-only (&rest body)
  18806. "Inhibit read-only for BODY."
  18807. `(let ((inhibit-read-only t)) ,@body))
  18808. (defun org-check-for-org-mode ()
  18809. "Make sure current buffer is in org-mode. Error if not."
  18810. (or (org-mode-p)
  18811. (error "Cannot execute org-mode agenda command on buffer in %s."
  18812. major-mode)))
  18813. (defun org-fit-agenda-window ()
  18814. "Fit the window to the buffer size."
  18815. (and (memq org-agenda-window-setup '(reorganize-frame))
  18816. (fboundp 'fit-window-to-buffer)
  18817. (fit-window-to-buffer
  18818. nil
  18819. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18820. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18821. ;;; Agenda file list
  18822. (defun org-agenda-files (&optional unrestricted)
  18823. "Get the list of agenda files.
  18824. Optional UNRESTRICTED means return the full list even if a restriction
  18825. is currently in place."
  18826. (let ((files
  18827. (cond
  18828. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18829. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18830. ((listp org-agenda-files) org-agenda-files)
  18831. (t (error "Invalid value of `org-agenda-files'")))))
  18832. (setq files (apply 'append
  18833. (mapcar (lambda (f)
  18834. (if (file-directory-p f)
  18835. (directory-files f t
  18836. org-agenda-file-regexp)
  18837. (list f)))
  18838. files)))
  18839. (if org-agenda-skip-unavailable-files
  18840. (delq nil
  18841. (mapcar (function
  18842. (lambda (file)
  18843. (and (file-readable-p file) file)))
  18844. files))
  18845. files))) ; `org-check-agenda-file' will remove them from the list
  18846. (defun org-edit-agenda-file-list ()
  18847. "Edit the list of agenda files.
  18848. Depending on setup, this either uses customize to edit the variable
  18849. `org-agenda-files', or it visits the file that is holding the list. In the
  18850. latter case, the buffer is set up in a way that saving it automatically kills
  18851. the buffer and restores the previous window configuration."
  18852. (interactive)
  18853. (if (stringp org-agenda-files)
  18854. (let ((cw (current-window-configuration)))
  18855. (find-file org-agenda-files)
  18856. (org-set-local 'org-window-configuration cw)
  18857. (org-add-hook 'after-save-hook
  18858. (lambda ()
  18859. (set-window-configuration
  18860. (prog1 org-window-configuration
  18861. (kill-buffer (current-buffer))))
  18862. (org-install-agenda-files-menu)
  18863. (message "New agenda file list installed"))
  18864. nil 'local)
  18865. (message "%s" (substitute-command-keys
  18866. "Edit list and finish with \\[save-buffer]")))
  18867. (customize-variable 'org-agenda-files)))
  18868. (defun org-store-new-agenda-file-list (list)
  18869. "Set new value for the agenda file list and save it correcly."
  18870. (if (stringp org-agenda-files)
  18871. (let ((f org-agenda-files) b)
  18872. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18873. (with-temp-file f
  18874. (insert (mapconcat 'identity list "\n") "\n")))
  18875. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18876. (setq org-agenda-files list)
  18877. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18878. (defun org-read-agenda-file-list ()
  18879. "Read the list of agenda files from a file."
  18880. (when (stringp org-agenda-files)
  18881. (with-temp-buffer
  18882. (insert-file-contents org-agenda-files)
  18883. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18884. ;;;###autoload
  18885. (defun org-cycle-agenda-files ()
  18886. "Cycle through the files in `org-agenda-files'.
  18887. If the current buffer visits an agenda file, find the next one in the list.
  18888. If the current buffer does not, find the first agenda file."
  18889. (interactive)
  18890. (let* ((fs (org-agenda-files t))
  18891. (files (append fs (list (car fs))))
  18892. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18893. file)
  18894. (unless files (error "No agenda files"))
  18895. (catch 'exit
  18896. (while (setq file (pop files))
  18897. (if (equal (file-truename file) tcf)
  18898. (when (car files)
  18899. (find-file (car files))
  18900. (throw 'exit t))))
  18901. (find-file (car fs)))
  18902. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18903. (defun org-agenda-file-to-front (&optional to-end)
  18904. "Move/add the current file to the top of the agenda file list.
  18905. If the file is not present in the list, it is added to the front. If it is
  18906. present, it is moved there. With optional argument TO-END, add/move to the
  18907. end of the list."
  18908. (interactive "P")
  18909. (let ((org-agenda-skip-unavailable-files nil)
  18910. (file-alist (mapcar (lambda (x)
  18911. (cons (file-truename x) x))
  18912. (org-agenda-files t)))
  18913. (ctf (file-truename buffer-file-name))
  18914. x had)
  18915. (setq x (assoc ctf file-alist) had x)
  18916. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18917. (if to-end
  18918. (setq file-alist (append (delq x file-alist) (list x)))
  18919. (setq file-alist (cons x (delq x file-alist))))
  18920. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18921. (org-install-agenda-files-menu)
  18922. (message "File %s to %s of agenda file list"
  18923. (if had "moved" "added") (if to-end "end" "front"))))
  18924. (defun org-remove-file (&optional file)
  18925. "Remove current file from the list of files in variable `org-agenda-files'.
  18926. These are the files which are being checked for agenda entries.
  18927. Optional argument FILE means, use this file instead of the current."
  18928. (interactive)
  18929. (let* ((org-agenda-skip-unavailable-files nil)
  18930. (file (or file buffer-file-name))
  18931. (true-file (file-truename file))
  18932. (afile (abbreviate-file-name file))
  18933. (files (delq nil (mapcar
  18934. (lambda (x)
  18935. (if (equal true-file
  18936. (file-truename x))
  18937. nil x))
  18938. (org-agenda-files t)))))
  18939. (if (not (= (length files) (length (org-agenda-files t))))
  18940. (progn
  18941. (org-store-new-agenda-file-list files)
  18942. (org-install-agenda-files-menu)
  18943. (message "Removed file: %s" afile))
  18944. (message "File was not in list: %s (not removed)" afile))))
  18945. (defun org-file-menu-entry (file)
  18946. (vector file (list 'find-file file) t))
  18947. (defun org-check-agenda-file (file)
  18948. "Make sure FILE exists. If not, ask user what to do."
  18949. (when (not (file-exists-p file))
  18950. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18951. (abbreviate-file-name file))
  18952. (let ((r (downcase (read-char-exclusive))))
  18953. (cond
  18954. ((equal r ?r)
  18955. (org-remove-file file)
  18956. (throw 'nextfile t))
  18957. (t (error "Abort"))))))
  18958. ;;; Agenda prepare and finalize
  18959. (defvar org-agenda-multi nil) ; dynammically scoped
  18960. (defvar org-agenda-buffer-name "*Org Agenda*")
  18961. (defvar org-pre-agenda-window-conf nil)
  18962. (defvar org-agenda-name nil)
  18963. (defun org-prepare-agenda (&optional name)
  18964. (setq org-todo-keywords-for-agenda nil)
  18965. (setq org-done-keywords-for-agenda nil)
  18966. (if org-agenda-multi
  18967. (progn
  18968. (setq buffer-read-only nil)
  18969. (goto-char (point-max))
  18970. (unless (or (bobp) org-agenda-compact-blocks)
  18971. (insert "\n" (make-string (window-width) ?=) "\n"))
  18972. (narrow-to-region (point) (point-max)))
  18973. (org-agenda-reset-markers)
  18974. (org-prepare-agenda-buffers (org-agenda-files))
  18975. (setq org-todo-keywords-for-agenda
  18976. (org-uniquify org-todo-keywords-for-agenda))
  18977. (setq org-done-keywords-for-agenda
  18978. (org-uniquify org-done-keywords-for-agenda))
  18979. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18980. (awin (get-buffer-window abuf)))
  18981. (cond
  18982. ((equal (current-buffer) abuf) nil)
  18983. (awin (select-window awin))
  18984. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18985. ((equal org-agenda-window-setup 'current-window)
  18986. (switch-to-buffer abuf))
  18987. ((equal org-agenda-window-setup 'other-window)
  18988. (org-switch-to-buffer-other-window abuf))
  18989. ((equal org-agenda-window-setup 'other-frame)
  18990. (switch-to-buffer-other-frame abuf))
  18991. ((equal org-agenda-window-setup 'reorganize-frame)
  18992. (delete-other-windows)
  18993. (org-switch-to-buffer-other-window abuf))))
  18994. (setq buffer-read-only nil)
  18995. (erase-buffer)
  18996. (org-agenda-mode)
  18997. (and name (not org-agenda-name)
  18998. (org-set-local 'org-agenda-name name)))
  18999. (setq buffer-read-only nil))
  19000. (defun org-finalize-agenda ()
  19001. "Finishing touch for the agenda buffer, called just before displaying it."
  19002. (unless org-agenda-multi
  19003. (save-excursion
  19004. (let ((inhibit-read-only t))
  19005. (goto-char (point-min))
  19006. (while (org-activate-bracket-links (point-max))
  19007. (add-text-properties (match-beginning 0) (match-end 0)
  19008. '(face org-link)))
  19009. (org-agenda-align-tags)
  19010. (unless org-agenda-with-colors
  19011. (remove-text-properties (point-min) (point-max) '(face nil))))
  19012. (if (and (boundp 'org-overriding-columns-format)
  19013. org-overriding-columns-format)
  19014. (org-set-local 'org-overriding-columns-format
  19015. org-overriding-columns-format))
  19016. (if (and (boundp 'org-agenda-view-columns-initially)
  19017. org-agenda-view-columns-initially)
  19018. (org-agenda-columns))
  19019. (when org-agenda-fontify-priorities
  19020. (org-fontify-priorities))
  19021. (run-hooks 'org-finalize-agenda-hook)
  19022. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  19023. )))
  19024. (defun org-fontify-priorities ()
  19025. "Make highest priority lines bold, and lowest italic."
  19026. (interactive)
  19027. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  19028. (org-delete-overlay o)))
  19029. (org-overlays-in (point-min) (point-max)))
  19030. (save-excursion
  19031. (let ((inhibit-read-only t)
  19032. b e p ov h l)
  19033. (goto-char (point-min))
  19034. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  19035. (setq h (or (get-char-property (point) 'org-highest-priority)
  19036. org-highest-priority)
  19037. l (or (get-char-property (point) 'org-lowest-priority)
  19038. org-lowest-priority)
  19039. p (string-to-char (match-string 1))
  19040. b (match-beginning 0) e (point-at-eol)
  19041. ov (org-make-overlay b e))
  19042. (org-overlay-put
  19043. ov 'face
  19044. (cond ((listp org-agenda-fontify-priorities)
  19045. (cdr (assoc p org-agenda-fontify-priorities)))
  19046. ((equal p l) 'italic)
  19047. ((equal p h) 'bold)))
  19048. (org-overlay-put ov 'org-type 'org-priority)))))
  19049. (defun org-prepare-agenda-buffers (files)
  19050. "Create buffers for all agenda files, protect archived trees and comments."
  19051. (interactive)
  19052. (let ((pa '(:org-archived t))
  19053. (pc '(:org-comment t))
  19054. (pall '(:org-archived t :org-comment t))
  19055. (inhibit-read-only t)
  19056. (rea (concat ":" org-archive-tag ":"))
  19057. bmp file re)
  19058. (save-excursion
  19059. (save-restriction
  19060. (while (setq file (pop files))
  19061. (if (bufferp file)
  19062. (set-buffer file)
  19063. (org-check-agenda-file file)
  19064. (set-buffer (org-get-agenda-file-buffer file)))
  19065. (widen)
  19066. (setq bmp (buffer-modified-p))
  19067. (org-refresh-category-properties)
  19068. (setq org-todo-keywords-for-agenda
  19069. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  19070. (setq org-done-keywords-for-agenda
  19071. (append org-done-keywords-for-agenda org-done-keywords))
  19072. (save-excursion
  19073. (remove-text-properties (point-min) (point-max) pall)
  19074. (when org-agenda-skip-archived-trees
  19075. (goto-char (point-min))
  19076. (while (re-search-forward rea nil t)
  19077. (if (org-on-heading-p t)
  19078. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  19079. (goto-char (point-min))
  19080. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  19081. (while (re-search-forward re nil t)
  19082. (add-text-properties
  19083. (match-beginning 0) (org-end-of-subtree t) pc)))
  19084. (set-buffer-modified-p bmp))))))
  19085. (defvar org-agenda-skip-function nil
  19086. "Function to be called at each match during agenda construction.
  19087. If this function returns nil, the current match should not be skipped.
  19088. Otherwise, the function must return a position from where the search
  19089. should be continued.
  19090. This may also be a Lisp form, it will be evaluated.
  19091. Never set this variable using `setq' or so, because then it will apply
  19092. to all future agenda commands. Instead, bind it with `let' to scope
  19093. it dynamically into the agenda-constructing command. A good way to set
  19094. it is through options in org-agenda-custom-commands.")
  19095. (defun org-agenda-skip ()
  19096. "Throw to `:skip' in places that should be skipped.
  19097. Also moves point to the end of the skipped region, so that search can
  19098. continue from there."
  19099. (let ((p (point-at-bol)) to fp)
  19100. (and org-agenda-skip-archived-trees
  19101. (get-text-property p :org-archived)
  19102. (org-end-of-subtree t)
  19103. (throw :skip t))
  19104. (and (get-text-property p :org-comment)
  19105. (org-end-of-subtree t)
  19106. (throw :skip t))
  19107. (if (equal (char-after p) ?#) (throw :skip t))
  19108. (when (and (or (setq fp (functionp org-agenda-skip-function))
  19109. (consp org-agenda-skip-function))
  19110. (setq to (save-excursion
  19111. (save-match-data
  19112. (if fp
  19113. (funcall org-agenda-skip-function)
  19114. (eval org-agenda-skip-function))))))
  19115. (goto-char to)
  19116. (throw :skip t))))
  19117. (defvar org-agenda-markers nil
  19118. "List of all currently active markers created by `org-agenda'.")
  19119. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  19120. "Creation time of the last agenda marker.")
  19121. (defun org-agenda-new-marker (&optional pos)
  19122. "Return a new agenda marker.
  19123. Org-mode keeps a list of these markers and resets them when they are
  19124. no longer in use."
  19125. (let ((m (copy-marker (or pos (point)))))
  19126. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  19127. (push m org-agenda-markers)
  19128. m))
  19129. (defun org-agenda-reset-markers ()
  19130. "Reset markers created by `org-agenda'."
  19131. (while org-agenda-markers
  19132. (move-marker (pop org-agenda-markers) nil)))
  19133. (defun org-get-agenda-file-buffer (file)
  19134. "Get a buffer visiting FILE. If the buffer needs to be created, add
  19135. it to the list of buffers which might be released later."
  19136. (let ((buf (org-find-base-buffer-visiting file)))
  19137. (if buf
  19138. buf ; just return it
  19139. ;; Make a new buffer and remember it
  19140. (setq buf (find-file-noselect file))
  19141. (if buf (push buf org-agenda-new-buffers))
  19142. buf)))
  19143. (defun org-release-buffers (blist)
  19144. "Release all buffers in list, asking the user for confirmation when needed.
  19145. When a buffer is unmodified, it is just killed. When modified, it is saved
  19146. \(if the user agrees) and then killed."
  19147. (let (buf file)
  19148. (while (setq buf (pop blist))
  19149. (setq file (buffer-file-name buf))
  19150. (when (and (buffer-modified-p buf)
  19151. file
  19152. (y-or-n-p (format "Save file %s? " file)))
  19153. (with-current-buffer buf (save-buffer)))
  19154. (kill-buffer buf))))
  19155. (defun org-get-category (&optional pos)
  19156. "Get the category applying to position POS."
  19157. (get-text-property (or pos (point)) 'org-category))
  19158. ;;; Agenda timeline
  19159. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  19160. (defun org-timeline (&optional include-all)
  19161. "Show a time-sorted view of the entries in the current org file.
  19162. Only entries with a time stamp of today or later will be listed. With
  19163. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  19164. under the current date.
  19165. If the buffer contains an active region, only check the region for
  19166. dates."
  19167. (interactive "P")
  19168. (require 'calendar)
  19169. (org-compile-prefix-format 'timeline)
  19170. (org-set-sorting-strategy 'timeline)
  19171. (let* ((dopast t)
  19172. (dotodo include-all)
  19173. (doclosed org-agenda-show-log)
  19174. (entry buffer-file-name)
  19175. (date (calendar-current-date))
  19176. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  19177. (end (if (org-region-active-p) (region-end) (point-max)))
  19178. (day-numbers (org-get-all-dates beg end 'no-ranges
  19179. t doclosed ; always include today
  19180. org-timeline-show-empty-dates))
  19181. (org-deadline-warning-days 0)
  19182. (org-agenda-only-exact-dates t)
  19183. (today (time-to-days (current-time)))
  19184. (past t)
  19185. args
  19186. s e rtn d emptyp)
  19187. (setq org-agenda-redo-command
  19188. (list 'progn
  19189. (list 'org-switch-to-buffer-other-window (current-buffer))
  19190. (list 'org-timeline (list 'quote include-all))))
  19191. (if (not dopast)
  19192. ;; Remove past dates from the list of dates.
  19193. (setq day-numbers (delq nil (mapcar (lambda(x)
  19194. (if (>= x today) x nil))
  19195. day-numbers))))
  19196. (org-prepare-agenda (concat "Timeline "
  19197. (file-name-nondirectory buffer-file-name)))
  19198. (if doclosed (push :closed args))
  19199. (push :timestamp args)
  19200. (push :deadline args)
  19201. (push :scheduled args)
  19202. (push :sexp args)
  19203. (if dotodo (push :todo args))
  19204. (while (setq d (pop day-numbers))
  19205. (if (and (listp d) (eq (car d) :omitted))
  19206. (progn
  19207. (setq s (point))
  19208. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19209. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19210. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19211. (if (and (>= d today)
  19212. dopast
  19213. past)
  19214. (progn
  19215. (setq past nil)
  19216. (insert (make-string 79 ?-) "\n")))
  19217. (setq date (calendar-gregorian-from-absolute d))
  19218. (setq s (point))
  19219. (setq rtn (and (not emptyp)
  19220. (apply 'org-agenda-get-day-entries entry
  19221. date args)))
  19222. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19223. (progn
  19224. (insert
  19225. (if (stringp org-agenda-format-date)
  19226. (format-time-string org-agenda-format-date
  19227. (org-time-from-absolute date))
  19228. (funcall org-agenda-format-date date))
  19229. "\n")
  19230. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19231. (put-text-property s (1- (point)) 'org-date-line t)
  19232. (if (equal d today)
  19233. (put-text-property s (1- (point)) 'org-today t))
  19234. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19235. (put-text-property s (1- (point)) 'day d)))))
  19236. (goto-char (point-min))
  19237. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19238. (point-min)))
  19239. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19240. (org-finalize-agenda)
  19241. (setq buffer-read-only t)))
  19242. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19243. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19244. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19245. not every single day in the range. If FORCE-TODAY is non-nil, make
  19246. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19247. inactive time stamps (those in square brackets) are included.
  19248. When EMPTY is non-nil, also include days without any entries."
  19249. (let ((re (concat
  19250. (if pre-re pre-re "")
  19251. (if inactive org-ts-regexp-both org-ts-regexp)))
  19252. dates dates1 date day day1 day2 ts1 ts2)
  19253. (if force-today
  19254. (setq dates (list (time-to-days (current-time)))))
  19255. (save-excursion
  19256. (goto-char beg)
  19257. (while (re-search-forward re end t)
  19258. (setq day (time-to-days (org-time-string-to-time
  19259. (substring (match-string 1) 0 10))))
  19260. (or (memq day dates) (push day dates)))
  19261. (unless no-ranges
  19262. (goto-char beg)
  19263. (while (re-search-forward org-tr-regexp end t)
  19264. (setq ts1 (substring (match-string 1) 0 10)
  19265. ts2 (substring (match-string 2) 0 10)
  19266. day1 (time-to-days (org-time-string-to-time ts1))
  19267. day2 (time-to-days (org-time-string-to-time ts2)))
  19268. (while (< (setq day1 (1+ day1)) day2)
  19269. (or (memq day1 dates) (push day1 dates)))))
  19270. (setq dates (sort dates '<))
  19271. (when empty
  19272. (while (setq day (pop dates))
  19273. (setq day2 (car dates))
  19274. (push day dates1)
  19275. (when (and day2 empty)
  19276. (if (or (eq empty t)
  19277. (and (numberp empty) (<= (- day2 day) empty)))
  19278. (while (< (setq day (1+ day)) day2)
  19279. (push (list day) dates1))
  19280. (push (cons :omitted (- day2 day)) dates1))))
  19281. (setq dates (nreverse dates1)))
  19282. dates)))
  19283. ;;; Agenda Daily/Weekly
  19284. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19285. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19286. (defvar org-agenda-last-arguments nil
  19287. "The arguments of the previous call to org-agenda")
  19288. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19289. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19290. (defvar org-include-all-loc nil) ; local variable
  19291. (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
  19292. ;;;###autoload
  19293. (defun org-agenda-list (&optional include-all start-day ndays)
  19294. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19295. The view will be for the current day or week, but from the overview buffer
  19296. you will be able to go to other days/weeks.
  19297. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19298. all unfinished TODO items will also be shown, before the agenda.
  19299. This feature is considered obsolete, please use the TODO list or a block
  19300. agenda instead.
  19301. With a numeric prefix argument in an interactive call, the agenda will
  19302. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19303. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19304. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19305. given in `org-agenda-start-on-weekday'."
  19306. (interactive "P")
  19307. (if (and (integerp include-all) (> include-all 0))
  19308. (setq ndays include-all include-all nil))
  19309. (setq ndays (or ndays org-agenda-ndays)
  19310. start-day (or start-day org-agenda-start-day))
  19311. (if org-agenda-overriding-arguments
  19312. (setq include-all (car org-agenda-overriding-arguments)
  19313. start-day (nth 1 org-agenda-overriding-arguments)
  19314. ndays (nth 2 org-agenda-overriding-arguments)))
  19315. (if (stringp start-day)
  19316. ;; Convert to an absolute day number
  19317. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19318. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19319. (org-compile-prefix-format 'agenda)
  19320. (org-set-sorting-strategy 'agenda)
  19321. (require 'calendar)
  19322. (let* ((org-agenda-start-on-weekday
  19323. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19324. org-agenda-start-on-weekday nil))
  19325. (thefiles (org-agenda-files))
  19326. (files thefiles)
  19327. (today (time-to-days
  19328. (time-subtract (current-time)
  19329. (list 0 (* 3600 org-extend-today-until) 0))))
  19330. (sd (or start-day today))
  19331. (start (if (or (null org-agenda-start-on-weekday)
  19332. (< org-agenda-ndays 7))
  19333. sd
  19334. (let* ((nt (calendar-day-of-week
  19335. (calendar-gregorian-from-absolute sd)))
  19336. (n1 org-agenda-start-on-weekday)
  19337. (d (- nt n1)))
  19338. (- sd (+ (if (< d 0) 7 0) d)))))
  19339. (day-numbers (list start))
  19340. (day-cnt 0)
  19341. (inhibit-redisplay (not debug-on-error))
  19342. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19343. (setq org-agenda-redo-command
  19344. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19345. ;; Make the list of days
  19346. (setq ndays (or ndays org-agenda-ndays)
  19347. nd ndays)
  19348. (while (> ndays 1)
  19349. (push (1+ (car day-numbers)) day-numbers)
  19350. (setq ndays (1- ndays)))
  19351. (setq day-numbers (nreverse day-numbers))
  19352. (org-prepare-agenda "Day/Week")
  19353. (org-set-local 'org-starting-day (car day-numbers))
  19354. (org-set-local 'org-include-all-loc include-all)
  19355. (org-set-local 'org-agenda-span
  19356. (org-agenda-ndays-to-span nd))
  19357. (when (and (or include-all org-agenda-include-all-todo)
  19358. (member today day-numbers))
  19359. (setq files thefiles
  19360. rtnall nil)
  19361. (while (setq file (pop files))
  19362. (catch 'nextfile
  19363. (org-check-agenda-file file)
  19364. (setq date (calendar-gregorian-from-absolute today)
  19365. rtn (org-agenda-get-day-entries
  19366. file date :todo))
  19367. (setq rtnall (append rtnall rtn))))
  19368. (when rtnall
  19369. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19370. (add-text-properties (point-min) (1- (point))
  19371. (list 'face 'org-agenda-structure))
  19372. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19373. (unless org-agenda-compact-blocks
  19374. (setq s (point))
  19375. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19376. "-agenda:\n")
  19377. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19378. 'org-date-line t)))
  19379. (while (setq d (pop day-numbers))
  19380. (setq date (calendar-gregorian-from-absolute d)
  19381. s (point))
  19382. (if (or (setq todayp (= d today))
  19383. (and (not start-pos) (= d sd)))
  19384. (setq start-pos (point))
  19385. (if (and start-pos (not end-pos))
  19386. (setq end-pos (point))))
  19387. (setq files thefiles
  19388. rtnall nil)
  19389. (while (setq file (pop files))
  19390. (catch 'nextfile
  19391. (org-check-agenda-file file)
  19392. (if org-agenda-show-log
  19393. (setq rtn (org-agenda-get-day-entries
  19394. file date
  19395. :deadline :scheduled :timestamp :sexp :closed))
  19396. (setq rtn (org-agenda-get-day-entries
  19397. file date
  19398. :deadline :scheduled :sexp :timestamp)))
  19399. (setq rtnall (append rtnall rtn))))
  19400. (if org-agenda-include-diary
  19401. (progn
  19402. (require 'diary-lib)
  19403. (setq rtn (org-get-entries-from-diary date))
  19404. (setq rtnall (append rtnall rtn))))
  19405. (if (or rtnall org-agenda-show-all-dates)
  19406. (progn
  19407. (setq day-cnt (1+ day-cnt))
  19408. (insert
  19409. (if (stringp org-agenda-format-date)
  19410. (format-time-string org-agenda-format-date
  19411. (org-time-from-absolute date))
  19412. (funcall org-agenda-format-date date))
  19413. "\n")
  19414. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19415. (put-text-property s (1- (point)) 'org-date-line t)
  19416. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19417. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19418. (if rtnall (insert
  19419. (org-finalize-agenda-entries
  19420. (org-agenda-add-time-grid-maybe
  19421. rtnall nd todayp))
  19422. "\n"))
  19423. (put-text-property s (1- (point)) 'day d)
  19424. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19425. (goto-char (point-min))
  19426. (org-fit-agenda-window)
  19427. (unless (and (pos-visible-in-window-p (point-min))
  19428. (pos-visible-in-window-p (point-max)))
  19429. (goto-char (1- (point-max)))
  19430. (recenter -1)
  19431. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19432. (progn
  19433. (goto-char (or start-pos 1))
  19434. (recenter 1))))
  19435. (goto-char (or start-pos 1))
  19436. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19437. (org-finalize-agenda)
  19438. (setq buffer-read-only t)
  19439. (message "")))
  19440. (defun org-agenda-ndays-to-span (n)
  19441. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19442. ;;; Agenda word search
  19443. (defvar org-agenda-search-history nil)
  19444. ;;;###autoload
  19445. (defun org-search-view (&optional arg string)
  19446. "Show all entries that contain words or regular expressions.
  19447. If the first character of the search string is an asterisks,
  19448. search only the headlines.
  19449. The search string is broken into \"words\" by splitting at whitespace.
  19450. The individual words are then interpreted as a boolean expression with
  19451. logical AND. Words prefixed with a minus must not occur in the entry.
  19452. Words without a prefix or prefixed with a plus must occur in the entry.
  19453. Matching is case-insensitive and the words are enclosed by word delimiters.
  19454. Words enclosed by curly braces are interpreted as regular expressions
  19455. that must or must not match in the entry.
  19456. This command searches the agenda files, and in addition the files listed
  19457. in `org-agenda-text-search-extra-files'."
  19458. (interactive "P")
  19459. (org-compile-prefix-format 'search)
  19460. (org-set-sorting-strategy 'search)
  19461. (org-prepare-agenda "SEARCH")
  19462. (let* ((props (list 'face nil
  19463. 'done-face 'org-done
  19464. 'org-not-done-regexp org-not-done-regexp
  19465. 'org-todo-regexp org-todo-regexp
  19466. 'mouse-face 'highlight
  19467. 'keymap org-agenda-keymap
  19468. 'help-echo (format "mouse-2 or RET jump to location")))
  19469. regexp rtn rtnall files file pos
  19470. marker priority category tags c neg re
  19471. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19472. (unless (and (not arg)
  19473. (stringp string)
  19474. (string-match "\\S-" string))
  19475. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19476. (cond
  19477. ((integerp arg) (cons string arg))
  19478. (arg string))
  19479. 'org-agenda-search-history)))
  19480. (setq org-agenda-redo-command
  19481. (list 'org-search-view 'current-prefix-arg string))
  19482. (setq org-agenda-query-string string)
  19483. (if (equal (string-to-char string) ?*)
  19484. (setq hdl-only t
  19485. words (substring string 1))
  19486. (setq words string))
  19487. (setq words (org-split-string words))
  19488. (mapc (lambda (w)
  19489. (setq c (string-to-char w))
  19490. (if (equal c ?-)
  19491. (setq neg t w (substring w 1))
  19492. (if (equal c ?+)
  19493. (setq neg nil w (substring w 1))
  19494. (setq neg nil)))
  19495. (if (string-match "\\`{.*}\\'" w)
  19496. (setq re (substring w 1 -1))
  19497. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19498. (if neg (push re regexps-) (push re regexps+)))
  19499. words)
  19500. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19501. (if (not regexps+)
  19502. (setq regexp (concat "^" org-outline-regexp))
  19503. (setq regexp (pop regexps+))
  19504. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19505. regexp))))
  19506. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19507. rtnall nil)
  19508. (while (setq file (pop files))
  19509. (setq ee nil)
  19510. (catch 'nextfile
  19511. (org-check-agenda-file file)
  19512. (setq buffer (if (file-exists-p file)
  19513. (org-get-agenda-file-buffer file)
  19514. (error "No such file %s" file)))
  19515. (if (not buffer)
  19516. ;; If file does not exist, make sure an error message is sent
  19517. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19518. file))))
  19519. (with-current-buffer buffer
  19520. (unless (org-mode-p)
  19521. (error "Agenda file %s is not in `org-mode'" file))
  19522. (let ((case-fold-search t))
  19523. (save-excursion
  19524. (save-restriction
  19525. (if org-agenda-restrict
  19526. (narrow-to-region org-agenda-restrict-begin
  19527. org-agenda-restrict-end)
  19528. (widen))
  19529. (goto-char (point-min))
  19530. (unless (or (org-on-heading-p)
  19531. (outline-next-heading))
  19532. (throw 'nextfile t))
  19533. (goto-char (max (point-min) (1- (point))))
  19534. (while (re-search-forward regexp nil t)
  19535. (org-back-to-heading t)
  19536. (skip-chars-forward "* ")
  19537. (setq beg (point-at-bol)
  19538. beg1 (point)
  19539. end (progn (outline-next-heading) (point)))
  19540. (catch :skip
  19541. (goto-char beg)
  19542. (org-agenda-skip)
  19543. (setq str (buffer-substring-no-properties
  19544. (point-at-bol)
  19545. (if hdl-only (point-at-eol) end)))
  19546. (mapc (lambda (wr) (when (string-match wr str)
  19547. (goto-char (1- end))
  19548. (throw :skip t)))
  19549. regexps-)
  19550. (mapc (lambda (wr) (unless (string-match wr str)
  19551. (goto-char (1- end))
  19552. (throw :skip t)))
  19553. regexps+)
  19554. (goto-char beg)
  19555. (setq marker (org-agenda-new-marker (point))
  19556. category (org-get-category)
  19557. tags (org-get-tags-at (point))
  19558. txt (org-format-agenda-item
  19559. ""
  19560. (buffer-substring-no-properties
  19561. beg1 (point-at-eol))
  19562. category tags))
  19563. (org-add-props txt props
  19564. 'org-marker marker 'org-hd-marker marker
  19565. 'priority 1000 'org-category category
  19566. 'type "search")
  19567. (push txt ee)
  19568. (goto-char (1- end)))))))))
  19569. (setq rtn (nreverse ee))
  19570. (setq rtnall (append rtnall rtn)))
  19571. (if org-agenda-overriding-header
  19572. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19573. nil 'face 'org-agenda-structure) "\n")
  19574. (insert "Search words: ")
  19575. (add-text-properties (point-min) (1- (point))
  19576. (list 'face 'org-agenda-structure))
  19577. (setq pos (point))
  19578. (insert string "\n")
  19579. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19580. (setq pos (point))
  19581. (unless org-agenda-multi
  19582. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19583. (add-text-properties pos (1- (point))
  19584. (list 'face 'org-agenda-structure))))
  19585. (when rtnall
  19586. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19587. (goto-char (point-min))
  19588. (org-fit-agenda-window)
  19589. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19590. (org-finalize-agenda)
  19591. (setq buffer-read-only t)))
  19592. ;;; Agenda TODO list
  19593. (defvar org-select-this-todo-keyword nil)
  19594. (defvar org-last-arg nil)
  19595. ;;;###autoload
  19596. (defun org-todo-list (arg)
  19597. "Show all TODO entries from all agenda file in a single list.
  19598. The prefix arg can be used to select a specific TODO keyword and limit
  19599. the list to these. When using \\[universal-argument], you will be prompted
  19600. for a keyword. A numeric prefix directly selects the Nth keyword in
  19601. `org-todo-keywords-1'."
  19602. (interactive "P")
  19603. (require 'calendar)
  19604. (org-compile-prefix-format 'todo)
  19605. (org-set-sorting-strategy 'todo)
  19606. (org-prepare-agenda "TODO")
  19607. (let* ((today (time-to-days (current-time)))
  19608. (date (calendar-gregorian-from-absolute today))
  19609. (kwds org-todo-keywords-for-agenda)
  19610. (completion-ignore-case t)
  19611. (org-select-this-todo-keyword
  19612. (if (stringp arg) arg
  19613. (and arg (integerp arg) (> arg 0)
  19614. (nth (1- arg) kwds))))
  19615. rtn rtnall files file pos)
  19616. (when (equal arg '(4))
  19617. (setq org-select-this-todo-keyword
  19618. (completing-read "Keyword (or KWD1|K2D2|...): "
  19619. (mapcar 'list kwds) nil nil)))
  19620. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19621. (org-set-local 'org-last-arg arg)
  19622. (setq org-agenda-redo-command
  19623. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19624. (setq files (org-agenda-files)
  19625. rtnall nil)
  19626. (while (setq file (pop files))
  19627. (catch 'nextfile
  19628. (org-check-agenda-file file)
  19629. (setq rtn (org-agenda-get-day-entries file date :todo))
  19630. (setq rtnall (append rtnall rtn))))
  19631. (if org-agenda-overriding-header
  19632. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19633. nil 'face 'org-agenda-structure) "\n")
  19634. (insert "Global list of TODO items of type: ")
  19635. (add-text-properties (point-min) (1- (point))
  19636. (list 'face 'org-agenda-structure))
  19637. (setq pos (point))
  19638. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19639. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19640. (setq pos (point))
  19641. (unless org-agenda-multi
  19642. (insert "Available with `N r': (0)ALL")
  19643. (let ((n 0) s)
  19644. (mapc (lambda (x)
  19645. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19646. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19647. (insert "\n "))
  19648. (insert " " s))
  19649. kwds))
  19650. (insert "\n"))
  19651. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19652. (when rtnall
  19653. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19654. (goto-char (point-min))
  19655. (org-fit-agenda-window)
  19656. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19657. (org-finalize-agenda)
  19658. (setq buffer-read-only t)))
  19659. ;;; Agenda tags match
  19660. ;;;###autoload
  19661. (defun org-tags-view (&optional todo-only match)
  19662. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19663. The prefix arg TODO-ONLY limits the search to TODO entries."
  19664. (interactive "P")
  19665. (org-compile-prefix-format 'tags)
  19666. (org-set-sorting-strategy 'tags)
  19667. (let* ((org-tags-match-list-sublevels
  19668. (if todo-only t org-tags-match-list-sublevels))
  19669. (completion-ignore-case t)
  19670. rtn rtnall files file pos matcher
  19671. buffer)
  19672. (setq matcher (org-make-tags-matcher match)
  19673. match (car matcher) matcher (cdr matcher))
  19674. (org-prepare-agenda (concat "TAGS " match))
  19675. (setq org-agenda-query-string match)
  19676. (setq org-agenda-redo-command
  19677. (list 'org-tags-view (list 'quote todo-only)
  19678. (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
  19679. (setq files (org-agenda-files)
  19680. rtnall nil)
  19681. (while (setq file (pop files))
  19682. (catch 'nextfile
  19683. (org-check-agenda-file file)
  19684. (setq buffer (if (file-exists-p file)
  19685. (org-get-agenda-file-buffer file)
  19686. (error "No such file %s" file)))
  19687. (if (not buffer)
  19688. ;; If file does not exist, merror message to agenda
  19689. (setq rtn (list
  19690. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19691. rtnall (append rtnall rtn))
  19692. (with-current-buffer buffer
  19693. (unless (org-mode-p)
  19694. (error "Agenda file %s is not in `org-mode'" file))
  19695. (save-excursion
  19696. (save-restriction
  19697. (if org-agenda-restrict
  19698. (narrow-to-region org-agenda-restrict-begin
  19699. org-agenda-restrict-end)
  19700. (widen))
  19701. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19702. (setq rtnall (append rtnall rtn))))))))
  19703. (if org-agenda-overriding-header
  19704. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19705. nil 'face 'org-agenda-structure) "\n")
  19706. (insert "Headlines with TAGS match: ")
  19707. (add-text-properties (point-min) (1- (point))
  19708. (list 'face 'org-agenda-structure))
  19709. (setq pos (point))
  19710. (insert match "\n")
  19711. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19712. (setq pos (point))
  19713. (unless org-agenda-multi
  19714. (insert "Press `C-u r' to search again with new search string\n"))
  19715. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19716. (when rtnall
  19717. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19718. (goto-char (point-min))
  19719. (org-fit-agenda-window)
  19720. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19721. (org-finalize-agenda)
  19722. (setq buffer-read-only t)))
  19723. ;;; Agenda Finding stuck projects
  19724. (defvar org-agenda-skip-regexp nil
  19725. "Regular expression used in skipping subtrees for the agenda.
  19726. This is basically a temporary global variable that can be set and then
  19727. used by user-defined selections using `org-agenda-skip-function'.")
  19728. (defvar org-agenda-overriding-header nil
  19729. "When this is set during todo and tags searches, will replace header.")
  19730. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19731. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19732. If yes, it returns the end position of this tree, causing agenda commands
  19733. to skip this subtree. This is a function that can be put into
  19734. `org-agenda-skip-function' for the duration of a command."
  19735. (let ((end (save-excursion (org-end-of-subtree t)))
  19736. skip)
  19737. (save-excursion
  19738. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19739. (and skip end)))
  19740. (defun org-agenda-skip-entry-if (&rest conditions)
  19741. "Skip entry if any of CONDITIONS is true.
  19742. See `org-agenda-skip-if' for details."
  19743. (org-agenda-skip-if nil conditions))
  19744. (defun org-agenda-skip-subtree-if (&rest conditions)
  19745. "Skip entry if any of CONDITIONS is true.
  19746. See `org-agenda-skip-if' for details."
  19747. (org-agenda-skip-if t conditions))
  19748. (defun org-agenda-skip-if (subtree conditions)
  19749. "Checks current entity for CONDITIONS.
  19750. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19751. the entry, i.e. the text before the next heading is checked.
  19752. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19753. from different tests. Valid conditions are:
  19754. scheduled Check if there is a scheduled cookie
  19755. notscheduled Check if there is no scheduled cookie
  19756. deadline Check if there is a deadline
  19757. notdeadline Check if there is no deadline
  19758. regexp Check if regexp matches
  19759. notregexp Check if regexp does not match.
  19760. The regexp is taken from the conditions list, it must come right after
  19761. the `regexp' or `notregexp' element.
  19762. If any of these conditions is met, this function returns the end point of
  19763. the entity, causing the search to continue from there. This is a function
  19764. that can be put into `org-agenda-skip-function' for the duration of a command."
  19765. (let (beg end m)
  19766. (org-back-to-heading t)
  19767. (setq beg (point)
  19768. end (if subtree
  19769. (progn (org-end-of-subtree t) (point))
  19770. (progn (outline-next-heading) (1- (point)))))
  19771. (goto-char beg)
  19772. (and
  19773. (or
  19774. (and (memq 'scheduled conditions)
  19775. (re-search-forward org-scheduled-time-regexp end t))
  19776. (and (memq 'notscheduled conditions)
  19777. (not (re-search-forward org-scheduled-time-regexp end t)))
  19778. (and (memq 'deadline conditions)
  19779. (re-search-forward org-deadline-time-regexp end t))
  19780. (and (memq 'notdeadline conditions)
  19781. (not (re-search-forward org-deadline-time-regexp end t)))
  19782. (and (setq m (memq 'regexp conditions))
  19783. (stringp (nth 1 m))
  19784. (re-search-forward (nth 1 m) end t))
  19785. (and (setq m (memq 'notregexp conditions))
  19786. (stringp (nth 1 m))
  19787. (not (re-search-forward (nth 1 m) end t))))
  19788. end)))
  19789. ;;;###autoload
  19790. (defun org-agenda-list-stuck-projects (&rest ignore)
  19791. "Create agenda view for projects that are stuck.
  19792. Stuck projects are project that have no next actions. For the definitions
  19793. of what a project is and how to check if it stuck, customize the variable
  19794. `org-stuck-projects'.
  19795. MATCH is being ignored."
  19796. (interactive)
  19797. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19798. ;; FIXME: we could have used org-agenda-skip-if here.
  19799. (org-agenda-overriding-header "List of stuck projects: ")
  19800. (matcher (nth 0 org-stuck-projects))
  19801. (todo (nth 1 org-stuck-projects))
  19802. (todo-wds (if (member "*" todo)
  19803. (progn
  19804. (org-prepare-agenda-buffers (org-agenda-files))
  19805. (org-delete-all
  19806. org-done-keywords-for-agenda
  19807. (copy-sequence org-todo-keywords-for-agenda)))
  19808. todo))
  19809. (todo-re (concat "^\\*+[ \t]+\\("
  19810. (mapconcat 'identity todo-wds "\\|")
  19811. "\\)\\>"))
  19812. (tags (nth 2 org-stuck-projects))
  19813. (tags-re (if (member "*" tags)
  19814. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19815. (concat "^\\*+ .*:\\("
  19816. (mapconcat 'identity tags "\\|")
  19817. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19818. (gen-re (nth 3 org-stuck-projects))
  19819. (re-list
  19820. (delq nil
  19821. (list
  19822. (if todo todo-re)
  19823. (if tags tags-re)
  19824. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19825. gen-re)))))
  19826. (setq org-agenda-skip-regexp
  19827. (if re-list
  19828. (mapconcat 'identity re-list "\\|")
  19829. (error "No information how to identify unstuck projects")))
  19830. (org-tags-view nil matcher)
  19831. (with-current-buffer org-agenda-buffer-name
  19832. (setq org-agenda-redo-command
  19833. '(org-agenda-list-stuck-projects
  19834. (or current-prefix-arg org-last-arg))))))
  19835. ;;; Diary integration
  19836. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19837. (defun org-get-entries-from-diary (date)
  19838. "Get the (Emacs Calendar) diary entries for DATE."
  19839. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19840. (diary-display-hook '(fancy-diary-display))
  19841. (pop-up-frames nil)
  19842. (list-diary-entries-hook
  19843. (cons 'org-diary-default-entry list-diary-entries-hook))
  19844. (diary-file-name-prefix-function nil) ; turn this feature off
  19845. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19846. entries
  19847. (org-disable-agenda-to-diary t))
  19848. (save-excursion
  19849. (save-window-excursion
  19850. (funcall (if (fboundp 'diary-list-entries)
  19851. 'diary-list-entries 'list-diary-entries)
  19852. date 1)))
  19853. (if (not (get-buffer fancy-diary-buffer))
  19854. (setq entries nil)
  19855. (with-current-buffer fancy-diary-buffer
  19856. (setq buffer-read-only nil)
  19857. (if (zerop (buffer-size))
  19858. ;; No entries
  19859. (setq entries nil)
  19860. ;; Omit the date and other unnecessary stuff
  19861. (org-agenda-cleanup-fancy-diary)
  19862. ;; Add prefix to each line and extend the text properties
  19863. (if (zerop (buffer-size))
  19864. (setq entries nil)
  19865. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19866. (set-buffer-modified-p nil)
  19867. (kill-buffer fancy-diary-buffer)))
  19868. (when entries
  19869. (setq entries (org-split-string entries "\n"))
  19870. (setq entries
  19871. (mapcar
  19872. (lambda (x)
  19873. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19874. ;; Extend the text properties to the beginning of the line
  19875. (org-add-props x (text-properties-at (1- (length x)) x)
  19876. 'type "diary" 'date date))
  19877. entries)))))
  19878. (defun org-agenda-cleanup-fancy-diary ()
  19879. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19880. This gets rid of the date, the underline under the date, and
  19881. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19882. date. It also removes lines that contain only whitespace."
  19883. (goto-char (point-min))
  19884. (if (looking-at ".*?:[ \t]*")
  19885. (progn
  19886. (replace-match "")
  19887. (re-search-forward "\n=+$" nil t)
  19888. (replace-match "")
  19889. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19890. (re-search-forward "\n=+$" nil t)
  19891. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19892. (goto-char (point-min))
  19893. (while (re-search-forward "^ +\n" nil t)
  19894. (replace-match ""))
  19895. (goto-char (point-min))
  19896. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19897. (replace-match "")))
  19898. ;; Make sure entries from the diary have the right text properties.
  19899. (eval-after-load "diary-lib"
  19900. '(if (boundp 'diary-modify-entry-list-string-function)
  19901. ;; We can rely on the hook, nothing to do
  19902. nil
  19903. ;; Hook not avaiable, must use advice to make this work
  19904. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19905. "Make the position visible."
  19906. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19907. (stringp string)
  19908. buffer-file-name)
  19909. (setq string (org-modify-diary-entry-string string))))))
  19910. (defun org-modify-diary-entry-string (string)
  19911. "Add text properties to string, allowing org-mode to act on it."
  19912. (org-add-props string nil
  19913. 'mouse-face 'highlight
  19914. 'keymap org-agenda-keymap
  19915. 'help-echo (if buffer-file-name
  19916. (format "mouse-2 or RET jump to diary file %s"
  19917. (abbreviate-file-name buffer-file-name))
  19918. "")
  19919. 'org-agenda-diary-link t
  19920. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19921. (defun org-diary-default-entry ()
  19922. "Add a dummy entry to the diary.
  19923. Needed to avoid empty dates which mess up holiday display."
  19924. ;; Catch the error if dealing with the new add-to-diary-alist
  19925. (when org-disable-agenda-to-diary
  19926. (condition-case nil
  19927. (add-to-diary-list original-date "Org-mode dummy" "")
  19928. (error
  19929. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19930. ;;;###autoload
  19931. (defun org-diary (&rest args)
  19932. "Return diary information from org-files.
  19933. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19934. It accesses org files and extracts information from those files to be
  19935. listed in the diary. The function accepts arguments specifying what
  19936. items should be listed. The following arguments are allowed:
  19937. :timestamp List the headlines of items containing a date stamp or
  19938. date range matching the selected date. Deadlines will
  19939. also be listed, on the expiration day.
  19940. :sexp List entries resulting from diary-like sexps.
  19941. :deadline List any deadlines past due, or due within
  19942. `org-deadline-warning-days'. The listing occurs only
  19943. in the diary for *today*, not at any other date. If
  19944. an entry is marked DONE, it is no longer listed.
  19945. :scheduled List all items which are scheduled for the given date.
  19946. The diary for *today* also contains items which were
  19947. scheduled earlier and are not yet marked DONE.
  19948. :todo List all TODO items from the org-file. This may be a
  19949. long list - so this is not turned on by default.
  19950. Like deadlines, these entries only show up in the
  19951. diary for *today*, not at any other date.
  19952. The call in the diary file should look like this:
  19953. &%%(org-diary) ~/path/to/some/orgfile.org
  19954. Use a separate line for each org file to check. Or, if you omit the file name,
  19955. all files listed in `org-agenda-files' will be checked automatically:
  19956. &%%(org-diary)
  19957. If you don't give any arguments (as in the example above), the default
  19958. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19959. So the example above may also be written as
  19960. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19961. The function expects the lisp variables `entry' and `date' to be provided
  19962. by the caller, because this is how the calendar works. Don't use this
  19963. function from a program - use `org-agenda-get-day-entries' instead."
  19964. (when (> (- (time-to-seconds (current-time))
  19965. org-agenda-last-marker-time)
  19966. 5)
  19967. (org-agenda-reset-markers))
  19968. (org-compile-prefix-format 'agenda)
  19969. (org-set-sorting-strategy 'agenda)
  19970. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19971. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19972. (list entry)
  19973. (org-agenda-files t)))
  19974. file rtn results)
  19975. (org-prepare-agenda-buffers files)
  19976. ;; If this is called during org-agenda, don't return any entries to
  19977. ;; the calendar. Org Agenda will list these entries itself.
  19978. (if org-disable-agenda-to-diary (setq files nil))
  19979. (while (setq file (pop files))
  19980. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19981. (setq results (append results rtn)))
  19982. (if results
  19983. (concat (org-finalize-agenda-entries results) "\n"))))
  19984. ;;; Agenda entry finders
  19985. (defun org-agenda-get-day-entries (file date &rest args)
  19986. "Does the work for `org-diary' and `org-agenda'.
  19987. FILE is the path to a file to be checked for entries. DATE is date like
  19988. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19989. which kind of entries should be extracted. For details about these, see
  19990. the documentation of `org-diary'."
  19991. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19992. (let* ((org-startup-folded nil)
  19993. (org-startup-align-all-tables nil)
  19994. (buffer (if (file-exists-p file)
  19995. (org-get-agenda-file-buffer file)
  19996. (error "No such file %s" file)))
  19997. arg results rtn)
  19998. (if (not buffer)
  19999. ;; If file does not exist, make sure an error message ends up in diary
  20000. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  20001. (with-current-buffer buffer
  20002. (unless (org-mode-p)
  20003. (error "Agenda file %s is not in `org-mode'" file))
  20004. (let ((case-fold-search nil))
  20005. (save-excursion
  20006. (save-restriction
  20007. (if org-agenda-restrict
  20008. (narrow-to-region org-agenda-restrict-begin
  20009. org-agenda-restrict-end)
  20010. (widen))
  20011. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  20012. (while (setq arg (pop args))
  20013. (cond
  20014. ((and (eq arg :todo)
  20015. (equal date (calendar-current-date)))
  20016. (setq rtn (org-agenda-get-todos))
  20017. (setq results (append results rtn)))
  20018. ((eq arg :timestamp)
  20019. (setq rtn (org-agenda-get-blocks))
  20020. (setq results (append results rtn))
  20021. (setq rtn (org-agenda-get-timestamps))
  20022. (setq results (append results rtn)))
  20023. ((eq arg :sexp)
  20024. (setq rtn (org-agenda-get-sexps))
  20025. (setq results (append results rtn)))
  20026. ((eq arg :scheduled)
  20027. (setq rtn (org-agenda-get-scheduled))
  20028. (setq results (append results rtn)))
  20029. ((eq arg :closed)
  20030. (setq rtn (org-agenda-get-closed))
  20031. (setq results (append results rtn)))
  20032. ((eq arg :deadline)
  20033. (setq rtn (org-agenda-get-deadlines))
  20034. (setq results (append results rtn))))))))
  20035. results))))
  20036. (defun org-entry-is-todo-p ()
  20037. (member (org-get-todo-state) org-not-done-keywords))
  20038. (defun org-entry-is-done-p ()
  20039. (member (org-get-todo-state) org-done-keywords))
  20040. (defun org-get-todo-state ()
  20041. (save-excursion
  20042. (org-back-to-heading t)
  20043. (and (looking-at org-todo-line-regexp)
  20044. (match-end 2)
  20045. (match-string 2))))
  20046. (defun org-at-date-range-p (&optional inactive-ok)
  20047. "Is the cursor inside a date range?"
  20048. (interactive)
  20049. (save-excursion
  20050. (catch 'exit
  20051. (let ((pos (point)))
  20052. (skip-chars-backward "^[<\r\n")
  20053. (skip-chars-backward "<[")
  20054. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  20055. (>= (match-end 0) pos)
  20056. (throw 'exit t))
  20057. (skip-chars-backward "^<[\r\n")
  20058. (skip-chars-backward "<[")
  20059. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  20060. (>= (match-end 0) pos)
  20061. (throw 'exit t)))
  20062. nil)))
  20063. (defun org-agenda-get-todos ()
  20064. "Return the TODO information for agenda display."
  20065. (let* ((props (list 'face nil
  20066. 'done-face 'org-done
  20067. 'org-not-done-regexp org-not-done-regexp
  20068. 'org-todo-regexp org-todo-regexp
  20069. 'mouse-face 'highlight
  20070. 'keymap org-agenda-keymap
  20071. 'help-echo
  20072. (format "mouse-2 or RET jump to org file %s"
  20073. (abbreviate-file-name buffer-file-name))))
  20074. ;; FIXME: get rid of the \n at some point but watch out
  20075. (regexp (concat "^\\*+[ \t]+\\("
  20076. (if org-select-this-todo-keyword
  20077. (if (equal org-select-this-todo-keyword "*")
  20078. org-todo-regexp
  20079. (concat "\\<\\("
  20080. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  20081. "\\)\\>"))
  20082. org-not-done-regexp)
  20083. "[^\n\r]*\\)"))
  20084. marker priority category tags
  20085. ee txt beg end)
  20086. (goto-char (point-min))
  20087. (while (re-search-forward regexp nil t)
  20088. (catch :skip
  20089. (save-match-data
  20090. (beginning-of-line)
  20091. (setq beg (point) end (progn (outline-next-heading) (point)))
  20092. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  20093. (re-search-forward org-ts-regexp end t))
  20094. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  20095. (re-search-forward org-scheduled-time-regexp end t))
  20096. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  20097. (re-search-forward org-deadline-time-regexp end t)
  20098. (org-deadline-close (match-string 1))))
  20099. (goto-char (1+ beg))
  20100. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  20101. (throw :skip nil)))
  20102. (goto-char beg)
  20103. (org-agenda-skip)
  20104. (goto-char (match-beginning 1))
  20105. (setq marker (org-agenda-new-marker (match-beginning 0))
  20106. category (org-get-category)
  20107. tags (org-get-tags-at (point))
  20108. txt (org-format-agenda-item "" (match-string 1) category tags)
  20109. priority (1+ (org-get-priority txt)))
  20110. (org-add-props txt props
  20111. 'org-marker marker 'org-hd-marker marker
  20112. 'priority priority 'org-category category
  20113. 'type "todo")
  20114. (push txt ee)
  20115. (if org-agenda-todo-list-sublevels
  20116. (goto-char (match-end 1))
  20117. (org-end-of-subtree 'invisible))))
  20118. (nreverse ee)))
  20119. (defconst org-agenda-no-heading-message
  20120. "No heading for this item in buffer or region.")
  20121. (defun org-agenda-get-timestamps ()
  20122. "Return the date stamp information for agenda display."
  20123. (let* ((props (list 'face nil
  20124. 'org-not-done-regexp org-not-done-regexp
  20125. 'org-todo-regexp org-todo-regexp
  20126. 'mouse-face 'highlight
  20127. 'keymap org-agenda-keymap
  20128. 'help-echo
  20129. (format "mouse-2 or RET jump to org file %s"
  20130. (abbreviate-file-name buffer-file-name))))
  20131. (d1 (calendar-absolute-from-gregorian date))
  20132. (remove-re
  20133. (concat
  20134. (regexp-quote
  20135. (format-time-string
  20136. "<%Y-%m-%d"
  20137. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  20138. ".*?>"))
  20139. (regexp
  20140. (concat
  20141. (regexp-quote
  20142. (substring
  20143. (format-time-string
  20144. (car org-time-stamp-formats)
  20145. (apply 'encode-time ; DATE bound by calendar
  20146. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20147. 0 11))
  20148. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  20149. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  20150. marker hdmarker deadlinep scheduledp donep tmp priority category
  20151. ee txt timestr tags b0 b3 e3 head)
  20152. (goto-char (point-min))
  20153. (while (re-search-forward regexp nil t)
  20154. (setq b0 (match-beginning 0)
  20155. b3 (match-beginning 3) e3 (match-end 3))
  20156. (catch :skip
  20157. (and (org-at-date-range-p) (throw :skip nil))
  20158. (org-agenda-skip)
  20159. (if (and (match-end 1)
  20160. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  20161. (throw :skip nil))
  20162. (if (and e3
  20163. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  20164. (throw :skip nil))
  20165. (setq marker (org-agenda-new-marker b0)
  20166. category (org-get-category b0)
  20167. tmp (buffer-substring (max (point-min)
  20168. (- b0 org-ds-keyword-length))
  20169. b0)
  20170. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  20171. deadlinep (string-match org-deadline-regexp tmp)
  20172. scheduledp (string-match org-scheduled-regexp tmp)
  20173. donep (org-entry-is-done-p))
  20174. (if (or scheduledp deadlinep) (throw :skip t))
  20175. (if (string-match ">" timestr)
  20176. ;; substring should only run to end of time stamp
  20177. (setq timestr (substring timestr 0 (match-end 0))))
  20178. (save-excursion
  20179. (if (re-search-backward "^\\*+ " nil t)
  20180. (progn
  20181. (goto-char (match-beginning 0))
  20182. (setq hdmarker (org-agenda-new-marker)
  20183. tags (org-get-tags-at))
  20184. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20185. (setq head (match-string 1))
  20186. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  20187. (setq txt (org-format-agenda-item
  20188. nil head category tags timestr nil
  20189. remove-re)))
  20190. (setq txt org-agenda-no-heading-message))
  20191. (setq priority (org-get-priority txt))
  20192. (org-add-props txt props
  20193. 'org-marker marker 'org-hd-marker hdmarker)
  20194. (org-add-props txt nil 'priority priority
  20195. 'org-category category 'date date
  20196. 'type "timestamp")
  20197. (push txt ee))
  20198. (outline-next-heading)))
  20199. (nreverse ee)))
  20200. (defun org-agenda-get-sexps ()
  20201. "Return the sexp information for agenda display."
  20202. (require 'diary-lib)
  20203. (let* ((props (list 'face nil
  20204. 'mouse-face 'highlight
  20205. 'keymap org-agenda-keymap
  20206. 'help-echo
  20207. (format "mouse-2 or RET jump to org file %s"
  20208. (abbreviate-file-name buffer-file-name))))
  20209. (regexp "^&?%%(")
  20210. marker category ee txt tags entry result beg b sexp sexp-entry)
  20211. (goto-char (point-min))
  20212. (while (re-search-forward regexp nil t)
  20213. (catch :skip
  20214. (org-agenda-skip)
  20215. (setq beg (match-beginning 0))
  20216. (goto-char (1- (match-end 0)))
  20217. (setq b (point))
  20218. (forward-sexp 1)
  20219. (setq sexp (buffer-substring b (point)))
  20220. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20221. (org-trim (match-string 1))
  20222. ""))
  20223. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20224. (when result
  20225. (setq marker (org-agenda-new-marker beg)
  20226. category (org-get-category beg))
  20227. (if (string-match "\\S-" result)
  20228. (setq txt result)
  20229. (setq txt "SEXP entry returned empty string"))
  20230. (setq txt (org-format-agenda-item
  20231. "" txt category tags 'time))
  20232. (org-add-props txt props 'org-marker marker)
  20233. (org-add-props txt nil
  20234. 'org-category category 'date date
  20235. 'type "sexp")
  20236. (push txt ee))))
  20237. (nreverse ee)))
  20238. (defun org-agenda-get-closed ()
  20239. "Return the logged TODO entries for agenda display."
  20240. (let* ((props (list 'mouse-face 'highlight
  20241. 'org-not-done-regexp org-not-done-regexp
  20242. 'org-todo-regexp org-todo-regexp
  20243. 'keymap org-agenda-keymap
  20244. 'help-echo
  20245. (format "mouse-2 or RET jump to org file %s"
  20246. (abbreviate-file-name buffer-file-name))))
  20247. (regexp (concat
  20248. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20249. (regexp-quote
  20250. (substring
  20251. (format-time-string
  20252. (car org-time-stamp-formats)
  20253. (apply 'encode-time ; DATE bound by calendar
  20254. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20255. 1 11))))
  20256. marker hdmarker priority category tags closedp
  20257. ee txt timestr)
  20258. (goto-char (point-min))
  20259. (while (re-search-forward regexp nil t)
  20260. (catch :skip
  20261. (org-agenda-skip)
  20262. (setq marker (org-agenda-new-marker (match-beginning 0))
  20263. closedp (equal (match-string 1) org-closed-string)
  20264. category (org-get-category (match-beginning 0))
  20265. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20266. ;; donep (org-entry-is-done-p)
  20267. )
  20268. (if (string-match "\\]" timestr)
  20269. ;; substring should only run to end of time stamp
  20270. (setq timestr (substring timestr 0 (match-end 0))))
  20271. (save-excursion
  20272. (if (re-search-backward "^\\*+ " nil t)
  20273. (progn
  20274. (goto-char (match-beginning 0))
  20275. (setq hdmarker (org-agenda-new-marker)
  20276. tags (org-get-tags-at))
  20277. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20278. (setq txt (org-format-agenda-item
  20279. (if closedp "Closed: " "Clocked: ")
  20280. (match-string 1) category tags timestr)))
  20281. (setq txt org-agenda-no-heading-message))
  20282. (setq priority 100000)
  20283. (org-add-props txt props
  20284. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20285. 'priority priority 'org-category category
  20286. 'type "closed" 'date date
  20287. 'undone-face 'org-warning 'done-face 'org-done)
  20288. (push txt ee))
  20289. (goto-char (point-at-eol))))
  20290. (nreverse ee)))
  20291. (defun org-agenda-get-deadlines ()
  20292. "Return the deadline information for agenda display."
  20293. (let* ((props (list 'mouse-face 'highlight
  20294. 'org-not-done-regexp org-not-done-regexp
  20295. 'org-todo-regexp org-todo-regexp
  20296. 'keymap org-agenda-keymap
  20297. 'help-echo
  20298. (format "mouse-2 or RET jump to org file %s"
  20299. (abbreviate-file-name buffer-file-name))))
  20300. (regexp org-deadline-time-regexp)
  20301. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20302. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20303. d2 diff dfrac wdays pos pos1 category tags
  20304. ee txt head face s upcomingp donep timestr)
  20305. (goto-char (point-min))
  20306. (while (re-search-forward regexp nil t)
  20307. (catch :skip
  20308. (org-agenda-skip)
  20309. (setq s (match-string 1)
  20310. pos (1- (match-beginning 1))
  20311. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20312. diff (- d2 d1)
  20313. wdays (org-get-wdays s)
  20314. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20315. upcomingp (and todayp (> diff 0)))
  20316. ;; When to show a deadline in the calendar:
  20317. ;; If the expiration is within wdays warning time.
  20318. ;; Past-due deadlines are only shown on the current date
  20319. (if (or (and (<= diff wdays)
  20320. (and todayp (not org-agenda-only-exact-dates)))
  20321. (= diff 0))
  20322. (save-excursion
  20323. (setq category (org-get-category))
  20324. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20325. (progn
  20326. (goto-char (match-end 0))
  20327. (setq pos1 (match-beginning 0))
  20328. (setq tags (org-get-tags-at pos1))
  20329. (setq head (buffer-substring-no-properties
  20330. (point)
  20331. (progn (skip-chars-forward "^\r\n")
  20332. (point))))
  20333. (setq donep (string-match org-looking-at-done-regexp head))
  20334. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20335. (setq timestr
  20336. (concat (substring s (match-beginning 1)) " "))
  20337. (setq timestr 'time))
  20338. (if (and donep
  20339. (or org-agenda-skip-deadline-if-done
  20340. (not (= diff 0))))
  20341. (setq txt nil)
  20342. (setq txt (org-format-agenda-item
  20343. (if (= diff 0)
  20344. (car org-agenda-deadline-leaders)
  20345. (format (nth 1 org-agenda-deadline-leaders)
  20346. diff))
  20347. head category tags timestr))))
  20348. (setq txt org-agenda-no-heading-message))
  20349. (when txt
  20350. (setq face (org-agenda-deadline-face dfrac wdays))
  20351. (org-add-props txt props
  20352. 'org-marker (org-agenda-new-marker pos)
  20353. 'org-hd-marker (org-agenda-new-marker pos1)
  20354. 'priority (+ (- diff)
  20355. (org-get-priority txt))
  20356. 'org-category category
  20357. 'type (if upcomingp "upcoming-deadline" "deadline")
  20358. 'date (if upcomingp date d2)
  20359. 'face (if donep 'org-done face)
  20360. 'undone-face face 'done-face 'org-done)
  20361. (push txt ee))))))
  20362. (nreverse ee)))
  20363. (defun org-agenda-deadline-face (fraction &optional wdays)
  20364. "Return the face to displaying a deadline item.
  20365. FRACTION is what fraction of the head-warning time has passed."
  20366. (if (equal wdays 0) (setq fraction 1.))
  20367. (let ((faces org-agenda-deadline-faces) f)
  20368. (catch 'exit
  20369. (while (setq f (pop faces))
  20370. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20371. (defun org-agenda-get-scheduled ()
  20372. "Return the scheduled information for agenda display."
  20373. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20374. 'org-todo-regexp org-todo-regexp
  20375. 'done-face 'org-done
  20376. 'mouse-face 'highlight
  20377. 'keymap org-agenda-keymap
  20378. 'help-echo
  20379. (format "mouse-2 or RET jump to org file %s"
  20380. (abbreviate-file-name buffer-file-name))))
  20381. (regexp org-scheduled-time-regexp)
  20382. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20383. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20384. d2 diff pos pos1 category tags
  20385. ee txt head pastschedp donep face timestr s)
  20386. (goto-char (point-min))
  20387. (while (re-search-forward regexp nil t)
  20388. (catch :skip
  20389. (org-agenda-skip)
  20390. (setq s (match-string 1)
  20391. pos (1- (match-beginning 1))
  20392. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20393. ;;; is this right?
  20394. ;;; do we need to do this for deadleine too????
  20395. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20396. diff (- d2 d1))
  20397. (setq pastschedp (and todayp (< diff 0)))
  20398. ;; When to show a scheduled item in the calendar:
  20399. ;; If it is on or past the date.
  20400. (if (or (and (< diff 0)
  20401. (and todayp (not org-agenda-only-exact-dates)))
  20402. (= diff 0))
  20403. (save-excursion
  20404. (setq category (org-get-category))
  20405. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20406. (progn
  20407. (goto-char (match-end 0))
  20408. (setq pos1 (match-beginning 0))
  20409. (setq tags (org-get-tags-at))
  20410. (setq head (buffer-substring-no-properties
  20411. (point)
  20412. (progn (skip-chars-forward "^\r\n") (point))))
  20413. (setq donep (string-match org-looking-at-done-regexp head))
  20414. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20415. (setq timestr
  20416. (concat (substring s (match-beginning 1)) " "))
  20417. (setq timestr 'time))
  20418. (if (and donep
  20419. (or org-agenda-skip-scheduled-if-done
  20420. (not (= diff 0))))
  20421. (setq txt nil)
  20422. (setq txt (org-format-agenda-item
  20423. (if (= diff 0)
  20424. (car org-agenda-scheduled-leaders)
  20425. (format (nth 1 org-agenda-scheduled-leaders)
  20426. (- 1 diff)))
  20427. head category tags timestr))))
  20428. (setq txt org-agenda-no-heading-message))
  20429. (when txt
  20430. (setq face (if pastschedp
  20431. 'org-scheduled-previously
  20432. 'org-scheduled-today))
  20433. (org-add-props txt props
  20434. 'undone-face face
  20435. 'face (if donep 'org-done face)
  20436. 'org-marker (org-agenda-new-marker pos)
  20437. 'org-hd-marker (org-agenda-new-marker pos1)
  20438. 'type (if pastschedp "past-scheduled" "scheduled")
  20439. 'date (if pastschedp d2 date)
  20440. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20441. 'org-category category)
  20442. (push txt ee))))))
  20443. (nreverse ee)))
  20444. (defun org-agenda-get-blocks ()
  20445. "Return the date-range information for agenda display."
  20446. (let* ((props (list 'face nil
  20447. 'org-not-done-regexp org-not-done-regexp
  20448. 'org-todo-regexp org-todo-regexp
  20449. 'mouse-face 'highlight
  20450. 'keymap org-agenda-keymap
  20451. 'help-echo
  20452. (format "mouse-2 or RET jump to org file %s"
  20453. (abbreviate-file-name buffer-file-name))))
  20454. (regexp org-tr-regexp)
  20455. (d0 (calendar-absolute-from-gregorian date))
  20456. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20457. donep head)
  20458. (goto-char (point-min))
  20459. (while (re-search-forward regexp nil t)
  20460. (catch :skip
  20461. (org-agenda-skip)
  20462. (setq pos (point))
  20463. (setq timestr (match-string 0)
  20464. s1 (match-string 1)
  20465. s2 (match-string 2)
  20466. d1 (time-to-days (org-time-string-to-time s1))
  20467. d2 (time-to-days (org-time-string-to-time s2)))
  20468. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20469. ;; Only allow days between the limits, because the normal
  20470. ;; date stamps will catch the limits.
  20471. (save-excursion
  20472. (setq marker (org-agenda-new-marker (point)))
  20473. (setq category (org-get-category))
  20474. (if (re-search-backward "^\\*+ " nil t)
  20475. (progn
  20476. (goto-char (match-beginning 0))
  20477. (setq hdmarker (org-agenda-new-marker (point)))
  20478. (setq tags (org-get-tags-at))
  20479. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20480. (setq head (match-string 1))
  20481. (and org-agenda-skip-timestamp-if-done
  20482. (org-entry-is-done-p)
  20483. (throw :skip t))
  20484. (setq txt (org-format-agenda-item
  20485. (format (if (= d1 d2) "" "(%d/%d): ")
  20486. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20487. head category tags
  20488. (if (= d0 d1) timestr))))
  20489. (setq txt org-agenda-no-heading-message))
  20490. (org-add-props txt props
  20491. 'org-marker marker 'org-hd-marker hdmarker
  20492. 'type "block" 'date date
  20493. 'priority (org-get-priority txt) 'org-category category)
  20494. (push txt ee)))
  20495. (goto-char pos)))
  20496. ;; Sort the entries by expiration date.
  20497. (nreverse ee)))
  20498. ;;; Agenda presentation and sorting
  20499. (defconst org-plain-time-of-day-regexp
  20500. (concat
  20501. "\\(\\<[012]?[0-9]"
  20502. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20503. "\\(--?"
  20504. "\\(\\<[012]?[0-9]"
  20505. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20506. "\\)?")
  20507. "Regular expression to match a plain time or time range.
  20508. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20509. groups carry important information:
  20510. 0 the full match
  20511. 1 the first time, range or not
  20512. 8 the second time, if it is a range.")
  20513. (defconst org-plain-time-extension-regexp
  20514. (concat
  20515. "\\(\\<[012]?[0-9]"
  20516. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20517. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20518. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20519. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20520. groups carry important information:
  20521. 0 the full match
  20522. 7 hours of duration
  20523. 9 minutes of duration")
  20524. (defconst org-stamp-time-of-day-regexp
  20525. (concat
  20526. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20527. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20528. "\\(--?"
  20529. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20530. "Regular expression to match a timestamp time or time range.
  20531. After a match, the following groups carry important information:
  20532. 0 the full match
  20533. 1 date plus weekday, for backreferencing to make sure both times on same day
  20534. 2 the first time, range or not
  20535. 4 the second time, if it is a range.")
  20536. (defvar org-prefix-has-time nil
  20537. "A flag, set by `org-compile-prefix-format'.
  20538. The flag is set if the currently compiled format contains a `%t'.")
  20539. (defvar org-prefix-has-tag nil
  20540. "A flag, set by `org-compile-prefix-format'.
  20541. The flag is set if the currently compiled format contains a `%T'.")
  20542. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20543. noprefix remove-re)
  20544. "Format TXT to be inserted into the agenda buffer.
  20545. In particular, it adds the prefix and corresponding text properties. EXTRA
  20546. must be a string and replaces the `%s' specifier in the prefix format.
  20547. CATEGORY (string, symbol or nil) may be used to overrule the default
  20548. category taken from local variable or file name. It will replace the `%c'
  20549. specifier in the format. DOTIME, when non-nil, indicates that a
  20550. time-of-day should be extracted from TXT for sorting of this entry, and for
  20551. the `%t' specifier in the format. When DOTIME is a string, this string is
  20552. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20553. only the correctly processes TXT should be returned - this is used by
  20554. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20555. Any match of REMOVE-RE will be removed from TXT."
  20556. (save-match-data
  20557. ;; Diary entries sometimes have extra whitespace at the beginning
  20558. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20559. (let* ((category (or category
  20560. org-category
  20561. (if buffer-file-name
  20562. (file-name-sans-extension
  20563. (file-name-nondirectory buffer-file-name))
  20564. "")))
  20565. (tag (if tags (nth (1- (length tags)) tags) ""))
  20566. time ; time and tag are needed for the eval of the prefix format
  20567. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20568. (time-of-day (and dotime (org-get-time-of-day ts)))
  20569. stamp plain s0 s1 s2 rtn srp)
  20570. (when (and dotime time-of-day org-prefix-has-time)
  20571. ;; Extract starting and ending time and move them to prefix
  20572. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20573. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20574. (setq s0 (match-string 0 ts)
  20575. srp (and stamp (match-end 3))
  20576. s1 (match-string (if plain 1 2) ts)
  20577. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20578. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20579. ;; them, we might want to remove them there to avoid duplication.
  20580. ;; The user can turn this off with a variable.
  20581. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20582. (string-match (concat (regexp-quote s0) " *") txt)
  20583. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20584. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20585. (= (match-beginning 0) 0)
  20586. t))
  20587. (setq txt (replace-match "" nil nil txt))))
  20588. ;; Normalize the time(s) to 24 hour
  20589. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20590. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20591. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20592. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20593. (let ((m (+ (string-to-number (match-string 2 s1))
  20594. (* 60 (string-to-number (match-string 1 s1)))
  20595. org-agenda-default-appointment-duration))
  20596. h)
  20597. (setq h (/ m 60) m (- m (* h 60)))
  20598. (setq s2 (format "%02d:%02d" h m))))
  20599. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20600. txt)
  20601. ;; Tags are in the string
  20602. (if (or (eq org-agenda-remove-tags t)
  20603. (and org-agenda-remove-tags
  20604. org-prefix-has-tag))
  20605. (setq txt (replace-match "" t t txt))
  20606. (setq txt (replace-match
  20607. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20608. (match-string 2 txt))
  20609. t t txt))))
  20610. (when remove-re
  20611. (while (string-match remove-re txt)
  20612. (setq txt (replace-match "" t t txt))))
  20613. ;; Create the final string
  20614. (if noprefix
  20615. (setq rtn txt)
  20616. ;; Prepare the variables needed in the eval of the compiled format
  20617. (setq time (cond (s2 (concat s1 "-" s2))
  20618. (s1 (concat s1 "......"))
  20619. (t ""))
  20620. extra (or extra "")
  20621. category (if (symbolp category) (symbol-name category) category))
  20622. ;; Evaluate the compiled format
  20623. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20624. ;; And finally add the text properties
  20625. (org-add-props rtn nil
  20626. 'org-category (downcase category) 'tags tags
  20627. 'org-highest-priority org-highest-priority
  20628. 'org-lowest-priority org-lowest-priority
  20629. 'prefix-length (- (length rtn) (length txt))
  20630. 'time-of-day time-of-day
  20631. 'txt txt
  20632. 'time time
  20633. 'extra extra
  20634. 'dotime dotime))))
  20635. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20636. (defvar org-agenda-sorting-strategy-selected nil)
  20637. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20638. (catch 'exit
  20639. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20640. ((and todayp (member 'today (car org-agenda-time-grid))))
  20641. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20642. ((member 'weekly (car org-agenda-time-grid)))
  20643. (t (throw 'exit list)))
  20644. (let* ((have (delq nil (mapcar
  20645. (lambda (x) (get-text-property 1 'time-of-day x))
  20646. list)))
  20647. (string (nth 1 org-agenda-time-grid))
  20648. (gridtimes (nth 2 org-agenda-time-grid))
  20649. (req (car org-agenda-time-grid))
  20650. (remove (member 'remove-match req))
  20651. new time)
  20652. (if (and (member 'require-timed req) (not have))
  20653. ;; don't show empty grid
  20654. (throw 'exit list))
  20655. (while (setq time (pop gridtimes))
  20656. (unless (and remove (member time have))
  20657. (setq time (int-to-string time))
  20658. (push (org-format-agenda-item
  20659. nil string "" nil
  20660. (concat (substring time 0 -2) ":" (substring time -2)))
  20661. new)
  20662. (put-text-property
  20663. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20664. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20665. (append new list)
  20666. (append list new)))))
  20667. (defun org-compile-prefix-format (key)
  20668. "Compile the prefix format into a Lisp form that can be evaluated.
  20669. The resulting form is returned and stored in the variable
  20670. `org-prefix-format-compiled'."
  20671. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20672. (let ((s (cond
  20673. ((stringp org-agenda-prefix-format)
  20674. org-agenda-prefix-format)
  20675. ((assq key org-agenda-prefix-format)
  20676. (cdr (assq key org-agenda-prefix-format)))
  20677. (t " %-12:c%?-12t% s")))
  20678. (start 0)
  20679. varform vars var e c f opt)
  20680. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20681. s start)
  20682. (setq var (cdr (assoc (match-string 4 s)
  20683. '(("c" . category) ("t" . time) ("s" . extra)
  20684. ("T" . tag))))
  20685. c (or (match-string 3 s) "")
  20686. opt (match-beginning 1)
  20687. start (1+ (match-beginning 0)))
  20688. (if (equal var 'time) (setq org-prefix-has-time t))
  20689. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20690. (setq f (concat "%" (match-string 2 s) "s"))
  20691. (if opt
  20692. (setq varform
  20693. `(if (equal "" ,var)
  20694. ""
  20695. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20696. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20697. (setq s (replace-match "%s" t nil s))
  20698. (push varform vars))
  20699. (setq vars (nreverse vars))
  20700. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20701. (defun org-set-sorting-strategy (key)
  20702. (if (symbolp (car org-agenda-sorting-strategy))
  20703. ;; the old format
  20704. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20705. (setq org-agenda-sorting-strategy-selected
  20706. (or (cdr (assq key org-agenda-sorting-strategy))
  20707. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20708. '(time-up category-keep priority-down)))))
  20709. (defun org-get-time-of-day (s &optional string mod24)
  20710. "Check string S for a time of day.
  20711. If found, return it as a military time number between 0 and 2400.
  20712. If not found, return nil.
  20713. The optional STRING argument forces conversion into a 5 character wide string
  20714. HH:MM."
  20715. (save-match-data
  20716. (when
  20717. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20718. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20719. (let* ((h (string-to-number (match-string 1 s)))
  20720. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20721. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20722. (am-p (equal ampm "am"))
  20723. (h1 (cond ((not ampm) h)
  20724. ((= h 12) (if am-p 0 12))
  20725. (t (+ h (if am-p 0 12)))))
  20726. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20727. (mod h1 24) h1))
  20728. (t0 (+ (* 100 h2) m))
  20729. (t1 (concat (if (>= h1 24) "+" " ")
  20730. (if (< t0 100) "0" "")
  20731. (if (< t0 10) "0" "")
  20732. (int-to-string t0))))
  20733. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20734. (defun org-finalize-agenda-entries (list &optional nosort)
  20735. "Sort and concatenate the agenda items."
  20736. (setq list (mapcar 'org-agenda-highlight-todo list))
  20737. (if nosort
  20738. list
  20739. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20740. (defun org-agenda-highlight-todo (x)
  20741. (let (re pl)
  20742. (if (eq x 'line)
  20743. (save-excursion
  20744. (beginning-of-line 1)
  20745. (setq re (get-text-property (point) 'org-todo-regexp))
  20746. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20747. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20748. (add-text-properties (match-beginning 0) (match-end 0)
  20749. (list 'face (org-get-todo-face 0)))
  20750. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20751. (delete-region (match-beginning 1) (1- (match-end 0)))
  20752. (goto-char (match-beginning 1))
  20753. (insert (format org-agenda-todo-keyword-format s)))))
  20754. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20755. pl (get-text-property 0 'prefix-length x))
  20756. (when (and re
  20757. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20758. x (or pl 0)) pl))
  20759. (add-text-properties
  20760. (or (match-end 1) (match-end 0)) (match-end 0)
  20761. (list 'face (org-get-todo-face (match-string 2 x)))
  20762. x)
  20763. (setq x (concat (substring x 0 (match-end 1))
  20764. (format org-agenda-todo-keyword-format
  20765. (match-string 2 x))
  20766. " "
  20767. (substring x (match-end 3)))))
  20768. x)))
  20769. (defsubst org-cmp-priority (a b)
  20770. "Compare the priorities of string A and B."
  20771. (let ((pa (or (get-text-property 1 'priority a) 0))
  20772. (pb (or (get-text-property 1 'priority b) 0)))
  20773. (cond ((> pa pb) +1)
  20774. ((< pa pb) -1)
  20775. (t nil))))
  20776. (defsubst org-cmp-category (a b)
  20777. "Compare the string values of categories of strings A and B."
  20778. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20779. (cb (or (get-text-property 1 'org-category b) "")))
  20780. (cond ((string-lessp ca cb) -1)
  20781. ((string-lessp cb ca) +1)
  20782. (t nil))))
  20783. (defsubst org-cmp-tag (a b)
  20784. "Compare the string values of categories of strings A and B."
  20785. (let ((ta (car (last (get-text-property 1 'tags a))))
  20786. (tb (car (last (get-text-property 1 'tags b)))))
  20787. (cond ((not ta) +1)
  20788. ((not tb) -1)
  20789. ((string-lessp ta tb) -1)
  20790. ((string-lessp tb ta) +1)
  20791. (t nil))))
  20792. (defsubst org-cmp-time (a b)
  20793. "Compare the time-of-day values of strings A and B."
  20794. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20795. (ta (or (get-text-property 1 'time-of-day a) def))
  20796. (tb (or (get-text-property 1 'time-of-day b) def)))
  20797. (cond ((< ta tb) -1)
  20798. ((< tb ta) +1)
  20799. (t nil))))
  20800. (defun org-entries-lessp (a b)
  20801. "Predicate for sorting agenda entries."
  20802. ;; The following variables will be used when the form is evaluated.
  20803. ;; So even though the compiler complains, keep them.
  20804. (let* ((time-up (org-cmp-time a b))
  20805. (time-down (if time-up (- time-up) nil))
  20806. (priority-up (org-cmp-priority a b))
  20807. (priority-down (if priority-up (- priority-up) nil))
  20808. (category-up (org-cmp-category a b))
  20809. (category-down (if category-up (- category-up) nil))
  20810. (category-keep (if category-up +1 nil))
  20811. (tag-up (org-cmp-tag a b))
  20812. (tag-down (if tag-up (- tag-up) nil)))
  20813. (cdr (assoc
  20814. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20815. '((-1 . t) (1 . nil) (nil . nil))))))
  20816. ;;; Agenda restriction lock
  20817. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20818. "Overlay to mark the headline to which arenda commands are restricted.")
  20819. (org-overlay-put org-agenda-restriction-lock-overlay
  20820. 'face 'org-agenda-restriction-lock)
  20821. (org-overlay-put org-agenda-restriction-lock-overlay
  20822. 'help-echo "Agendas are currently limited to this subtree.")
  20823. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20824. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20825. "Overlay marking the agenda restriction line in speedbar.")
  20826. (org-overlay-put org-speedbar-restriction-lock-overlay
  20827. 'face 'org-agenda-restriction-lock)
  20828. (org-overlay-put org-speedbar-restriction-lock-overlay
  20829. 'help-echo "Agendas are currently limited to this item.")
  20830. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20831. (defun org-agenda-set-restriction-lock (&optional type)
  20832. "Set restriction lock for agenda, to current subtree or file.
  20833. Restriction will be the file if TYPE is `file', or if type is the
  20834. universal prefix '(4), or if the cursor is before the first headline
  20835. in the file. Otherwise, restriction will be to the current subtree."
  20836. (interactive "P")
  20837. (and (equal type '(4)) (setq type 'file))
  20838. (setq type (cond
  20839. (type type)
  20840. ((org-at-heading-p) 'subtree)
  20841. ((condition-case nil (org-back-to-heading t) (error nil))
  20842. 'subtree)
  20843. (t 'file)))
  20844. (if (eq type 'subtree)
  20845. (progn
  20846. (setq org-agenda-restrict t)
  20847. (setq org-agenda-overriding-restriction 'subtree)
  20848. (put 'org-agenda-files 'org-restrict
  20849. (list (buffer-file-name (buffer-base-buffer))))
  20850. (org-back-to-heading t)
  20851. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20852. (move-marker org-agenda-restrict-begin (point))
  20853. (move-marker org-agenda-restrict-end
  20854. (save-excursion (org-end-of-subtree t)))
  20855. (message "Locking agenda restriction to subtree"))
  20856. (put 'org-agenda-files 'org-restrict
  20857. (list (buffer-file-name (buffer-base-buffer))))
  20858. (setq org-agenda-restrict nil)
  20859. (setq org-agenda-overriding-restriction 'file)
  20860. (move-marker org-agenda-restrict-begin nil)
  20861. (move-marker org-agenda-restrict-end nil)
  20862. (message "Locking agenda restriction to file"))
  20863. (setq current-prefix-arg nil)
  20864. (org-agenda-maybe-redo))
  20865. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20866. "Remove the agenda restriction lock."
  20867. (interactive "P")
  20868. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20869. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20870. (setq org-agenda-overriding-restriction nil)
  20871. (setq org-agenda-restrict nil)
  20872. (put 'org-agenda-files 'org-restrict nil)
  20873. (move-marker org-agenda-restrict-begin nil)
  20874. (move-marker org-agenda-restrict-end nil)
  20875. (setq current-prefix-arg nil)
  20876. (message "Agenda restriction lock removed")
  20877. (or noupdate (org-agenda-maybe-redo)))
  20878. (defun org-agenda-maybe-redo ()
  20879. "If there is any window showing the agenda view, update it."
  20880. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20881. (w0 (selected-window)))
  20882. (when w
  20883. (select-window w)
  20884. (org-agenda-redo)
  20885. (select-window w0)
  20886. (if org-agenda-overriding-restriction
  20887. (message "Agenda view shifted to new %s restriction"
  20888. org-agenda-overriding-restriction)
  20889. (message "Agenda restriction lock removed")))))
  20890. ;;; Agenda commands
  20891. (defun org-agenda-check-type (error &rest types)
  20892. "Check if agenda buffer is of allowed type.
  20893. If ERROR is non-nil, throw an error, otherwise just return nil."
  20894. (if (memq org-agenda-type types)
  20895. t
  20896. (if error
  20897. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20898. nil)))
  20899. (defun org-agenda-quit ()
  20900. "Exit agenda by removing the window or the buffer."
  20901. (interactive)
  20902. (let ((buf (current-buffer)))
  20903. (if (not (one-window-p)) (delete-window))
  20904. (kill-buffer buf)
  20905. (org-agenda-reset-markers)
  20906. (org-columns-remove-overlays))
  20907. ;; Maybe restore the pre-agenda window configuration.
  20908. (and org-agenda-restore-windows-after-quit
  20909. (not (eq org-agenda-window-setup 'other-frame))
  20910. org-pre-agenda-window-conf
  20911. (set-window-configuration org-pre-agenda-window-conf)))
  20912. (defun org-agenda-exit ()
  20913. "Exit agenda by removing the window or the buffer.
  20914. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20915. Org-mode buffers visited directly by the user will not be touched."
  20916. (interactive)
  20917. (org-release-buffers org-agenda-new-buffers)
  20918. (setq org-agenda-new-buffers nil)
  20919. (org-agenda-quit))
  20920. (defun org-agenda-execute (arg)
  20921. "Execute another agenda command, keeping same window.\\<global-map>
  20922. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20923. (interactive "P")
  20924. (let ((org-agenda-window-setup 'current-window))
  20925. (org-agenda arg)))
  20926. (defun org-save-all-org-buffers ()
  20927. "Save all Org-mode buffers without user confirmation."
  20928. (interactive)
  20929. (message "Saving all Org-mode buffers...")
  20930. (save-some-buffers t 'org-mode-p)
  20931. (message "Saving all Org-mode buffers... done"))
  20932. (defun org-agenda-redo ()
  20933. "Rebuild Agenda.
  20934. When this is the global TODO list, a prefix argument will be interpreted."
  20935. (interactive)
  20936. (let* ((org-agenda-keep-modes t)
  20937. (line (org-current-line))
  20938. (window-line (- line (org-current-line (window-start))))
  20939. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20940. (message "Rebuilding agenda buffer...")
  20941. (org-let lprops '(eval org-agenda-redo-command))
  20942. (setq org-agenda-undo-list nil
  20943. org-agenda-pending-undo-list nil)
  20944. (message "Rebuilding agenda buffer...done")
  20945. (goto-line line)
  20946. (recenter window-line)))
  20947. (defun org-agenda-manipulate-query-add ()
  20948. "Manipulate the query by adding a search term with positive selection.
  20949. Positive selection means, the term must be matched for selection of an entry."
  20950. (interactive)
  20951. (org-agenda-manipulate-query ?\[))
  20952. (defun org-agenda-manipulate-query-subtract ()
  20953. "Manipulate the query by adding a search term with negative selection.
  20954. Negative selection means, term must not be matched for selection of an entry."
  20955. (interactive)
  20956. (org-agenda-manipulate-query ?\]))
  20957. (defun org-agenda-manipulate-query-add-re ()
  20958. "Manipulate the query by adding a search regexp with positive selection.
  20959. Positive selection means, the regexp must match for selection of an entry."
  20960. (interactive)
  20961. (org-agenda-manipulate-query ?\{))
  20962. (defun org-agenda-manipulate-query-subtract-re ()
  20963. "Manipulate the query by adding a search regexp with negative selection.
  20964. Negative selection means, regexp must not match for selection of an entry."
  20965. (interactive)
  20966. (org-agenda-manipulate-query ?\}))
  20967. (defun org-agenda-manipulate-query (char)
  20968. (cond
  20969. ((eq org-agenda-type 'search)
  20970. (org-add-to-string
  20971. 'org-agenda-query-string
  20972. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20973. (?\{ . " +{}") (?\} . " -{}")))))
  20974. (setq org-agenda-redo-command
  20975. (list 'org-search-view
  20976. (+ (length org-agenda-query-string)
  20977. (if (member char '(?\{ ?\})) 0 1))
  20978. org-agenda-query-string))
  20979. (set-register org-agenda-query-register org-agenda-query-string)
  20980. (org-agenda-redo))
  20981. (t (error "Canot manipulate query for %s-type agenda buffers"
  20982. org-agenda-type))))
  20983. (defun org-add-to-string (var string)
  20984. (set var (concat (symbol-value var) string)))
  20985. (defun org-agenda-goto-date (date)
  20986. "Jump to DATE in agenda."
  20987. (interactive (list (org-read-date)))
  20988. (org-agenda-list nil date))
  20989. (defun org-agenda-goto-today ()
  20990. "Go to today."
  20991. (interactive)
  20992. (org-agenda-check-type t 'timeline 'agenda)
  20993. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20994. (cond
  20995. (tdpos (goto-char tdpos))
  20996. ((eq org-agenda-type 'agenda)
  20997. (let* ((sd (time-to-days
  20998. (time-subtract (current-time)
  20999. (list 0 (* 3600 org-extend-today-until) 0))))
  21000. (comp (org-agenda-compute-time-span sd org-agenda-span))
  21001. (org-agenda-overriding-arguments org-agenda-last-arguments))
  21002. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  21003. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  21004. (org-agenda-redo)
  21005. (org-agenda-find-same-or-today-or-agenda)))
  21006. (t (error "Cannot find today")))))
  21007. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  21008. (goto-char
  21009. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  21010. (text-property-any (point-min) (point-max) 'org-today t)
  21011. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  21012. (point-min))))
  21013. (defun org-agenda-later (arg)
  21014. "Go forward in time by thee current span.
  21015. With prefix ARG, go forward that many times the current span."
  21016. (interactive "p")
  21017. (org-agenda-check-type t 'agenda)
  21018. (let* ((span org-agenda-span)
  21019. (sd org-starting-day)
  21020. (greg (calendar-gregorian-from-absolute sd))
  21021. (cnt (get-text-property (point) 'org-day-cnt))
  21022. greg2 nd)
  21023. (cond
  21024. ((eq span 'day)
  21025. (setq sd (+ arg sd) nd 1))
  21026. ((eq span 'week)
  21027. (setq sd (+ (* 7 arg) sd) nd 7))
  21028. ((eq span 'month)
  21029. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  21030. sd (calendar-absolute-from-gregorian greg2))
  21031. (setcar greg2 (1+ (car greg2)))
  21032. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  21033. ((eq span 'year)
  21034. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  21035. sd (calendar-absolute-from-gregorian greg2))
  21036. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  21037. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  21038. (let ((org-agenda-overriding-arguments
  21039. (list (car org-agenda-last-arguments) sd nd t)))
  21040. (org-agenda-redo)
  21041. (org-agenda-find-same-or-today-or-agenda cnt))))
  21042. (defun org-agenda-earlier (arg)
  21043. "Go backward in time by the current span.
  21044. With prefix ARG, go backward that many times the current span."
  21045. (interactive "p")
  21046. (org-agenda-later (- arg)))
  21047. (defun org-agenda-day-view ()
  21048. "Switch to daily view for agenda."
  21049. (interactive)
  21050. (setq org-agenda-ndays 1)
  21051. (org-agenda-change-time-span 'day))
  21052. (defun org-agenda-week-view ()
  21053. "Switch to daily view for agenda."
  21054. (interactive)
  21055. (setq org-agenda-ndays 7)
  21056. (org-agenda-change-time-span 'week))
  21057. (defun org-agenda-month-view ()
  21058. "Switch to daily view for agenda."
  21059. (interactive)
  21060. (org-agenda-change-time-span 'month))
  21061. (defun org-agenda-year-view ()
  21062. "Switch to daily view for agenda."
  21063. (interactive)
  21064. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  21065. (org-agenda-change-time-span 'year)
  21066. (error "Abort")))
  21067. (defun org-agenda-change-time-span (span)
  21068. "Change the agenda view to SPAN.
  21069. SPAN may be `day', `week', `month', `year'."
  21070. (org-agenda-check-type t 'agenda)
  21071. (if (equal org-agenda-span span)
  21072. (error "Viewing span is already \"%s\"" span))
  21073. (let* ((sd (or (get-text-property (point) 'day)
  21074. org-starting-day))
  21075. (computed (org-agenda-compute-time-span sd span))
  21076. (org-agenda-overriding-arguments
  21077. (list (car org-agenda-last-arguments)
  21078. (car computed) (cdr computed) t)))
  21079. (org-agenda-redo)
  21080. (org-agenda-find-same-or-today-or-agenda))
  21081. (org-agenda-set-mode-name)
  21082. (message "Switched to %s view" span))
  21083. (defun org-agenda-compute-time-span (sd span)
  21084. "Compute starting date and number of days for agenda.
  21085. SPAN may be `day', `week', `month', `year'. The return value
  21086. is a cons cell with the starting date and the number of days,
  21087. so that the date SD will be in that range."
  21088. (let* ((greg (calendar-gregorian-from-absolute sd))
  21089. nd)
  21090. (cond
  21091. ((eq span 'day)
  21092. (setq nd 1))
  21093. ((eq span 'week)
  21094. (let* ((nt (calendar-day-of-week
  21095. (calendar-gregorian-from-absolute sd)))
  21096. (d (if org-agenda-start-on-weekday
  21097. (- nt org-agenda-start-on-weekday)
  21098. 0)))
  21099. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  21100. (setq nd 7)))
  21101. ((eq span 'month)
  21102. (setq sd (calendar-absolute-from-gregorian
  21103. (list (car greg) 1 (nth 2 greg)))
  21104. nd (- (calendar-absolute-from-gregorian
  21105. (list (1+ (car greg)) 1 (nth 2 greg)))
  21106. sd)))
  21107. ((eq span 'year)
  21108. (setq sd (calendar-absolute-from-gregorian
  21109. (list 1 1 (nth 2 greg)))
  21110. nd (- (calendar-absolute-from-gregorian
  21111. (list 1 1 (1+ (nth 2 greg))))
  21112. sd))))
  21113. (cons sd nd)))
  21114. ;; FIXME: does not work if user makes date format that starts with a blank
  21115. (defun org-agenda-next-date-line (&optional arg)
  21116. "Jump to the next line indicating a date in agenda buffer."
  21117. (interactive "p")
  21118. (org-agenda-check-type t 'agenda 'timeline)
  21119. (beginning-of-line 1)
  21120. (if (looking-at "^\\S-") (forward-char 1))
  21121. (if (not (re-search-forward "^\\S-" nil t arg))
  21122. (progn
  21123. (backward-char 1)
  21124. (error "No next date after this line in this buffer")))
  21125. (goto-char (match-beginning 0)))
  21126. (defun org-agenda-previous-date-line (&optional arg)
  21127. "Jump to the previous line indicating a date in agenda buffer."
  21128. (interactive "p")
  21129. (org-agenda-check-type t 'agenda 'timeline)
  21130. (beginning-of-line 1)
  21131. (if (not (re-search-backward "^\\S-" nil t arg))
  21132. (error "No previous date before this line in this buffer")))
  21133. ;; Initialize the highlight
  21134. (defvar org-hl (org-make-overlay 1 1))
  21135. (org-overlay-put org-hl 'face 'highlight)
  21136. (defun org-highlight (begin end &optional buffer)
  21137. "Highlight a region with overlay."
  21138. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  21139. org-hl begin end (or buffer (current-buffer))))
  21140. (defun org-unhighlight ()
  21141. "Detach overlay INDEX."
  21142. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  21143. ;; FIXME this is currently not used.
  21144. (defun org-highlight-until-next-command (beg end &optional buffer)
  21145. (org-highlight beg end buffer)
  21146. (add-hook 'pre-command-hook 'org-unhighlight-once))
  21147. (defun org-unhighlight-once ()
  21148. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  21149. (org-unhighlight))
  21150. (defun org-agenda-follow-mode ()
  21151. "Toggle follow mode in an agenda buffer."
  21152. (interactive)
  21153. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  21154. (org-agenda-set-mode-name)
  21155. (message "Follow mode is %s"
  21156. (if org-agenda-follow-mode "on" "off")))
  21157. (defun org-agenda-log-mode ()
  21158. "Toggle log mode in an agenda buffer."
  21159. (interactive)
  21160. (org-agenda-check-type t 'agenda 'timeline)
  21161. (setq org-agenda-show-log (not org-agenda-show-log))
  21162. (org-agenda-set-mode-name)
  21163. (org-agenda-redo)
  21164. (message "Log mode is %s"
  21165. (if org-agenda-show-log "on" "off")))
  21166. (defun org-agenda-toggle-diary ()
  21167. "Toggle diary inclusion in an agenda buffer."
  21168. (interactive)
  21169. (org-agenda-check-type t 'agenda)
  21170. (setq org-agenda-include-diary (not org-agenda-include-diary))
  21171. (org-agenda-redo)
  21172. (org-agenda-set-mode-name)
  21173. (message "Diary inclusion turned %s"
  21174. (if org-agenda-include-diary "on" "off")))
  21175. (defun org-agenda-toggle-time-grid ()
  21176. "Toggle time grid in an agenda buffer."
  21177. (interactive)
  21178. (org-agenda-check-type t 'agenda)
  21179. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  21180. (org-agenda-redo)
  21181. (org-agenda-set-mode-name)
  21182. (message "Time-grid turned %s"
  21183. (if org-agenda-use-time-grid "on" "off")))
  21184. (defun org-agenda-set-mode-name ()
  21185. "Set the mode name to indicate all the small mode settings."
  21186. (setq mode-name
  21187. (concat "Org-Agenda"
  21188. (if (equal org-agenda-ndays 1) " Day" "")
  21189. (if (equal org-agenda-ndays 7) " Week" "")
  21190. (if org-agenda-follow-mode " Follow" "")
  21191. (if org-agenda-include-diary " Diary" "")
  21192. (if org-agenda-use-time-grid " Grid" "")
  21193. (if org-agenda-show-log " Log" "")))
  21194. (force-mode-line-update))
  21195. (defun org-agenda-post-command-hook ()
  21196. (and (eolp) (not (bolp)) (backward-char 1))
  21197. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21198. (if (and org-agenda-follow-mode
  21199. (get-text-property (point) 'org-marker))
  21200. (org-agenda-show)))
  21201. (defun org-agenda-show-priority ()
  21202. "Show the priority of the current item.
  21203. This priority is composed of the main priority given with the [#A] cookies,
  21204. and by additional input from the age of a schedules or deadline entry."
  21205. (interactive)
  21206. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21207. (message "Priority is %d" (if pri pri -1000))))
  21208. (defun org-agenda-show-tags ()
  21209. "Show the tags applicable to the current item."
  21210. (interactive)
  21211. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21212. (if tags
  21213. (message "Tags are :%s:"
  21214. (org-no-properties (mapconcat 'identity tags ":")))
  21215. (message "No tags associated with this line"))))
  21216. (defun org-agenda-goto (&optional highlight)
  21217. "Go to the Org-mode file which contains the item at point."
  21218. (interactive)
  21219. (let* ((marker (or (get-text-property (point) 'org-marker)
  21220. (org-agenda-error)))
  21221. (buffer (marker-buffer marker))
  21222. (pos (marker-position marker)))
  21223. (switch-to-buffer-other-window buffer)
  21224. (widen)
  21225. (goto-char pos)
  21226. (when (org-mode-p)
  21227. (org-show-context 'agenda)
  21228. (save-excursion
  21229. (and (outline-next-heading)
  21230. (org-flag-heading nil)))) ; show the next heading
  21231. (recenter (/ (window-height) 2))
  21232. (run-hooks 'org-agenda-after-show-hook)
  21233. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21234. (defvar org-agenda-after-show-hook nil
  21235. "Normal hook run after an item has been shown from the agenda.
  21236. Point is in the buffer where the item originated.")
  21237. (defun org-agenda-kill ()
  21238. "Kill the entry or subtree belonging to the current agenda entry."
  21239. (interactive)
  21240. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21241. (let* ((marker (or (get-text-property (point) 'org-marker)
  21242. (org-agenda-error)))
  21243. (buffer (marker-buffer marker))
  21244. (pos (marker-position marker))
  21245. (type (get-text-property (point) 'type))
  21246. dbeg dend (n 0) conf)
  21247. (org-with-remote-undo buffer
  21248. (with-current-buffer buffer
  21249. (save-excursion
  21250. (goto-char pos)
  21251. (if (and (org-mode-p) (not (member type '("sexp"))))
  21252. (setq dbeg (progn (org-back-to-heading t) (point))
  21253. dend (org-end-of-subtree t t))
  21254. (setq dbeg (point-at-bol)
  21255. dend (min (point-max) (1+ (point-at-eol)))))
  21256. (goto-char dbeg)
  21257. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21258. (setq conf (or (eq t org-agenda-confirm-kill)
  21259. (and (numberp org-agenda-confirm-kill)
  21260. (> n org-agenda-confirm-kill))))
  21261. (and conf
  21262. (not (y-or-n-p
  21263. (format "Delete entry with %d lines in buffer \"%s\"? "
  21264. n (buffer-name buffer))))
  21265. (error "Abort"))
  21266. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21267. (with-current-buffer buffer (delete-region dbeg dend))
  21268. (message "Agenda item and source killed"))))
  21269. (defun org-agenda-archive ()
  21270. "Kill the entry or subtree belonging to the current agenda entry."
  21271. (interactive)
  21272. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21273. (let* ((marker (or (get-text-property (point) 'org-marker)
  21274. (org-agenda-error)))
  21275. (buffer (marker-buffer marker))
  21276. (pos (marker-position marker)))
  21277. (org-with-remote-undo buffer
  21278. (with-current-buffer buffer
  21279. (if (org-mode-p)
  21280. (save-excursion
  21281. (goto-char pos)
  21282. (org-remove-subtree-entries-from-agenda)
  21283. (org-back-to-heading t)
  21284. (org-archive-subtree))
  21285. (error "Archiving works only in Org-mode files"))))))
  21286. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21287. "Remove all lines in the agenda that correspond to a given subtree.
  21288. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21289. If this information is not given, the function uses the tree at point."
  21290. (let ((buf (or buf (current-buffer))) m p)
  21291. (save-excursion
  21292. (unless (and beg end)
  21293. (org-back-to-heading t)
  21294. (setq beg (point))
  21295. (org-end-of-subtree t)
  21296. (setq end (point)))
  21297. (set-buffer (get-buffer org-agenda-buffer-name))
  21298. (save-excursion
  21299. (goto-char (point-max))
  21300. (beginning-of-line 1)
  21301. (while (not (bobp))
  21302. (when (and (setq m (get-text-property (point) 'org-marker))
  21303. (equal buf (marker-buffer m))
  21304. (setq p (marker-position m))
  21305. (>= p beg)
  21306. (<= p end))
  21307. (let ((inhibit-read-only t))
  21308. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21309. (beginning-of-line 0))))))
  21310. (defun org-agenda-open-link ()
  21311. "Follow the link in the current line, if any."
  21312. (interactive)
  21313. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21314. (save-excursion
  21315. (save-restriction
  21316. (narrow-to-region (point-at-bol) (point-at-eol))
  21317. (org-open-at-point))))
  21318. (defun org-agenda-copy-local-variable (var)
  21319. "Get a variable from a referenced buffer and install it here."
  21320. (let ((m (get-text-property (point) 'org-marker)))
  21321. (when (and m (buffer-live-p (marker-buffer m)))
  21322. (org-set-local var (with-current-buffer (marker-buffer m)
  21323. (symbol-value var))))))
  21324. (defun org-agenda-switch-to (&optional delete-other-windows)
  21325. "Go to the Org-mode file which contains the item at point."
  21326. (interactive)
  21327. (let* ((marker (or (get-text-property (point) 'org-marker)
  21328. (org-agenda-error)))
  21329. (buffer (marker-buffer marker))
  21330. (pos (marker-position marker)))
  21331. (switch-to-buffer buffer)
  21332. (and delete-other-windows (delete-other-windows))
  21333. (widen)
  21334. (goto-char pos)
  21335. (when (org-mode-p)
  21336. (org-show-context 'agenda)
  21337. (save-excursion
  21338. (and (outline-next-heading)
  21339. (org-flag-heading nil)))))) ; show the next heading
  21340. (defun org-agenda-goto-mouse (ev)
  21341. "Go to the Org-mode file which contains the item at the mouse click."
  21342. (interactive "e")
  21343. (mouse-set-point ev)
  21344. (org-agenda-goto))
  21345. (defun org-agenda-show ()
  21346. "Display the Org-mode file which contains the item at point."
  21347. (interactive)
  21348. (let ((win (selected-window)))
  21349. (org-agenda-goto t)
  21350. (select-window win)))
  21351. (defun org-agenda-recenter (arg)
  21352. "Display the Org-mode file which contains the item at point and recenter."
  21353. (interactive "P")
  21354. (let ((win (selected-window)))
  21355. (org-agenda-goto t)
  21356. (recenter arg)
  21357. (select-window win)))
  21358. (defun org-agenda-show-mouse (ev)
  21359. "Display the Org-mode file which contains the item at the mouse click."
  21360. (interactive "e")
  21361. (mouse-set-point ev)
  21362. (org-agenda-show))
  21363. (defun org-agenda-check-no-diary ()
  21364. "Check if the entry is a diary link and abort if yes."
  21365. (if (get-text-property (point) 'org-agenda-diary-link)
  21366. (org-agenda-error)))
  21367. (defun org-agenda-error ()
  21368. (error "Command not allowed in this line"))
  21369. (defun org-agenda-tree-to-indirect-buffer ()
  21370. "Show the subtree corresponding to the current entry in an indirect buffer.
  21371. This calls the command `org-tree-to-indirect-buffer' from the original
  21372. Org-mode buffer.
  21373. With numerical prefix arg ARG, go up to this level and then take that tree.
  21374. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21375. dedicated frame)."
  21376. (interactive)
  21377. (org-agenda-check-no-diary)
  21378. (let* ((marker (or (get-text-property (point) 'org-marker)
  21379. (org-agenda-error)))
  21380. (buffer (marker-buffer marker))
  21381. (pos (marker-position marker)))
  21382. (with-current-buffer buffer
  21383. (save-excursion
  21384. (goto-char pos)
  21385. (call-interactively 'org-tree-to-indirect-buffer)))))
  21386. (defvar org-last-heading-marker (make-marker)
  21387. "Marker pointing to the headline that last changed its TODO state
  21388. by a remote command from the agenda.")
  21389. (defun org-agenda-todo-nextset ()
  21390. "Switch TODO entry to next sequence."
  21391. (interactive)
  21392. (org-agenda-todo 'nextset))
  21393. (defun org-agenda-todo-previousset ()
  21394. "Switch TODO entry to previous sequence."
  21395. (interactive)
  21396. (org-agenda-todo 'previousset))
  21397. (defun org-agenda-todo (&optional arg)
  21398. "Cycle TODO state of line at point, also in Org-mode file.
  21399. This changes the line at point, all other lines in the agenda referring to
  21400. the same tree node, and the headline of the tree node in the Org-mode file."
  21401. (interactive "P")
  21402. (org-agenda-check-no-diary)
  21403. (let* ((col (current-column))
  21404. (marker (or (get-text-property (point) 'org-marker)
  21405. (org-agenda-error)))
  21406. (buffer (marker-buffer marker))
  21407. (pos (marker-position marker))
  21408. (hdmarker (get-text-property (point) 'org-hd-marker))
  21409. (inhibit-read-only t)
  21410. newhead)
  21411. (org-with-remote-undo buffer
  21412. (with-current-buffer buffer
  21413. (widen)
  21414. (goto-char pos)
  21415. (org-show-context 'agenda)
  21416. (save-excursion
  21417. (and (outline-next-heading)
  21418. (org-flag-heading nil))) ; show the next heading
  21419. (org-todo arg)
  21420. (and (bolp) (forward-char 1))
  21421. (setq newhead (org-get-heading))
  21422. (save-excursion
  21423. (org-back-to-heading)
  21424. (move-marker org-last-heading-marker (point))))
  21425. (beginning-of-line 1)
  21426. (save-excursion
  21427. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21428. (move-to-column col))))
  21429. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21430. "Change all lines in the agenda buffer which match HDMARKER.
  21431. The new content of the line will be NEWHEAD (as modified by
  21432. `org-format-agenda-item'). HDMARKER is checked with
  21433. `equal' against all `org-hd-marker' text properties in the file.
  21434. If FIXFACE is non-nil, the face of each item is modified acording to
  21435. the new TODO state."
  21436. (let* ((inhibit-read-only t)
  21437. props m pl undone-face done-face finish new dotime cat tags)
  21438. (save-excursion
  21439. (goto-char (point-max))
  21440. (beginning-of-line 1)
  21441. (while (not finish)
  21442. (setq finish (bobp))
  21443. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21444. (equal m hdmarker))
  21445. (setq props (text-properties-at (point))
  21446. dotime (get-text-property (point) 'dotime)
  21447. cat (get-text-property (point) 'org-category)
  21448. tags (get-text-property (point) 'tags)
  21449. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21450. pl (get-text-property (point) 'prefix-length)
  21451. undone-face (get-text-property (point) 'undone-face)
  21452. done-face (get-text-property (point) 'done-face))
  21453. (move-to-column pl)
  21454. (cond
  21455. ((equal new "")
  21456. (beginning-of-line 1)
  21457. (and (looking-at ".*\n?") (replace-match "")))
  21458. ((looking-at ".*")
  21459. (replace-match new t t)
  21460. (beginning-of-line 1)
  21461. (add-text-properties (point-at-bol) (point-at-eol) props)
  21462. (when fixface
  21463. (add-text-properties
  21464. (point-at-bol) (point-at-eol)
  21465. (list 'face
  21466. (if org-last-todo-state-is-todo
  21467. undone-face done-face))))
  21468. (org-agenda-highlight-todo 'line)
  21469. (beginning-of-line 1))
  21470. (t (error "Line update did not work"))))
  21471. (beginning-of-line 0)))
  21472. (org-finalize-agenda)))
  21473. (defun org-agenda-align-tags (&optional line)
  21474. "Align all tags in agenda items to `org-agenda-tags-column'."
  21475. (let ((inhibit-read-only t) l c)
  21476. (save-excursion
  21477. (goto-char (if line (point-at-bol) (point-min)))
  21478. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21479. (if line (point-at-eol) nil) t)
  21480. (add-text-properties
  21481. (match-beginning 2) (match-end 2)
  21482. (list 'face (delq nil (list 'org-tag (get-text-property
  21483. (match-beginning 2) 'face)))))
  21484. (setq l (- (match-end 2) (match-beginning 2))
  21485. c (if (< org-agenda-tags-column 0)
  21486. (- (abs org-agenda-tags-column) l)
  21487. org-agenda-tags-column))
  21488. (delete-region (match-beginning 1) (match-end 1))
  21489. (goto-char (match-beginning 1))
  21490. (insert (org-add-props
  21491. (make-string (max 1 (- c (current-column))) ?\ )
  21492. (text-properties-at (point))))))))
  21493. (defun org-agenda-priority-up ()
  21494. "Increase the priority of line at point, also in Org-mode file."
  21495. (interactive)
  21496. (org-agenda-priority 'up))
  21497. (defun org-agenda-priority-down ()
  21498. "Decrease the priority of line at point, also in Org-mode file."
  21499. (interactive)
  21500. (org-agenda-priority 'down))
  21501. (defun org-agenda-priority (&optional force-direction)
  21502. "Set the priority of line at point, also in Org-mode file.
  21503. This changes the line at point, all other lines in the agenda referring to
  21504. the same tree node, and the headline of the tree node in the Org-mode file."
  21505. (interactive)
  21506. (org-agenda-check-no-diary)
  21507. (let* ((marker (or (get-text-property (point) 'org-marker)
  21508. (org-agenda-error)))
  21509. (hdmarker (get-text-property (point) 'org-hd-marker))
  21510. (buffer (marker-buffer hdmarker))
  21511. (pos (marker-position hdmarker))
  21512. (inhibit-read-only t)
  21513. newhead)
  21514. (org-with-remote-undo buffer
  21515. (with-current-buffer buffer
  21516. (widen)
  21517. (goto-char pos)
  21518. (org-show-context 'agenda)
  21519. (save-excursion
  21520. (and (outline-next-heading)
  21521. (org-flag-heading nil))) ; show the next heading
  21522. (funcall 'org-priority force-direction)
  21523. (end-of-line 1)
  21524. (setq newhead (org-get-heading)))
  21525. (org-agenda-change-all-lines newhead hdmarker)
  21526. (beginning-of-line 1))))
  21527. (defun org-get-tags-at (&optional pos)
  21528. "Get a list of all headline tags applicable at POS.
  21529. POS defaults to point. If tags are inherited, the list contains
  21530. the targets in the same sequence as the headlines appear, i.e.
  21531. the tags of the current headline come last."
  21532. (interactive)
  21533. (let (tags lastpos)
  21534. (save-excursion
  21535. (save-restriction
  21536. (widen)
  21537. (goto-char (or pos (point)))
  21538. (save-match-data
  21539. (condition-case nil
  21540. (progn
  21541. (org-back-to-heading t)
  21542. (while (not (equal lastpos (point)))
  21543. (setq lastpos (point))
  21544. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21545. (setq tags (append (org-split-string
  21546. (org-match-string-no-properties 1) ":")
  21547. tags)))
  21548. (or org-use-tag-inheritance (error ""))
  21549. (org-up-heading-all 1)))
  21550. (error nil))))
  21551. tags)))
  21552. ;; FIXME: should fix the tags property of the agenda line.
  21553. (defun org-agenda-set-tags ()
  21554. "Set tags for the current headline."
  21555. (interactive)
  21556. (org-agenda-check-no-diary)
  21557. (if (and (org-region-active-p) (interactive-p))
  21558. (call-interactively 'org-change-tag-in-region)
  21559. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21560. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21561. (org-agenda-error)))
  21562. (buffer (marker-buffer hdmarker))
  21563. (pos (marker-position hdmarker))
  21564. (inhibit-read-only t)
  21565. newhead)
  21566. (org-with-remote-undo buffer
  21567. (with-current-buffer buffer
  21568. (widen)
  21569. (goto-char pos)
  21570. (save-excursion
  21571. (org-show-context 'agenda))
  21572. (save-excursion
  21573. (and (outline-next-heading)
  21574. (org-flag-heading nil))) ; show the next heading
  21575. (goto-char pos)
  21576. (call-interactively 'org-set-tags)
  21577. (end-of-line 1)
  21578. (setq newhead (org-get-heading)))
  21579. (org-agenda-change-all-lines newhead hdmarker)
  21580. (beginning-of-line 1)))))
  21581. (defun org-agenda-toggle-archive-tag ()
  21582. "Toggle the archive tag for the current entry."
  21583. (interactive)
  21584. (org-agenda-check-no-diary)
  21585. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21586. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21587. (org-agenda-error)))
  21588. (buffer (marker-buffer hdmarker))
  21589. (pos (marker-position hdmarker))
  21590. (inhibit-read-only t)
  21591. newhead)
  21592. (org-with-remote-undo buffer
  21593. (with-current-buffer buffer
  21594. (widen)
  21595. (goto-char pos)
  21596. (org-show-context 'agenda)
  21597. (save-excursion
  21598. (and (outline-next-heading)
  21599. (org-flag-heading nil))) ; show the next heading
  21600. (call-interactively 'org-toggle-archive-tag)
  21601. (end-of-line 1)
  21602. (setq newhead (org-get-heading)))
  21603. (org-agenda-change-all-lines newhead hdmarker)
  21604. (beginning-of-line 1))))
  21605. (defun org-agenda-date-later (arg &optional what)
  21606. "Change the date of this item to one day later."
  21607. (interactive "p")
  21608. (org-agenda-check-type t 'agenda 'timeline)
  21609. (org-agenda-check-no-diary)
  21610. (let* ((marker (or (get-text-property (point) 'org-marker)
  21611. (org-agenda-error)))
  21612. (buffer (marker-buffer marker))
  21613. (pos (marker-position marker)))
  21614. (org-with-remote-undo buffer
  21615. (with-current-buffer buffer
  21616. (widen)
  21617. (goto-char pos)
  21618. (if (not (org-at-timestamp-p))
  21619. (error "Cannot find time stamp"))
  21620. (org-timestamp-change arg (or what 'day)))
  21621. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21622. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21623. (defun org-agenda-date-earlier (arg &optional what)
  21624. "Change the date of this item to one day earlier."
  21625. (interactive "p")
  21626. (org-agenda-date-later (- arg) what))
  21627. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21628. "Show new date stamp via text properties."
  21629. ;; We use text properties to make this undoable
  21630. (let ((inhibit-read-only t))
  21631. (setq stamp (concat " " prefix " => " stamp))
  21632. (save-excursion
  21633. (goto-char (point-max))
  21634. (while (not (bobp))
  21635. (when (equal marker (get-text-property (point) 'org-marker))
  21636. (move-to-column (- (window-width) (length stamp)) t)
  21637. (if (featurep 'xemacs)
  21638. ;; Use `duplicable' property to trigger undo recording
  21639. (let ((ex (make-extent nil nil))
  21640. (gl (make-glyph stamp)))
  21641. (set-glyph-face gl 'secondary-selection)
  21642. (set-extent-properties
  21643. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21644. (insert-extent ex (1- (point)) (point-at-eol)))
  21645. (add-text-properties
  21646. (1- (point)) (point-at-eol)
  21647. (list 'display (org-add-props stamp nil
  21648. 'face 'secondary-selection))))
  21649. (beginning-of-line 1))
  21650. (beginning-of-line 0)))))
  21651. (defun org-agenda-date-prompt (arg)
  21652. "Change the date of this item. Date is prompted for, with default today.
  21653. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21654. be used to request time specification in the time stamp."
  21655. (interactive "P")
  21656. (org-agenda-check-type t 'agenda 'timeline)
  21657. (org-agenda-check-no-diary)
  21658. (let* ((marker (or (get-text-property (point) 'org-marker)
  21659. (org-agenda-error)))
  21660. (buffer (marker-buffer marker))
  21661. (pos (marker-position marker)))
  21662. (org-with-remote-undo buffer
  21663. (with-current-buffer buffer
  21664. (widen)
  21665. (goto-char pos)
  21666. (if (not (org-at-timestamp-p))
  21667. (error "Cannot find time stamp"))
  21668. (org-time-stamp arg)
  21669. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21670. (defun org-agenda-schedule (arg)
  21671. "Schedule the item at point."
  21672. (interactive "P")
  21673. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21674. (org-agenda-check-no-diary)
  21675. (let* ((marker (or (get-text-property (point) 'org-marker)
  21676. (org-agenda-error)))
  21677. (type (marker-insertion-type marker))
  21678. (buffer (marker-buffer marker))
  21679. (pos (marker-position marker))
  21680. (org-insert-labeled-timestamps-at-point nil)
  21681. ts)
  21682. (when type (message "%s" type) (sit-for 3))
  21683. (set-marker-insertion-type marker t)
  21684. (org-with-remote-undo buffer
  21685. (with-current-buffer buffer
  21686. (widen)
  21687. (goto-char pos)
  21688. (setq ts (org-schedule arg)))
  21689. (org-agenda-show-new-time marker ts "S"))
  21690. (message "Item scheduled for %s" ts)))
  21691. (defun org-agenda-deadline (arg)
  21692. "Schedule the item at point."
  21693. (interactive "P")
  21694. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21695. (org-agenda-check-no-diary)
  21696. (let* ((marker (or (get-text-property (point) 'org-marker)
  21697. (org-agenda-error)))
  21698. (buffer (marker-buffer marker))
  21699. (pos (marker-position marker))
  21700. (org-insert-labeled-timestamps-at-point nil)
  21701. ts)
  21702. (org-with-remote-undo buffer
  21703. (with-current-buffer buffer
  21704. (widen)
  21705. (goto-char pos)
  21706. (setq ts (org-deadline arg)))
  21707. (org-agenda-show-new-time marker ts "S"))
  21708. (message "Deadline for this item set to %s" ts)))
  21709. (defun org-get-heading (&optional no-tags)
  21710. "Return the heading of the current entry, without the stars."
  21711. (save-excursion
  21712. (org-back-to-heading t)
  21713. (if (looking-at
  21714. (if no-tags
  21715. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21716. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21717. (match-string 1) "")))
  21718. (defun org-agenda-clock-in (&optional arg)
  21719. "Start the clock on the currently selected item."
  21720. (interactive "P")
  21721. (org-agenda-check-no-diary)
  21722. (let* ((marker (or (get-text-property (point) 'org-marker)
  21723. (org-agenda-error)))
  21724. (pos (marker-position marker)))
  21725. (org-with-remote-undo (marker-buffer marker)
  21726. (with-current-buffer (marker-buffer marker)
  21727. (widen)
  21728. (goto-char pos)
  21729. (org-clock-in)))))
  21730. (defun org-agenda-clock-out (&optional arg)
  21731. "Stop the currently running clock."
  21732. (interactive "P")
  21733. (unless (marker-buffer org-clock-marker)
  21734. (error "No running clock"))
  21735. (org-with-remote-undo (marker-buffer org-clock-marker)
  21736. (org-clock-out)))
  21737. (defun org-agenda-clock-cancel (&optional arg)
  21738. "Cancel the currently running clock."
  21739. (interactive "P")
  21740. (unless (marker-buffer org-clock-marker)
  21741. (error "No running clock"))
  21742. (org-with-remote-undo (marker-buffer org-clock-marker)
  21743. (org-clock-cancel)))
  21744. (defun org-agenda-diary-entry ()
  21745. "Make a diary entry, like the `i' command from the calendar.
  21746. All the standard commands work: block, weekly etc."
  21747. (interactive)
  21748. (org-agenda-check-type t 'agenda 'timeline)
  21749. (require 'diary-lib)
  21750. (let* ((char (progn
  21751. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21752. (read-char-exclusive)))
  21753. (cmd (cdr (assoc char
  21754. '((?d . insert-diary-entry)
  21755. (?w . insert-weekly-diary-entry)
  21756. (?m . insert-monthly-diary-entry)
  21757. (?y . insert-yearly-diary-entry)
  21758. (?a . insert-anniversary-diary-entry)
  21759. (?b . insert-block-diary-entry)
  21760. (?c . insert-cyclic-diary-entry)))))
  21761. (oldf (symbol-function 'calendar-cursor-to-date))
  21762. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21763. (point (point))
  21764. (mark (or (mark t) (point))))
  21765. (unless cmd
  21766. (error "No command associated with <%c>" char))
  21767. (unless (and (get-text-property point 'day)
  21768. (or (not (equal ?b char))
  21769. (get-text-property mark 'day)))
  21770. (error "Don't know which date to use for diary entry"))
  21771. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21772. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21773. (let ((calendar-mark-ring
  21774. (list (calendar-gregorian-from-absolute
  21775. (or (get-text-property mark 'day)
  21776. (get-text-property point 'day))))))
  21777. (unwind-protect
  21778. (progn
  21779. (fset 'calendar-cursor-to-date
  21780. (lambda (&optional error)
  21781. (calendar-gregorian-from-absolute
  21782. (get-text-property point 'day))))
  21783. (call-interactively cmd))
  21784. (fset 'calendar-cursor-to-date oldf)))))
  21785. (defun org-agenda-execute-calendar-command (cmd)
  21786. "Execute a calendar command from the agenda, with the date associated to
  21787. the cursor position."
  21788. (org-agenda-check-type t 'agenda 'timeline)
  21789. (require 'diary-lib)
  21790. (unless (get-text-property (point) 'day)
  21791. (error "Don't know which date to use for calendar command"))
  21792. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21793. (point (point))
  21794. (date (calendar-gregorian-from-absolute
  21795. (get-text-property point 'day)))
  21796. ;; the following 3 vars are needed in the calendar
  21797. (displayed-day (extract-calendar-day date))
  21798. (displayed-month (extract-calendar-month date))
  21799. (displayed-year (extract-calendar-year date)))
  21800. (unwind-protect
  21801. (progn
  21802. (fset 'calendar-cursor-to-date
  21803. (lambda (&optional error)
  21804. (calendar-gregorian-from-absolute
  21805. (get-text-property point 'day))))
  21806. (call-interactively cmd))
  21807. (fset 'calendar-cursor-to-date oldf))))
  21808. (defun org-agenda-phases-of-moon ()
  21809. "Display the phases of the moon for the 3 months around the cursor date."
  21810. (interactive)
  21811. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21812. (defun org-agenda-holidays ()
  21813. "Display the holidays for the 3 months around the cursor date."
  21814. (interactive)
  21815. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21816. (defun org-agenda-sunrise-sunset (arg)
  21817. "Display sunrise and sunset for the cursor date.
  21818. Latitude and longitude can be specified with the variables
  21819. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21820. argument, latitude and longitude will be prompted for."
  21821. (interactive "P")
  21822. (let ((calendar-longitude (if arg nil calendar-longitude))
  21823. (calendar-latitude (if arg nil calendar-latitude))
  21824. (calendar-location-name
  21825. (if arg "the given coordinates" calendar-location-name)))
  21826. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21827. (defun org-agenda-goto-calendar ()
  21828. "Open the Emacs calendar with the date at the cursor."
  21829. (interactive)
  21830. (org-agenda-check-type t 'agenda 'timeline)
  21831. (let* ((day (or (get-text-property (point) 'day)
  21832. (error "Don't know which date to open in calendar")))
  21833. (date (calendar-gregorian-from-absolute day))
  21834. (calendar-move-hook nil)
  21835. (view-calendar-holidays-initially nil)
  21836. (view-diary-entries-initially nil))
  21837. (calendar)
  21838. (calendar-goto-date date)))
  21839. (defun org-calendar-goto-agenda ()
  21840. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21841. This is a command that has to be installed in `calendar-mode-map'."
  21842. (interactive)
  21843. (org-agenda-list nil (calendar-absolute-from-gregorian
  21844. (calendar-cursor-to-date))
  21845. nil))
  21846. (defun org-agenda-convert-date ()
  21847. (interactive)
  21848. (org-agenda-check-type t 'agenda 'timeline)
  21849. (let ((day (get-text-property (point) 'day))
  21850. date s)
  21851. (unless day
  21852. (error "Don't know which date to convert"))
  21853. (setq date (calendar-gregorian-from-absolute day))
  21854. (setq s (concat
  21855. "Gregorian: " (calendar-date-string date) "\n"
  21856. "ISO: " (calendar-iso-date-string date) "\n"
  21857. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21858. "Julian: " (calendar-julian-date-string date) "\n"
  21859. "Astron. JD: " (calendar-astro-date-string date)
  21860. " (Julian date number at noon UTC)\n"
  21861. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21862. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21863. "French: " (calendar-french-date-string date) "\n"
  21864. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21865. "Mayan: " (calendar-mayan-date-string date) "\n"
  21866. "Coptic: " (calendar-coptic-date-string date) "\n"
  21867. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21868. "Persian: " (calendar-persian-date-string date) "\n"
  21869. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21870. (with-output-to-temp-buffer "*Dates*"
  21871. (princ s))
  21872. (if (fboundp 'fit-window-to-buffer)
  21873. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21874. ;;;; Embedded LaTeX
  21875. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21876. "Keymap for the minor `org-cdlatex-mode'.")
  21877. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21878. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21879. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21880. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21881. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21882. (defvar org-cdlatex-texmathp-advice-is-done nil
  21883. "Flag remembering if we have applied the advice to texmathp already.")
  21884. (define-minor-mode org-cdlatex-mode
  21885. "Toggle the minor `org-cdlatex-mode'.
  21886. This mode supports entering LaTeX environment and math in LaTeX fragments
  21887. in Org-mode.
  21888. \\{org-cdlatex-mode-map}"
  21889. nil " OCDL" nil
  21890. (when org-cdlatex-mode (require 'cdlatex))
  21891. (unless org-cdlatex-texmathp-advice-is-done
  21892. (setq org-cdlatex-texmathp-advice-is-done t)
  21893. (defadvice texmathp (around org-math-always-on activate)
  21894. "Always return t in org-mode buffers.
  21895. This is because we want to insert math symbols without dollars even outside
  21896. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21897. en embedded LaTeX fragement, let texmathp do its job.
  21898. \\[org-cdlatex-mode-map]"
  21899. (interactive)
  21900. (let (p)
  21901. (cond
  21902. ((not (org-mode-p)) ad-do-it)
  21903. ((eq this-command 'cdlatex-math-symbol)
  21904. (setq ad-return-value t
  21905. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21906. (t
  21907. (let ((p (org-inside-LaTeX-fragment-p)))
  21908. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21909. (setq ad-return-value t
  21910. texmathp-why '("Org-mode embedded math" . 0))
  21911. (if p ad-do-it)))))))))
  21912. (defun turn-on-org-cdlatex ()
  21913. "Unconditionally turn on `org-cdlatex-mode'."
  21914. (org-cdlatex-mode 1))
  21915. (defun org-inside-LaTeX-fragment-p ()
  21916. "Test if point is inside a LaTeX fragment.
  21917. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21918. sequence appearing also before point.
  21919. Even though the matchers for math are configurable, this function assumes
  21920. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21921. delimiters are skipped when they have been removed by customization.
  21922. The return value is nil, or a cons cell with the delimiter and
  21923. and the position of this delimiter.
  21924. This function does a reasonably good job, but can locally be fooled by
  21925. for example currency specifications. For example it will assume being in
  21926. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21927. fragments that are properly closed, but during editing, we have to live
  21928. with the uncertainty caused by missing closing delimiters. This function
  21929. looks only before point, not after."
  21930. (catch 'exit
  21931. (let ((pos (point))
  21932. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21933. (lim (progn
  21934. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21935. (point)))
  21936. dd-on str (start 0) m re)
  21937. (goto-char pos)
  21938. (when dodollar
  21939. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21940. re (nth 1 (assoc "$" org-latex-regexps)))
  21941. (while (string-match re str start)
  21942. (cond
  21943. ((= (match-end 0) (length str))
  21944. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21945. ((= (match-end 0) (- (length str) 5))
  21946. (throw 'exit nil))
  21947. (t (setq start (match-end 0))))))
  21948. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21949. (goto-char pos)
  21950. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21951. (and (match-beginning 2) (throw 'exit nil))
  21952. ;; count $$
  21953. (while (re-search-backward "\\$\\$" lim t)
  21954. (setq dd-on (not dd-on)))
  21955. (goto-char pos)
  21956. (if dd-on (cons "$$" m))))))
  21957. (defun org-try-cdlatex-tab ()
  21958. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21959. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21960. - inside a LaTeX fragment, or
  21961. - after the first word in a line, where an abbreviation expansion could
  21962. insert a LaTeX environment."
  21963. (when org-cdlatex-mode
  21964. (cond
  21965. ((save-excursion
  21966. (skip-chars-backward "a-zA-Z0-9*")
  21967. (skip-chars-backward " \t")
  21968. (bolp))
  21969. (cdlatex-tab) t)
  21970. ((org-inside-LaTeX-fragment-p)
  21971. (cdlatex-tab) t)
  21972. (t nil))))
  21973. (defun org-cdlatex-underscore-caret (&optional arg)
  21974. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21975. Revert to the normal definition outside of these fragments."
  21976. (interactive "P")
  21977. (if (org-inside-LaTeX-fragment-p)
  21978. (call-interactively 'cdlatex-sub-superscript)
  21979. (let (org-cdlatex-mode)
  21980. (call-interactively (key-binding (vector last-input-event))))))
  21981. (defun org-cdlatex-math-modify (&optional arg)
  21982. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21983. Revert to the normal definition outside of these fragments."
  21984. (interactive "P")
  21985. (if (org-inside-LaTeX-fragment-p)
  21986. (call-interactively 'cdlatex-math-modify)
  21987. (let (org-cdlatex-mode)
  21988. (call-interactively (key-binding (vector last-input-event))))))
  21989. (defvar org-latex-fragment-image-overlays nil
  21990. "List of overlays carrying the images of latex fragments.")
  21991. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21992. (defun org-remove-latex-fragment-image-overlays ()
  21993. "Remove all overlays with LaTeX fragment images in current buffer."
  21994. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21995. (setq org-latex-fragment-image-overlays nil))
  21996. (defun org-preview-latex-fragment (&optional subtree)
  21997. "Preview the LaTeX fragment at point, or all locally or globally.
  21998. If the cursor is in a LaTeX fragment, create the image and overlay
  21999. it over the source code. If there is no fragment at point, display
  22000. all fragments in the current text, from one headline to the next. With
  22001. prefix SUBTREE, display all fragments in the current subtree. With a
  22002. double prefix `C-u C-u', or when the cursor is before the first headline,
  22003. display all fragments in the buffer.
  22004. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  22005. (interactive "P")
  22006. (org-remove-latex-fragment-image-overlays)
  22007. (save-excursion
  22008. (save-restriction
  22009. (let (beg end at msg)
  22010. (cond
  22011. ((or (equal subtree '(16))
  22012. (not (save-excursion
  22013. (re-search-backward (concat "^" outline-regexp) nil t))))
  22014. (setq beg (point-min) end (point-max)
  22015. msg "Creating images for buffer...%s"))
  22016. ((equal subtree '(4))
  22017. (org-back-to-heading)
  22018. (setq beg (point) end (org-end-of-subtree t)
  22019. msg "Creating images for subtree...%s"))
  22020. (t
  22021. (if (setq at (org-inside-LaTeX-fragment-p))
  22022. (goto-char (max (point-min) (- (cdr at) 2)))
  22023. (org-back-to-heading))
  22024. (setq beg (point) end (progn (outline-next-heading) (point))
  22025. msg (if at "Creating image...%s"
  22026. "Creating images for entry...%s"))))
  22027. (message msg "")
  22028. (narrow-to-region beg end)
  22029. (goto-char beg)
  22030. (org-format-latex
  22031. (concat "ltxpng/" (file-name-sans-extension
  22032. (file-name-nondirectory
  22033. buffer-file-name)))
  22034. default-directory 'overlays msg at 'forbuffer)
  22035. (message msg "done. Use `C-c C-c' to remove images.")))))
  22036. (defvar org-latex-regexps
  22037. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  22038. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  22039. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  22040. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  22041. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  22042. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  22043. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  22044. "Regular expressions for matching embedded LaTeX.")
  22045. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  22046. "Replace LaTeX fragments with links to an image, and produce images."
  22047. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  22048. (let* ((prefixnodir (file-name-nondirectory prefix))
  22049. (absprefix (expand-file-name prefix dir))
  22050. (todir (file-name-directory absprefix))
  22051. (opt org-format-latex-options)
  22052. (matchers (plist-get opt :matchers))
  22053. (re-list org-latex-regexps)
  22054. (cnt 0) txt link beg end re e checkdir
  22055. m n block linkfile movefile ov)
  22056. ;; Check if there are old images files with this prefix, and remove them
  22057. (when (file-directory-p todir)
  22058. (mapc 'delete-file
  22059. (directory-files
  22060. todir 'full
  22061. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  22062. ;; Check the different regular expressions
  22063. (while (setq e (pop re-list))
  22064. (setq m (car e) re (nth 1 e) n (nth 2 e)
  22065. block (if (nth 3 e) "\n\n" ""))
  22066. (when (member m matchers)
  22067. (goto-char (point-min))
  22068. (while (re-search-forward re nil t)
  22069. (when (or (not at) (equal (cdr at) (match-beginning n)))
  22070. (setq txt (match-string n)
  22071. beg (match-beginning n) end (match-end n)
  22072. cnt (1+ cnt)
  22073. linkfile (format "%s_%04d.png" prefix cnt)
  22074. movefile (format "%s_%04d.png" absprefix cnt)
  22075. link (concat block "[[file:" linkfile "]]" block))
  22076. (if msg (message msg cnt))
  22077. (goto-char beg)
  22078. (unless checkdir ; make sure the directory exists
  22079. (setq checkdir t)
  22080. (or (file-directory-p todir) (make-directory todir)))
  22081. (org-create-formula-image
  22082. txt movefile opt forbuffer)
  22083. (if overlays
  22084. (progn
  22085. (setq ov (org-make-overlay beg end))
  22086. (if (featurep 'xemacs)
  22087. (progn
  22088. (org-overlay-put ov 'invisible t)
  22089. (org-overlay-put
  22090. ov 'end-glyph
  22091. (make-glyph (vector 'png :file movefile))))
  22092. (org-overlay-put
  22093. ov 'display
  22094. (list 'image :type 'png :file movefile :ascent 'center)))
  22095. (push ov org-latex-fragment-image-overlays)
  22096. (goto-char end))
  22097. (delete-region beg end)
  22098. (insert link))))))))
  22099. ;; This function borrows from Ganesh Swami's latex2png.el
  22100. (defun org-create-formula-image (string tofile options buffer)
  22101. (let* ((tmpdir (if (featurep 'xemacs)
  22102. (temp-directory)
  22103. temporary-file-directory))
  22104. (texfilebase (make-temp-name
  22105. (expand-file-name "orgtex" tmpdir)))
  22106. (texfile (concat texfilebase ".tex"))
  22107. (dvifile (concat texfilebase ".dvi"))
  22108. (pngfile (concat texfilebase ".png"))
  22109. (fnh (face-attribute 'default :height nil))
  22110. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  22111. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  22112. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  22113. "Black"))
  22114. (bg (or (plist-get options (if buffer :background :html-background))
  22115. "Transparent")))
  22116. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  22117. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  22118. (with-temp-file texfile
  22119. (insert org-format-latex-header
  22120. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  22121. (let ((dir default-directory))
  22122. (condition-case nil
  22123. (progn
  22124. (cd tmpdir)
  22125. (call-process "latex" nil nil nil texfile))
  22126. (error nil))
  22127. (cd dir))
  22128. (if (not (file-exists-p dvifile))
  22129. (progn (message "Failed to create dvi file from %s" texfile) nil)
  22130. (call-process "dvipng" nil nil nil
  22131. "-E" "-fg" fg "-bg" bg
  22132. "-D" dpi
  22133. ;;"-x" scale "-y" scale
  22134. "-T" "tight"
  22135. "-o" pngfile
  22136. dvifile)
  22137. (if (not (file-exists-p pngfile))
  22138. (progn (message "Failed to create png file from %s" texfile) nil)
  22139. ;; Use the requested file name and clean up
  22140. (copy-file pngfile tofile 'replace)
  22141. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  22142. (delete-file (concat texfilebase e)))
  22143. pngfile))))
  22144. (defun org-dvipng-color (attr)
  22145. "Return an rgb color specification for dvipng."
  22146. (apply 'format "rgb %s %s %s"
  22147. (mapcar 'org-normalize-color
  22148. (color-values (face-attribute 'default attr nil)))))
  22149. (defun org-normalize-color (value)
  22150. "Return string to be used as color value for an RGB component."
  22151. (format "%g" (/ value 65535.0)))
  22152. ;;;; Exporting
  22153. ;;; Variables, constants, and parameter plists
  22154. (defconst org-level-max 20)
  22155. (defvar org-export-html-preamble nil
  22156. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  22157. (defvar org-export-html-postamble nil
  22158. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  22159. (defvar org-export-html-auto-preamble t
  22160. "Should default preamble be inserted? Set by publishing functions.")
  22161. (defvar org-export-html-auto-postamble t
  22162. "Should default postamble be inserted? Set by publishing functions.")
  22163. (defvar org-current-export-file nil) ; dynamically scoped parameter
  22164. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  22165. (defconst org-export-plist-vars
  22166. '((:language . org-export-default-language)
  22167. (:customtime . org-display-custom-times)
  22168. (:headline-levels . org-export-headline-levels)
  22169. (:section-numbers . org-export-with-section-numbers)
  22170. (:table-of-contents . org-export-with-toc)
  22171. (:preserve-breaks . org-export-preserve-breaks)
  22172. (:archived-trees . org-export-with-archived-trees)
  22173. (:emphasize . org-export-with-emphasize)
  22174. (:sub-superscript . org-export-with-sub-superscripts)
  22175. (:special-strings . org-export-with-special-strings)
  22176. (:footnotes . org-export-with-footnotes)
  22177. (:drawers . org-export-with-drawers)
  22178. (:tags . org-export-with-tags)
  22179. (:TeX-macros . org-export-with-TeX-macros)
  22180. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  22181. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  22182. (:fixed-width . org-export-with-fixed-width)
  22183. (:timestamps . org-export-with-timestamps)
  22184. (:author-info . org-export-author-info)
  22185. (:time-stamp-file . org-export-time-stamp-file)
  22186. (:tables . org-export-with-tables)
  22187. (:table-auto-headline . org-export-highlight-first-table-line)
  22188. (:style . org-export-html-style)
  22189. (:agenda-style . org-agenda-export-html-style)
  22190. (:convert-org-links . org-export-html-link-org-files-as-html)
  22191. (:inline-images . org-export-html-inline-images)
  22192. (:html-extension . org-export-html-extension)
  22193. (:html-table-tag . org-export-html-table-tag)
  22194. (:expand-quoted-html . org-export-html-expand)
  22195. (:timestamp . org-export-html-with-timestamp)
  22196. (:publishing-directory . org-export-publishing-directory)
  22197. (:preamble . org-export-html-preamble)
  22198. (:postamble . org-export-html-postamble)
  22199. (:auto-preamble . org-export-html-auto-preamble)
  22200. (:auto-postamble . org-export-html-auto-postamble)
  22201. (:author . user-full-name)
  22202. (:email . user-mail-address)))
  22203. (defun org-default-export-plist ()
  22204. "Return the property list with default settings for the export variables."
  22205. (let ((l org-export-plist-vars) rtn e)
  22206. (while (setq e (pop l))
  22207. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22208. rtn))
  22209. (defun org-infile-export-plist ()
  22210. "Return the property list with file-local settings for export."
  22211. (save-excursion
  22212. (save-restriction
  22213. (widen)
  22214. (goto-char 0)
  22215. (let ((re (org-make-options-regexp
  22216. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22217. p key val text options)
  22218. (while (re-search-forward re nil t)
  22219. (setq key (org-match-string-no-properties 1)
  22220. val (org-match-string-no-properties 2))
  22221. (cond
  22222. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22223. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22224. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22225. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22226. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22227. ((string-equal key "TEXT")
  22228. (setq text (if text (concat text "\n" val) val)))
  22229. ((string-equal key "OPTIONS") (setq options val))))
  22230. (setq p (plist-put p :text text))
  22231. (when options
  22232. (let ((op '(("H" . :headline-levels)
  22233. ("num" . :section-numbers)
  22234. ("toc" . :table-of-contents)
  22235. ("\\n" . :preserve-breaks)
  22236. ("@" . :expand-quoted-html)
  22237. (":" . :fixed-width)
  22238. ("|" . :tables)
  22239. ("^" . :sub-superscript)
  22240. ("-" . :special-strings)
  22241. ("f" . :footnotes)
  22242. ("d" . :drawers)
  22243. ("tags" . :tags)
  22244. ("*" . :emphasize)
  22245. ("TeX" . :TeX-macros)
  22246. ("LaTeX" . :LaTeX-fragments)
  22247. ("skip" . :skip-before-1st-heading)
  22248. ("author" . :author-info)
  22249. ("timestamp" . :time-stamp-file)))
  22250. o)
  22251. (while (setq o (pop op))
  22252. (if (string-match (concat (regexp-quote (car o))
  22253. ":\\([^ \t\n\r;,.]*\\)")
  22254. options)
  22255. (setq p (plist-put p (cdr o)
  22256. (car (read-from-string
  22257. (match-string 1 options)))))))))
  22258. p))))
  22259. (defun org-export-directory (type plist)
  22260. (let* ((val (plist-get plist :publishing-directory))
  22261. (dir (if (listp val)
  22262. (or (cdr (assoc type val)) ".")
  22263. val)))
  22264. dir))
  22265. (defun org-skip-comments (lines)
  22266. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22267. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22268. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22269. (case-fold-search nil)
  22270. rtn line level)
  22271. (while (setq line (pop lines))
  22272. (cond
  22273. ((and (string-match re1 line)
  22274. (setq level (- (match-end 1) (match-beginning 1))))
  22275. ;; Beginning of a COMMENT subtree. Skip it.
  22276. (while (and (setq line (pop lines))
  22277. (or (not (string-match re2 line))
  22278. (> (- (match-end 1) (match-beginning 1)) level))))
  22279. (setq lines (cons line lines)))
  22280. ((string-match "^#" line)
  22281. ;; an ordinary comment line
  22282. )
  22283. ((and org-export-table-remove-special-lines
  22284. (string-match "^[ \t]*|" line)
  22285. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22286. (and (string-match "| *<[0-9]+> *|" line)
  22287. (not (string-match "| *[^ <|]" line)))))
  22288. ;; a special table line that should be removed
  22289. )
  22290. (t (setq rtn (cons line rtn)))))
  22291. (nreverse rtn)))
  22292. (defun org-export (&optional arg)
  22293. (interactive)
  22294. (let ((help "[t] insert the export option template
  22295. \[v] limit export to visible part of outline tree
  22296. \[a] export as ASCII
  22297. \[h] export as HTML
  22298. \[H] export as HTML to temporary buffer
  22299. \[R] export region as HTML
  22300. \[b] export as HTML and browse immediately
  22301. \[x] export as XOXO
  22302. \[l] export as LaTeX
  22303. \[L] export as LaTeX to temporary buffer
  22304. \[i] export current file as iCalendar file
  22305. \[I] export all agenda files as iCalendar files
  22306. \[c] export agenda files into combined iCalendar file
  22307. \[F] publish current file
  22308. \[P] publish current project
  22309. \[X] publish... (project will be prompted for)
  22310. \[A] publish all projects")
  22311. (cmds
  22312. '((?t . org-insert-export-options-template)
  22313. (?v . org-export-visible)
  22314. (?a . org-export-as-ascii)
  22315. (?h . org-export-as-html)
  22316. (?b . org-export-as-html-and-open)
  22317. (?H . org-export-as-html-to-buffer)
  22318. (?R . org-export-region-as-html)
  22319. (?x . org-export-as-xoxo)
  22320. (?l . org-export-as-latex)
  22321. (?L . org-export-as-latex-to-buffer)
  22322. (?i . org-export-icalendar-this-file)
  22323. (?I . org-export-icalendar-all-agenda-files)
  22324. (?c . org-export-icalendar-combine-agenda-files)
  22325. (?F . org-publish-current-file)
  22326. (?P . org-publish-current-project)
  22327. (?X . org-publish)
  22328. (?A . org-publish-all)))
  22329. r1 r2 ass)
  22330. (save-window-excursion
  22331. (delete-other-windows)
  22332. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22333. (princ help))
  22334. (message "Select command: ")
  22335. (setq r1 (read-char-exclusive)))
  22336. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22337. (if (setq ass (assq r2 cmds))
  22338. (call-interactively (cdr ass))
  22339. (error "No command associated with key %c" r1))))
  22340. (defconst org-html-entities
  22341. '(("nbsp")
  22342. ("iexcl")
  22343. ("cent")
  22344. ("pound")
  22345. ("curren")
  22346. ("yen")
  22347. ("brvbar")
  22348. ("vert" . "&#124;")
  22349. ("sect")
  22350. ("uml")
  22351. ("copy")
  22352. ("ordf")
  22353. ("laquo")
  22354. ("not")
  22355. ("shy")
  22356. ("reg")
  22357. ("macr")
  22358. ("deg")
  22359. ("plusmn")
  22360. ("sup2")
  22361. ("sup3")
  22362. ("acute")
  22363. ("micro")
  22364. ("para")
  22365. ("middot")
  22366. ("odot"."o")
  22367. ("star"."*")
  22368. ("cedil")
  22369. ("sup1")
  22370. ("ordm")
  22371. ("raquo")
  22372. ("frac14")
  22373. ("frac12")
  22374. ("frac34")
  22375. ("iquest")
  22376. ("Agrave")
  22377. ("Aacute")
  22378. ("Acirc")
  22379. ("Atilde")
  22380. ("Auml")
  22381. ("Aring") ("AA"."&Aring;")
  22382. ("AElig")
  22383. ("Ccedil")
  22384. ("Egrave")
  22385. ("Eacute")
  22386. ("Ecirc")
  22387. ("Euml")
  22388. ("Igrave")
  22389. ("Iacute")
  22390. ("Icirc")
  22391. ("Iuml")
  22392. ("ETH")
  22393. ("Ntilde")
  22394. ("Ograve")
  22395. ("Oacute")
  22396. ("Ocirc")
  22397. ("Otilde")
  22398. ("Ouml")
  22399. ("times")
  22400. ("Oslash")
  22401. ("Ugrave")
  22402. ("Uacute")
  22403. ("Ucirc")
  22404. ("Uuml")
  22405. ("Yacute")
  22406. ("THORN")
  22407. ("szlig")
  22408. ("agrave")
  22409. ("aacute")
  22410. ("acirc")
  22411. ("atilde")
  22412. ("auml")
  22413. ("aring")
  22414. ("aelig")
  22415. ("ccedil")
  22416. ("egrave")
  22417. ("eacute")
  22418. ("ecirc")
  22419. ("euml")
  22420. ("igrave")
  22421. ("iacute")
  22422. ("icirc")
  22423. ("iuml")
  22424. ("eth")
  22425. ("ntilde")
  22426. ("ograve")
  22427. ("oacute")
  22428. ("ocirc")
  22429. ("otilde")
  22430. ("ouml")
  22431. ("divide")
  22432. ("oslash")
  22433. ("ugrave")
  22434. ("uacute")
  22435. ("ucirc")
  22436. ("uuml")
  22437. ("yacute")
  22438. ("thorn")
  22439. ("yuml")
  22440. ("fnof")
  22441. ("Alpha")
  22442. ("Beta")
  22443. ("Gamma")
  22444. ("Delta")
  22445. ("Epsilon")
  22446. ("Zeta")
  22447. ("Eta")
  22448. ("Theta")
  22449. ("Iota")
  22450. ("Kappa")
  22451. ("Lambda")
  22452. ("Mu")
  22453. ("Nu")
  22454. ("Xi")
  22455. ("Omicron")
  22456. ("Pi")
  22457. ("Rho")
  22458. ("Sigma")
  22459. ("Tau")
  22460. ("Upsilon")
  22461. ("Phi")
  22462. ("Chi")
  22463. ("Psi")
  22464. ("Omega")
  22465. ("alpha")
  22466. ("beta")
  22467. ("gamma")
  22468. ("delta")
  22469. ("epsilon")
  22470. ("varepsilon"."&epsilon;")
  22471. ("zeta")
  22472. ("eta")
  22473. ("theta")
  22474. ("iota")
  22475. ("kappa")
  22476. ("lambda")
  22477. ("mu")
  22478. ("nu")
  22479. ("xi")
  22480. ("omicron")
  22481. ("pi")
  22482. ("rho")
  22483. ("sigmaf") ("varsigma"."&sigmaf;")
  22484. ("sigma")
  22485. ("tau")
  22486. ("upsilon")
  22487. ("phi")
  22488. ("chi")
  22489. ("psi")
  22490. ("omega")
  22491. ("thetasym") ("vartheta"."&thetasym;")
  22492. ("upsih")
  22493. ("piv")
  22494. ("bull") ("bullet"."&bull;")
  22495. ("hellip") ("dots"."&hellip;")
  22496. ("prime")
  22497. ("Prime")
  22498. ("oline")
  22499. ("frasl")
  22500. ("weierp")
  22501. ("image")
  22502. ("real")
  22503. ("trade")
  22504. ("alefsym")
  22505. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22506. ("uarr") ("uparrow"."&uarr;")
  22507. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22508. ("darr")("downarrow"."&darr;")
  22509. ("harr") ("leftrightarrow"."&harr;")
  22510. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22511. ("lArr") ("Leftarrow"."&lArr;")
  22512. ("uArr") ("Uparrow"."&uArr;")
  22513. ("rArr") ("Rightarrow"."&rArr;")
  22514. ("dArr") ("Downarrow"."&dArr;")
  22515. ("hArr") ("Leftrightarrow"."&hArr;")
  22516. ("forall")
  22517. ("part") ("partial"."&part;")
  22518. ("exist") ("exists"."&exist;")
  22519. ("empty") ("emptyset"."&empty;")
  22520. ("nabla")
  22521. ("isin") ("in"."&isin;")
  22522. ("notin")
  22523. ("ni")
  22524. ("prod")
  22525. ("sum")
  22526. ("minus")
  22527. ("lowast") ("ast"."&lowast;")
  22528. ("radic")
  22529. ("prop") ("proptp"."&prop;")
  22530. ("infin") ("infty"."&infin;")
  22531. ("ang") ("angle"."&ang;")
  22532. ("and") ("wedge"."&and;")
  22533. ("or") ("vee"."&or;")
  22534. ("cap")
  22535. ("cup")
  22536. ("int")
  22537. ("there4")
  22538. ("sim")
  22539. ("cong") ("simeq"."&cong;")
  22540. ("asymp")("approx"."&asymp;")
  22541. ("ne") ("neq"."&ne;")
  22542. ("equiv")
  22543. ("le")
  22544. ("ge")
  22545. ("sub") ("subset"."&sub;")
  22546. ("sup") ("supset"."&sup;")
  22547. ("nsub")
  22548. ("sube")
  22549. ("supe")
  22550. ("oplus")
  22551. ("otimes")
  22552. ("perp")
  22553. ("sdot") ("cdot"."&sdot;")
  22554. ("lceil")
  22555. ("rceil")
  22556. ("lfloor")
  22557. ("rfloor")
  22558. ("lang")
  22559. ("rang")
  22560. ("loz") ("Diamond"."&loz;")
  22561. ("spades") ("spadesuit"."&spades;")
  22562. ("clubs") ("clubsuit"."&clubs;")
  22563. ("hearts") ("diamondsuit"."&hearts;")
  22564. ("diams") ("diamondsuit"."&diams;")
  22565. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22566. ("quot")
  22567. ("amp")
  22568. ("lt")
  22569. ("gt")
  22570. ("OElig")
  22571. ("oelig")
  22572. ("Scaron")
  22573. ("scaron")
  22574. ("Yuml")
  22575. ("circ")
  22576. ("tilde")
  22577. ("ensp")
  22578. ("emsp")
  22579. ("thinsp")
  22580. ("zwnj")
  22581. ("zwj")
  22582. ("lrm")
  22583. ("rlm")
  22584. ("ndash")
  22585. ("mdash")
  22586. ("lsquo")
  22587. ("rsquo")
  22588. ("sbquo")
  22589. ("ldquo")
  22590. ("rdquo")
  22591. ("bdquo")
  22592. ("dagger")
  22593. ("Dagger")
  22594. ("permil")
  22595. ("lsaquo")
  22596. ("rsaquo")
  22597. ("euro")
  22598. ("arccos"."arccos")
  22599. ("arcsin"."arcsin")
  22600. ("arctan"."arctan")
  22601. ("arg"."arg")
  22602. ("cos"."cos")
  22603. ("cosh"."cosh")
  22604. ("cot"."cot")
  22605. ("coth"."coth")
  22606. ("csc"."csc")
  22607. ("deg"."deg")
  22608. ("det"."det")
  22609. ("dim"."dim")
  22610. ("exp"."exp")
  22611. ("gcd"."gcd")
  22612. ("hom"."hom")
  22613. ("inf"."inf")
  22614. ("ker"."ker")
  22615. ("lg"."lg")
  22616. ("lim"."lim")
  22617. ("liminf"."liminf")
  22618. ("limsup"."limsup")
  22619. ("ln"."ln")
  22620. ("log"."log")
  22621. ("max"."max")
  22622. ("min"."min")
  22623. ("Pr"."Pr")
  22624. ("sec"."sec")
  22625. ("sin"."sin")
  22626. ("sinh"."sinh")
  22627. ("sup"."sup")
  22628. ("tan"."tan")
  22629. ("tanh"."tanh")
  22630. )
  22631. "Entities for TeX->HTML translation.
  22632. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22633. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22634. In that case, \"\\ent\" will be translated to \"&other;\".
  22635. The list contains HTML entities for Latin-1, Greek and other symbols.
  22636. It is supplemented by a number of commonly used TeX macros with appropriate
  22637. translations. There is currently no way for users to extend this.")
  22638. ;;; General functions for all backends
  22639. (defun org-cleaned-string-for-export (string &rest parameters)
  22640. "Cleanup a buffer STRING so that links can be created safely."
  22641. (interactive)
  22642. (let* ((re-radio (and org-target-link-regexp
  22643. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22644. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22645. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22646. (re-archive (concat ":" org-archive-tag ":"))
  22647. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22648. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22649. (htmlp (plist-get parameters :for-html))
  22650. (asciip (plist-get parameters :for-ascii))
  22651. (latexp (plist-get parameters :for-LaTeX))
  22652. (commentsp (plist-get parameters :comments))
  22653. (archived-trees (plist-get parameters :archived-trees))
  22654. (inhibit-read-only t)
  22655. (drawers org-drawers)
  22656. (exp-drawers (plist-get parameters :drawers))
  22657. (outline-regexp "\\*+ ")
  22658. a b xx
  22659. rtn p)
  22660. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22661. (erase-buffer)
  22662. (insert string)
  22663. ;; Remove license-to-kill stuff
  22664. (while (setq p (text-property-any (point-min) (point-max)
  22665. :org-license-to-kill t))
  22666. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22667. (let ((org-inhibit-startup t)) (org-mode))
  22668. (untabify (point-min) (point-max))
  22669. ;; Get rid of drawers
  22670. (unless (eq t exp-drawers)
  22671. (goto-char (point-min))
  22672. (let ((re (concat "^[ \t]*:\\("
  22673. (mapconcat
  22674. 'identity
  22675. (org-delete-all exp-drawers
  22676. (copy-sequence drawers))
  22677. "\\|")
  22678. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22679. (while (re-search-forward re nil t)
  22680. (replace-match ""))))
  22681. ;; Get the correct stuff before the first headline
  22682. (when (plist-get parameters :skip-before-1st-heading)
  22683. (goto-char (point-min))
  22684. (when (re-search-forward "^\\*+[ \t]" nil t)
  22685. (delete-region (point-min) (match-beginning 0))
  22686. (goto-char (point-min))
  22687. (insert "\n")))
  22688. (when (plist-get parameters :add-text)
  22689. (goto-char (point-min))
  22690. (insert (plist-get parameters :add-text) "\n"))
  22691. ;; Get rid of archived trees
  22692. (when (not (eq archived-trees t))
  22693. (goto-char (point-min))
  22694. (while (re-search-forward re-archive nil t)
  22695. (if (not (org-on-heading-p t))
  22696. (org-end-of-subtree t)
  22697. (beginning-of-line 1)
  22698. (setq a (if archived-trees
  22699. (1+ (point-at-eol)) (point))
  22700. b (org-end-of-subtree t))
  22701. (if (> b a) (delete-region a b)))))
  22702. ;; Find targets in comments and move them out of comments,
  22703. ;; but mark them as targets that should be invisible
  22704. (goto-char (point-min))
  22705. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22706. (replace-match "\\1(INVISIBLE)"))
  22707. ;; Protect backend specific stuff, throw away the others.
  22708. (let ((formatters
  22709. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22710. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22711. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22712. fmt)
  22713. (goto-char (point-min))
  22714. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22715. (goto-char (match-end 0))
  22716. (while (not (looking-at "#\\+END_EXAMPLE"))
  22717. (insert ": ")
  22718. (beginning-of-line 2)))
  22719. (goto-char (point-min))
  22720. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22721. (add-text-properties (match-beginning 0) (match-end 0)
  22722. '(org-protected t)))
  22723. (while formatters
  22724. (setq fmt (pop formatters))
  22725. (when (car fmt)
  22726. (goto-char (point-min))
  22727. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22728. ":[ \t]*\\(.*\\)") nil t)
  22729. (replace-match "\\1" t)
  22730. (add-text-properties
  22731. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22732. '(org-protected t))))
  22733. (goto-char (point-min))
  22734. (while (re-search-forward
  22735. (concat "^#\\+"
  22736. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22737. (cadddr fmt) "\\>.*\n?") nil t)
  22738. (if (car fmt)
  22739. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22740. '(org-protected t))
  22741. (delete-region (match-beginning 0) (match-end 0))))))
  22742. ;; Protect quoted subtrees
  22743. (goto-char (point-min))
  22744. (while (re-search-forward re-quote nil t)
  22745. (goto-char (match-beginning 0))
  22746. (end-of-line 1)
  22747. (add-text-properties (point) (org-end-of-subtree t)
  22748. '(org-protected t)))
  22749. ;; Protect verbatim elements
  22750. (goto-char (point-min))
  22751. (while (re-search-forward org-verbatim-re nil t)
  22752. (add-text-properties (match-beginning 4) (match-end 4)
  22753. '(org-protected t))
  22754. (goto-char (1+ (match-end 4))))
  22755. ;; Remove subtrees that are commented
  22756. (goto-char (point-min))
  22757. (while (re-search-forward re-commented nil t)
  22758. (goto-char (match-beginning 0))
  22759. (delete-region (point) (org-end-of-subtree t)))
  22760. ;; Remove special table lines
  22761. (when org-export-table-remove-special-lines
  22762. (goto-char (point-min))
  22763. (while (re-search-forward "^[ \t]*|" nil t)
  22764. (beginning-of-line 1)
  22765. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22766. (and (looking-at ".*?| *<[0-9]+> *|")
  22767. (not (looking-at ".*?| *[^ <|]"))))
  22768. (delete-region (max (point-min) (1- (point-at-bol)))
  22769. (point-at-eol))
  22770. (end-of-line 1))))
  22771. ;; Specific LaTeX stuff
  22772. (when latexp
  22773. (require 'org-export-latex nil)
  22774. (org-export-latex-cleaned-string))
  22775. (when asciip
  22776. (org-export-ascii-clean-string))
  22777. ;; Specific HTML stuff
  22778. (when htmlp
  22779. ;; Convert LaTeX fragments to images
  22780. (when (plist-get parameters :LaTeX-fragments)
  22781. (org-format-latex
  22782. (concat "ltxpng/" (file-name-sans-extension
  22783. (file-name-nondirectory
  22784. org-current-export-file)))
  22785. org-current-export-dir nil "Creating LaTeX image %s"))
  22786. (message "Exporting..."))
  22787. ;; Remove or replace comments
  22788. (goto-char (point-min))
  22789. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22790. (if commentsp
  22791. (progn (add-text-properties
  22792. (match-beginning 0) (match-end 0) '(org-protected t))
  22793. (replace-match (format commentsp (match-string 1)) t t))
  22794. (replace-match "")))
  22795. ;; Find matches for radio targets and turn them into internal links
  22796. (goto-char (point-min))
  22797. (when re-radio
  22798. (while (re-search-forward re-radio nil t)
  22799. (org-if-unprotected
  22800. (replace-match "\\1[[\\2]]"))))
  22801. ;; Find all links that contain a newline and put them into a single line
  22802. (goto-char (point-min))
  22803. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22804. (org-if-unprotected
  22805. (replace-match "\\1 \\3")
  22806. (goto-char (match-beginning 0))))
  22807. ;; Normalize links: Convert angle and plain links into bracket links
  22808. ;; Expand link abbreviations
  22809. (goto-char (point-min))
  22810. (while (re-search-forward re-plain-link nil t)
  22811. (goto-char (1- (match-end 0)))
  22812. (org-if-unprotected
  22813. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22814. ":" (match-string 3) "]]")))
  22815. ;; added 'org-link face to links
  22816. (put-text-property 0 (length s) 'face 'org-link s)
  22817. (replace-match s t t))))
  22818. (goto-char (point-min))
  22819. (while (re-search-forward re-angle-link nil t)
  22820. (goto-char (1- (match-end 0)))
  22821. (org-if-unprotected
  22822. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22823. ":" (match-string 3) "]]")))
  22824. (put-text-property 0 (length s) 'face 'org-link s)
  22825. (replace-match s t t))))
  22826. (goto-char (point-min))
  22827. (while (re-search-forward org-bracket-link-regexp nil t)
  22828. (org-if-unprotected
  22829. (let* ((s (concat "[[" (setq xx (save-match-data
  22830. (org-link-expand-abbrev (match-string 1))))
  22831. "]"
  22832. (if (match-end 3)
  22833. (match-string 2)
  22834. (concat "[" xx "]"))
  22835. "]")))
  22836. (put-text-property 0 (length s) 'face 'org-link s)
  22837. (replace-match s t t))))
  22838. ;; Find multiline emphasis and put them into single line
  22839. (when (plist-get parameters :emph-multiline)
  22840. (goto-char (point-min))
  22841. (while (re-search-forward org-emph-re nil t)
  22842. (if (not (= (char-after (match-beginning 3))
  22843. (char-after (match-beginning 4))))
  22844. (org-if-unprotected
  22845. (subst-char-in-region (match-beginning 0) (match-end 0)
  22846. ?\n ?\ t)
  22847. (goto-char (1- (match-end 0))))
  22848. (goto-char (1+ (match-beginning 0))))))
  22849. (setq rtn (buffer-string)))
  22850. (kill-buffer " org-mode-tmp")
  22851. rtn))
  22852. (defun org-export-grab-title-from-buffer ()
  22853. "Get a title for the current document, from looking at the buffer."
  22854. (let ((inhibit-read-only t))
  22855. (save-excursion
  22856. (goto-char (point-min))
  22857. (let ((end (save-excursion (outline-next-heading) (point))))
  22858. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22859. ;; Mark the line so that it will not be exported as normal text.
  22860. (org-unmodified
  22861. (add-text-properties (match-beginning 0) (match-end 0)
  22862. (list :org-license-to-kill t)))
  22863. ;; Return the title string
  22864. (org-trim (match-string 0)))))))
  22865. (defun org-export-get-title-from-subtree ()
  22866. "Return subtree title and exclude it from export."
  22867. (let (title (m (mark)))
  22868. (save-excursion
  22869. (goto-char (region-beginning))
  22870. (when (and (org-at-heading-p)
  22871. (>= (org-end-of-subtree t t) (region-end)))
  22872. ;; This is a subtree, we take the title from the first heading
  22873. (goto-char (region-beginning))
  22874. (looking-at org-todo-line-regexp)
  22875. (setq title (match-string 3))
  22876. (org-unmodified
  22877. (add-text-properties (point) (1+ (point-at-eol))
  22878. (list :org-license-to-kill t)))))
  22879. title))
  22880. (defun org-solidify-link-text (s &optional alist)
  22881. "Take link text and make a safe target out of it."
  22882. (save-match-data
  22883. (let* ((rtn
  22884. (mapconcat
  22885. 'identity
  22886. (org-split-string s "[ \t\r\n]+") "--"))
  22887. (a (assoc rtn alist)))
  22888. (or (cdr a) rtn))))
  22889. (defun org-get-min-level (lines)
  22890. "Get the minimum level in LINES."
  22891. (let ((re "^\\(\\*+\\) ") l min)
  22892. (catch 'exit
  22893. (while (setq l (pop lines))
  22894. (if (string-match re l)
  22895. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22896. 1)))
  22897. ;; Variable holding the vector with section numbers
  22898. (defvar org-section-numbers (make-vector org-level-max 0))
  22899. (defun org-init-section-numbers ()
  22900. "Initialize the vector for the section numbers."
  22901. (let* ((level -1)
  22902. (numbers (nreverse (org-split-string "" "\\.")))
  22903. (depth (1- (length org-section-numbers)))
  22904. (i depth) number-string)
  22905. (while (>= i 0)
  22906. (if (> i level)
  22907. (aset org-section-numbers i 0)
  22908. (setq number-string (or (car numbers) "0"))
  22909. (if (string-match "\\`[A-Z]\\'" number-string)
  22910. (aset org-section-numbers i
  22911. (- (string-to-char number-string) ?A -1))
  22912. (aset org-section-numbers i (string-to-number number-string)))
  22913. (pop numbers))
  22914. (setq i (1- i)))))
  22915. (defun org-section-number (&optional level)
  22916. "Return a string with the current section number.
  22917. When LEVEL is non-nil, increase section numbers on that level."
  22918. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22919. (when level
  22920. (when (> level -1)
  22921. (aset org-section-numbers
  22922. level (1+ (aref org-section-numbers level))))
  22923. (setq idx (1+ level))
  22924. (while (<= idx depth)
  22925. (if (not (= idx 1))
  22926. (aset org-section-numbers idx 0))
  22927. (setq idx (1+ idx))))
  22928. (setq idx 0)
  22929. (while (<= idx depth)
  22930. (setq n (aref org-section-numbers idx))
  22931. (setq string (concat string (if (not (string= string "")) "." "")
  22932. (int-to-string n)))
  22933. (setq idx (1+ idx)))
  22934. (save-match-data
  22935. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22936. (setq string (replace-match "" t nil string)))
  22937. (if (string-match "\\(\\.0\\)+\\'" string)
  22938. (setq string (replace-match "" t nil string))))
  22939. string))
  22940. ;;; ASCII export
  22941. (defvar org-last-level nil) ; dynamically scoped variable
  22942. (defvar org-min-level nil) ; dynamically scoped variable
  22943. (defvar org-levels-open nil) ; dynamically scoped parameter
  22944. (defvar org-ascii-current-indentation nil) ; For communication
  22945. (defun org-export-as-ascii (arg)
  22946. "Export the outline as a pretty ASCII file.
  22947. If there is an active region, export only the region.
  22948. The prefix ARG specifies how many levels of the outline should become
  22949. underlined headlines. The default is 3."
  22950. (interactive "P")
  22951. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22952. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22953. (org-infile-export-plist)))
  22954. (region-p (org-region-active-p))
  22955. (subtree-p
  22956. (when region-p
  22957. (save-excursion
  22958. (goto-char (region-beginning))
  22959. (and (org-at-heading-p)
  22960. (>= (org-end-of-subtree t t) (region-end))))))
  22961. (custom-times org-display-custom-times)
  22962. (org-ascii-current-indentation '(0 . 0))
  22963. (level 0) line txt
  22964. (umax nil)
  22965. (umax-toc nil)
  22966. (case-fold-search nil)
  22967. (filename (concat (file-name-as-directory
  22968. (org-export-directory :ascii opt-plist))
  22969. (file-name-sans-extension
  22970. (or (and subtree-p
  22971. (org-entry-get (region-beginning)
  22972. "EXPORT_FILE_NAME" t))
  22973. (file-name-nondirectory buffer-file-name)))
  22974. ".txt"))
  22975. (filename (if (equal (file-truename filename)
  22976. (file-truename buffer-file-name))
  22977. (concat filename ".txt")
  22978. filename))
  22979. (buffer (find-file-noselect filename))
  22980. (org-levels-open (make-vector org-level-max nil))
  22981. (odd org-odd-levels-only)
  22982. (date (plist-get opt-plist :date))
  22983. (author (plist-get opt-plist :author))
  22984. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22985. (plist-get opt-plist :title)
  22986. (and (not
  22987. (plist-get opt-plist :skip-before-1st-heading))
  22988. (org-export-grab-title-from-buffer))
  22989. (file-name-sans-extension
  22990. (file-name-nondirectory buffer-file-name))))
  22991. (email (plist-get opt-plist :email))
  22992. (language (plist-get opt-plist :language))
  22993. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22994. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22995. (todo nil)
  22996. (lang-words nil)
  22997. (region
  22998. (buffer-substring
  22999. (if (org-region-active-p) (region-beginning) (point-min))
  23000. (if (org-region-active-p) (region-end) (point-max))))
  23001. (lines (org-split-string
  23002. (org-cleaned-string-for-export
  23003. region
  23004. :for-ascii t
  23005. :skip-before-1st-heading
  23006. (plist-get opt-plist :skip-before-1st-heading)
  23007. :drawers (plist-get opt-plist :drawers)
  23008. :verbatim-multiline t
  23009. :archived-trees
  23010. (plist-get opt-plist :archived-trees)
  23011. :add-text (plist-get opt-plist :text))
  23012. "\n"))
  23013. thetoc have-headings first-heading-pos
  23014. table-open table-buffer)
  23015. (let ((inhibit-read-only t))
  23016. (org-unmodified
  23017. (remove-text-properties (point-min) (point-max)
  23018. '(:org-license-to-kill t))))
  23019. (setq org-min-level (org-get-min-level lines))
  23020. (setq org-last-level org-min-level)
  23021. (org-init-section-numbers)
  23022. (find-file-noselect filename)
  23023. (setq lang-words (or (assoc language org-export-language-setup)
  23024. (assoc "en" org-export-language-setup)))
  23025. (switch-to-buffer-other-window buffer)
  23026. (erase-buffer)
  23027. (fundamental-mode)
  23028. ;; create local variables for all options, to make sure all called
  23029. ;; functions get the correct information
  23030. (mapc (lambda (x)
  23031. (set (make-local-variable (cdr x))
  23032. (plist-get opt-plist (car x))))
  23033. org-export-plist-vars)
  23034. (org-set-local 'org-odd-levels-only odd)
  23035. (setq umax (if arg (prefix-numeric-value arg)
  23036. org-export-headline-levels))
  23037. (setq umax-toc (if (integerp org-export-with-toc)
  23038. (min org-export-with-toc umax)
  23039. umax))
  23040. ;; File header
  23041. (if title (org-insert-centered title ?=))
  23042. (insert "\n")
  23043. (if (and (or author email)
  23044. org-export-author-info)
  23045. (insert (concat (nth 1 lang-words) ": " (or author "")
  23046. (if email (concat " <" email ">") "")
  23047. "\n")))
  23048. (cond
  23049. ((and date (string-match "%" date))
  23050. (setq date (format-time-string date (current-time))))
  23051. (date)
  23052. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23053. (if (and date org-export-time-stamp-file)
  23054. (insert (concat (nth 2 lang-words) ": " date"\n")))
  23055. (insert "\n\n")
  23056. (if org-export-with-toc
  23057. (progn
  23058. (push (concat (nth 3 lang-words) "\n") thetoc)
  23059. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  23060. (mapc '(lambda (line)
  23061. (if (string-match org-todo-line-regexp
  23062. line)
  23063. ;; This is a headline
  23064. (progn
  23065. (setq have-headings t)
  23066. (setq level (- (match-end 1) (match-beginning 1))
  23067. level (org-tr-level level)
  23068. txt (match-string 3 line)
  23069. todo
  23070. (or (and org-export-mark-todo-in-toc
  23071. (match-beginning 2)
  23072. (not (member (match-string 2 line)
  23073. org-done-keywords)))
  23074. ; TODO, not DONE
  23075. (and org-export-mark-todo-in-toc
  23076. (= level umax-toc)
  23077. (org-search-todo-below
  23078. line lines level))))
  23079. (setq txt (org-html-expand-for-ascii txt))
  23080. (while (string-match org-bracket-link-regexp txt)
  23081. (setq txt
  23082. (replace-match
  23083. (match-string (if (match-end 2) 3 1) txt)
  23084. t t txt)))
  23085. (if (and (memq org-export-with-tags '(not-in-toc nil))
  23086. (string-match
  23087. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  23088. txt))
  23089. (setq txt (replace-match "" t t txt)))
  23090. (if (string-match quote-re0 txt)
  23091. (setq txt (replace-match "" t t txt)))
  23092. (if org-export-with-section-numbers
  23093. (setq txt (concat (org-section-number level)
  23094. " " txt)))
  23095. (if (<= level umax-toc)
  23096. (progn
  23097. (push
  23098. (concat
  23099. (make-string
  23100. (* (max 0 (- level org-min-level)) 4) ?\ )
  23101. (format (if todo "%s (*)\n" "%s\n") txt))
  23102. thetoc)
  23103. (setq org-last-level level))
  23104. ))))
  23105. lines)
  23106. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23107. (org-init-section-numbers)
  23108. (while (setq line (pop lines))
  23109. ;; Remove the quoted HTML tags.
  23110. (setq line (org-html-expand-for-ascii line))
  23111. ;; Remove targets
  23112. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  23113. (setq line (replace-match "" t t line)))
  23114. ;; Replace internal links
  23115. (while (string-match org-bracket-link-regexp line)
  23116. (setq line (replace-match
  23117. (if (match-end 3) "[\\3]" "[\\1]")
  23118. t nil line)))
  23119. (when custom-times
  23120. (setq line (org-translate-time line)))
  23121. (cond
  23122. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23123. ;; a Headline
  23124. (setq first-heading-pos (or first-heading-pos (point)))
  23125. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23126. txt (match-string 2 line))
  23127. (org-ascii-level-start level txt umax lines))
  23128. ((and org-export-with-tables
  23129. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23130. (if (not table-open)
  23131. ;; New table starts
  23132. (setq table-open t table-buffer nil))
  23133. ;; Accumulate lines
  23134. (setq table-buffer (cons line table-buffer))
  23135. (when (or (not lines)
  23136. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23137. (car lines))))
  23138. (setq table-open nil
  23139. table-buffer (nreverse table-buffer))
  23140. (insert (mapconcat
  23141. (lambda (x)
  23142. (org-fix-indentation x org-ascii-current-indentation))
  23143. (org-format-table-ascii table-buffer)
  23144. "\n") "\n")))
  23145. (t
  23146. (setq line (org-fix-indentation line org-ascii-current-indentation))
  23147. (if (and org-export-with-fixed-width
  23148. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  23149. (setq line (replace-match "\\1" nil nil line)))
  23150. (insert line "\n"))))
  23151. (normal-mode)
  23152. ;; insert the table of contents
  23153. (when thetoc
  23154. (goto-char (point-min))
  23155. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  23156. (progn
  23157. (goto-char (match-beginning 0))
  23158. (replace-match ""))
  23159. (goto-char first-heading-pos))
  23160. (mapc 'insert thetoc)
  23161. (or (looking-at "[ \t]*\n[ \t]*\n")
  23162. (insert "\n\n")))
  23163. ;; Convert whitespace place holders
  23164. (goto-char (point-min))
  23165. (let (beg end)
  23166. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23167. (setq end (next-single-property-change beg 'org-whitespace))
  23168. (goto-char beg)
  23169. (delete-region beg end)
  23170. (insert (make-string (- end beg) ?\ ))))
  23171. (save-buffer)
  23172. ;; remove display and invisible chars
  23173. (let (beg end)
  23174. (goto-char (point-min))
  23175. (while (setq beg (next-single-property-change (point) 'display))
  23176. (setq end (next-single-property-change beg 'display))
  23177. (delete-region beg end)
  23178. (goto-char beg)
  23179. (insert "=>"))
  23180. (goto-char (point-min))
  23181. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  23182. (setq end (next-single-property-change beg 'org-cwidth))
  23183. (delete-region beg end)
  23184. (goto-char beg)))
  23185. (goto-char (point-min))))
  23186. (defun org-export-ascii-clean-string ()
  23187. "Do extra work for ASCII export"
  23188. (goto-char (point-min))
  23189. (while (re-search-forward org-verbatim-re nil t)
  23190. (goto-char (match-end 2))
  23191. (backward-delete-char 1) (insert "'")
  23192. (goto-char (match-beginning 2))
  23193. (delete-char 1) (insert "`")
  23194. (goto-char (match-end 2))))
  23195. (defun org-search-todo-below (line lines level)
  23196. "Search the subtree below LINE for any TODO entries."
  23197. (let ((rest (cdr (memq line lines)))
  23198. (re org-todo-line-regexp)
  23199. line lv todo)
  23200. (catch 'exit
  23201. (while (setq line (pop rest))
  23202. (if (string-match re line)
  23203. (progn
  23204. (setq lv (- (match-end 1) (match-beginning 1))
  23205. todo (and (match-beginning 2)
  23206. (not (member (match-string 2 line)
  23207. org-done-keywords))))
  23208. ; TODO, not DONE
  23209. (if (<= lv level) (throw 'exit nil))
  23210. (if todo (throw 'exit t))))))))
  23211. (defun org-html-expand-for-ascii (line)
  23212. "Handle quoted HTML for ASCII export."
  23213. (if org-export-html-expand
  23214. (while (string-match "@<[^<>\n]*>" line)
  23215. ;; We just remove the tags for now.
  23216. (setq line (replace-match "" nil nil line))))
  23217. line)
  23218. (defun org-insert-centered (s &optional underline)
  23219. "Insert the string S centered and underline it with character UNDERLINE."
  23220. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23221. (insert (make-string ind ?\ ) s "\n")
  23222. (if underline
  23223. (insert (make-string ind ?\ )
  23224. (make-string (string-width s) underline)
  23225. "\n"))))
  23226. (defun org-ascii-level-start (level title umax &optional lines)
  23227. "Insert a new level in ASCII export."
  23228. (let (char (n (- level umax 1)) (ind 0))
  23229. (if (> level umax)
  23230. (progn
  23231. (insert (make-string (* 2 n) ?\ )
  23232. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23233. org-export-ascii-bullets))
  23234. " " title "\n")
  23235. ;; find the indentation of the next non-empty line
  23236. (catch 'stop
  23237. (while lines
  23238. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23239. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23240. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23241. (pop lines)))
  23242. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23243. (if (or (not (equal (char-before) ?\n))
  23244. (not (equal (char-before (1- (point))) ?\n)))
  23245. (insert "\n"))
  23246. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23247. (unless org-export-with-tags
  23248. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23249. (setq title (replace-match "" t t title))))
  23250. (if org-export-with-section-numbers
  23251. (setq title (concat (org-section-number level) " " title)))
  23252. (insert title "\n" (make-string (string-width title) char) "\n")
  23253. (setq org-ascii-current-indentation '(0 . 0)))))
  23254. (defun org-export-visible (type arg)
  23255. "Create a copy of the visible part of the current buffer, and export it.
  23256. The copy is created in a temporary buffer and removed after use.
  23257. TYPE is the final key (as a string) that also select the export command in
  23258. the `C-c C-e' export dispatcher.
  23259. As a special case, if the you type SPC at the prompt, the temporary
  23260. org-mode file will not be removed but presented to you so that you can
  23261. continue to use it. The prefix arg ARG is passed through to the exporting
  23262. command."
  23263. (interactive
  23264. (list (progn
  23265. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23266. (read-char-exclusive))
  23267. current-prefix-arg))
  23268. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23269. (error "Invalid export key"))
  23270. (let* ((binding (cdr (assoc type
  23271. '((?a . org-export-as-ascii)
  23272. (?\C-a . org-export-as-ascii)
  23273. (?b . org-export-as-html-and-open)
  23274. (?\C-b . org-export-as-html-and-open)
  23275. (?h . org-export-as-html)
  23276. (?H . org-export-as-html-to-buffer)
  23277. (?R . org-export-region-as-html)
  23278. (?x . org-export-as-xoxo)))))
  23279. (keepp (equal type ?\ ))
  23280. (file buffer-file-name)
  23281. (buffer (get-buffer-create "*Org Export Visible*"))
  23282. s e)
  23283. ;; Need to hack the drawers here.
  23284. (save-excursion
  23285. (goto-char (point-min))
  23286. (while (re-search-forward org-drawer-regexp nil t)
  23287. (goto-char (match-beginning 1))
  23288. (or (org-invisible-p) (org-flag-drawer nil))))
  23289. (with-current-buffer buffer (erase-buffer))
  23290. (save-excursion
  23291. (setq s (goto-char (point-min)))
  23292. (while (not (= (point) (point-max)))
  23293. (goto-char (org-find-invisible))
  23294. (append-to-buffer buffer s (point))
  23295. (setq s (goto-char (org-find-visible))))
  23296. (org-cycle-hide-drawers 'all)
  23297. (goto-char (point-min))
  23298. (unless keepp
  23299. ;; Copy all comment lines to the end, to make sure #+ settings are
  23300. ;; still available for the second export step. Kind of a hack, but
  23301. ;; does do the trick.
  23302. (if (looking-at "#[^\r\n]*")
  23303. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23304. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23305. (append-to-buffer buffer (1+ (match-beginning 0))
  23306. (min (point-max) (1+ (match-end 0))))))
  23307. (set-buffer buffer)
  23308. (let ((buffer-file-name file)
  23309. (org-inhibit-startup t))
  23310. (org-mode)
  23311. (show-all)
  23312. (unless keepp (funcall binding arg))))
  23313. (if (not keepp)
  23314. (kill-buffer buffer)
  23315. (switch-to-buffer-other-window buffer)
  23316. (goto-char (point-min)))))
  23317. (defun org-find-visible ()
  23318. (let ((s (point)))
  23319. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23320. (get-char-property s 'invisible)))
  23321. s))
  23322. (defun org-find-invisible ()
  23323. (let ((s (point)))
  23324. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23325. (not (get-char-property s 'invisible))))
  23326. s))
  23327. ;;; HTML export
  23328. (defun org-get-current-options ()
  23329. "Return a string with current options as keyword options.
  23330. Does include HTML export options as well as TODO and CATEGORY stuff."
  23331. (format
  23332. "#+TITLE: %s
  23333. #+AUTHOR: %s
  23334. #+EMAIL: %s
  23335. #+LANGUAGE: %s
  23336. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23337. #+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
  23338. #+CATEGORY: %s
  23339. #+SEQ_TODO: %s
  23340. #+TYP_TODO: %s
  23341. #+PRIORITIES: %c %c %c
  23342. #+DRAWERS: %s
  23343. #+STARTUP: %s %s %s %s %s
  23344. #+TAGS: %s
  23345. #+ARCHIVE: %s
  23346. #+LINK: %s
  23347. "
  23348. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23349. org-export-headline-levels
  23350. org-export-with-section-numbers
  23351. org-export-with-toc
  23352. org-export-preserve-breaks
  23353. org-export-html-expand
  23354. org-export-with-fixed-width
  23355. org-export-with-tables
  23356. org-export-with-sub-superscripts
  23357. org-export-with-special-strings
  23358. org-export-with-footnotes
  23359. org-export-with-emphasize
  23360. org-export-with-TeX-macros
  23361. org-export-with-LaTeX-fragments
  23362. org-export-skip-text-before-1st-heading
  23363. org-export-with-drawers
  23364. org-export-with-tags
  23365. (file-name-nondirectory buffer-file-name)
  23366. "TODO FEEDBACK VERIFY DONE"
  23367. "Me Jason Marie DONE"
  23368. org-highest-priority org-lowest-priority org-default-priority
  23369. (mapconcat 'identity org-drawers " ")
  23370. (cdr (assoc org-startup-folded
  23371. '((nil . "showall") (t . "overview") (content . "content"))))
  23372. (if org-odd-levels-only "odd" "oddeven")
  23373. (if org-hide-leading-stars "hidestars" "showstars")
  23374. (if org-startup-align-all-tables "align" "noalign")
  23375. (cond ((eq org-log-done t) "logdone")
  23376. ((equal org-log-done 'note) "lognotedone")
  23377. ((not org-log-done) "nologdone"))
  23378. (or (mapconcat (lambda (x)
  23379. (cond
  23380. ((equal '(:startgroup) x) "{")
  23381. ((equal '(:endgroup) x) "}")
  23382. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23383. (t (car x))))
  23384. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23385. org-archive-location
  23386. "org file:~/org/%s.org"
  23387. ))
  23388. (defun org-insert-export-options-template ()
  23389. "Insert into the buffer a template with information for exporting."
  23390. (interactive)
  23391. (if (not (bolp)) (newline))
  23392. (let ((s (org-get-current-options)))
  23393. (and (string-match "#\\+CATEGORY" s)
  23394. (setq s (substring s 0 (match-beginning 0))))
  23395. (insert s)))
  23396. (defun org-toggle-fixed-width-section (arg)
  23397. "Toggle the fixed-width export.
  23398. If there is no active region, the QUOTE keyword at the current headline is
  23399. inserted or removed. When present, it causes the text between this headline
  23400. and the next to be exported as fixed-width text, and unmodified.
  23401. If there is an active region, this command adds or removes a colon as the
  23402. first character of this line. If the first character of a line is a colon,
  23403. this line is also exported in fixed-width font."
  23404. (interactive "P")
  23405. (let* ((cc 0)
  23406. (regionp (org-region-active-p))
  23407. (beg (if regionp (region-beginning) (point)))
  23408. (end (if regionp (region-end)))
  23409. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23410. (case-fold-search nil)
  23411. (re "[ \t]*\\(:\\)")
  23412. off)
  23413. (if regionp
  23414. (save-excursion
  23415. (goto-char beg)
  23416. (setq cc (current-column))
  23417. (beginning-of-line 1)
  23418. (setq off (looking-at re))
  23419. (while (> nlines 0)
  23420. (setq nlines (1- nlines))
  23421. (beginning-of-line 1)
  23422. (cond
  23423. (arg
  23424. (move-to-column cc t)
  23425. (insert ":\n")
  23426. (forward-line -1))
  23427. ((and off (looking-at re))
  23428. (replace-match "" t t nil 1))
  23429. ((not off) (move-to-column cc t) (insert ":")))
  23430. (forward-line 1)))
  23431. (save-excursion
  23432. (org-back-to-heading)
  23433. (if (looking-at (concat outline-regexp
  23434. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23435. (replace-match "" t t nil 1)
  23436. (if (looking-at outline-regexp)
  23437. (progn
  23438. (goto-char (match-end 0))
  23439. (insert org-quote-string " "))))))))
  23440. (defun org-export-as-html-and-open (arg)
  23441. "Export the outline as HTML and immediately open it with a browser.
  23442. If there is an active region, export only the region.
  23443. The prefix ARG specifies how many levels of the outline should become
  23444. headlines. The default is 3. Lower levels will become bulleted lists."
  23445. (interactive "P")
  23446. (org-export-as-html arg 'hidden)
  23447. (org-open-file buffer-file-name))
  23448. (defun org-export-as-html-batch ()
  23449. "Call `org-export-as-html', may be used in batch processing as
  23450. emacs --batch
  23451. --load=$HOME/lib/emacs/org.el
  23452. --eval \"(setq org-export-headline-levels 2)\"
  23453. --visit=MyFile --funcall org-export-as-html-batch"
  23454. (org-export-as-html org-export-headline-levels 'hidden))
  23455. (defun org-export-as-html-to-buffer (arg)
  23456. "Call `org-exort-as-html` with output to a temporary buffer.
  23457. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23458. (interactive "P")
  23459. (org-export-as-html arg nil nil "*Org HTML Export*")
  23460. (switch-to-buffer-other-window "*Org HTML Export*"))
  23461. (defun org-replace-region-by-html (beg end)
  23462. "Assume the current region has org-mode syntax, and convert it to HTML.
  23463. This can be used in any buffer. For example, you could write an
  23464. itemized list in org-mode syntax in an HTML buffer and then use this
  23465. command to convert it."
  23466. (interactive "r")
  23467. (let (reg html buf pop-up-frames)
  23468. (save-window-excursion
  23469. (if (org-mode-p)
  23470. (setq html (org-export-region-as-html
  23471. beg end t 'string))
  23472. (setq reg (buffer-substring beg end)
  23473. buf (get-buffer-create "*Org tmp*"))
  23474. (with-current-buffer buf
  23475. (erase-buffer)
  23476. (insert reg)
  23477. (org-mode)
  23478. (setq html (org-export-region-as-html
  23479. (point-min) (point-max) t 'string)))
  23480. (kill-buffer buf)))
  23481. (delete-region beg end)
  23482. (insert html)))
  23483. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23484. "Convert region from BEG to END in org-mode buffer to HTML.
  23485. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23486. contents, and only produce the region of converted text, useful for
  23487. cut-and-paste operations.
  23488. If BUFFER is a buffer or a string, use/create that buffer as a target
  23489. of the converted HTML. If BUFFER is the symbol `string', return the
  23490. produced HTML as a string and leave not buffer behind. For example,
  23491. a Lisp program could call this function in the following way:
  23492. (setq html (org-export-region-as-html beg end t 'string))
  23493. When called interactively, the output buffer is selected, and shown
  23494. in a window. A non-interactive call will only retunr the buffer."
  23495. (interactive "r\nP")
  23496. (when (interactive-p)
  23497. (setq buffer "*Org HTML Export*"))
  23498. (let ((transient-mark-mode t) (zmacs-regions t)
  23499. rtn)
  23500. (goto-char end)
  23501. (set-mark (point)) ;; to activate the region
  23502. (goto-char beg)
  23503. (setq rtn (org-export-as-html
  23504. nil nil nil
  23505. buffer body-only))
  23506. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23507. (if (and (interactive-p) (bufferp rtn))
  23508. (switch-to-buffer-other-window rtn)
  23509. rtn)))
  23510. (defvar html-table-tag nil) ; dynamically scoped into this.
  23511. (defun org-export-as-html (arg &optional hidden ext-plist
  23512. to-buffer body-only pub-dir)
  23513. "Export the outline as a pretty HTML file.
  23514. If there is an active region, export only the region. The prefix
  23515. ARG specifies how many levels of the outline should become
  23516. headlines. The default is 3. Lower levels will become bulleted
  23517. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23518. EXT-PLIST is a property list with external parameters overriding
  23519. org-mode's default settings, but still inferior to file-local
  23520. settings. When TO-BUFFER is non-nil, create a buffer with that
  23521. name and export to that buffer. If TO-BUFFER is the symbol
  23522. `string', don't leave any buffer behind but just return the
  23523. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23524. the file header and footer, simply return the content of
  23525. <body>...</body>, without even the body tags themselves. When
  23526. PUB-DIR is set, use this as the publishing directory."
  23527. (interactive "P")
  23528. ;; Make sure we have a file name when we need it.
  23529. (when (and (not (or to-buffer body-only))
  23530. (not buffer-file-name))
  23531. (if (buffer-base-buffer)
  23532. (org-set-local 'buffer-file-name
  23533. (with-current-buffer (buffer-base-buffer)
  23534. buffer-file-name))
  23535. (error "Need a file name to be able to export.")))
  23536. (message "Exporting...")
  23537. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23538. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23539. (setq-default org-done-keywords org-done-keywords)
  23540. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23541. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23542. ext-plist
  23543. (org-infile-export-plist)))
  23544. (style (plist-get opt-plist :style))
  23545. (html-extension (plist-get opt-plist :html-extension))
  23546. (link-validate (plist-get opt-plist :link-validation-function))
  23547. valid thetoc have-headings first-heading-pos
  23548. (odd org-odd-levels-only)
  23549. (region-p (org-region-active-p))
  23550. (subtree-p
  23551. (when region-p
  23552. (save-excursion
  23553. (goto-char (region-beginning))
  23554. (and (org-at-heading-p)
  23555. (>= (org-end-of-subtree t t) (region-end))))))
  23556. ;; The following two are dynamically scoped into other
  23557. ;; routines below.
  23558. (org-current-export-dir
  23559. (or pub-dir (org-export-directory :html opt-plist)))
  23560. (org-current-export-file buffer-file-name)
  23561. (level 0) (line "") (origline "") txt todo
  23562. (umax nil)
  23563. (umax-toc nil)
  23564. (filename (if to-buffer nil
  23565. (expand-file-name
  23566. (concat
  23567. (file-name-sans-extension
  23568. (or (and subtree-p
  23569. (org-entry-get (region-beginning)
  23570. "EXPORT_FILE_NAME" t))
  23571. (file-name-nondirectory buffer-file-name)))
  23572. "." html-extension)
  23573. (file-name-as-directory
  23574. (or pub-dir (org-export-directory :html opt-plist))))))
  23575. (current-dir (if buffer-file-name
  23576. (file-name-directory buffer-file-name)
  23577. default-directory))
  23578. (buffer (if to-buffer
  23579. (cond
  23580. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23581. (t (get-buffer-create to-buffer)))
  23582. (find-file-noselect filename)))
  23583. (org-levels-open (make-vector org-level-max nil))
  23584. (date (plist-get opt-plist :date))
  23585. (author (plist-get opt-plist :author))
  23586. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23587. (plist-get opt-plist :title)
  23588. (and (not
  23589. (plist-get opt-plist :skip-before-1st-heading))
  23590. (org-export-grab-title-from-buffer))
  23591. (and buffer-file-name
  23592. (file-name-sans-extension
  23593. (file-name-nondirectory buffer-file-name)))
  23594. "UNTITLED"))
  23595. (html-table-tag (plist-get opt-plist :html-table-tag))
  23596. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23597. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23598. (inquote nil)
  23599. (infixed nil)
  23600. (in-local-list nil)
  23601. (local-list-num nil)
  23602. (local-list-indent nil)
  23603. (llt org-plain-list-ordered-item-terminator)
  23604. (email (plist-get opt-plist :email))
  23605. (language (plist-get opt-plist :language))
  23606. (lang-words nil)
  23607. (target-alist nil) tg
  23608. (head-count 0) cnt
  23609. (start 0)
  23610. (coding-system (and (boundp 'buffer-file-coding-system)
  23611. buffer-file-coding-system))
  23612. (coding-system-for-write (or org-export-html-coding-system
  23613. coding-system))
  23614. (save-buffer-coding-system (or org-export-html-coding-system
  23615. coding-system))
  23616. (charset (and coding-system-for-write
  23617. (fboundp 'coding-system-get)
  23618. (coding-system-get coding-system-for-write
  23619. 'mime-charset)))
  23620. (region
  23621. (buffer-substring
  23622. (if region-p (region-beginning) (point-min))
  23623. (if region-p (region-end) (point-max))))
  23624. (lines
  23625. (org-split-string
  23626. (org-cleaned-string-for-export
  23627. region
  23628. :emph-multiline t
  23629. :for-html t
  23630. :skip-before-1st-heading
  23631. (plist-get opt-plist :skip-before-1st-heading)
  23632. :drawers (plist-get opt-plist :drawers)
  23633. :archived-trees
  23634. (plist-get opt-plist :archived-trees)
  23635. :add-text
  23636. (plist-get opt-plist :text)
  23637. :LaTeX-fragments
  23638. (plist-get opt-plist :LaTeX-fragments))
  23639. "[\r\n]"))
  23640. table-open type
  23641. table-buffer table-orig-buffer
  23642. ind start-is-num starter didclose
  23643. rpl path desc descp desc1 desc2 link
  23644. )
  23645. (let ((inhibit-read-only t))
  23646. (org-unmodified
  23647. (remove-text-properties (point-min) (point-max)
  23648. '(:org-license-to-kill t))))
  23649. (message "Exporting...")
  23650. (setq org-min-level (org-get-min-level lines))
  23651. (setq org-last-level org-min-level)
  23652. (org-init-section-numbers)
  23653. (cond
  23654. ((and date (string-match "%" date))
  23655. (setq date (format-time-string date (current-time))))
  23656. (date)
  23657. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23658. ;; Get the language-dependent settings
  23659. (setq lang-words (or (assoc language org-export-language-setup)
  23660. (assoc "en" org-export-language-setup)))
  23661. ;; Switch to the output buffer
  23662. (set-buffer buffer)
  23663. (let ((inhibit-read-only t)) (erase-buffer))
  23664. (fundamental-mode)
  23665. (and (fboundp 'set-buffer-file-coding-system)
  23666. (set-buffer-file-coding-system coding-system-for-write))
  23667. (let ((case-fold-search nil)
  23668. (org-odd-levels-only odd))
  23669. ;; create local variables for all options, to make sure all called
  23670. ;; functions get the correct information
  23671. (mapc (lambda (x)
  23672. (set (make-local-variable (cdr x))
  23673. (plist-get opt-plist (car x))))
  23674. org-export-plist-vars)
  23675. (setq umax (if arg (prefix-numeric-value arg)
  23676. org-export-headline-levels))
  23677. (setq umax-toc (if (integerp org-export-with-toc)
  23678. (min org-export-with-toc umax)
  23679. umax))
  23680. (unless body-only
  23681. ;; File header
  23682. (insert (format
  23683. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23684. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23685. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23686. lang=\"%s\" xml:lang=\"%s\">
  23687. <head>
  23688. <title>%s</title>
  23689. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23690. <meta name=\"generator\" content=\"Org-mode\"/>
  23691. <meta name=\"generated\" content=\"%s\"/>
  23692. <meta name=\"author\" content=\"%s\"/>
  23693. %s
  23694. </head><body>
  23695. "
  23696. language language (org-html-expand title)
  23697. (or charset "iso-8859-1") date author style))
  23698. (insert (or (plist-get opt-plist :preamble) ""))
  23699. (when (plist-get opt-plist :auto-preamble)
  23700. (if title (insert (format org-export-html-title-format
  23701. (org-html-expand title))))))
  23702. (if (and org-export-with-toc (not body-only))
  23703. (progn
  23704. (push (format "<h%d>%s</h%d>\n"
  23705. org-export-html-toplevel-hlevel
  23706. (nth 3 lang-words)
  23707. org-export-html-toplevel-hlevel)
  23708. thetoc)
  23709. (push "<ul>\n<li>" thetoc)
  23710. (setq lines
  23711. (mapcar '(lambda (line)
  23712. (if (string-match org-todo-line-regexp line)
  23713. ;; This is a headline
  23714. (progn
  23715. (setq have-headings t)
  23716. (setq level (- (match-end 1) (match-beginning 1))
  23717. level (org-tr-level level)
  23718. txt (save-match-data
  23719. (org-html-expand
  23720. (org-export-cleanup-toc-line
  23721. (match-string 3 line))))
  23722. todo
  23723. (or (and org-export-mark-todo-in-toc
  23724. (match-beginning 2)
  23725. (not (member (match-string 2 line)
  23726. org-done-keywords)))
  23727. ; TODO, not DONE
  23728. (and org-export-mark-todo-in-toc
  23729. (= level umax-toc)
  23730. (org-search-todo-below
  23731. line lines level))))
  23732. (if (string-match
  23733. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23734. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23735. (if (string-match quote-re0 txt)
  23736. (setq txt (replace-match "" t t txt)))
  23737. (if org-export-with-section-numbers
  23738. (setq txt (concat (org-section-number level)
  23739. " " txt)))
  23740. (if (<= level (max umax umax-toc))
  23741. (setq head-count (+ head-count 1)))
  23742. (if (<= level umax-toc)
  23743. (progn
  23744. (if (> level org-last-level)
  23745. (progn
  23746. (setq cnt (- level org-last-level))
  23747. (while (>= (setq cnt (1- cnt)) 0)
  23748. (push "\n<ul>\n<li>" thetoc))
  23749. (push "\n" thetoc)))
  23750. (if (< level org-last-level)
  23751. (progn
  23752. (setq cnt (- org-last-level level))
  23753. (while (>= (setq cnt (1- cnt)) 0)
  23754. (push "</li>\n</ul>" thetoc))
  23755. (push "\n" thetoc)))
  23756. ;; Check for targets
  23757. (while (string-match org-target-regexp line)
  23758. (setq tg (match-string 1 line)
  23759. line (replace-match
  23760. (concat "@<span class=\"target\">" tg "@</span> ")
  23761. t t line))
  23762. (push (cons (org-solidify-link-text tg)
  23763. (format "sec-%d" head-count))
  23764. target-alist))
  23765. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23766. (setq txt (replace-match "" t t txt)))
  23767. (push
  23768. (format
  23769. (if todo
  23770. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23771. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23772. head-count txt) thetoc)
  23773. (setq org-last-level level))
  23774. )))
  23775. line)
  23776. lines))
  23777. (while (> org-last-level (1- org-min-level))
  23778. (setq org-last-level (1- org-last-level))
  23779. (push "</li>\n</ul>\n" thetoc))
  23780. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23781. (setq head-count 0)
  23782. (org-init-section-numbers)
  23783. (while (setq line (pop lines) origline line)
  23784. (catch 'nextline
  23785. ;; end of quote section?
  23786. (when (and inquote (string-match "^\\*+ " line))
  23787. (insert "</pre>\n")
  23788. (setq inquote nil))
  23789. ;; inside a quote section?
  23790. (when inquote
  23791. (insert (org-html-protect line) "\n")
  23792. (throw 'nextline nil))
  23793. ;; verbatim lines
  23794. (when (and org-export-with-fixed-width
  23795. (string-match "^[ \t]*:\\(.*\\)" line))
  23796. (when (not infixed)
  23797. (setq infixed t)
  23798. (insert "<pre>\n"))
  23799. (insert (org-html-protect (match-string 1 line)) "\n")
  23800. (when (and lines
  23801. (not (string-match "^[ \t]*\\(:.*\\)"
  23802. (car lines))))
  23803. (setq infixed nil)
  23804. (insert "</pre>\n"))
  23805. (throw 'nextline nil))
  23806. ;; Protected HTML
  23807. (when (get-text-property 0 'org-protected line)
  23808. (let (par)
  23809. (when (re-search-backward
  23810. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23811. (setq par (match-string 1))
  23812. (replace-match "\\2\n"))
  23813. (insert line "\n")
  23814. (while (and lines
  23815. (or (= (length (car lines)) 0)
  23816. (get-text-property 0 'org-protected (car lines))))
  23817. (insert (pop lines) "\n"))
  23818. (and par (insert "<p>\n")))
  23819. (throw 'nextline nil))
  23820. ;; Horizontal line
  23821. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23822. (insert "\n<hr/>\n")
  23823. (throw 'nextline nil))
  23824. ;; make targets to anchors
  23825. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23826. (cond
  23827. ((match-end 2)
  23828. (setq line (replace-match
  23829. (concat "@<a name=\""
  23830. (org-solidify-link-text (match-string 1 line))
  23831. "\">\\nbsp@</a>")
  23832. t t line)))
  23833. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23834. (setq line (replace-match
  23835. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23836. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23837. t t line)))
  23838. (t
  23839. (setq line (replace-match
  23840. (concat "@<a name=\""
  23841. (org-solidify-link-text (match-string 1 line))
  23842. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23843. t t line)))))
  23844. (setq line (org-html-handle-time-stamps line))
  23845. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23846. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23847. ;; Also handle sub_superscripts and checkboxes
  23848. (or (string-match org-table-hline-regexp line)
  23849. (setq line (org-html-expand line)))
  23850. ;; Format the links
  23851. (setq start 0)
  23852. (while (string-match org-bracket-link-analytic-regexp line start)
  23853. (setq start (match-beginning 0))
  23854. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23855. (setq path (match-string 3 line))
  23856. (setq desc1 (if (match-end 5) (match-string 5 line))
  23857. desc2 (if (match-end 2) (concat type ":" path) path)
  23858. descp (and desc1 (not (equal desc1 desc2)))
  23859. desc (or desc1 desc2))
  23860. ;; Make an image out of the description if that is so wanted
  23861. (when (and descp (org-file-image-p desc))
  23862. (save-match-data
  23863. (if (string-match "^file:" desc)
  23864. (setq desc (substring desc (match-end 0)))))
  23865. (setq desc (concat "<img src=\"" desc "\"/>")))
  23866. ;; FIXME: do we need to unescape here somewhere?
  23867. (cond
  23868. ((equal type "internal")
  23869. (setq rpl
  23870. (concat
  23871. "<a href=\"#"
  23872. (org-solidify-link-text
  23873. (save-match-data (org-link-unescape path)) target-alist)
  23874. "\">" desc "</a>")))
  23875. ((member type '("http" "https"))
  23876. ;; standard URL, just check if we need to inline an image
  23877. (if (and (or (eq t org-export-html-inline-images)
  23878. (and org-export-html-inline-images (not descp)))
  23879. (org-file-image-p path))
  23880. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23881. (setq link (concat type ":" path))
  23882. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23883. ((member type '("ftp" "mailto" "news"))
  23884. ;; standard URL
  23885. (setq link (concat type ":" path))
  23886. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23887. ((string= type "file")
  23888. ;; FILE link
  23889. (let* ((filename path)
  23890. (abs-p (file-name-absolute-p filename))
  23891. thefile file-is-image-p search)
  23892. (save-match-data
  23893. (if (string-match "::\\(.*\\)" filename)
  23894. (setq search (match-string 1 filename)
  23895. filename (replace-match "" t nil filename)))
  23896. (setq valid
  23897. (if (functionp link-validate)
  23898. (funcall link-validate filename current-dir)
  23899. t))
  23900. (setq file-is-image-p (org-file-image-p filename))
  23901. (setq thefile (if abs-p (expand-file-name filename) filename))
  23902. (when (and org-export-html-link-org-files-as-html
  23903. (string-match "\\.org$" thefile))
  23904. (setq thefile (concat (substring thefile 0
  23905. (match-beginning 0))
  23906. "." html-extension))
  23907. (if (and search
  23908. ;; make sure this is can be used as target search
  23909. (not (string-match "^[0-9]*$" search))
  23910. (not (string-match "^\\*" search))
  23911. (not (string-match "^/.*/$" search)))
  23912. (setq thefile (concat thefile "#"
  23913. (org-solidify-link-text
  23914. (org-link-unescape search)))))
  23915. (when (string-match "^file:" desc)
  23916. (setq desc (replace-match "" t t desc))
  23917. (if (string-match "\\.org$" desc)
  23918. (setq desc (replace-match "" t t desc))))))
  23919. (setq rpl (if (and file-is-image-p
  23920. (or (eq t org-export-html-inline-images)
  23921. (and org-export-html-inline-images
  23922. (not descp))))
  23923. (concat "<img src=\"" thefile "\"/>")
  23924. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23925. (if (not valid) (setq rpl desc))))
  23926. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23927. (setq rpl (concat "<i>&lt;" type ":"
  23928. (save-match-data (org-link-unescape path))
  23929. "&gt;</i>"))))
  23930. (setq line (replace-match rpl t t line)
  23931. start (+ start (length rpl))))
  23932. ;; TODO items
  23933. (if (and (string-match org-todo-line-regexp line)
  23934. (match-beginning 2))
  23935. (setq line
  23936. (concat (substring line 0 (match-beginning 2))
  23937. "<span class=\""
  23938. (if (member (match-string 2 line)
  23939. org-done-keywords)
  23940. "done" "todo")
  23941. "\">" (match-string 2 line)
  23942. "</span>" (substring line (match-end 2)))))
  23943. ;; Does this contain a reference to a footnote?
  23944. (when org-export-with-footnotes
  23945. (setq start 0)
  23946. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23947. (if (get-text-property (match-beginning 2) 'org-protected line)
  23948. (setq start (match-end 2))
  23949. (let ((n (match-string 2 line)))
  23950. (setq line
  23951. (replace-match
  23952. (format
  23953. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23954. (match-string 1 line) n n n)
  23955. t t line))))))
  23956. (cond
  23957. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23958. ;; This is a headline
  23959. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23960. txt (match-string 2 line))
  23961. (if (string-match quote-re0 txt)
  23962. (setq txt (replace-match "" t t txt)))
  23963. (if (<= level (max umax umax-toc))
  23964. (setq head-count (+ head-count 1)))
  23965. (when in-local-list
  23966. ;; Close any local lists before inserting a new header line
  23967. (while local-list-num
  23968. (org-close-li)
  23969. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23970. (pop local-list-num))
  23971. (setq local-list-indent nil
  23972. in-local-list nil))
  23973. (setq first-heading-pos (or first-heading-pos (point)))
  23974. (org-html-level-start level txt umax
  23975. (and org-export-with-toc (<= level umax))
  23976. head-count)
  23977. ;; QUOTES
  23978. (when (string-match quote-re line)
  23979. (insert "<pre>")
  23980. (setq inquote t)))
  23981. ((and org-export-with-tables
  23982. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23983. (if (not table-open)
  23984. ;; New table starts
  23985. (setq table-open t table-buffer nil table-orig-buffer nil))
  23986. ;; Accumulate lines
  23987. (setq table-buffer (cons line table-buffer)
  23988. table-orig-buffer (cons origline table-orig-buffer))
  23989. (when (or (not lines)
  23990. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23991. (car lines))))
  23992. (setq table-open nil
  23993. table-buffer (nreverse table-buffer)
  23994. table-orig-buffer (nreverse table-orig-buffer))
  23995. (org-close-par-maybe)
  23996. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23997. (t
  23998. ;; Normal lines
  23999. (when (string-match
  24000. (cond
  24001. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  24002. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  24003. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  24004. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  24005. line)
  24006. (setq ind (org-get-string-indentation line)
  24007. start-is-num (match-beginning 4)
  24008. starter (if (match-beginning 2)
  24009. (substring (match-string 2 line) 0 -1))
  24010. line (substring line (match-beginning 5)))
  24011. (unless (string-match "[^ \t]" line)
  24012. ;; empty line. Pretend indentation is large.
  24013. (setq ind (if org-empty-line-terminates-plain-lists
  24014. 0
  24015. (1+ (or (car local-list-indent) 1)))))
  24016. (setq didclose nil)
  24017. (while (and in-local-list
  24018. (or (and (= ind (car local-list-indent))
  24019. (not starter))
  24020. (< ind (car local-list-indent))))
  24021. (setq didclose t)
  24022. (org-close-li)
  24023. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  24024. (pop local-list-num) (pop local-list-indent)
  24025. (setq in-local-list local-list-indent))
  24026. (cond
  24027. ((and starter
  24028. (or (not in-local-list)
  24029. (> ind (car local-list-indent))))
  24030. ;; Start new (level of) list
  24031. (org-close-par-maybe)
  24032. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  24033. (push start-is-num local-list-num)
  24034. (push ind local-list-indent)
  24035. (setq in-local-list t))
  24036. (starter
  24037. ;; continue current list
  24038. (org-close-li)
  24039. (insert "<li>\n"))
  24040. (didclose
  24041. ;; we did close a list, normal text follows: need <p>
  24042. (org-open-par)))
  24043. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  24044. (setq line
  24045. (replace-match
  24046. (if (equal (match-string 1 line) "X")
  24047. "<b>[X]</b>"
  24048. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  24049. t t line))))
  24050. ;; Empty lines start a new paragraph. If hand-formatted lists
  24051. ;; are not fully interpreted, lines starting with "-", "+", "*"
  24052. ;; also start a new paragraph.
  24053. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  24054. ;; Is this the start of a footnote?
  24055. (when org-export-with-footnotes
  24056. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  24057. (org-close-par-maybe)
  24058. (let ((n (match-string 1 line)))
  24059. (setq line (replace-match
  24060. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  24061. ;; Check if the line break needs to be conserved
  24062. (cond
  24063. ((string-match "\\\\\\\\[ \t]*$" line)
  24064. (setq line (replace-match "<br/>" t t line)))
  24065. (org-export-preserve-breaks
  24066. (setq line (concat line "<br/>"))))
  24067. (insert line "\n")))))
  24068. ;; Properly close all local lists and other lists
  24069. (when inquote (insert "</pre>\n"))
  24070. (when in-local-list
  24071. ;; Close any local lists before inserting a new header line
  24072. (while local-list-num
  24073. (org-close-li)
  24074. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  24075. (pop local-list-num))
  24076. (setq local-list-indent nil
  24077. in-local-list nil))
  24078. (org-html-level-start 1 nil umax
  24079. (and org-export-with-toc (<= level umax))
  24080. head-count)
  24081. (unless body-only
  24082. (when (plist-get opt-plist :auto-postamble)
  24083. (insert "<div id=\"postamble\">")
  24084. (when (and org-export-author-info author)
  24085. (insert "<p class=\"author\"> "
  24086. (nth 1 lang-words) ": " author "\n")
  24087. (when email
  24088. (if (listp (split-string email ",+ *"))
  24089. (mapc (lambda(e)
  24090. (insert "<a href=\"mailto:" e "\">&lt;"
  24091. e "&gt;</a>\n"))
  24092. (split-string email ",+ *"))
  24093. (insert "<a href=\"mailto:" email "\">&lt;"
  24094. email "&gt;</a>\n")))
  24095. (insert "</p>\n"))
  24096. (when (and date org-export-time-stamp-file)
  24097. (insert "<p class=\"date\"> "
  24098. (nth 2 lang-words) ": "
  24099. date "</p>\n"))
  24100. (insert "</div>"))
  24101. (if org-export-html-with-timestamp
  24102. (insert org-export-html-html-helper-timestamp))
  24103. (insert (or (plist-get opt-plist :postamble) ""))
  24104. (insert "</body>\n</html>\n"))
  24105. (normal-mode)
  24106. (if (eq major-mode default-major-mode) (html-mode))
  24107. ;; insert the table of contents
  24108. (goto-char (point-min))
  24109. (when thetoc
  24110. (if (or (re-search-forward
  24111. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  24112. (re-search-forward
  24113. "\\[TABLE-OF-CONTENTS\\]" nil t))
  24114. (progn
  24115. (goto-char (match-beginning 0))
  24116. (replace-match ""))
  24117. (goto-char first-heading-pos)
  24118. (when (looking-at "\\s-*</p>")
  24119. (goto-char (match-end 0))
  24120. (insert "\n")))
  24121. (insert "<div id=\"table-of-contents\">\n")
  24122. (mapc 'insert thetoc)
  24123. (insert "</div>\n"))
  24124. ;; remove empty paragraphs and lists
  24125. (goto-char (point-min))
  24126. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  24127. (replace-match ""))
  24128. (goto-char (point-min))
  24129. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  24130. (replace-match ""))
  24131. (goto-char (point-min))
  24132. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  24133. (replace-match ""))
  24134. ;; Convert whitespace place holders
  24135. (goto-char (point-min))
  24136. (let (beg end n)
  24137. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  24138. (setq n (get-text-property beg 'org-whitespace)
  24139. end (next-single-property-change beg 'org-whitespace))
  24140. (goto-char beg)
  24141. (delete-region beg end)
  24142. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  24143. (make-string n ?x)))))
  24144. (or to-buffer (save-buffer))
  24145. (goto-char (point-min))
  24146. (message "Exporting... done")
  24147. (if (eq to-buffer 'string)
  24148. (prog1 (buffer-substring (point-min) (point-max))
  24149. (kill-buffer (current-buffer)))
  24150. (current-buffer)))))
  24151. (defvar org-table-colgroup-info nil)
  24152. (defun org-format-table-ascii (lines)
  24153. "Format a table for ascii export."
  24154. (if (stringp lines)
  24155. (setq lines (org-split-string lines "\n")))
  24156. (if (not (string-match "^[ \t]*|" (car lines)))
  24157. ;; Table made by table.el - test for spanning
  24158. lines
  24159. ;; A normal org table
  24160. ;; Get rid of hlines at beginning and end
  24161. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24162. (setq lines (nreverse lines))
  24163. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24164. (setq lines (nreverse lines))
  24165. (when org-export-table-remove-special-lines
  24166. ;; Check if the table has a marking column. If yes remove the
  24167. ;; column and the special lines
  24168. (setq lines (org-table-clean-before-export lines)))
  24169. ;; Get rid of the vertical lines except for grouping
  24170. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  24171. rtn line vl1 start)
  24172. (while (setq line (pop lines))
  24173. (if (string-match org-table-hline-regexp line)
  24174. (and (string-match "|\\(.*\\)|" line)
  24175. (setq line (replace-match " \\1" t nil line)))
  24176. (setq start 0 vl1 vl)
  24177. (while (string-match "|" line start)
  24178. (setq start (match-end 0))
  24179. (or (pop vl1) (setq line (replace-match " " t t line)))))
  24180. (push line rtn))
  24181. (nreverse rtn))))
  24182. (defun org-colgroup-info-to-vline-list (info)
  24183. (let (vl new last)
  24184. (while info
  24185. (setq last new new (pop info))
  24186. (if (or (memq last '(:end :startend))
  24187. (memq new '(:start :startend)))
  24188. (push t vl)
  24189. (push nil vl)))
  24190. (setq vl (nreverse vl))
  24191. (and vl (setcar vl nil))
  24192. vl))
  24193. (defun org-format-table-html (lines olines)
  24194. "Find out which HTML converter to use and return the HTML code."
  24195. (if (stringp lines)
  24196. (setq lines (org-split-string lines "\n")))
  24197. (if (string-match "^[ \t]*|" (car lines))
  24198. ;; A normal org table
  24199. (org-format-org-table-html lines)
  24200. ;; Table made by table.el - test for spanning
  24201. (let* ((hlines (delq nil (mapcar
  24202. (lambda (x)
  24203. (if (string-match "^[ \t]*\\+-" x) x
  24204. nil))
  24205. lines)))
  24206. (first (car hlines))
  24207. (ll (and (string-match "\\S-+" first)
  24208. (match-string 0 first)))
  24209. (re (concat "^[ \t]*" (regexp-quote ll)))
  24210. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24211. hlines))))
  24212. (if (and (not spanning)
  24213. (not org-export-prefer-native-exporter-for-tables))
  24214. ;; We can use my own converter with HTML conversions
  24215. (org-format-table-table-html lines)
  24216. ;; Need to use the code generator in table.el, with the original text.
  24217. (org-format-table-table-html-using-table-generate-source olines)))))
  24218. (defun org-format-org-table-html (lines &optional splice)
  24219. "Format a table into HTML."
  24220. ;; Get rid of hlines at beginning and end
  24221. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24222. (setq lines (nreverse lines))
  24223. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24224. (setq lines (nreverse lines))
  24225. (when org-export-table-remove-special-lines
  24226. ;; Check if the table has a marking column. If yes remove the
  24227. ;; column and the special lines
  24228. (setq lines (org-table-clean-before-export lines)))
  24229. (let ((head (and org-export-highlight-first-table-line
  24230. (delq nil (mapcar
  24231. (lambda (x) (string-match "^[ \t]*|-" x))
  24232. (cdr lines)))))
  24233. (nlines 0) fnum i
  24234. tbopen line fields html gr colgropen)
  24235. (if splice (setq head nil))
  24236. (unless splice (push (if head "<thead>" "<tbody>") html))
  24237. (setq tbopen t)
  24238. (while (setq line (pop lines))
  24239. (catch 'next-line
  24240. (if (string-match "^[ \t]*|-" line)
  24241. (progn
  24242. (unless splice
  24243. (push (if head "</thead>" "</tbody>") html)
  24244. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24245. (setq head nil) ;; head ends here, first time around
  24246. ;; ignore this line
  24247. (throw 'next-line t)))
  24248. ;; Break the line into fields
  24249. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24250. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24251. (setq nlines (1+ nlines) i -1)
  24252. (push (concat "<tr>"
  24253. (mapconcat
  24254. (lambda (x)
  24255. (setq i (1+ i))
  24256. (if (and (< i nlines)
  24257. (string-match org-table-number-regexp x))
  24258. (incf (aref fnum i)))
  24259. (if head
  24260. (concat (car org-export-table-header-tags) x
  24261. (cdr org-export-table-header-tags))
  24262. (concat (car org-export-table-data-tags) x
  24263. (cdr org-export-table-data-tags))))
  24264. fields "")
  24265. "</tr>")
  24266. html)))
  24267. (unless splice (if tbopen (push "</tbody>" html)))
  24268. (unless splice (push "</table>\n" html))
  24269. (setq html (nreverse html))
  24270. (unless splice
  24271. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24272. (push (mapconcat
  24273. (lambda (x)
  24274. (setq gr (pop org-table-colgroup-info))
  24275. (format "%s<col align=\"%s\"></col>%s"
  24276. (if (memq gr '(:start :startend))
  24277. (prog1
  24278. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24279. (setq colgropen t))
  24280. "")
  24281. (if (> (/ (float x) nlines) org-table-number-fraction)
  24282. "right" "left")
  24283. (if (memq gr '(:end :startend))
  24284. (progn (setq colgropen nil) "</colgroup>")
  24285. "")))
  24286. fnum "")
  24287. html)
  24288. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24289. (push html-table-tag html))
  24290. (concat (mapconcat 'identity html "\n") "\n")))
  24291. (defun org-table-clean-before-export (lines)
  24292. "Check if the table has a marking column.
  24293. If yes remove the column and the special lines."
  24294. (setq org-table-colgroup-info nil)
  24295. (if (memq nil
  24296. (mapcar
  24297. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24298. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24299. lines))
  24300. (progn
  24301. (setq org-table-clean-did-remove-column nil)
  24302. (delq nil
  24303. (mapcar
  24304. (lambda (x)
  24305. (cond
  24306. ((string-match "^[ \t]*| */ *|" x)
  24307. (setq org-table-colgroup-info
  24308. (mapcar (lambda (x)
  24309. (cond ((member x '("<" "&lt;")) :start)
  24310. ((member x '(">" "&gt;")) :end)
  24311. ((member x '("<>" "&lt;&gt;")) :startend)
  24312. (t nil)))
  24313. (org-split-string x "[ \t]*|[ \t]*")))
  24314. nil)
  24315. (t x)))
  24316. lines)))
  24317. (setq org-table-clean-did-remove-column t)
  24318. (delq nil
  24319. (mapcar
  24320. (lambda (x)
  24321. (cond
  24322. ((string-match "^[ \t]*| */ *|" x)
  24323. (setq org-table-colgroup-info
  24324. (mapcar (lambda (x)
  24325. (cond ((member x '("<" "&lt;")) :start)
  24326. ((member x '(">" "&gt;")) :end)
  24327. ((member x '("<>" "&lt;&gt;")) :startend)
  24328. (t nil)))
  24329. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24330. nil)
  24331. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24332. nil) ; ignore this line
  24333. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24334. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24335. ;; remove the first column
  24336. (replace-match "\\1|" t nil x))))
  24337. lines))))
  24338. (defun org-format-table-table-html (lines)
  24339. "Format a table generated by table.el into HTML.
  24340. This conversion does *not* use `table-generate-source' from table.el.
  24341. This has the advantage that Org-mode's HTML conversions can be used.
  24342. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24343. (let (line field-buffer
  24344. (head org-export-highlight-first-table-line)
  24345. fields html empty)
  24346. (setq html (concat html-table-tag "\n"))
  24347. (while (setq line (pop lines))
  24348. (setq empty "&nbsp;")
  24349. (catch 'next-line
  24350. (if (string-match "^[ \t]*\\+-" line)
  24351. (progn
  24352. (if field-buffer
  24353. (progn
  24354. (setq
  24355. html
  24356. (concat
  24357. html
  24358. "<tr>"
  24359. (mapconcat
  24360. (lambda (x)
  24361. (if (equal x "") (setq x empty))
  24362. (if head
  24363. (concat (car org-export-table-header-tags) x
  24364. (cdr org-export-table-header-tags))
  24365. (concat (car org-export-table-data-tags) x
  24366. (cdr org-export-table-data-tags))))
  24367. field-buffer "\n")
  24368. "</tr>\n"))
  24369. (setq head nil)
  24370. (setq field-buffer nil)))
  24371. ;; Ignore this line
  24372. (throw 'next-line t)))
  24373. ;; Break the line into fields and store the fields
  24374. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24375. (if field-buffer
  24376. (setq field-buffer (mapcar
  24377. (lambda (x)
  24378. (concat x "<br/>" (pop fields)))
  24379. field-buffer))
  24380. (setq field-buffer fields))))
  24381. (setq html (concat html "</table>\n"))
  24382. html))
  24383. (defun org-format-table-table-html-using-table-generate-source (lines)
  24384. "Format a table into html, using `table-generate-source' from table.el.
  24385. This has the advantage that cell- or row-spanning is allowed.
  24386. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24387. (require 'table)
  24388. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24389. (erase-buffer)
  24390. (insert (mapconcat 'identity lines "\n"))
  24391. (goto-char (point-min))
  24392. (if (not (re-search-forward "|[^+]" nil t))
  24393. (error "Error processing table"))
  24394. (table-recognize-table)
  24395. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24396. (table-generate-source 'html " org-tmp2 ")
  24397. (set-buffer " org-tmp2 ")
  24398. (buffer-substring (point-min) (point-max))))
  24399. (defun org-html-handle-time-stamps (s)
  24400. "Format time stamps in string S, or remove them."
  24401. (catch 'exit
  24402. (let (r b)
  24403. (while (string-match org-maybe-keyword-time-regexp s)
  24404. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24405. ;; never export CLOCK
  24406. (throw 'exit ""))
  24407. (or b (setq b (substring s 0 (match-beginning 0))))
  24408. (if (not org-export-with-timestamps)
  24409. (setq r (concat r (substring s 0 (match-beginning 0)))
  24410. s (substring s (match-end 0)))
  24411. (setq r (concat
  24412. r (substring s 0 (match-beginning 0))
  24413. (if (match-end 1)
  24414. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24415. (match-string 1 s)))
  24416. (format " @<span class=\"timestamp\">%s@</span>"
  24417. (substring
  24418. (org-translate-time (match-string 3 s)) 1 -1)))
  24419. s (substring s (match-end 0)))))
  24420. ;; Line break if line started and ended with time stamp stuff
  24421. (if (not r)
  24422. s
  24423. (setq r (concat r s))
  24424. (unless (string-match "\\S-" (concat b s))
  24425. (setq r (concat r "@<br/>")))
  24426. r))))
  24427. (defun org-html-protect (s)
  24428. ;; convert & to &amp;, < to &lt; and > to &gt;
  24429. (let ((start 0))
  24430. (while (string-match "&" s start)
  24431. (setq s (replace-match "&amp;" t t s)
  24432. start (1+ (match-beginning 0))))
  24433. (while (string-match "<" s)
  24434. (setq s (replace-match "&lt;" t t s)))
  24435. (while (string-match ">" s)
  24436. (setq s (replace-match "&gt;" t t s))))
  24437. s)
  24438. (defun org-export-cleanup-toc-line (s)
  24439. "Remove tags and time staps from lines going into the toc."
  24440. (when (memq org-export-with-tags '(not-in-toc nil))
  24441. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24442. (setq s (replace-match "" t t s))))
  24443. (when org-export-remove-timestamps-from-toc
  24444. (while (string-match org-maybe-keyword-time-regexp s)
  24445. (setq s (replace-match "" t t s))))
  24446. (while (string-match org-bracket-link-regexp s)
  24447. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24448. t t s)))
  24449. s)
  24450. (defun org-html-expand (string)
  24451. "Prepare STRING for HTML export. Applies all active conversions.
  24452. If there are links in the string, don't modify these."
  24453. (let* ((re (concat org-bracket-link-regexp "\\|"
  24454. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24455. m s l res)
  24456. (while (setq m (string-match re string))
  24457. (setq s (substring string 0 m)
  24458. l (match-string 0 string)
  24459. string (substring string (match-end 0)))
  24460. (push (org-html-do-expand s) res)
  24461. (push l res))
  24462. (push (org-html-do-expand string) res)
  24463. (apply 'concat (nreverse res))))
  24464. (defun org-html-do-expand (s)
  24465. "Apply all active conversions to translate special ASCII to HTML."
  24466. (setq s (org-html-protect s))
  24467. (if org-export-html-expand
  24468. (let ((start 0))
  24469. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24470. (setq s (replace-match "<\\1>" t nil s)))))
  24471. (if org-export-with-emphasize
  24472. (setq s (org-export-html-convert-emphasize s)))
  24473. (if org-export-with-special-strings
  24474. (setq s (org-export-html-convert-special-strings s)))
  24475. (if org-export-with-sub-superscripts
  24476. (setq s (org-export-html-convert-sub-super s)))
  24477. (if org-export-with-TeX-macros
  24478. (let ((start 0) wd ass)
  24479. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24480. (if (get-text-property (match-beginning 0) 'org-protected s)
  24481. (setq start (match-end 0))
  24482. (setq wd (match-string 1 s))
  24483. (if (setq ass (assoc wd org-html-entities))
  24484. (setq s (replace-match (or (cdr ass)
  24485. (concat "&" (car ass) ";"))
  24486. t t s))
  24487. (setq start (+ start (length wd))))))))
  24488. s)
  24489. (defun org-create-multibrace-regexp (left right n)
  24490. "Create a regular expression which will match a balanced sexp.
  24491. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24492. as single character strings.
  24493. The regexp returned will match the entire expression including the
  24494. delimiters. It will also define a single group which contains the
  24495. match except for the outermost delimiters. The maximum depth of
  24496. stacked delimiters is N. Escaping delimiters is not possible."
  24497. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24498. (or "\\|")
  24499. (re nothing)
  24500. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24501. (while (> n 1)
  24502. (setq n (1- n)
  24503. re (concat re or next)
  24504. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24505. (concat left "\\(" re "\\)" right)))
  24506. (defvar org-match-substring-regexp
  24507. (concat
  24508. "\\([^\\]\\)\\([_^]\\)\\("
  24509. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24510. "\\|"
  24511. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24512. "\\|"
  24513. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24514. "The regular expression matching a sub- or superscript.")
  24515. (defvar org-match-substring-with-braces-regexp
  24516. (concat
  24517. "\\([^\\]\\)\\([_^]\\)\\("
  24518. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24519. "\\)")
  24520. "The regular expression matching a sub- or superscript, forcing braces.")
  24521. (defconst org-export-html-special-string-regexps
  24522. '(("\\\\-" . "&shy;")
  24523. ("---\\([^-]\\)" . "&mdash;\\1")
  24524. ("--\\([^-]\\)" . "&ndash;\\1")
  24525. ("\\.\\.\\." . "&hellip;"))
  24526. "Regular expressions for special string conversion.")
  24527. (defun org-export-html-convert-special-strings (string)
  24528. "Convert special characters in STRING to HTML."
  24529. (let ((all org-export-html-special-string-regexps)
  24530. e a re rpl start)
  24531. (while (setq a (pop all))
  24532. (setq re (car a) rpl (cdr a) start 0)
  24533. (while (string-match re string start)
  24534. (if (get-text-property (match-beginning 0) 'org-protected string)
  24535. (setq start (match-end 0))
  24536. (setq string (replace-match rpl t nil string)))))
  24537. string))
  24538. (defun org-export-html-convert-sub-super (string)
  24539. "Convert sub- and superscripts in STRING to HTML."
  24540. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24541. (while (string-match org-match-substring-regexp string s)
  24542. (cond
  24543. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24544. ((get-text-property (match-beginning 2) 'org-protected string)
  24545. (setq s (match-end 2)))
  24546. (t
  24547. (setq s (match-end 1)
  24548. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24549. c (or (match-string 8 string)
  24550. (match-string 6 string)
  24551. (match-string 5 string))
  24552. string (replace-match
  24553. (concat (match-string 1 string)
  24554. "<" key ">" c "</" key ">")
  24555. t t string)))))
  24556. (while (string-match "\\\\\\([_^]\\)" string)
  24557. (setq string (replace-match (match-string 1 string) t t string)))
  24558. string))
  24559. (defun org-export-html-convert-emphasize (string)
  24560. "Apply emphasis."
  24561. (let ((s 0) rpl)
  24562. (while (string-match org-emph-re string s)
  24563. (if (not (equal
  24564. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24565. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24566. (setq s (match-beginning 0)
  24567. rpl
  24568. (concat
  24569. (match-string 1 string)
  24570. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24571. (match-string 4 string)
  24572. (nth 3 (assoc (match-string 3 string)
  24573. org-emphasis-alist))
  24574. (match-string 5 string))
  24575. string (replace-match rpl t t string)
  24576. s (+ s (- (length rpl) 2)))
  24577. (setq s (1+ s))))
  24578. string))
  24579. (defvar org-par-open nil)
  24580. (defun org-open-par ()
  24581. "Insert <p>, but first close previous paragraph if any."
  24582. (org-close-par-maybe)
  24583. (insert "\n<p>")
  24584. (setq org-par-open t))
  24585. (defun org-close-par-maybe ()
  24586. "Close paragraph if there is one open."
  24587. (when org-par-open
  24588. (insert "</p>")
  24589. (setq org-par-open nil)))
  24590. (defun org-close-li ()
  24591. "Close <li> if necessary."
  24592. (org-close-par-maybe)
  24593. (insert "</li>\n"))
  24594. (defvar body-only) ; dynamically scoped into this.
  24595. (defun org-html-level-start (level title umax with-toc head-count)
  24596. "Insert a new level in HTML export.
  24597. When TITLE is nil, just close all open levels."
  24598. (org-close-par-maybe)
  24599. (let ((l org-level-max))
  24600. (while (>= l level)
  24601. (if (aref org-levels-open (1- l))
  24602. (progn
  24603. (org-html-level-close l umax)
  24604. (aset org-levels-open (1- l) nil)))
  24605. (setq l (1- l)))
  24606. (when title
  24607. ;; If title is nil, this means this function is called to close
  24608. ;; all levels, so the rest is done only if title is given
  24609. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24610. (setq title (replace-match
  24611. (if org-export-with-tags
  24612. (save-match-data
  24613. (concat
  24614. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24615. (mapconcat 'identity (org-split-string
  24616. (match-string 1 title) ":")
  24617. "&nbsp;")
  24618. "</span>"))
  24619. "")
  24620. t t title)))
  24621. (if (> level umax)
  24622. (progn
  24623. (if (aref org-levels-open (1- level))
  24624. (progn
  24625. (org-close-li)
  24626. (insert "<li>" title "<br/>\n"))
  24627. (aset org-levels-open (1- level) t)
  24628. (org-close-par-maybe)
  24629. (insert "<ul>\n<li>" title "<br/>\n")))
  24630. (aset org-levels-open (1- level) t)
  24631. (if (and org-export-with-section-numbers (not body-only))
  24632. (setq title (concat (org-section-number level) " " title)))
  24633. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24634. (if with-toc
  24635. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24636. level level head-count title level))
  24637. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24638. (org-open-par)))))
  24639. (defun org-html-level-close (level max-outline-level)
  24640. "Terminate one level in HTML export."
  24641. (if (<= level max-outline-level)
  24642. (insert "</div>\n")
  24643. (org-close-li)
  24644. (insert "</ul>\n")))
  24645. ;;; iCalendar export
  24646. ;;;###autoload
  24647. (defun org-export-icalendar-this-file ()
  24648. "Export current file as an iCalendar file.
  24649. The iCalendar file will be located in the same directory as the Org-mode
  24650. file, but with extension `.ics'."
  24651. (interactive)
  24652. (org-export-icalendar nil buffer-file-name))
  24653. ;;;###autoload
  24654. (defun org-export-icalendar-all-agenda-files ()
  24655. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24656. Each iCalendar file will be located in the same directory as the Org-mode
  24657. file, but with extension `.ics'."
  24658. (interactive)
  24659. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24660. ;;;###autoload
  24661. (defun org-export-icalendar-combine-agenda-files ()
  24662. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24663. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24664. (interactive)
  24665. (apply 'org-export-icalendar t (org-agenda-files t)))
  24666. (defun org-export-icalendar (combine &rest files)
  24667. "Create iCalendar files for all elements of FILES.
  24668. If COMBINE is non-nil, combine all calendar entries into a single large
  24669. file and store it under the name `org-combined-agenda-icalendar-file'."
  24670. (save-excursion
  24671. (org-prepare-agenda-buffers files)
  24672. (let* ((dir (org-export-directory
  24673. :ical (list :publishing-directory
  24674. org-export-publishing-directory)))
  24675. file ical-file ical-buffer category started org-agenda-new-buffers)
  24676. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24677. (when combine
  24678. (setq ical-file
  24679. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24680. org-combined-agenda-icalendar-file
  24681. (expand-file-name org-combined-agenda-icalendar-file dir))
  24682. ical-buffer (org-get-agenda-file-buffer ical-file))
  24683. (set-buffer ical-buffer) (erase-buffer))
  24684. (while (setq file (pop files))
  24685. (catch 'nextfile
  24686. (org-check-agenda-file file)
  24687. (set-buffer (org-get-agenda-file-buffer file))
  24688. (unless combine
  24689. (setq ical-file (concat (file-name-as-directory dir)
  24690. (file-name-sans-extension
  24691. (file-name-nondirectory buffer-file-name))
  24692. ".ics"))
  24693. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24694. (with-current-buffer ical-buffer (erase-buffer)))
  24695. (setq category (or org-category
  24696. (file-name-sans-extension
  24697. (file-name-nondirectory buffer-file-name))))
  24698. (if (symbolp category) (setq category (symbol-name category)))
  24699. (let ((standard-output ical-buffer))
  24700. (if combine
  24701. (and (not started) (setq started t)
  24702. (org-start-icalendar-file org-icalendar-combined-name))
  24703. (org-start-icalendar-file category))
  24704. (org-print-icalendar-entries combine)
  24705. (when (or (and combine (not files)) (not combine))
  24706. (org-finish-icalendar-file)
  24707. (set-buffer ical-buffer)
  24708. (save-buffer)
  24709. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24710. (org-release-buffers org-agenda-new-buffers))))
  24711. (defvar org-after-save-iCalendar-file-hook nil
  24712. "Hook run after an iCalendar file has been saved.
  24713. The iCalendar buffer is still current when this hook is run.
  24714. A good way to use this is to tell a desktop calenndar application to re-read
  24715. the iCalendar file.")
  24716. (defun org-print-icalendar-entries (&optional combine)
  24717. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24718. When COMBINE is non nil, add the category to each line."
  24719. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24720. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24721. (dts (org-ical-ts-to-string
  24722. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24723. "DTSTART"))
  24724. hd ts ts2 state status (inc t) pos b sexp rrule
  24725. scheduledp deadlinep tmp pri category entry location summary desc
  24726. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24727. (org-refresh-category-properties)
  24728. (save-excursion
  24729. (goto-char (point-min))
  24730. (while (re-search-forward re1 nil t)
  24731. (catch :skip
  24732. (org-agenda-skip)
  24733. (setq pos (match-beginning 0)
  24734. ts (match-string 0)
  24735. inc t
  24736. hd (org-get-heading)
  24737. summary (org-icalendar-cleanup-string
  24738. (org-entry-get nil "SUMMARY"))
  24739. desc (org-icalendar-cleanup-string
  24740. (or (org-entry-get nil "DESCRIPTION")
  24741. (and org-icalendar-include-body (org-get-entry)))
  24742. t org-icalendar-include-body)
  24743. location (org-icalendar-cleanup-string
  24744. (org-entry-get nil "LOCATION"))
  24745. category (org-get-category))
  24746. (if (looking-at re2)
  24747. (progn
  24748. (goto-char (match-end 0))
  24749. (setq ts2 (match-string 1) inc nil))
  24750. (setq tmp (buffer-substring (max (point-min)
  24751. (- pos org-ds-keyword-length))
  24752. pos)
  24753. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24754. (progn
  24755. (setq inc nil)
  24756. (replace-match "\\1" t nil ts))
  24757. ts)
  24758. deadlinep (string-match org-deadline-regexp tmp)
  24759. scheduledp (string-match org-scheduled-regexp tmp)
  24760. ;; donep (org-entry-is-done-p)
  24761. ))
  24762. (if (or (string-match org-tr-regexp hd)
  24763. (string-match org-ts-regexp hd))
  24764. (setq hd (replace-match "" t t hd)))
  24765. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24766. (setq rrule
  24767. (concat "\nRRULE:FREQ="
  24768. (cdr (assoc
  24769. (match-string 2 ts)
  24770. '(("d" . "DAILY")("w" . "WEEKLY")
  24771. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24772. ";INTERVAL=" (match-string 1 ts)))
  24773. (setq rrule ""))
  24774. (setq summary (or summary hd))
  24775. (if (string-match org-bracket-link-regexp summary)
  24776. (setq summary
  24777. (replace-match (if (match-end 3)
  24778. (match-string 3 summary)
  24779. (match-string 1 summary))
  24780. t t summary)))
  24781. (if deadlinep (setq summary (concat "DL: " summary)))
  24782. (if scheduledp (setq summary (concat "S: " summary)))
  24783. (if (string-match "\\`<%%" ts)
  24784. (with-current-buffer sexp-buffer
  24785. (insert (substring ts 1 -1) " " summary "\n"))
  24786. (princ (format "BEGIN:VEVENT
  24787. %s
  24788. %s%s
  24789. SUMMARY:%s%s%s
  24790. CATEGORIES:%s
  24791. END:VEVENT\n"
  24792. (org-ical-ts-to-string ts "DTSTART")
  24793. (org-ical-ts-to-string ts2 "DTEND" inc)
  24794. rrule summary
  24795. (if (and desc (string-match "\\S-" desc))
  24796. (concat "\nDESCRIPTION: " desc) "")
  24797. (if (and location (string-match "\\S-" location))
  24798. (concat "\nLOCATION: " location) "")
  24799. category)))))
  24800. (when (and org-icalendar-include-sexps
  24801. (condition-case nil (require 'icalendar) (error nil))
  24802. (fboundp 'icalendar-export-region))
  24803. ;; Get all the literal sexps
  24804. (goto-char (point-min))
  24805. (while (re-search-forward "^&?%%(" nil t)
  24806. (catch :skip
  24807. (org-agenda-skip)
  24808. (setq b (match-beginning 0))
  24809. (goto-char (1- (match-end 0)))
  24810. (forward-sexp 1)
  24811. (end-of-line 1)
  24812. (setq sexp (buffer-substring b (point)))
  24813. (with-current-buffer sexp-buffer
  24814. (insert sexp "\n"))
  24815. (princ (org-diary-to-ical-string sexp-buffer)))))
  24816. (when org-icalendar-include-todo
  24817. (goto-char (point-min))
  24818. (while (re-search-forward org-todo-line-regexp nil t)
  24819. (catch :skip
  24820. (org-agenda-skip)
  24821. (setq state (match-string 2))
  24822. (setq status (if (member state org-done-keywords)
  24823. "COMPLETED" "NEEDS-ACTION"))
  24824. (when (and state
  24825. (or (not (member state org-done-keywords))
  24826. (eq org-icalendar-include-todo 'all))
  24827. (not (member org-archive-tag (org-get-tags-at)))
  24828. )
  24829. (setq hd (match-string 3)
  24830. summary (org-icalendar-cleanup-string
  24831. (org-entry-get nil "SUMMARY"))
  24832. desc (org-icalendar-cleanup-string
  24833. (or (org-entry-get nil "DESCRIPTION")
  24834. (and org-icalendar-include-body (org-get-entry)))
  24835. t org-icalendar-include-body)
  24836. location (org-icalendar-cleanup-string
  24837. (org-entry-get nil "LOCATION")))
  24838. (if (string-match org-bracket-link-regexp hd)
  24839. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24840. (match-string 1 hd))
  24841. t t hd)))
  24842. (if (string-match org-priority-regexp hd)
  24843. (setq pri (string-to-char (match-string 2 hd))
  24844. hd (concat (substring hd 0 (match-beginning 1))
  24845. (substring hd (match-end 1))))
  24846. (setq pri org-default-priority))
  24847. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24848. (- org-lowest-priority org-highest-priority))))))
  24849. (princ (format "BEGIN:VTODO
  24850. %s
  24851. SUMMARY:%s%s%s
  24852. CATEGORIES:%s
  24853. SEQUENCE:1
  24854. PRIORITY:%d
  24855. STATUS:%s
  24856. END:VTODO\n"
  24857. dts
  24858. (or summary hd)
  24859. (if (and location (string-match "\\S-" location))
  24860. (concat "\nLOCATION: " location) "")
  24861. (if (and desc (string-match "\\S-" desc))
  24862. (concat "\nDESCRIPTION: " desc) "")
  24863. category pri status)))))))))
  24864. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24865. "Take out stuff and quote what needs to be quoted.
  24866. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24867. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24868. characters."
  24869. (if (not s)
  24870. nil
  24871. (when is-body
  24872. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24873. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24874. (while (string-match re s) (setq s (replace-match "" t t s)))
  24875. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24876. (let ((start 0))
  24877. (while (string-match "\\([,;\\]\\)" s start)
  24878. (setq start (+ (match-beginning 0) 2)
  24879. s (replace-match "\\\\\\1" nil nil s))))
  24880. (when is-body
  24881. (while (string-match "[ \t]*\n[ \t]*" s)
  24882. (setq s (replace-match "\\n" t t s))))
  24883. (setq s (org-trim s))
  24884. (if is-body
  24885. (if maxlength
  24886. (if (and (numberp maxlength)
  24887. (> (length s) maxlength))
  24888. (setq s (substring s 0 maxlength)))))
  24889. s))
  24890. (defun org-get-entry ()
  24891. "Clean-up description string."
  24892. (save-excursion
  24893. (org-back-to-heading t)
  24894. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24895. (defun org-start-icalendar-file (name)
  24896. "Start an iCalendar file by inserting the header."
  24897. (let ((user user-full-name)
  24898. (name (or name "unknown"))
  24899. (timezone (cadr (current-time-zone))))
  24900. (princ
  24901. (format "BEGIN:VCALENDAR
  24902. VERSION:2.0
  24903. X-WR-CALNAME:%s
  24904. PRODID:-//%s//Emacs with Org-mode//EN
  24905. X-WR-TIMEZONE:%s
  24906. CALSCALE:GREGORIAN\n" name user timezone))))
  24907. (defun org-finish-icalendar-file ()
  24908. "Finish an iCalendar file by inserting the END statement."
  24909. (princ "END:VCALENDAR\n"))
  24910. (defun org-ical-ts-to-string (s keyword &optional inc)
  24911. "Take a time string S and convert it to iCalendar format.
  24912. KEYWORD is added in front, to make a complete line like DTSTART....
  24913. When INC is non-nil, increase the hour by two (if time string contains
  24914. a time), or the day by one (if it does not contain a time)."
  24915. (let ((t1 (org-parse-time-string s 'nodefault))
  24916. t2 fmt have-time time)
  24917. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24918. (setq t2 t1 have-time t)
  24919. (setq t2 (org-parse-time-string s)))
  24920. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24921. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24922. (when inc
  24923. (if have-time
  24924. (if org-agenda-default-appointment-duration
  24925. (setq mi (+ org-agenda-default-appointment-duration mi))
  24926. (setq h (+ 2 h)))
  24927. (setq d (1+ d))))
  24928. (setq time (encode-time s mi h d m y)))
  24929. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24930. (concat keyword (format-time-string fmt time))))
  24931. ;;; XOXO export
  24932. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24933. (with-current-buffer buffer
  24934. (apply 'insert output)))
  24935. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24936. (defun org-export-as-xoxo (&optional buffer)
  24937. "Export the org buffer as XOXO.
  24938. The XOXO buffer is named *xoxo-<source buffer name>*"
  24939. (interactive (list (current-buffer)))
  24940. ;; A quickie abstraction
  24941. ;; Output everything as XOXO
  24942. (with-current-buffer (get-buffer buffer)
  24943. (let* ((pos (point))
  24944. (opt-plist (org-combine-plists (org-default-export-plist)
  24945. (org-infile-export-plist)))
  24946. (filename (concat (file-name-as-directory
  24947. (org-export-directory :xoxo opt-plist))
  24948. (file-name-sans-extension
  24949. (file-name-nondirectory buffer-file-name))
  24950. ".html"))
  24951. (out (find-file-noselect filename))
  24952. (last-level 1)
  24953. (hanging-li nil))
  24954. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24955. ;; Check the output buffer is empty.
  24956. (with-current-buffer out (erase-buffer))
  24957. ;; Kick off the output
  24958. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24959. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24960. (let* ((hd (match-string-no-properties 1))
  24961. (level (length hd))
  24962. (text (concat
  24963. (match-string-no-properties 2)
  24964. (save-excursion
  24965. (goto-char (match-end 0))
  24966. (let ((str ""))
  24967. (catch 'loop
  24968. (while 't
  24969. (forward-line)
  24970. (if (looking-at "^[ \t]\\(.*\\)")
  24971. (setq str (concat str (match-string-no-properties 1)))
  24972. (throw 'loop str)))))))))
  24973. ;; Handle level rendering
  24974. (cond
  24975. ((> level last-level)
  24976. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24977. ((< level last-level)
  24978. (dotimes (- (- last-level level) 1)
  24979. (if hanging-li
  24980. (org-export-as-xoxo-insert-into out "</li>\n"))
  24981. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24982. (when hanging-li
  24983. (org-export-as-xoxo-insert-into out "</li>\n")
  24984. (setq hanging-li nil)))
  24985. ((equal level last-level)
  24986. (if hanging-li
  24987. (org-export-as-xoxo-insert-into out "</li>\n")))
  24988. )
  24989. (setq last-level level)
  24990. ;; And output the new li
  24991. (setq hanging-li 't)
  24992. (if (equal ?+ (elt text 0))
  24993. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24994. (org-export-as-xoxo-insert-into out "<li>" text))))
  24995. ;; Finally finish off the ol
  24996. (dotimes (- last-level 1)
  24997. (if hanging-li
  24998. (org-export-as-xoxo-insert-into out "</li>\n"))
  24999. (org-export-as-xoxo-insert-into out "</ol>\n"))
  25000. (goto-char pos)
  25001. ;; Finish the buffer off and clean it up.
  25002. (switch-to-buffer-other-window out)
  25003. (indent-region (point-min) (point-max) nil)
  25004. (save-buffer)
  25005. (goto-char (point-min))
  25006. )))
  25007. ;;;; Key bindings
  25008. ;; Make `C-c C-x' a prefix key
  25009. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  25010. ;; TAB key with modifiers
  25011. (org-defkey org-mode-map "\C-i" 'org-cycle)
  25012. (org-defkey org-mode-map [(tab)] 'org-cycle)
  25013. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  25014. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  25015. (org-defkey org-mode-map "\M-\t" 'org-complete)
  25016. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  25017. ;; The following line is necessary under Suse GNU/Linux
  25018. (unless (featurep 'xemacs)
  25019. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  25020. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  25021. (define-key org-mode-map [backtab] 'org-shifttab)
  25022. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  25023. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  25024. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  25025. ;; Cursor keys with modifiers
  25026. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  25027. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  25028. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  25029. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  25030. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  25031. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  25032. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  25033. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  25034. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  25035. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  25036. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  25037. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  25038. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  25039. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  25040. ;;; Extra keys for tty access.
  25041. ;; We only set them when really needed because otherwise the
  25042. ;; menus don't show the simple keys
  25043. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  25044. (not window-system))
  25045. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  25046. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  25047. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  25048. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  25049. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  25050. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  25051. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  25052. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  25053. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  25054. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  25055. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  25056. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  25057. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  25058. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  25059. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  25060. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  25061. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  25062. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  25063. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  25064. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  25065. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  25066. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  25067. ;; All the other keys
  25068. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  25069. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  25070. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  25071. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  25072. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  25073. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  25074. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  25075. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  25076. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  25077. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  25078. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  25079. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  25080. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  25081. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  25082. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  25083. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  25084. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  25085. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  25086. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  25087. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  25088. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  25089. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  25090. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  25091. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  25092. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  25093. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  25094. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  25095. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  25096. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  25097. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  25098. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  25099. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  25100. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  25101. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  25102. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  25103. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  25104. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  25105. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25106. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  25107. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  25108. (org-defkey org-mode-map "\C-c^" 'org-sort)
  25109. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  25110. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  25111. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  25112. (org-defkey org-mode-map "\C-m" 'org-return)
  25113. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  25114. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  25115. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  25116. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  25117. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  25118. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  25119. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  25120. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  25121. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  25122. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  25123. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  25124. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  25125. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  25126. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  25127. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  25128. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  25129. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  25130. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  25131. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  25132. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  25133. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  25134. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  25135. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  25136. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  25137. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  25138. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  25139. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  25140. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  25141. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  25142. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  25143. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  25144. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  25145. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  25146. (when (featurep 'xemacs)
  25147. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  25148. (defsubst org-table-p () (org-at-table-p))
  25149. (defun org-self-insert-command (N)
  25150. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  25151. If the cursor is in a table looking at whitespace, the whitespace is
  25152. overwritten, and the table is not marked as requiring realignment."
  25153. (interactive "p")
  25154. (if (and (org-table-p)
  25155. (progn
  25156. ;; check if we blank the field, and if that triggers align
  25157. (and org-table-auto-blank-field
  25158. (member last-command
  25159. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  25160. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  25161. ;; got extra space, this field does not determine column width
  25162. (let (org-table-may-need-update) (org-table-blank-field))
  25163. ;; no extra space, this field may determine column width
  25164. (org-table-blank-field)))
  25165. t)
  25166. (eq N 1)
  25167. (looking-at "[^|\n]* |"))
  25168. (let (org-table-may-need-update)
  25169. (goto-char (1- (match-end 0)))
  25170. (delete-backward-char 1)
  25171. (goto-char (match-beginning 0))
  25172. (self-insert-command N))
  25173. (setq org-table-may-need-update t)
  25174. (self-insert-command N)
  25175. (org-fix-tags-on-the-fly)))
  25176. (defun org-fix-tags-on-the-fly ()
  25177. (when (and (equal (char-after (point-at-bol)) ?*)
  25178. (org-on-heading-p))
  25179. (org-align-tags-here org-tags-column)))
  25180. (defun org-delete-backward-char (N)
  25181. "Like `delete-backward-char', insert whitespace at field end in tables.
  25182. When deleting backwards, in tables this function will insert whitespace in
  25183. front of the next \"|\" separator, to keep the table aligned. The table will
  25184. still be marked for re-alignment if the field did fill the entire column,
  25185. because, in this case the deletion might narrow the column."
  25186. (interactive "p")
  25187. (if (and (org-table-p)
  25188. (eq N 1)
  25189. (string-match "|" (buffer-substring (point-at-bol) (point)))
  25190. (looking-at ".*?|"))
  25191. (let ((pos (point))
  25192. (noalign (looking-at "[^|\n\r]* |"))
  25193. (c org-table-may-need-update))
  25194. (backward-delete-char N)
  25195. (skip-chars-forward "^|")
  25196. (insert " ")
  25197. (goto-char (1- pos))
  25198. ;; noalign: if there were two spaces at the end, this field
  25199. ;; does not determine the width of the column.
  25200. (if noalign (setq org-table-may-need-update c)))
  25201. (backward-delete-char N)
  25202. (org-fix-tags-on-the-fly)))
  25203. (defun org-delete-char (N)
  25204. "Like `delete-char', but insert whitespace at field end in tables.
  25205. When deleting characters, in tables this function will insert whitespace in
  25206. front of the next \"|\" separator, to keep the table aligned. The table will
  25207. still be marked for re-alignment if the field did fill the entire column,
  25208. because, in this case the deletion might narrow the column."
  25209. (interactive "p")
  25210. (if (and (org-table-p)
  25211. (not (bolp))
  25212. (not (= (char-after) ?|))
  25213. (eq N 1))
  25214. (if (looking-at ".*?|")
  25215. (let ((pos (point))
  25216. (noalign (looking-at "[^|\n\r]* |"))
  25217. (c org-table-may-need-update))
  25218. (replace-match (concat
  25219. (substring (match-string 0) 1 -1)
  25220. " |"))
  25221. (goto-char pos)
  25222. ;; noalign: if there were two spaces at the end, this field
  25223. ;; does not determine the width of the column.
  25224. (if noalign (setq org-table-may-need-update c)))
  25225. (delete-char N))
  25226. (delete-char N)
  25227. (org-fix-tags-on-the-fly)))
  25228. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25229. (put 'org-self-insert-command 'delete-selection t)
  25230. (put 'orgtbl-self-insert-command 'delete-selection t)
  25231. (put 'org-delete-char 'delete-selection 'supersede)
  25232. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25233. ;; Make `flyspell-mode' delay after some commands
  25234. (put 'org-self-insert-command 'flyspell-delayed t)
  25235. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25236. (put 'org-delete-char 'flyspell-delayed t)
  25237. (put 'org-delete-backward-char 'flyspell-delayed t)
  25238. ;; Make pabbrev-mode expand after org-mode commands
  25239. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25240. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25241. ;; How to do this: Measure non-white length of current string
  25242. ;; If equal to column width, we should realign.
  25243. (defun org-remap (map &rest commands)
  25244. "In MAP, remap the functions given in COMMANDS.
  25245. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25246. (let (new old)
  25247. (while commands
  25248. (setq old (pop commands) new (pop commands))
  25249. (if (fboundp 'command-remapping)
  25250. (org-defkey map (vector 'remap old) new)
  25251. (substitute-key-definition old new map global-map)))))
  25252. (when (eq org-enable-table-editor 'optimized)
  25253. ;; If the user wants maximum table support, we need to hijack
  25254. ;; some standard editing functions
  25255. (org-remap org-mode-map
  25256. 'self-insert-command 'org-self-insert-command
  25257. 'delete-char 'org-delete-char
  25258. 'delete-backward-char 'org-delete-backward-char)
  25259. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25260. (defun org-shiftcursor-error ()
  25261. "Throw an error because Shift-Cursor command was applied in wrong context."
  25262. (error "This command is active in special context like tables, headlines or timestamps"))
  25263. (defun org-shifttab (&optional arg)
  25264. "Global visibility cycling or move to previous table field.
  25265. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25266. on context.
  25267. See the individual commands for more information."
  25268. (interactive "P")
  25269. (cond
  25270. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25271. (arg (message "Content view to level: ")
  25272. (org-content (prefix-numeric-value arg))
  25273. (setq org-cycle-global-status 'overview))
  25274. (t (call-interactively 'org-global-cycle))))
  25275. (defun org-shiftmetaleft ()
  25276. "Promote subtree or delete table column.
  25277. Calls `org-promote-subtree', `org-outdent-item',
  25278. or `org-table-delete-column', depending on context.
  25279. See the individual commands for more information."
  25280. (interactive)
  25281. (cond
  25282. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25283. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25284. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25285. (t (org-shiftcursor-error))))
  25286. (defun org-shiftmetaright ()
  25287. "Demote subtree or insert table column.
  25288. Calls `org-demote-subtree', `org-indent-item',
  25289. or `org-table-insert-column', depending on context.
  25290. See the individual commands for more information."
  25291. (interactive)
  25292. (cond
  25293. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25294. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25295. ((org-at-item-p) (call-interactively 'org-indent-item))
  25296. (t (org-shiftcursor-error))))
  25297. (defun org-shiftmetaup (&optional arg)
  25298. "Move subtree up or kill table row.
  25299. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25300. `org-move-item-up' depending on context. See the individual commands
  25301. for more information."
  25302. (interactive "P")
  25303. (cond
  25304. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25305. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25306. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25307. (t (org-shiftcursor-error))))
  25308. (defun org-shiftmetadown (&optional arg)
  25309. "Move subtree down or insert table row.
  25310. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25311. `org-move-item-down', depending on context. See the individual
  25312. commands for more information."
  25313. (interactive "P")
  25314. (cond
  25315. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25316. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25317. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25318. (t (org-shiftcursor-error))))
  25319. (defun org-metaleft (&optional arg)
  25320. "Promote heading or move table column to left.
  25321. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25322. With no specific context, calls the Emacs default `backward-word'.
  25323. See the individual commands for more information."
  25324. (interactive "P")
  25325. (cond
  25326. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25327. ((or (org-on-heading-p) (org-region-active-p))
  25328. (call-interactively 'org-do-promote))
  25329. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25330. (t (call-interactively 'backward-word))))
  25331. (defun org-metaright (&optional arg)
  25332. "Demote subtree or move table column to right.
  25333. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25334. With no specific context, calls the Emacs default `forward-word'.
  25335. See the individual commands for more information."
  25336. (interactive "P")
  25337. (cond
  25338. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25339. ((or (org-on-heading-p) (org-region-active-p))
  25340. (call-interactively 'org-do-demote))
  25341. ((org-at-item-p) (call-interactively 'org-indent-item))
  25342. (t (call-interactively 'forward-word))))
  25343. (defun org-metaup (&optional arg)
  25344. "Move subtree up or move table row up.
  25345. Calls `org-move-subtree-up' or `org-table-move-row' or
  25346. `org-move-item-up', depending on context. See the individual commands
  25347. for more information."
  25348. (interactive "P")
  25349. (cond
  25350. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25351. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25352. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25353. (t (transpose-lines 1) (beginning-of-line -1))))
  25354. (defun org-metadown (&optional arg)
  25355. "Move subtree down or move table row down.
  25356. Calls `org-move-subtree-down' or `org-table-move-row' or
  25357. `org-move-item-down', depending on context. See the individual
  25358. commands for more information."
  25359. (interactive "P")
  25360. (cond
  25361. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25362. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25363. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25364. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25365. (defun org-shiftup (&optional arg)
  25366. "Increase item in timestamp or increase priority of current headline.
  25367. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25368. depending on context. See the individual commands for more information."
  25369. (interactive "P")
  25370. (cond
  25371. ((org-at-timestamp-p t)
  25372. (call-interactively (if org-edit-timestamp-down-means-later
  25373. 'org-timestamp-down 'org-timestamp-up)))
  25374. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25375. ((org-at-item-p) (call-interactively 'org-previous-item))
  25376. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25377. (defun org-shiftdown (&optional arg)
  25378. "Decrease item in timestamp or decrease priority of current headline.
  25379. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25380. depending on context. See the individual commands for more information."
  25381. (interactive "P")
  25382. (cond
  25383. ((org-at-timestamp-p t)
  25384. (call-interactively (if org-edit-timestamp-down-means-later
  25385. 'org-timestamp-up 'org-timestamp-down)))
  25386. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25387. (t (call-interactively 'org-next-item))))
  25388. (defun org-shiftright ()
  25389. "Next TODO keyword or timestamp one day later, depending on context."
  25390. (interactive)
  25391. (cond
  25392. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25393. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25394. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25395. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25396. (t (org-shiftcursor-error))))
  25397. (defun org-shiftleft ()
  25398. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25399. (interactive)
  25400. (cond
  25401. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25402. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25403. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25404. ((org-at-property-p)
  25405. (call-interactively 'org-property-previous-allowed-value))
  25406. (t (org-shiftcursor-error))))
  25407. (defun org-shiftcontrolright ()
  25408. "Switch to next TODO set."
  25409. (interactive)
  25410. (cond
  25411. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25412. (t (org-shiftcursor-error))))
  25413. (defun org-shiftcontrolleft ()
  25414. "Switch to previous TODO set."
  25415. (interactive)
  25416. (cond
  25417. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25418. (t (org-shiftcursor-error))))
  25419. (defun org-ctrl-c-ret ()
  25420. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25421. (interactive)
  25422. (cond
  25423. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25424. (t (call-interactively 'org-insert-heading))))
  25425. (defun org-copy-special ()
  25426. "Copy region in table or copy current subtree.
  25427. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25428. See the individual commands for more information."
  25429. (interactive)
  25430. (call-interactively
  25431. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25432. (defun org-cut-special ()
  25433. "Cut region in table or cut current subtree.
  25434. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25435. See the individual commands for more information."
  25436. (interactive)
  25437. (call-interactively
  25438. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25439. (defun org-paste-special (arg)
  25440. "Paste rectangular region into table, or past subtree relative to level.
  25441. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25442. See the individual commands for more information."
  25443. (interactive "P")
  25444. (if (org-at-table-p)
  25445. (org-table-paste-rectangle)
  25446. (org-paste-subtree arg)))
  25447. (defun org-ctrl-c-ctrl-c (&optional arg)
  25448. "Set tags in headline, or update according to changed information at point.
  25449. This command does many different things, depending on context:
  25450. - If the cursor is in a headline, prompt for tags and insert them
  25451. into the current line, aligned to `org-tags-column'. When called
  25452. with prefix arg, realign all tags in the current buffer.
  25453. - If the cursor is in one of the special #+KEYWORD lines, this
  25454. triggers scanning the buffer for these lines and updating the
  25455. information.
  25456. - If the cursor is inside a table, realign the table. This command
  25457. works even if the automatic table editor has been turned off.
  25458. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25459. the entire table.
  25460. - If the cursor is a the beginning of a dynamic block, update it.
  25461. - If the cursor is inside a table created by the table.el package,
  25462. activate that table.
  25463. - If the current buffer is a remember buffer, close note and file it.
  25464. with a prefix argument, file it without further interaction to the default
  25465. location.
  25466. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25467. links in this buffer.
  25468. - If the cursor is on a numbered item in a plain list, renumber the
  25469. ordered list.
  25470. - If the cursor is on a checkbox, toggle it."
  25471. (interactive "P")
  25472. (let ((org-enable-table-editor t))
  25473. (cond
  25474. ((or org-clock-overlays
  25475. org-occur-highlights
  25476. org-latex-fragment-image-overlays)
  25477. (org-remove-clock-overlays)
  25478. (org-remove-occur-highlights)
  25479. (org-remove-latex-fragment-image-overlays)
  25480. (message "Temporary highlights/overlays removed from current buffer"))
  25481. ((and (local-variable-p 'org-finish-function (current-buffer))
  25482. (fboundp org-finish-function))
  25483. (funcall org-finish-function))
  25484. ((org-at-property-p)
  25485. (call-interactively 'org-property-action))
  25486. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25487. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25488. ((org-at-table.el-p)
  25489. (require 'table)
  25490. (beginning-of-line 1)
  25491. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25492. (call-interactively 'table-recognize-table))
  25493. ((org-at-table-p)
  25494. (org-table-maybe-eval-formula)
  25495. (if arg
  25496. (call-interactively 'org-table-recalculate)
  25497. (org-table-maybe-recalculate-line))
  25498. (call-interactively 'org-table-align))
  25499. ((org-at-item-checkbox-p)
  25500. (call-interactively 'org-toggle-checkbox))
  25501. ((org-at-item-p)
  25502. (call-interactively 'org-maybe-renumber-ordered-list))
  25503. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25504. ;; Dynamic block
  25505. (beginning-of-line 1)
  25506. (org-update-dblock))
  25507. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25508. (cond
  25509. ((equal (match-string 1) "TBLFM")
  25510. ;; Recalculate the table before this line
  25511. (save-excursion
  25512. (beginning-of-line 1)
  25513. (skip-chars-backward " \r\n\t")
  25514. (if (org-at-table-p)
  25515. (org-call-with-arg 'org-table-recalculate t))))
  25516. (t
  25517. (call-interactively 'org-mode-restart))))
  25518. (t (error "C-c C-c can do nothing useful at this location.")))))
  25519. (defun org-mode-restart ()
  25520. "Restart Org-mode, to scan again for special lines.
  25521. Also updates the keyword regular expressions."
  25522. (interactive)
  25523. (let ((org-inhibit-startup t)) (org-mode))
  25524. (message "Org-mode restarted to refresh keyword and special line setup"))
  25525. (defun org-kill-note-or-show-branches ()
  25526. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25527. (interactive)
  25528. (if (not org-finish-function)
  25529. (call-interactively 'show-branches)
  25530. (let ((org-note-abort t))
  25531. (funcall org-finish-function))))
  25532. (defun org-return (&optional indent)
  25533. "Goto next table row or insert a newline.
  25534. Calls `org-table-next-row' or `newline', depending on context.
  25535. See the individual commands for more information."
  25536. (interactive)
  25537. (cond
  25538. ((bobp) (if indent (newline-and-indent) (newline)))
  25539. ((and (org-at-heading-p)
  25540. (looking-at
  25541. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25542. (org-show-entry)
  25543. (end-of-line 1)
  25544. (newline))
  25545. ((org-at-table-p)
  25546. (org-table-justify-field-maybe)
  25547. (call-interactively 'org-table-next-row))
  25548. (t (if indent (newline-and-indent) (newline)))))
  25549. (defun org-return-indent ()
  25550. "Goto next table row or insert a newline and indent.
  25551. Calls `org-table-next-row' or `newline-and-indent', depending on
  25552. context. See the individual commands for more information."
  25553. (interactive)
  25554. (org-return t))
  25555. (defun org-ctrl-c-star ()
  25556. "Compute table, or change heading status of lines.
  25557. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25558. depending on context. This will also turn a plain list item or a normal
  25559. line into a subheading."
  25560. (interactive)
  25561. (cond
  25562. ((org-at-table-p)
  25563. (call-interactively 'org-table-recalculate))
  25564. ((org-region-active-p)
  25565. ;; Convert all lines in region to list items
  25566. (call-interactively 'org-toggle-region-headings))
  25567. ((org-on-heading-p)
  25568. (org-toggle-region-headings (point-at-bol)
  25569. (min (1+ (point-at-eol)) (point-max))))
  25570. ((org-at-item-p)
  25571. ;; Convert to heading
  25572. ;; FIXME: not yet implemented
  25573. )
  25574. (t (org-toggle-region-headings (point-at-bol)
  25575. (min (1+ (point-at-eol)) (point-max))))))
  25576. (defun org-ctrl-c-minus ()
  25577. "Insert separator line in table or modify bullet status of line.
  25578. Also turns a plain line or a region of lines into list items.
  25579. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25580. `org-cycle-list-bullet', depending on context."
  25581. (interactive)
  25582. (cond
  25583. ((org-at-table-p)
  25584. (call-interactively 'org-table-insert-hline))
  25585. ((org-on-heading-p)
  25586. ;; Convert to item
  25587. (save-excursion
  25588. (beginning-of-line 1)
  25589. (if (looking-at "\\*+ ")
  25590. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25591. ((org-region-active-p)
  25592. ;; Convert all lines in region to list items
  25593. (call-interactively 'org-toggle-region-items))
  25594. ((org-in-item-p)
  25595. (call-interactively 'org-cycle-list-bullet))
  25596. (t (org-toggle-region-items (point-at-bol)
  25597. (min (1+ (point-at-eol)) (point-max))))))
  25598. (defun org-toggle-region-items (beg end)
  25599. "Convert all lines in region to list items.
  25600. If the first line is already an item, convert all list items in the region
  25601. to normal lines."
  25602. (interactive "r")
  25603. (let (l2 l)
  25604. (save-excursion
  25605. (goto-char end)
  25606. (setq l2 (org-current-line))
  25607. (goto-char beg)
  25608. (beginning-of-line 1)
  25609. (setq l (1- (org-current-line)))
  25610. (if (org-at-item-p)
  25611. ;; We already have items, de-itemize
  25612. (while (< (setq l (1+ l)) l2)
  25613. (when (org-at-item-p)
  25614. (goto-char (match-beginning 2))
  25615. (delete-region (match-beginning 2) (match-end 2))
  25616. (and (looking-at "[ \t]+") (replace-match "")))
  25617. (beginning-of-line 2))
  25618. (while (< (setq l (1+ l)) l2)
  25619. (unless (org-at-item-p)
  25620. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25621. (replace-match "\\1- \\2")))
  25622. (beginning-of-line 2))))))
  25623. (defun org-toggle-region-headings (beg end)
  25624. "Convert all lines in region to list items.
  25625. If the first line is already an item, convert all list items in the region
  25626. to normal lines."
  25627. (interactive "r")
  25628. (let (l2 l)
  25629. (save-excursion
  25630. (goto-char end)
  25631. (setq l2 (org-current-line))
  25632. (goto-char beg)
  25633. (beginning-of-line 1)
  25634. (setq l (1- (org-current-line)))
  25635. (if (org-on-heading-p)
  25636. ;; We already have headlines, de-star them
  25637. (while (< (setq l (1+ l)) l2)
  25638. (when (org-on-heading-p t)
  25639. (and (looking-at outline-regexp) (replace-match "")))
  25640. (beginning-of-line 2))
  25641. (let* ((stars (save-excursion
  25642. (re-search-backward org-complex-heading-regexp nil t)
  25643. (or (match-string 1) "*")))
  25644. (add-stars (if org-odd-levels-only "**" "*"))
  25645. (rpl (concat stars add-stars " \\2")))
  25646. (while (< (setq l (1+ l)) l2)
  25647. (unless (org-on-heading-p)
  25648. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25649. (replace-match rpl)))
  25650. (beginning-of-line 2)))))))
  25651. (defun org-meta-return (&optional arg)
  25652. "Insert a new heading or wrap a region in a table.
  25653. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25654. See the individual commands for more information."
  25655. (interactive "P")
  25656. (cond
  25657. ((org-at-table-p)
  25658. (call-interactively 'org-table-wrap-region))
  25659. (t (call-interactively 'org-insert-heading))))
  25660. ;;; Menu entries
  25661. ;; Define the Org-mode menus
  25662. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25663. '("Tbl"
  25664. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25665. ["Next Field" org-cycle (org-at-table-p)]
  25666. ["Previous Field" org-shifttab (org-at-table-p)]
  25667. ["Next Row" org-return (org-at-table-p)]
  25668. "--"
  25669. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25670. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25671. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25672. "--"
  25673. ("Column"
  25674. ["Move Column Left" org-metaleft (org-at-table-p)]
  25675. ["Move Column Right" org-metaright (org-at-table-p)]
  25676. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25677. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25678. ("Row"
  25679. ["Move Row Up" org-metaup (org-at-table-p)]
  25680. ["Move Row Down" org-metadown (org-at-table-p)]
  25681. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25682. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25683. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25684. "--"
  25685. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25686. ("Rectangle"
  25687. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25688. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25689. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25690. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25691. "--"
  25692. ("Calculate"
  25693. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25694. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25695. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25696. "--"
  25697. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25698. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25699. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25700. "--"
  25701. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25702. "--"
  25703. ["Sum Column/Rectangle" org-table-sum
  25704. (or (org-at-table-p) (org-region-active-p))]
  25705. ["Which Column?" org-table-current-column (org-at-table-p)])
  25706. ["Debug Formulas"
  25707. org-table-toggle-formula-debugger
  25708. :style toggle :selected org-table-formula-debug]
  25709. ["Show Col/Row Numbers"
  25710. org-table-toggle-coordinate-overlays
  25711. :style toggle :selected org-table-overlay-coordinates]
  25712. "--"
  25713. ["Create" org-table-create (and (not (org-at-table-p))
  25714. org-enable-table-editor)]
  25715. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25716. ["Import from File" org-table-import (not (org-at-table-p))]
  25717. ["Export to File" org-table-export (org-at-table-p)]
  25718. "--"
  25719. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25720. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25721. '("Org"
  25722. ("Show/Hide"
  25723. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25724. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25725. ["Sparse Tree" org-occur t]
  25726. ["Reveal Context" org-reveal t]
  25727. ["Show All" show-all t]
  25728. "--"
  25729. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25730. "--"
  25731. ["New Heading" org-insert-heading t]
  25732. ("Navigate Headings"
  25733. ["Up" outline-up-heading t]
  25734. ["Next" outline-next-visible-heading t]
  25735. ["Previous" outline-previous-visible-heading t]
  25736. ["Next Same Level" outline-forward-same-level t]
  25737. ["Previous Same Level" outline-backward-same-level t]
  25738. "--"
  25739. ["Jump" org-goto t])
  25740. ("Edit Structure"
  25741. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25742. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25743. "--"
  25744. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25745. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25746. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25747. "--"
  25748. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25749. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25750. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25751. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25752. "--"
  25753. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25754. "--"
  25755. ["Convert to odd levels" org-convert-to-odd-levels t]
  25756. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25757. ("Editing"
  25758. ["Emphasis..." org-emphasize t])
  25759. ("Archive"
  25760. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25761. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25762. ; :active t :keys "C-u C-c C-x C-a"]
  25763. ["Sparse trees open ARCHIVE trees"
  25764. (setq org-sparse-tree-open-archived-trees
  25765. (not org-sparse-tree-open-archived-trees))
  25766. :style toggle :selected org-sparse-tree-open-archived-trees]
  25767. ["Cycling opens ARCHIVE trees"
  25768. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25769. :style toggle :selected org-cycle-open-archived-trees]
  25770. ["Agenda includes ARCHIVE trees"
  25771. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25772. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25773. "--"
  25774. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25775. ; ["Check and Move Children" (org-archive-subtree '(4))
  25776. ; :active t :keys "C-u C-c C-x C-s"]
  25777. )
  25778. "--"
  25779. ("TODO Lists"
  25780. ["TODO/DONE/-" org-todo t]
  25781. ("Select keyword"
  25782. ["Next keyword" org-shiftright (org-on-heading-p)]
  25783. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25784. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25785. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25786. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25787. ["Show TODO Tree" org-show-todo-tree t]
  25788. ["Global TODO list" org-todo-list t]
  25789. "--"
  25790. ["Set Priority" org-priority t]
  25791. ["Priority Up" org-shiftup t]
  25792. ["Priority Down" org-shiftdown t])
  25793. ("TAGS and Properties"
  25794. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25795. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25796. "--"
  25797. ["Set property" 'org-set-property t]
  25798. ["Column view of properties" org-columns t]
  25799. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25800. ("Dates and Scheduling"
  25801. ["Timestamp" org-time-stamp t]
  25802. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25803. ("Change Date"
  25804. ["1 Day Later" org-shiftright t]
  25805. ["1 Day Earlier" org-shiftleft t]
  25806. ["1 ... Later" org-shiftup t]
  25807. ["1 ... Earlier" org-shiftdown t])
  25808. ["Compute Time Range" org-evaluate-time-range t]
  25809. ["Schedule Item" org-schedule t]
  25810. ["Deadline" org-deadline t]
  25811. "--"
  25812. ["Custom time format" org-toggle-time-stamp-overlays
  25813. :style radio :selected org-display-custom-times]
  25814. "--"
  25815. ["Goto Calendar" org-goto-calendar t]
  25816. ["Date from Calendar" org-date-from-calendar t])
  25817. ("Logging work"
  25818. ["Clock in" org-clock-in t]
  25819. ["Clock out" org-clock-out t]
  25820. ["Clock cancel" org-clock-cancel t]
  25821. ["Goto running clock" org-clock-goto t]
  25822. ["Display times" org-clock-display t]
  25823. ["Create clock table" org-clock-report t]
  25824. "--"
  25825. ["Record DONE time"
  25826. (progn (setq org-log-done (not org-log-done))
  25827. (message "Switching to %s will %s record a timestamp"
  25828. (car org-done-keywords)
  25829. (if org-log-done "automatically" "not")))
  25830. :style toggle :selected org-log-done])
  25831. "--"
  25832. ["Agenda Command..." org-agenda t]
  25833. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25834. ("File List for Agenda")
  25835. ("Special views current file"
  25836. ["TODO Tree" org-show-todo-tree t]
  25837. ["Check Deadlines" org-check-deadlines t]
  25838. ["Timeline" org-timeline t]
  25839. ["Tags Tree" org-tags-sparse-tree t])
  25840. "--"
  25841. ("Hyperlinks"
  25842. ["Store Link (Global)" org-store-link t]
  25843. ["Insert Link" org-insert-link t]
  25844. ["Follow Link" org-open-at-point t]
  25845. "--"
  25846. ["Next link" org-next-link t]
  25847. ["Previous link" org-previous-link t]
  25848. "--"
  25849. ["Descriptive Links"
  25850. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25851. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25852. ["Literal Links"
  25853. (progn
  25854. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25855. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25856. "--"
  25857. ["Export/Publish..." org-export t]
  25858. ("LaTeX"
  25859. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25860. :selected org-cdlatex-mode]
  25861. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25862. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25863. ["Modify math symbol" org-cdlatex-math-modify
  25864. (org-inside-LaTeX-fragment-p)]
  25865. ["Export LaTeX fragments as images"
  25866. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25867. :style toggle :selected org-export-with-LaTeX-fragments])
  25868. "--"
  25869. ("Documentation"
  25870. ["Show Version" org-version t]
  25871. ["Info Documentation" org-info t])
  25872. ("Customize"
  25873. ["Browse Org Group" org-customize t]
  25874. "--"
  25875. ["Expand This Menu" org-create-customize-menu
  25876. (fboundp 'customize-menu-create)])
  25877. "--"
  25878. ["Refresh setup" org-mode-restart t]
  25879. ))
  25880. (defun org-info (&optional node)
  25881. "Read documentation for Org-mode in the info system.
  25882. With optional NODE, go directly to that node."
  25883. (interactive)
  25884. (info (format "(org)%s" (or node ""))))
  25885. (defun org-install-agenda-files-menu ()
  25886. (let ((bl (buffer-list)))
  25887. (save-excursion
  25888. (while bl
  25889. (set-buffer (pop bl))
  25890. (if (org-mode-p) (setq bl nil)))
  25891. (when (org-mode-p)
  25892. (easy-menu-change
  25893. '("Org") "File List for Agenda"
  25894. (append
  25895. (list
  25896. ["Edit File List" (org-edit-agenda-file-list) t]
  25897. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25898. ["Remove Current File from List" org-remove-file t]
  25899. ["Cycle through agenda files" org-cycle-agenda-files t]
  25900. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25901. "--")
  25902. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25903. ;;;; Documentation
  25904. (defun org-customize ()
  25905. "Call the customize function with org as argument."
  25906. (interactive)
  25907. (customize-browse 'org))
  25908. (defun org-create-customize-menu ()
  25909. "Create a full customization menu for Org-mode, insert it into the menu."
  25910. (interactive)
  25911. (if (fboundp 'customize-menu-create)
  25912. (progn
  25913. (easy-menu-change
  25914. '("Org") "Customize"
  25915. `(["Browse Org group" org-customize t]
  25916. "--"
  25917. ,(customize-menu-create 'org)
  25918. ["Set" Custom-set t]
  25919. ["Save" Custom-save t]
  25920. ["Reset to Current" Custom-reset-current t]
  25921. ["Reset to Saved" Custom-reset-saved t]
  25922. ["Reset to Standard Settings" Custom-reset-standard t]))
  25923. (message "\"Org\"-menu now contains full customization menu"))
  25924. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25925. ;;;; Miscellaneous stuff
  25926. ;;; Generally useful functions
  25927. (defun org-context ()
  25928. "Return a list of contexts of the current cursor position.
  25929. If several contexts apply, all are returned.
  25930. Each context entry is a list with a symbol naming the context, and
  25931. two positions indicating start and end of the context. Possible
  25932. contexts are:
  25933. :headline anywhere in a headline
  25934. :headline-stars on the leading stars in a headline
  25935. :todo-keyword on a TODO keyword (including DONE) in a headline
  25936. :tags on the TAGS in a headline
  25937. :priority on the priority cookie in a headline
  25938. :item on the first line of a plain list item
  25939. :item-bullet on the bullet/number of a plain list item
  25940. :checkbox on the checkbox in a plain list item
  25941. :table in an org-mode table
  25942. :table-special on a special filed in a table
  25943. :table-table in a table.el table
  25944. :link on a hyperlink
  25945. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25946. :target on a <<target>>
  25947. :radio-target on a <<<radio-target>>>
  25948. :latex-fragment on a LaTeX fragment
  25949. :latex-preview on a LaTeX fragment with overlayed preview image
  25950. This function expects the position to be visible because it uses font-lock
  25951. faces as a help to recognize the following contexts: :table-special, :link,
  25952. and :keyword."
  25953. (let* ((f (get-text-property (point) 'face))
  25954. (faces (if (listp f) f (list f)))
  25955. (p (point)) clist o)
  25956. ;; First the large context
  25957. (cond
  25958. ((org-on-heading-p t)
  25959. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25960. (when (progn
  25961. (beginning-of-line 1)
  25962. (looking-at org-todo-line-tags-regexp))
  25963. (push (org-point-in-group p 1 :headline-stars) clist)
  25964. (push (org-point-in-group p 2 :todo-keyword) clist)
  25965. (push (org-point-in-group p 4 :tags) clist))
  25966. (goto-char p)
  25967. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25968. (if (looking-at "\\[#[A-Z0-9]\\]")
  25969. (push (org-point-in-group p 0 :priority) clist)))
  25970. ((org-at-item-p)
  25971. (push (org-point-in-group p 2 :item-bullet) clist)
  25972. (push (list :item (point-at-bol)
  25973. (save-excursion (org-end-of-item) (point)))
  25974. clist)
  25975. (and (org-at-item-checkbox-p)
  25976. (push (org-point-in-group p 0 :checkbox) clist)))
  25977. ((org-at-table-p)
  25978. (push (list :table (org-table-begin) (org-table-end)) clist)
  25979. (if (memq 'org-formula faces)
  25980. (push (list :table-special
  25981. (previous-single-property-change p 'face)
  25982. (next-single-property-change p 'face)) clist)))
  25983. ((org-at-table-p 'any)
  25984. (push (list :table-table) clist)))
  25985. (goto-char p)
  25986. ;; Now the small context
  25987. (cond
  25988. ((org-at-timestamp-p)
  25989. (push (org-point-in-group p 0 :timestamp) clist))
  25990. ((memq 'org-link faces)
  25991. (push (list :link
  25992. (previous-single-property-change p 'face)
  25993. (next-single-property-change p 'face)) clist))
  25994. ((memq 'org-special-keyword faces)
  25995. (push (list :keyword
  25996. (previous-single-property-change p 'face)
  25997. (next-single-property-change p 'face)) clist))
  25998. ((org-on-target-p)
  25999. (push (org-point-in-group p 0 :target) clist)
  26000. (goto-char (1- (match-beginning 0)))
  26001. (if (looking-at org-radio-target-regexp)
  26002. (push (org-point-in-group p 0 :radio-target) clist))
  26003. (goto-char p))
  26004. ((setq o (car (delq nil
  26005. (mapcar
  26006. (lambda (x)
  26007. (if (memq x org-latex-fragment-image-overlays) x))
  26008. (org-overlays-at (point))))))
  26009. (push (list :latex-fragment
  26010. (org-overlay-start o) (org-overlay-end o)) clist)
  26011. (push (list :latex-preview
  26012. (org-overlay-start o) (org-overlay-end o)) clist))
  26013. ((org-inside-LaTeX-fragment-p)
  26014. ;; FIXME: positions wrong.
  26015. (push (list :latex-fragment (point) (point)) clist)))
  26016. (setq clist (nreverse (delq nil clist)))
  26017. clist))
  26018. ;; FIXME: Compare with at-regexp-p Do we need both?
  26019. (defun org-in-regexp (re &optional nlines visually)
  26020. "Check if point is inside a match of regexp.
  26021. Normally only the current line is checked, but you can include NLINES extra
  26022. lines both before and after point into the search.
  26023. If VISUALLY is set, require that the cursor is not after the match but
  26024. really on, so that the block visually is on the match."
  26025. (catch 'exit
  26026. (let ((pos (point))
  26027. (eol (point-at-eol (+ 1 (or nlines 0))))
  26028. (inc (if visually 1 0)))
  26029. (save-excursion
  26030. (beginning-of-line (- 1 (or nlines 0)))
  26031. (while (re-search-forward re eol t)
  26032. (if (and (<= (match-beginning 0) pos)
  26033. (>= (+ inc (match-end 0)) pos))
  26034. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  26035. (defun org-at-regexp-p (regexp)
  26036. "Is point inside a match of REGEXP in the current line?"
  26037. (catch 'exit
  26038. (save-excursion
  26039. (let ((pos (point)) (end (point-at-eol)))
  26040. (beginning-of-line 1)
  26041. (while (re-search-forward regexp end t)
  26042. (if (and (<= (match-beginning 0) pos)
  26043. (>= (match-end 0) pos))
  26044. (throw 'exit t)))
  26045. nil))))
  26046. (defun org-occur-in-agenda-files (regexp &optional nlines)
  26047. "Call `multi-occur' with buffers for all agenda files."
  26048. (interactive "sOrg-files matching: \np")
  26049. (let* ((files (org-agenda-files))
  26050. (tnames (mapcar 'file-truename files))
  26051. (extra org-agenda-text-search-extra-files)
  26052. f)
  26053. (while (setq f (pop extra))
  26054. (unless (member (file-truename f) tnames)
  26055. (add-to-list 'files f 'append)
  26056. (add-to-list 'tnames (file-truename f) 'append)))
  26057. (multi-occur
  26058. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  26059. regexp)))
  26060. (if (boundp 'occur-mode-find-occurrence-hook)
  26061. ;; Emacs 23
  26062. (add-hook 'occur-mode-find-occurrence-hook
  26063. (lambda ()
  26064. (when (org-mode-p)
  26065. (org-reveal))))
  26066. ;; Emacs 22
  26067. (defadvice occur-mode-goto-occurrence
  26068. (after org-occur-reveal activate)
  26069. (and (org-mode-p) (org-reveal)))
  26070. (defadvice occur-mode-goto-occurrence-other-window
  26071. (after org-occur-reveal activate)
  26072. (and (org-mode-p) (org-reveal)))
  26073. (defadvice occur-mode-display-occurrence
  26074. (after org-occur-reveal activate)
  26075. (when (org-mode-p)
  26076. (let ((pos (occur-mode-find-occurrence)))
  26077. (with-current-buffer (marker-buffer pos)
  26078. (save-excursion
  26079. (goto-char pos)
  26080. (org-reveal)))))))
  26081. (defun org-uniquify (list)
  26082. "Remove duplicate elements from LIST."
  26083. (let (res)
  26084. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  26085. res))
  26086. (defun org-delete-all (elts list)
  26087. "Remove all elements in ELTS from LIST."
  26088. (while elts
  26089. (setq list (delete (pop elts) list)))
  26090. list)
  26091. (defun org-back-over-empty-lines ()
  26092. "Move backwards over witespace, to the beginning of the first empty line.
  26093. Returns the number o empty lines passed."
  26094. (let ((pos (point)))
  26095. (skip-chars-backward " \t\n\r")
  26096. (beginning-of-line 2)
  26097. (goto-char (min (point) pos))
  26098. (count-lines (point) pos)))
  26099. (defun org-skip-whitespace ()
  26100. (skip-chars-forward " \t\n\r"))
  26101. (defun org-point-in-group (point group &optional context)
  26102. "Check if POINT is in match-group GROUP.
  26103. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  26104. match. If the match group does ot exist or point is not inside it,
  26105. return nil."
  26106. (and (match-beginning group)
  26107. (>= point (match-beginning group))
  26108. (<= point (match-end group))
  26109. (if context
  26110. (list context (match-beginning group) (match-end group))
  26111. t)))
  26112. (defun org-switch-to-buffer-other-window (&rest args)
  26113. "Switch to buffer in a second window on the current frame.
  26114. In particular, do not allow pop-up frames."
  26115. (let (pop-up-frames special-display-buffer-names special-display-regexps
  26116. special-display-function)
  26117. (apply 'switch-to-buffer-other-window args)))
  26118. (defun org-combine-plists (&rest plists)
  26119. "Create a single property list from all plists in PLISTS.
  26120. The process starts by copying the first list, and then setting properties
  26121. from the other lists. Settings in the last list are the most significant
  26122. ones and overrule settings in the other lists."
  26123. (let ((rtn (copy-sequence (pop plists)))
  26124. p v ls)
  26125. (while plists
  26126. (setq ls (pop plists))
  26127. (while ls
  26128. (setq p (pop ls) v (pop ls))
  26129. (setq rtn (plist-put rtn p v))))
  26130. rtn))
  26131. (defun org-move-line-down (arg)
  26132. "Move the current line down. With prefix argument, move it past ARG lines."
  26133. (interactive "p")
  26134. (let ((col (current-column))
  26135. beg end pos)
  26136. (beginning-of-line 1) (setq beg (point))
  26137. (beginning-of-line 2) (setq end (point))
  26138. (beginning-of-line (+ 1 arg))
  26139. (setq pos (move-marker (make-marker) (point)))
  26140. (insert (delete-and-extract-region beg end))
  26141. (goto-char pos)
  26142. (move-to-column col)))
  26143. (defun org-move-line-up (arg)
  26144. "Move the current line up. With prefix argument, move it past ARG lines."
  26145. (interactive "p")
  26146. (let ((col (current-column))
  26147. beg end pos)
  26148. (beginning-of-line 1) (setq beg (point))
  26149. (beginning-of-line 2) (setq end (point))
  26150. (beginning-of-line (- arg))
  26151. (setq pos (move-marker (make-marker) (point)))
  26152. (insert (delete-and-extract-region beg end))
  26153. (goto-char pos)
  26154. (move-to-column col)))
  26155. (defun org-replace-escapes (string table)
  26156. "Replace %-escapes in STRING with values in TABLE.
  26157. TABLE is an association list with keys like \"%a\" and string values.
  26158. The sequences in STRING may contain normal field width and padding information,
  26159. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  26160. so values can contain further %-escapes if they are define later in TABLE."
  26161. (let ((case-fold-search nil)
  26162. e re rpl)
  26163. (while (setq e (pop table))
  26164. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  26165. (while (string-match re string)
  26166. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  26167. (cdr e)))
  26168. (setq string (replace-match rpl t t string))))
  26169. string))
  26170. (defun org-sublist (list start end)
  26171. "Return a section of LIST, from START to END.
  26172. Counting starts at 1."
  26173. (let (rtn (c start))
  26174. (setq list (nthcdr (1- start) list))
  26175. (while (and list (<= c end))
  26176. (push (pop list) rtn)
  26177. (setq c (1+ c)))
  26178. (nreverse rtn)))
  26179. (defun org-find-base-buffer-visiting (file)
  26180. "Like `find-buffer-visiting' but alway return the base buffer and
  26181. not an indirect buffer"
  26182. (let ((buf (find-buffer-visiting file)))
  26183. (if buf
  26184. (or (buffer-base-buffer buf) buf)
  26185. nil)))
  26186. (defun org-image-file-name-regexp ()
  26187. "Return regexp matching the file names of images."
  26188. (if (fboundp 'image-file-name-regexp)
  26189. (image-file-name-regexp)
  26190. (let ((image-file-name-extensions
  26191. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  26192. "xbm" "xpm" "pbm" "pgm" "ppm")))
  26193. (concat "\\."
  26194. (regexp-opt (nconc (mapcar 'upcase
  26195. image-file-name-extensions)
  26196. image-file-name-extensions)
  26197. t)
  26198. "\\'"))))
  26199. (defun org-file-image-p (file)
  26200. "Return non-nil if FILE is an image."
  26201. (save-match-data
  26202. (string-match (org-image-file-name-regexp) file)))
  26203. ;;; Paragraph filling stuff.
  26204. ;; We want this to be just right, so use the full arsenal.
  26205. (defun org-indent-line-function ()
  26206. "Indent line like previous, but further if previous was headline or item."
  26207. (interactive)
  26208. (let* ((pos (point))
  26209. (itemp (org-at-item-p))
  26210. column bpos bcol tpos tcol bullet btype bullet-type)
  26211. ;; Find the previous relevant line
  26212. (beginning-of-line 1)
  26213. (cond
  26214. ((looking-at "#") (setq column 0))
  26215. ((looking-at "\\*+ ") (setq column 0))
  26216. (t
  26217. (beginning-of-line 0)
  26218. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  26219. (beginning-of-line 0))
  26220. (cond
  26221. ((looking-at "\\*+[ \t]+")
  26222. (goto-char (match-end 0))
  26223. (setq column (current-column)))
  26224. ((org-in-item-p)
  26225. (org-beginning-of-item)
  26226. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26227. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26228. (setq bpos (match-beginning 1) tpos (match-end 0)
  26229. bcol (progn (goto-char bpos) (current-column))
  26230. tcol (progn (goto-char tpos) (current-column))
  26231. bullet (match-string 1)
  26232. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26233. (if (not itemp)
  26234. (setq column tcol)
  26235. (goto-char pos)
  26236. (beginning-of-line 1)
  26237. (if (looking-at "\\S-")
  26238. (progn
  26239. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26240. (setq bullet (match-string 1)
  26241. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26242. (setq column (if (equal btype bullet-type) bcol tcol)))
  26243. (setq column (org-get-indentation)))))
  26244. (t (setq column (org-get-indentation))))))
  26245. (goto-char pos)
  26246. (if (<= (current-column) (current-indentation))
  26247. (indent-line-to column)
  26248. (save-excursion (indent-line-to column)))
  26249. (setq column (current-column))
  26250. (beginning-of-line 1)
  26251. (if (looking-at
  26252. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26253. (replace-match (concat "\\1" (format org-property-format
  26254. (match-string 2) (match-string 3)))
  26255. t nil))
  26256. (move-to-column column)))
  26257. (defun org-set-autofill-regexps ()
  26258. (interactive)
  26259. ;; In the paragraph separator we include headlines, because filling
  26260. ;; text in a line directly attached to a headline would otherwise
  26261. ;; fill the headline as well.
  26262. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26263. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26264. ;; The paragraph starter includes hand-formatted lists.
  26265. (org-set-local 'paragraph-start
  26266. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26267. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26268. ;; But only if the user has not turned off tables or fixed-width regions
  26269. (org-set-local
  26270. 'auto-fill-inhibit-regexp
  26271. (concat "\\*+ \\|#\\+"
  26272. "\\|[ \t]*" org-keyword-time-regexp
  26273. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26274. (concat
  26275. "\\|[ \t]*["
  26276. (if org-enable-table-editor "|" "")
  26277. (if org-enable-fixed-width-editor ":" "")
  26278. "]"))))
  26279. ;; We use our own fill-paragraph function, to make sure that tables
  26280. ;; and fixed-width regions are not wrapped. That function will pass
  26281. ;; through to `fill-paragraph' when appropriate.
  26282. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26283. ; Adaptive filling: To get full control, first make sure that
  26284. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26285. (org-set-local 'adaptive-fill-regexp "\000")
  26286. (org-set-local 'adaptive-fill-function
  26287. 'org-adaptive-fill-function)
  26288. (org-set-local
  26289. 'align-mode-rules-list
  26290. '((org-in-buffer-settings
  26291. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26292. (modes . '(org-mode))))))
  26293. (defun org-fill-paragraph (&optional justify)
  26294. "Re-align a table, pass through to fill-paragraph if no table."
  26295. (let ((table-p (org-at-table-p))
  26296. (table.el-p (org-at-table.el-p)))
  26297. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26298. (save-excursion (goto-char (point-at-bol))
  26299. (looking-at outline-regexp)))
  26300. t) ; skip headlines
  26301. (table.el-p t) ; skip table.el tables
  26302. (table-p (org-table-align) t) ; align org-mode tables
  26303. (t nil)))) ; call paragraph-fill
  26304. ;; For reference, this is the default value of adaptive-fill-regexp
  26305. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26306. (defun org-adaptive-fill-function ()
  26307. "Return a fill prefix for org-mode files.
  26308. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26309. work correctly."
  26310. (cond ((looking-at "#[ \t]+")
  26311. (match-string 0))
  26312. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26313. (save-excursion
  26314. (goto-char (match-end 0))
  26315. (make-string (current-column) ?\ )))
  26316. (t nil)))
  26317. ;;;; Functions extending outline functionality
  26318. (defun org-beginning-of-line (&optional arg)
  26319. "Go to the beginning of the current line. If that is invisible, continue
  26320. to a visible line beginning. This makes the function of C-a more intuitive.
  26321. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26322. first attempt, and only move to after the tags when the cursor is already
  26323. beyond the end of the headline."
  26324. (interactive "P")
  26325. (let ((pos (point)))
  26326. (beginning-of-line 1)
  26327. (if (bobp)
  26328. nil
  26329. (backward-char 1)
  26330. (if (org-invisible-p)
  26331. (while (and (not (bobp)) (org-invisible-p))
  26332. (backward-char 1)
  26333. (beginning-of-line 1))
  26334. (forward-char 1)))
  26335. (when org-special-ctrl-a/e
  26336. (cond
  26337. ((and (looking-at org-todo-line-regexp)
  26338. (= (char-after (match-end 1)) ?\ ))
  26339. (goto-char
  26340. (if (eq org-special-ctrl-a/e t)
  26341. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26342. ((= pos (point)) (match-beginning 3))
  26343. (t (point)))
  26344. (cond ((> pos (point)) (point))
  26345. ((not (eq last-command this-command)) (point))
  26346. (t (match-beginning 3))))))
  26347. ((org-at-item-p)
  26348. (goto-char
  26349. (if (eq org-special-ctrl-a/e t)
  26350. (cond ((> pos (match-end 4)) (match-end 4))
  26351. ((= pos (point)) (match-end 4))
  26352. (t (point)))
  26353. (cond ((> pos (point)) (point))
  26354. ((not (eq last-command this-command)) (point))
  26355. (t (match-end 4))))))))))
  26356. (defun org-end-of-line (&optional arg)
  26357. "Go to the end of the line.
  26358. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26359. first attempt, and only move to after the tags when the cursor is already
  26360. beyond the end of the headline."
  26361. (interactive "P")
  26362. (if (or (not org-special-ctrl-a/e)
  26363. (not (org-on-heading-p)))
  26364. (end-of-line arg)
  26365. (let ((pos (point)))
  26366. (beginning-of-line 1)
  26367. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26368. (if (eq org-special-ctrl-a/e t)
  26369. (if (or (< pos (match-beginning 1))
  26370. (= pos (match-end 0)))
  26371. (goto-char (match-beginning 1))
  26372. (goto-char (match-end 0)))
  26373. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26374. (goto-char (match-end 0))
  26375. (goto-char (match-beginning 1))))
  26376. (end-of-line arg)))))
  26377. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26378. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26379. (defun org-kill-line (&optional arg)
  26380. "Kill line, to tags or end of line."
  26381. (interactive "P")
  26382. (cond
  26383. ((or (not org-special-ctrl-k)
  26384. (bolp)
  26385. (not (org-on-heading-p)))
  26386. (call-interactively 'kill-line))
  26387. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26388. (kill-region (point) (match-beginning 1))
  26389. (org-set-tags nil t))
  26390. (t (kill-region (point) (point-at-eol)))))
  26391. (define-key org-mode-map "\C-k" 'org-kill-line)
  26392. (defun org-invisible-p ()
  26393. "Check if point is at a character currently not visible."
  26394. ;; Early versions of noutline don't have `outline-invisible-p'.
  26395. (if (fboundp 'outline-invisible-p)
  26396. (outline-invisible-p)
  26397. (get-char-property (point) 'invisible)))
  26398. (defun org-invisible-p2 ()
  26399. "Check if point is at a character currently not visible."
  26400. (save-excursion
  26401. (if (and (eolp) (not (bobp))) (backward-char 1))
  26402. ;; Early versions of noutline don't have `outline-invisible-p'.
  26403. (if (fboundp 'outline-invisible-p)
  26404. (outline-invisible-p)
  26405. (get-char-property (point) 'invisible))))
  26406. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26407. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26408. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26409. (defun org-at-heading-or-item-p ()
  26410. (or (org-on-heading-p) (org-at-item-p)))
  26411. (defun org-on-target-p ()
  26412. (or (org-in-regexp org-radio-target-regexp)
  26413. (org-in-regexp org-target-regexp)))
  26414. (defun org-up-heading-all (arg)
  26415. "Move to the heading line of which the present line is a subheading.
  26416. This function considers both visible and invisible heading lines.
  26417. With argument, move up ARG levels."
  26418. (if (fboundp 'outline-up-heading-all)
  26419. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26420. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26421. (defun org-up-heading-safe ()
  26422. "Move to the heading line of which the present line is a subheading.
  26423. This version will not throw an error. It will return the level of the
  26424. headline found, or nil if no higher level is found."
  26425. (let ((pos (point)) start-level level
  26426. (re (concat "^" outline-regexp)))
  26427. (catch 'exit
  26428. (outline-back-to-heading t)
  26429. (setq start-level (funcall outline-level))
  26430. (if (equal start-level 1) (throw 'exit nil))
  26431. (while (re-search-backward re nil t)
  26432. (setq level (funcall outline-level))
  26433. (if (< level start-level) (throw 'exit level)))
  26434. nil)))
  26435. (defun org-first-sibling-p ()
  26436. "Is this heading the first child of its parents?"
  26437. (interactive)
  26438. (let ((re (concat "^" outline-regexp))
  26439. level l)
  26440. (unless (org-at-heading-p t)
  26441. (error "Not at a heading"))
  26442. (setq level (funcall outline-level))
  26443. (save-excursion
  26444. (if (not (re-search-backward re nil t))
  26445. t
  26446. (setq l (funcall outline-level))
  26447. (< l level)))))
  26448. (defun org-goto-sibling (&optional previous)
  26449. "Goto the next sibling, even if it is invisible.
  26450. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26451. when a sibling was found. When none is found, return nil and don't
  26452. move point."
  26453. (let ((fun (if previous 're-search-backward 're-search-forward))
  26454. (pos (point))
  26455. (re (concat "^" outline-regexp))
  26456. level l)
  26457. (when (condition-case nil (org-back-to-heading t) (error nil))
  26458. (setq level (funcall outline-level))
  26459. (catch 'exit
  26460. (or previous (forward-char 1))
  26461. (while (funcall fun re nil t)
  26462. (setq l (funcall outline-level))
  26463. (when (< l level) (goto-char pos) (throw 'exit nil))
  26464. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26465. (goto-char pos)
  26466. nil))))
  26467. (defun org-show-siblings ()
  26468. "Show all siblings of the current headline."
  26469. (save-excursion
  26470. (while (org-goto-sibling) (org-flag-heading nil)))
  26471. (save-excursion
  26472. (while (org-goto-sibling 'previous)
  26473. (org-flag-heading nil))))
  26474. (defun org-show-hidden-entry ()
  26475. "Show an entry where even the heading is hidden."
  26476. (save-excursion
  26477. (org-show-entry)))
  26478. (defun org-flag-heading (flag &optional entry)
  26479. "Flag the current heading. FLAG non-nil means make invisible.
  26480. When ENTRY is non-nil, show the entire entry."
  26481. (save-excursion
  26482. (org-back-to-heading t)
  26483. ;; Check if we should show the entire entry
  26484. (if entry
  26485. (progn
  26486. (org-show-entry)
  26487. (save-excursion
  26488. (and (outline-next-heading)
  26489. (org-flag-heading nil))))
  26490. (outline-flag-region (max (point-min) (1- (point)))
  26491. (save-excursion (outline-end-of-heading) (point))
  26492. flag))))
  26493. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26494. ;; This is an exact copy of the original function, but it uses
  26495. ;; `org-back-to-heading', to make it work also in invisible
  26496. ;; trees. And is uses an invisible-OK argument.
  26497. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26498. (org-back-to-heading invisible-OK)
  26499. (let ((first t)
  26500. (level (funcall outline-level)))
  26501. (while (and (not (eobp))
  26502. (or first (> (funcall outline-level) level)))
  26503. (setq first nil)
  26504. (outline-next-heading))
  26505. (unless to-heading
  26506. (if (memq (preceding-char) '(?\n ?\^M))
  26507. (progn
  26508. ;; Go to end of line before heading
  26509. (forward-char -1)
  26510. (if (memq (preceding-char) '(?\n ?\^M))
  26511. ;; leave blank line before heading
  26512. (forward-char -1))))))
  26513. (point))
  26514. (defun org-show-subtree ()
  26515. "Show everything after this heading at deeper levels."
  26516. (outline-flag-region
  26517. (point)
  26518. (save-excursion
  26519. (outline-end-of-subtree) (outline-next-heading) (point))
  26520. nil))
  26521. (defun org-show-entry ()
  26522. "Show the body directly following this heading.
  26523. Show the heading too, if it is currently invisible."
  26524. (interactive)
  26525. (save-excursion
  26526. (condition-case nil
  26527. (progn
  26528. (org-back-to-heading t)
  26529. (outline-flag-region
  26530. (max (point-min) (1- (point)))
  26531. (save-excursion
  26532. (re-search-forward
  26533. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26534. (or (match-beginning 1) (point-max)))
  26535. nil))
  26536. (error nil))))
  26537. (defun org-make-options-regexp (kwds)
  26538. "Make a regular expression for keyword lines."
  26539. (concat
  26540. "^"
  26541. "#?[ \t]*\\+\\("
  26542. (mapconcat 'regexp-quote kwds "\\|")
  26543. "\\):[ \t]*"
  26544. "\\(.+\\)"))
  26545. ;; Make isearch reveal the necessary context
  26546. (defun org-isearch-end ()
  26547. "Reveal context after isearch exits."
  26548. (when isearch-success ; only if search was successful
  26549. (if (featurep 'xemacs)
  26550. ;; Under XEmacs, the hook is run in the correct place,
  26551. ;; we directly show the context.
  26552. (org-show-context 'isearch)
  26553. ;; In Emacs the hook runs *before* restoring the overlays.
  26554. ;; So we have to use a one-time post-command-hook to do this.
  26555. ;; (Emacs 22 has a special variable, see function `org-mode')
  26556. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26557. isearch-mode-end-hook-quit)
  26558. ;; Only when the isearch was not quitted.
  26559. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26560. 'append 'local)))))
  26561. (defun org-isearch-post-command ()
  26562. "Remove self from hook, and show context."
  26563. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26564. (org-show-context 'isearch))
  26565. ;;;; Integration with and fixes for other packages
  26566. ;;; Imenu support
  26567. (defvar org-imenu-markers nil
  26568. "All markers currently used by Imenu.")
  26569. (make-variable-buffer-local 'org-imenu-markers)
  26570. (defun org-imenu-new-marker (&optional pos)
  26571. "Return a new marker for use by Imenu, and remember the marker."
  26572. (let ((m (make-marker)))
  26573. (move-marker m (or pos (point)))
  26574. (push m org-imenu-markers)
  26575. m))
  26576. (defun org-imenu-get-tree ()
  26577. "Produce the index for Imenu."
  26578. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26579. (setq org-imenu-markers nil)
  26580. (let* ((n org-imenu-depth)
  26581. (re (concat "^" outline-regexp))
  26582. (subs (make-vector (1+ n) nil))
  26583. (last-level 0)
  26584. m tree level head)
  26585. (save-excursion
  26586. (save-restriction
  26587. (widen)
  26588. (goto-char (point-max))
  26589. (while (re-search-backward re nil t)
  26590. (setq level (org-reduced-level (funcall outline-level)))
  26591. (when (<= level n)
  26592. (looking-at org-complex-heading-regexp)
  26593. (setq head (org-match-string-no-properties 4)
  26594. m (org-imenu-new-marker))
  26595. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26596. (if (>= level last-level)
  26597. (push (cons head m) (aref subs level))
  26598. (push (cons head (aref subs (1+ level))) (aref subs level))
  26599. (loop for i from (1+ level) to n do (aset subs i nil)))
  26600. (setq last-level level)))))
  26601. (aref subs 1)))
  26602. (eval-after-load "imenu"
  26603. '(progn
  26604. (add-hook 'imenu-after-jump-hook
  26605. (lambda () (org-show-context 'org-goto)))))
  26606. ;; Speedbar support
  26607. (defun org-speedbar-set-agenda-restriction ()
  26608. "Restrict future agenda commands to the location at point in speedbar.
  26609. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26610. (interactive)
  26611. (let (p m tp np dir txt w)
  26612. (cond
  26613. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26614. 'org-imenu t))
  26615. (setq m (get-text-property p 'org-imenu-marker))
  26616. (save-excursion
  26617. (save-restriction
  26618. (set-buffer (marker-buffer m))
  26619. (goto-char m)
  26620. (org-agenda-set-restriction-lock 'subtree))))
  26621. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26622. 'speedbar-function 'speedbar-find-file))
  26623. (setq tp (previous-single-property-change
  26624. (1+ p) 'speedbar-function)
  26625. np (next-single-property-change
  26626. tp 'speedbar-function)
  26627. dir (speedbar-line-directory)
  26628. txt (buffer-substring-no-properties (or tp (point-min))
  26629. (or np (point-max))))
  26630. (save-excursion
  26631. (save-restriction
  26632. (set-buffer (find-file-noselect
  26633. (let ((default-directory dir))
  26634. (expand-file-name txt))))
  26635. (unless (org-mode-p)
  26636. (error "Cannot restrict to non-Org-mode file"))
  26637. (org-agenda-set-restriction-lock 'file))))
  26638. (t (error "Don't know how to restrict Org-mode's agenda")))
  26639. (org-move-overlay org-speedbar-restriction-lock-overlay
  26640. (point-at-bol) (point-at-eol))
  26641. (setq current-prefix-arg nil)
  26642. (org-agenda-maybe-redo)))
  26643. (eval-after-load "speedbar"
  26644. '(progn
  26645. (speedbar-add-supported-extension ".org")
  26646. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26647. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26648. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26649. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26650. (add-hook 'speedbar-visiting-tag-hook
  26651. (lambda () (org-show-context 'org-goto)))))
  26652. ;;; Fixes and Hacks
  26653. ;; Make flyspell not check words in links, to not mess up our keymap
  26654. (defun org-mode-flyspell-verify ()
  26655. "Don't let flyspell put overlays at active buttons."
  26656. (not (get-text-property (point) 'keymap)))
  26657. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26658. (eval-after-load "bookmark"
  26659. '(if (boundp 'bookmark-after-jump-hook)
  26660. ;; We can use the hook
  26661. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26662. ;; Hook not available, use advice
  26663. (defadvice bookmark-jump (after org-make-visible activate)
  26664. "Make the position visible."
  26665. (org-bookmark-jump-unhide))))
  26666. (defun org-bookmark-jump-unhide ()
  26667. "Unhide the current position, to show the bookmark location."
  26668. (and (org-mode-p)
  26669. (or (org-invisible-p)
  26670. (save-excursion (goto-char (max (point-min) (1- (point))))
  26671. (org-invisible-p)))
  26672. (org-show-context 'bookmark-jump)))
  26673. ;; Make session.el ignore our circular variable
  26674. (eval-after-load "session"
  26675. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26676. ;;;; Experimental code
  26677. (defun org-closed-in-range ()
  26678. "Sparse tree of items closed in a certain time range.
  26679. Still experimental, may disappear in the future."
  26680. (interactive)
  26681. ;; Get the time interval from the user.
  26682. (let* ((time1 (time-to-seconds
  26683. (org-read-date nil 'to-time nil "Starting date: ")))
  26684. (time2 (time-to-seconds
  26685. (org-read-date nil 'to-time nil "End date:")))
  26686. ;; callback function
  26687. (callback (lambda ()
  26688. (let ((time
  26689. (time-to-seconds
  26690. (apply 'encode-time
  26691. (org-parse-time-string
  26692. (match-string 1))))))
  26693. ;; check if time in interval
  26694. (and (>= time time1) (<= time time2))))))
  26695. ;; make tree, check each match with the callback
  26696. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26697. ;;;; Finish up
  26698. (provide 'org)
  26699. (run-hooks 'org-load-hook)
  26700. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26701. ;;; org.el ends here