org.el 644 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.54
  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 2, 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://staff.science.uva.nl/~dominik/Tools/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. ;; Recent changes
  61. ;; --------------
  62. ;; Version 4.54
  63. ;; - Improvements to fast tag selection
  64. ;; + show status also in target line.
  65. ;; + option to auto-exit after first change to tags list (see manual).
  66. ;; - Tags sparse trees now also respect the settings in
  67. ;; `org-show-hierarchy-above' and `org-show-following-heading'.
  68. ;; - Bug fixes.
  69. ;;
  70. ;; Version 4.53
  71. ;; - Custom time formats can be overlayed over time stamps.
  72. ;; - New option `org-agenda-todo-ignore-deadlines'.
  73. ;; - Work-around for flyspell bug (CVS Emacs has this fixed in flyspell.el).
  74. ;; - Work-around for session.el problem with circular data structures.
  75. ;; - Bug fixes.
  76. ;;
  77. ;; Version 4.52
  78. ;; - TAG matches can also specify conditions on TODO keywords.
  79. ;; - The fast tag interface allows setting tags that are not in the
  80. ;; predefined list.
  81. ;; - Bug fixes.
  82. ;;
  83. ;; Version 4.51
  84. ;; - Link abbreviations (manual section 4.5).
  85. ;; - More control over how agenda is displayed. See the new variables
  86. ;; `org-agenda-window-setup', `org-agenda-restore-windows-after-quit'.
  87. ;; - Bug fixes.
  88. ;;
  89. ;; Version 4.50
  90. ;; - Closing a TODO item can record an additional note.
  91. ;; See variables `org-log-done' and `org-log-note-headings'.
  92. ;; - Inserting headlines and bullets can leave an extra blank line.
  93. ;; See variable `org-blank-before-new-entry'. (Ed Hirgelt patch)
  94. ;; - [[bracket links]] in the agenda are active just as in org-mode buffers.
  95. ;; - C-c C-o on a date range displays the agenda for exactly this range.
  96. ;; - The default for `org-cycle-include-plain-lists' is back to nil.
  97. ;; - Calls to `org-occur' can be stacked by using a prefix argument.
  98. ;; - The options `org-show-hierarchy-above' and `org-show-following-heading'
  99. ;; now always default to `t', but can be customized differently for
  100. ;; different types of sparse trees or jump commands.
  101. ;; - Bug fixes.
  102. ;;
  103. ;; Version 4.49
  104. ;; - Agenda views can be made in batch mode from the command line.
  105. ;; - `org-store-link' does the right thing in dired-mode.
  106. ;; - File links can contain environment variables.
  107. ;; - Full Emacs 21 compatibility has been restored.
  108. ;; - Bug fixes.
  109. ;;
  110. ;; Version 4.47
  111. ;; - Custom commands may produce an agenda which contains several blocks,
  112. ;; each block created by a different agenda command.
  113. ;; - Agenda commands can be restricted to the current file, region, subtree.
  114. ;; - The timeline command must now be called through the agenda
  115. ;; dispatcher (C-c a L). `C-c C-r' no longer works.
  116. ;; - Agenda items can be sorted by tag. The *last* tag is used for this.
  117. ;; - The prefix and the sorting strategy for agenda items can depend
  118. ;; upon the agenda type.
  119. ;; - The handling of `mailto:' links can be customized, see the new
  120. ;; variable `org-link-mailto-program'.
  121. ;; - `mailto' links can specify a subject after a double colon,
  122. ;; like [[mailto:carsten@orgmode.org::Org-mode is buggy]].
  123. ;; - In the #+STARTUP line, M-TAB completes valid keywords.
  124. ;; - In the #+TAGS: line, M-TAB after ":" inserts all currently used tags.
  125. ;; - Again full Emacs 21 support: Checkboxes and publishing are fixed.
  126. ;; - More minor bug fixes.
  127. ;;
  128. ;; Version 4.45
  129. ;; - Checkbox lists can show statistics about checked items.
  130. ;; - C-TAB will cycle the visibility of archived subtrees.
  131. ;;; - Documentation about checkboxes has been moved to chapter 5.
  132. ;; - Bux fixes.
  133. ;;
  134. ;; Version 4.44
  135. ;; - Clock table can be done for a limited time interval.
  136. ;; - Obsolete support for the old outline mode has been removed.
  137. ;; - Bug fixes and code cleaning.
  138. ;;
  139. ;; Version 4.43
  140. ;; - Bug fixes
  141. ;; - `s' key in the agenda saves all org-mode buffers.
  142. ;;
  143. ;; Version 4.41
  144. ;; - Shift-curser keys can modify inactive time stamps (inactive time
  145. ;; stamps are the ones in [...] brackets.
  146. ;; - Toggle all checkboxes in a region/below a headline.
  147. ;; - Bug fixes.
  148. ;;
  149. ;;; Code:
  150. (eval-when-compile
  151. (require 'cl)
  152. (require 'calendar))
  153. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  154. ;; the file noutline.el being loaded.
  155. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  156. ;; We require noutline, which might be provided in outline.el
  157. (require 'outline) (require 'noutline)
  158. ;; Other stuff we need.
  159. (require 'time-date)
  160. (require 'easymenu)
  161. ;;; Customization variables
  162. (defvar org-version "4.54"
  163. "The version number of the file org.el.")
  164. (defun org-version ()
  165. (interactive)
  166. (message "Org-mode version %s" org-version))
  167. ;; Compatibility constants
  168. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  169. (defconst org-format-transports-properties-p
  170. (let ((x "a"))
  171. (add-text-properties 0 1 '(test t) x)
  172. (get-text-property 0 'test (format "%s" x)))
  173. "Does format transport text properties?")
  174. (defgroup org nil
  175. "Outline-based notes management and organizer."
  176. :tag "Org"
  177. :group 'outlines
  178. :group 'hypermedia
  179. :group 'calendar)
  180. (defgroup org-startup nil
  181. "Options concerning startup of Org-mode."
  182. :tag "Org Startup"
  183. :group 'org)
  184. (defcustom org-startup-folded t
  185. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  186. This can also be configured on a per-file basis by adding one of
  187. the following lines anywhere in the buffer:
  188. #+STARTUP: fold
  189. #+STARTUP: nofold
  190. #+STARTUP: content"
  191. :group 'org-startup
  192. :type '(choice
  193. (const :tag "nofold: show all" nil)
  194. (const :tag "fold: overview" t)
  195. (const :tag "content: all headlines" content)))
  196. (defcustom org-startup-truncated t
  197. "Non-nil means, entering Org-mode will set `truncate-lines'.
  198. This is useful since some lines containing links can be very long and
  199. uninteresting. Also tables look terrible when wrapped."
  200. :group 'org-startup
  201. :type 'boolean)
  202. (defcustom org-startup-align-all-tables nil
  203. "Non-nil means, align all tables when visiting a file.
  204. This is useful when the column width in tables is forced with <N> cookies
  205. in table fields. Such tables will look correct only after the first re-align.
  206. This can also be configured on a per-file basis by adding one of
  207. the following lines anywhere in the buffer:
  208. #+STARTUP: align
  209. #+STARTUP: noalign"
  210. :group 'org-startup
  211. :type 'boolean)
  212. (defcustom org-startup-with-deadline-check nil
  213. "Non-nil means, entering Org-mode will run the deadline check.
  214. This means, if you start editing an org file, you will get an
  215. immediate reminder of any due deadlines.
  216. This can also be configured on a per-file basis by adding one of
  217. the following lines anywhere in the buffer:
  218. #+STARTUP: dlcheck
  219. #+STARTUP: nodlcheck"
  220. :group 'org-startup
  221. :type 'boolean)
  222. (defcustom org-insert-mode-line-in-empty-file nil
  223. "Non-nil means insert the first line setting Org-mode in empty files.
  224. When the function `org-mode' is called interactively in an empty file, this
  225. normally means that the file name does not automatically trigger Org-mode.
  226. To ensure that the file will always be in Org-mode in the future, a
  227. line enforcing Org-mode will be inserted into the buffer, if this option
  228. has been set."
  229. :group 'org-startup
  230. :type 'boolean)
  231. (defcustom org-CUA-compatible nil
  232. "Non-nil means use alternative key bindings for S-<cursor movement>.
  233. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  234. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  235. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  236. alternative bindings. Setting this variable to t will replace the following
  237. keys both in Org-mode and in the Org-agenda buffer.
  238. S-RET -> C-S-RET
  239. S-up -> M-p
  240. S-down -> M-n
  241. S-left -> M--
  242. S-right -> M-+
  243. If you do not like the alternative keys, take a look at the variable
  244. `org-disputed-keys'.
  245. This option is only relevant at load-time of Org-mode. Changing it requires
  246. a restart of Emacs to become effective."
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defvar org-disputed-keys
  250. '((S-up [(shift up)] [(meta ?p)])
  251. (S-down [(shift down)] [(meta ?n)])
  252. (S-left [(shift left)] [(meta ?-)])
  253. (S-right [(shift right)] [(meta ?+)])
  254. (S-return [(shift return)] [(control shift return)]))
  255. "Keys for which Org-mode and other modes compete.
  256. This is an alist, cars are symbols for lookup, 1st element is the default key,
  257. second element will be used when `org-CUA-compatible' is t.")
  258. (defun org-key (key)
  259. "Select a key according to `org-CUA-compatible'."
  260. (nth (if org-CUA-compatible 2 1)
  261. (or (assq key org-disputed-keys)
  262. (error "Invalid Key %s in `org-key'" key))))
  263. (defcustom org-ellipsis nil
  264. "The ellipsis to use in the Org-mode outline.
  265. When nil, just use the standard three dots. When a string, use that instead,
  266. and just in Org-mode (which will then use its own display table).
  267. Changing this requires executing `M-x org-mode' in a buffer to become
  268. effective."
  269. :group 'org-startup
  270. :type '(choice (const :tag "Default" nil)
  271. (string :tag "String" :value "...#")))
  272. (defvar org-display-table nil
  273. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  274. (defgroup org-keywords nil
  275. "Keywords in Org-mode."
  276. :tag "Org Keywords"
  277. :group 'org)
  278. (defcustom org-deadline-string "DEADLINE:"
  279. "String to mark deadline entries.
  280. A deadline is this string, followed by a time stamp. Should be a word,
  281. terminated by a colon. You can insert a schedule keyword and
  282. a timestamp with \\[org-deadline].
  283. Changes become only effective after restarting Emacs."
  284. :group 'org-keywords
  285. :type 'string)
  286. (defcustom org-scheduled-string "SCHEDULED:"
  287. "String to mark scheduled TODO entries.
  288. A schedule is this string, followed by a time stamp. Should be a word,
  289. terminated by a colon. You can insert a schedule keyword and
  290. a timestamp with \\[org-schedule].
  291. Changes become only effective after restarting Emacs."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-closed-string "CLOSED:"
  295. "String used as the prefix for timestamps logging closing a TODO entry."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-clock-string "CLOCK:"
  299. "String used as prefix for timestamps clocking work hours on an item."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-comment-string "COMMENT"
  303. "Entries starting with this keyword will never be exported.
  304. An entry can be toggled between COMMENT and normal with
  305. \\[org-toggle-comment].
  306. Changes become only effective after restarting Emacs."
  307. :group 'org-keywords
  308. :type 'string)
  309. (defcustom org-quote-string "QUOTE"
  310. "Entries starting with this keyword will be exported in fixed-width font.
  311. Quoting applies only to the text in the entry following the headline, and does
  312. not extend beyond the next headline, even if that is lower level.
  313. An entry can be toggled between QUOTE and normal with
  314. \\[org-toggle-fixed-width-section]."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-cycle nil
  322. "Options concerning visibility cycling in Org-mode."
  323. :tag "Org Cycle"
  324. :group 'org-structure)
  325. (defcustom org-cycle-global-at-bob t
  326. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  327. This makes it possible to do global cycling without having to use S-TAB or
  328. C-u TAB. For this special case to work, the first line of the buffer
  329. must not be a headline - it may be empty ot some other text. When used in
  330. this way, `org-cycle-hook' is disables temporarily, to make sure the
  331. cursor stays at the beginning of the buffer.
  332. When this option is nil, don't do anything special at the beginning
  333. of the buffer."
  334. :group 'org-cycle
  335. :type 'boolean)
  336. (defcustom org-cycle-emulate-tab t
  337. "Where should `org-cycle' emulate TAB.
  338. nil Never
  339. white Only in completely white lines
  340. whitestart Only at the beginning of lines, before the first non-white char.
  341. t Everywhere except in headlines
  342. If TAB is used in a place where it does not emulate TAB, the current subtree
  343. visibility is cycled."
  344. :group 'org-cycle
  345. :type '(choice (const :tag "Never" nil)
  346. (const :tag "Only in completely white lines" white)
  347. (const :tag "Before first char in a line" whitestart)
  348. (const :tag "Everywhere except in headlines" t)
  349. ))
  350. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  351. org-optimize-window-after-visibility-change)
  352. "Hook that is run after `org-cycle' has changed the buffer visibility.
  353. The function(s) in this hook must accept a single argument which indicates
  354. the new state that was set by the most recent `org-cycle' command. The
  355. argument is a symbol. After a global state change, it can have the values
  356. `overview', `content', or `all'. After a local state change, it can have
  357. the values `folded', `children', or `subtree'."
  358. :group 'org-cycle
  359. :type 'hook)
  360. (defgroup org-edit-structure nil
  361. "Options concerning structure editing in Org-mode."
  362. :tag "Org Edit Structure"
  363. :group 'org-structure)
  364. (defcustom org-odd-levels-only nil
  365. "Non-nil means, skip even levels and only use odd levels for the outline.
  366. This has the effect that two stars are being added/taken away in
  367. promotion/demotion commands. It also influences how levels are
  368. handled by the exporters.
  369. Changing it requires restart of `font-lock-mode' to become effective
  370. for fontification also in regions already fontified.
  371. You may also set this on a per-file basis by adding one of the following
  372. lines to the buffer:
  373. #+STARTUP: odd
  374. #+STARTUP: oddeven"
  375. :group 'org-edit-structure
  376. :group 'org-font-lock
  377. :type 'boolean)
  378. (defcustom org-adapt-indentation t
  379. "Non-nil means, adapt indentation when promoting and demoting.
  380. When this is set and the *entire* text in an entry is indented, the
  381. indentation is increased by one space in a demotion command, and
  382. decreased by one in a promotion command. If any line in the entry
  383. body starts at column 0, indentation is not changed at all."
  384. :group 'org-edit-structure
  385. :type 'boolean)
  386. (defcustom org-blank-before-new-entry '((heading . nil)
  387. (plain-list-item . nil))
  388. "Should `org-insert-heading' leave a blank line before new heading/item?
  389. The value is an alist, with `heading' and `plain-list-item' as car,
  390. and a boolean flag as cdr."
  391. :group 'org-edit-structure
  392. :type '(list
  393. (cons (const heading) (boolean))
  394. (cons (const plain-list-item) (boolean))))
  395. (defcustom org-insert-heading-hook nil
  396. "Hook being run after inserting a new heading."
  397. :group 'org-edit-structure
  398. :type 'boolean)
  399. (defcustom org-enable-fixed-width-editor t
  400. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  401. This currently only means, they are never auto-wrapped.
  402. When nil, such lines will be treated like ordinary lines.
  403. See also the QUOTE keyword."
  404. :group 'org-edit-structure
  405. :type 'boolean)
  406. (defgroup org-sparse-trees nil
  407. "Options concerning sparse trees in Org-mode."
  408. :tag "Org Sparse Trees"
  409. :group 'org-structure)
  410. (defcustom org-highlight-sparse-tree-matches t
  411. "Non-nil means, highlight all matches that define a sparse tree.
  412. The highlights will automatically disappear the next time the buffer is
  413. changed by an edit command."
  414. :group 'org-sparse-trees
  415. :type 'boolean)
  416. (defcustom org-remove-highlights-with-change t
  417. "Non-nil means, any change to the buffer will remove temporary highlights.
  418. Such highlights are created by `org-occur' and `org-clock-display'.
  419. When nil, `C-c C-c needs to be used to get rid of the highlights.
  420. The highlights created by `org-preview-latex-fragment' always need
  421. `C-c C-c' to be removed."
  422. :group 'org-sparse-trees
  423. :group 'org-time
  424. :type 'boolean)
  425. (defcustom org-show-hierarchy-above '((default . t))
  426. "Non-nil means, show full hierarchy when showing a spot in the tree.
  427. Turning this off makes sparse trees more compact, but also less clear.
  428. Instead of t, this can also be an alist specifying this option for different
  429. contexts. Valid contexts are
  430. agenda when exposing an entry from the agenda
  431. org-goto when using the command `org-goto' on key C-c C-j
  432. occur-tree when using the command `org-occur' on key C-c /
  433. tags-tree when constructing a sparse tree based on tags matches
  434. link-search when exposing search matches associated with a link
  435. mark-goto when exposing the jump goal of a mark
  436. bookmark-jump when exposing a bookmark location
  437. default default for all contexts not set explicitly"
  438. :group 'org-sparse-trees
  439. :type '(choice
  440. (const :tag "Always" t)
  441. (const :tag "Never" nil)
  442. (repeat :greedy t :tag "Individual contexts"
  443. (cons
  444. (choice :tag "Context"
  445. (const agenda)
  446. (const org-goto)
  447. (const occur-tree)
  448. (const tags-tree)
  449. (const link-search)
  450. (const mark-goto)
  451. (const bookmark-jump)
  452. (const default))
  453. (boolean)))))
  454. (defcustom org-show-following-heading '((default . t))
  455. "Non-nil means, show heading following match in `org-occur'.
  456. When doing an `org-occur' it is useful to show the headline which
  457. follows the match, even if they do not match the regexp. This makes it
  458. easier to edit directly inside the sparse tree. However, if you use
  459. `org-occur' mainly as an overview, the following headlines are
  460. unnecessary clutter.
  461. Instead of t, this can also be an alist specifying this option for different
  462. contexts. See `org-show-hierarchy-above' for valid contexts."
  463. :group 'org-sparse-trees
  464. :type '(choice
  465. (const :tag "Always" t)
  466. (const :tag "Never" nil)
  467. (repeat :greedy t :tag "Individual contexts"
  468. (cons
  469. (choice :tag "Context"
  470. (const agenda)
  471. (const org-goto)
  472. (const occur-tree)
  473. (const tags-tree)
  474. (const link-search)
  475. (const mark-goto)
  476. (const bookmark-jump)
  477. (const default))
  478. (boolean)))))
  479. (defcustom org-occur-hook '(org-first-headline-recenter)
  480. "Hook that is run after `org-occur' has constructed a sparse tree.
  481. This can be used to recenter the window to show as much of the structure
  482. as possible."
  483. :group 'org-sparse-trees
  484. :type 'hook)
  485. (defgroup org-plain-lists nil
  486. "Options concerning plain lists in Org-mode."
  487. :tag "Org Plain lists"
  488. :group 'org-structure)
  489. (defcustom org-cycle-include-plain-lists nil
  490. "Non-nil means, include plain lists into visibility cycling.
  491. This means that during cycling, plain list items will *temporarily* be
  492. interpreted as outline headlines with a level given by 1000+i where i is the
  493. indentation of the bullet. In all other operations, plain list items are
  494. not seen as headlines. For example, you cannot assign a TODO keyword to
  495. such an item."
  496. :group 'org-plain-lists
  497. :type 'boolean)
  498. (defcustom org-plain-list-ordered-item-terminator t
  499. "The character that makes a line with leading number an ordered list item.
  500. Valid values are ?. and ?\). To get both terminators, use t. While
  501. ?. may look nicer, it creates the danger that a line with leading
  502. number may be incorrectly interpreted as an item. ?\) therefore is
  503. the safe choice."
  504. :group 'org-plain-lists
  505. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  506. (const :tag "paren like in \"2)\"" ?\))
  507. (const :tab "both" t)))
  508. (defcustom org-auto-renumber-ordered-lists t
  509. "Non-nil means, automatically renumber ordered plain lists.
  510. Renumbering happens when the sequence have been changed with
  511. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  512. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  513. :group 'org-plain-lists
  514. :type 'boolean)
  515. (defcustom org-provide-checkbox-statistics t
  516. "Non-nil means, update checkbox statistics after insert and toggle.
  517. When this is set, checkbox statistics is updated each time you either insert
  518. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  519. with \\[org-ctrl-c-ctrl-c\\]."
  520. :group 'org
  521. :type 'boolean)
  522. (defgroup org-archive nil
  523. "Options concerning archiving in Org-mode."
  524. :tag "Org Archive"
  525. :group 'org-structure)
  526. (defcustom org-archive-tag "ARCHIVE"
  527. "The tag that marks a subtree as archived.
  528. An archived subtree does not open during visibility cycling, and does
  529. not contribute to the agenda listings."
  530. :group 'org-archive
  531. :group 'org-keywords
  532. :type 'string)
  533. (defcustom org-agenda-skip-archived-trees t
  534. "Non-nil means, the agenda will skip any items located in archived trees.
  535. An archived tree is a tree marked with the tag ARCHIVE."
  536. :group 'org-archive
  537. :group 'org-agenda-display
  538. :type 'boolean)
  539. (defcustom org-cycle-open-archived-trees nil
  540. "Non-nil means, `org-cycle' will open archived trees.
  541. An archived tree is a tree marked with the tag ARCHIVE.
  542. When nil, archived trees will stay folded. You can still open them with
  543. normal outline commands like `show-all', but not with the cycling commands."
  544. :group 'org-archive
  545. :group 'org-cycle
  546. :type 'boolean)
  547. (defcustom org-sparse-tree-open-archived-trees nil
  548. "Non-nil means sparse tree construction shows matches in archived trees.
  549. When nil, matches in these trees are highlighted, but the trees are kept in
  550. collapsed state."
  551. :group 'org-archive
  552. :group 'org-sparse-trees
  553. :type 'boolean)
  554. (defcustom org-archive-location "%s_archive::"
  555. "The location where subtrees should be archived.
  556. This string consists of two parts, separated by a double-colon.
  557. The first part is a file name - when omitted, archiving happens in the same
  558. file. %s will be replaced by the current file name (without directory part).
  559. Archiving to a different file is useful to keep archived entries from
  560. contributing to the Org-mode Agenda.
  561. The part after the double colon is a headline. The archived entries will be
  562. filed under that headline. When omitted, the subtrees are simply filed away
  563. at the end of the file, as top-level entries.
  564. Here are a few examples:
  565. \"%s_archive::\"
  566. If the current file is Projects.org, archive in file
  567. Projects.org_archive, as top-level trees. This is the default.
  568. \"::* Archived Tasks\"
  569. Archive in the current file, under the top-level headline
  570. \"* Archived Tasks\".
  571. \"~/org/archive.org::\"
  572. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  573. \"basement::** Finished Tasks\"
  574. Archive in file ./basement (relative path), as level 3 trees
  575. below the level 2 heading \"** Finished Tasks\".
  576. You may set this option on a per-file basis by adding to the buffer a
  577. line like
  578. #+ARCHIVE: basement::** Finished Tasks"
  579. :group 'org-archive
  580. :type 'string)
  581. (defcustom org-archive-mark-done t
  582. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  583. :group 'org-archive
  584. :type 'boolean)
  585. (defcustom org-archive-stamp-time t
  586. "Non-nil means, add a time stamp to entries moved to an archive file.
  587. The time stamp will be added directly after the TODO state keyword in the
  588. first line, so it is probably best to use this in combinations with
  589. `org-archive-mark-done'."
  590. :group 'org-archive
  591. :type 'boolean)
  592. (defgroup org-table nil
  593. "Options concerning tables in Org-mode."
  594. :tag "Org Table"
  595. :group 'org)
  596. (defcustom org-enable-table-editor 'optimized
  597. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  598. When nil, such lines will be treated like ordinary lines.
  599. When equal to the symbol `optimized', the table editor will be optimized to
  600. do the following:
  601. - Automatic overwrite mode in front of whitespace in table fields.
  602. This makes the structure of the table stay in tact as long as the edited
  603. field does not exceed the column width.
  604. - Minimize the number of realigns. Normally, the table is aligned each time
  605. TAB or RET are pressed to move to another field. With optimization this
  606. happens only if changes to a field might have changed the column width.
  607. Optimization requires replacing the functions `self-insert-command',
  608. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  609. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  610. very good at guessing when a re-align will be necessary, but you can always
  611. force one with \\[org-ctrl-c-ctrl-c].
  612. If you would like to use the optimized version in Org-mode, but the
  613. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  614. This variable can be used to turn on and off the table editor during a session,
  615. but in order to toggle optimization, a restart is required.
  616. See also the variable `org-table-auto-blank-field'."
  617. :group 'org-table
  618. :type '(choice
  619. (const :tag "off" nil)
  620. (const :tag "on" t)
  621. (const :tag "on, optimized" optimized)))
  622. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  623. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  624. In the optimized version, the table editor takes over all simple keys that
  625. normally just insert a character. In tables, the characters are inserted
  626. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  627. for empty fields). Outside tables, the correct binding of the keys is
  628. restored.
  629. The default for this option is t if the optimized version is also used in
  630. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  631. this variable requires a restart of Emacs to become effective."
  632. :group 'org-table
  633. :type 'boolean)
  634. (defgroup org-table-settings nil
  635. "Settings for tables in Org-mode."
  636. :tag "Org Table Settings"
  637. :group 'org-table)
  638. (defcustom org-table-default-size "5x2"
  639. "The default size for newly created tables, Columns x Rows."
  640. :group 'org-table-settings
  641. :type 'string)
  642. (defcustom org-table-number-regexp
  643. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)?[0-9a-fA-F]+\\)$"
  644. "Regular expression for recognizing numbers in table columns.
  645. If a table column contains mostly numbers, it will be aligned to the
  646. right. If not, it will be aligned to the left.
  647. The default value of this option is a regular expression which allows
  648. anything which looks remotely like a number as used in scientific
  649. context. For example, all of the following will be considered a
  650. number:
  651. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  652. Other options offered by the customize interface are more restrictive."
  653. :group 'org-table-settings
  654. :type '(choice
  655. (const :tag "Positive Integers"
  656. "^[0-9]+$")
  657. (const :tag "Integers"
  658. "^[-+]?[0-9]+$")
  659. (const :tag "Floating Point Numbers"
  660. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  661. (const :tag "Floating Point Number or Integer"
  662. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  663. (const :tag "Exponential, Floating point, Integer"
  664. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  665. (const :tag "Very General Number-Like, including hex"
  666. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)?[0-9a-fA-F]+\\)$")
  667. (string :tag "Regexp:")))
  668. (defcustom org-table-number-fraction 0.5
  669. "Fraction of numbers in a column required to make the column align right.
  670. In a column all non-white fields are considered. If at least this
  671. fraction of fields is matched by `org-table-number-fraction',
  672. alignment to the right border applies."
  673. :group 'org-table-settings
  674. :type 'number)
  675. (defgroup org-table-editing nil
  676. "Bahavior of tables during editing in Org-mode."
  677. :tag "Org Table Editing"
  678. :group 'org-table)
  679. (defcustom org-table-automatic-realign t
  680. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  681. When nil, aligning is only done with \\[org-table-align], or after column
  682. removal/insertion."
  683. :group 'org-table-editing
  684. :type 'boolean)
  685. (defcustom org-table-limit-column-width t ;kw
  686. "Non-nil means, allow to limit the width of table columns with <N> fields."
  687. :group 'org-table-editing
  688. :type 'boolean)
  689. (defcustom org-table-auto-blank-field t
  690. "Non-nil means, automatically blank table field when starting to type into it.
  691. This only happens when typing immediately after a field motion
  692. command (TAB, S-TAB or RET).
  693. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  694. :group 'org-table-editing
  695. :type 'boolean)
  696. (defcustom org-table-tab-jumps-over-hlines t
  697. "Non-nil means, tab in the last column of a table with jump over a hline.
  698. If a horizontal separator line is following the current line,
  699. `org-table-next-field' can either create a new row before that line, or jump
  700. over the line. When this option is nil, a new line will be created before
  701. this line."
  702. :group 'org-table-editing
  703. :type 'boolean)
  704. (defcustom org-table-tab-recognizes-table.el t
  705. "Non-nil means, TAB will automatically notice a table.el table.
  706. When it sees such a table, it moves point into it and - if necessary -
  707. calls `table-recognize-table'."
  708. :group 'org-table-editing
  709. :type 'boolean)
  710. (defgroup org-table-calculation nil
  711. "Options concerning tables in Org-mode."
  712. :tag "Org Table Calculation"
  713. :group 'org-table)
  714. (defcustom org-table-copy-increment t
  715. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  716. :group 'org-table-calculation
  717. :type 'boolean)
  718. (defcustom org-calc-default-modes
  719. '(calc-internal-prec 12
  720. calc-float-format (float 5)
  721. calc-angle-mode deg
  722. calc-prefer-frac nil
  723. calc-symbolic-mode nil
  724. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  725. calc-display-working-message t
  726. )
  727. "List with Calc mode settings for use in calc-eval for table formulas.
  728. The list must contain alternating symbols (Calc modes variables and values).
  729. Don't remove any of the default settings, just change the values. Org-mode
  730. relies on the variables to be present in the list."
  731. :group 'org-table-calculation
  732. :type 'plist)
  733. (defcustom org-table-formula-evaluate-inline t
  734. "Non-nil means, TAB and RET evaluate a formula in current table field.
  735. If the current field starts with an equal sign, it is assumed to be a formula
  736. which should be evaluated as described in the manual and in the documentation
  737. string of the command `org-table-eval-formula'. This feature requires the
  738. Emacs calc package.
  739. When this variable is nil, formula calculation is only available through
  740. the command \\[org-table-eval-formula]."
  741. :group 'org-table-calculation
  742. :type 'boolean)
  743. (defcustom org-table-formula-use-constants t
  744. "Non-nil means, interpret constants in formulas in tables.
  745. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  746. by the value given in `org-table-formula-constants', or by a value obtained
  747. from the `constants.el' package."
  748. :group 'org-table-calculation
  749. :type 'boolean)
  750. (defcustom org-table-formula-constants nil
  751. "Alist with constant names and values, for use in table formulas.
  752. The car of each element is a name of a constant, without the `$' before it.
  753. The cdr is the value as a string. For example, if you'd like to use the
  754. speed of light in a formula, you would configure
  755. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  756. and then use it in an equation like `$1*$c'."
  757. :group 'org-table-calculation
  758. :type '(repeat
  759. (cons (string :tag "name")
  760. (string :tag "value"))))
  761. (defcustom org-table-formula-numbers-only nil
  762. "Non-nil means, calculate only with numbers in table formulas.
  763. Then all input fields will be converted to a number, and the result
  764. must also be a number. When nil, calc's full potential is available
  765. in table calculations, including symbolics etc."
  766. :group 'org-table-calculation
  767. :type 'boolean)
  768. (defcustom org-table-allow-automatic-line-recalculation t
  769. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  770. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  771. :group 'org-table-calculation
  772. :type 'boolean)
  773. (defgroup org-link nil
  774. "Options concerning links in Org-mode."
  775. :tag "Org Link"
  776. :group 'org)
  777. (defvar org-link-abbrev-alist-local nil
  778. "buffer-local version of `org-link-abbrev-alist', which see.
  779. The value of this is taken from the #+LINK lines.")
  780. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  781. (defcustom org-link-abbrev-alist nil
  782. "Alist of link abbreviations.
  783. The car of each element is a string, to be replaced at the start of a link.
  784. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  785. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  786. [[linkkey::tag][description]]
  787. If REPLACE is a string, the tag will simply be appended to create the link.
  788. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  789. also be a function that will be called with the tag as the only argument to
  790. create the link. See the manual for examples."
  791. :group 'org-link
  792. :type 'alist)
  793. (defcustom org-descriptive-links t
  794. "Non-nil means, hide link part and only show description of bracket links.
  795. Bracket links are like [[link][descritpion]]. This variable sets the initial
  796. state in new org-mode buffers. The setting can then be toggled on a
  797. per-buffer basis from the Org->Hyperlinks menu."
  798. :group 'org-link
  799. :type 'boolean)
  800. (defcustom org-link-style 'bracket
  801. "The style of links to be inserted with \\[org-insert-link].
  802. Possible values are:
  803. bracket [[link][description]]. This is recommended
  804. plain Description \\n link. The old way, no longer recommended."
  805. :group 'org-link
  806. :type '(choice
  807. (const :tag "Bracket (recommended)" bracket)
  808. (const :tag "Plain (no longer recommended)" plain)))
  809. (defcustom org-link-format "%s"
  810. "Default format for external, URL-like linkes in the buffer.
  811. This is a format string for printf, %s will be replaced by the link text.
  812. The recommended value is just \"%s\", since links will be protected by
  813. enclosing them in double brackets. If you prefer plain links (see variable
  814. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  815. additional URL: prefix, so the format would be \"<URL:%s>\"."
  816. :group 'org-link
  817. :type '(choice
  818. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  819. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  820. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  821. (string :tag "Other" :value "<%s>")))
  822. (defcustom org-link-file-path-type 'adaptive
  823. "How the path name in file links should be stored.
  824. Valid values are:
  825. relative relative to the current directory, i.e. the directory of the file
  826. into which the link is being inserted.
  827. absolute absolute path, if possible with ~ for home directory.
  828. noabbrev absolute path, no abbreviation of home directory.
  829. adaptive Use relative path for files in the current directory and sub-
  830. directories of it. For other files, use an absolute path."
  831. :group 'org-link
  832. :type '(choice
  833. (const relative)
  834. (const absolute)
  835. (const noabbrev)
  836. (const adaptive)))
  837. (defcustom org-activate-links '(bracket angle plain radio tag date)
  838. "Types of links that should be activated in Org-mode files.
  839. This is a list of symbols, each leading to the activation of a certain link
  840. type. In principle, it does not hurt to turn on most link types - there may
  841. be a small gain when turning off unused link types. The types are:
  842. bracket The recommended [[link][description]] or [[link]] links with hiding.
  843. angular Links in angular brackes that may contain whitespace like
  844. <bbdb:Carsten Dominik>.
  845. plain Plain links in normal text, no whitespace, like http://google.com.
  846. radio Text that is matched by a radio target, see manual for details.
  847. tag Tag settings in a headline (link to tag search).
  848. date Time stamps (link to calendar).
  849. camel CamelCase words defining text searches.
  850. Changing this variable requires a restart of Emacs to become effective."
  851. :group 'org-link
  852. :type '(set (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "CamelCase words" camel)))
  859. (defgroup org-link-store nil
  860. "Options concerning storing links in Org-mode"
  861. :tag "Org Store Link"
  862. :group 'org-link)
  863. (defcustom org-context-in-file-links t
  864. "Non-nil means, file links from `org-store-link' contain context.
  865. A search string will be added to the file name with :: as separator and
  866. used to find the context when the link is activated by the command
  867. `org-open-at-point'.
  868. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  869. negates this setting for the duration of the command."
  870. :group 'org-link-store
  871. :type 'boolean)
  872. (defcustom org-file-link-context-use-camel-case nil
  873. "Non-nil means, use CamelCase to store a search context in a file link.
  874. When nil, the search string simply consists of the words of the string.
  875. CamelCase is deprecated, and support for it may be dropped in the future."
  876. :group 'org-link-store
  877. :type 'boolean)
  878. (defcustom org-keep-stored-link-after-insertion nil
  879. "Non-nil means, keep link in list for entire session.
  880. The command `org-store-link' adds a link pointing to the current
  881. location to an internal list. These links accumulate during a session.
  882. The command `org-insert-link' can be used to insert links into any
  883. Org-mode file (offering completion for all stored links). When this
  884. option is nil, every link which has been inserted once using \\[org-insert-link]
  885. will be removed from the list, to make completing the unused links
  886. more efficient."
  887. :group 'org-link-store
  888. :type 'boolean)
  889. (defcustom org-usenet-links-prefer-google nil
  890. "Non-nil means, `org-store-link' will create web links to Google groups.
  891. When nil, Gnus will be used for such links.
  892. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  893. negates this setting for the duration of the command."
  894. :group 'org-link-store
  895. :type 'boolean)
  896. (defgroup org-link-follow nil
  897. "Options concerning following links in Org-mode"
  898. :tag "Org Follow Link"
  899. :group 'org-link)
  900. (defcustom org-tab-follows-link nil
  901. "Non-nil means, on links TAB will follow the link.
  902. Needs to be set before org.el is loaded."
  903. :group 'org-link-follow
  904. :type 'boolean)
  905. (defcustom org-return-follows-link nil
  906. "Non-nil means, on links RET will follow the link.
  907. Needs to be set before org.el is loaded."
  908. :group 'org-link-follow
  909. :type 'boolean)
  910. (defcustom org-mouse-1-follows-link t
  911. "Non-nil means, mouse-1 on a link will follow the link.
  912. A longer mouse click will still set point. Does not wortk on XEmacs.
  913. Needs to be set before org.el is loaded."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-mark-ring-length 4
  917. "Number of different positions to be recorded in the ring
  918. Changing this requires a restart of Emacs to work correctly."
  919. :group 'org-link-follow
  920. :type 'interger)
  921. (defcustom org-link-frame-setup
  922. '((vm . vm-visit-folder-other-frame)
  923. (gnus . gnus-other-frame)
  924. (file . find-file-other-window))
  925. "Setup the frame configuration for following links.
  926. When following a link with Emacs, it may often be useful to display
  927. this link in another window or frame. This variable can be used to
  928. set this up for the different types of links.
  929. For VM, use any of
  930. `vm-visit-folder'
  931. `vm-visit-folder-other-frame'
  932. For Gnus, use any of
  933. `gnus'
  934. `gnus-other-frame'
  935. For FILE, use any of
  936. `find-file'
  937. `find-file-other-window'
  938. `find-file-other-frame'
  939. For the calendar, use the variable `calendar-setup'.
  940. For BBDB, it is currently only possible to display the matches in
  941. another window."
  942. :group 'org-link-follow
  943. :type '(list
  944. (cons (const vm)
  945. (choice
  946. (const vm-visit-folder)
  947. (const vm-visit-folder-other-window)
  948. (const vm-visit-folder-other-frame)))
  949. (cons (const gnus)
  950. (choice
  951. (const gnus)
  952. (const gnus-other-frame)))
  953. (cons (const file)
  954. (choice
  955. (const find-file)
  956. (const find-file-other-window)
  957. (const find-file-other-frame)))))
  958. (defcustom org-display-internal-link-with-indirect-buffer nil
  959. "Non-nil means, use indirect buffer to display infile links.
  960. Activating internal links (from one location in a file to another location
  961. in the same file) normally just jumps to the location. When the link is
  962. activated with a C-u prefix (or with mouse-3), the link is displayed in
  963. another window. When this option is set, the other window actually displays
  964. an indirect buffer clone of the current buffer, to avoid any visibility
  965. changes to the current buffer."
  966. :group 'org-link-follow
  967. :type 'boolean)
  968. (defcustom org-open-non-existing-files nil
  969. "Non-nil means, `org-open-file' will open non-existing file.
  970. When nil, an error will be generated."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  974. "Function and arguments to call for following mailto links.
  975. This is a list with the first element being a lisp function, and the
  976. remaining elements being arguments to the function. In string arguments,
  977. %a will be replaced by the address, and %s will be replaced by the subject
  978. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  979. :group 'org-link-follow
  980. :type '(choice
  981. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  982. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  983. (const :tag "message-mail" (message-mail "%a" "%s"))
  984. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  985. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  986. "Non-nil means, ask for confirmation before executing shell links.
  987. Shell links can be dangerous, just thing about a link
  988. [[shell:rm -rf ~/*][Google Search]]
  989. This link would show up in your Org-mode document as \"Google Search\"
  990. but really it would remove your entire home directory.
  991. Therefore I *definitely* advise against setting this variable to nil.
  992. Just change it to `y-or-n-p' of you want to confirm with a single key press
  993. rather than having to type \"yes\"."
  994. :group 'org-link-follow
  995. :type '(choice
  996. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  997. (const :tag "with y-or-n (faster)" y-or-n-p)
  998. (const :tag "no confirmation (dangerous)" nil)))
  999. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1000. "Non-nil means, ask for confirmation before executing elisp links.
  1001. Elisp links can be dangerous, just think about a link
  1002. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1003. This link would show up in your Org-mode document as \"Google Search\"
  1004. but really it would remove your entire home directory.
  1005. Therefore I *definitely* advise against setting this variable to nil.
  1006. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1007. rather than having to type \"yes\"."
  1008. :group 'org-link-follow
  1009. :type '(choice
  1010. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1011. (const :tag "with y-or-n (faster)" y-or-n-p)
  1012. (const :tag "no confirmation (dangerous)" nil)))
  1013. (defconst org-file-apps-defaults-gnu
  1014. '((remote . emacs)
  1015. (t . mailcap))
  1016. "Default file applications on a UNIX or GNU/Linux system.
  1017. See `org-file-apps'.")
  1018. (defconst org-file-apps-defaults-macosx
  1019. '((remote . emacs)
  1020. (t . "open %s")
  1021. ("ps" . "gv %s")
  1022. ("ps.gz" . "gv %s")
  1023. ("eps" . "gv %s")
  1024. ("eps.gz" . "gv %s")
  1025. ("dvi" . "xdvi %s")
  1026. ("fig" . "xfig %s"))
  1027. "Default file applications on a MacOS X system.
  1028. The system \"open\" is known as a default, but we use X11 applications
  1029. for some files for which the OS does not have a good default.
  1030. See `org-file-apps'.")
  1031. (defconst org-file-apps-defaults-windowsnt
  1032. (list
  1033. '(remote . emacs)
  1034. (cons t
  1035. (list (if (featurep 'xemacs)
  1036. 'mswindows-shell-execute
  1037. 'w32-shell-execute)
  1038. "open" 'file)))
  1039. "Default file applications on a Windows NT system.
  1040. The system \"open\" is used for most files.
  1041. See `org-file-apps'.")
  1042. (defcustom org-file-apps
  1043. '(
  1044. ("txt" . emacs)
  1045. ("tex" . emacs)
  1046. ("ltx" . emacs)
  1047. ("org" . emacs)
  1048. ("el" . emacs)
  1049. ("bib" . emacs)
  1050. )
  1051. "External applications for opening `file:path' items in a document.
  1052. Org-mode uses system defaults for different file types, but
  1053. you can use this variable to set the application for a given file
  1054. extension. The entries in this list are cons cells where the car identifies
  1055. files and the cdr the corresponding command. Possible values for the
  1056. file identifier are
  1057. \"ext\" A string identifying an extension
  1058. `directory' Matches a directory
  1059. `remote' Matches a remote file, accessible through tramp or efs.
  1060. Remote files most likely should be visited through Emacs
  1061. because external applications cannot handle such paths.
  1062. t Default for all remaining files
  1063. Possible values for the command are:
  1064. `emacs' The file will be visited by the current Emacs process.
  1065. `default' Use the default application for this file type.
  1066. string A command to be executed by a shell; %s will be replaced
  1067. by the path to the file.
  1068. sexp A Lisp form which will be evaluated. The file path will
  1069. be available in the Lisp variable `file'.
  1070. For more examples, see the system specific constants
  1071. `org-file-apps-defaults-macosx'
  1072. `org-file-apps-defaults-windowsnt'
  1073. `org-file-apps-defaults-gnu'."
  1074. :group 'org-link-follow
  1075. :type '(repeat
  1076. (cons (choice :value ""
  1077. (string :tag "Extension")
  1078. (const :tag "Default for unrecognized files" t)
  1079. (const :tag "Remote file" remote)
  1080. (const :tag "Links to a directory" directory))
  1081. (choice :value ""
  1082. (const :tag "Visit with Emacs" emacs)
  1083. (const :tag "Use system default" default)
  1084. (string :tag "Command")
  1085. (sexp :tag "Lisp form")))))
  1086. (defcustom org-mhe-search-all-folders nil
  1087. "Non-nil means, that the search for the mh-message will be extended to
  1088. all folders if the message cannot be found in the folder given in the link.
  1089. Searching all folders is very effective with one of the search engines
  1090. supported by MH-E, but will be slow with pick."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defgroup org-remember nil
  1094. "Options concerning interaction with remember.el."
  1095. :tag "Org Remember"
  1096. :group 'org)
  1097. (defcustom org-directory "~/org"
  1098. "Directory with org files.
  1099. This directory will be used as default to prompt for org files.
  1100. Used by the hooks for remember.el."
  1101. :group 'org-remember
  1102. :type 'directory)
  1103. (defcustom org-default-notes-file "~/.notes"
  1104. "Default target for storing notes.
  1105. Used by the hooks for remember.el. This can be a string, or nil to mean
  1106. the value of `remember-data-file'."
  1107. :group 'org-remember
  1108. :type '(choice
  1109. (const :tag "Default from remember-data-file" nil)
  1110. file))
  1111. (defcustom org-remember-templates nil
  1112. "Templates for the creation of remember buffers.
  1113. When nil, just let remember make the buffer.
  1114. When not nil, this is a list of 3-element lists. In each entry, the first
  1115. element is a character, a unique key to select this template.
  1116. The second element is the template. The third element is optional and can
  1117. specify a destination file for remember items created with this template.
  1118. The default file is given by `org-default-notes-file'.
  1119. The template specifies the structure of the remember buffer. It should have
  1120. a first line starting with a star, to act as the org-mode headline.
  1121. Furthermore, the following %-escapes will be replaced with content:
  1122. %t time stamp, date only
  1123. %T time stamp with date and time
  1124. %u inactive time stamp, date only
  1125. %U inactive time stamp with date and time
  1126. %n user name
  1127. %a annotation, normally the link created with org-store-link
  1128. %i initial content, the region when remember is called with C-u.
  1129. If %i is indented, the entire inserted text will be indented as well.
  1130. %? This will be removed, and the cursor placed at this position."
  1131. :group 'org-remember
  1132. :type '(repeat :tag "enabled"
  1133. (list :value (?a "\n" nil)
  1134. (character :tag "Selection Key")
  1135. (string :tag "Template")
  1136. (file :tag "Destination file (optional)"))))
  1137. (defcustom org-reverse-note-order nil
  1138. "Non-nil means, store new notes at the beginning of a file or entry.
  1139. When nil, new notes will be filed to the end of a file or entry."
  1140. :group 'org-remember
  1141. :type '(choice
  1142. (const :tag "Reverse always" t)
  1143. (const :tag "Reverse never" nil)
  1144. (repeat :tag "By file name regexp"
  1145. (cons regexp boolean))))
  1146. (defgroup org-todo nil
  1147. "Options concerning TODO items in Org-mode."
  1148. :tag "Org TODO"
  1149. :group 'org)
  1150. (defcustom org-todo-keywords '("TODO" "DONE")
  1151. "List of TODO entry keywords.
  1152. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1153. considered to mean that the entry is \"done\". All the other mean that
  1154. action is required, and will make the entry show up in todo lists, diaries
  1155. etc.
  1156. The command \\[org-todo] cycles an entry through these states, and an
  1157. additional state where no keyword is present. For details about this
  1158. cycling, see also the variable `org-todo-interpretation'
  1159. Changes become only effective after restarting Emacs."
  1160. :group 'org-todo
  1161. :group 'org-keywords
  1162. :type '(repeat (string :tag "Keyword")))
  1163. (defcustom org-todo-interpretation 'sequence
  1164. "Controls how TODO keywords are interpreted.
  1165. This variable is only relevant if `org-todo-keywords' contains more than two
  1166. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1167. When `sequence', \\[org-todo] will always switch to the next state in the
  1168. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1169. to state when executed several times in direct succession. Otherwise, it
  1170. switches directly to DONE from any state.
  1171. See the manual for more information."
  1172. :group 'org-todo
  1173. :group 'org-keywords
  1174. :type '(choice (const sequence)
  1175. (const type)))
  1176. (defcustom org-after-todo-state-change-hook nil
  1177. "Hook which is run after the state of a TODO item was changed.
  1178. The new state (a string with a TODO keyword, or nil) is available in the
  1179. Lisp variable `state'."
  1180. :group 'org-todo
  1181. :type 'hook)
  1182. (defcustom org-log-done nil
  1183. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1184. When the state of an entry is changed from nothing to TODO, remove a previous
  1185. closing date.
  1186. This can also be a list of symbols indicating under which conditions
  1187. the time stamp recording the action should be annotated with a short note.
  1188. Valid members of this list are
  1189. done Offer to record a note when marking entries done
  1190. clock-out Offer to record a note when clocking out of an item.
  1191. A separate window will then pop up and allow you to type a note.
  1192. After finishing with C-c C-c, the note will be added directly after the
  1193. timestamp, as a plain list item. See also the variable
  1194. `org-log-note-headings'.
  1195. Logging can also be configured on a per-file basis by adding one of
  1196. the following lines anywhere in the buffer:
  1197. #+STARTUP: logging
  1198. #+STARTUP: nologging"
  1199. ;; FIXME: in-buffer words for notes???????
  1200. :group 'org-todo
  1201. :type '(choice
  1202. (const :tag "off" nil)
  1203. (const :tag "on" t)
  1204. (set :tag "on, with notes" :greedy t :value (done)
  1205. (const done) (const clock-out))))
  1206. (defcustom org-log-note-headings '((done . "CLOSING NOTE") (clock-out . ""))
  1207. "Headings for notes added when clocking out or closing TODO items.
  1208. The value is an alist, with the car being a sympol indicating the note
  1209. context, and the cdr is the heading to be used. The heading may also be the
  1210. empty string."
  1211. :group 'org-todo
  1212. :type '(list :greedy t
  1213. (cons (const :tag "Heading when closing an item" done) string)
  1214. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1215. (defgroup org-priorities nil
  1216. "Priorities in Org-mode."
  1217. :tag "Org Priorities"
  1218. :group 'org-todo)
  1219. (defcustom org-default-priority ?B
  1220. "The default priority of TODO items.
  1221. This is the priority an item get if no explicit priority is given."
  1222. :group 'org-priorities
  1223. :type 'character)
  1224. (defcustom org-lowest-priority ?C
  1225. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1226. :group 'org-priorities
  1227. :type 'character)
  1228. (defgroup org-time nil
  1229. "Options concerning time stamps and deadlines in Org-mode."
  1230. :tag "Org Time"
  1231. :group 'org)
  1232. (defcustom org-insert-labeled-timestamps-at-point nil
  1233. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1234. When nil, these labeled time stamps are forces into the second line of an
  1235. entry, just after the headline. When scheduling from the global TODO list,
  1236. the time stamp will always be forced into the second line."
  1237. :group 'org-time
  1238. :type 'boolean)
  1239. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1240. "Formats for `format-time-string' which are used for time stamps.
  1241. It is not recommended to change this constant.")
  1242. (defcustom org-time-stamp-rounding-minutes 0
  1243. "Number of minutes to round time stamps to upon insertion.
  1244. When zero, insert the time unmodified. Useful rounding numbers
  1245. should be factors of 60, so for example 5, 10, 15.
  1246. When this is not zero, you can still force an exact time-stamp by using
  1247. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1248. :group 'org-time
  1249. :type 'integer)
  1250. (defcustom org-display-custom-times nil
  1251. "Non-nil means, overlay custom formats over all time stamps.
  1252. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1253. To turn this on on a per-file basis, insert anywhere in the file:
  1254. #+STARTUP: customtime"
  1255. :group 'org-time
  1256. :set 'set-default
  1257. :type 'sexp)
  1258. (make-variable-buffer-local 'org-display-custom-times)
  1259. (defcustom org-time-stamp-custom-formats
  1260. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1261. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1262. These are overlayed over the default ISO format if the variable
  1263. `org-display-custom-times' is set."
  1264. :group 'org-time
  1265. :type 'sexp)
  1266. (defcustom org-deadline-warning-days 30
  1267. "No. of days before expiration during which a deadline becomes active.
  1268. This variable governs the display in sparse trees and in the agenda."
  1269. :group 'org-time
  1270. :type 'number)
  1271. (defcustom org-popup-calendar-for-date-prompt t
  1272. "Non-nil means, pop up a calendar when prompting for a date.
  1273. In the calendar, the date can be selected with mouse-1. However, the
  1274. minibuffer will also be active, and you can simply enter the date as well.
  1275. When nil, only the minibuffer will be available."
  1276. :group 'org-time
  1277. :type 'boolean)
  1278. (defcustom org-calendar-follow-timestamp-change t
  1279. "Non-nil means, make the calendar window follow timestamp changes.
  1280. When a timestamp is modified and the calendar window is visible, it will be
  1281. moved to the new date."
  1282. :group 'org-time
  1283. :type 'boolean)
  1284. (defgroup org-tags nil
  1285. "Options concerning tags in Org-mode."
  1286. :tag "Org Tags"
  1287. :group 'org)
  1288. (defcustom org-tag-alist nil
  1289. "List of tags allowed in Org-mode files.
  1290. When this list is nil, Org-mode will base TAG input on what is already in the
  1291. buffer.
  1292. The value of this variable is an alist, the car may be (and should) be a
  1293. character that is used to select that tag through the fast-tag-selection
  1294. interface. See the manual for details."
  1295. :group 'org-tags
  1296. :type '(repeat
  1297. (choice
  1298. (cons (string :tag "Tag name")
  1299. (character :tag "Access char"))
  1300. (const :tag "Start radio group" (:startgroup))
  1301. (const :tag "End radio group" (:endgroup)))))
  1302. (defcustom org-use-fast-tag-selection 'auto
  1303. "Non-nil means, use fast tag selection scheme.
  1304. This is a special interface to select and deselect tags with single keys.
  1305. When nil, fast selection is never used.
  1306. When the symbol `auto', fast selection is used if and only if selection
  1307. characters for tags have been configured, either through the variable
  1308. `org-tag-alist' or through a #+TAGS line in the buffer.
  1309. When t, fast selection is always used and selection keys are assigned
  1310. automatically if necessary."
  1311. :group 'org-tags
  1312. :type '(choice
  1313. (const :tag "Always" t)
  1314. (const :tag "Never" nil)
  1315. (const :tag "When selection characters are configured" 'auto)))
  1316. (defcustom org-fast-tag-selection-single-key nil
  1317. "Non-nil means, fast tag selection exits after first change.
  1318. When nil, you have to press RET to exit it.
  1319. During fast tag selection, you can toggle this flag with `C-c'."
  1320. :group 'org-tags
  1321. :type 'boolean)
  1322. (defcustom org-tags-column 48
  1323. "The column to which tags should be indented in a headline.
  1324. If this number is positive, it specifies the column. If it is negative,
  1325. it means that the tags should be flushright to that column. For example,
  1326. -79 works well for a normal 80 character screen."
  1327. :group 'org-tags
  1328. :type 'integer)
  1329. (defcustom org-auto-align-tags t
  1330. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1331. These operations change the length of a headline and therefore shift
  1332. the tags around. With this options turned on, after each such operation
  1333. the tags are again aligned to `org-tags-column'."
  1334. :group 'org-tags
  1335. :type 'boolean)
  1336. (defcustom org-use-tag-inheritance t
  1337. "Non-nil means, tags in levels apply also for sublevels.
  1338. When nil, only the tags directly given in a specific line apply there.
  1339. If you turn off this option, you very likely want to turn on the
  1340. companion option `org-tags-match-list-sublevels'."
  1341. :group 'org-tags
  1342. :type 'boolean)
  1343. (defcustom org-tags-match-list-sublevels nil
  1344. "Non-nil means list also sublevels of headlines matching tag search.
  1345. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1346. the sublevels of a headline matching a tag search often also match
  1347. the same search. Listing all of them can create very long lists.
  1348. Setting this variable to nil causes subtrees of a match to be skipped.
  1349. This option is off by default, because inheritance in on. If you turn
  1350. inheritance off, you very likely want to turn this option on.
  1351. As a special case, if the tag search is restricted to TODO items, the
  1352. value of this variable is ignored and sublevels are always checked, to
  1353. make sure all corresponding TODO items find their way into the list."
  1354. :group 'org-tags
  1355. :type 'boolean)
  1356. (defvar org-tags-history nil
  1357. "History of minibuffer reads for tags.")
  1358. (defvar org-last-tags-completion-table nil
  1359. "The last used completion table for tags.")
  1360. (defgroup org-agenda nil
  1361. "Options concerning agenda display Org-mode."
  1362. :tag "Org Agenda"
  1363. :group 'org)
  1364. (defvar org-category nil
  1365. "Variable used by org files to set a category for agenda display.
  1366. Such files should use a file variable to set it, for example
  1367. -*- mode: org; org-category: \"ELisp\"
  1368. or contain a special line
  1369. #+CATEGORY: ELisp
  1370. If the file does not specify a category, then file's base name
  1371. is used instead.")
  1372. (make-variable-buffer-local 'org-category)
  1373. (defcustom org-agenda-files nil
  1374. "The files to be used for agenda display.
  1375. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1376. \\[org-remove-file]. You can also use customize to edit the list.
  1377. If the value of the variable is not a list but a single file name, then
  1378. the list of agenda files is actually stored and maintained in that file, one
  1379. agenda file per line."
  1380. :group 'org-agenda
  1381. :type '(choice
  1382. (repeat :tag "List of files" file)
  1383. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1384. (defcustom org-agenda-custom-commands ;'(("w" todo "WAITING"))
  1385. '(("w" todo "WAITING" ((aaa 1) (bbb 2))))
  1386. "Custom commands for the agenda.
  1387. These commands will be offered on the splash screen displayed by the
  1388. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1389. (key type match options)
  1390. key The key (a single char as a string) to be associated with the command.
  1391. type The command type, any of the following symbols:
  1392. todo Entries with a specific TODO keyword, in all agenda files.
  1393. tags Tags match in all agenda files.
  1394. tags-todo Tags match in all agenda files, TODO entries only.
  1395. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1396. tags-tree Sparse tree with all tags matches in *current* file.
  1397. occur-tree Occur sparse tree for *current* file.
  1398. match What to search for:
  1399. - a single keyword for TODO keyword searches
  1400. - a tags match expression for tags searches
  1401. - a regular expression for occur searches
  1402. options A list of option setttings, similar to that in a let form, so like
  1403. this: ((opt1 val1) (opt2 val2) ...)
  1404. You can also define a set of commands, to create a composite agenda buffer.
  1405. In this case, an entry looks like this:
  1406. (key desc (cmd1 cmd2 ...) general-options)
  1407. where
  1408. desc A description string to be displayed in the dispatcher menu.
  1409. cmd An agenda command, similar to the above. However, tree commands
  1410. are no allowed, but instead you can get agenda and global todo list.
  1411. So valid commands for a set are:
  1412. (agenda)
  1413. (alltodo)
  1414. (todo \"match\" options)
  1415. (tags \"match\" options )
  1416. (tags-todo \"match\" options)
  1417. Each command can carry a list of options, and another set of options can be
  1418. given for the whole set of commands. Individual command options take
  1419. precedence over the general options."
  1420. :group 'org-agenda
  1421. :type '(repeat
  1422. (choice
  1423. (list :tag "Single command"
  1424. (string :tag "Key")
  1425. (choice
  1426. (const :tag "Tags search (all agenda files)" tags)
  1427. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1428. (const :tag "TODO keyword search (all agenda files)" todo)
  1429. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1430. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1431. (const :tag "Occur tree (current buffer)" occur-tree))
  1432. (string :tag "Match")
  1433. (repeat :tag "Local options"
  1434. (list (variable :tag "Option") (sexp :tag "Value"))))
  1435. (list :tag "Command series, all agenda files"
  1436. (string :tag "Key")
  1437. (string :tag "Description")
  1438. (repeat
  1439. (choice
  1440. (const :tag "Agenda" (agenda))
  1441. (const :tag "TODO list" (alltodo))
  1442. (list :tag "Tags search"
  1443. (const :format "" tags)
  1444. (string :tag "Match")
  1445. (repeat :tag "Local options"
  1446. (list (variable :tag "Option")
  1447. (sexp :tag "Value"))))
  1448. (list :tag "Tags search, TODO entries only"
  1449. (const :format "" tags-todo)
  1450. (string :tag "Match")
  1451. (repeat :tag "Local options"
  1452. (list (variable :tag "Option")
  1453. (sexp :tag "Value"))))
  1454. (list :tag "TODO keyword search"
  1455. (const :format "" todo)
  1456. (string :tag "Match")
  1457. (repeat :tag "Local options"
  1458. (list (variable :tag "Option")
  1459. (sexp :tag "Value"))))))
  1460. (repeat :tag "General options"
  1461. (list (variable :tag "Option")
  1462. (sexp :tag "Value")))))))
  1463. (defcustom org-agenda-todo-list-sublevels t
  1464. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1465. When nil, the sublevels of a TODO entry are not checked, resulting in
  1466. potentially much shorter TODO lists."
  1467. :group 'org-agenda
  1468. :group 'org-todo
  1469. :type 'boolean)
  1470. (defcustom org-agenda-todo-ignore-scheduled nil
  1471. "Non-nil means, don't show scheduled entries in the global todo list.
  1472. The idea behind this is that by scheduling it, you have already taken care
  1473. of this item."
  1474. :group 'org-agenda
  1475. :group 'org-todo
  1476. :type 'boolean)
  1477. (defcustom org-agenda-todo-ignore-deadlines nil
  1478. "Non-nil means, don't show near deadline entries in the global todo list.
  1479. Near means closer than `org-deadline-warning-days' days.
  1480. The idea behind this is that such items will appear in the agenda anyway."
  1481. :group 'org-agenda
  1482. :group 'org-todo
  1483. :type 'boolean)
  1484. (defcustom org-timeline-show-empty-dates 3
  1485. "Non-nil means, `org-timeline' also shows dates without an entry.
  1486. When nil, only the days which actually have entries are shown.
  1487. When t, all days between the first and the last date are shown.
  1488. When an integer, show also empty dates, but if there is a gap of more than
  1489. N days, just insert a special line indicating the size of the gap."
  1490. :group 'org-agenda
  1491. :type '(choice
  1492. (const :tag "None" nil)
  1493. (const :tag "All" t)
  1494. (number :tag "at most")))
  1495. (defcustom org-agenda-include-all-todo nil
  1496. "Set means weekly/daily agenda will always contain all TODO entries.
  1497. The TODO entries will be listed at the top of the agenda, before
  1498. the entries for specific days."
  1499. :group 'org-agenda
  1500. :type 'boolean)
  1501. (defcustom org-agenda-include-diary nil
  1502. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1503. :group 'org-agenda
  1504. :type 'boolean)
  1505. (defcustom org-calendar-to-agenda-key [?c]
  1506. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1507. The command `org-calendar-goto-agenda' will be bound to this key. The
  1508. default is the character `c' because then `c' can be used to switch back and
  1509. forth between agenda and calendar."
  1510. :group 'org-agenda
  1511. :type 'sexp)
  1512. (defgroup org-agenda-setup nil
  1513. "Options concerning setting up the Agenda window in Org Mode."
  1514. :tag "Org Agenda Window Setup"
  1515. :group 'org-agenda)
  1516. (defcustom org-agenda-window-setup 'reorganize-frame
  1517. "How the agenda buffer should be displayed.
  1518. Possible values for this option are:
  1519. current-window Show agenda in the current window, keeping all other windows.
  1520. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1521. other-window Use `switch-to-buffer-other-window' to display agenda.
  1522. reorganize-frame Show only two windows on the current frame, the current
  1523. window and the agenda. Also, if the option
  1524. `org-fit-agenda-window' is set, resize the agenda window to
  1525. try to show as much as possible of the buffer content.
  1526. See also the variable `org-agenda-restore-windows-after-quit'."
  1527. :group 'org-agenda-setup
  1528. :type '(choice
  1529. (const current-window)
  1530. (const other-frame)
  1531. (const other-window)
  1532. (const reorganize-frame)))
  1533. (defcustom org-agenda-restore-windows-after-quit nil
  1534. "Non-nil means, restore window configuration open exiting agenda.
  1535. Before the window configuration is changed for displaying the agenda,
  1536. the current status is recorded. When the agenda is exited with
  1537. `q' or `x' and this option is set, the old state is restored. If
  1538. `org-agenda-window-setup' is `other-frame', the value of this
  1539. option will be ignored.."
  1540. :group 'org-agenda-setup
  1541. :type 'boolean)
  1542. ;; FIXME: I think this variable could be removed.
  1543. (defcustom org-select-agenda-window t
  1544. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1545. When nil, cursor will remain in the current window."
  1546. :group 'org-agenda-setup
  1547. :type 'boolean)
  1548. ;; FIXME: I think this variable could be removed.
  1549. (defcustom org-fit-agenda-window t
  1550. "Non-nil means, change window size of agenda to fit content.
  1551. This is only effective if `org-agenda-window-setup' is `reorganize-frame'."
  1552. :group 'org-agenda-setup
  1553. :type 'boolean)
  1554. (defcustom org-finalize-agenda-hook nil
  1555. "Hook run just before displaying an agenda buffer."
  1556. :group 'org-agenda-setup
  1557. :type 'hook)
  1558. (defcustom org-agenda-mouse-1-follows-link nil
  1559. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1560. A longer mouse click will still set point. Does not wortk on XEmacs.
  1561. Needs to be set before org.el is loaded."
  1562. :group 'org-agenda-setup
  1563. :type 'boolean)
  1564. (defcustom org-agenda-start-with-follow-mode nil
  1565. "The initial value of follwo-mode in a newly created agenda window."
  1566. :group 'org-agenda-setup
  1567. :type 'boolean)
  1568. (defgroup org-agenda-display nil
  1569. "Options concerning what to display initially in Agenda."
  1570. :tag "Org Agenda Display"
  1571. :group 'org-agenda)
  1572. (defcustom org-agenda-show-all-dates t
  1573. "Non-nil means, `org-agenda' shows every day in the selected range.
  1574. When nil, only the days which actually have entries are shown."
  1575. :group 'org-agenda-display
  1576. :type 'boolean)
  1577. (defcustom org-agenda-start-on-weekday 1
  1578. "Non-nil means, start the overview always on the specified weekday.
  1579. 0 denotes Sunday, 1 denotes Monday etc.
  1580. When nil, always start on the current day."
  1581. :group 'org-agenda-display
  1582. :type '(choice (const :tag "Today" nil)
  1583. (number :tag "Weekday No.")))
  1584. (defcustom org-agenda-ndays 7
  1585. "Number of days to include in overview display.
  1586. Should be 1 or 7."
  1587. :group 'org-agenda-display
  1588. :type 'number)
  1589. (defcustom org-agenda-use-time-grid t
  1590. "Non-nil means, show a time grid in the agenda schedule.
  1591. A time grid is a set of lines for specific times (like every two hours between
  1592. 8:00 and 20:00). The items scheduled for a day at specific times are
  1593. sorted in between these lines.
  1594. For details about when the grid will be shown, and what it will look like, see
  1595. the variable `org-agenda-time-grid'."
  1596. :group 'org-agenda-display
  1597. :type 'boolean)
  1598. (defcustom org-agenda-time-grid
  1599. '((daily today require-timed)
  1600. "----------------"
  1601. (800 1000 1200 1400 1600 1800 2000))
  1602. "The settings for time grid for agenda display.
  1603. This is a list of three items. The first item is again a list. It contains
  1604. symbols specifying conditions when the grid should be displayed:
  1605. daily if the agenda shows a single day
  1606. weekly if the agenda shows an entire week
  1607. today show grid on current date, independent of daily/weekly display
  1608. require-timed show grid only if at least one item has a time specification
  1609. The second item is a string which will be places behing the grid time.
  1610. The third item is a list of integers, indicating the times that should have
  1611. a grid line."
  1612. :group 'org-agenda-display
  1613. :type
  1614. '(list
  1615. (set :greedy t :tag "Grid Display Options"
  1616. (const :tag "Show grid in single day agenda display" daily)
  1617. (const :tag "Show grid in weekly agenda display" weekly)
  1618. (const :tag "Always show grid for today" today)
  1619. (const :tag "Show grid only if any timed entries are present"
  1620. require-timed)
  1621. (const :tag "Skip grid times already present in an entry"
  1622. remove-match))
  1623. (string :tag "Grid String")
  1624. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1625. (let ((sorting-choice
  1626. '(choice
  1627. (const time-up) (const time-down)
  1628. (const category-keep) (const category-up) (const category-down)
  1629. (const tag-down) (const tag-up)
  1630. (const priority-up) (const priority-down))))
  1631. (defcustom org-agenda-sorting-strategy
  1632. '((agenda time-up category-keep priority-down)
  1633. (todo category-keep priority-down)
  1634. (tags category-keep))
  1635. "Sorting structure for the agenda items of a single day.
  1636. This is a list of symbols which will be used in sequence to determine
  1637. if an entry should be listed before another entry. The following
  1638. symbols are recognized:
  1639. time-up Put entries with time-of-day indications first, early first
  1640. time-down Put entries with time-of-day indications first, late first
  1641. category-keep Keep the default order of categories, corresponding to the
  1642. sequence in `org-agenda-files'.
  1643. category-up Sort alphabetically by category, A-Z.
  1644. category-down Sort alphabetically by category, Z-A.
  1645. tag-up Sort alphabetically by last tag, A-Z.
  1646. tag-down Sort alphabetically by last tag, Z-A.
  1647. priority-up Sort numerically by priority, high priority last.
  1648. priority-down Sort numerically by priority, high priority first.
  1649. The different possibilities will be tried in sequence, and testing stops
  1650. if one comparison returns a \"not-equal\". For example, the default
  1651. '(time-up category-keep priority-down)
  1652. means: Pull out all entries having a specified time of day and sort them,
  1653. in order to make a time schedule for the current day the first thing in the
  1654. agenda listing for the day. Of the entries without a time indication, keep
  1655. the grouped in categories, don't sort the categories, but keep them in
  1656. the sequence given in `org-agenda-files'. Within each category sort by
  1657. priority.
  1658. Leaving out `category-keep' would mean that items will be sorted across
  1659. categories by priority."
  1660. :group 'org-agenda-display
  1661. :type `(choice
  1662. (repeat :tag "General" ,sorting-choice)
  1663. (list :tag "Individually"
  1664. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1665. (repeat ,sorting-choice))
  1666. (cons (const :tag "Strategy for TODO lists" todo)
  1667. (repeat ,sorting-choice))
  1668. (cons (const :tag "Strategy for Tags matches" tags)
  1669. (repeat ,sorting-choice))))))
  1670. (defcustom org-sort-agenda-notime-is-late t
  1671. "Non-nil means, items without time are considered late.
  1672. This is only relevant for sorting. When t, items which have no explicit
  1673. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1674. do have a time. When nil, the default time is before 0:00. You can use this
  1675. option to decide if the schedule for today should come before or after timeless
  1676. agenda entries."
  1677. :group 'org-agenda-display
  1678. :type 'boolean)
  1679. (defgroup org-agenda-prefix nil
  1680. "Options concerning the entry prefix in the Org-mode agenda display."
  1681. :tag "Org Agenda Prefix"
  1682. :group 'org-agenda)
  1683. (defcustom org-agenda-prefix-format
  1684. '((agenda . " %-12:c%?-12t% s")
  1685. (timeline . " % s")
  1686. (todo . " %-12:c")
  1687. (tags . " %-12:c"))
  1688. "Format specifications for the prefix of items in the agenda views.
  1689. An alist with four entries, for the different agenda types. The keys to the
  1690. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1691. are format strings.
  1692. This format works similar to a printf format, with the following meaning:
  1693. %c the category of the item, \"Diary\" for entries from the diary, or
  1694. as given by the CATEGORY keyword or derived from the file name.
  1695. %T the *last* tag of the item. Last because inherited tags come
  1696. first in the list.
  1697. %t the time-of-day specification if one applies to the entry, in the
  1698. format HH:MM
  1699. %s Scheduling/Deadline information, a short string
  1700. All specifiers work basically like the standard `%s' of printf, but may
  1701. contain two additional characters: A question mark just after the `%' and
  1702. a whitespace/punctuation character just before the final letter.
  1703. If the first character after `%' is a question mark, the entire field
  1704. will only be included if the corresponding value applies to the
  1705. current entry. This is useful for fields which should have fixed
  1706. width when present, but zero width when absent. For example,
  1707. \"%?-12t\" will result in a 12 character time field if a time of the
  1708. day is specified, but will completely disappear in entries which do
  1709. not contain a time.
  1710. If there is punctuation or whitespace character just before the final
  1711. format letter, this character will be appended to the field value if
  1712. the value is not empty. For example, the format \"%-12:c\" leads to
  1713. \"Diary: \" if the category is \"Diary\". If the category were be
  1714. empty, no additional colon would be interted.
  1715. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1716. - Indent the line with two space characters
  1717. - Give the category in a 12 chars wide field, padded with whitespace on
  1718. the right (because of `-'). Append a colon if there is a category
  1719. (because of `:').
  1720. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1721. time, don't put in an empty field, just skip it (because of '?').
  1722. - Finally, put the scheduling information and append a whitespace.
  1723. As another example, if you don't want the time-of-day of entries in
  1724. the prefix, you could use:
  1725. (setq org-agenda-prefix-format \" %-11:c% s\")
  1726. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1727. `org-agenda-remove-tags-when-in-prefix'."
  1728. :type '(choice
  1729. (string :tag "General format")
  1730. (list :greedy t :tag "View dependent"
  1731. (cons (const agenda) (string :tag "Format"))
  1732. (cons (const timeline) (string :tag "Format"))
  1733. (cons (const todo) (string :tag "Format"))
  1734. (cons (const tags) (string :tag "Format"))))
  1735. :group 'org-agenda-prefix)
  1736. (defvar org-prefix-format-compiled nil
  1737. "The compiled version of the most recently used prefix format.
  1738. See the variable `org-agenda-prefix-format'.")
  1739. (defcustom org-agenda-remove-times-when-in-prefix t
  1740. "Non-nil means, remove duplicate time specifications in agenda items.
  1741. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1742. time-of-day specification in a headline or diary entry is extracted and
  1743. placed into the prefix. If this option is non-nil, the original specification
  1744. \(a timestamp or -range, or just a plain time(range) specification like
  1745. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1746. cluttered.
  1747. The option can be t or nil. It may also be the symbol `beg', indicating
  1748. that the time should only be removed what it is located at the beginning of
  1749. the headline/diary entry."
  1750. :group 'org-agenda-prefix
  1751. :type '(choice
  1752. (const :tag "Always" t)
  1753. (const :tag "Never" nil)
  1754. (const :tag "When at beginning of entry" beg)))
  1755. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1756. "Non-nil means, remove the tags from the headline copy in the agenda.
  1757. When this is the symbol `prefix', only remove tags when
  1758. `org-agenda-prefix-format' contains a `%T' specifier."
  1759. :group 'org-agenda-prefix
  1760. :type '(choice
  1761. (const :tag "Always" t)
  1762. (const :tag "Never" nil)
  1763. (const :tag "When prefix format contains %T" prefix)))
  1764. (defcustom org-agenda-align-tags-to-column 65
  1765. "Shift tags in agenda items to this column."
  1766. :group 'org-agenda-prefix
  1767. :type 'integer)
  1768. (defgroup org-latex nil
  1769. "Options for embedding LaTeX code into Org-mode"
  1770. :tag "Org LaTeX"
  1771. :group 'org)
  1772. (defcustom org-format-latex-options
  1773. '(:foreground "Black" :background "Transparent" :scale 1.0
  1774. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1775. "Options for creating images from LaTeX fragments.
  1776. This is a property list with the following properties:
  1777. :foreground the foreground color, for example \"Black\".
  1778. :background the background color, or \"Transparent\".
  1779. :scale a scaling factor for the size of the images
  1780. :matchers a list indicating which matchers should be used to
  1781. find LaTeX fragments. Valid members of this list are:
  1782. \"begin\" find environments
  1783. \"$\" find math expressions surrounded by $...$
  1784. \"$$\" find math expressions surrounded by $$....$$
  1785. \"\\(\" find math expressions surrounded by \\(...\\)
  1786. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1787. :group 'org-latex
  1788. :type 'plist)
  1789. (defgroup org-export nil
  1790. "Options for exporting org-listings."
  1791. :tag "Org Export"
  1792. :group 'org)
  1793. (defgroup org-export-general nil
  1794. "General options for exporting Org-mode files."
  1795. :tag "Org Export General"
  1796. :group 'org-export)
  1797. (defcustom org-export-publishing-directory "."
  1798. "Path to the location where exported files should be located.
  1799. This path may be relative to the directory where the Org-mode file lives.
  1800. The default is to put them into the same directory as the Org-mode file.
  1801. The variable may also be an alist with export types `:html', `:ascii',
  1802. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1803. is relative, it is interpreted relative to the directory where the exported
  1804. Org-mode files lives."
  1805. :group 'org-export-general
  1806. :type '(choice
  1807. (directory)
  1808. (repeat
  1809. (cons
  1810. (choice :tag "Type"
  1811. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1812. (directory)))))
  1813. (defcustom org-export-language-setup
  1814. '(("en" "Author" "Date" "Table of Contents")
  1815. ("cs" "Autor" "Datum" "Obsah")
  1816. ("da" "Ophavsmand" "Dato" "Indhold")
  1817. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1818. ("es" "Autor" "Fecha" "\xccndice")
  1819. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1820. ("it" "Autore" "Data" "Indice")
  1821. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1822. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1823. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1824. "Terms used in export text, translated to different languages.
  1825. Use the variable `org-export-default-language' to set the language,
  1826. or use the +OPTION lines for a per-file setting."
  1827. :group 'org-export-general
  1828. :type '(repeat
  1829. (list
  1830. (string :tag "HTML language tag")
  1831. (string :tag "Author")
  1832. (string :tag "Date")
  1833. (string :tag "Table of Contents"))))
  1834. (defcustom org-export-default-language "en"
  1835. "The default language of HTML export, as a string.
  1836. This should have an association in `org-export-language-setup'."
  1837. :group 'org-export-general
  1838. :type 'string)
  1839. (defcustom org-export-headline-levels 3
  1840. "The last level which is still exported as a headline.
  1841. Inferior levels will produce itemize lists when exported.
  1842. Note that a numeric prefix argument to an exporter function overrides
  1843. this setting.
  1844. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1845. :group 'org-export-general
  1846. :type 'number)
  1847. (defcustom org-export-with-section-numbers t
  1848. "Non-nil means, add section numbers to headlines when exporting.
  1849. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1850. :group 'org-export-general
  1851. :type 'boolean)
  1852. (defcustom org-export-with-toc t
  1853. "Non-nil means, create a table of contents in exported files.
  1854. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1855. Headlines which contain any TODO items will be marked with \"(*)\" in
  1856. ASCII export, and with red color in HTML output.
  1857. In HTML output, the TOC will be clickable.
  1858. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1859. :group 'org-export-general
  1860. :type 'boolean)
  1861. (defcustom org-export-mark-todo-in-toc nil
  1862. "Non-nil means, mark TOC lines that contain any open TODO items."
  1863. :group 'org-export-general
  1864. :type 'boolean)
  1865. (defcustom org-export-preserve-breaks nil
  1866. "Non-nil means, preserve all line breaks when exporting.
  1867. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1868. export, line breaks will always be preserved, regardless of this variable.
  1869. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1870. :group 'org-export-general
  1871. :type 'boolean)
  1872. (defcustom org-export-with-archived-trees 'headline
  1873. "Whether subtrees with the ARCHIVE tag should be exported.
  1874. This can have three different values
  1875. nil Do not export, pretend this tree is not present
  1876. t Do export the entire tree
  1877. headline Only export the headline, but skip the tree below it."
  1878. :group 'org-export-general
  1879. :group 'org-archive
  1880. :type '(choice
  1881. (const :tag "not at all" nil)
  1882. (const :tag "headline only" 'headline)
  1883. (const :tag "entirely" t)))
  1884. (defcustom org-export-with-timestamps t
  1885. "Nil means, do not export time stamps and associated keywords."
  1886. :group 'org-export-general
  1887. :type 'boolean)
  1888. (defcustom org-export-remove-timestamps-from-toc t
  1889. "Nil means, remove timestamps from the table of contents entries."
  1890. :group 'org-export-general
  1891. :type 'boolean)
  1892. (defcustom org-export-with-tags 'not-in-toc
  1893. "Nil means, do not export tags, just remove them from headlines.
  1894. If this is the sysmbol `not-in-toc', tags will be removed from table of
  1895. contents entries, but still be shown in the headlines of the document."
  1896. :group 'org-export-general
  1897. :type '(choice
  1898. (const :tag "Off" nil)
  1899. (const :tag "Not in TOC" not-in-toc)
  1900. (const :tag "On" t)))
  1901. (defgroup org-export-translation nil
  1902. "Options for translating special ascii sequences for the export backends."
  1903. :tag "Org Export Translation"
  1904. :group 'org-export)
  1905. (defcustom org-export-with-emphasize t
  1906. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1907. If the export target supports emphasizing text, the word will be
  1908. typeset in bold, italic, or underlined, respectively. Works only for
  1909. single words, but you can say: I *really* *mean* *this*.
  1910. Not all export backends support this.
  1911. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1912. :group 'org-export-translation
  1913. :type 'boolean)
  1914. (defcustom org-export-with-sub-superscripts t
  1915. "Non-nil means, interpret \"_\" and \"^\" for export.
  1916. When this option is turned on, you can use TeX-like syntax for sub- and
  1917. superscripts. Several characters after \"_\" or \"^\" will be
  1918. considered as a single item - so grouping with {} is normally not
  1919. needed. For example, the following things will be parsed as single
  1920. sub- or superscripts.
  1921. 10^24 or 10^tau several digits will be considered 1 item.
  1922. 10^-12 or 10^-tau a leading sign with digits or a word
  1923. x^2-y^3 will be read as x^2 - y^3, because items are
  1924. terminated by almost any nonword/nondigit char.
  1925. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1926. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1927. sub/superscript.
  1928. Not all export backends support this, but HTML does.
  1929. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1930. :group 'org-export-translation
  1931. :type 'boolean)
  1932. (defcustom org-export-with-TeX-macros t
  1933. "Non-nil means, interpret simple TeX-like macros when exporting.
  1934. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1935. No only real TeX macros will work here, but the standard HTML entities
  1936. for math can be used as macro names as well. For a list of supported
  1937. names in HTML export, see the constant `org-html-entities'.
  1938. Not all export backends support this.
  1939. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1940. :group 'org-export-translation
  1941. :group 'org-latex
  1942. :type 'boolean)
  1943. (defcustom org-export-with-LaTeX-fragments nil
  1944. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1945. When set, the exporter will find LaTeX environments if the \\begin line is
  1946. the first non-white thing on a line. It will also find the math delimiters
  1947. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1948. display math.
  1949. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1950. :group 'org-export-translation
  1951. :group 'org-latex
  1952. :type 'boolean)
  1953. (defcustom org-export-with-fixed-width t
  1954. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1955. This can be used to have pre-formatted text, fragments of code etc. For
  1956. example:
  1957. : ;; Some Lisp examples
  1958. : (while (defc cnt)
  1959. : (ding))
  1960. will be looking just like this in also HTML. See also the QUOTE keyword.
  1961. Not all export backends support this.
  1962. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1963. :group 'org-export-translation
  1964. :type 'boolean)
  1965. (defcustom org-match-sexp-depth 3
  1966. "Number of stacked braces for sub/superscript matching.
  1967. This has to be set before loading org.el to be effective."
  1968. :group 'org-export-translation
  1969. :type 'integer)
  1970. (defgroup org-export-tables nil
  1971. "Options for exporting tables in Org-mode."
  1972. :tag "Org Export Tables"
  1973. :group 'org-export)
  1974. (defcustom org-export-with-tables t
  1975. "If non-nil, lines starting with \"|\" define a table.
  1976. For example:
  1977. | Name | Address | Birthday |
  1978. |-------------+----------+-----------|
  1979. | Arthur Dent | England | 29.2.2100 |
  1980. Not all export backends support this.
  1981. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1982. :group 'org-export-tables
  1983. :type 'boolean)
  1984. (defcustom org-export-highlight-first-table-line t
  1985. "Non-nil means, highlight the first table line.
  1986. In HTML export, this means use <th> instead of <td>.
  1987. In tables created with table.el, this applies to the first table line.
  1988. In Org-mode tables, all lines before the first horizontal separator
  1989. line will be formatted with <th> tags."
  1990. :group 'org-export-tables
  1991. :type 'boolean)
  1992. (defcustom org-export-table-remove-special-lines t
  1993. "Remove special lines and marking characters in calculating tables.
  1994. This removes the special marking character column from tables that are set
  1995. up for spreadsheet calculations. It also removes the entire lines
  1996. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1997. the values of constants may be useful to have."
  1998. :group 'org-export-tables
  1999. :type 'boolean)
  2000. (defcustom org-export-prefer-native-exporter-for-tables nil
  2001. "Non-nil means, always export tables created with table.el natively.
  2002. Natively means, use the HTML code generator in table.el.
  2003. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2004. the table does not use row- or column-spanning). This has the
  2005. advantage, that the automatic HTML conversions for math symbols and
  2006. sub/superscripts can be applied. Org-mode's HTML generator is also
  2007. much faster."
  2008. :group 'org-export-tables
  2009. :type 'boolean)
  2010. (defgroup org-export-ascii nil
  2011. "Options specific for ASCII export of Org-mode files."
  2012. :tag "Org Export ASCII"
  2013. :group 'org-export)
  2014. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2015. "Characters for underlining headings in ASCII export.
  2016. In the given sequence, these characters will be used for level 1, 2, ..."
  2017. :group 'org-export-ascii
  2018. :type '(repeat character))
  2019. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2020. "Bullet characters for headlines converted to lists in ASCII export.
  2021. The first character is is used for the first lest level generated in this
  2022. way, and so on. If there are more levels than characters given here,
  2023. the list will be repeated.
  2024. Note that plain lists will keep the same bullets as the have in the
  2025. Org-mode file."
  2026. :group 'org-export-ascii
  2027. :type '(repeat character))
  2028. (defcustom org-export-ascii-show-new-buffer t
  2029. "Non-nil means, popup buffer containing the exported ASCII text.
  2030. Otherwise the buffer will just be saved to a file and stay hidden."
  2031. :group 'org-export-ascii
  2032. :type 'boolean)
  2033. (defgroup org-export-xml nil
  2034. "Options specific for XML export of Org-mode files."
  2035. :tag "Org Export XML"
  2036. :group 'org-export)
  2037. (defgroup org-export-html nil
  2038. "Options specific for HTML export of Org-mode files."
  2039. :tag "Org Export HTML"
  2040. :group 'org-export)
  2041. (defcustom org-export-html-style
  2042. "<style type=\"text/css\">
  2043. html {
  2044. font-family: Times, serif;
  2045. font-size: 12pt;
  2046. }
  2047. .title { text-align: center; }
  2048. .todo { color: red; }
  2049. .done { color: green; }
  2050. .timestamp { color: grey }
  2051. .timestamp-kwd { color: CadetBlue }
  2052. .tag { background-color:lightblue; font-weight:normal }
  2053. .target { background-color: lavender; }
  2054. pre {
  2055. border: 1pt solid #AEBDCC;
  2056. background-color: #F3F5F7;
  2057. padding: 5pt;
  2058. font-family: courier, monospace;
  2059. }
  2060. table { border-collapse: collapse; }
  2061. td, th {
  2062. vertical-align: top;
  2063. border: 1pt solid #ADB9CC;
  2064. }
  2065. </style>"
  2066. "The default style specification for exported HTML files.
  2067. Since there are different ways of setting style information, this variable
  2068. needs to contain the full HTML structure to provide a style, including the
  2069. surrounding HTML tags. The style specifications should include definitions
  2070. for new classes todo, done, title, and deadline. For example, legal values
  2071. would be:
  2072. <style type=\"text/css\">
  2073. p { font-weight: normal; color: gray; }
  2074. h1 { color: black; }
  2075. .title { text-align: center; }
  2076. .todo, .deadline { color: red; }
  2077. .done { color: green; }
  2078. </style>
  2079. or, if you want to keep the style in a file,
  2080. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2081. As the value of this option simply gets inserted into the HTML <head> header,
  2082. you can \"misuse\" it to add arbitrary text to the header."
  2083. :group 'org-export-html
  2084. :type 'string)
  2085. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2086. "Format for typesetting the document title in HTML export."
  2087. :group 'org-export-html
  2088. :type 'string)
  2089. (defcustom org-export-html-toplevel-hlevel 2
  2090. "The <H> level for level 1 headings in HTML export."
  2091. :group 'org-export-html
  2092. :type 'string)
  2093. (defcustom org-export-html-link-org-files-as-html t
  2094. "Non-nil means, make file links to `file.org' point to `file.html'.
  2095. When org-mode is exporting an org-mode file to HTML, links to
  2096. non-html files are directly put into a href tag in HTML.
  2097. However, links to other Org-mode files (recognized by the
  2098. extension `.org.) should become links to the corresponding html
  2099. file, assuming that the linked org-mode file will also be
  2100. converted to HTML.
  2101. When nil, the links still point to the plain `.org' file."
  2102. :group 'org-export-html
  2103. :type 'boolean)
  2104. (defcustom org-export-html-inline-images 'maybe
  2105. "Non-nil means, inline images into exported HTML pages.
  2106. This is done using an <img> tag. When nil, an anchor with href is used to
  2107. link to the image. If this option is `maybe', then images in links with
  2108. an empty description will be inlined, while images with a description will
  2109. be linked only."
  2110. :group 'org-export-html
  2111. :type '(choice (const :tag "Never" nil)
  2112. (const :tag "Always" t)
  2113. (const :tag "When there is no description" maybe)))
  2114. (defcustom org-export-html-expand t
  2115. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2116. When nil, these tags will be exported as plain text and therefore
  2117. not be interpreted by a browser.
  2118. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2119. :group 'org-export-html
  2120. :type 'boolean)
  2121. (defcustom org-export-html-table-tag
  2122. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  2123. "The HTML tag used to start a table.
  2124. This must be a <table> tag, but you may change the options like
  2125. borders and spacing."
  2126. :group 'org-export-html
  2127. :type 'string)
  2128. (defcustom org-export-html-with-timestamp nil
  2129. "If non-nil, write `org-export-html-html-helper-timestamp'
  2130. into the exported HTML text. Otherwise, the buffer will just be saved
  2131. to a file."
  2132. :group 'org-export-html
  2133. :type 'boolean)
  2134. (defcustom org-export-html-html-helper-timestamp
  2135. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2136. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2137. :group 'org-export-html
  2138. :type 'string)
  2139. (defcustom org-export-html-show-new-buffer nil
  2140. "Non-nil means, popup buffer containing the exported html text.
  2141. Otherwise, the buffer will just be saved to a file and stay hidden."
  2142. :group 'org-export-html
  2143. :type 'boolean)
  2144. (defgroup org-export-icalendar nil
  2145. "Options specific for iCalendar export of Org-mode files."
  2146. :tag "Org Export iCalendar"
  2147. :group 'org-export)
  2148. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2149. "The file name for the iCalendar file covering all agenda files.
  2150. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2151. The file name should be absolute."
  2152. :group 'org-export-icalendar
  2153. :type 'file)
  2154. (defcustom org-icalendar-include-todo nil
  2155. "Non-nil means, export to iCalendar files should also cover TODO items."
  2156. :group 'org-export-icalendar
  2157. :type 'boolean)
  2158. (defcustom org-icalendar-combined-name "OrgMode"
  2159. "Calendar name for the combined iCalendar representing all agenda files."
  2160. :group 'org-export-icalendar
  2161. :type 'string)
  2162. (defgroup org-font-lock nil
  2163. "Font-lock settings for highlighting in Org-mode."
  2164. :tag "Org Font Lock"
  2165. :group 'org)
  2166. (defcustom org-level-color-stars-only nil
  2167. "Non-nil means fontify only the stars in each headline.
  2168. When nil, the entire headline is fontified.
  2169. Changing it requires restart of `font-lock-mode' to become effective
  2170. also in regions already fontified."
  2171. :group 'org-font-lock
  2172. :type 'boolean)
  2173. (defcustom org-hide-leading-stars nil
  2174. "Non-nil means, hide the first N-1 stars in a headline.
  2175. This works by using the face `org-hide' for these stars. This
  2176. face is white for a light background, and black for a dark
  2177. background. You may have to customize the face `org-hide' to
  2178. make this work.
  2179. Changing it requires restart of `font-lock-mode' to become effective
  2180. also in regions already fontified.
  2181. You may also set this on a per-file basis by adding one of the following
  2182. lines to the buffer:
  2183. #+STARTUP: hidestars
  2184. #+STARTUP: showstars"
  2185. :group 'org-font-lock
  2186. :type 'boolean)
  2187. (defcustom org-fontify-done-headline nil
  2188. "Non-nil means, change the face of a headline if it is marked DONE.
  2189. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2190. When this is non-nil, the headline after the keyword is set to the
  2191. `org-headline-done' as an additional indication."
  2192. :group 'org-font-lock
  2193. :type 'boolean)
  2194. (defcustom org-fontify-emphasized-text t
  2195. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2196. Changing this variable requires a restart of Emacs to take effect."
  2197. :group 'org-font-lock
  2198. :type 'boolean)
  2199. (defvar org-emph-re nil
  2200. "Regular expression for matching emphasis.")
  2201. (defvar org-emphasis-regexp-components) ; defined just below
  2202. (defvar org-emphasis-alist) ; defined just below
  2203. (defun org-set-emph-re (var val)
  2204. "Set variable and compute the emphasis regular expression."
  2205. (set var val)
  2206. (when (and (boundp 'org-emphasis-alist)
  2207. (boundp 'org-emphasis-regexp-components)
  2208. org-emphasis-alist org-emphasis-regexp-components)
  2209. (let* ((e org-emphasis-regexp-components)
  2210. (pre (car e))
  2211. (post (nth 1 e))
  2212. (border (nth 2 e))
  2213. (body (nth 3 e))
  2214. (nl (nth 4 e))
  2215. (stacked (nth 5 e))
  2216. (body1 (concat body "*?"))
  2217. (markers (mapconcat 'car org-emphasis-alist "")))
  2218. ;; make sure special characters appear at the right position in the class
  2219. (if (string-match "\\^" markers)
  2220. (setq markers (concat (replace-match "" t t markers) "^")))
  2221. (if (string-match "-" markers)
  2222. (setq markers (concat (replace-match "" t t markers) "-")))
  2223. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2224. ; (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\\(?:\n?" body "*?\\)?")))
  2225. (if (> nl 0)
  2226. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2227. (int-to-string nl) "\\}")))
  2228. ;; Make the regexp
  2229. (setq org-emph-re
  2230. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2231. "\\("
  2232. "\\([" markers "]\\)"
  2233. "\\("
  2234. "[^" border markers "]"
  2235. body1
  2236. "[^" border markers "]"
  2237. "\\)"
  2238. "\\3\\)"
  2239. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2240. (defcustom org-emphasis-regexp-components
  2241. '(" \t(" " \t.,?;'\")" " \t\r\n," "." 1 nil)
  2242. "Components used to build the reqular expression for emphasis.
  2243. This is a list with 6 entries. Terminology: In an emphasis string
  2244. like \" *strong word* \", we call the initial space PREMATCH, the final
  2245. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2246. and \"trong wor\" is the body. The different components in this variable
  2247. specify what is allowed/forbidden in each part:
  2248. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2249. post Chars allowed as postmatch. End of line will be allowed too.
  2250. border The chars *forbidden* as border characters. In addition to the
  2251. characters given here, all marker characters are forbidden too.
  2252. body-regexp A regexp like \".\" to match a body character. Don't use
  2253. non-shy groups here, and don't allow newline here.
  2254. newline The maximum number of newlines allowed in an emphasis exp.
  2255. stacked Non-nil means, allow stacked styles. This works only in HTML
  2256. export. When this is set, all marker characters (as given in
  2257. `org-emphasis-alist') will be allowed as pre/post, aiding
  2258. inside-out matching.
  2259. Use customize to modify this, or restart Emacs after changing it."
  2260. :group 'org-font-lock
  2261. :set 'org-set-emph-re
  2262. :type '(list
  2263. (sexp :tag "Allowed chars in pre ")
  2264. (sexp :tag "Allowed chars in post ")
  2265. (sexp :tag "Forbidden chars in border ")
  2266. (sexp :tag "Regexp for body ")
  2267. (integer :tag "number of newlines allowed")
  2268. (boolean :tag "Stacking allowed ")))
  2269. (defcustom org-emphasis-alist
  2270. '(("*" bold "<b>" "</b>")
  2271. ("/" italic "<i>" "</i>")
  2272. ("_" underline "<u>" "</u>")
  2273. ("=" shadow "<code>" "</code>")
  2274. ("+" (:strike-through t) "<del>" "</del>")
  2275. )
  2276. "Special syntax for emphasized text.
  2277. Text starting and ending with a special character will be emphasized, for
  2278. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2279. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2280. Emacs buffers, and the HTML tags to be used for this.
  2281. Use customize to modify this, or restart Emacs after changing it."
  2282. :group 'org-font-lock
  2283. :set 'org-set-emph-re
  2284. :type '(repeat
  2285. (list
  2286. (string :tag "Marker character")
  2287. (choice
  2288. (face :tag "Font-lock-face")
  2289. (plist :tag "Face property list"))
  2290. (string :tag "HTML start tag")
  2291. (string :tag "HTML end tag"))))
  2292. (defgroup org-faces nil
  2293. "Faces in Org-mode."
  2294. :tag "Org Faces"
  2295. :group 'org-font-lock)
  2296. (defun org-compatible-face (specs)
  2297. "Make a compatible face specification.
  2298. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2299. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2300. to the top of the list. The `min-colors' attribute will be removed from
  2301. any other entries, and any resulting duplicates will be removed entirely."
  2302. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2303. (let (r e a)
  2304. (while (setq e (pop specs))
  2305. (cond
  2306. ((memq (car e) '(t default)) (push e r))
  2307. ((setq a (member '(min-colors 8) (car e)))
  2308. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2309. (cdr e)))))
  2310. ((setq a (assq 'min-colors (car e)))
  2311. (setq e (cons (delq a (car e)) (cdr e)))
  2312. (or (assoc (car e) r) (push e r)))
  2313. (t (or (assoc (car e) r) (push e r)))))
  2314. (nreverse r))
  2315. specs))
  2316. (defface org-hide
  2317. '((((background light)) (:foreground "white"))
  2318. (((background dark)) (:foreground "black")))
  2319. "Face used to hide leading stars in headlines.
  2320. The forground color of this face should be equal to the background
  2321. color of the frame."
  2322. :group 'org-faces)
  2323. (defface org-level-1 ;; font-lock-function-name-face
  2324. (org-compatible-face
  2325. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2326. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2327. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2328. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2329. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2330. (t (:bold t))))
  2331. "Face used for level 1 headlines."
  2332. :group 'org-faces)
  2333. (defface org-level-2 ;; font-lock-variable-name-face
  2334. (org-compatible-face
  2335. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2336. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2337. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2338. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2339. (t (:bold t))))
  2340. "Face used for level 2 headlines."
  2341. :group 'org-faces)
  2342. (defface org-level-3 ;; font-lock-keyword-face
  2343. (org-compatible-face
  2344. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2345. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2346. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2347. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2348. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2349. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2350. (t (:bold t))))
  2351. "Face used for level 3 headlines."
  2352. :group 'org-faces)
  2353. (defface org-level-4 ;; font-lock-comment-face
  2354. (org-compatible-face
  2355. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2356. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2357. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2358. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2359. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2360. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2361. (t (:bold t))))
  2362. "Face used for level 4 headlines."
  2363. :group 'org-faces)
  2364. (defface org-level-5 ;; font-lock-type-face
  2365. (org-compatible-face
  2366. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2367. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2368. (((class color) (min-colors 8)) (:foreground "green"))))
  2369. "Face used for level 5 headlines."
  2370. :group 'org-faces)
  2371. (defface org-level-6 ;; font-lock-constant-face
  2372. (org-compatible-face
  2373. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2374. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2375. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2376. "Face used for level 6 headlines."
  2377. :group 'org-faces)
  2378. (defface org-level-7 ;; font-lock-builtin-face
  2379. (org-compatible-face
  2380. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2381. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2382. (((class color) (min-colors 8)) (:foreground "blue"))))
  2383. "Face used for level 7 headlines."
  2384. :group 'org-faces)
  2385. (defface org-level-8 ;; font-lock-string-face
  2386. (org-compatible-face
  2387. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2388. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2389. (((class color) (min-colors 8)) (:foreground "green"))))
  2390. "Face used for level 8 headlines."
  2391. :group 'org-faces)
  2392. (defface org-special-keyword ;; font-lock-string-face
  2393. (org-compatible-face
  2394. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2395. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2396. (t (:italic t))))
  2397. "Face used for special keywords."
  2398. :group 'org-faces)
  2399. (defface org-warning ;; font-lock-warning-face
  2400. (org-compatible-face
  2401. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2402. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2403. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2404. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2405. (t (:bold t))))
  2406. "Face for deadlines and TODO keywords."
  2407. :group 'org-faces)
  2408. (defface org-headline-done ;; font-lock-string-face
  2409. (org-compatible-face
  2410. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2411. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2412. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2413. "Face used to indicate that a headline is DONE.
  2414. This face is only used if `org-fontify-done-headline' is set."
  2415. :group 'org-faces)
  2416. (defface org-archived ; similar to shadow
  2417. (org-compatible-face
  2418. '((((class color grayscale) (min-colors 88) (background light))
  2419. (:foreground "grey50"))
  2420. (((class color grayscale) (min-colors 88) (background dark))
  2421. (:foreground "grey70"))
  2422. (((class color) (min-colors 8) (background light))
  2423. (:foreground "green"))
  2424. (((class color) (min-colors 8) (background dark))
  2425. (:foreground "yellow"))))
  2426. "Face for headline with the ARCHIVE tag."
  2427. :group 'org-faces)
  2428. (defface org-link
  2429. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2430. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2431. (t (:underline t)))
  2432. "Face for links."
  2433. :group 'org-faces)
  2434. (defface org-date
  2435. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2436. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2437. (t (:underline t)))
  2438. "Face for links."
  2439. :group 'org-faces)
  2440. (defface org-tag
  2441. '((t (:bold t)))
  2442. "Face for tags."
  2443. :group 'org-faces)
  2444. (defface org-todo ;; font-lock-warning-face
  2445. (org-compatible-face
  2446. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2447. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2448. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2449. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2450. (t (:inverse-video t :bold t))))
  2451. "Face for TODO keywords."
  2452. :group 'org-faces)
  2453. (defface org-done ;; font-lock-type-face
  2454. (org-compatible-face
  2455. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2456. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2457. (((class color) (min-colors 8)) (:foreground "green"))
  2458. (t (:bold t))))
  2459. "Face used for DONE."
  2460. :group 'org-faces)
  2461. (defface org-table ;; font-lock-function-name-face
  2462. (org-compatible-face
  2463. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2464. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2465. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2466. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2467. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2468. (((class color) (min-colors 8) (background dark)))))
  2469. "Face used for tables."
  2470. :group 'org-faces)
  2471. (defface org-formula
  2472. (org-compatible-face
  2473. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2474. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2475. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2476. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2477. (t (:bold t :italic t))))
  2478. "Face for formulas."
  2479. :group 'org-faces)
  2480. (defface org-scheduled-today
  2481. (org-compatible-face
  2482. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2483. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2484. (((class color) (min-colors 8)) (:foreground "green"))
  2485. (t (:bold t :italic t))))
  2486. "Face for items scheduled for a certain day."
  2487. :group 'org-faces)
  2488. (defface org-scheduled-previously
  2489. (org-compatible-face
  2490. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2491. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2492. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2493. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2494. (t (:bold t))))
  2495. "Face for items scheduled previously, and not yet done."
  2496. :group 'org-faces)
  2497. (defface org-upcoming-deadline
  2498. (org-compatible-face
  2499. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2500. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2501. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2502. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2503. (t (:bold t))))
  2504. "Face for items scheduled previously, and not yet done."
  2505. :group 'org-faces)
  2506. (defface org-time-grid ;; font-lock-variable-name-face
  2507. (org-compatible-face
  2508. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2509. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2510. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2511. "Face used for time grids."
  2512. :group 'org-faces)
  2513. (defconst org-level-faces
  2514. '(org-level-1 org-level-2 org-level-3 org-level-4
  2515. org-level-5 org-level-6 org-level-7 org-level-8
  2516. ))
  2517. (defconst org-n-levels (length org-level-faces))
  2518. ;; Variables for pre-computed regular expressions, all buffer local
  2519. (defvar org-done-string nil
  2520. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2521. (make-variable-buffer-local 'org-done-string)
  2522. (defvar org-todo-regexp nil
  2523. "Matches any of the TODO state keywords.")
  2524. (make-variable-buffer-local 'org-todo-regexp)
  2525. (defvar org-not-done-regexp nil
  2526. "Matches any of the TODO state keywords except the last one.")
  2527. (make-variable-buffer-local 'org-not-done-regexp)
  2528. (defvar org-todo-line-regexp nil
  2529. "Matches a headline and puts TODO state into group 2 if present.")
  2530. (make-variable-buffer-local 'org-todo-line-regexp)
  2531. (defvar org-todo-line-tags-regexp nil
  2532. "Matches a headline and puts TODO state into group 2 if present.
  2533. Also put tags into group 4 if tags are present.")
  2534. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2535. (defvar org-nl-done-regexp nil
  2536. "Matches newline followed by a headline with the DONE keyword.")
  2537. (make-variable-buffer-local 'org-nl-done-regexp)
  2538. (defvar org-looking-at-done-regexp nil
  2539. "Matches the DONE keyword a point.")
  2540. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2541. (defvar org-todo-kwd-priority-p nil
  2542. "Do TODO items have priorities?")
  2543. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2544. (defvar org-todo-kwd-max-priority nil
  2545. "Maximum priority of TODO items.")
  2546. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2547. (defvar org-ds-keyword-length 12
  2548. "Maximum length of the Deadline and SCHEDULED keywords.")
  2549. (make-variable-buffer-local 'org-ds-keyword-length)
  2550. (defvar org-deadline-regexp nil
  2551. "Matches the DEADLINE keyword.")
  2552. (make-variable-buffer-local 'org-deadline-regexp)
  2553. (defvar org-deadline-time-regexp nil
  2554. "Matches the DEADLINE keyword together with a time stamp.")
  2555. (make-variable-buffer-local 'org-deadline-time-regexp)
  2556. (defvar org-deadline-line-regexp nil
  2557. "Matches the DEADLINE keyword and the rest of the line.")
  2558. (make-variable-buffer-local 'org-deadline-line-regexp)
  2559. (defvar org-scheduled-regexp nil
  2560. "Matches the SCHEDULED keyword.")
  2561. (make-variable-buffer-local 'org-scheduled-regexp)
  2562. (defvar org-scheduled-time-regexp nil
  2563. "Matches the SCHEDULED keyword together with a time stamp.")
  2564. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2565. (defvar org-closed-time-regexp nil
  2566. "Matches the CLOSED keyword together with a time stamp.")
  2567. (make-variable-buffer-local 'org-closed-time-regexp)
  2568. (defvar org-keyword-time-regexp nil
  2569. "Matches any of the 3 keywords, together with the time stamp.")
  2570. (make-variable-buffer-local 'org-keyword-time-regexp)
  2571. (defvar org-maybe-keyword-time-regexp nil
  2572. "Matches a timestamp, possibly preceeded by a keyword.")
  2573. (make-variable-buffer-local 'org-keyword-time-regexp)
  2574. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2575. rear-nonsticky t mouse-map t)
  2576. "Properties to remove when a string without properties is wanted.")
  2577. (defsubst org-match-string-no-properties (num &optional string)
  2578. (if (featurep 'xemacs)
  2579. (let ((s (match-string num string)))
  2580. (remove-text-properties 0 (length s) org-rm-props s)
  2581. s)
  2582. (match-string-no-properties num string)))
  2583. (defsubst org-no-properties (s)
  2584. (remove-text-properties 0 (length s) org-rm-props s)
  2585. s)
  2586. (defsubst org-get-alist-option (option key)
  2587. (cond ((eq key t) t)
  2588. ((eq option t) t)
  2589. ((assoc key option) (cdr (assoc key option)))
  2590. (t (cdr (assq 'default option)))))
  2591. (defsubst org-set-local (var value)
  2592. "Make VAR local in current buffer and set it to VALUE."
  2593. (set (make-variable-buffer-local var) value))
  2594. (defsubst org-mode-p ()
  2595. "Check if the current buffer is in Org-mode."
  2596. (eq major-mode 'org-mode))
  2597. (defsubst org-last (list)
  2598. "Return the last element of LIST."
  2599. (car (last list)))
  2600. (defun org-let (list &rest body)
  2601. (eval (cons 'let (cons list body))))
  2602. (put 'org-let 'lisp-indent-function 1)
  2603. (defun org-let2 (list1 list2 &rest body)
  2604. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2605. (put 'org-let2 'lisp-indent-function 2)
  2606. (defconst org-startup-options
  2607. '(("fold" org-startup-folded t)
  2608. ("overview" org-startup-folded t)
  2609. ("nofold" org-startup-folded nil)
  2610. ("showall" org-startup-folded nil)
  2611. ("content" org-startup-folded content)
  2612. ("hidestars" org-hide-leading-stars t)
  2613. ("showstars" org-hide-leading-stars nil)
  2614. ("odd" org-odd-levels-only t)
  2615. ("oddeven" org-odd-levels-only nil)
  2616. ("align" org-startup-align-all-tables t)
  2617. ("noalign" org-startup-align-all-tables nil)
  2618. ("customtime" org-display-custom-times t)
  2619. ("logging" org-log-done t)
  2620. ("nologging" org-log-done nil)
  2621. ("dlcheck" org-startup-with-deadline-check t)
  2622. ("nodlcheck" org-startup-with-deadline-check nil)))
  2623. (defun org-set-regexps-and-options ()
  2624. "Precompute regular expressions for current buffer."
  2625. (when (org-mode-p)
  2626. (let ((re (org-make-options-regexp
  2627. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2628. "STARTUP" "ARCHIVE" "TAGS" "LINK")))
  2629. (splitre "[ \t]+")
  2630. kwds int key value cat arch tags links)
  2631. (save-excursion
  2632. (save-restriction
  2633. (widen)
  2634. (goto-char (point-min))
  2635. (while (re-search-forward re nil t)
  2636. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2637. (cond
  2638. ((equal key "CATEGORY")
  2639. (if (string-match "[ \t]+$" value)
  2640. (setq value (replace-match "" t t value)))
  2641. (setq cat (intern value)))
  2642. ((equal key "SEQ_TODO")
  2643. (setq int 'sequence
  2644. kwds (append kwds (org-split-string value splitre))))
  2645. ((equal key "PRI_TODO")
  2646. (setq int 'priority
  2647. kwds (append kwds (org-split-string value splitre))))
  2648. ((equal key "TYP_TODO")
  2649. (setq int 'type
  2650. kwds (append kwds (org-split-string value splitre))))
  2651. ((equal key "TAGS")
  2652. (setq tags (append tags (org-split-string value splitre))))
  2653. ((equal key "LINK")
  2654. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2655. (push (cons (match-string 1 value)
  2656. (org-trim (match-string 2 value)))
  2657. links)))
  2658. ((equal key "STARTUP")
  2659. (let ((opts (org-split-string value splitre))
  2660. l var val)
  2661. (while (setq l (assoc (pop opts) org-startup-options))
  2662. (setq var (nth 1 l) val (nth 2 l))
  2663. (set (make-local-variable var) val))))
  2664. ((equal key "ARCHIVE")
  2665. (string-match " *$" value)
  2666. (setq arch (replace-match "" t t value))
  2667. (remove-text-properties 0 (length arch)
  2668. '(face t fontified t) arch)))
  2669. )))
  2670. (and cat (org-set-local 'org-category cat))
  2671. (and kwds (org-set-local 'org-todo-keywords kwds))
  2672. (and arch (org-set-local 'org-archive-location arch))
  2673. (and int (org-set-local 'org-todo-interpretation int))
  2674. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2675. (when tags
  2676. (let (e tgs)
  2677. (while (setq e (pop tags))
  2678. (cond
  2679. ((equal e "{") (push '(:startgroup) tgs))
  2680. ((equal e "}") (push '(:endgroup) tgs))
  2681. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2682. (push (cons (match-string 1 e)
  2683. (string-to-char (match-string 2 e)))
  2684. tgs))
  2685. (t (push (list e) tgs))))
  2686. (org-set-local 'org-tag-alist nil)
  2687. (while (setq e (pop tgs))
  2688. (or (and (stringp (car e))
  2689. (assoc (car e) org-tag-alist))
  2690. (push e org-tag-alist))))))
  2691. ;; Compute the regular expressions and other local variables
  2692. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2693. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2694. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2695. (length org-scheduled-string)))
  2696. org-done-string
  2697. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2698. org-todo-regexp
  2699. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2700. "\\|") "\\)\\>")
  2701. org-not-done-regexp
  2702. (concat "\\<\\("
  2703. (mapconcat 'regexp-quote
  2704. (nreverse (cdr (reverse org-todo-keywords)))
  2705. "\\|")
  2706. "\\)\\>")
  2707. org-todo-line-regexp
  2708. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2709. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2710. "\\)\\>\\)? *\\(.*\\)")
  2711. org-nl-done-regexp
  2712. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2713. org-todo-line-tags-regexp
  2714. (concat "^\\(\\*+\\)[ \t]*\\(?:\\("
  2715. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2716. "\\)\\>\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2717. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2718. org-deadline-regexp (concat "\\<" org-deadline-string)
  2719. org-deadline-time-regexp
  2720. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2721. org-deadline-line-regexp
  2722. (concat "\\<\\(" org-deadline-string "\\).*")
  2723. org-scheduled-regexp
  2724. (concat "\\<" org-scheduled-string)
  2725. org-scheduled-time-regexp
  2726. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2727. org-closed-time-regexp
  2728. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2729. org-keyword-time-regexp
  2730. (concat "\\<\\(" org-scheduled-string
  2731. "\\|" org-deadline-string
  2732. "\\|" org-closed-string
  2733. "\\|" org-clock-string "\\)"
  2734. " *[[<]\\([^]>]+\\)[]>]")
  2735. org-maybe-keyword-time-regexp
  2736. (concat "\\(\\<\\(" org-scheduled-string
  2737. "\\|" org-deadline-string
  2738. "\\|" org-closed-string
  2739. "\\|" org-clock-string "\\)\\)?"
  2740. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2741. (org-set-font-lock-defaults)))
  2742. ;; Tell the compiler about dynamically scoped variables,
  2743. ;; and variables from other packages
  2744. (defvar calc-embedded-close-formula) ; defined by the calc package
  2745. (defvar calc-embedded-open-formula) ; defined by the calc package
  2746. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2747. (defvar zmacs-regions) ; XEmacs regions
  2748. (defvar original-date) ; dynamically scoped in calendar
  2749. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2750. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2751. (defvar org-html-entities) ; defined later in this file
  2752. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2753. (defvar org-time-was-given) ; dynamically scoped parameter
  2754. (defvar org-ts-what) ; dynamically scoped parameter
  2755. (defvar org-current-export-file) ; dynamically scoped parameter
  2756. (defvar org-current-export-dir) ; dynamically scoped parameter
  2757. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2758. (defvar timecnt) ; dynamically scoped parameter
  2759. (defvar levels-open) ; dynamically scoped parameter
  2760. (defvar entry) ; dynamically scoped parameter
  2761. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2762. (defvar date) ; dynamically scoped parameter
  2763. (defvar description) ; dynamically scoped parameter
  2764. (defvar ans1) ; dynamically scoped parameter
  2765. (defvar ans2) ; dynamically scoped parameter
  2766. (defvar starting-day) ; local variable
  2767. (defvar include-all-loc) ; local variable
  2768. (defvar vm-message-pointer) ; from vm
  2769. (defvar vm-folder-directory) ; from vm
  2770. (defvar gnus-other-frame-object) ; from gnus
  2771. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2772. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2773. (defvar gnus-group-name) ; from gnus
  2774. (defvar gnus-article-current) ; from gnus
  2775. (defvar w3m-current-url) ; from w3m
  2776. (defvar w3m-current-title) ; from w3m
  2777. (defvar mh-progs) ; from MH-E
  2778. (defvar mh-current-folder) ; from MH-E
  2779. (defvar mh-show-folder-buffer) ; from MH-E
  2780. (defvar mh-index-folder) ; from MH-E
  2781. (defvar mh-searcher) ; from MH-E
  2782. (defvar org-selected-point) ; dynamically scoped parameter
  2783. (defvar calendar-mode-map) ; from calendar.el
  2784. (defvar last-arg) ; local variable
  2785. (defvar remember-save-after-remembering) ; from remember.el
  2786. (defvar remember-data-file) ; from remember.el
  2787. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2788. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2789. (defvar orgtbl-mode) ; defined later in this file
  2790. (defvar Info-current-file) ; from info.el
  2791. (defvar Info-current-node) ; from info.el
  2792. (defvar texmathp-why) ; from texmathp.el
  2793. (defvar org-latex-regexps)
  2794. (defvar outline-mode-menu-heading)
  2795. (defvar outline-mode-menu-show)
  2796. (defvar outline-mode-menu-hide)
  2797. ;;; Define the mode
  2798. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2799. (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."))
  2800. (defvar org-struct-menu) ; defined later in this file
  2801. (defvar org-org-menu) ; defined later in this file
  2802. (defvar org-tbl-menu) ; defined later in this file
  2803. ;; We use a before-change function to check if a table might need
  2804. ;; an update.
  2805. (defvar org-table-may-need-update t
  2806. "Indicates that a table might need an update.
  2807. This variable is set by `org-before-change-function'.
  2808. `org-table-align' sets it back to nil.")
  2809. (defvar org-mode-map)
  2810. (defvar org-mode-hook nil)
  2811. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2812. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2813. ;;;###autoload
  2814. (define-derived-mode org-mode outline-mode "Org"
  2815. "Outline-based notes management and organizer, alias
  2816. \"Carsten's outline-mode for keeping track of everything.\"
  2817. Org-mode develops organizational tasks around a NOTES file which
  2818. contains information about projects as plain text. Org-mode is
  2819. implemented on top of outline-mode, which is ideal to keep the content
  2820. of large files well structured. It supports ToDo items, deadlines and
  2821. time stamps, which magically appear in the diary listing of the Emacs
  2822. calendar. Tables are easily created with a built-in table editor.
  2823. Plain text URL-like links connect to websites, emails (VM), Usenet
  2824. messages (Gnus), BBDB entries, and any files related to the project.
  2825. For printing and sharing of notes, an Org-mode file (or a part of it)
  2826. can be exported as a structured ASCII or HTML file.
  2827. The following commands are available:
  2828. \\{org-mode-map}"
  2829. ;; Get rid of Outline menus, they are not needed
  2830. ;; Need to do this here because define-derived-mode sets up
  2831. ;; the keymap so late. Still, it is a waste to call this each time
  2832. ;; we switch another buffer into org-mode.
  2833. (if (featurep 'xemacs)
  2834. (when (boundp 'outline-mode-menu-heading)
  2835. ;; Assume this is Greg's port, it used easymenu
  2836. (easy-menu-remove outline-mode-menu-heading)
  2837. (easy-menu-remove outline-mode-menu-show)
  2838. (easy-menu-remove outline-mode-menu-hide))
  2839. (define-key org-mode-map [menu-bar headings] 'undefined)
  2840. (define-key org-mode-map [menu-bar hide] 'undefined)
  2841. (define-key org-mode-map [menu-bar show] 'undefined))
  2842. (easy-menu-add org-org-menu)
  2843. (easy-menu-add org-tbl-menu)
  2844. (org-install-agenda-files-menu)
  2845. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2846. (org-add-to-invisibility-spec '(org-cwidth))
  2847. (when (featurep 'xemacs)
  2848. (org-set-local 'line-move-ignore-invisible t))
  2849. (setq outline-regexp "\\*+")
  2850. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2851. (setq outline-level 'org-outline-level)
  2852. (when (and org-ellipsis (stringp org-ellipsis))
  2853. (unless org-display-table
  2854. (setq org-display-table (make-display-table)))
  2855. (set-display-table-slot org-display-table
  2856. 4 (string-to-vector org-ellipsis))
  2857. (setq buffer-display-table org-display-table))
  2858. (org-set-regexps-and-options)
  2859. ;; Calc embedded
  2860. (org-set-local 'calc-embedded-open-mode "# ")
  2861. (modify-syntax-entry ?# "<")
  2862. (if org-startup-truncated (setq truncate-lines t))
  2863. (org-set-local 'font-lock-unfontify-region-function
  2864. 'org-unfontify-region)
  2865. ;; Activate before-change-function
  2866. (org-set-local 'org-table-may-need-update t)
  2867. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2868. 'local)
  2869. ;; Check for running clock before killing a buffer
  2870. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2871. ;; Paragraphs and auto-filling
  2872. (org-set-autofill-regexps)
  2873. (org-update-radio-target-regexp)
  2874. (if (and org-insert-mode-line-in-empty-file
  2875. (interactive-p)
  2876. (= (point-min) (point-max)))
  2877. (insert " -*- mode: org -*-\n\n"))
  2878. (unless org-inhibit-startup
  2879. (when org-startup-align-all-tables
  2880. (let ((bmp (buffer-modified-p)))
  2881. (org-table-map-tables 'org-table-align)
  2882. (set-buffer-modified-p bmp)))
  2883. (if org-startup-with-deadline-check
  2884. (call-interactively 'org-check-deadlines)
  2885. (cond
  2886. ((eq org-startup-folded t)
  2887. (org-cycle '(4)))
  2888. ((eq org-startup-folded 'content)
  2889. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2890. (org-cycle '(4)) (org-cycle '(4))))))))
  2891. (defsubst org-call-with-arg (command arg)
  2892. "Call COMMAND interactively, but pretend prefix are was ARG."
  2893. (let ((current-prefix-arg arg)) (call-interactively command)))
  2894. (defsubst org-current-line (&optional pos)
  2895. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2896. (defun org-current-time ()
  2897. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2898. (if (> org-time-stamp-rounding-minutes 0)
  2899. (let ((r org-time-stamp-rounding-minutes)
  2900. (time (decode-time)))
  2901. (apply 'encode-time
  2902. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2903. (nthcdr 2 time))))
  2904. (current-time)))
  2905. (defun org-add-props (string plist &rest props)
  2906. "Add text properties to entire string, from beginning to end.
  2907. PLIST may be a list of properties, PROPS are individual properties and values
  2908. that will be added to PLIST. Returns the string that was modified."
  2909. (add-text-properties
  2910. 0 (length string) (if props (append plist props) plist) string)
  2911. string)
  2912. (put 'org-add-props 'lisp-indent-function 2)
  2913. ;;; Font-Lock stuff
  2914. (defvar org-mouse-map (make-sparse-keymap))
  2915. (define-key org-mouse-map
  2916. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2917. (define-key org-mouse-map
  2918. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2919. (when org-mouse-1-follows-link
  2920. (define-key org-mouse-map [follow-link] 'mouse-face))
  2921. (when org-tab-follows-link
  2922. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2923. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2924. (when org-return-follows-link
  2925. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2926. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2927. (require 'font-lock)
  2928. (defconst org-non-link-chars "]\t\n\r<>")
  2929. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2930. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2931. (defconst org-link-re-with-space
  2932. (concat
  2933. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2934. "\\([^" org-non-link-chars " ]"
  2935. "[^" org-non-link-chars "]*"
  2936. "[^" org-non-link-chars " ]\\)>?")
  2937. "Matches a link with spaces, optional angular brackets around it.")
  2938. (defconst org-link-re-with-space2
  2939. (concat
  2940. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2941. "\\([^" org-non-link-chars " ]"
  2942. "[^]\t\n\r]*"
  2943. "[^" org-non-link-chars " ]\\)>?")
  2944. "Matches a link with spaces, optional angular brackets around it.")
  2945. (defconst org-angle-link-re
  2946. (concat
  2947. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2948. "\\([^" org-non-link-chars " ]"
  2949. "[^" org-non-link-chars "]*"
  2950. "\\)>")
  2951. "Matches link with angular brackets, spaces are allowed.")
  2952. (defconst org-plain-link-re
  2953. (concat
  2954. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2955. "\\([^]\t\n\r<>,;() ]+\\)")
  2956. "Matches plain link, without spaces.")
  2957. (defconst org-bracket-link-regexp
  2958. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2959. "Matches a link in double brackets.")
  2960. (defconst org-bracket-link-analytic-regexp
  2961. (concat
  2962. "\\[\\["
  2963. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2964. "\\([^]]+\\)"
  2965. "\\]"
  2966. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2967. "\\]"))
  2968. ; 1: http:
  2969. ; 2: http
  2970. ; 3: path
  2971. ; 4: [desc]
  2972. ; 5: desc
  2973. (defconst org-ts-lengths
  2974. (cons (length (format-time-string (car org-time-stamp-formats)))
  2975. (length (format-time-string (cdr org-time-stamp-formats))))
  2976. "This holds the lengths of the two different time formats.")
  2977. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2978. "Regular expression for fast time stamp matching.")
  2979. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2980. "Regular expression for fast time stamp matching.")
  2981. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2982. "Regular expression matching time strings for analysis.")
  2983. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2984. "Regular expression matching time stamps, with groups.")
  2985. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2986. "Regular expression matching time stamps (also [..]), with groups.")
  2987. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2988. "Regular expression matching a time stamp range.")
  2989. (defconst org-tr-regexp-both
  2990. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2991. "Regular expression matching a time stamp range.")
  2992. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2993. org-ts-regexp "\\)?")
  2994. "Regular expression matching a time stamp or time stamp range.")
  2995. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2996. org-ts-regexp-both "\\)?")
  2997. "Regular expression matching a time stamp or time stamp range.
  2998. The time stamps may be either active or inactive.")
  2999. (defvar org-�§emph-face nil)
  3000. (defun org-do-emphasis-faces (limit)
  3001. "Run through the buffer and add overlays to links."
  3002. (if (re-search-forward org-emph-re limit t)
  3003. (progn
  3004. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3005. 'face
  3006. (nth 1 (assoc (match-string 3)
  3007. org-emphasis-alist)))
  3008. (add-text-properties (match-beginning 2) (match-end 2)
  3009. '(font-lock-multiline t))
  3010. (backward-char 1)
  3011. t)))
  3012. (defun org-activate-plain-links (limit)
  3013. "Run through the buffer and add overlays to links."
  3014. (if (re-search-forward org-plain-link-re limit t)
  3015. (progn
  3016. (add-text-properties (match-beginning 0) (match-end 0)
  3017. (list 'mouse-face 'highlight
  3018. 'rear-nonsticky t
  3019. 'keymap org-mouse-map
  3020. ))
  3021. t)))
  3022. (defun org-activate-angle-links (limit)
  3023. "Run through the buffer and add overlays to links."
  3024. (if (re-search-forward org-angle-link-re limit t)
  3025. (progn
  3026. (add-text-properties (match-beginning 0) (match-end 0)
  3027. (list 'mouse-face 'highlight
  3028. 'rear-nonsticky t
  3029. 'keymap org-mouse-map
  3030. ))
  3031. t)))
  3032. (defmacro org-maybe-intangible (props)
  3033. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3034. In emacs 21, invisible text is not avoided by the command loop, so the
  3035. intangible property is needed to make sure point skips this text.
  3036. In Emacs 22, this is not necessary. The intangible text property has
  3037. led to problems with flyspell. These problems are fixed in flyspell.el,
  3038. but we still avoid setting the property in Emacs 22 and later.
  3039. We use a macro so that the test can happen at compilation time."
  3040. (if (< emacs-major-version 22)
  3041. `(append '(intangible t) ,props)
  3042. props))
  3043. (defun org-activate-bracket-links (limit)
  3044. "Run through the buffer and add overlays to bracketed links."
  3045. (if (re-search-forward org-bracket-link-regexp limit t)
  3046. (let* ((help (concat "LINK: "
  3047. (org-match-string-no-properties 1)))
  3048. ;; FIXME: above we should remove the escapes.
  3049. ;; but that requires another match, protecting match data,
  3050. ;; a lot of overhead for font-lock.
  3051. (ip (org-maybe-intangible
  3052. (list 'invisible 'org-link 'rear-nonsticky t
  3053. 'keymap org-mouse-map 'mouse-face 'highlight
  3054. 'help-echo help)))
  3055. (vp (list 'rear-nonsticky t
  3056. 'keymap org-mouse-map 'mouse-face 'highlight
  3057. 'help-echo help)))
  3058. ;; We need to remove the invisible property here. Table narrowing
  3059. ;; may have made some of this invisible.
  3060. (remove-text-properties (match-beginning 0) (match-end 0)
  3061. '(invisible nil))
  3062. (if (match-end 3)
  3063. (progn
  3064. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3065. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3066. (add-text-properties (match-end 3) (match-end 0) ip))
  3067. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3068. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3069. (add-text-properties (match-end 1) (match-end 0) ip))
  3070. t)))
  3071. (defun org-activate-dates (limit)
  3072. "Run through the buffer and add overlays to dates."
  3073. ; (if (re-search-forward org-tsr-regexp limit t)
  3074. ; (if (re-search-forward
  3075. ; (if org-display-custom-times org-ts-regexp-both org-tsr-regexp-both)
  3076. ; limit t)
  3077. (if (re-search-forward org-tsr-regexp-both limit t)
  3078. (progn
  3079. (add-text-properties (match-beginning 0) (match-end 0)
  3080. (list 'mouse-face 'highlight
  3081. 'rear-nonsticky t
  3082. 'keymap org-mouse-map))
  3083. (when org-display-custom-times
  3084. (if (match-end 3)
  3085. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3086. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3087. t)))
  3088. (defvar org-target-link-regexp nil
  3089. "Regular expression matching radio targets in plain text.")
  3090. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3091. "Regular expression matching a link target.")
  3092. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3093. "Regular expression matching a link target.")
  3094. (defun org-activate-target-links (limit)
  3095. "Run through the buffer and add overlays to target matches."
  3096. (when org-target-link-regexp
  3097. (let ((case-fold-search t))
  3098. (if (re-search-forward org-target-link-regexp limit t)
  3099. (progn
  3100. (add-text-properties (match-beginning 0) (match-end 0)
  3101. (list 'mouse-face 'highlight
  3102. 'rear-nonsticky t
  3103. 'keymap org-mouse-map
  3104. 'help-echo "Radio target link"
  3105. 'org-linked-text t))
  3106. t)))))
  3107. (defun org-update-radio-target-regexp ()
  3108. "Find all radio targets in this file and update the regular expression."
  3109. (interactive)
  3110. (when (memq 'radio org-activate-links)
  3111. (setq org-target-link-regexp
  3112. (org-make-target-link-regexp (org-all-targets 'radio)))
  3113. (org-restart-font-lock)))
  3114. (defun org-hide-wide-columns (limit)
  3115. (let (s e)
  3116. (setq s (text-property-any (point) (or limit (point-max))
  3117. 'org-cwidth t))
  3118. (when s
  3119. (setq e (next-single-property-change s 'org-cwidth))
  3120. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3121. (goto-char e)
  3122. t)))
  3123. (defun org-restart-font-lock ()
  3124. "Restart font-lock-mode, to force refontification."
  3125. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3126. (font-lock-mode -1)
  3127. (font-lock-mode 1)))
  3128. (defun org-all-targets (&optional radio)
  3129. "Return a list of all targets in this file.
  3130. With optional argument RADIO, only find radio targets."
  3131. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3132. rtn)
  3133. (save-excursion
  3134. (goto-char (point-min))
  3135. (while (re-search-forward re nil t)
  3136. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3137. rtn)))
  3138. (defun org-make-target-link-regexp (targets)
  3139. "Make regular expression matching all strings in TARGETS.
  3140. The regular expression finds the targets also if there is a line break
  3141. between words."
  3142. (and targets
  3143. (concat
  3144. "\\<\\("
  3145. (mapconcat
  3146. (lambda (x)
  3147. (while (string-match " +" x)
  3148. (setq x (replace-match "\\s-+" t t x)))
  3149. x)
  3150. targets
  3151. "\\|")
  3152. "\\)\\>")))
  3153. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  3154. "Matches CamelCase words, possibly with a star before it.")
  3155. (defun org-activate-camels (limit)
  3156. "Run through the buffer and add overlays to dates."
  3157. (if (re-search-forward org-camel-regexp limit t)
  3158. (progn
  3159. (add-text-properties (match-beginning 0) (match-end 0)
  3160. (list 'mouse-face 'highlight
  3161. 'rear-nonsticky t
  3162. 'keymap org-mouse-map))
  3163. t)))
  3164. (defun org-activate-tags (limit)
  3165. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  3166. (progn
  3167. (add-text-properties (match-beginning 1) (match-end 1)
  3168. (list 'mouse-face 'highlight
  3169. 'rear-nonsticky t
  3170. 'keymap org-mouse-map))
  3171. t)))
  3172. (defun org-font-lock-level ()
  3173. (save-excursion
  3174. (org-back-to-heading t)
  3175. (- (match-end 0) (match-beginning 0))))
  3176. (defun org-outline-level ()
  3177. (save-excursion
  3178. (looking-at outline-regexp)
  3179. (if (match-beginning 1)
  3180. (+ (org-get-string-indentation (match-string 1)) 1000)
  3181. (- (match-end 0) (match-beginning 0)))))
  3182. (defvar org-font-lock-keywords nil)
  3183. (defun org-set-font-lock-defaults ()
  3184. (let* ((em org-fontify-emphasized-text)
  3185. (lk org-activate-links)
  3186. (org-font-lock-extra-keywords
  3187. ;; Headlines
  3188. (list
  3189. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  3190. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3191. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3192. (1 'org-table))
  3193. ;; Links
  3194. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3195. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3196. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3197. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3198. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3199. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  3200. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3201. (if org-table-limit-column-width
  3202. '(org-hide-wide-columns (0 nil append)))
  3203. ;; TODO lines
  3204. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  3205. '(1 'org-todo t))
  3206. ;; Priorities
  3207. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  3208. ;; Special keywords
  3209. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3210. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3211. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3212. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3213. ;; Emphasis
  3214. (if em
  3215. (if (featurep 'xemacs)
  3216. '(org-do-emphasis-faces (0 nil append))
  3217. '(org-do-emphasis-faces)))
  3218. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  3219. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  3220. 2 'bold prepend)
  3221. (if org-provide-checkbox-statistics
  3222. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3223. (0 (org-get-checkbox-statistics-face) t)))
  3224. ;; COMMENT
  3225. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  3226. "\\|" org-quote-string "\\)\\>")
  3227. '(1 'org-special-keyword t))
  3228. '("^#.*" (0 'font-lock-comment-face t))
  3229. ;; DONE
  3230. (if org-fontify-done-headline
  3231. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  3232. '(1 'org-done t) '(2 'org-headline-done t))
  3233. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  3234. '(1 'org-done t)))
  3235. ;; Table stuff
  3236. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  3237. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3238. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  3239. (if org-format-transports-properties-p
  3240. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3241. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  3242. )))
  3243. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3244. ;; Now set the full font-lock-keywords
  3245. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3246. (org-set-local 'font-lock-defaults
  3247. '(org-font-lock-keywords t nil nil backward-paragraph))
  3248. (kill-local-variable 'font-lock-keywords) nil))
  3249. (defvar org-m nil)
  3250. (defvar org-l nil)
  3251. (defvar org-f nil)
  3252. (defun org-get-level-face (n)
  3253. "Get the right face for match N in font-lock matching of healdines."
  3254. (setq org-l (- (match-end 2) (match-beginning 1)))
  3255. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3256. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  3257. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  3258. (cond
  3259. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3260. ((eq n 2) org-f)
  3261. (t (if org-level-color-stars-only nil org-f))))
  3262. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3263. "Remove fontification and activation overlays from links."
  3264. (font-lock-default-unfontify-region beg end)
  3265. (let* ((buffer-undo-list t)
  3266. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3267. (inhibit-modification-hooks t)
  3268. deactivate-mark buffer-file-name buffer-file-truename)
  3269. (remove-text-properties beg end
  3270. '(mouse-face t keymap t org-linked-text t
  3271. rear-nonsticky t
  3272. invisible t intangible t))))
  3273. ;;; Visibility cycling
  3274. (defvar org-cycle-global-status nil)
  3275. (make-variable-buffer-local 'org-cycle-global-status)
  3276. (defvar org-cycle-subtree-status nil)
  3277. (make-variable-buffer-local 'org-cycle-subtree-status)
  3278. ;;;###autoload
  3279. (defun org-cycle (&optional arg)
  3280. "Visibility cycling for Org-mode.
  3281. - When this function is called with a prefix argument, rotate the entire
  3282. buffer through 3 states (global cycling)
  3283. 1. OVERVIEW: Show only top-level headlines.
  3284. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3285. 3. SHOW ALL: Show everything.
  3286. - When point is at the beginning of a headline, rotate the subtree started
  3287. by this line through 3 different states (local cycling)
  3288. 1. FOLDED: Only the main headline is shown.
  3289. 2. CHILDREN: The main headline and the direct children are shown.
  3290. From this state, you can move to one of the children
  3291. and zoom in further.
  3292. 3. SUBTREE: Show the entire subtree, including body text.
  3293. - When there is a numeric prefix, go up to a heading with level ARG, do
  3294. a `show-subtree' and return to the previous cursor position. If ARG
  3295. is negative, go up that many levels.
  3296. - When point is not at the beginning of a headline, execute
  3297. `indent-relative', like TAB normally does. See the option
  3298. `org-cycle-emulate-tab' for details.
  3299. - Special case: if point is the the beginning of the buffer and there is
  3300. no headline in line 1, this function will act as if called with prefix arg."
  3301. (interactive "P")
  3302. (let* ((outline-regexp
  3303. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3304. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3305. outline-regexp))
  3306. (bob-special (and org-cycle-global-at-bob (bobp)
  3307. (not (looking-at outline-regexp))))
  3308. (org-cycle-hook
  3309. (if bob-special
  3310. (delq 'org-optimize-window-after-visibility-change
  3311. (copy-sequence org-cycle-hook))
  3312. org-cycle-hook))
  3313. (pos (point)))
  3314. (if (or bob-special (equal arg '(4)))
  3315. ;; special case: use global cycling
  3316. (setq arg t))
  3317. (cond
  3318. ((org-at-table-p 'any)
  3319. ;; Enter the table or move to the next field in the table
  3320. (or (org-table-recognize-table.el)
  3321. (progn
  3322. (if arg (org-table-edit-field t)
  3323. (org-table-justify-field-maybe)
  3324. (call-interactively 'org-table-next-field)))))
  3325. ((eq arg t) ;; Global cycling
  3326. (cond
  3327. ((and (eq last-command this-command)
  3328. (eq org-cycle-global-status 'overview))
  3329. ;; We just created the overview - now do table of contents
  3330. ;; This can be slow in very large buffers, so indicate action
  3331. (message "CONTENTS...")
  3332. (org-content)
  3333. (message "CONTENTS...done")
  3334. (setq org-cycle-global-status 'contents)
  3335. (run-hook-with-args 'org-cycle-hook 'contents))
  3336. ((and (eq last-command this-command)
  3337. (eq org-cycle-global-status 'contents))
  3338. ;; We just showed the table of contents - now show everything
  3339. (show-all)
  3340. (message "SHOW ALL")
  3341. (setq org-cycle-global-status 'all)
  3342. (run-hook-with-args 'org-cycle-hook 'all))
  3343. (t
  3344. ;; Default action: go to overview
  3345. (org-overview)
  3346. (message "OVERVIEW")
  3347. (setq org-cycle-global-status 'overview)
  3348. (run-hook-with-args 'org-cycle-hook 'overview))))
  3349. ((integerp arg)
  3350. ;; Show-subtree, ARG levels up from here.
  3351. (save-excursion
  3352. (org-back-to-heading)
  3353. (outline-up-heading (if (< arg 0) (- arg)
  3354. (- (funcall outline-level) arg)))
  3355. (org-show-subtree)))
  3356. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3357. ;; At a heading: rotate between three different views
  3358. (org-back-to-heading)
  3359. (let ((goal-column 0) eoh eol eos)
  3360. ;; First, some boundaries
  3361. (save-excursion
  3362. (org-back-to-heading)
  3363. (save-excursion
  3364. (beginning-of-line 2)
  3365. (while (and (not (eobp)) ;; this is like `next-line'
  3366. (get-char-property (1- (point)) 'invisible))
  3367. (beginning-of-line 2)) (setq eol (point)))
  3368. (outline-end-of-heading) (setq eoh (point))
  3369. (org-end-of-subtree t) (setq eos (point))
  3370. (outline-next-heading))
  3371. ;; Find out what to do next and set `this-command'
  3372. (cond
  3373. ((and (= eos eoh)
  3374. ;; Nothing is hidden behind this heading
  3375. (message "EMPTY ENTRY")
  3376. (setq org-cycle-subtree-status nil)))
  3377. ((>= eol eos)
  3378. ;; Entire subtree is hidden in one line: open it
  3379. (org-show-entry)
  3380. (show-children)
  3381. (message "CHILDREN")
  3382. (setq org-cycle-subtree-status 'children)
  3383. (run-hook-with-args 'org-cycle-hook 'children))
  3384. ((and (eq last-command this-command)
  3385. (eq org-cycle-subtree-status 'children))
  3386. ;; We just showed the children, now show everything.
  3387. (org-show-subtree)
  3388. (message "SUBTREE")
  3389. (setq org-cycle-subtree-status 'subtree)
  3390. (run-hook-with-args 'org-cycle-hook 'subtree))
  3391. (t
  3392. ;; Default action: hide the subtree.
  3393. (hide-subtree)
  3394. (message "FOLDED")
  3395. (setq org-cycle-subtree-status 'folded)
  3396. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3397. ;; TAB emulation
  3398. (buffer-read-only (org-back-to-heading))
  3399. ((org-try-cdlatex-tab))
  3400. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3401. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3402. (or (and (eq org-cycle-emulate-tab 'white)
  3403. (= (match-end 0) (point-at-eol)))
  3404. (and (eq org-cycle-emulate-tab 'whitestart)
  3405. (>= (match-end 0) pos))))
  3406. t
  3407. (eq org-cycle-emulate-tab t))
  3408. (if (and (looking-at "[ \n\r\t]")
  3409. (string-match "^[ \t]*$" (buffer-substring
  3410. (point-at-bol) (point))))
  3411. (progn
  3412. (beginning-of-line 1)
  3413. (and (looking-at "[ \t]+") (replace-match ""))))
  3414. (indent-relative))
  3415. (t (save-excursion
  3416. (org-back-to-heading)
  3417. (org-cycle))))))
  3418. ;;;###autoload
  3419. (defun org-global-cycle (&optional arg)
  3420. "Cycle the global visibility. For details see `org-cycle'."
  3421. (interactive "P")
  3422. (let ((org-cycle-include-plain-lists
  3423. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3424. (if (integerp arg)
  3425. (progn
  3426. (show-all)
  3427. (hide-sublevels arg)
  3428. (setq org-cycle-global-status 'contents))
  3429. (org-cycle '(4)))))
  3430. (defun org-overview ()
  3431. "Switch to overview mode, shoing only top-level headlines.
  3432. Really, this shows all headlines with level equal or greater than the level
  3433. of the first headline in the buffer. This is important, because if the
  3434. first headline is not level one, then (hide-sublevels 1) gives confusing
  3435. results."
  3436. (interactive)
  3437. (hide-sublevels (save-excursion
  3438. (goto-char (point-min))
  3439. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3440. (progn
  3441. (goto-char (match-beginning 0))
  3442. (funcall outline-level))
  3443. 1))))
  3444. ;; FIXME: allow an argument to give a limiting level for this.
  3445. (defun org-content ()
  3446. "Show all headlines in the buffer, like a table of contents"
  3447. (interactive)
  3448. (save-excursion
  3449. ;; Visit all headings and show their offspring
  3450. (goto-char (point-max))
  3451. (catch 'exit
  3452. (while (and (progn (condition-case nil
  3453. (outline-previous-visible-heading 1)
  3454. (error (goto-char (point-min))))
  3455. t)
  3456. (looking-at outline-regexp))
  3457. (show-branches)
  3458. (if (bobp) (throw 'exit nil))))))
  3459. (defun org-optimize-window-after-visibility-change (state)
  3460. "Adjust the window after a change in outline visibility.
  3461. This function is the default value of the hook `org-cycle-hook'."
  3462. (when (get-buffer-window (current-buffer))
  3463. (cond
  3464. ((eq state 'overview) (org-first-headline-recenter 1))
  3465. ((eq state 'content) nil)
  3466. ((eq state 'all) nil)
  3467. ((eq state 'folded) nil)
  3468. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3469. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3470. (defun org-subtree-end-visible-p ()
  3471. "Is the end of the current subtree visible?"
  3472. (pos-visible-in-window-p
  3473. (save-excursion (org-end-of-subtree t) (point))))
  3474. (defun org-first-headline-recenter (&optional N)
  3475. "Move cursor to the first headline and recenter the headline.
  3476. Optional argument N means, put the headline into the Nth line of the window."
  3477. (goto-char (point-min))
  3478. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3479. (beginning-of-line)
  3480. (recenter (prefix-numeric-value N))))
  3481. (defvar org-goto-window-configuration nil)
  3482. (defvar org-goto-marker nil)
  3483. (defvar org-goto-map (make-sparse-keymap))
  3484. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3485. (while (setq cmd (pop cmds))
  3486. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3487. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3488. (define-key org-goto-map [(left)] 'org-goto-left)
  3489. (define-key org-goto-map [(right)] 'org-goto-right)
  3490. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3491. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3492. (define-key org-goto-map "\C-i" 'org-cycle)
  3493. (define-key org-goto-map [(tab)] 'org-cycle)
  3494. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3495. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3496. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3497. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3498. (define-key org-goto-map "f" 'outline-forward-same-level)
  3499. (define-key org-goto-map "b" 'outline-backward-same-level)
  3500. (define-key org-goto-map "u" 'outline-up-heading)
  3501. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3502. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3503. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3504. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3505. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3506. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3507. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3508. (defconst org-goto-help
  3509. "Select a location to jump to, press RET
  3510. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3511. (defun org-goto ()
  3512. "Go to a different location of the document, keeping current visibility.
  3513. When you want to go to a different location in a document, the fastest way
  3514. is often to fold the entire buffer and then dive into the tree. This
  3515. method has the disadvantage, that the previous location will be folded,
  3516. which may not be what you want.
  3517. This command works around this by showing a copy of the current buffer in
  3518. overview mode. You can dive into the tree in that copy, to find the
  3519. location you want to reach. When pressing RET, the command returns to the
  3520. original buffer in which the visibility is still unchanged. It then jumps
  3521. to the new location, making it and the headline hierarchy above it visible."
  3522. (interactive)
  3523. (let* ((org-goto-start-pos (point))
  3524. (selected-point
  3525. (org-get-location (current-buffer) org-goto-help)))
  3526. (if selected-point
  3527. (progn
  3528. (org-mark-ring-push org-goto-start-pos)
  3529. (goto-char selected-point)
  3530. (if (or (org-invisible-p) (org-invisible-p2))
  3531. (org-show-context 'org-goto)))
  3532. (error "Quit"))))
  3533. (defun org-get-location (buf help)
  3534. "Let the user select a location in the Org-mode buffer BUF.
  3535. This function uses a recursive edit. It returns the selected position
  3536. or nil."
  3537. (let (org-selected-point)
  3538. (save-excursion
  3539. (save-window-excursion
  3540. (delete-other-windows)
  3541. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3542. (with-output-to-temp-buffer "*Help*"
  3543. (princ help))
  3544. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3545. (setq buffer-read-only nil)
  3546. (erase-buffer)
  3547. (insert-buffer-substring buf)
  3548. (let ((org-startup-truncated t)
  3549. (org-startup-folded t)
  3550. (org-startup-align-all-tables nil)
  3551. (org-startup-with-deadline-check nil))
  3552. (org-mode))
  3553. (setq buffer-read-only t)
  3554. (if (boundp 'org-goto-start-pos)
  3555. (goto-char org-goto-start-pos)
  3556. (goto-char (point-min)))
  3557. (org-beginning-of-line)
  3558. (message "Select location and press RET")
  3559. ;; now we make sure that during selection, ony very few keys work
  3560. ;; and that it is impossible to switch to another window.
  3561. (let ((gm (current-global-map))
  3562. (overriding-local-map org-goto-map))
  3563. (unwind-protect
  3564. (progn
  3565. (use-global-map org-goto-map)
  3566. (recursive-edit))
  3567. (use-global-map gm)))))
  3568. (kill-buffer "*org-goto*")
  3569. org-selected-point))
  3570. (defun org-goto-ret (&optional arg)
  3571. "Finish `org-goto' by going to the new location."
  3572. (interactive "P")
  3573. (setq org-selected-point (point)
  3574. current-prefix-arg arg)
  3575. (throw 'exit nil))
  3576. (defun org-goto-left ()
  3577. "Finish `org-goto' by going to the new location."
  3578. (interactive)
  3579. (if (org-on-heading-p)
  3580. (progn
  3581. (beginning-of-line 1)
  3582. (setq org-selected-point (point)
  3583. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3584. (throw 'exit nil))
  3585. (error "Not on a heading")))
  3586. (defun org-goto-right ()
  3587. "Finish `org-goto' by going to the new location."
  3588. (interactive)
  3589. (if (org-on-heading-p)
  3590. (progn
  3591. (outline-end-of-subtree)
  3592. (or (eobp) (forward-char 1))
  3593. (setq org-selected-point (point)
  3594. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3595. (throw 'exit nil))
  3596. (error "Not on a heading")))
  3597. (defun org-goto-quit ()
  3598. "Finish `org-goto' without cursor motion."
  3599. (interactive)
  3600. (setq org-selected-point nil)
  3601. (throw 'exit nil))
  3602. ;;; Promotion, Demotion, Inserting new headlines
  3603. (defvar org-ignore-region nil
  3604. "To temporarily disable the active region.")
  3605. (defun org-insert-heading (&optional force-heading)
  3606. "Insert a new heading or item with same depth at point.
  3607. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3608. If point is at the beginning of a headline, insert a sibling before the
  3609. current headline. If point is in the middle of a headline, split the headline
  3610. at that position and make the rest of the headline part of the sibling below
  3611. the current headline."
  3612. (interactive "P")
  3613. (if (= (buffer-size) 0)
  3614. (insert "\n* ")
  3615. (when (or force-heading (not (org-insert-item)))
  3616. (let* ((head (save-excursion
  3617. (condition-case nil
  3618. (progn
  3619. (org-back-to-heading)
  3620. (match-string 0))
  3621. (error "*"))))
  3622. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3623. pos)
  3624. (cond
  3625. ((and (org-on-heading-p) (bolp)
  3626. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3627. (open-line (if blank 2 1)))
  3628. ((and (bolp) (save-excursion
  3629. (backward-char 1) (not (org-invisible-p))))
  3630. nil)
  3631. (t (newline (if blank 2 1))))
  3632. (insert head) (just-one-space)
  3633. (setq pos (point))
  3634. (end-of-line 1)
  3635. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3636. (run-hooks 'org-insert-heading-hook)))))
  3637. (defun org-in-item-p ()
  3638. "It the cursor inside a plain list item.
  3639. Does not have to be the first line."
  3640. (save-excursion
  3641. (condition-case nil
  3642. (progn
  3643. (org-beginning-of-item)
  3644. (org-at-item-p)
  3645. t)
  3646. (error nil))))
  3647. (defun org-insert-item (&optional checkbox)
  3648. "Insert a new item at the current level.
  3649. Return t when things worked, nil when we are not in an item."
  3650. (when (save-excursion
  3651. (condition-case nil
  3652. (progn
  3653. (org-beginning-of-item)
  3654. (org-at-item-p)
  3655. (if (org-invisible-p) (error "Invisible item"))
  3656. t)
  3657. (error nil)))
  3658. (let* ((bul (match-string 0))
  3659. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3660. (match-end 0)))
  3661. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  3662. pos)
  3663. (cond
  3664. ((and (org-at-item-p) (<= (point) eow))
  3665. ;; before the bullet
  3666. (beginning-of-line 1)
  3667. (open-line (if blank 2 1)))
  3668. ((<= (point) eow)
  3669. (beginning-of-line 1))
  3670. (t (newline (if blank 2 1))))
  3671. (insert bul (if checkbox "[ ]" ""))
  3672. (just-one-space)
  3673. (setq pos (point))
  3674. (end-of-line 1)
  3675. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3676. (org-maybe-renumber-ordered-list)
  3677. (and checkbox (org-update-checkbox-count-maybe))
  3678. t))
  3679. (defun org-insert-todo-heading (arg)
  3680. "Insert a new heading with the same level and TODO state as current heading.
  3681. If the heading has no TODO state, or if the state is DONE, use the first
  3682. state (TODO by default). Also with prefix arg, force first state."
  3683. (interactive "P")
  3684. (when (not (org-insert-item 'checkbox))
  3685. (org-insert-heading)
  3686. (save-excursion
  3687. (org-back-to-heading)
  3688. (outline-previous-heading)
  3689. (looking-at org-todo-line-regexp))
  3690. (if (or arg
  3691. (not (match-beginning 2))
  3692. (equal (match-string 2) org-done-string))
  3693. (insert (car org-todo-keywords) " ")
  3694. (insert (match-string 2) " "))))
  3695. (defun org-promote-subtree ()
  3696. "Promote the entire subtree.
  3697. See also `org-promote'."
  3698. (interactive)
  3699. (save-excursion
  3700. (org-map-tree 'org-promote))
  3701. (org-fix-position-after-promote))
  3702. (defun org-demote-subtree ()
  3703. "Demote the entire subtree. See `org-demote'.
  3704. See also `org-promote'."
  3705. (interactive)
  3706. (save-excursion
  3707. (org-map-tree 'org-demote))
  3708. (org-fix-position-after-promote))
  3709. (defun org-do-promote ()
  3710. "Promote the current heading higher up the tree.
  3711. If the region is active in `transient-mark-mode', promote all headings
  3712. in the region."
  3713. (interactive)
  3714. (save-excursion
  3715. (if (org-region-active-p)
  3716. (org-map-region 'org-promote (region-beginning) (region-end))
  3717. (org-promote)))
  3718. (org-fix-position-after-promote))
  3719. (defun org-do-demote ()
  3720. "Demote the current heading lower down the tree.
  3721. If the region is active in `transient-mark-mode', demote all headings
  3722. in the region."
  3723. (interactive)
  3724. (save-excursion
  3725. (if (org-region-active-p)
  3726. (org-map-region 'org-demote (region-beginning) (region-end))
  3727. (org-demote)))
  3728. (org-fix-position-after-promote))
  3729. (defun org-fix-position-after-promote ()
  3730. "Make sure that after pro/demotion cursor position is right."
  3731. (if (and (equal (char-after) ?\n)
  3732. (save-excursion
  3733. (skip-chars-backward "a-zA-Z0-9_@")
  3734. (looking-at org-todo-regexp)))
  3735. (insert " "))
  3736. (and (equal (char-after) ?\ )
  3737. (equal (char-before) ?*)
  3738. (forward-char 1)))
  3739. (defun org-get-legal-level (level &optional change)
  3740. "Rectify a level change under the influence of `org-odd-levels-only'
  3741. LEVEL is a current level, CHANGE is by how much the level should be
  3742. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3743. even level numbers will become the next higher odd number."
  3744. (if org-odd-levels-only
  3745. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  3746. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3747. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3748. (max 1 (+ level change))))
  3749. (defun org-promote ()
  3750. "Promote the current heading higher up the tree.
  3751. If the region is active in `transient-mark-mode', promote all headings
  3752. in the region."
  3753. (org-back-to-heading t)
  3754. (let* ((level (save-match-data (funcall outline-level)))
  3755. (up-head (make-string (org-get-legal-level level -1) ?*))
  3756. (diff (abs (- level (length up-head)))))
  3757. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3758. (replace-match up-head nil t)
  3759. ;; Fixup tag positioning
  3760. (and org-auto-align-tags (org-set-tags nil t))
  3761. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3762. (defun org-demote ()
  3763. "Demote the current heading lower down the tree.
  3764. If the region is active in `transient-mark-mode', demote all headings
  3765. in the region."
  3766. (org-back-to-heading t)
  3767. (let* ((level (save-match-data (funcall outline-level)))
  3768. (down-head (make-string (org-get-legal-level level 1) ?*))
  3769. (diff (abs (- level (length down-head)))))
  3770. (replace-match down-head nil t)
  3771. ;; Fixup tag positioning
  3772. (and org-auto-align-tags (org-set-tags nil t))
  3773. (if org-adapt-indentation (org-fixup-indentation diff))))
  3774. (defun org-map-tree (fun)
  3775. "Call FUN for every heading underneath the current one."
  3776. (org-back-to-heading)
  3777. (let ((level (funcall outline-level)))
  3778. (save-excursion
  3779. (funcall fun)
  3780. (while (and (progn
  3781. (outline-next-heading)
  3782. (> (funcall outline-level) level))
  3783. (not (eobp)))
  3784. (funcall fun)))))
  3785. (defun org-map-region (fun beg end)
  3786. "Call FUN for every heading between BEG and END."
  3787. (let ((org-ignore-region t))
  3788. (save-excursion
  3789. (setq end (copy-marker end))
  3790. (goto-char beg)
  3791. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3792. (< (point) end))
  3793. (funcall fun))
  3794. (while (and (progn
  3795. (outline-next-heading)
  3796. (< (point) end))
  3797. (not (eobp)))
  3798. (funcall fun)))))
  3799. (defun org-fixup-indentation (diff)
  3800. "Change the indentation in the current entry by DIFF
  3801. However, if any line in the current entry has no indentation, or if it
  3802. would end up with no indentation after the change, nothing at all is done."
  3803. (save-excursion
  3804. (let ((end (save-excursion (outline-next-heading)
  3805. (point-marker)))
  3806. (prohibit (if (> diff 0)
  3807. "^\\S-"
  3808. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3809. col)
  3810. (unless (save-excursion (re-search-forward prohibit end t))
  3811. (while (re-search-forward "^[ \t]+" end t)
  3812. (goto-char (match-end 0))
  3813. (setq col (current-column))
  3814. (if (< diff 0) (replace-match ""))
  3815. (indent-to (+ diff col))))
  3816. (move-marker end nil))))
  3817. ;;; Vertical tree motion, cutting and pasting of subtrees
  3818. (defun org-move-subtree-up (&optional arg)
  3819. "Move the current subtree up past ARG headlines of the same level."
  3820. (interactive "p")
  3821. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3822. (defun org-move-subtree-down (&optional arg)
  3823. "Move the current subtree down past ARG headlines of the same level."
  3824. (interactive "p")
  3825. (setq arg (prefix-numeric-value arg))
  3826. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3827. 'outline-get-last-sibling))
  3828. (ins-point (make-marker))
  3829. (cnt (abs arg))
  3830. beg end txt folded)
  3831. ;; Select the tree
  3832. (org-back-to-heading)
  3833. (setq beg (point))
  3834. (save-match-data
  3835. (save-excursion (outline-end-of-heading)
  3836. (setq folded (org-invisible-p)))
  3837. (outline-end-of-subtree))
  3838. (outline-next-heading)
  3839. (setq end (point))
  3840. ;; Find insertion point, with error handling
  3841. (goto-char beg)
  3842. (while (> cnt 0)
  3843. (or (and (funcall movfunc) (looking-at outline-regexp))
  3844. (progn (goto-char beg)
  3845. (error "Cannot move past superior level or buffer limit")))
  3846. (setq cnt (1- cnt)))
  3847. (if (> arg 0)
  3848. ;; Moving forward - still need to move over subtree
  3849. (progn (outline-end-of-subtree)
  3850. (outline-next-heading)
  3851. (if (not (or (looking-at (concat "^" outline-regexp))
  3852. (bolp)))
  3853. (newline))))
  3854. (move-marker ins-point (point))
  3855. (setq txt (buffer-substring beg end))
  3856. (delete-region beg end)
  3857. (insert txt)
  3858. (goto-char ins-point)
  3859. (if folded (hide-subtree))
  3860. (move-marker ins-point nil)))
  3861. (defvar org-subtree-clip ""
  3862. "Clipboard for cut and paste of subtrees.
  3863. This is actually only a copy of the kill, because we use the normal kill
  3864. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3865. (defvar org-subtree-clip-folded nil
  3866. "Was the last copied subtree folded?
  3867. This is used to fold the tree back after pasting.")
  3868. (defun org-cut-subtree ()
  3869. "Cut the current subtree into the clipboard.
  3870. This is a short-hand for marking the subtree and then cutting it."
  3871. (interactive)
  3872. (org-copy-subtree 'cut))
  3873. (defun org-copy-subtree (&optional cut)
  3874. "Cut the current subtree into the clipboard.
  3875. This is a short-hand for marking the subtree and then copying it.
  3876. If CUT is non-nil, actually cut the subtree."
  3877. (interactive)
  3878. (let (beg end folded)
  3879. (org-back-to-heading)
  3880. (setq beg (point))
  3881. (save-match-data
  3882. (save-excursion (outline-end-of-heading)
  3883. (setq folded (org-invisible-p)))
  3884. (outline-end-of-subtree))
  3885. (if (equal (char-after) ?\n) (forward-char 1))
  3886. (setq end (point))
  3887. (goto-char beg)
  3888. (when (> end beg)
  3889. (setq org-subtree-clip-folded folded)
  3890. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3891. (setq org-subtree-clip (current-kill 0))
  3892. (message "%s: Subtree with %d characters"
  3893. (if cut "Cut" "Copied")
  3894. (length org-subtree-clip)))))
  3895. (defun org-paste-subtree (&optional level tree)
  3896. "Paste the clipboard as a subtree, with modification of headline level.
  3897. The entire subtree is promoted or demoted in order to match a new headline
  3898. level. By default, the new level is derived from the visible headings
  3899. before and after the insertion point, and taken to be the inferior headline
  3900. level of the two. So if the previous visible heading is level 3 and the
  3901. next is level 4 (or vice versa), level 4 will be used for insertion.
  3902. This makes sure that the subtree remains an independent subtree and does
  3903. not swallow low level entries.
  3904. You can also force a different level, either by using a numeric prefix
  3905. argument, or by inserting the heading marker by hand. For example, if the
  3906. cursor is after \"*****\", then the tree will be shifted to level 5.
  3907. If you want to insert the tree as is, just use \\[yank].
  3908. If optional TREE is given, use this text instead of the kill ring."
  3909. (interactive "P")
  3910. (unless (org-kill-is-subtree-p tree)
  3911. (error
  3912. (substitute-command-keys
  3913. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3914. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3915. (^re (concat "^\\(" outline-regexp "\\)"))
  3916. (re (concat "\\(" outline-regexp "\\)"))
  3917. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3918. (old-level (if (string-match ^re txt)
  3919. (- (match-end 0) (match-beginning 0))
  3920. -1))
  3921. (force-level (cond (level (prefix-numeric-value level))
  3922. ((string-match
  3923. ^re_ (buffer-substring (point-at-bol) (point)))
  3924. (- (match-end 0) (match-beginning 0)))
  3925. (t nil)))
  3926. (previous-level (save-excursion
  3927. (condition-case nil
  3928. (progn
  3929. (outline-previous-visible-heading 1)
  3930. (if (looking-at re)
  3931. (- (match-end 0) (match-beginning 0))
  3932. 1))
  3933. (error 1))))
  3934. (next-level (save-excursion
  3935. (condition-case nil
  3936. (progn
  3937. (outline-next-visible-heading 1)
  3938. (if (looking-at re)
  3939. (- (match-end 0) (match-beginning 0))
  3940. 1))
  3941. (error 1))))
  3942. (new-level (or force-level (max previous-level next-level)))
  3943. (shift (if (or (= old-level -1)
  3944. (= new-level -1)
  3945. (= old-level new-level))
  3946. 0
  3947. (- new-level old-level)))
  3948. (shift1 shift)
  3949. (delta (if (> shift 0) -1 1))
  3950. (func (if (> shift 0) 'org-demote 'org-promote))
  3951. (org-odd-levels-only nil)
  3952. beg end)
  3953. ;; Remove the forces level indicator
  3954. (if force-level
  3955. (delete-region (point-at-bol) (point)))
  3956. ;; Make sure we start at the beginning of an empty line
  3957. (if (not (bolp)) (insert "\n"))
  3958. (if (not (looking-at "[ \t]*$"))
  3959. (progn (insert "\n") (backward-char 1)))
  3960. ;; Paste
  3961. (setq beg (point))
  3962. (if (string-match "[ \t\r\n]+\\'" txt)
  3963. (setq txt (replace-match "\n" t t txt)))
  3964. (insert txt)
  3965. (setq end (point))
  3966. (if (looking-at "[ \t\r\n]+")
  3967. (replace-match "\n"))
  3968. (goto-char beg)
  3969. ;; Shift if necessary
  3970. (if (= shift 0)
  3971. (message "Pasted at level %d, without shift" new-level)
  3972. (save-restriction
  3973. (narrow-to-region beg end)
  3974. (while (not (= shift 0))
  3975. (org-map-region func (point-min) (point-max))
  3976. (setq shift (+ delta shift)))
  3977. (goto-char (point-min))
  3978. (message "Pasted at level %d, with shift by %d levels"
  3979. new-level shift1)))
  3980. (if (and kill-ring
  3981. (eq org-subtree-clip (current-kill 0))
  3982. org-subtree-clip-folded)
  3983. ;; The tree was folded before it was killed/copied
  3984. (hide-subtree))))
  3985. (defun org-kill-is-subtree-p (&optional txt)
  3986. "Check if the current kill is an outline subtree, or a set of trees.
  3987. Returns nil if kill does not start with a headline, or if the first
  3988. headline level is not the largest headline level in the tree.
  3989. So this will actually accept several entries of equal levels as well,
  3990. which is OK for `org-paste-subtree'.
  3991. If optional TXT is given, check this string instead of the current kill."
  3992. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3993. (start-level (and kill
  3994. (string-match (concat "\\`" outline-regexp) kill)
  3995. (- (match-end 0) (match-beginning 0))))
  3996. (re (concat "^" outline-regexp))
  3997. (start 1))
  3998. (if (not start-level)
  3999. nil ;; does not even start with a heading
  4000. (catch 'exit
  4001. (while (setq start (string-match re kill (1+ start)))
  4002. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  4003. (throw 'exit nil)))
  4004. t))))
  4005. (defun org-narrow-to-subtree ()
  4006. "Narrow buffer to the current subtree."
  4007. (interactive)
  4008. (save-excursion
  4009. (narrow-to-region
  4010. (progn (org-back-to-heading) (point))
  4011. (progn (org-end-of-subtree t) (point)))))
  4012. ;;; Plain list items
  4013. (defun org-at-item-p ()
  4014. "Is point in a line starting a hand-formatted item?"
  4015. (let ((llt org-plain-list-ordered-item-terminator))
  4016. (save-excursion
  4017. (goto-char (point-at-bol))
  4018. (looking-at
  4019. (cond
  4020. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4021. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4022. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4023. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4024. (defun org-at-item-checkbox-p ()
  4025. "Is point at a line starting a plain-list item with a checklet?"
  4026. (and (org-at-item-p)
  4027. (save-excursion
  4028. (goto-char (match-end 0))
  4029. (skip-chars-forward " \t")
  4030. (looking-at "\\[[ X]\\]"))))
  4031. (defun org-toggle-checkbox (&optional arg)
  4032. "Toggle the checkbox in the current line."
  4033. (interactive "P")
  4034. (catch 'exit
  4035. (let (beg end status (firstnew 'unknown))
  4036. (cond
  4037. ((org-region-active-p)
  4038. (setq beg (region-beginning) end (region-end)))
  4039. ((org-on-heading-p)
  4040. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4041. ((org-at-item-checkbox-p)
  4042. (save-excursion
  4043. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  4044. (throw 'exit t))
  4045. (t (error "Not at a checkbox or heading, and no active region")))
  4046. (save-excursion
  4047. (goto-char beg)
  4048. (while (< (point) end)
  4049. (when (org-at-item-checkbox-p)
  4050. (setq status (equal (match-string 0) "[X]"))
  4051. (when (eq firstnew 'unknown)
  4052. (setq firstnew (not status)))
  4053. (replace-match
  4054. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4055. (beginning-of-line 2)))))
  4056. (org-update-checkbox-count-maybe))
  4057. (defun org-update-checkbox-count-maybe ()
  4058. "Update checkbox statistics unless turned off by user."
  4059. (when org-provide-checkbox-statistics
  4060. (org-update-checkbox-count)))
  4061. (defun org-update-checkbox-count (&optional all)
  4062. "Update the checkbox statistics in the current section.
  4063. This will find all statistic cookies like [57%] and [6/12] and update them
  4064. with the current numbers. With optional prefix argument ALL, do this for
  4065. the whole buffer."
  4066. (interactive "P")
  4067. (save-excursion
  4068. (let* ((buffer-invisibility-spec nil) ; Emacs 21 compatibility
  4069. (beg (progn (outline-back-to-heading) (point)))
  4070. (end (move-marker (make-marker)
  4071. (progn (outline-next-heading) (point))))
  4072. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  4073. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  4074. b1 e1 f1 c-on c-off lim (cstat 0))
  4075. (when all
  4076. (goto-char (point-min))
  4077. (outline-next-heading)
  4078. (setq beg (point) end (point-max)))
  4079. (goto-char beg)
  4080. (while (re-search-forward re end t)
  4081. (setq cstat (1+ cstat)
  4082. b1 (match-beginning 0)
  4083. e1 (match-end 0)
  4084. f1 (match-beginning 1)
  4085. lim (cond
  4086. ((org-on-heading-p) (outline-next-heading) (point))
  4087. ((org-at-item-p) (org-end-of-item) (point))
  4088. (t nil))
  4089. c-on 0 c-off 0)
  4090. (goto-char e1)
  4091. (when lim
  4092. (while (re-search-forward re-box lim t)
  4093. (if (equal (match-string 2) "[ ]")
  4094. (setq c-off (1+ c-off))
  4095. (setq c-on (1+ c-on))))
  4096. (delete-region b1 e1)
  4097. (goto-char b1)
  4098. (insert (if f1
  4099. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4100. (format "[%d/%d]" c-on (+ c-on c-off))))))
  4101. (when (interactive-p)
  4102. (message "Checkbox satistics updated %s (%d places)"
  4103. (if all "in entire file" "in current outline entry") cstat)))))
  4104. (defun org-get-checkbox-statistics-face ()
  4105. "Select the face for checkbox statistics.
  4106. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4107. it will be `org-todo'."
  4108. (if (match-end 1)
  4109. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4110. (if (and (> (match-end 2) (match-beginning 2))
  4111. (equal (match-string 2) (match-string 3)))
  4112. 'org-done
  4113. 'org-todo)))
  4114. (defun org-get-indentation (&optional line)
  4115. "Get the indentation of the current line, interpreting tabs.
  4116. When LINE is given, assume it represents a line and compute its indentation."
  4117. (if line
  4118. (if (string-match "^ *" (org-remove-tabs line))
  4119. (match-end 0))
  4120. (save-excursion
  4121. (beginning-of-line 1)
  4122. (skip-chars-forward " \t")
  4123. (current-column))))
  4124. (defun org-remove-tabs (s &optional width)
  4125. "Replace tabulators in S with spaces.
  4126. Assumes that s is a single line, starting in column 0."
  4127. (setq width (or width tab-width))
  4128. (while (string-match "\t" s)
  4129. (setq s (replace-match
  4130. (make-string
  4131. (- (* width (/ (+ (match-beginning 0) width) width))
  4132. (match-beginning 0)) ?\ )
  4133. t t s)))
  4134. s)
  4135. ;; FIXME: document properly.
  4136. (defun org-fix-indentation (line ind)
  4137. "If the current indenation is smaller than ind1, leave it alone.
  4138. If it is larger than ind, reduce it by ind."
  4139. (let* ((l (org-remove-tabs line))
  4140. (i (org-get-indentation l))
  4141. (i1 (car ind)) (i2 (cdr ind)))
  4142. (if (>= i i2) (setq l (substring line i2)))
  4143. (if (> i1 0)
  4144. (concat (make-string i1 ?\ ) l)
  4145. l)))
  4146. (defun org-beginning-of-item ()
  4147. "Go to the beginning of the current hand-formatted item.
  4148. If the cursor is not in an item, throw an error."
  4149. (interactive)
  4150. (let ((pos (point))
  4151. (limit (save-excursion (org-back-to-heading)
  4152. (beginning-of-line 2) (point)))
  4153. ind ind1)
  4154. (if (org-at-item-p)
  4155. (beginning-of-line 1)
  4156. (beginning-of-line 1)
  4157. (skip-chars-forward " \t")
  4158. (setq ind (current-column))
  4159. (if (catch 'exit
  4160. (while t
  4161. (beginning-of-line 0)
  4162. (if (< (point) limit) (throw 'exit nil))
  4163. (unless (looking-at "[ \t]*$")
  4164. (skip-chars-forward " \t")
  4165. (setq ind1 (current-column))
  4166. (if (< ind1 ind)
  4167. (throw 'exit (org-at-item-p))))))
  4168. nil
  4169. (goto-char pos)
  4170. (error "Not in an item")))))
  4171. (defun org-end-of-item ()
  4172. "Go to the end of the current hand-formatted item.
  4173. If the cursor is not in an item, throw an error."
  4174. (interactive)
  4175. (let ((pos (point))
  4176. (limit (save-excursion (outline-next-heading) (point)))
  4177. (ind (save-excursion
  4178. (org-beginning-of-item)
  4179. (skip-chars-forward " \t")
  4180. (current-column)))
  4181. ind1)
  4182. (if (catch 'exit
  4183. (while t
  4184. (beginning-of-line 2)
  4185. (if (>= (point) limit) (throw 'exit t))
  4186. (unless (looking-at "[ \t]*$")
  4187. (skip-chars-forward " \t")
  4188. (setq ind1 (current-column))
  4189. (if (<= ind1 ind) (throw 'exit t)))))
  4190. (beginning-of-line 1)
  4191. (goto-char pos)
  4192. (error "Not in an item"))))
  4193. (defun org-next-item ()
  4194. "Move to the beginning of the next item in the current plain list.
  4195. Error if not at a plain list, or if this is the last item in the list."
  4196. (interactive)
  4197. (let (ind ind1 (pos (point)))
  4198. (org-beginning-of-item)
  4199. (setq ind (org-get-indentation))
  4200. (org-end-of-item)
  4201. (setq ind1 (org-get-indentation))
  4202. (unless (and (org-at-item-p) (= ind ind1))
  4203. (goto-char pos)
  4204. (error "On last item"))))
  4205. (defun org-previous-item ()
  4206. "Move to the beginning of the previous item in the current plain list.
  4207. Error if not at a plain list, or if this is the last item in the list."
  4208. (interactive)
  4209. (let (beg ind (pos (point)))
  4210. (org-beginning-of-item)
  4211. (setq beg (point))
  4212. (setq ind (org-get-indentation))
  4213. (goto-char beg)
  4214. (catch 'exit
  4215. (while t
  4216. (beginning-of-line 0)
  4217. (if (looking-at "[ \t]*$")
  4218. nil
  4219. (if (<= (org-get-indentation) ind)
  4220. (throw 'exit t)))))
  4221. (condition-case nil
  4222. (org-beginning-of-item)
  4223. (error (goto-char pos)
  4224. (error "On first item")))))
  4225. (defun org-move-item-down ()
  4226. "Move the plain list item at point down, i.e. swap with following item.
  4227. Subitems (items with larger indentation) are considered part of the item,
  4228. so this really moves item trees."
  4229. (interactive)
  4230. (let (beg end ind ind1 (pos (point)) txt)
  4231. (org-beginning-of-item)
  4232. (setq beg (point))
  4233. (setq ind (org-get-indentation))
  4234. (org-end-of-item)
  4235. (setq end (point))
  4236. (setq ind1 (org-get-indentation))
  4237. (if (and (org-at-item-p) (= ind ind1))
  4238. (progn
  4239. (org-end-of-item)
  4240. (setq txt (buffer-substring beg end))
  4241. (save-excursion
  4242. (delete-region beg end))
  4243. (setq pos (point))
  4244. (insert txt)
  4245. (goto-char pos)
  4246. (org-maybe-renumber-ordered-list))
  4247. (goto-char pos)
  4248. (error "Cannot move this item further down"))))
  4249. (defun org-move-item-up (arg)
  4250. "Move the plain list item at point up, i.e. swap with previous item.
  4251. Subitems (items with larger indentation) are considered part of the item,
  4252. so this really moves item trees."
  4253. (interactive "p")
  4254. (let (beg end ind ind1 (pos (point)) txt)
  4255. (org-beginning-of-item)
  4256. (setq beg (point))
  4257. (setq ind (org-get-indentation))
  4258. (org-end-of-item)
  4259. (setq end (point))
  4260. (goto-char beg)
  4261. (catch 'exit
  4262. (while t
  4263. (beginning-of-line 0)
  4264. (if (looking-at "[ \t]*$")
  4265. nil
  4266. (if (<= (setq ind1 (org-get-indentation)) ind)
  4267. (throw 'exit t)))))
  4268. (condition-case nil
  4269. (org-beginning-of-item)
  4270. (error (goto-char beg)
  4271. (error "Cannot move this item further up")))
  4272. (setq ind1 (org-get-indentation))
  4273. (if (and (org-at-item-p) (= ind ind1))
  4274. (progn
  4275. (setq txt (buffer-substring beg end))
  4276. (save-excursion
  4277. (delete-region beg end))
  4278. (setq pos (point))
  4279. (insert txt)
  4280. (goto-char pos)
  4281. (org-maybe-renumber-ordered-list))
  4282. (goto-char pos)
  4283. (error "Cannot move this item further up"))))
  4284. (defun org-maybe-renumber-ordered-list ()
  4285. "Renumber the ordered list at point if setup allows it.
  4286. This tests the user option `org-auto-renumber-ordered-lists' before
  4287. doing the renumbering."
  4288. (and org-auto-renumber-ordered-lists
  4289. (org-at-item-p)
  4290. (match-beginning 3)
  4291. (org-renumber-ordered-list 1)))
  4292. (defun org-get-string-indentation (s)
  4293. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4294. (let ((n -1) (i 0) (w tab-width) c)
  4295. (catch 'exit
  4296. (while (< (setq n (1+ n)) (length s))
  4297. (setq c (aref s n))
  4298. (cond ((= c ?\ ) (setq i (1+ i)))
  4299. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4300. (t (throw 'exit t)))))
  4301. i))
  4302. (defun org-renumber-ordered-list (arg)
  4303. "Renumber an ordered plain list.
  4304. Cursor needs to be in the first line of an item, the line that starts
  4305. with something like \"1.\" or \"2)\"."
  4306. (interactive "p")
  4307. (unless (and (org-at-item-p)
  4308. (match-beginning 3))
  4309. (error "This is not an ordered list"))
  4310. (let ((line (org-current-line))
  4311. (col (current-column))
  4312. (ind (org-get-string-indentation
  4313. (buffer-substring (point-at-bol) (match-beginning 3))))
  4314. ;; (term (substring (match-string 3) -1))
  4315. ind1 (n (1- arg)))
  4316. ;; find where this list begins
  4317. (catch 'exit
  4318. (while t
  4319. (catch 'next
  4320. (beginning-of-line 0)
  4321. (if (looking-at "[ \t]*$") (throw 'next t))
  4322. (skip-chars-forward " \t") (setq ind1 (current-column))
  4323. (if (or (< ind1 ind)
  4324. (and (= ind1 ind)
  4325. (not (org-at-item-p))))
  4326. (throw 'exit t)))))
  4327. ;; Walk forward and replace these numbers
  4328. (catch 'exit
  4329. (while t
  4330. (catch 'next
  4331. (beginning-of-line 2)
  4332. (if (eobp) (throw 'exit nil))
  4333. (if (looking-at "[ \t]*$") (throw 'next nil))
  4334. (skip-chars-forward " \t") (setq ind1 (current-column))
  4335. (if (> ind1 ind) (throw 'next t))
  4336. (if (< ind1 ind) (throw 'exit t))
  4337. (if (not (org-at-item-p)) (throw 'exit nil))
  4338. (if (not (match-beginning 3))
  4339. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4340. (delete-region (match-beginning 3) (1- (match-end 3)))
  4341. (goto-char (match-beginning 3))
  4342. (insert (format "%d" (setq n (1+ n)))))))
  4343. (goto-line line)
  4344. (move-to-column col)))
  4345. (defvar org-last-indent-begin-marker (make-marker))
  4346. (defvar org-last-indent-end-marker (make-marker))
  4347. (defun org-outdent-item (arg)
  4348. "Outdent a local list item."
  4349. (interactive "p")
  4350. (org-indent-item (- arg)))
  4351. (defun org-indent-item (arg)
  4352. "Indent a local list item."
  4353. (interactive "p")
  4354. (unless (org-at-item-p)
  4355. (error "Not on an item"))
  4356. (save-excursion
  4357. (let (beg end ind ind1)
  4358. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4359. (setq beg org-last-indent-begin-marker
  4360. end org-last-indent-end-marker)
  4361. (org-beginning-of-item)
  4362. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4363. (org-end-of-item)
  4364. (setq end (move-marker org-last-indent-end-marker (point))))
  4365. (goto-char beg)
  4366. (skip-chars-forward " \t") (setq ind (current-column))
  4367. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4368. (while (< (point) end)
  4369. (beginning-of-line 1)
  4370. (skip-chars-forward " \t") (setq ind1 (current-column))
  4371. (delete-region (point-at-bol) (point))
  4372. (indent-to-column (+ ind1 arg))
  4373. (beginning-of-line 2)))))
  4374. ;;; Archiving
  4375. (defun org-archive-subtree (&optional find-done)
  4376. "Move the current subtree to the archive.
  4377. The archive can be a certain top-level heading in the current file, or in
  4378. a different file. The tree will be moved to that location, the subtree
  4379. heading be marked DONE, and the current time will be added.
  4380. When called with prefix argument FIND-DONE, find whole trees without any
  4381. open TODO items and archive them (after getting confirmation from the user).
  4382. If the cursor is not at a headline when this comand is called, try all level
  4383. 1 trees. If the cursor is on a headline, only try the direct children of
  4384. this heading. "
  4385. (interactive "P")
  4386. (if find-done
  4387. (org-archive-all-done)
  4388. ;; Save all relevant TODO keyword-relatex variables
  4389. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4390. (tr-org-todo-keywords org-todo-keywords)
  4391. (tr-org-todo-interpretation org-todo-interpretation)
  4392. (tr-org-done-string org-done-string)
  4393. (tr-org-todo-regexp org-todo-regexp)
  4394. (tr-org-todo-line-regexp org-todo-line-regexp)
  4395. (this-buffer (current-buffer))
  4396. file heading buffer level newfile-p)
  4397. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4398. (progn
  4399. (setq file (format (match-string 1 org-archive-location)
  4400. (file-name-nondirectory buffer-file-name))
  4401. heading (match-string 2 org-archive-location)))
  4402. (error "Invalid `org-archive-location'"))
  4403. (if (> (length file) 0)
  4404. (setq newfile-p (not (file-exists-p file))
  4405. buffer (find-file-noselect file))
  4406. (setq buffer (current-buffer)))
  4407. (unless buffer
  4408. (error "Cannot access file \"%s\"" file))
  4409. (if (and (> (length heading) 0)
  4410. (string-match "^\\*+" heading))
  4411. (setq level (match-end 0))
  4412. (setq heading nil level 0))
  4413. (save-excursion
  4414. ;; We first only copy, in case something goes wrong
  4415. ;; we need to protect this-command, to avoid kill-region sets it,
  4416. ;; which would lead to duplication of subtrees
  4417. (let (this-command) (org-copy-subtree))
  4418. (set-buffer buffer)
  4419. ;; Enforce org-mode for the archive buffer
  4420. (if (not (org-mode-p))
  4421. ;; Force the mode for future visits.
  4422. (let ((org-insert-mode-line-in-empty-file t))
  4423. (call-interactively 'org-mode)))
  4424. (when newfile-p
  4425. (goto-char (point-max))
  4426. (insert (format "\nArchived entries from file %s\n\n"
  4427. (buffer-file-name this-buffer))))
  4428. ;; Force the TODO keywords of the original buffer
  4429. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4430. (org-todo-keywords tr-org-todo-keywords)
  4431. (org-todo-interpretation tr-org-todo-interpretation)
  4432. (org-done-string tr-org-done-string)
  4433. (org-todo-regexp tr-org-todo-regexp)
  4434. (org-todo-line-regexp tr-org-todo-line-regexp))
  4435. (goto-char (point-min))
  4436. (if heading
  4437. (progn
  4438. (if (re-search-forward
  4439. (concat "\\(^\\|\r\\)"
  4440. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4441. nil t)
  4442. (goto-char (match-end 0))
  4443. ;; Heading not found, just insert it at the end
  4444. (goto-char (point-max))
  4445. (or (bolp) (insert "\n"))
  4446. (insert "\n" heading "\n")
  4447. (end-of-line 0))
  4448. ;; Make the subtree visible
  4449. (show-subtree)
  4450. (org-end-of-subtree t)
  4451. (skip-chars-backward " \t\r\n")
  4452. (and (looking-at "[ \t\r\n]*")
  4453. (replace-match "\n\n")))
  4454. ;; No specific heading, just go to end of file.
  4455. (goto-char (point-max)) (insert "\n"))
  4456. ;; Paste
  4457. (org-paste-subtree (1+ level))
  4458. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4459. (if org-archive-mark-done
  4460. (org-todo (length org-todo-keywords)))
  4461. ;; Move cursor to right after the TODO keyword
  4462. (when org-archive-stamp-time
  4463. (beginning-of-line 1)
  4464. (looking-at org-todo-line-regexp)
  4465. (goto-char (or (match-end 2) (match-beginning 3)))
  4466. (org-insert-time-stamp (org-current-time) t t "(" ")"))
  4467. ;; Save the buffer, if it is not the same buffer.
  4468. (if (not (eq this-buffer buffer)) (save-buffer))))
  4469. ;; Here we are back in the original buffer. Everything seems to have
  4470. ;; worked. So now cut the tree and finish up.
  4471. (let (this-command) (org-cut-subtree))
  4472. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4473. (message "Subtree archived %s"
  4474. (if (eq this-buffer buffer)
  4475. (concat "under heading: " heading)
  4476. (concat "in file: " (abbreviate-file-name file)))))))
  4477. (defun org-archive-all-done (&optional tag)
  4478. "Archive sublevels of the current tree without open TODO items.
  4479. If the cursor is not on a headline, try all level 1 trees. If
  4480. it is on a headline, try all direct children.
  4481. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4482. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4483. (rea (concat ".*:" org-archive-tag ":"))
  4484. (begm (make-marker))
  4485. (endm (make-marker))
  4486. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4487. "Move subtree to archive (no open TODO items)? "))
  4488. beg end (cntarch 0))
  4489. (if (org-on-heading-p)
  4490. (progn
  4491. (setq re1 (concat "^" (regexp-quote
  4492. (make-string
  4493. (1+ (- (match-end 0) (match-beginning 0)))
  4494. ?*))
  4495. " "))
  4496. (move-marker begm (point))
  4497. (move-marker endm (org-end-of-subtree t)))
  4498. (setq re1 "^* ")
  4499. (move-marker begm (point-min))
  4500. (move-marker endm (point-max)))
  4501. (save-excursion
  4502. (goto-char begm)
  4503. (while (re-search-forward re1 endm t)
  4504. (setq beg (match-beginning 0)
  4505. end (save-excursion (org-end-of-subtree t) (point)))
  4506. (goto-char beg)
  4507. (if (re-search-forward re end t)
  4508. (goto-char end)
  4509. (goto-char beg)
  4510. (if (and (or (not tag) (not (looking-at rea)))
  4511. (y-or-n-p question))
  4512. (progn
  4513. (if tag
  4514. (org-toggle-tag org-archive-tag 'on)
  4515. (org-archive-subtree))
  4516. (setq cntarch (1+ cntarch)))
  4517. (goto-char end)))))
  4518. (message "%d trees archived" cntarch)))
  4519. (defun org-cycle-hide-archived-subtrees (state)
  4520. "Re-hide all archived subtrees after a visibility state change."
  4521. (when (and (not org-cycle-open-archived-trees)
  4522. (not (memq state '(overview folded))))
  4523. (save-excursion
  4524. (let* ((globalp (memq state '(contents all)))
  4525. (beg (if globalp (point-min) (point)))
  4526. (end (if globalp (point-max) (org-end-of-subtree t))))
  4527. (org-hide-archived-subtrees beg end)
  4528. (goto-char beg)
  4529. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4530. (message (substitute-command-keys
  4531. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4532. (defun org-force-cycle-archived ()
  4533. "Cycle subtree even if it is archived."
  4534. (interactive)
  4535. (setq this-command 'org-cycle)
  4536. (let ((org-cycle-open-archived-trees t))
  4537. (call-interactively 'org-cycle)))
  4538. (defun org-hide-archived-subtrees (beg end)
  4539. "Re-hide all archived subtrees after a visibility state change."
  4540. (save-excursion
  4541. (let* ((re (concat ":" org-archive-tag ":")))
  4542. (goto-char beg)
  4543. (while (re-search-forward re end t)
  4544. (and (org-on-heading-p) (hide-subtree))
  4545. (org-end-of-subtree t)))))
  4546. (defun org-toggle-tag (tag &optional onoff)
  4547. "Toggle the tag TAG for the current line.
  4548. If ONOFF is `on' or `off', don't toggle but set to this state."
  4549. (unless (org-on-heading-p) (error "Not on headling"))
  4550. (let (res current)
  4551. (save-excursion
  4552. (beginning-of-line)
  4553. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4554. (point-at-eol) t)
  4555. (progn
  4556. (setq current (match-string 1))
  4557. (replace-match ""))
  4558. (setq current ""))
  4559. (setq current (nreverse (org-split-string current ":")))
  4560. (cond
  4561. ((eq onoff 'on)
  4562. (setq res t)
  4563. (or (member tag current) (push tag current)))
  4564. ((eq onoff 'off)
  4565. (or (not (member tag current)) (setq current (delete tag current))))
  4566. (t (if (member tag current)
  4567. (setq current (delete tag current))
  4568. (setq res t)
  4569. (push tag current))))
  4570. (end-of-line 1)
  4571. (when current
  4572. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4573. (org-set-tags nil t))
  4574. res))
  4575. (defun org-toggle-archive-tag (&optional arg)
  4576. "Toggle the archive tag for the current headline.
  4577. With prefix ARG, check all children of current headline and offer tagging
  4578. the children that do not contain any open TODO items."
  4579. (interactive "P")
  4580. (if arg
  4581. (org-archive-all-done 'tag)
  4582. (let (set)
  4583. (save-excursion
  4584. (org-back-to-heading t)
  4585. (setq set (org-toggle-tag org-archive-tag))
  4586. (when set (hide-subtree)))
  4587. (and set (beginning-of-line 1))
  4588. (message "Subtree %s" (if set "archived" "unarchived")))))
  4589. (defvar org-agenda-multi nil) ; dynammically scoped
  4590. (defvar org-agenda-buffer-name "*Org Agenda*")
  4591. (defvar org-pre-agenda-window-conf nil)
  4592. (defun org-prepare-agenda ()
  4593. (if org-agenda-multi
  4594. (progn
  4595. (setq buffer-read-only nil)
  4596. (goto-char (point-max))
  4597. (unless (= (point) 1)
  4598. (insert "\n" (make-string (window-width) ?=) "\n"))
  4599. (narrow-to-region (point) (point-max)))
  4600. (org-agenda-maybe-reset-markers 'force)
  4601. (org-prepare-agenda-buffers (org-agenda-files))
  4602. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  4603. (awin (get-buffer-window abuf)))
  4604. (cond
  4605. ((equal (current-buffer) abuf) nil)
  4606. (awin (select-window awin))
  4607. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  4608. ((equal org-agenda-window-setup 'current-window)
  4609. (switch-to-buffer abuf))
  4610. ((equal org-agenda-window-setup 'other-window)
  4611. (switch-to-buffer-other-window abuf))
  4612. ((equal org-agenda-window-setup 'other-frame)
  4613. (switch-to-buffer-other-frame abuf))
  4614. ((equal org-agenda-window-setup 'reorganize-frame)
  4615. (delete-other-windows)
  4616. (switch-to-buffer-other-window abuf))))
  4617. (setq buffer-read-only nil)
  4618. (erase-buffer)
  4619. (org-agenda-mode))
  4620. (setq buffer-read-only nil))
  4621. (defun org-finalize-agenda ()
  4622. "Finishing touch for the agenda buffer, called just before displaying it."
  4623. (unless org-agenda-multi
  4624. (org-agenda-align-tags)
  4625. (save-excursion
  4626. (let ((buffer-read-only))
  4627. (goto-char (point-min))
  4628. (while (org-activate-bracket-links (point-max))
  4629. (add-text-properties (match-beginning 0) (match-end 0)
  4630. '(face org-link))))
  4631. (run-hooks 'org-finalize-agenda-hook))))
  4632. (defun org-prepare-agenda-buffers (files)
  4633. "Create buffers for all agenda files, protect archived trees and comments."
  4634. (interactive)
  4635. (let ((pa '(:org-archived t))
  4636. (pc '(:org-comment t))
  4637. (pall '(:org-archived t :org-comment t))
  4638. (rea (concat ":" org-archive-tag ":"))
  4639. bmp file re)
  4640. (save-excursion
  4641. (while (setq file (pop files))
  4642. (org-check-agenda-file file)
  4643. (set-buffer (org-get-agenda-file-buffer file))
  4644. (widen)
  4645. (setq bmp (buffer-modified-p))
  4646. (save-excursion
  4647. (remove-text-properties (point-min) (point-max) pall)
  4648. (when org-agenda-skip-archived-trees
  4649. (goto-char (point-min))
  4650. (while (re-search-forward rea nil t)
  4651. (if (org-on-heading-p)
  4652. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4653. (goto-char (point-min))
  4654. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4655. (while (re-search-forward re nil t)
  4656. (add-text-properties
  4657. (match-beginning 0) (org-end-of-subtree t) pc)))
  4658. (set-buffer-modified-p bmp)))))
  4659. (defun org-agenda-skip ()
  4660. "Throw to `:skip' in places that should be skipped."
  4661. (let ((p (point-at-bol)))
  4662. (and org-agenda-skip-archived-trees
  4663. (get-text-property p :org-archived)
  4664. (org-end-of-subtree t)
  4665. (throw :skip t))
  4666. (and (get-text-property p :org-comment)
  4667. (org-end-of-subtree t)
  4668. (throw :skip t))
  4669. (if (equal (char-after p) ?#) (throw :skip t))))
  4670. (defun org-agenda-toggle-archive-tag ()
  4671. "Toggle the archive tag for the current entry."
  4672. (interactive)
  4673. (org-agenda-check-no-diary)
  4674. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4675. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4676. (org-agenda-error)))
  4677. (buffer (marker-buffer hdmarker))
  4678. (pos (marker-position hdmarker))
  4679. (buffer-read-only nil)
  4680. newhead)
  4681. (with-current-buffer buffer
  4682. (widen)
  4683. (goto-char pos)
  4684. (org-show-context 'agenda)
  4685. (save-excursion
  4686. (and (outline-next-heading)
  4687. (org-flag-heading nil))) ; show the next heading
  4688. (call-interactively 'org-toggle-archive-tag)
  4689. (end-of-line 1)
  4690. (setq newhead (org-get-heading)))
  4691. (org-agenda-change-all-lines newhead hdmarker)
  4692. (beginning-of-line 1)))
  4693. ;;; Dynamic blocks
  4694. (defun org-find-dblock (name)
  4695. "Find the first dynamic block with name NAME in the buffer.
  4696. If not found, stay at current position and return nil."
  4697. (let (pos)
  4698. (save-excursion
  4699. (goto-char (point-min))
  4700. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4701. nil t)
  4702. (match-beginning 0))))
  4703. (if pos (goto-char pos))
  4704. pos))
  4705. (defconst org-dblock-start-re
  4706. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4707. "Matches the startline of a dynamic block, with parameters.")
  4708. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4709. "Matches the end of a dyhamic block.")
  4710. (defun org-create-dblock (plist)
  4711. "Create a dynamic block section, with parameters taken from PLIST.
  4712. PLIST must containe a :name entry which is used as name of the block."
  4713. (unless (bolp) (newline))
  4714. (let ((name (plist-get plist :name)))
  4715. (insert "#+BEGIN: " name)
  4716. (while plist
  4717. (if (eq (car plist) :name)
  4718. (setq plist (cddr plist))
  4719. (insert " " (prin1-to-string (pop plist)))))
  4720. (insert "\n\n#+END:\n")
  4721. (beginning-of-line -2)))
  4722. (defun org-prepare-dblock ()
  4723. "Prepare dynamic block for refresh.
  4724. This empties the block, puts the cursor at the insert position and returns
  4725. the property list including an extra property :name with the block name."
  4726. (unless (looking-at org-dblock-start-re)
  4727. (error "Not at a dynamic block"))
  4728. (let* ((begdel (1+ (match-end 0)))
  4729. (name (match-string 1))
  4730. (params (append (list :name name)
  4731. (read (concat "(" (match-string 3) ")")))))
  4732. (unless (re-search-forward org-dblock-end-re nil t)
  4733. (error "Dynamic block not terminated"))
  4734. (delete-region begdel (match-beginning 0))
  4735. (goto-char begdel)
  4736. (open-line 1)
  4737. params))
  4738. (defun org-map-dblocks (&optional command)
  4739. "Apply COMMAND to all dynamic blocks in the current buffer.
  4740. If COMMAND is not given, use `org-update-dblock'."
  4741. (let ((cmd (or command 'org-update-dblock))
  4742. pos)
  4743. (save-excursion
  4744. (goto-char (point-min))
  4745. (while (re-search-forward org-dblock-start-re nil t)
  4746. (goto-char (setq pos (match-beginning 0)))
  4747. (condition-case nil
  4748. (funcall cmd)
  4749. (error (message "Error during update of dynamic block")))
  4750. (goto-char pos)
  4751. (unless (re-search-forward org-dblock-end-re nil t)
  4752. (error "Dynamic block not terminated"))))))
  4753. (defun org-dblock-update (&optional arg)
  4754. "User command for updating dynamic blocks.
  4755. Update the dynamic block at point. With prefix ARG, update all dynamic
  4756. blocks in the buffer."
  4757. (interactive "P")
  4758. (if arg
  4759. (org-update-all-dblocks)
  4760. (or (looking-at org-dblock-start-re)
  4761. (org-beginning-of-dblock))
  4762. (org-update-dblock)))
  4763. (defun org-update-dblock ()
  4764. "Update the dynamic block at point
  4765. This means to empty the block, parse for parameters and then call
  4766. the correct writing function."
  4767. (let* ((pos (point))
  4768. (params (org-prepare-dblock))
  4769. (name (plist-get params :name))
  4770. (cmd (intern (concat "org-dblock-write:" name))))
  4771. (funcall cmd params)
  4772. (goto-char pos)))
  4773. (defun org-beginning-of-dblock ()
  4774. "Find the beginning of the dynamic block at point.
  4775. Error if there is no scuh block at point."
  4776. (let ((pos (point))
  4777. beg)
  4778. (end-of-line 1)
  4779. (if (and (re-search-backward org-dblock-start-re nil t)
  4780. (setq beg (match-beginning 0))
  4781. (re-search-forward org-dblock-end-re nil t)
  4782. (> (match-end 0) pos))
  4783. (goto-char beg)
  4784. (goto-char pos)
  4785. (error "Not in a dynamic block"))))
  4786. (defun org-update-all-dblocks ()
  4787. "Update all dynamic blocks in the buffer.
  4788. This function can be used in a hook."
  4789. (when (org-mode-p)
  4790. (org-map-dblocks 'org-update-dblock)))
  4791. ;;; Completion
  4792. (defun org-complete (&optional arg)
  4793. "Perform completion on word at point.
  4794. At the beginning of a headline, this completes TODO keywords as given in
  4795. `org-todo-keywords'.
  4796. If the current word is preceded by a backslash, completes the TeX symbols
  4797. that are supported for HTML support.
  4798. If the current word is preceded by \"#+\", completes special words for
  4799. setting file options.
  4800. In the line after \"#+STARTUP:, complete valid keywords.\"
  4801. At all other locations, this simply calls `ispell-complete-word'."
  4802. (interactive "P")
  4803. (catch 'exit
  4804. (let* ((end (point))
  4805. (beg1 (save-excursion
  4806. (skip-chars-backward "a-zA-Z_@0-9")
  4807. (point)))
  4808. (beg (save-excursion
  4809. (skip-chars-backward "a-zA-Z0-9_:$")
  4810. (point)))
  4811. (confirm (lambda (x) (stringp (car x))))
  4812. (camel (equal (char-before beg) ?*))
  4813. (tag (equal (char-before beg1) ?:))
  4814. (texp (equal (char-before beg) ?\\))
  4815. (link (equal (char-before beg) ?\[))
  4816. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4817. beg)
  4818. "#+"))
  4819. (startup (string-match "^#\\+STARTUP:.*"
  4820. (buffer-substring (point-at-bol) (point))))
  4821. (completion-ignore-case opt)
  4822. (type nil)
  4823. (tbl nil)
  4824. (table (cond
  4825. (opt
  4826. (setq type :opt)
  4827. (mapcar (lambda (x)
  4828. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4829. (cons (match-string 2 x) (match-string 1 x)))
  4830. (org-split-string (org-get-current-options) "\n")))
  4831. (startup
  4832. (setq type :startup)
  4833. org-startup-options)
  4834. (link (append org-link-abbrev-alist-local
  4835. org-link-abbrev-alist))
  4836. (texp
  4837. (setq type :tex)
  4838. org-html-entities)
  4839. ((string-match "\\`\\*+[ \t]*\\'"
  4840. (buffer-substring (point-at-bol) beg))
  4841. (setq type :todo)
  4842. (mapcar 'list org-todo-keywords))
  4843. (camel
  4844. (setq type :camel)
  4845. (save-excursion
  4846. (goto-char (point-min))
  4847. (while (re-search-forward org-todo-line-regexp nil t)
  4848. (push (list
  4849. (if org-file-link-context-use-camel-case
  4850. (org-make-org-heading-camel (match-string 3) t)
  4851. (org-make-org-heading-search-string
  4852. (match-string 3) t)))
  4853. tbl)))
  4854. tbl)
  4855. (tag (setq type :tag beg beg1)
  4856. (or org-tag-alist (org-get-buffer-tags)))
  4857. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4858. (pattern (buffer-substring-no-properties beg end))
  4859. (completion (try-completion pattern table confirm)))
  4860. (cond ((eq completion t)
  4861. (if (equal type :opt)
  4862. (insert (substring (cdr (assoc (upcase pattern) table))
  4863. (length pattern)))))
  4864. ((null completion)
  4865. (message "Can't find completion for \"%s\"" pattern)
  4866. (ding))
  4867. ((not (string= pattern completion))
  4868. (delete-region beg end)
  4869. (if (string-match " +$" completion)
  4870. (setq completion (replace-match "" t t completion)))
  4871. (insert completion)
  4872. (if (get-buffer-window "*Completions*")
  4873. (delete-window (get-buffer-window "*Completions*")))
  4874. (if (assoc completion table)
  4875. (if (eq type :todo) (insert " ")
  4876. (if (eq type :tag) (insert ":"))))
  4877. (if (and (equal type :opt) (assoc completion table))
  4878. (message "%s" (substitute-command-keys
  4879. "Press \\[org-complete] again to insert example settings"))))
  4880. (t
  4881. (message "Making completion list...")
  4882. (let ((list (sort (all-completions pattern table confirm)
  4883. 'string<)))
  4884. (with-output-to-temp-buffer "*Completions*"
  4885. (condition-case nil
  4886. ;; Protection needed for XEmacs and emacs 21
  4887. (display-completion-list list pattern)
  4888. (error (display-completion-list list)))))
  4889. (message "Making completion list...%s" "done"))))))
  4890. ;;; Comments, TODO and DEADLINE
  4891. (defun org-toggle-comment ()
  4892. "Change the COMMENT state of an entry."
  4893. (interactive)
  4894. (save-excursion
  4895. (org-back-to-heading)
  4896. (if (looking-at (concat outline-regexp
  4897. "\\( +\\<" org-comment-string "\\>\\)"))
  4898. (replace-match "" t t nil 1)
  4899. (if (looking-at outline-regexp)
  4900. (progn
  4901. (goto-char (match-end 0))
  4902. (insert " " org-comment-string))))))
  4903. (defvar org-last-todo-state-is-todo nil
  4904. "This is non-nil when the last TODO state change led to a TODO state.
  4905. If the last change removed the TODO tag or switched to DONE, then
  4906. this is nil.")
  4907. (defun org-todo (&optional arg)
  4908. "Change the TODO state of an item.
  4909. The state of an item is given by a keyword at the start of the heading,
  4910. like
  4911. *** TODO Write paper
  4912. *** DONE Call mom
  4913. The different keywords are specified in the variable `org-todo-keywords'.
  4914. By default the available states are \"TODO\" and \"DONE\".
  4915. So for this example: when the item starts with TODO, it is changed to DONE.
  4916. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4917. DONE are present, add TODO at the beginning of the heading.
  4918. With prefix arg, use completion to determine the new state. With numeric
  4919. prefix arg, switch to that state."
  4920. (interactive "P")
  4921. (save-excursion
  4922. (org-back-to-heading)
  4923. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4924. (or (looking-at (concat " +" org-todo-regexp " *"))
  4925. (looking-at " *"))
  4926. (let* ((this (match-string 1))
  4927. (completion-ignore-case t)
  4928. (member (member this org-todo-keywords))
  4929. (tail (cdr member))
  4930. (state (cond
  4931. ((equal arg '(4))
  4932. ;; Read a state with completion
  4933. (completing-read "State: " (mapcar (lambda(x) (list x))
  4934. org-todo-keywords)
  4935. nil t))
  4936. ((eq arg 'right)
  4937. (if this
  4938. (if tail (car tail) nil)
  4939. (car org-todo-keywords)))
  4940. ((eq arg 'left)
  4941. (if (equal member org-todo-keywords)
  4942. nil
  4943. (if this
  4944. (nth (- (length org-todo-keywords) (length tail) 2)
  4945. org-todo-keywords)
  4946. org-done-string)))
  4947. (arg
  4948. ;; user requests a specific state
  4949. (nth (1- (prefix-numeric-value arg))
  4950. org-todo-keywords))
  4951. ((null member) (car org-todo-keywords))
  4952. ((null tail) nil) ;; -> first entry
  4953. ((eq org-todo-interpretation 'sequence)
  4954. (car tail))
  4955. ((memq org-todo-interpretation '(type priority))
  4956. (if (eq this-command last-command)
  4957. (car tail)
  4958. (if (> (length tail) 0) org-done-string nil)))
  4959. (t nil)))
  4960. (next (if state (concat " " state " ") " ")))
  4961. (replace-match next t t)
  4962. (setq org-last-todo-state-is-todo
  4963. (not (equal state org-done-string)))
  4964. (when org-log-done
  4965. (if (equal state org-done-string)
  4966. (org-add-planning-info 'closed (org-current-time) 'scheduled)
  4967. (if (not this)
  4968. (org-add-planning-info nil nil 'closed))))
  4969. ;; Fixup tag positioning
  4970. (and org-auto-align-tags (org-set-tags nil t))
  4971. (run-hooks 'org-after-todo-state-change-hook)))
  4972. ;; Fixup cursor location if close to the keyword
  4973. (if (and (outline-on-heading-p)
  4974. (not (bolp))
  4975. (save-excursion (beginning-of-line 1)
  4976. (looking-at org-todo-line-regexp))
  4977. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4978. (progn
  4979. (goto-char (or (match-end 2) (match-end 1)))
  4980. (just-one-space))))
  4981. (defun org-show-todo-tree (arg)
  4982. "Make a compact tree which shows all headlines marked with TODO.
  4983. The tree will show the lines where the regexp matches, and all higher
  4984. headlines above the match.
  4985. With \\[universal-argument] prefix, also show the DONE entries.
  4986. With a numeric prefix N, construct a sparse tree for the Nth element
  4987. of `org-todo-keywords'."
  4988. (interactive "P")
  4989. (let ((case-fold-search nil)
  4990. (kwd-re
  4991. (cond ((null arg) org-not-done-regexp)
  4992. ((equal arg '(4)) org-todo-regexp)
  4993. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4994. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4995. org-todo-keywords)))
  4996. (t (error "Invalid prefix argument: %s" arg)))))
  4997. (message "%d TODO entries found"
  4998. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4999. (defun org-deadline ()
  5000. "Insert the DEADLINE: string to make a deadline.
  5001. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  5002. to modify it to the correct date."
  5003. (interactive)
  5004. (org-add-planning-info 'deadline nil 'closed))
  5005. (defun org-schedule ()
  5006. "Insert the SCHEDULED: string to schedule a TODO item.
  5007. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  5008. to modify it to the correct date."
  5009. (interactive)
  5010. (org-add-planning-info 'scheduled nil 'closed))
  5011. (defun org-add-planning-info (what &optional time &rest remove)
  5012. "Insert new timestamp with keyword in the line directly after the headline.
  5013. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  5014. If non is given, the user is prompted for a date.
  5015. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  5016. be removed."
  5017. (interactive)
  5018. (when what (setq time (or time (org-read-date nil 'to-time))))
  5019. (when (and org-insert-labeled-timestamps-at-point
  5020. (member what '(scheduled deadline)))
  5021. (insert
  5022. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  5023. (org-insert-time-stamp time)
  5024. (setq what nil))
  5025. (save-excursion
  5026. (save-restriction
  5027. (let (col list elt ts buffer-invisibility-spec)
  5028. (org-back-to-heading t)
  5029. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  5030. (goto-char (match-end 1))
  5031. (setq col (current-column))
  5032. (goto-char (1+ (match-end 0)))
  5033. (if (and (not (looking-at outline-regexp))
  5034. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  5035. "[^\r\n]*"))
  5036. (not (equal (match-string 1) org-clock-string)))
  5037. (narrow-to-region (match-beginning 0) (match-end 0))
  5038. (insert "\n")
  5039. (backward-char 1)
  5040. (narrow-to-region (point) (point))
  5041. (indent-to-column col))
  5042. ;; Check if we have to remove something.
  5043. (setq list (cons what remove))
  5044. (while list
  5045. (setq elt (pop list))
  5046. (goto-char (point-min))
  5047. (when (or (and (eq elt 'scheduled)
  5048. (re-search-forward org-scheduled-time-regexp nil t))
  5049. (and (eq elt 'deadline)
  5050. (re-search-forward org-deadline-time-regexp nil t))
  5051. (and (eq elt 'closed)
  5052. (re-search-forward org-closed-time-regexp nil t)))
  5053. (replace-match "")
  5054. (if (looking-at " +") (replace-match ""))))
  5055. (goto-char (point-max))
  5056. (when what
  5057. (insert
  5058. (if (not (equal (char-before) ?\ )) " " "")
  5059. (cond ((eq what 'scheduled) org-scheduled-string)
  5060. ((eq what 'deadline) org-deadline-string)
  5061. ((eq what 'closed) org-closed-string))
  5062. " ")
  5063. (org-insert-time-stamp time nil (eq what 'closed))
  5064. (end-of-line 1)
  5065. (org-add-log-maybe 'done))
  5066. (goto-char (point-min))
  5067. (widen)
  5068. (if (looking-at "[ \t]+\r?\n")
  5069. (replace-match ""))
  5070. ts))))
  5071. (defvar org-log-note-marker (make-marker))
  5072. (defvar org-log-note-purpose nil)
  5073. (defvar org-log-note-window-configuration nil)
  5074. (defun org-add-log-maybe (&optional purpose)
  5075. (when (and (listp org-log-done)
  5076. (memq purpose org-log-done))
  5077. (move-marker org-log-note-marker (point))
  5078. (setq org-log-note-purpose purpose)
  5079. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  5080. (defun org-add-log-note (&optional purpose)
  5081. "Pop up a window for taking a note, and add this note later at point."
  5082. (remove-hook 'post-command-hook 'org-add-log-note)
  5083. (setq org-log-note-window-configuration (current-window-configuration))
  5084. (delete-other-windows)
  5085. (switch-to-buffer (marker-buffer org-log-note-marker))
  5086. (goto-char org-log-note-marker)
  5087. (switch-to-buffer-other-window "*Org Note*")
  5088. (erase-buffer)
  5089. (org-mode)
  5090. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  5091. (cond
  5092. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  5093. ((eq org-log-note-purpose 'done) "closed todo item")
  5094. (t (error "This should not happen")))))
  5095. (org-set-local 'org-finish-function 'org-store-log-note))
  5096. (defun org-store-log-note ()
  5097. "Finish taking a log note, and insert it to where it belongs."
  5098. (let ((txt (buffer-string))
  5099. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  5100. lines ind)
  5101. (kill-buffer (current-buffer))
  5102. (if (string-match "^#.*\n[ \t\\n]*" txt)
  5103. (setq txt (replace-match "" t t txt)))
  5104. (when (string-match "\\S-" txt)
  5105. (if (string-match "\\s-+\\'" txt)
  5106. (setq txt (replace-match "" t t txt)))
  5107. (setq lines (org-split-string txt "\n"))
  5108. (and note (string-match "\\S-" note) (push note lines))
  5109. (save-excursion
  5110. (set-buffer (marker-buffer org-log-note-marker))
  5111. (save-excursion
  5112. (goto-char org-log-note-marker)
  5113. (if (not (bolp)) (newline))
  5114. (indent-relative t)
  5115. (setq ind (concat (buffer-substring (point-at-bol) (point)) " "))
  5116. (insert " - " (pop lines))
  5117. (while lines
  5118. (insert "\n" ind (pop lines))))))
  5119. (set-window-configuration org-log-note-window-configuration)))
  5120. (defvar org-occur-highlights nil)
  5121. (make-variable-buffer-local 'org-occur-highlights)
  5122. (defun org-occur (regexp &optional keep-previous callback)
  5123. "Make a compact tree which shows all matches of REGEXP.
  5124. The tree will show the lines where the regexp matches, and all higher
  5125. headlines above the match. It will also show the heading after the match,
  5126. to make sure editing the matching entry is easy.
  5127. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  5128. call to `org-occur' will be kept, to allow stacking of calls to this
  5129. command.
  5130. If CALLBACK is non-nil, it is a function which is called to confirm
  5131. that the match should indeed be shown."
  5132. (interactive "sRegexp: \nP")
  5133. (or keep-previous (org-remove-occur-highlights nil nil t))
  5134. (let ((cnt 0))
  5135. (save-excursion
  5136. (goto-char (point-min))
  5137. (if (or (not keep-previous) ; do not want to keep
  5138. (not org-occur-highlights)) ; no previous matches
  5139. ;; hide everything
  5140. (org-overview))
  5141. (while (re-search-forward regexp nil t)
  5142. (when (or (not callback)
  5143. (save-match-data (funcall callback)))
  5144. (setq cnt (1+ cnt))
  5145. (org-highlight-new-match (match-beginning 0) (match-end 0))
  5146. (org-show-context 'occur-tree))))
  5147. (when org-remove-highlights-with-change
  5148. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  5149. nil 'local))
  5150. (unless org-sparse-tree-open-archived-trees
  5151. (org-hide-archived-subtrees (point-min) (point-max)))
  5152. (run-hooks 'org-occur-hook)
  5153. (if (interactive-p)
  5154. (message "%d match(es) for regexp %s" cnt regexp))
  5155. cnt))
  5156. (defun org-show-context (&optional key siblings)
  5157. "Make sure point and context and visible.
  5158. How much context is shown depends upon the variables
  5159. `org-show-hierarchy-above' and `org-show-following-heading'.
  5160. When SIBLINGS is non-nil, show all siblings on each hierarchy level."
  5161. (let ((heading-p (org-on-heading-p t))
  5162. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  5163. (following-p (org-get-alist-option org-show-following-heading key)))
  5164. (catch 'exit
  5165. ;; Show heading or entry text
  5166. (if heading-p
  5167. (org-flag-heading nil) ; only show the heading
  5168. (and (or (org-invisible-p) (org-invisible-p2))
  5169. (org-show-hidden-entry))) ; show entire entry
  5170. (when following-p
  5171. ;; Show next sibling, or heading below text
  5172. (save-excursion
  5173. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  5174. (org-flag-heading nil))))
  5175. (when hierarchy-p
  5176. ;; show all higher headings, possibly with siblings
  5177. (save-excursion
  5178. (while (and (condition-case nil
  5179. (progn (org-up-heading-all 1) t)
  5180. (error nil))
  5181. (not (bobp)))
  5182. (org-flag-heading nil)
  5183. (when siblings
  5184. (save-excursion
  5185. (while (org-goto-sibling) (org-flag-heading nil)))
  5186. (save-excursion
  5187. (while (org-goto-sibling 'previous)
  5188. (org-flag-heading nil))))))))))
  5189. (defun org-reveal (&optional siblings)
  5190. "Show current entry, hierarchy above it, and the following headline.
  5191. This can be used to show a consistent set of context around locations
  5192. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  5193. not t for the search context.
  5194. With optional argument SIBLINGS, on each level of the hierarchy all
  5195. siblings are shown. This repairs the tree structure so what it would
  5196. look like when opend with successive calls to `org-cycle'."
  5197. (interactive "P")
  5198. (let ((org-show-hierarchy-above t)
  5199. (org-show-following-heading t))
  5200. (org-show-context nil siblings)))
  5201. ;; Overlay compatibility functions
  5202. (defun org-make-overlay (beg end &optional buffer)
  5203. (if (featurep 'xemacs)
  5204. (make-extent beg end buffer)
  5205. (make-overlay beg end buffer)))
  5206. (defun org-delete-overlay (ovl)
  5207. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  5208. (defun org-detatch-overlay (ovl)
  5209. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  5210. (defun org-move-overlay (ovl beg end &optional buffer)
  5211. (if (featurep 'xemacs)
  5212. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  5213. (move-overlay ovl beg end buffer)))
  5214. (defun org-overlay-put (ovl prop value)
  5215. (if (featurep 'xemacs)
  5216. (set-extent-property ovl prop value)
  5217. (overlay-put ovl prop value)))
  5218. (defun org-overlay-display (ovl text &optional face)
  5219. "Make overlay OVL display TEXT with face FACE."
  5220. (if (featurep 'xemacs)
  5221. (let ((gl (make-glyph text)))
  5222. (and face (set-glyph-face gl face))
  5223. (set-extent-property ovl 'invisible t)
  5224. (set-extent-property ovl 'end-glyph gl))
  5225. (overlay-put ovl 'display text)
  5226. (if face (overlay-put ovl 'face face))))
  5227. (defun org-overlay-get (ovl prop)
  5228. (if (featurep 'xemacs)
  5229. (extent-property ovl prop)
  5230. (overlay-get ovl prop)))
  5231. (defun org-overlays-at (pos)
  5232. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  5233. (defun org-overlays-in (&optional start end)
  5234. (if (featurep 'xemacs)
  5235. (extent-list nil start end)
  5236. (overlays-in start end)))
  5237. (defun org-overlay-start (o)
  5238. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  5239. (defun org-overlay-end (o)
  5240. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  5241. (defun org-highlight-new-match (beg end)
  5242. "Highlight from BEG to END and mark the highlight is an occur headline."
  5243. (let ((ov (org-make-overlay beg end)))
  5244. (org-overlay-put ov 'face 'secondary-selection)
  5245. (push ov org-occur-highlights)))
  5246. (defvar org-inhibit-highlight-removal nil)
  5247. (defun org-remove-occur-highlights (&optional beg end noremove)
  5248. "Remove the occur highlights from the buffer.
  5249. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5250. from the `before-change-functions' in the current buffer."
  5251. (interactive)
  5252. (unless org-inhibit-highlight-removal
  5253. (mapc 'org-delete-overlay org-occur-highlights)
  5254. (setq org-occur-highlights nil)
  5255. (unless noremove
  5256. (remove-hook 'before-change-functions
  5257. 'org-remove-occur-highlights 'local))))
  5258. ;;; Priorities
  5259. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  5260. "Regular expression matching the priority indicator.")
  5261. (defvar org-remove-priority-next-time nil)
  5262. (defun org-priority-up ()
  5263. "Increase the priority of the current item."
  5264. (interactive)
  5265. (org-priority 'up))
  5266. (defun org-priority-down ()
  5267. "Decrease the priority of the current item."
  5268. (interactive)
  5269. (org-priority 'down))
  5270. (defun org-priority (&optional action)
  5271. "Change the priority of an item by ARG.
  5272. ACTION can be set, up, or down."
  5273. (interactive)
  5274. (setq action (or action 'set))
  5275. (let (current new news have remove)
  5276. (save-excursion
  5277. (org-back-to-heading)
  5278. (if (looking-at org-priority-regexp)
  5279. (setq current (string-to-char (match-string 2))
  5280. have t)
  5281. (setq current org-default-priority))
  5282. (cond
  5283. ((eq action 'set)
  5284. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  5285. (setq new (read-char-exclusive))
  5286. (cond ((equal new ?\ ) (setq remove t))
  5287. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  5288. (error "Priority must be between `%c' and `%c'"
  5289. ?A org-lowest-priority))))
  5290. ((eq action 'up)
  5291. (setq new (1- current)))
  5292. ((eq action 'down)
  5293. (setq new (1+ current)))
  5294. (t (error "Invalid action")))
  5295. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  5296. (setq news (format "%c" new))
  5297. (if have
  5298. (if remove
  5299. (replace-match "" t t nil 1)
  5300. (replace-match news t t nil 2))
  5301. (if remove
  5302. (error "No priority cookie found in line")
  5303. (looking-at org-todo-line-regexp)
  5304. (if (match-end 2)
  5305. (progn
  5306. (goto-char (match-end 2))
  5307. (insert " [#" news "]"))
  5308. (goto-char (match-beginning 3))
  5309. (insert "[#" news "] ")))))
  5310. (if remove
  5311. (message "Priority removed")
  5312. (message "Priority of current item set to %s" news))))
  5313. (defun org-get-priority (s)
  5314. "Find priority cookie and return priority."
  5315. (save-match-data
  5316. (if (not (string-match org-priority-regexp s))
  5317. (* 1000 (- org-lowest-priority org-default-priority))
  5318. (* 1000 (- org-lowest-priority
  5319. (string-to-char (match-string 2 s)))))))
  5320. ;;; Timestamps
  5321. (defvar org-last-changed-timestamp nil)
  5322. (defun org-time-stamp (arg)
  5323. "Prompt for a date/time and insert a time stamp.
  5324. If the user specifies a time like HH:MM, or if this command is called
  5325. with a prefix argument, the time stamp will contain date and time.
  5326. Otherwise, only the date will be included. All parts of a date not
  5327. specified by the user will be filled in from the current date/time.
  5328. So if you press just return without typing anything, the time stamp
  5329. will represent the current date/time. If there is already a timestamp
  5330. at the cursor, it will be modified."
  5331. (interactive "P")
  5332. (let (org-time-was-given time)
  5333. (cond
  5334. ((and (org-at-timestamp-p)
  5335. (eq last-command 'org-time-stamp)
  5336. (eq this-command 'org-time-stamp))
  5337. (insert "--")
  5338. (setq time (let ((this-command this-command))
  5339. (org-read-date arg 'totime)))
  5340. (org-insert-time-stamp time (or org-time-was-given arg)))
  5341. ((org-at-timestamp-p)
  5342. (setq time (let ((this-command this-command))
  5343. (org-read-date arg 'totime)))
  5344. (when (org-at-timestamp-p) ; just to get the match data
  5345. (replace-match "")
  5346. (setq org-last-changed-timestamp
  5347. (org-insert-time-stamp time (or org-time-was-given arg))))
  5348. (message "Timestamp updated"))
  5349. (t
  5350. (setq time (let ((this-command this-command))
  5351. (org-read-date arg 'totime)))
  5352. (org-insert-time-stamp time (or org-time-was-given arg))))))
  5353. (defun org-time-stamp-inactive (&optional arg)
  5354. "Insert an inactive time stamp.
  5355. An inactive time stamp is enclosed in square brackets instead of angle
  5356. brackets. It is inactive in the sense that it does not trigger agenda entries,
  5357. does not link to the calendar and cannot be changed with the S-cursor keys.
  5358. So these are more for recording a certain time/date."
  5359. (interactive "P")
  5360. (let (org-time-was-given time)
  5361. (setq time (org-read-date arg 'totime))
  5362. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive)))
  5363. (defvar org-date-ovl (org-make-overlay 1 1))
  5364. (org-overlay-put org-date-ovl 'face 'org-warning)
  5365. (org-detatch-overlay org-date-ovl)
  5366. (defun org-read-date (&optional with-time to-time from-string)
  5367. "Read a date and make things smooth for the user.
  5368. The prompt will suggest to enter an ISO date, but you can also enter anything
  5369. which will at least partially be understood by `parse-time-string'.
  5370. Unrecognized parts of the date will default to the current day, month, year,
  5371. hour and minute. For example,
  5372. 3-2-5 --> 2003-02-05
  5373. feb 15 --> currentyear-02-15
  5374. sep 12 9 --> 2009-09-12
  5375. 12:45 --> today 12:45
  5376. 22 sept 0:34 --> currentyear-09-22 0:34
  5377. 12 --> currentyear-currentmonth-12
  5378. Fri --> nearest Friday (today or later)
  5379. etc.
  5380. The function understands only English month and weekday abbreviations,
  5381. but this can be configured with the variables `parse-time-months' and
  5382. `parse-time-weekdays'.
  5383. While prompting, a calendar is popped up - you can also select the
  5384. date with the mouse (button 1). The calendar shows a period of three
  5385. months. To scroll it to other months, use the keys `>' and `<'.
  5386. If you don't like the calendar, turn it off with
  5387. \(setq org-popup-calendar-for-date-prompt Nil)
  5388. With optional argument TO-TIME, the date will immediately be converted
  5389. to an internal time.
  5390. With an optional argument WITH-TIME, the prompt will suggest to also
  5391. insert a time. Note that when WITH-TIME is not set, you can still
  5392. enter a time, and this function will inform the calling routine about
  5393. this change. The calling routine may then choose to change the format
  5394. used to insert the time stamp into the buffer to include the time."
  5395. (require 'parse-time)
  5396. (let* ((org-time-stamp-rounding-minutes
  5397. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  5398. (ct (org-current-time))
  5399. (default-time
  5400. ;; Default time is either today, or, when entering a range,
  5401. ;; the range start.
  5402. (if (save-excursion
  5403. (re-search-backward
  5404. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  5405. (- (point) 20) t))
  5406. (apply
  5407. 'encode-time
  5408. (mapcar (lambda(x) (or x 0))
  5409. (parse-time-string (match-string 1))))
  5410. ct))
  5411. (calendar-move-hook nil)
  5412. (view-diary-entries-initially nil)
  5413. (view-calendar-holidays-initially nil)
  5414. (timestr (format-time-string
  5415. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  5416. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  5417. ans ans1 ans2
  5418. second minute hour day month year tl wday wday1)
  5419. (cond
  5420. (from-string (setq ans from-string))
  5421. (org-popup-calendar-for-date-prompt
  5422. (save-excursion
  5423. (save-window-excursion
  5424. (calendar)
  5425. (calendar-forward-day (- (time-to-days default-time)
  5426. (calendar-absolute-from-gregorian
  5427. (calendar-current-date))))
  5428. (org-eval-in-calendar nil)
  5429. (let* ((old-map (current-local-map))
  5430. (map (copy-keymap calendar-mode-map))
  5431. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  5432. (define-key map (kbd "RET") 'org-calendar-select)
  5433. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  5434. 'org-calendar-select-mouse)
  5435. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  5436. 'org-calendar-select-mouse)
  5437. (define-key minibuffer-local-map [(meta shift left)]
  5438. (lambda () (interactive)
  5439. (org-eval-in-calendar '(calendar-backward-month 1))))
  5440. (define-key minibuffer-local-map [(meta shift right)]
  5441. (lambda () (interactive)
  5442. (org-eval-in-calendar '(calendar-forward-month 1))))
  5443. (define-key minibuffer-local-map [(shift up)]
  5444. (lambda () (interactive)
  5445. (org-eval-in-calendar '(calendar-backward-week 1))))
  5446. (define-key minibuffer-local-map [(shift down)]
  5447. (lambda () (interactive)
  5448. (org-eval-in-calendar '(calendar-forward-week 1))))
  5449. (define-key minibuffer-local-map [(shift left)]
  5450. (lambda () (interactive)
  5451. (org-eval-in-calendar '(calendar-backward-day 1))))
  5452. (define-key minibuffer-local-map [(shift right)]
  5453. (lambda () (interactive)
  5454. (org-eval-in-calendar '(calendar-forward-day 1))))
  5455. (define-key minibuffer-local-map ">"
  5456. (lambda () (interactive)
  5457. (org-eval-in-calendar '(scroll-calendar-left 1))))
  5458. (define-key minibuffer-local-map "<"
  5459. (lambda () (interactive)
  5460. (org-eval-in-calendar '(scroll-calendar-right 1))))
  5461. (unwind-protect
  5462. (progn
  5463. (use-local-map map)
  5464. (setq ans (read-string prompt "" nil nil))
  5465. (if (not (string-match "\\S-" ans)) (setq ans nil))
  5466. (setq ans (or ans1 ans ans2)))
  5467. (use-local-map old-map))))))
  5468. (t ; Naked prompt only
  5469. (setq ans (read-string prompt "" nil timestr))))
  5470. (org-detatch-overlay org-date-ovl)
  5471. (if (string-match
  5472. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5473. (progn
  5474. (setq year (if (match-end 2)
  5475. (string-to-number (match-string 2 ans))
  5476. (string-to-number (format-time-string "%Y")))
  5477. month (string-to-number (match-string 3 ans))
  5478. day (string-to-number (match-string 4 ans)))
  5479. (if (< year 100) (setq year (+ 2000 year)))
  5480. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5481. t nil ans))))
  5482. (setq tl (parse-time-string ans)
  5483. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5484. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5485. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5486. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5487. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5488. second (or (nth 0 tl) 0)
  5489. wday (nth 6 tl))
  5490. (when (and wday (not (nth 3 tl)))
  5491. ;; Weekday was given, but no day, so pick that day in the week
  5492. ;; on or after the derived date.
  5493. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5494. (unless (equal wday wday1)
  5495. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5496. (if (and (boundp 'org-time-was-given)
  5497. (nth 2 tl))
  5498. (setq org-time-was-given t))
  5499. (if (< year 100) (setq year (+ 2000 year)))
  5500. (if to-time
  5501. (encode-time second minute hour day month year)
  5502. (if (or (nth 1 tl) (nth 2 tl))
  5503. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5504. (format "%04d-%02d-%02d" year month day)))))
  5505. (defun org-eval-in-calendar (form)
  5506. "Eval FORM in the calendar window and return to current window.
  5507. Also, store the cursor date in variable ans2."
  5508. (let ((sw (selected-window)))
  5509. (select-window (get-buffer-window "*Calendar*"))
  5510. (eval form)
  5511. (when (calendar-cursor-to-date)
  5512. (let* ((date (calendar-cursor-to-date))
  5513. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5514. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5515. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5516. (select-window sw)))
  5517. (defun org-calendar-select ()
  5518. "Return to `org-read-date' with the date currently selected.
  5519. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5520. (interactive)
  5521. (when (calendar-cursor-to-date)
  5522. (let* ((date (calendar-cursor-to-date))
  5523. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5524. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5525. (if (active-minibuffer-window) (exit-minibuffer))))
  5526. (defun org-insert-time-stamp (time &optional with-hm inactive pre post)
  5527. "Insert a date stamp for the date given by the internal TIME.
  5528. WITH-HM means, use the stamp format that includes the time of the day.
  5529. INACTIVE means use square brackets instead of angular ones, so that the
  5530. stamp will not contribute to the agenda.
  5531. PRE and POST are optional strings to be inserted before and after the
  5532. stamp.
  5533. The command returns the inserted time stamp."
  5534. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  5535. stamp)
  5536. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5537. (insert (or pre ""))
  5538. (insert (setq stamp (format-time-string fmt time)))
  5539. (insert (or post ""))
  5540. stamp))
  5541. (defun org-toggle-time-stamp-overlays ()
  5542. "Toggle the use of custom time stamp formats."
  5543. (interactive)
  5544. (setq org-display-custom-times (not org-display-custom-times))
  5545. (unless org-display-custom-times
  5546. (let ((p (point-min)) (bmp (buffer-modified-p)))
  5547. (while (setq p (next-single-property-change p 'display))
  5548. (if (and (get-text-property p 'display)
  5549. (eq (get-text-property p 'face) 'org-date))
  5550. (remove-text-properties
  5551. p (setq p (next-single-property-change p 'display))
  5552. '(display t))))
  5553. (set-buffer-modified-p bmp)))
  5554. (if (featurep 'xemacs)
  5555. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  5556. (org-restart-font-lock)
  5557. (setq org-table-may-need-update t)
  5558. (if org-display-custom-times
  5559. (message "Time stamps are overlayed with custom format")
  5560. (message "Time stamp overlays removed")))
  5561. (defun org-display-custom-time (beg end)
  5562. "Overlay modified time stamp format over timestamp between BED and END."
  5563. (let* ((t1 (save-match-data
  5564. (org-parse-time-string (buffer-substring beg end) t)))
  5565. (w1 (- end beg))
  5566. (with-hm (and (nth 1 t1) (nth 2 t1)))
  5567. (inactive (= (char-before (1- beg)) ?\[))
  5568. (tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats))
  5569. (time (mapcar (lambda (x) (or x 0)) t1))
  5570. (str (org-add-props
  5571. (format-time-string
  5572. (substring tf 1 -1) (apply 'encode-time time))
  5573. nil 'mouse-face 'highlight))
  5574. (w2 (length str)))
  5575. (if (not (= w2 w1))
  5576. (add-text-properties (1+ beg) (+ 2 beg)
  5577. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  5578. (if (featurep 'xemacs)
  5579. (progn
  5580. (put-text-property beg end 'invisible t)
  5581. (put-text-property beg end 'end-glyph (make-glyph str)))
  5582. (put-text-property beg end 'display str))))
  5583. (defun org-days-to-time (timestamp-string)
  5584. "Difference between TIMESTAMP-STRING and now in days."
  5585. (- (time-to-days (org-time-string-to-time timestamp-string))
  5586. (time-to-days (current-time))))
  5587. (defun org-deadline-close (timestamp-string &optional ndays)
  5588. "Is the time in TIMESTAMP-STRING close to the current date?"
  5589. (and (< (org-days-to-time timestamp-string)
  5590. (or ndays org-deadline-warning-days))
  5591. (not (org-entry-is-done-p))))
  5592. (defun org-calendar-select-mouse (ev)
  5593. "Return to `org-read-date' with the date currently selected.
  5594. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5595. (interactive "e")
  5596. (mouse-set-point ev)
  5597. (when (calendar-cursor-to-date)
  5598. (let* ((date (calendar-cursor-to-date))
  5599. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5600. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5601. (if (active-minibuffer-window) (exit-minibuffer))))
  5602. (defun org-check-deadlines (ndays)
  5603. "Check if there are any deadlines due or past due.
  5604. A deadline is considered due if it happens within `org-deadline-warning-days'
  5605. days from today's date. If the deadline appears in an entry marked DONE,
  5606. it is not shown. The prefix arg NDAYS can be used to test that many
  5607. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5608. (interactive "P")
  5609. (let* ((org-warn-days
  5610. (cond
  5611. ((equal ndays '(4)) 100000)
  5612. (ndays (prefix-numeric-value ndays))
  5613. (t org-deadline-warning-days)))
  5614. (case-fold-search nil)
  5615. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5616. (callback
  5617. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  5618. (message "%d deadlines past-due or due within %d days"
  5619. (org-occur regexp nil callback)
  5620. org-warn-days)))
  5621. (defun org-evaluate-time-range (&optional to-buffer)
  5622. "Evaluate a time range by computing the difference between start and end.
  5623. Normally the result is just printed in the echo area, but with prefix arg
  5624. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5625. If the time range is actually in a table, the result is inserted into the
  5626. next column.
  5627. For time difference computation, a year is assumed to be exactly 365
  5628. days in order to avoid rounding problems."
  5629. (interactive "P")
  5630. (or
  5631. (org-clock-update-time-maybe)
  5632. (save-excursion
  5633. (unless (org-at-date-range-p)
  5634. (goto-char (point-at-bol))
  5635. (re-search-forward org-tr-regexp (point-at-eol) t))
  5636. (if (not (org-at-date-range-p))
  5637. (error "Not at a time-stamp range, and none found in current line")))
  5638. (let* ((ts1 (match-string 1))
  5639. (ts2 (match-string 2))
  5640. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5641. (match-end (match-end 0))
  5642. (time1 (org-time-string-to-time ts1))
  5643. (time2 (org-time-string-to-time ts2))
  5644. (t1 (time-to-seconds time1))
  5645. (t2 (time-to-seconds time2))
  5646. (diff (abs (- t2 t1)))
  5647. (negative (< (- t2 t1) 0))
  5648. ;; (ys (floor (* 365 24 60 60)))
  5649. (ds (* 24 60 60))
  5650. (hs (* 60 60))
  5651. (fy "%dy %dd %02d:%02d")
  5652. (fy1 "%dy %dd")
  5653. (fd "%dd %02d:%02d")
  5654. (fd1 "%dd")
  5655. (fh "%02d:%02d")
  5656. y d h m align)
  5657. (if havetime
  5658. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5659. y 0
  5660. d (floor (/ diff ds)) diff (mod diff ds)
  5661. h (floor (/ diff hs)) diff (mod diff hs)
  5662. m (floor (/ diff 60)))
  5663. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5664. y 0
  5665. d (floor (+ (/ diff ds) 0.5))
  5666. h 0 m 0))
  5667. (if (not to-buffer)
  5668. (message (org-make-tdiff-string y d h m))
  5669. (when (org-at-table-p)
  5670. (goto-char match-end)
  5671. (setq align t)
  5672. (and (looking-at " *|") (goto-char (match-end 0))))
  5673. (if (looking-at
  5674. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5675. (replace-match ""))
  5676. (if negative (insert " -"))
  5677. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5678. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5679. (insert " " (format fh h m))))
  5680. (if align (org-table-align))
  5681. (message "Time difference inserted")))))
  5682. (defun org-make-tdiff-string (y d h m)
  5683. (let ((fmt "")
  5684. (l nil))
  5685. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5686. l (push y l)))
  5687. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5688. l (push d l)))
  5689. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5690. l (push h l)))
  5691. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5692. l (push m l)))
  5693. (apply 'format fmt (nreverse l))))
  5694. (defun org-time-string-to-time (s)
  5695. (apply 'encode-time (org-parse-time-string s)))
  5696. (defun org-parse-time-string (s &optional nodefault)
  5697. "Parse the standard Org-mode time string.
  5698. This should be a lot faster than the normal `parse-time-string'.
  5699. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5700. hour and minute fields will be nil if not given."
  5701. (if (string-match org-ts-regexp1 s)
  5702. (list 0
  5703. (if (or (match-beginning 8) (not nodefault))
  5704. (string-to-number (or (match-string 8 s) "0")))
  5705. (if (or (match-beginning 7) (not nodefault))
  5706. (string-to-number (or (match-string 7 s) "0")))
  5707. (string-to-number (match-string 4 s))
  5708. (string-to-number (match-string 3 s))
  5709. (string-to-number (match-string 2 s))
  5710. nil nil nil)
  5711. (make-list 9 0)))
  5712. (defun org-timestamp-up (&optional arg)
  5713. "Increase the date item at the cursor by one.
  5714. If the cursor is on the year, change the year. If it is on the month or
  5715. the day, change that.
  5716. With prefix ARG, change by that many units."
  5717. (interactive "p")
  5718. (org-timestamp-change (prefix-numeric-value arg)))
  5719. (defun org-timestamp-down (&optional arg)
  5720. "Decrease the date item at the cursor by one.
  5721. If the cursor is on the year, change the year. If it is on the month or
  5722. the day, change that.
  5723. With prefix ARG, change by that many units."
  5724. (interactive "p")
  5725. (org-timestamp-change (- (prefix-numeric-value arg))))
  5726. (defun org-timestamp-up-day (&optional arg)
  5727. "Increase the date in the time stamp by one day.
  5728. With prefix ARG, change that many days."
  5729. (interactive "p")
  5730. (if (and (not (org-at-timestamp-p t))
  5731. (org-on-heading-p))
  5732. (org-todo 'up)
  5733. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5734. (defun org-timestamp-down-day (&optional arg)
  5735. "Decrease the date in the time stamp by one day.
  5736. With prefix ARG, change that many days."
  5737. (interactive "p")
  5738. (if (and (not (org-at-timestamp-p t))
  5739. (org-on-heading-p))
  5740. (org-todo 'down)
  5741. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5742. (defsubst org-pos-in-match-range (pos n)
  5743. (and (match-beginning n)
  5744. (<= (match-beginning n) pos)
  5745. (>= (match-end n) pos)))
  5746. (defun org-at-timestamp-p (&optional inactive-ok)
  5747. "Determine if the cursor is in or at a timestamp."
  5748. (interactive)
  5749. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  5750. (pos (point))
  5751. (ans (or (looking-at tsr)
  5752. (save-excursion
  5753. (skip-chars-backward "^[<\n\r\t")
  5754. (if (> (point) 1) (backward-char 1))
  5755. (and (looking-at tsr)
  5756. (> (- (match-end 0) pos) -1))))))
  5757. (and (boundp 'org-ts-what)
  5758. (setq org-ts-what
  5759. (cond
  5760. ((org-pos-in-match-range pos 2) 'year)
  5761. ((org-pos-in-match-range pos 3) 'month)
  5762. ((org-pos-in-match-range pos 7) 'hour)
  5763. ((org-pos-in-match-range pos 8) 'minute)
  5764. ((or (org-pos-in-match-range pos 4)
  5765. (org-pos-in-match-range pos 5)) 'day)
  5766. (t 'day))))
  5767. ans))
  5768. (defun org-timestamp-change (n &optional what)
  5769. "Change the date in the time stamp at point.
  5770. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5771. `year', `minute', `second'. If WHAT is not given, the cursor position
  5772. in the timestamp determines what will be changed."
  5773. (let ((pos (point))
  5774. with-hm inactive
  5775. org-ts-what
  5776. ts time time0)
  5777. (if (not (org-at-timestamp-p t))
  5778. (error "Not at a timestamp"))
  5779. (if (and (not what) (not (eq org-ts-what 'day))
  5780. org-display-custom-times
  5781. (get-text-property (point) 'display)
  5782. (not (get-text-property (1- (point)) 'display)))
  5783. (setq org-ts-what 'day))
  5784. (setq org-ts-what (or what org-ts-what)
  5785. with-hm (<= (abs (- (cdr org-ts-lengths)
  5786. (- (match-end 0) (match-beginning 0))))
  5787. 1)
  5788. inactive (= (char-after (match-beginning 0)) ?\[)
  5789. ts (match-string 0))
  5790. (replace-match "")
  5791. (setq time0 (org-parse-time-string ts))
  5792. (setq time
  5793. (apply 'encode-time
  5794. (append
  5795. (list (or (car time0) 0))
  5796. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5797. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5798. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5799. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5800. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5801. (nthcdr 6 time0))))
  5802. (if (eq what 'calendar)
  5803. (let ((cal-date
  5804. (save-excursion
  5805. (save-match-data
  5806. (set-buffer "*Calendar*")
  5807. (calendar-cursor-to-date)))))
  5808. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5809. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5810. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5811. (setcar time0 (or (car time0) 0))
  5812. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5813. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5814. (setq time (apply 'encode-time time0))))
  5815. (org-insert-time-stamp time with-hm inactive)
  5816. (org-clock-update-time-maybe)
  5817. (goto-char pos)
  5818. ;; Try to recenter the calendar window, if any
  5819. (if (and org-calendar-follow-timestamp-change
  5820. (get-buffer-window "*Calendar*" t)
  5821. (memq org-ts-what '(day month year)))
  5822. (org-recenter-calendar (time-to-days time)))))
  5823. (defun org-recenter-calendar (date)
  5824. "If the calendar is visible, recenter it to DATE."
  5825. (let* ((win (selected-window))
  5826. (cwin (get-buffer-window "*Calendar*" t))
  5827. (calendar-move-hook nil))
  5828. (when cwin
  5829. (select-window cwin)
  5830. (calendar-goto-date (if (listp date) date
  5831. (calendar-gregorian-from-absolute date)))
  5832. (select-window win))))
  5833. (defun org-goto-calendar (&optional arg)
  5834. "Go to the Emacs calendar at the current date.
  5835. If there is a time stamp in the current line, go to that date.
  5836. A prefix ARG can be used to force the current date."
  5837. (interactive "P")
  5838. (let ((tsr org-ts-regexp) diff
  5839. (calendar-move-hook nil)
  5840. (view-calendar-holidays-initially nil)
  5841. (view-diary-entries-initially nil))
  5842. (if (or (org-at-timestamp-p)
  5843. (save-excursion
  5844. (beginning-of-line 1)
  5845. (looking-at (concat ".*" tsr))))
  5846. (let ((d1 (time-to-days (current-time)))
  5847. (d2 (time-to-days
  5848. (org-time-string-to-time (match-string 1)))))
  5849. (setq diff (- d2 d1))))
  5850. (calendar)
  5851. (calendar-goto-today)
  5852. (if (and diff (not arg)) (calendar-forward-day diff))))
  5853. (defun org-date-from-calendar ()
  5854. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  5855. If there is already a time stamp at the cursor position, update it."
  5856. (interactive)
  5857. (org-timestamp-change 0 'calendar))
  5858. ;;; The clock for measuring work time.
  5859. (defvar org-clock-marker (make-marker)
  5860. "Marker recording the last clock-in.")
  5861. (defun org-clock-in ()
  5862. "Start the clock on the current item.
  5863. If necessary, clock-out of the currently active clock."
  5864. (interactive)
  5865. (org-clock-out t)
  5866. (let (ts)
  5867. (save-excursion
  5868. (org-back-to-heading t)
  5869. (beginning-of-line 2)
  5870. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5871. (not (equal (match-string 1) org-clock-string)))
  5872. (beginning-of-line 1))
  5873. (insert "\n") (backward-char 1)
  5874. (indent-relative)
  5875. (insert org-clock-string " ")
  5876. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  5877. (move-marker org-clock-marker (point))
  5878. (message "Clock started at %s" ts))))
  5879. (defun org-clock-out (&optional fail-quietly)
  5880. "Stop the currently running clock.
  5881. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  5882. (interactive)
  5883. (catch 'exit
  5884. (if (not (marker-buffer org-clock-marker))
  5885. (if fail-quietly (throw 'exit t) (error "No active clock")))
  5886. (let (ts te s h m)
  5887. (save-excursion
  5888. (set-buffer (marker-buffer org-clock-marker))
  5889. (goto-char org-clock-marker)
  5890. (beginning-of-line 1)
  5891. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5892. (equal (match-string 1) org-clock-string))
  5893. (setq ts (match-string 2))
  5894. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  5895. (goto-char org-clock-marker)
  5896. (insert "--")
  5897. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  5898. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  5899. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  5900. h (floor (/ s 3600))
  5901. s (- s (* 3600 h))
  5902. m (floor (/ s 60))
  5903. s (- s (* 60 s)))
  5904. (insert " => " (format "%2d:%02d" h m))
  5905. (move-marker org-clock-marker nil)
  5906. (org-add-log-maybe 'clock-out)
  5907. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  5908. (defun org-clock-cancel ()
  5909. "Cancel the running clock be removing the start timestamp."
  5910. (interactive)
  5911. (if (not (marker-buffer org-clock-marker))
  5912. (error "No active clock"))
  5913. (save-excursion
  5914. (set-buffer (marker-buffer org-clock-marker))
  5915. (goto-char org-clock-marker)
  5916. (delete-region (1- (point-at-bol)) (point-at-eol)))
  5917. (message "Clock canceled"))
  5918. (defvar org-clock-file-total-minutes nil
  5919. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  5920. (make-variable-buffer-local 'org-clock-file-total-minutes)
  5921. (defun org-clock-sum (&optional tstart tend)
  5922. "Sum the times for each subtree.
  5923. Puts the resulting times in minutes as a text property on each headline."
  5924. (interactive)
  5925. (let* ((bmp (buffer-modified-p))
  5926. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  5927. org-clock-string
  5928. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  5929. (lmax 30)
  5930. (ltimes (make-vector lmax 0))
  5931. (t1 0)
  5932. (level 0)
  5933. ts te dt
  5934. time)
  5935. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  5936. (save-excursion
  5937. (goto-char (point-max))
  5938. (while (re-search-backward re nil t)
  5939. (if (match-end 2)
  5940. ;; A time
  5941. (setq ts (match-string 2)
  5942. te (match-string 3)
  5943. ts (time-to-seconds
  5944. (apply 'encode-time (org-parse-time-string ts)))
  5945. te (time-to-seconds
  5946. (apply 'encode-time (org-parse-time-string te)))
  5947. ts (if tstart (max ts tstart) ts)
  5948. te (if tend (min te tend) te)
  5949. dt (- te ts)
  5950. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  5951. ;; A headline
  5952. (setq level (- (match-end 1) (match-beginning 1)))
  5953. (when (or (> t1 0) (> (aref ltimes level) 0))
  5954. (loop for l from 0 to level do
  5955. (aset ltimes l (+ (aref ltimes l) t1)))
  5956. (setq t1 0 time (aref ltimes level))
  5957. (loop for l from level to (1- lmax) do
  5958. (aset ltimes l 0))
  5959. (goto-char (match-beginning 0))
  5960. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  5961. (setq org-clock-file-total-minutes (aref ltimes 0)))
  5962. (set-buffer-modified-p bmp)))
  5963. (defun org-clock-display (&optional total-only)
  5964. "Show subtree times in the entire buffer.
  5965. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  5966. in the echo area."
  5967. (interactive)
  5968. (org-remove-clock-overlays)
  5969. (let (time h m p)
  5970. (org-clock-sum)
  5971. (unless total-only
  5972. (save-excursion
  5973. (goto-char (point-min))
  5974. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5975. (goto-char p)
  5976. (when (setq time (get-text-property p :org-clock-minutes))
  5977. (org-put-clock-overlay time (funcall outline-level))))
  5978. (setq h (/ org-clock-file-total-minutes 60)
  5979. m (- org-clock-file-total-minutes (* 60 h)))
  5980. ;; Arrange to remove the overlays upon next change.
  5981. (when org-remove-highlights-with-change
  5982. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  5983. nil 'local))))
  5984. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  5985. (defvar org-clock-overlays nil)
  5986. (make-variable-buffer-local 'org-clock-overlays)
  5987. (defun org-put-clock-overlay (time &optional level)
  5988. "Put an overlays on the current line, displaying TIME.
  5989. If LEVEL is given, prefix time with a corresponding number of stars.
  5990. This creates a new overlay and stores it in `org-clock-overlays', so that it
  5991. will be easy to remove."
  5992. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  5993. (l (if level (org-get-legal-level level 0) 0))
  5994. (off 0)
  5995. ov tx)
  5996. (move-to-column c)
  5997. (unless (eolp) (skip-chars-backward "^ \t"))
  5998. (skip-chars-backward " \t")
  5999. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  6000. tx (concat (buffer-substring (1- (point)) (point))
  6001. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  6002. (org-add-props (format "%s %2d:%02d%s"
  6003. (make-string l ?*) h m
  6004. (make-string (- 10 l) ?\ ))
  6005. '(face secondary-selection))
  6006. ""))
  6007. (if (not (featurep 'xemacs))
  6008. (org-overlay-put ov 'display tx)
  6009. (org-overlay-put ov 'invisible t)
  6010. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  6011. (push ov org-clock-overlays)))
  6012. (defun org-remove-clock-overlays (&optional beg end noremove)
  6013. "Remove the occur highlights from the buffer.
  6014. BEG and END are ignored. If NOREMOVE is nil, remove this function
  6015. from the `before-change-functions' in the current buffer."
  6016. (interactive)
  6017. (unless org-inhibit-highlight-removal
  6018. (mapc 'org-delete-overlay org-clock-overlays)
  6019. (setq org-clock-overlays nil)
  6020. (unless noremove
  6021. (remove-hook 'before-change-functions
  6022. 'org-remove-clock-overlays 'local))))
  6023. (defun org-clock-out-if-current ()
  6024. "Clock out if the current entry contains the running clock.
  6025. This is used to stop the clock after a TODO entry is marked DONE."
  6026. (when (and (equal state org-done-string)
  6027. (equal (marker-buffer org-clock-marker) (current-buffer))
  6028. (< (point) org-clock-marker)
  6029. (> (save-excursion (outline-next-heading) (point))
  6030. org-clock-marker))
  6031. ;; Clock out, but don't accept a logging message for this.
  6032. (let ((org-log-done (if (and (listp org-log-done)
  6033. (member 'clock-out org-log-done))
  6034. '(done)
  6035. org-log-done)))
  6036. (org-clock-out))))
  6037. (add-hook 'org-after-todo-state-change-hook
  6038. 'org-clock-out-if-current)
  6039. (defun org-check-running-clock ()
  6040. "Check if the current buffer contains the running clock.
  6041. If yes, offer to stop it and to save the buffer with the changes."
  6042. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  6043. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  6044. (buffer-name))))
  6045. (org-clock-out)
  6046. (when (y-or-n-p "Save changed buffer?")
  6047. (save-buffer))))
  6048. (defun org-clock-report ()
  6049. "Create a table containing a report about clocked time.
  6050. If the buffer contains lines
  6051. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  6052. #+END: clocktable
  6053. then the table will be inserted between these lines, replacing whatever
  6054. is was there before. If these lines are not in the buffer, the table
  6055. is inserted at point, surrounded by the special lines.
  6056. The BEGIN line can contain parameters. Allowed are:
  6057. :maxlevel The maximum level to be included in the table. Default is 3.
  6058. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  6059. (interactive)
  6060. (org-remove-clock-overlays)
  6061. (unless (org-find-dblock "clocktable")
  6062. (org-create-dblock (list :name "clocktable"
  6063. :maxlevel 2 :emphasize nil)))
  6064. (org-update-dblock))
  6065. (defun org-clock-update-time-maybe ()
  6066. "If this is a CLOCK line, update it and return t.
  6067. Otherwise, return nil."
  6068. (interactive)
  6069. (save-excursion
  6070. (beginning-of-line 1)
  6071. (skip-chars-forward " \t")
  6072. (when (looking-at org-clock-string)
  6073. (let ((re (concat "[ \t]*" org-clock-string
  6074. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  6075. "\\([ \t]*=>.*\\)?"))
  6076. ts te h m s)
  6077. (if (not (looking-at re))
  6078. nil
  6079. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  6080. (end-of-line 1)
  6081. (setq ts (match-string 1)
  6082. te (match-string 2))
  6083. (setq s (- (time-to-seconds
  6084. (apply 'encode-time (org-parse-time-string te)))
  6085. (time-to-seconds
  6086. (apply 'encode-time (org-parse-time-string ts))))
  6087. h (floor (/ s 3600))
  6088. s (- s (* 3600 h))
  6089. m (floor (/ s 60))
  6090. s (- s (* 60 s)))
  6091. (insert " => " (format "%2d:%02d" h m))
  6092. t)))))
  6093. (defun org-clock-special-range (key &optional time as-strings)
  6094. "Return two times bordering a special time range.
  6095. Key is a symbol specifying the range and can be one of `today', `yesterday',
  6096. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  6097. A week starts Monday 0:00 and ends Sunday 24:00.
  6098. The range is determined relative to TIME. TIME defaults to the current time.
  6099. The return value is a cons cell with two internal times like the ones
  6100. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  6101. the returned times will be formatted strings."
  6102. (let* ((tm (decode-time (or time (current-time))))
  6103. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  6104. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  6105. (dow (nth 6 tm))
  6106. s1 m1 h1 d1 month1 y1 diff ts te fm)
  6107. (cond
  6108. ((eq key 'today)
  6109. (setq h 0 m 0 h1 24 m1 0))
  6110. ((eq key 'yesterday)
  6111. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  6112. ((eq key 'thisweek)
  6113. (setq diff (if (= dow 0) 6 (1- dow))
  6114. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  6115. ((eq key 'lastweek)
  6116. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  6117. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  6118. ((eq key 'thismonth)
  6119. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  6120. ((eq key 'lastmonth)
  6121. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  6122. ((eq key 'thisyear)
  6123. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  6124. ((eq key 'lastyear)
  6125. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  6126. (t (error "No such time block %s" key)))
  6127. (setq ts (encode-time s m h d month y)
  6128. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  6129. (or d1 d) (or month1 month) (or y1 y)))
  6130. (setq fm (cdr org-time-stamp-formats))
  6131. (if as-strings
  6132. (cons (format-time-string fm ts) (format-time-string fm te))
  6133. (cons ts te))))
  6134. (defun org-dblock-write:clocktable (params)
  6135. "Write the standard clocktable."
  6136. (let ((hlchars '((1 . "*") (2 . ?/)))
  6137. (emph nil)
  6138. (ins (make-marker))
  6139. ipos time h m p level hlc hdl maxlevel
  6140. ts te cc block)
  6141. (setq maxlevel (or (plist-get params :maxlevel) 3)
  6142. emph (plist-get params :emphasize)
  6143. ts (plist-get params :tstart)
  6144. te (plist-get params :tend)
  6145. block (plist-get params :block))
  6146. (when block
  6147. (setq cc (org-clock-special-range block nil t)
  6148. ts (car cc) te (cdr cc)))
  6149. (if ts (setq ts (time-to-seconds
  6150. (apply 'encode-time (org-parse-time-string ts)))))
  6151. (if te (setq te (time-to-seconds
  6152. (apply 'encode-time (org-parse-time-string te)))))
  6153. (move-marker ins (point))
  6154. (setq ipos (point))
  6155. ;; FIXME: does not yet use org-insert-time-stamp or custom format
  6156. (insert-before-markers "Clock summary at ["
  6157. (substring
  6158. (format-time-string (cdr org-time-stamp-formats))
  6159. 1 -1)
  6160. "]."
  6161. (if block
  6162. (format " Considered range is /%s/." block)
  6163. "")
  6164. "\n\n|L|Headline|Time|\n")
  6165. (org-clock-sum ts te)
  6166. (setq h (/ org-clock-file-total-minutes 60)
  6167. m (- org-clock-file-total-minutes (* 60 h)))
  6168. (insert-before-markers "|-\n|0|" "*Total file time*| "
  6169. (format "*%d:%02d*" h m)
  6170. "|\n")
  6171. (goto-char (point-min))
  6172. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  6173. (goto-char p)
  6174. (when (setq time (get-text-property p :org-clock-minutes))
  6175. (save-excursion
  6176. (beginning-of-line 1)
  6177. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  6178. (setq level (- (match-end 1) (match-beginning 1)))
  6179. (<= level maxlevel))
  6180. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  6181. hdl (match-string 2)
  6182. h (/ time 60)
  6183. m (- time (* 60 h)))
  6184. (goto-char ins)
  6185. (if (= level 1) (insert-before-markers "|-\n"))
  6186. (insert-before-markers
  6187. "| " (int-to-string level) "|" hlc hdl hlc " |"
  6188. (make-string (1- level) ?|)
  6189. hlc
  6190. (format "%d:%02d" h m)
  6191. hlc
  6192. " |\n")))))
  6193. (goto-char ins)
  6194. (backward-delete-char 1)
  6195. (goto-char ipos)
  6196. (skip-chars-forward "^|")
  6197. (org-table-align)))
  6198. (defun org-collect-clock-time-entries ()
  6199. "Return an internal list with clocking information.
  6200. This list has one entry for each CLOCK interval.
  6201. FIXME: describe the elements."
  6202. (interactive)
  6203. (let ((re (concat "^[ \t]*" org-clock-string
  6204. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  6205. rtn beg end next cont level title total closedp leafp
  6206. clockpos titlepos h m donep)
  6207. (save-excursion
  6208. (org-clock-sum)
  6209. (goto-char (point-min))
  6210. (while (re-search-forward re nil t)
  6211. (setq clockpos (match-beginning 0)
  6212. beg (match-string 1) end (match-string 2)
  6213. cont (match-end 0))
  6214. (setq beg (apply 'encode-time (org-parse-time-string beg))
  6215. end (apply 'encode-time (org-parse-time-string end)))
  6216. (org-back-to-heading t)
  6217. (setq donep (org-entry-is-done-p))
  6218. (setq titlepos (point)
  6219. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  6220. h (/ total 60) m (- total (* 60 h))
  6221. total (cons h m))
  6222. (looking-at "\\(\\*+\\) +\\(.*\\)")
  6223. (setq level (- (match-end 1) (match-beginning 1))
  6224. title (org-match-string-no-properties 2))
  6225. (save-excursion (outline-next-heading) (setq next (point)))
  6226. (setq closedp (re-search-forward org-closed-time-regexp next t))
  6227. (goto-char next)
  6228. (setq leafp (and (looking-at "^\\*+ ")
  6229. (<= (- (match-end 0) (point)) level)))
  6230. (push (list beg end clockpos closedp donep
  6231. total title titlepos level leafp)
  6232. rtn)
  6233. (goto-char cont)))
  6234. (nreverse rtn)))
  6235. ;;; Agenda, and Diary Integration
  6236. ;;; Define the mode
  6237. (defvar org-agenda-mode-map (make-sparse-keymap)
  6238. "Keymap for `org-agenda-mode'.")
  6239. (defvar org-agenda-menu) ; defined later in this file.
  6240. (defvar org-agenda-follow-mode nil)
  6241. (defvar org-agenda-show-log nil)
  6242. (defvar org-agenda-redo-command nil)
  6243. (defvar org-agenda-mode-hook nil)
  6244. (defvar org-agenda-type nil)
  6245. (defvar org-agenda-force-single-file nil)
  6246. (defun org-agenda-mode ()
  6247. "Mode for time-sorted view on action items in Org-mode files.
  6248. The following commands are available:
  6249. \\{org-agenda-mode-map}"
  6250. (interactive)
  6251. (kill-all-local-variables)
  6252. (setq major-mode 'org-agenda-mode)
  6253. (setq mode-name "Org-Agenda")
  6254. (use-local-map org-agenda-mode-map)
  6255. (easy-menu-add org-agenda-menu)
  6256. (if org-startup-truncated (setq truncate-lines t))
  6257. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  6258. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  6259. (unless org-agenda-keep-modes
  6260. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  6261. org-agenda-show-log nil))
  6262. (easy-menu-change
  6263. '("Agenda") "Agenda Files"
  6264. (append
  6265. (list
  6266. (vector
  6267. (if (get 'org-agenda-files 'org-restrict)
  6268. "Restricted to single file"
  6269. "Edit File List")
  6270. '(org-edit-agenda-file-list)
  6271. (not (get 'org-agenda-files 'org-restrict)))
  6272. "--")
  6273. (mapcar 'org-file-menu-entry (org-agenda-files))))
  6274. (org-agenda-set-mode-name)
  6275. (apply
  6276. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  6277. (list 'org-agenda-mode-hook)))
  6278. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  6279. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  6280. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  6281. (define-key org-agenda-mode-map " " 'org-agenda-show)
  6282. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  6283. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  6284. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  6285. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  6286. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  6287. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  6288. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  6289. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  6290. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  6291. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  6292. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  6293. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  6294. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  6295. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  6296. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  6297. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  6298. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  6299. (while l (define-key org-agenda-mode-map
  6300. (int-to-string (pop l)) 'digit-argument)))
  6301. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  6302. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  6303. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  6304. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  6305. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  6306. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  6307. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  6308. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  6309. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  6310. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  6311. (define-key org-agenda-mode-map "n" 'next-line)
  6312. (define-key org-agenda-mode-map "p" 'previous-line)
  6313. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  6314. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  6315. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  6316. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  6317. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  6318. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  6319. (eval-after-load "calendar"
  6320. '(define-key calendar-mode-map org-calendar-to-agenda-key
  6321. 'org-calendar-goto-agenda))
  6322. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  6323. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  6324. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  6325. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  6326. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  6327. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  6328. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  6329. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  6330. (define-key org-agenda-mode-map "O" 'org-clock-out)
  6331. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  6332. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  6333. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  6334. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  6335. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  6336. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  6337. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  6338. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  6339. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  6340. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  6341. "Local keymap for agenda entries from Org-mode.")
  6342. (define-key org-agenda-keymap
  6343. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  6344. (define-key org-agenda-keymap
  6345. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  6346. (when org-agenda-mouse-1-follows-link
  6347. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  6348. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  6349. '("Agenda"
  6350. ("Agenda Files")
  6351. "--"
  6352. ["Show" org-agenda-show t]
  6353. ["Go To (other window)" org-agenda-goto t]
  6354. ["Go To (this window)" org-agenda-switch-to t]
  6355. ["Follow Mode" org-agenda-follow-mode
  6356. :style toggle :selected org-agenda-follow-mode :active t]
  6357. "--"
  6358. ["Cycle TODO" org-agenda-todo t]
  6359. ("Tags"
  6360. ["Show all Tags" org-agenda-show-tags t]
  6361. ["Set Tags" org-agenda-set-tags t])
  6362. ("Schedule"
  6363. ["Schedule" org-agenda-schedule t]
  6364. ["Set Deadline" org-agenda-deadline t]
  6365. "--"
  6366. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  6367. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  6368. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  6369. ("Priority"
  6370. ["Set Priority" org-agenda-priority t]
  6371. ["Increase Priority" org-agenda-priority-up t]
  6372. ["Decrease Priority" org-agenda-priority-down t]
  6373. ["Show Priority" org-agenda-show-priority t])
  6374. "--"
  6375. ;; ["New agenda command" org-agenda t]
  6376. ["Rebuild buffer" org-agenda-redo t]
  6377. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  6378. "--"
  6379. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  6380. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  6381. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  6382. "--"
  6383. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  6384. :style radio :selected (equal org-agenda-ndays 1)]
  6385. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  6386. :style radio :selected (equal org-agenda-ndays 7)]
  6387. "--"
  6388. ["Show Logbook entries" org-agenda-log-mode
  6389. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  6390. ["Include Diary" org-agenda-toggle-diary
  6391. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  6392. ["Use Time Grid" org-agenda-toggle-time-grid
  6393. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  6394. "--"
  6395. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  6396. ("Calendar Commands"
  6397. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  6398. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  6399. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  6400. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  6401. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  6402. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  6403. "--"
  6404. ["Quit" org-agenda-quit t]
  6405. ["Exit and Release Buffers" org-agenda-exit t]
  6406. ))
  6407. (defvar org-agenda-restrict nil)
  6408. (defvar org-agenda-restrict-begin (make-marker))
  6409. (defvar org-agenda-restrict-end (make-marker))
  6410. (defvar org-agenda-last-dispatch-buffer nil)
  6411. ;;;###autoload
  6412. (defun org-agenda (arg)
  6413. "Dispatch agenda commands to collect entries to the agenda buffer.
  6414. Prompts for a character to select a command. Any prefix arg will be passed
  6415. on to the selected command. The default selections are:
  6416. g
  6417. a Call `org-agenda-list' to display the agenda for current day or week.
  6418. t Call `org-todo-list' to display the global todo list.
  6419. T Call `org-todo-list' to display the global todo list, select only
  6420. entries with a specific TODO keyword (the user gets a prompt).
  6421. m Call `org-tags-view' to display headlines with tags matching
  6422. a condition (the user is prompted for the condition).
  6423. M Like `m', but select only TODO entries, no ordinary headlines.
  6424. l Create a timeeline for the current buffer.
  6425. More commands can be added by configuring the variable
  6426. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  6427. searches can be pre-defined in this way.
  6428. If the current buffer is in Org-mode and visiting a file, you can also
  6429. first press `1' to indicate that the agenda should be temporarily (until the
  6430. next use of \\[org-agenda]) restricted to the current file."
  6431. (interactive "P")
  6432. (catch 'exit
  6433. (let* ((buf (current-buffer))
  6434. (bfn (buffer-file-name (buffer-base-buffer)))
  6435. (restrict-ok (and bfn (org-mode-p)))
  6436. (custom org-agenda-custom-commands)
  6437. c entry key type match lprops)
  6438. ;; Turn off restriction
  6439. (put 'org-agenda-files 'org-restrict nil)
  6440. (setq org-agenda-restrict nil)
  6441. (move-marker org-agenda-restrict-begin nil)
  6442. (move-marker org-agenda-restrict-end nil)
  6443. ;; Remember where this call originated
  6444. (setq org-agenda-last-dispatch-buffer (current-buffer))
  6445. (save-window-excursion
  6446. (delete-other-windows)
  6447. (switch-to-buffer-other-window " *Agenda Commands*")
  6448. (erase-buffer)
  6449. (insert
  6450. "Press key for an agenda command:
  6451. --------------------------------
  6452. a Agenda for current week or day
  6453. t List of all TODO entries T Entries with special TODO kwd
  6454. m Match a TAGS query M Like m, but only TODO entries
  6455. L Timeline for current buffer C Configure custom agenda commands")
  6456. (while (setq entry (pop custom))
  6457. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  6458. (insert (format "\n%-4s%-14s: %s"
  6459. key
  6460. (cond
  6461. ((stringp type) type)
  6462. ((eq type 'tags) "Tags query")
  6463. ((eq type 'todo) "TODO keyword")
  6464. ((eq type 'tags-tree) "Tags tree")
  6465. ((eq type 'todo-tree) "TODO kwd tree")
  6466. ((eq type 'occur-tree) "Occur tree")
  6467. (t "???"))
  6468. (if (stringp match)
  6469. (org-add-props match nil 'face 'org-warning)
  6470. (format "set of %d commands" (+ -2 (length entry)))))))
  6471. (if restrict-ok
  6472. (insert "\n"
  6473. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  6474. (goto-char (point-min))
  6475. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  6476. (message "Press key for agenda command%s"
  6477. (if restrict-ok ", or [1] or [0] to restrict" ""))
  6478. (setq c (read-char-exclusive))
  6479. (message "")
  6480. (when (memq c '(?L ?1 ?0))
  6481. (if restrict-ok
  6482. (put 'org-agenda-files 'org-restrict (list bfn))
  6483. (error "Cannot restrict agenda to current buffer"))
  6484. (with-current-buffer " *Agenda Commands*"
  6485. (goto-char (point-max))
  6486. (delete-region (point-at-bol) (point))
  6487. (goto-char (point-min)))
  6488. (when (eq c ?0)
  6489. (setq org-agenda-restrict t)
  6490. (with-current-buffer buf
  6491. (if (org-region-active-p)
  6492. (progn
  6493. (move-marker org-agenda-restrict-begin (region-beginning))
  6494. (move-marker org-agenda-restrict-end (region-end)))
  6495. (save-excursion
  6496. (org-back-to-heading t)
  6497. (move-marker org-agenda-restrict-begin (point))
  6498. (move-marker org-agenda-restrict-end
  6499. (progn (org-end-of-subtree t)))))))
  6500. (unless (eq c ?L)
  6501. (message "Press key for agenda command%s"
  6502. (if restrict-ok " (restricted to current file)" ""))
  6503. (setq c (read-char-exclusive)))
  6504. (message "")))
  6505. (require 'calendar) ; FIXME: can we avoid this for some commands?
  6506. ;; For example the todo list should not need it (but does...)
  6507. (cond
  6508. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  6509. ((equal c ?a) (call-interactively 'org-agenda-list))
  6510. ((equal c ?t) (call-interactively 'org-todo-list))
  6511. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  6512. ((equal c ?m) (call-interactively 'org-tags-view))
  6513. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  6514. ((equal c ?L)
  6515. (unless restrict-ok
  6516. (error "This is not an Org-mode file"))
  6517. (org-call-with-arg 'org-timeline arg))
  6518. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  6519. (if (symbolp (nth 1 entry))
  6520. (progn
  6521. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  6522. lprops (nth 3 entry))
  6523. (cond
  6524. ((eq type 'tags)
  6525. (org-let lprops '(org-tags-view current-prefix-arg match)))
  6526. ((eq type 'tags-todo)
  6527. (org-let lprops '(org-tags-view '(4) match)))
  6528. ((eq type 'todo)
  6529. (org-let lprops '(org-todo-list match)))
  6530. ((eq type 'tags-tree)
  6531. (org-check-for-org-mode)
  6532. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  6533. ((eq type 'todo-tree)
  6534. (org-check-for-org-mode)
  6535. (org-let lprops
  6536. '(org-occur (concat "^" outline-regexp "[ \t]*"
  6537. (regexp-quote match) "\\>"))))
  6538. ((eq type 'occur-tree)
  6539. (org-check-for-org-mode)
  6540. (org-let lprops '(org-occur match)))
  6541. (t (error "Invalid custom agenda command type %s" type))))
  6542. (org-run-agenda-series (cddr entry))))
  6543. (t (error "Invalid key"))))))
  6544. ;; FIXME: what is the meaning of WINDOW?????
  6545. (defun org-run-agenda-series (series &optional window)
  6546. (org-prepare-agenda)
  6547. (let* ((org-agenda-multi t)
  6548. (redo (list 'org-run-agenda-series (list 'quote series)))
  6549. (org-select-agenda-window t)
  6550. (cmds (car series))
  6551. (gprops (nth 1 series))
  6552. match ;; The byte compiler incorrectly complains about this. Keep it!
  6553. cmd type lprops)
  6554. (while (setq cmd (pop cmds))
  6555. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  6556. (cond
  6557. ((eq type 'agenda)
  6558. (call-interactively 'org-agenda-list))
  6559. ((eq type 'alltodo)
  6560. (call-interactively 'org-todo-list))
  6561. ((eq type 'tags)
  6562. (org-let2 gprops lprops
  6563. '(org-tags-view current-prefix-arg match)))
  6564. ((eq type 'tags-todo)
  6565. (org-let2 gprops lprops
  6566. '(org-tags-view '(4) match)))
  6567. ((eq type 'todo)
  6568. (org-let2 gprops lprops
  6569. '(org-todo-list match)))
  6570. (t (error "Invalid type in command series"))))
  6571. (widen)
  6572. (setq org-agenda-redo-command redo)
  6573. (goto-char (point-min)))
  6574. (org-finalize-agenda))
  6575. ;;;###autoload
  6576. (defmacro org-batch-agenda (cmd-key &rest parameters)
  6577. "Run an agenda command in batch mode, send result to STDOUT.
  6578. CMD-KEY is a string that is also a key in `org-agenda-custom-commands'.
  6579. Paramters are alternating variable names and values that will be bound
  6580. before running the agenda command."
  6581. (let (pars)
  6582. (while parameters
  6583. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  6584. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  6585. (eval (list 'let (nreverse pars) '(org-agenda nil))))
  6586. (set-buffer "*Org Agenda*")
  6587. (princ (buffer-string))))
  6588. (defmacro org-no-read-only (&rest body)
  6589. "Inhibit read-only for BODY."
  6590. `(let ((inhibit-read-only t)) ,@body))
  6591. (defun org-check-for-org-mode ()
  6592. "Make sure current buffer is in org-mode. Error if not."
  6593. (or (org-mode-p)
  6594. (error "Cannot execute org-mode agenda command on buffer in %s."
  6595. major-mode)))
  6596. (defun org-fit-agenda-window ()
  6597. "Fit the window to the buffer size."
  6598. (and org-fit-agenda-window
  6599. (memq org-agenda-window-setup '(reorganize-frame))
  6600. (fboundp 'fit-window-to-buffer)
  6601. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  6602. (/ (frame-height) 2))))
  6603. (defun org-agenda-files (&optional unrestricted)
  6604. "Get the list of agenda files.
  6605. Optional UNRESTRICTED means return the full list even if a restriction
  6606. is currently in place."
  6607. (cond
  6608. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  6609. ((stringp org-agenda-files) (org-read-agenda-file-list))
  6610. ((listp org-agenda-files) org-agenda-files)
  6611. (t (error "Invalid value of `org-agenda-files'"))))
  6612. (defvar org-window-configuration)
  6613. (defun org-edit-agenda-file-list ()
  6614. "Edit the list of agenda files.
  6615. Depending on setup, this either uses customize to edit the variable
  6616. `org-agenda-files', or it visits the file that is holding the list. In the
  6617. latter case, the buffer is set up in a way that saving it automatically kills
  6618. the buffer and restores the previous window configuration."
  6619. (interactive)
  6620. (if (stringp org-agenda-files)
  6621. (let ((cw (current-window-configuration)))
  6622. (find-file org-agenda-files)
  6623. (org-set-local 'org-window-configuration cw)
  6624. (org-add-hook 'after-save-hook
  6625. (lambda ()
  6626. (set-window-configuration
  6627. (prog1 org-window-configuration
  6628. (kill-buffer (current-buffer))))
  6629. (org-install-agenda-files-menu)
  6630. (message "New agenda file list installed"))
  6631. nil 'local)
  6632. (message (substitute-command-keys
  6633. "Edit list and finish with \\[save-buffer]")))
  6634. (customize-variable 'org-agenda-files)))
  6635. (defun org-store-new-agenda-file-list (list)
  6636. "Set new value for the agenda file list and save it correcly."
  6637. (if (stringp org-agenda-files)
  6638. (let ((f org-agenda-files) b)
  6639. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6640. (with-temp-file f
  6641. (insert (mapconcat 'identity list "\n") "\n")))
  6642. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6643. (setq org-agenda-files list)
  6644. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6645. (defun org-read-agenda-file-list ()
  6646. "Read the list of agenda files from a file."
  6647. (when (stringp org-agenda-files)
  6648. (with-temp-buffer
  6649. (insert-file-contents org-agenda-files)
  6650. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6651. (defvar org-agenda-markers nil
  6652. "List of all currently active markers created by `org-agenda'.")
  6653. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6654. "Creation time of the last agenda marker.")
  6655. (defun org-agenda-new-marker (&optional pos)
  6656. "Return a new agenda marker.
  6657. Org-mode keeps a list of these markers and resets them when they are
  6658. no longer in use."
  6659. (let ((m (copy-marker (or pos (point)))))
  6660. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6661. (push m org-agenda-markers)
  6662. m))
  6663. (defun org-agenda-maybe-reset-markers (&optional force)
  6664. "Reset markers created by `org-agenda'. But only if they are old enough."
  6665. (if (or (and force (not org-agenda-multi))
  6666. (> (- (time-to-seconds (current-time))
  6667. org-agenda-last-marker-time)
  6668. 5))
  6669. (while org-agenda-markers
  6670. (move-marker (pop org-agenda-markers) nil))))
  6671. (defvar org-agenda-new-buffers nil
  6672. "Buffers created to visit agenda files.")
  6673. (defun org-get-agenda-file-buffer (file)
  6674. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6675. it to the list of buffers which might be released later."
  6676. (let ((buf (find-buffer-visiting file)))
  6677. (if buf
  6678. buf ; just return it
  6679. ;; Make a new buffer and remember it
  6680. (setq buf (find-file-noselect file))
  6681. (if buf (push buf org-agenda-new-buffers))
  6682. buf)))
  6683. (defun org-release-buffers (blist)
  6684. "Release all buffers in list, asking the user for confirmation when needed.
  6685. When a buffer is unmodified, it is just killed. When modified, it is saved
  6686. \(if the user agrees) and then killed."
  6687. (let (buf file)
  6688. (while (setq buf (pop blist))
  6689. (setq file (buffer-file-name buf))
  6690. (when (and (buffer-modified-p buf)
  6691. file
  6692. (y-or-n-p (format "Save file %s? " file)))
  6693. (with-current-buffer buf (save-buffer)))
  6694. (kill-buffer buf))))
  6695. (defun org-timeline (&optional include-all)
  6696. "Show a time-sorted view of the entries in the current org file.
  6697. Only entries with a time stamp of today or later will be listed. With
  6698. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6699. under the current date.
  6700. If the buffer contains an active region, only check the region for
  6701. dates."
  6702. (interactive "P")
  6703. (require 'calendar)
  6704. (org-compile-prefix-format 'timeline)
  6705. (org-set-sorting-strategy 'timeline)
  6706. (let* ((dopast t)
  6707. (dotodo include-all)
  6708. (doclosed org-agenda-show-log)
  6709. (entry buffer-file-name)
  6710. (date (calendar-current-date))
  6711. (win (selected-window))
  6712. (pos1 (point))
  6713. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6714. (end (if (org-region-active-p) (region-end) (point-max)))
  6715. (day-numbers (org-get-all-dates beg end 'no-ranges
  6716. t doclosed ; always include today
  6717. org-timeline-show-empty-dates))
  6718. (today (time-to-days (current-time)))
  6719. (past t)
  6720. args
  6721. s e rtn d emptyp)
  6722. (setq org-agenda-redo-command
  6723. (list 'progn
  6724. (list 'switch-to-buffer-other-window (current-buffer))
  6725. (list 'org-timeline (list 'quote include-all))))
  6726. (if (not dopast)
  6727. ;; Remove past dates from the list of dates.
  6728. (setq day-numbers (delq nil (mapcar (lambda(x)
  6729. (if (>= x today) x nil))
  6730. day-numbers))))
  6731. (org-prepare-agenda)
  6732. (if doclosed (push :closed args))
  6733. (push :timestamp args)
  6734. (if dotodo (push :todo args))
  6735. (while (setq d (pop day-numbers))
  6736. (if (and (listp d) (eq (car d) :omitted))
  6737. (progn
  6738. (setq s (point))
  6739. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6740. (put-text-property s (1- (point)) 'face 'org-level-3))
  6741. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6742. (if (and (>= d today)
  6743. dopast
  6744. past)
  6745. (progn
  6746. (setq past nil)
  6747. (insert (make-string 79 ?-) "\n")))
  6748. (setq date (calendar-gregorian-from-absolute d))
  6749. (setq s (point))
  6750. (setq rtn (and (not emptyp)
  6751. (apply 'org-agenda-get-day-entries
  6752. entry date args)))
  6753. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6754. (progn
  6755. (insert (calendar-day-name date) " "
  6756. (number-to-string (extract-calendar-day date)) " "
  6757. (calendar-month-name (extract-calendar-month date)) " "
  6758. (number-to-string (extract-calendar-year date)) "\n")
  6759. (put-text-property s (1- (point)) 'face
  6760. 'org-level-3)
  6761. (if (equal d today)
  6762. (put-text-property s (1- (point)) 'org-today t))
  6763. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6764. (put-text-property s (1- (point)) 'day d)))))
  6765. (goto-char (point-min))
  6766. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6767. (point-min)))
  6768. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  6769. (org-finalize-agenda)
  6770. (setq buffer-read-only t)
  6771. (when (not org-select-agenda-window)
  6772. (select-window win)
  6773. (goto-char pos1))))
  6774. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  6775. (defvar org-agenda-last-arguments nil
  6776. "The arguments of the previous call to org-agenda")
  6777. ;;;###autoload
  6778. (defun org-agenda-list (&optional include-all start-day ndays)
  6779. "Produce a weekly view from all files in variable `org-agenda-files'.
  6780. The view will be for the current week, but from the overview buffer you
  6781. will be able to go to other weeks.
  6782. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6783. also be shown, under the current date.
  6784. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6785. on the days are also shown. See the variable `org-log-done' for how
  6786. to turn on logging.
  6787. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6788. given in `org-agenda-start-on-weekday'.
  6789. NDAYS defaults to `org-agenda-ndays'."
  6790. (interactive "P")
  6791. (if org-agenda-overriding-arguments
  6792. (setq include-all (car org-agenda-overriding-arguments)
  6793. start-day (nth 1 org-agenda-overriding-arguments)
  6794. ndays (nth 2 org-agenda-overriding-arguments)))
  6795. (setq org-agenda-last-arguments (list include-all start-day ndays))
  6796. (org-compile-prefix-format 'agenda)
  6797. (org-set-sorting-strategy 'agenda)
  6798. (require 'calendar)
  6799. (let* ((org-agenda-start-on-weekday
  6800. (if (or (equal ndays 1)
  6801. (and (null ndays) (equal 1 org-agenda-ndays)))
  6802. nil org-agenda-start-on-weekday))
  6803. (thefiles (org-agenda-files))
  6804. (files thefiles)
  6805. (win (selected-window))
  6806. (today (time-to-days (current-time)))
  6807. (sd (or start-day today))
  6808. (start (if (or (null org-agenda-start-on-weekday)
  6809. (< org-agenda-ndays 7))
  6810. sd
  6811. (let* ((nt (calendar-day-of-week
  6812. (calendar-gregorian-from-absolute sd)))
  6813. (n1 org-agenda-start-on-weekday)
  6814. (d (- nt n1)))
  6815. (- sd (+ (if (< d 0) 7 0) d)))))
  6816. (day-numbers (list start))
  6817. ;FIXME (inhibit-redisplay t)
  6818. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6819. (setq org-agenda-redo-command
  6820. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  6821. ;; Make the list of days
  6822. (setq ndays (or ndays org-agenda-ndays)
  6823. nd ndays)
  6824. (while (> ndays 1)
  6825. (push (1+ (car day-numbers)) day-numbers)
  6826. (setq ndays (1- ndays)))
  6827. (setq day-numbers (nreverse day-numbers))
  6828. (org-prepare-agenda)
  6829. (org-set-local 'starting-day (car day-numbers))
  6830. (org-set-local 'include-all-loc include-all)
  6831. (when (and (or include-all org-agenda-include-all-todo)
  6832. (member today day-numbers))
  6833. (setq files thefiles
  6834. rtnall nil)
  6835. (while (setq file (pop files))
  6836. (catch 'nextfile
  6837. (org-check-agenda-file file)
  6838. (setq date (calendar-gregorian-from-absolute today)
  6839. rtn (org-agenda-get-day-entries
  6840. file date :todo))
  6841. (setq rtnall (append rtnall rtn))))
  6842. (when rtnall
  6843. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  6844. (add-text-properties (point-min) (1- (point))
  6845. (list 'face 'org-level-3))
  6846. (insert (org-finalize-agenda-entries rtnall) "\n")))
  6847. (setq s (point))
  6848. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  6849. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  6850. (while (setq d (pop day-numbers))
  6851. (setq date (calendar-gregorian-from-absolute d)
  6852. s (point))
  6853. (if (or (setq todayp (= d today))
  6854. (and (not start-pos) (= d sd)))
  6855. (setq start-pos (point))
  6856. (if (and start-pos (not end-pos))
  6857. (setq end-pos (point))))
  6858. (setq files thefiles
  6859. rtnall nil)
  6860. (while (setq file (pop files))
  6861. (catch 'nextfile
  6862. (org-check-agenda-file file)
  6863. (if org-agenda-show-log
  6864. (setq rtn (org-agenda-get-day-entries
  6865. file date
  6866. :deadline :scheduled :timestamp :closed))
  6867. (setq rtn (org-agenda-get-day-entries
  6868. file date
  6869. :deadline :scheduled :timestamp)))
  6870. (setq rtnall (append rtnall rtn))))
  6871. (if org-agenda-include-diary
  6872. (progn
  6873. (require 'diary-lib)
  6874. (setq rtn (org-get-entries-from-diary date))
  6875. (setq rtnall (append rtnall rtn))))
  6876. (if (or rtnall org-agenda-show-all-dates)
  6877. (progn
  6878. (insert (format "%-9s %2d %s %4d\n"
  6879. (calendar-day-name date)
  6880. (extract-calendar-day date)
  6881. (calendar-month-name (extract-calendar-month date))
  6882. (extract-calendar-year date)))
  6883. (put-text-property s (1- (point)) 'face
  6884. 'org-level-3)
  6885. (if todayp (put-text-property s (1- (point)) 'org-today t))
  6886. (if rtnall (insert
  6887. (org-finalize-agenda-entries
  6888. (org-agenda-add-time-grid-maybe
  6889. rtnall nd todayp))
  6890. "\n"))
  6891. (put-text-property s (1- (point)) 'day d))))
  6892. (goto-char (point-min))
  6893. (org-fit-agenda-window)
  6894. (unless (and (pos-visible-in-window-p (point-min))
  6895. (pos-visible-in-window-p (point-max)))
  6896. (goto-char (1- (point-max)))
  6897. (recenter -1)
  6898. (if (not (pos-visible-in-window-p (or start-pos 1)))
  6899. (progn
  6900. (goto-char (or start-pos 1))
  6901. (recenter 1))))
  6902. (goto-char (or start-pos 1))
  6903. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  6904. (org-finalize-agenda)
  6905. (setq buffer-read-only t)
  6906. (if (not org-select-agenda-window) (select-window win))
  6907. (message "")))
  6908. (defvar org-select-this-todo-keyword nil)
  6909. ;;;###autoload
  6910. (defun org-todo-list (arg)
  6911. "Show all TODO entries from all agenda file in a single list.
  6912. The prefix arg can be used to select a specific TODO keyword and limit
  6913. the list to these. When using \\[universal-argument], you will be prompted
  6914. for a keyword. A numeric prefix directly selects the Nth keyword in
  6915. `org-todo-keywords'."
  6916. (interactive "P")
  6917. (org-compile-prefix-format 'todo)
  6918. (org-set-sorting-strategy 'todo)
  6919. (let* ((today (time-to-days (current-time)))
  6920. (date (calendar-gregorian-from-absolute today))
  6921. (win (selected-window))
  6922. (kwds org-todo-keywords)
  6923. (completion-ignore-case t)
  6924. (org-select-this-todo-keyword
  6925. (if (stringp arg) arg
  6926. (and arg (integerp arg) (> arg 0)
  6927. (nth (1- arg) org-todo-keywords))))
  6928. rtn rtnall files file pos)
  6929. (when (equal arg '(4))
  6930. (setq org-select-this-todo-keyword
  6931. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  6932. nil t)))
  6933. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  6934. (org-prepare-agenda)
  6935. (org-set-local 'last-arg arg)
  6936. (org-set-local 'org-todo-keywords kwds)
  6937. (setq org-agenda-redo-command
  6938. '(org-todo-list (or current-prefix-arg last-arg)))
  6939. (setq files (org-agenda-files)
  6940. rtnall nil)
  6941. (while (setq file (pop files))
  6942. (catch 'nextfile
  6943. (org-check-agenda-file file)
  6944. (setq rtn (org-agenda-get-day-entries file date :todo))
  6945. (setq rtnall (append rtnall rtn))))
  6946. (insert "Global list of TODO items of type: ")
  6947. (add-text-properties (point-min) (1- (point))
  6948. (list 'face 'org-level-3))
  6949. (setq pos (point))
  6950. (insert (or org-select-this-todo-keyword "ALL") "\n")
  6951. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6952. (setq pos (point))
  6953. (unless org-agenda-multi
  6954. (insert
  6955. "Available with `N r': (0)ALL "
  6956. (let ((n 0))
  6957. (mapconcat (lambda (x)
  6958. (format "(%d)%s" (setq n (1+ n)) x))
  6959. org-todo-keywords " "))
  6960. "\n"))
  6961. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6962. (when rtnall
  6963. (insert (org-finalize-agenda-entries rtnall) "\n"))
  6964. (goto-char (point-min))
  6965. (org-fit-agenda-window)
  6966. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  6967. (org-finalize-agenda)
  6968. (setq buffer-read-only t)
  6969. (if (not org-select-agenda-window) (select-window win))))
  6970. (defun org-check-agenda-file (file)
  6971. "Make sure FILE exists. If not, ask user what to do."
  6972. (when (not (file-exists-p file))
  6973. (message "non-existent file %s. [R]emove from list or [A]bort?"
  6974. (abbreviate-file-name file))
  6975. (let ((r (downcase (read-char-exclusive))))
  6976. (cond
  6977. ((equal r ?r)
  6978. (org-remove-file file)
  6979. (throw 'nextfile t))
  6980. (t (error "Abort"))))))
  6981. (defun org-agenda-check-type (error &rest types)
  6982. "Check if agenda buffer is of allowed type.
  6983. If ERROR is non-nil, throw an error, otherwise just return nil."
  6984. (if (memq org-agenda-type types)
  6985. t
  6986. (if error
  6987. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  6988. nil)))
  6989. (defun org-agenda-quit ()
  6990. "Exit agenda by removing the window or the buffer."
  6991. (interactive)
  6992. (let ((buf (current-buffer)))
  6993. (if (not (one-window-p)) (delete-window))
  6994. (kill-buffer buf)
  6995. (org-agenda-maybe-reset-markers 'force))
  6996. ;; Maybe restore the pre-agenda window configuration.
  6997. (and org-agenda-restore-windows-after-quit
  6998. (not (eq org-agenda-window-setup 'other-frame))
  6999. org-pre-agenda-window-conf
  7000. (set-window-configuration org-pre-agenda-window-conf)))
  7001. (defun org-agenda-exit ()
  7002. "Exit agenda by removing the window or the buffer.
  7003. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  7004. Org-mode buffers visited directly by the user will not be touched."
  7005. (interactive)
  7006. (org-release-buffers org-agenda-new-buffers)
  7007. (setq org-agenda-new-buffers nil)
  7008. (org-agenda-quit))
  7009. (defun org-save-all-org-buffers ()
  7010. "Save all Org-mode buffers without user confirmation."
  7011. (interactive)
  7012. (message "Saving all Org-mode buffers...")
  7013. (save-some-buffers t 'org-mode-p)
  7014. (message "Saving all Org-mode buffers... done"))
  7015. (defun org-agenda-redo ()
  7016. "Rebuild Agenda.
  7017. When this is the global TODO list, a prefix argument will be interpreted."
  7018. (interactive)
  7019. (let* ((org-agenda-keep-modes t)
  7020. (line (org-current-line))
  7021. (window-line (- line (org-current-line (window-start)))))
  7022. (message "Rebuilding agenda buffer...")
  7023. (eval org-agenda-redo-command)
  7024. (message "Rebuilding agenda buffer...done")
  7025. (goto-line line)
  7026. (recenter window-line)))
  7027. (defun org-agenda-goto-today ()
  7028. "Go to today."
  7029. (interactive)
  7030. (org-agenda-check-type t 'timeline 'agenda)
  7031. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  7032. (cond
  7033. (tdpos (goto-char tdpos))
  7034. ((eq org-agenda-type 'agenda)
  7035. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  7036. (setf (nth 1 org-agenda-overriding-arguments) nil)
  7037. (org-agenda-redo)
  7038. (org-agenda-find-today-or-agenda)))
  7039. (t (error "Cannot find today")))))
  7040. (defun org-agenda-find-today-or-agenda ()
  7041. (goto-char
  7042. (or (text-property-any (point-min) (point-max) 'org-today t)
  7043. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  7044. (point-min))))
  7045. (defun org-agenda-later (arg)
  7046. "Go forward in time by `org-agenda-ndays' days.
  7047. With prefix ARG, go forward that many times `org-agenda-ndays'."
  7048. (interactive "p")
  7049. (org-agenda-check-type t 'agenda)
  7050. (let ((org-agenda-overriding-arguments
  7051. (list (car org-agenda-last-arguments)
  7052. (+ starting-day (* arg org-agenda-ndays))
  7053. nil t)))
  7054. (org-agenda-redo)
  7055. (org-agenda-find-today-or-agenda)))
  7056. (defun org-agenda-earlier (arg)
  7057. "Go back in time by `org-agenda-ndays' days.
  7058. With prefix ARG, go back that many times `org-agenda-ndays'."
  7059. (interactive "p")
  7060. (org-agenda-check-type t 'agenda)
  7061. (let ((org-agenda-overriding-arguments
  7062. (list (car org-agenda-last-arguments)
  7063. (- starting-day (* arg org-agenda-ndays))
  7064. nil t)))
  7065. (org-agenda-redo)
  7066. (org-agenda-find-today-or-agenda)))
  7067. (defun org-agenda-week-view ()
  7068. "Switch to weekly view for agenda."
  7069. (interactive)
  7070. (org-agenda-check-type t 'agenda)
  7071. (if (= org-agenda-ndays 7)
  7072. (error "This is already the week view"))
  7073. (setq org-agenda-ndays 7)
  7074. (let ((org-agenda-overriding-arguments
  7075. (list (car org-agenda-last-arguments)
  7076. (or (get-text-property (point) 'day)
  7077. starting-day)
  7078. nil t)))
  7079. (org-agenda-redo)
  7080. (org-agenda-find-today-or-agenda))
  7081. (org-agenda-set-mode-name)
  7082. (message "Switched to week view"))
  7083. (defun org-agenda-day-view ()
  7084. "Switch to daily view for agenda."
  7085. (interactive)
  7086. (org-agenda-check-type t 'agenda)
  7087. (if (= org-agenda-ndays 1)
  7088. (error "This is already the day view"))
  7089. (setq org-agenda-ndays 1)
  7090. (let ((org-agenda-overriding-arguments
  7091. (list (car org-agenda-last-arguments)
  7092. (or (get-text-property (point) 'day)
  7093. starting-day)
  7094. nil t)))
  7095. (org-agenda-redo)
  7096. (org-agenda-find-today-or-agenda))
  7097. (org-agenda-set-mode-name)
  7098. (message "Switched to day view"))
  7099. (defun org-agenda-next-date-line (&optional arg)
  7100. "Jump to the next line indicating a date in agenda buffer."
  7101. (interactive "p")
  7102. (org-agenda-check-type t 'agenda 'timeline)
  7103. (beginning-of-line 1)
  7104. (if (looking-at "^\\S-") (forward-char 1))
  7105. (if (not (re-search-forward "^\\S-" nil t arg))
  7106. (progn
  7107. (backward-char 1)
  7108. (error "No next date after this line in this buffer")))
  7109. (goto-char (match-beginning 0)))
  7110. (defun org-agenda-previous-date-line (&optional arg)
  7111. "Jump to the previous line indicating a date in agenda buffer."
  7112. (interactive "p")
  7113. (org-agenda-check-type t 'agenda 'timeline)
  7114. (beginning-of-line 1)
  7115. (if (not (re-search-backward "^\\S-" nil t arg))
  7116. (error "No previous date before this line in this buffer")))
  7117. ;; Initialize the highlight
  7118. (defvar org-hl (org-make-overlay 1 1))
  7119. (org-overlay-put org-hl 'face 'highlight)
  7120. (defun org-highlight (begin end &optional buffer)
  7121. "Highlight a region with overlay."
  7122. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  7123. org-hl begin end (or buffer (current-buffer))))
  7124. (defun org-unhighlight ()
  7125. "Detach overlay INDEX."
  7126. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  7127. (defun org-agenda-follow-mode ()
  7128. "Toggle follow mode in an agenda buffer."
  7129. (interactive)
  7130. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  7131. (org-agenda-set-mode-name)
  7132. (message "Follow mode is %s"
  7133. (if org-agenda-follow-mode "on" "off")))
  7134. (defun org-agenda-log-mode ()
  7135. "Toggle log mode in an agenda buffer."
  7136. (interactive)
  7137. (org-agenda-check-type t 'agenda 'timeline)
  7138. (setq org-agenda-show-log (not org-agenda-show-log))
  7139. (org-agenda-set-mode-name)
  7140. (org-agenda-redo)
  7141. (message "Log mode is %s"
  7142. (if org-agenda-show-log "on" "off")))
  7143. (defun org-agenda-toggle-diary ()
  7144. "Toggle diary inclusion in an agenda buffer."
  7145. (interactive)
  7146. (org-agenda-check-type t 'agenda)
  7147. (setq org-agenda-include-diary (not org-agenda-include-diary))
  7148. (org-agenda-redo)
  7149. (org-agenda-set-mode-name)
  7150. (message "Diary inclusion turned %s"
  7151. (if org-agenda-include-diary "on" "off")))
  7152. (defun org-agenda-toggle-time-grid ()
  7153. "Toggle time grid in an agenda buffer."
  7154. (interactive)
  7155. (org-agenda-check-type t 'agenda)
  7156. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  7157. (org-agenda-redo)
  7158. (org-agenda-set-mode-name)
  7159. (message "Time-grid turned %s"
  7160. (if org-agenda-use-time-grid "on" "off")))
  7161. (defun org-agenda-set-mode-name ()
  7162. "Set the mode name to indicate all the small mode settings."
  7163. (setq mode-name
  7164. (concat "Org-Agenda"
  7165. (if (equal org-agenda-ndays 1) " Day" "")
  7166. (if (equal org-agenda-ndays 7) " Week" "")
  7167. (if org-agenda-follow-mode " Follow" "")
  7168. (if org-agenda-include-diary " Diary" "")
  7169. (if org-agenda-use-time-grid " Grid" "")
  7170. (if org-agenda-show-log " Log" "")))
  7171. (force-mode-line-update))
  7172. (defun org-agenda-post-command-hook ()
  7173. (and (eolp) (not (bolp)) (backward-char 1))
  7174. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  7175. (if (and org-agenda-follow-mode
  7176. (get-text-property (point) 'org-marker))
  7177. (org-agenda-show)))
  7178. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  7179. (defun org-get-entries-from-diary (date)
  7180. "Get the (Emacs Calendar) diary entries for DATE."
  7181. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  7182. (diary-display-hook '(fancy-diary-display))
  7183. (list-diary-entries-hook
  7184. (cons 'org-diary-default-entry list-diary-entries-hook))
  7185. (diary-file-name-prefix-function nil) ; turn this feature off
  7186. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  7187. entries
  7188. (org-disable-agenda-to-diary t))
  7189. (save-excursion
  7190. (save-window-excursion
  7191. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  7192. (if (not (get-buffer fancy-diary-buffer))
  7193. (setq entries nil)
  7194. (with-current-buffer fancy-diary-buffer
  7195. (setq buffer-read-only nil)
  7196. (if (= (point-max) 1)
  7197. ;; No entries
  7198. (setq entries nil)
  7199. ;; Omit the date and other unnecessary stuff
  7200. (org-agenda-cleanup-fancy-diary)
  7201. ;; Add prefix to each line and extend the text properties
  7202. (if (= (point-max) 1)
  7203. (setq entries nil)
  7204. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  7205. (set-buffer-modified-p nil)
  7206. (kill-buffer fancy-diary-buffer)))
  7207. (when entries
  7208. (setq entries (org-split-string entries "\n"))
  7209. (setq entries
  7210. (mapcar
  7211. (lambda (x)
  7212. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  7213. ;; Extend the text properties to the beginning of the line
  7214. (org-add-props x (text-properties-at (1- (length x)) x)))
  7215. entries)))))
  7216. (defun org-agenda-cleanup-fancy-diary ()
  7217. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  7218. This gets rid of the date, the underline under the date, and
  7219. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  7220. date. It also removes lines that contain only whitespace."
  7221. (goto-char (point-min))
  7222. (if (looking-at ".*?:[ \t]*")
  7223. (progn
  7224. (replace-match "")
  7225. (re-search-forward "\n=+$" nil t)
  7226. (replace-match "")
  7227. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  7228. (re-search-forward "\n=+$" nil t)
  7229. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  7230. (goto-char (point-min))
  7231. (while (re-search-forward "^ +\n" nil t)
  7232. (replace-match ""))
  7233. (goto-char (point-min))
  7234. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  7235. (replace-match "")))
  7236. ;; Make sure entries from the diary have the right text properties.
  7237. (eval-after-load "diary-lib"
  7238. '(if (boundp 'diary-modify-entry-list-string-function)
  7239. ;; We can rely on the hook, nothing to do
  7240. nil
  7241. ;; Hook not avaiable, must use advice to make this work
  7242. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  7243. "Make the position visible."
  7244. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  7245. (stringp string)
  7246. buffer-file-name)
  7247. (setq string (org-modify-diary-entry-string string))))))
  7248. (defun org-modify-diary-entry-string (string)
  7249. "Add text properties to string, allowing org-mode to act on it."
  7250. (org-add-props string nil
  7251. 'mouse-face 'highlight
  7252. 'keymap org-agenda-keymap
  7253. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  7254. (abbreviate-file-name buffer-file-name))
  7255. 'org-agenda-diary-link t
  7256. 'org-marker (org-agenda-new-marker (point-at-bol))))
  7257. (defun org-diary-default-entry ()
  7258. "Add a dummy entry to the diary.
  7259. Needed to avoid empty dates which mess up holiday display."
  7260. ;; Catch the error if dealing with the new add-to-diary-alist
  7261. (when org-disable-agenda-to-diary
  7262. (condition-case nil
  7263. (add-to-diary-list original-date "Org-mode dummy" "")
  7264. (error
  7265. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  7266. ;;;###autoload
  7267. (defun org-cycle-agenda-files ()
  7268. "Cycle through the files in `org-agenda-files'.
  7269. If the current buffer visits an agenda file, find the next one in the list.
  7270. If the current buffer does not, find the first agenda file."
  7271. (interactive)
  7272. (let* ((fs (org-agenda-files t))
  7273. (files (append fs (list (car fs))))
  7274. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  7275. file)
  7276. (unless files (error "No agenda files"))
  7277. (catch 'exit
  7278. (while (setq file (pop files))
  7279. (if (equal (file-truename file) tcf)
  7280. (when (car files)
  7281. (find-file (car files))
  7282. (throw 'exit t))))
  7283. (find-file (car fs)))))
  7284. (defun org-agenda-file-to-end ()
  7285. "Move/add the current file to the end of the agenda file list.
  7286. If the file is not present in the list, it is appended to the list. If it is
  7287. present, it is moved there."
  7288. (interactive)
  7289. (org-agenda-file-to-front 'to-end))
  7290. (defun org-agenda-file-to-front (&optional to-end)
  7291. "Move/add the current file to the top of the agenda file list.
  7292. If the file is not present in the list, it is added to the front. If it is
  7293. present, it is moved there. With optional argument TO-END, add/move to the
  7294. end of the list."
  7295. (interactive "P")
  7296. (let ((file-alist (mapcar (lambda (x)
  7297. (cons (file-truename x) x))
  7298. (org-agenda-files t)))
  7299. (ctf (file-truename buffer-file-name))
  7300. x had)
  7301. (setq x (assoc ctf file-alist) had x)
  7302. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  7303. (if to-end
  7304. (setq file-alist (append (delq x file-alist) (list x)))
  7305. (setq file-alist (cons x (delq x file-alist))))
  7306. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  7307. (org-install-agenda-files-menu)
  7308. (message "File %s to %s of agenda file list"
  7309. (if had "moved" "added") (if to-end "end" "front"))))
  7310. (defun org-remove-file (&optional file)
  7311. "Remove current file from the list of files in variable `org-agenda-files'.
  7312. These are the files which are being checked for agenda entries.
  7313. Optional argument FILE means, use this file instead of the current."
  7314. (interactive)
  7315. (let* ((file (or file buffer-file-name))
  7316. (true-file (file-truename file))
  7317. (afile (abbreviate-file-name file))
  7318. (files (delq nil (mapcar
  7319. (lambda (x)
  7320. (if (equal true-file
  7321. (file-truename x))
  7322. nil x))
  7323. (org-agenda-files t)))))
  7324. (if (not (= (length files) (length (org-agenda-files t))))
  7325. (progn
  7326. (org-store-new-agenda-file-list files)
  7327. (org-install-agenda-files-menu)
  7328. (message "Removed file: %s" afile))
  7329. (message "File was not in list: %s" afile))))
  7330. (defun org-file-menu-entry (file)
  7331. (vector file (list 'find-file file) t))
  7332. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  7333. "Return a list of all relevant day numbers from BEG to END buffer positions.
  7334. If NO-RANGES is non-nil, include only the start and end dates of a range,
  7335. not every single day in the range. If FORCE-TODAY is non-nil, make
  7336. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  7337. inactive time stamps (those in square brackets) are included.
  7338. When EMPTY is non-nil, also include days without any entries."
  7339. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  7340. dates dates1 date day day1 day2 ts1 ts2)
  7341. (if force-today
  7342. (setq dates (list (time-to-days (current-time)))))
  7343. (save-excursion
  7344. (goto-char beg)
  7345. (while (re-search-forward re end t)
  7346. (setq day (time-to-days (org-time-string-to-time
  7347. (substring (match-string 1) 0 10))))
  7348. (or (memq day dates) (push day dates)))
  7349. (unless no-ranges
  7350. (goto-char beg)
  7351. (while (re-search-forward org-tr-regexp end t)
  7352. (setq ts1 (substring (match-string 1) 0 10)
  7353. ts2 (substring (match-string 2) 0 10)
  7354. day1 (time-to-days (org-time-string-to-time ts1))
  7355. day2 (time-to-days (org-time-string-to-time ts2)))
  7356. (while (< (setq day1 (1+ day1)) day2)
  7357. (or (memq day1 dates) (push day1 dates)))))
  7358. (setq dates (sort dates '<))
  7359. (when empty
  7360. (while (setq day (pop dates))
  7361. (setq day2 (car dates))
  7362. (push day dates1)
  7363. (when (and day2 empty)
  7364. (if (or (eq empty t)
  7365. (and (numberp empty) (<= (- day2 day) empty)))
  7366. (while (< (setq day (1+ day)) day2)
  7367. (push (list day) dates1))
  7368. (push (cons :omitted (- day2 day)) dates1))))
  7369. (setq dates (nreverse dates1)))
  7370. dates)))
  7371. ;;;###autoload
  7372. (defun org-diary (&rest args)
  7373. "Return diary information from org-files.
  7374. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  7375. It accesses org files and extracts information from those files to be
  7376. listed in the diary. The function accepts arguments specifying what
  7377. items should be listed. The following arguments are allowed:
  7378. :timestamp List the headlines of items containing a date stamp or
  7379. date range matching the selected date. Deadlines will
  7380. also be listed, on the expiration day.
  7381. :deadline List any deadlines past due, or due within
  7382. `org-deadline-warning-days'. The listing occurs only
  7383. in the diary for *today*, not at any other date. If
  7384. an entry is marked DONE, it is no longer listed.
  7385. :scheduled List all items which are scheduled for the given date.
  7386. The diary for *today* also contains items which were
  7387. scheduled earlier and are not yet marked DONE.
  7388. :todo List all TODO items from the org-file. This may be a
  7389. long list - so this is not turned on by default.
  7390. Like deadlines, these entries only show up in the
  7391. diary for *today*, not at any other date.
  7392. The call in the diary file should look like this:
  7393. &%%(org-diary) ~/path/to/some/orgfile.org
  7394. Use a separate line for each org file to check. Or, if you omit the file name,
  7395. all files listed in `org-agenda-files' will be checked automatically:
  7396. &%%(org-diary)
  7397. If you don't give any arguments (as in the example above), the default
  7398. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  7399. also be written as
  7400. &%%(org-diary :deadline :timestamp :scheduled)
  7401. The function expects the lisp variables `entry' and `date' to be provided
  7402. by the caller, because this is how the calendar works. Don't use this
  7403. function from a program - use `org-agenda-get-day-entries' instead."
  7404. (org-agenda-maybe-reset-markers)
  7405. (org-compile-prefix-format 'agenda)
  7406. (org-set-sorting-strategy 'agenda)
  7407. (setq args (or args '(:deadline :scheduled :timestamp)))
  7408. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  7409. (list entry)
  7410. (org-agenda-files t)))
  7411. file rtn results)
  7412. ;; If this is called during org-agenda, don't return any entries to
  7413. ;; the calendar. Org Agenda will list these entries itself.
  7414. (if org-disable-agenda-to-diary (setq files nil))
  7415. (while (setq file (pop files))
  7416. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  7417. (setq results (append results rtn)))
  7418. (if results
  7419. (concat (org-finalize-agenda-entries results) "\n"))))
  7420. (defvar org-category-table nil)
  7421. (defun org-get-category-table ()
  7422. "Get the table of categories and positions in current buffer."
  7423. (let (tbl)
  7424. (save-excursion
  7425. (goto-char (point-min))
  7426. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  7427. (push (cons (point) (org-trim (match-string 2))) tbl)))
  7428. tbl))
  7429. (defun org-get-category (&optional pos)
  7430. "Get the category applying to position POS."
  7431. (if (not org-category-table)
  7432. (cond
  7433. ((null org-category)
  7434. (setq org-category
  7435. (if buffer-file-name
  7436. (file-name-sans-extension
  7437. (file-name-nondirectory buffer-file-name))
  7438. "???")))
  7439. ((symbolp org-category) (symbol-name org-category))
  7440. (t org-category))
  7441. (let ((tbl org-category-table)
  7442. (pos (or pos (point))))
  7443. (while (and tbl (> (caar tbl) pos))
  7444. (pop tbl))
  7445. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  7446. org-category-table))))))
  7447. (defun org-agenda-get-day-entries (file date &rest args)
  7448. "Does the work for `org-diary' and `org-agenda'.
  7449. FILE is the path to a file to be checked for entries. DATE is date like
  7450. the one returned by `calendar-current-date'. ARGS are symbols indicating
  7451. which kind of entries should be extracted. For details about these, see
  7452. the documentation of `org-diary'."
  7453. (setq args (or args '(:deadline :scheduled :timestamp)))
  7454. (let* ((org-startup-with-deadline-check nil)
  7455. (org-startup-folded nil)
  7456. (org-startup-align-all-tables nil)
  7457. (buffer (if (file-exists-p file)
  7458. (org-get-agenda-file-buffer file)
  7459. (error "No such file %s" file)))
  7460. arg results rtn)
  7461. (if (not buffer)
  7462. ;; If file does not exist, make sure an error message ends up in diary
  7463. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7464. (with-current-buffer buffer
  7465. (unless (org-mode-p)
  7466. (error "Agenda file %s is not in `org-mode'" file))
  7467. (setq org-category-table (org-get-category-table))
  7468. (let ((case-fold-search nil))
  7469. (save-excursion
  7470. (save-restriction
  7471. (if org-agenda-restrict
  7472. (narrow-to-region org-agenda-restrict-begin
  7473. org-agenda-restrict-end)
  7474. (widen))
  7475. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  7476. (while (setq arg (pop args))
  7477. (cond
  7478. ((and (eq arg :todo)
  7479. (equal date (calendar-current-date)))
  7480. (setq rtn (org-agenda-get-todos))
  7481. (setq results (append results rtn)))
  7482. ((eq arg :timestamp)
  7483. (setq rtn (org-agenda-get-blocks))
  7484. (setq results (append results rtn))
  7485. (setq rtn (org-agenda-get-timestamps))
  7486. (setq results (append results rtn)))
  7487. ((eq arg :scheduled)
  7488. (setq rtn (org-agenda-get-scheduled))
  7489. (setq results (append results rtn)))
  7490. ((eq arg :closed)
  7491. (setq rtn (org-agenda-get-closed))
  7492. (setq results (append results rtn)))
  7493. ((and (eq arg :deadline)
  7494. (equal date (calendar-current-date)))
  7495. (setq rtn (org-agenda-get-deadlines))
  7496. (setq results (append results rtn))))))))
  7497. results))))
  7498. (defun org-entry-is-done-p ()
  7499. "Is the current entry marked DONE?"
  7500. (save-excursion
  7501. (and (re-search-backward "[\r\n]\\*" nil t)
  7502. (looking-at org-nl-done-regexp))))
  7503. (defun org-at-date-range-p (&optional inactive-ok)
  7504. "Is the cursor inside a date range?"
  7505. (interactive)
  7506. (save-excursion
  7507. (catch 'exit
  7508. (let ((pos (point)))
  7509. (skip-chars-backward "^[<\r\n")
  7510. (skip-chars-backward "<[")
  7511. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7512. (>= (match-end 0) pos)
  7513. (throw 'exit t))
  7514. (skip-chars-backward "^<[\r\n")
  7515. (skip-chars-backward "<[")
  7516. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7517. (>= (match-end 0) pos)
  7518. (throw 'exit t)))
  7519. nil)))
  7520. (defun org-agenda-get-todos ()
  7521. "Return the TODO information for agenda display."
  7522. (let* ((props (list 'face nil
  7523. 'done-face 'org-done
  7524. 'org-not-done-regexp org-not-done-regexp
  7525. 'mouse-face 'highlight
  7526. 'keymap org-agenda-keymap
  7527. 'help-echo
  7528. (format "mouse-2 or RET jump to org file %s"
  7529. (abbreviate-file-name buffer-file-name))))
  7530. (regexp (concat "[\n\r]\\*+ *\\("
  7531. (if org-select-this-todo-keyword
  7532. (concat "\\<\\(" org-select-this-todo-keyword
  7533. "\\)\\>")
  7534. org-not-done-regexp)
  7535. "[^\n\r]*\\)"))
  7536. (deadline-re (concat ".*\\(\n[^*].*\\)?" org-deadline-time-regexp))
  7537. (sched-re (concat ".*\\(\n[^*].*\\)?" org-scheduled-time-regexp))
  7538. ; FIXME why was this wriong? (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  7539. marker priority category tags
  7540. ee txt)
  7541. (goto-char (point-min))
  7542. (while (re-search-forward regexp nil t)
  7543. (catch :skip
  7544. (save-match-data
  7545. (beginning-of-line)
  7546. (when (or (and org-agenda-todo-ignore-scheduled
  7547. (looking-at sched-re))
  7548. (and org-agenda-todo-ignore-deadlines
  7549. (looking-at deadline-re)
  7550. (org-deadline-close (match-string 2))))
  7551. ;; FIXME: the following test also happens below, but we need it here
  7552. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  7553. (throw :skip nil)))
  7554. (org-agenda-skip)
  7555. (goto-char (match-beginning 1))
  7556. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  7557. category (org-get-category)
  7558. tags (org-get-tags-at (point))
  7559. txt (org-format-agenda-item "" (match-string 1) category tags)
  7560. priority
  7561. (+ (org-get-priority txt)
  7562. (if org-todo-kwd-priority-p
  7563. (- org-todo-kwd-max-priority -2
  7564. (length
  7565. (member (match-string 2) org-todo-keywords)))
  7566. 1)))
  7567. (org-add-props txt props
  7568. 'org-marker marker 'org-hd-marker marker
  7569. 'priority priority 'category category)
  7570. (push txt ee)
  7571. (if org-agenda-todo-list-sublevels
  7572. (goto-char (match-end 1))
  7573. (org-end-of-subtree 'invisible))))
  7574. (nreverse ee)))
  7575. (defconst org-agenda-no-heading-message
  7576. "No heading for this item in buffer or region.")
  7577. (defun org-agenda-get-timestamps ()
  7578. "Return the date stamp information for agenda display."
  7579. (let* ((props (list 'face nil
  7580. 'org-not-done-regexp org-not-done-regexp
  7581. 'mouse-face 'highlight
  7582. 'keymap org-agenda-keymap
  7583. 'help-echo
  7584. (format "mouse-2 or RET jump to org file %s"
  7585. (abbreviate-file-name buffer-file-name))))
  7586. (regexp (regexp-quote
  7587. (substring
  7588. (format-time-string
  7589. (car org-time-stamp-formats)
  7590. (apply 'encode-time ; DATE bound by calendar
  7591. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7592. 0 11)))
  7593. marker hdmarker deadlinep scheduledp donep tmp priority category
  7594. ee txt timestr tags)
  7595. (goto-char (point-min))
  7596. (while (re-search-forward regexp nil t)
  7597. (catch :skip
  7598. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  7599. (org-agenda-skip)
  7600. (setq marker (org-agenda-new-marker (match-beginning 0))
  7601. category (org-get-category (match-beginning 0))
  7602. tmp (buffer-substring (max (point-min)
  7603. (- (match-beginning 0)
  7604. org-ds-keyword-length))
  7605. (match-beginning 0))
  7606. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7607. deadlinep (string-match org-deadline-regexp tmp)
  7608. scheduledp (string-match org-scheduled-regexp tmp)
  7609. donep (org-entry-is-done-p))
  7610. (if (string-match ">" timestr)
  7611. ;; substring should only run to end of time stamp
  7612. (setq timestr (substring timestr 0 (match-end 0))))
  7613. (save-excursion
  7614. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7615. (progn
  7616. (goto-char (match-end 1))
  7617. (setq hdmarker (org-agenda-new-marker)
  7618. tags (org-get-tags-at))
  7619. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7620. (setq txt (org-format-agenda-item
  7621. (format "%s%s"
  7622. (if deadlinep "Deadline: " "")
  7623. (if scheduledp "Scheduled: " ""))
  7624. (match-string 1) category tags timestr)))
  7625. (setq txt org-agenda-no-heading-message))
  7626. (setq priority (org-get-priority txt))
  7627. (org-add-props txt props
  7628. 'org-marker marker 'org-hd-marker hdmarker)
  7629. (if deadlinep
  7630. (org-add-props txt nil
  7631. 'face (if donep 'org-done 'org-warning)
  7632. 'undone-face 'org-warning 'done-face 'org-done
  7633. 'category category 'priority (+ 100 priority))
  7634. (if scheduledp
  7635. (org-add-props txt nil
  7636. 'face 'org-scheduled-today
  7637. 'undone-face 'org-scheduled-today 'done-face 'org-done
  7638. 'category category 'priority (+ 99 priority))
  7639. (org-add-props txt nil 'priority priority 'category category)))
  7640. (push txt ee))
  7641. (outline-next-heading)))
  7642. (nreverse ee)))
  7643. (defun org-agenda-get-closed ()
  7644. "Return the logged TODO entries for agenda display."
  7645. (let* ((props (list 'mouse-face 'highlight
  7646. 'org-not-done-regexp org-not-done-regexp
  7647. 'keymap org-agenda-keymap
  7648. 'help-echo
  7649. (format "mouse-2 or RET jump to org file %s"
  7650. (abbreviate-file-name buffer-file-name))))
  7651. (regexp (concat
  7652. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  7653. (regexp-quote
  7654. (substring
  7655. (format-time-string
  7656. (car org-time-stamp-formats)
  7657. (apply 'encode-time ; DATE bound by calendar
  7658. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7659. 1 11))))
  7660. marker hdmarker priority category tags closedp
  7661. ee txt timestr)
  7662. (goto-char (point-min))
  7663. (while (re-search-forward regexp nil t)
  7664. (catch :skip
  7665. (org-agenda-skip)
  7666. (setq marker (org-agenda-new-marker (match-beginning 0))
  7667. closedp (equal (match-string 1) org-closed-string)
  7668. category (org-get-category (match-beginning 0))
  7669. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7670. ;; donep (org-entry-is-done-p)
  7671. )
  7672. (if (string-match "\\]" timestr)
  7673. ;; substring should only run to end of time stamp
  7674. (setq timestr (substring timestr 0 (match-end 0))))
  7675. (save-excursion
  7676. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7677. (progn
  7678. (goto-char (match-end 1))
  7679. (setq hdmarker (org-agenda-new-marker)
  7680. tags (org-get-tags-at))
  7681. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7682. (setq txt (org-format-agenda-item
  7683. (if closedp "Closed: " "Clocked: ")
  7684. (match-string 1) category tags timestr)))
  7685. (setq txt org-agenda-no-heading-message))
  7686. (setq priority 100000)
  7687. (org-add-props txt props
  7688. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7689. 'priority priority 'category category
  7690. 'undone-face 'org-warning 'done-face 'org-done)
  7691. (push txt ee))
  7692. (outline-next-heading)))
  7693. (nreverse ee)))
  7694. (defun org-agenda-get-deadlines ()
  7695. "Return the deadline information for agenda display."
  7696. (let* ((wdays org-deadline-warning-days)
  7697. (props (list 'mouse-face 'highlight
  7698. 'org-not-done-regexp org-not-done-regexp
  7699. 'keymap org-agenda-keymap
  7700. 'help-echo
  7701. (format "mouse-2 or RET jump to org file %s"
  7702. (abbreviate-file-name buffer-file-name))))
  7703. (regexp org-deadline-time-regexp)
  7704. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7705. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7706. d2 diff pos pos1 category tags
  7707. ee txt head face)
  7708. (goto-char (point-min))
  7709. (while (re-search-forward regexp nil t)
  7710. (catch :skip
  7711. (org-agenda-skip)
  7712. (setq pos (1- (match-beginning 1))
  7713. d2 (time-to-days
  7714. (org-time-string-to-time (match-string 1)))
  7715. diff (- d2 d1))
  7716. ;; When to show a deadline in the calendar:
  7717. ;; If the expiration is within wdays warning time.
  7718. ;; Past-due deadlines are only shown on the current date
  7719. (if (and (< diff wdays) todayp (not (= diff 0)))
  7720. (save-excursion
  7721. (setq category (org-get-category))
  7722. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7723. (progn
  7724. (goto-char (match-end 0))
  7725. (setq pos1 (match-end 1))
  7726. (setq tags (org-get-tags-at pos1))
  7727. (setq head (buffer-substring-no-properties
  7728. (point)
  7729. (progn (skip-chars-forward "^\r\n")
  7730. (point))))
  7731. (if (string-match org-looking-at-done-regexp head)
  7732. (setq txt nil)
  7733. (setq txt (org-format-agenda-item
  7734. (format "In %3d d.: " diff) head category tags))))
  7735. (setq txt org-agenda-no-heading-message))
  7736. (when txt
  7737. (setq face (cond ((<= diff 0) 'org-warning)
  7738. ((<= diff 5) 'org-upcoming-deadline)
  7739. (t nil)))
  7740. (org-add-props txt props
  7741. 'org-marker (org-agenda-new-marker pos)
  7742. 'org-hd-marker (org-agenda-new-marker pos1)
  7743. 'priority (+ (- 10 diff) (org-get-priority txt))
  7744. 'category category
  7745. 'face face 'undone-face face 'done-face 'org-done)
  7746. (push txt ee))))))
  7747. ee))
  7748. (defun org-agenda-get-scheduled ()
  7749. "Return the scheduled information for agenda display."
  7750. (let* ((props (list 'face 'org-scheduled-previously
  7751. 'org-not-done-regexp org-not-done-regexp
  7752. 'undone-face 'org-scheduled-previously
  7753. 'done-face 'org-done
  7754. 'mouse-face 'highlight
  7755. 'keymap org-agenda-keymap
  7756. 'help-echo
  7757. (format "mouse-2 or RET jump to org file %s"
  7758. (abbreviate-file-name buffer-file-name))))
  7759. (regexp org-scheduled-time-regexp)
  7760. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7761. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7762. d2 diff pos pos1 category tags
  7763. ee txt head)
  7764. (goto-char (point-min))
  7765. (while (re-search-forward regexp nil t)
  7766. (catch :skip
  7767. (org-agenda-skip)
  7768. (setq pos (1- (match-beginning 1))
  7769. d2 (time-to-days
  7770. (org-time-string-to-time (match-string 1)))
  7771. diff (- d2 d1))
  7772. ;; When to show a scheduled item in the calendar:
  7773. ;; If it is on or past the date.
  7774. (if (and (< diff 0) todayp)
  7775. (save-excursion
  7776. (setq category (org-get-category))
  7777. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7778. (progn
  7779. (goto-char (match-end 0))
  7780. (setq pos1 (match-end 1))
  7781. (setq tags (org-get-tags-at))
  7782. (setq head (buffer-substring-no-properties
  7783. (point)
  7784. (progn (skip-chars-forward "^\r\n") (point))))
  7785. (if (string-match org-looking-at-done-regexp head)
  7786. (setq txt nil)
  7787. (setq txt (org-format-agenda-item
  7788. (format "Sched.%2dx: " (- 1 diff)) head
  7789. category tags))))
  7790. (setq txt org-agenda-no-heading-message))
  7791. (when txt
  7792. (org-add-props txt props
  7793. 'org-marker (org-agenda-new-marker pos)
  7794. 'org-hd-marker (org-agenda-new-marker pos1)
  7795. 'priority (+ (- 5 diff) (org-get-priority txt))
  7796. 'category category)
  7797. (push txt ee))))))
  7798. ee))
  7799. (defun org-agenda-get-blocks ()
  7800. "Return the date-range information for agenda display."
  7801. (let* ((props (list 'face nil
  7802. 'org-not-done-regexp org-not-done-regexp
  7803. 'mouse-face 'highlight
  7804. 'keymap org-agenda-keymap
  7805. 'help-echo
  7806. (format "mouse-2 or RET jump to org file %s"
  7807. (abbreviate-file-name buffer-file-name))))
  7808. (regexp org-tr-regexp)
  7809. (d0 (calendar-absolute-from-gregorian date))
  7810. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7811. (goto-char (point-min))
  7812. (while (re-search-forward regexp nil t)
  7813. (catch :skip
  7814. (org-agenda-skip)
  7815. (setq pos (point))
  7816. (setq timestr (match-string 0)
  7817. s1 (match-string 1)
  7818. s2 (match-string 2)
  7819. d1 (time-to-days (org-time-string-to-time s1))
  7820. d2 (time-to-days (org-time-string-to-time s2)))
  7821. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7822. ;; Only allow days between the limits, because the normal
  7823. ;; date stamps will catch the limits.
  7824. (save-excursion
  7825. (setq marker (org-agenda-new-marker (point)))
  7826. (setq category (org-get-category))
  7827. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7828. (progn
  7829. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  7830. (goto-char (match-end 1))
  7831. (setq tags (org-get-tags-at))
  7832. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7833. (setq txt (org-format-agenda-item
  7834. (format (if (= d1 d2) "" "(%d/%d): ")
  7835. (1+ (- d0 d1)) (1+ (- d2 d1)))
  7836. (match-string 1) category tags
  7837. (if (= d0 d1) timestr))))
  7838. (setq txt org-agenda-no-heading-message))
  7839. (org-add-props txt props
  7840. 'org-marker marker 'org-hd-marker hdmarker
  7841. 'priority (org-get-priority txt) 'category category)
  7842. (push txt ee)))
  7843. (goto-char pos)))
  7844. ;; Sort the entries by expiration date.
  7845. (nreverse ee)))
  7846. (defconst org-plain-time-of-day-regexp
  7847. (concat
  7848. "\\(\\<[012]?[0-9]"
  7849. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7850. "\\(--?"
  7851. "\\(\\<[012]?[0-9]"
  7852. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7853. "\\)?")
  7854. "Regular expression to match a plain time or time range.
  7855. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  7856. groups carry important information:
  7857. 0 the full match
  7858. 1 the first time, range or not
  7859. 8 the second time, if it is a range.")
  7860. (defconst org-stamp-time-of-day-regexp
  7861. (concat
  7862. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  7863. "\\([012][0-9]:[0-5][0-9]\\)>"
  7864. "\\(--?"
  7865. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  7866. "Regular expression to match a timestamp time or time range.
  7867. After a match, the following groups carry important information:
  7868. 0 the full match
  7869. 1 date plus weekday, for backreferencing to make sure both times on same day
  7870. 2 the first time, range or not
  7871. 4 the second time, if it is a range.")
  7872. (defvar org-prefix-has-time nil
  7873. "A flag, set by `org-compile-prefix-format'.
  7874. The flag is set if the currently compiled format contains a `%t'.")
  7875. (defvar org-prefix-has-tag nil
  7876. "A flag, set by `org-compile-prefix-format'.
  7877. The flag is set if the currently compiled format contains a `%T'.")
  7878. (defun org-format-agenda-item (extra txt &optional category tags dotime
  7879. noprefix)
  7880. "Format TXT to be inserted into the agenda buffer.
  7881. In particular, it adds the prefix and corresponding text properties. EXTRA
  7882. must be a string and replaces the `%s' specifier in the prefix format.
  7883. CATEGORY (string, symbol or nil) may be used to overrule the default
  7884. category taken from local variable or file name. It will replace the `%c'
  7885. specifier in the format. DOTIME, when non-nil, indicates that a
  7886. time-of-day should be extracted from TXT for sorting of this entry, and for
  7887. the `%t' specifier in the format. When DOTIME is a string, this string is
  7888. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  7889. only the correctly processes TXT should be returned - this is used by
  7890. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  7891. (save-match-data
  7892. ;; Diary entries sometimes have extra whitespace at the beginning
  7893. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  7894. (let* ((category (or category
  7895. org-category
  7896. (if buffer-file-name
  7897. (file-name-sans-extension
  7898. (file-name-nondirectory buffer-file-name))
  7899. "")))
  7900. (tag (if tags (nth (1- (length tags)) tags) ""))
  7901. time ;; needed for the eval of the prefix format
  7902. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  7903. (time-of-day (and dotime (org-get-time-of-day ts)))
  7904. stamp plain s0 s1 s2 rtn)
  7905. (when (and dotime time-of-day org-prefix-has-time)
  7906. ;; Extract starting and ending time and move them to prefix
  7907. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  7908. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  7909. (setq s0 (match-string 0 ts)
  7910. s1 (match-string (if plain 1 2) ts)
  7911. s2 (match-string (if plain 8 4) ts))
  7912. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  7913. ;; them, we might want to remove them there to avoid duplication.
  7914. ;; The user can turn this off with a variable.
  7915. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  7916. (string-match (concat (regexp-quote s0) " *") txt)
  7917. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  7918. (= (match-beginning 0) 0)
  7919. t))
  7920. (setq txt (replace-match "" nil nil txt))))
  7921. ;; Normalize the time(s) to 24 hour
  7922. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  7923. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  7924. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  7925. ;; Tags are in the string
  7926. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  7927. (and org-agenda-remove-tags-when-in-prefix
  7928. org-prefix-has-tag))
  7929. (setq txt (replace-match "" t t txt))
  7930. (setq txt (replace-match
  7931. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  7932. (match-string 2 txt))
  7933. t t txt))))
  7934. ;; Create the final string
  7935. (if noprefix
  7936. (setq rtn txt)
  7937. ;; Prepare the variables needed in the eval of the compiled format
  7938. (setq time (cond (s2 (concat s1 "-" s2))
  7939. (s1 (concat s1 "......"))
  7940. (t ""))
  7941. extra (or extra "")
  7942. category (if (symbolp category) (symbol-name category) category))
  7943. ;; Evaluate the compiled format
  7944. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  7945. ;; And finally add the text properties
  7946. (org-add-props rtn nil
  7947. 'category (downcase category) 'tags tags
  7948. 'prefix-length (- (length rtn) (length txt))
  7949. 'time-of-day time-of-day
  7950. 'dotime dotime))))
  7951. (defvar org-agenda-sorting-strategy)
  7952. (defvar org-agenda-sorting-strategy-selected nil)
  7953. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  7954. (catch 'exit
  7955. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  7956. ((and todayp (member 'today (car org-agenda-time-grid))))
  7957. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  7958. ((member 'weekly (car org-agenda-time-grid)))
  7959. (t (throw 'exit list)))
  7960. (let* ((have (delq nil (mapcar
  7961. (lambda (x) (get-text-property 1 'time-of-day x))
  7962. list)))
  7963. (string (nth 1 org-agenda-time-grid))
  7964. (gridtimes (nth 2 org-agenda-time-grid))
  7965. (req (car org-agenda-time-grid))
  7966. (remove (member 'remove-match req))
  7967. new time)
  7968. (if (and (member 'require-timed req) (not have))
  7969. ;; don't show empty grid
  7970. (throw 'exit list))
  7971. (while (setq time (pop gridtimes))
  7972. (unless (and remove (member time have))
  7973. (setq time (int-to-string time))
  7974. (push (org-format-agenda-item
  7975. nil string "" nil
  7976. (concat (substring time 0 -2) ":" (substring time -2)))
  7977. new)
  7978. (put-text-property
  7979. 1 (length (car new)) 'face 'org-time-grid (car new))))
  7980. (if (member 'time-up org-agenda-sorting-strategy-selected)
  7981. (append new list)
  7982. (append list new)))))
  7983. (defun org-compile-prefix-format (key)
  7984. "Compile the prefix format into a Lisp form that can be evaluated.
  7985. The resulting form is returned and stored in the variable
  7986. `org-prefix-format-compiled'."
  7987. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  7988. (let ((s (cond
  7989. ((stringp org-agenda-prefix-format)
  7990. org-agenda-prefix-format)
  7991. ((assq key org-agenda-prefix-format)
  7992. (cdr (assq key org-agenda-prefix-format)))
  7993. (t " %-12:c%?-12t% s")))
  7994. (start 0)
  7995. varform vars var e c f opt)
  7996. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  7997. s start)
  7998. (setq var (cdr (assoc (match-string 4 s)
  7999. '(("c" . category) ("t" . time) ("s" . extra)
  8000. ("T" . tag))))
  8001. c (or (match-string 3 s) "")
  8002. opt (match-beginning 1)
  8003. start (1+ (match-beginning 0)))
  8004. (if (equal var 'time) (setq org-prefix-has-time t))
  8005. (if (equal var 'tag) (setq org-prefix-has-tag t))
  8006. (setq f (concat "%" (match-string 2 s) "s"))
  8007. (if opt
  8008. (setq varform
  8009. `(if (equal "" ,var)
  8010. ""
  8011. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  8012. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  8013. (setq s (replace-match "%s" t nil s))
  8014. (push varform vars))
  8015. (setq vars (nreverse vars))
  8016. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  8017. (defun org-set-sorting-strategy (key)
  8018. (if (symbolp (car org-agenda-sorting-strategy))
  8019. ;; the old format
  8020. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  8021. (setq org-agenda-sorting-strategy-selected
  8022. (or (cdr (assq key org-agenda-sorting-strategy))
  8023. (cdr (assq 'agenda org-agenda-sorting-strategy))
  8024. '(time-up category-keep priority-down)))))
  8025. (defun org-get-time-of-day (s &optional string mod24)
  8026. "Check string S for a time of day.
  8027. If found, return it as a military time number between 0 and 2400.
  8028. If not found, return nil.
  8029. The optional STRING argument forces conversion into a 5 character wide string
  8030. HH:MM."
  8031. (save-match-data
  8032. (when
  8033. (or
  8034. (string-match
  8035. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  8036. (string-match
  8037. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  8038. (let* ((h (string-to-number (match-string 1 s)))
  8039. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  8040. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  8041. (am-p (equal ampm "am"))
  8042. (h1 (cond ((not ampm) h)
  8043. ((= h 12) (if am-p 0 12))
  8044. (t (+ h (if am-p 0 12)))))
  8045. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  8046. (mod h1 24) h1))
  8047. (t0 (+ (* 100 h2) m))
  8048. (t1 (concat (if (>= h1 24) "+" " ")
  8049. (if (< t0 100) "0" "")
  8050. (if (< t0 10) "0" "")
  8051. (int-to-string t0))))
  8052. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  8053. (defun org-finalize-agenda-entries (list &optional nosort)
  8054. "Sort and concatenate the agenda items."
  8055. (setq list (mapcar 'org-agenda-highlight-todo list))
  8056. (if nosort
  8057. list
  8058. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  8059. (defun org-agenda-highlight-todo (x)
  8060. (let (re pl)
  8061. (if (eq x 'line)
  8062. (save-excursion
  8063. (beginning-of-line 1)
  8064. (setq re (get-text-property (point) 'org-not-done-regexp))
  8065. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  8066. (and (looking-at (concat "[ \t]*\\.*" re))
  8067. (add-text-properties (match-beginning 0) (match-end 0)
  8068. '(face org-todo))))
  8069. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  8070. pl (get-text-property 0 'prefix-length x))
  8071. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  8072. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  8073. '(face org-todo) x))
  8074. x)))
  8075. (defsubst org-cmp-priority (a b)
  8076. "Compare the priorities of string A and B."
  8077. (let ((pa (or (get-text-property 1 'priority a) 0))
  8078. (pb (or (get-text-property 1 'priority b) 0)))
  8079. (cond ((> pa pb) +1)
  8080. ((< pa pb) -1)
  8081. (t nil))))
  8082. (defsubst org-cmp-category (a b)
  8083. "Compare the string values of categories of strings A and B."
  8084. (let ((ca (or (get-text-property 1 'category a) ""))
  8085. (cb (or (get-text-property 1 'category b) "")))
  8086. (cond ((string-lessp ca cb) -1)
  8087. ((string-lessp cb ca) +1)
  8088. (t nil))))
  8089. (defsubst org-cmp-tag (a b)
  8090. "Compare the string values of categories of strings A and B."
  8091. (let ((ta (car (last (get-text-property 1 'tags a))))
  8092. (tb (car (last (get-text-property 1 'tags b)))))
  8093. (cond ((not ta) +1)
  8094. ((not tb) -1)
  8095. ((string-lessp ta tb) -1)
  8096. ((string-lessp tb ta) +1)
  8097. (t nil))))
  8098. (defsubst org-cmp-time (a b)
  8099. "Compare the time-of-day values of strings A and B."
  8100. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  8101. (ta (or (get-text-property 1 'time-of-day a) def))
  8102. (tb (or (get-text-property 1 'time-of-day b) def)))
  8103. (cond ((< ta tb) -1)
  8104. ((< tb ta) +1)
  8105. (t nil))))
  8106. (defun org-entries-lessp (a b)
  8107. "Predicate for sorting agenda entries."
  8108. ;; The following variables will be used when the form is evaluated.
  8109. (let* ((time-up (org-cmp-time a b))
  8110. (time-down (if time-up (- time-up) nil))
  8111. (priority-up (org-cmp-priority a b))
  8112. (priority-down (if priority-up (- priority-up) nil))
  8113. (category-up (org-cmp-category a b))
  8114. (category-down (if category-up (- category-up) nil))
  8115. (category-keep (if category-up +1 nil))
  8116. (tag-up (org-cmp-tag a b))
  8117. (tag-down (if tag-up (- tag-up) nil)))
  8118. (cdr (assoc
  8119. (eval (cons 'or org-agenda-sorting-strategy-selected))
  8120. '((-1 . t) (1 . nil) (nil . nil))))))
  8121. (defun org-agenda-show-priority ()
  8122. "Show the priority of the current item.
  8123. This priority is composed of the main priority given with the [#A] cookies,
  8124. and by additional input from the age of a schedules or deadline entry."
  8125. (interactive)
  8126. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  8127. (message "Priority is %d" (if pri pri -1000))))
  8128. (defun org-agenda-show-tags ()
  8129. "Show the tags applicable to the current item."
  8130. (interactive)
  8131. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  8132. (if tags
  8133. (message "Tags are :%s:"
  8134. (org-no-properties (mapconcat 'identity tags ":")))
  8135. (message "No tags associated with this line"))))
  8136. (defun org-agenda-goto (&optional highlight)
  8137. "Go to the Org-mode file which contains the item at point."
  8138. (interactive)
  8139. (let* ((marker (or (get-text-property (point) 'org-marker)
  8140. (org-agenda-error)))
  8141. (buffer (marker-buffer marker))
  8142. (pos (marker-position marker)))
  8143. (switch-to-buffer-other-window buffer)
  8144. (widen)
  8145. (goto-char pos)
  8146. (when (org-mode-p)
  8147. (org-show-context 'agenda)
  8148. (save-excursion
  8149. (and (outline-next-heading)
  8150. (org-flag-heading nil)))) ; show the next heading
  8151. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  8152. (defun org-agenda-switch-to (&optional delete-other-windows)
  8153. "Go to the Org-mode file which contains the item at point."
  8154. (interactive)
  8155. (let* ((marker (or (get-text-property (point) 'org-marker)
  8156. (org-agenda-error)))
  8157. (buffer (marker-buffer marker))
  8158. (pos (marker-position marker)))
  8159. (switch-to-buffer buffer)
  8160. (and delete-other-windows (delete-other-windows))
  8161. (widen)
  8162. (goto-char pos)
  8163. (when (org-mode-p)
  8164. (org-show-context 'agenda)
  8165. (save-excursion
  8166. (and (outline-next-heading)
  8167. (org-flag-heading nil)))))) ; show the next heading
  8168. (defun org-agenda-goto-mouse (ev)
  8169. "Go to the Org-mode file which contains the item at the mouse click."
  8170. (interactive "e")
  8171. (mouse-set-point ev)
  8172. (org-agenda-goto))
  8173. (defun org-agenda-show ()
  8174. "Display the Org-mode file which contains the item at point."
  8175. (interactive)
  8176. (let ((win (selected-window)))
  8177. (org-agenda-goto t)
  8178. (select-window win)))
  8179. (defun org-agenda-recenter (arg)
  8180. "Display the Org-mode file which contains the item at point and recenter."
  8181. (interactive "P")
  8182. (let ((win (selected-window)))
  8183. (org-agenda-goto t)
  8184. (recenter arg)
  8185. (select-window win)))
  8186. (defun org-agenda-show-mouse (ev)
  8187. "Display the Org-mode file which contains the item at the mouse click."
  8188. (interactive "e")
  8189. (mouse-set-point ev)
  8190. (org-agenda-show))
  8191. (defun org-agenda-check-no-diary ()
  8192. "Check if the entry is a diary link and abort if yes."
  8193. (if (get-text-property (point) 'org-agenda-diary-link)
  8194. (org-agenda-error)))
  8195. (defun org-agenda-error ()
  8196. (error "Command not allowed in this line"))
  8197. (defvar org-last-heading-marker (make-marker)
  8198. "Marker pointing to the headline that last changed its TODO state
  8199. by a remote command from the agenda.")
  8200. (defun org-agenda-todo (&optional arg)
  8201. "Cycle TODO state of line at point, also in Org-mode file.
  8202. This changes the line at point, all other lines in the agenda referring to
  8203. the same tree node, and the headline of the tree node in the Org-mode file."
  8204. (interactive "P")
  8205. (org-agenda-check-no-diary)
  8206. (let* ((col (current-column))
  8207. (marker (or (get-text-property (point) 'org-marker)
  8208. (org-agenda-error)))
  8209. (buffer (marker-buffer marker))
  8210. (pos (marker-position marker))
  8211. (hdmarker (get-text-property (point) 'org-hd-marker))
  8212. (buffer-read-only nil)
  8213. newhead)
  8214. (with-current-buffer buffer
  8215. (widen)
  8216. (goto-char pos)
  8217. (org-show-context 'agenda)
  8218. (save-excursion
  8219. (and (outline-next-heading)
  8220. (org-flag-heading nil))) ; show the next heading
  8221. (org-todo arg)
  8222. (and (bolp) (forward-char 1))
  8223. (setq newhead (org-get-heading))
  8224. (save-excursion
  8225. (org-back-to-heading)
  8226. (move-marker org-last-heading-marker (point))))
  8227. (beginning-of-line 1)
  8228. (save-excursion
  8229. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  8230. (move-to-column col)))
  8231. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  8232. "Change all lines in the agenda buffer which match HDMARKER.
  8233. The new content of the line will be NEWHEAD (as modified by
  8234. `org-format-agenda-item'). HDMARKER is checked with
  8235. `equal' against all `org-hd-marker' text properties in the file.
  8236. If FIXFACE is non-nil, the face of each item is modified acording to
  8237. the new TODO state."
  8238. (let* (props m pl undone-face done-face finish new dotime cat tags)
  8239. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  8240. (save-excursion
  8241. (goto-char (point-max))
  8242. (beginning-of-line 1)
  8243. (while (not finish)
  8244. (setq finish (bobp))
  8245. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  8246. (equal m hdmarker))
  8247. (setq props (text-properties-at (point))
  8248. dotime (get-text-property (point) 'dotime)
  8249. cat (get-text-property (point) 'category)
  8250. tags (get-text-property (point) 'tags)
  8251. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  8252. pl (get-text-property (point) 'prefix-length)
  8253. undone-face (get-text-property (point) 'undone-face)
  8254. done-face (get-text-property (point) 'done-face))
  8255. (move-to-column pl)
  8256. (if (looking-at ".*")
  8257. (progn
  8258. (replace-match new t t)
  8259. (beginning-of-line 1)
  8260. (add-text-properties (point-at-bol) (point-at-eol) props)
  8261. (when fixface
  8262. (add-text-properties
  8263. (point-at-bol) (point-at-eol)
  8264. (list 'face
  8265. (if org-last-todo-state-is-todo
  8266. undone-face done-face)))
  8267. (org-agenda-highlight-todo 'line))
  8268. ;; (org-agenda-align-tags 'line) ;; done below by finalize
  8269. (beginning-of-line 1))
  8270. (error "Line update did not work")))
  8271. (beginning-of-line 0)))
  8272. (org-finalize-agenda)))
  8273. (defun org-agenda-align-tags (&optional line)
  8274. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  8275. (let ((buffer-read-only))
  8276. (save-excursion
  8277. (goto-char (if line (point-at-bol) (point-min)))
  8278. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  8279. (if line (point-at-eol) nil) t)
  8280. (delete-region (match-beginning 1) (match-end 1))
  8281. (goto-char (match-beginning 1))
  8282. (insert (org-add-props
  8283. (make-string (max 1 (- org-agenda-align-tags-to-column
  8284. (current-column))) ?\ )
  8285. (text-properties-at (point))))))))
  8286. (defun org-agenda-priority-up ()
  8287. "Increase the priority of line at point, also in Org-mode file."
  8288. (interactive)
  8289. (org-agenda-priority 'up))
  8290. (defun org-agenda-priority-down ()
  8291. "Decrease the priority of line at point, also in Org-mode file."
  8292. (interactive)
  8293. (org-agenda-priority 'down))
  8294. (defun org-agenda-priority (&optional force-direction)
  8295. "Set the priority of line at point, also in Org-mode file.
  8296. This changes the line at point, all other lines in the agenda referring to
  8297. the same tree node, and the headline of the tree node in the Org-mode file."
  8298. (interactive)
  8299. (org-agenda-check-no-diary)
  8300. (let* ((marker (or (get-text-property (point) 'org-marker)
  8301. (org-agenda-error)))
  8302. (buffer (marker-buffer marker))
  8303. (pos (marker-position marker))
  8304. (hdmarker (get-text-property (point) 'org-hd-marker))
  8305. (buffer-read-only nil)
  8306. newhead)
  8307. (with-current-buffer buffer
  8308. (widen)
  8309. (goto-char pos)
  8310. (org-show-context 'agenda)
  8311. (save-excursion
  8312. (and (outline-next-heading)
  8313. (org-flag-heading nil))) ; show the next heading
  8314. (funcall 'org-priority force-direction)
  8315. (end-of-line 1)
  8316. (setq newhead (org-get-heading)))
  8317. (org-agenda-change-all-lines newhead hdmarker)
  8318. (beginning-of-line 1)))
  8319. (defun org-get-tags-at (&optional pos)
  8320. "Get a list of all headline tags applicable at POS.
  8321. POS defaults to point. If tags are inherited, the list contains
  8322. the targets in the same sequence as the headlines appear, i.e.
  8323. the tags of the current headline come last."
  8324. (interactive)
  8325. (let (tags)
  8326. (save-excursion
  8327. (goto-char (or pos (point)))
  8328. (save-match-data
  8329. (org-back-to-heading t)
  8330. (condition-case nil
  8331. (while t
  8332. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  8333. (setq tags (append (org-split-string
  8334. (org-match-string-no-properties 1) ":")
  8335. tags)))
  8336. (or org-use-tag-inheritance (error ""))
  8337. (org-up-heading-all 1))
  8338. (error nil))))
  8339. tags))
  8340. (defun org-agenda-set-tags ()
  8341. "Set tags for the current headline."
  8342. (interactive)
  8343. (org-agenda-check-no-diary)
  8344. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  8345. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  8346. (org-agenda-error)))
  8347. (buffer (marker-buffer hdmarker))
  8348. (pos (marker-position hdmarker))
  8349. (buffer-read-only nil)
  8350. newhead)
  8351. (with-current-buffer buffer
  8352. (widen)
  8353. (goto-char pos)
  8354. (org-show-context 'agenda)
  8355. (save-excursion
  8356. (and (outline-next-heading)
  8357. (org-flag-heading nil))) ; show the next heading
  8358. (call-interactively 'org-set-tags)
  8359. (end-of-line 1)
  8360. (setq newhead (org-get-heading)))
  8361. (org-agenda-change-all-lines newhead hdmarker)
  8362. (beginning-of-line 1)))
  8363. (defun org-agenda-date-later (arg &optional what)
  8364. "Change the date of this item to one day later."
  8365. (interactive "p")
  8366. (org-agenda-check-type t 'agenda 'timeline)
  8367. (org-agenda-check-no-diary)
  8368. (let* ((marker (or (get-text-property (point) 'org-marker)
  8369. (org-agenda-error)))
  8370. (buffer (marker-buffer marker))
  8371. (pos (marker-position marker)))
  8372. (with-current-buffer buffer
  8373. (widen)
  8374. (goto-char pos)
  8375. (if (not (org-at-timestamp-p))
  8376. (error "Cannot find time stamp"))
  8377. (org-timestamp-change arg (or what 'day))
  8378. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  8379. (defun org-agenda-date-earlier (arg &optional what)
  8380. "Change the date of this item to one day earlier."
  8381. (interactive "p")
  8382. (org-agenda-date-later (- arg) what))
  8383. (defun org-agenda-date-prompt (arg)
  8384. "Change the date of this item. Date is prompted for, with default today.
  8385. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  8386. be used to request time specification in the time stamp."
  8387. (interactive "P")
  8388. (org-agenda-check-type t 'agenda 'timeline)
  8389. (org-agenda-check-no-diary)
  8390. (let* ((marker (or (get-text-property (point) 'org-marker)
  8391. (org-agenda-error)))
  8392. (buffer (marker-buffer marker))
  8393. (pos (marker-position marker)))
  8394. (with-current-buffer buffer
  8395. (widen)
  8396. (goto-char pos)
  8397. (if (not (org-at-timestamp-p))
  8398. (error "Cannot find time stamp"))
  8399. (org-time-stamp arg)
  8400. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  8401. (defun org-agenda-schedule (arg)
  8402. "Schedule the item at point."
  8403. (interactive "P")
  8404. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8405. (org-agenda-check-no-diary)
  8406. (let* ((marker (or (get-text-property (point) 'org-marker)
  8407. (org-agenda-error)))
  8408. (buffer (marker-buffer marker))
  8409. (pos (marker-position marker))
  8410. (org-insert-labeled-timestamps-at-point nil)
  8411. ts)
  8412. (with-current-buffer buffer
  8413. (widen)
  8414. (goto-char pos)
  8415. (setq ts (org-schedule))
  8416. (message "Item scheduled for %s" ts))))
  8417. (defun org-agenda-deadline (arg)
  8418. "Schedule the item at point."
  8419. (interactive "P")
  8420. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  8421. (org-agenda-check-no-diary)
  8422. (let* ((marker (or (get-text-property (point) 'org-marker)
  8423. (org-agenda-error)))
  8424. (buffer (marker-buffer marker))
  8425. (pos (marker-position marker))
  8426. (org-insert-labeled-timestamps-at-point nil)
  8427. ts)
  8428. (with-current-buffer buffer
  8429. (widen)
  8430. (goto-char pos)
  8431. (setq ts (org-deadline))
  8432. (message "Deadline for this item set to %s" ts))))
  8433. (defun org-get-heading ()
  8434. "Return the heading of the current entry, without the stars."
  8435. (save-excursion
  8436. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  8437. (if (and (re-search-backward "[\r\n]\\*" nil t)
  8438. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  8439. (match-string 1)
  8440. "")))
  8441. (defun org-agenda-clock-in (&optional arg)
  8442. "Start the clock on the currently selected item."
  8443. (interactive "P")
  8444. (org-agenda-check-no-diary)
  8445. (let* ((marker (or (get-text-property (point) 'org-marker)
  8446. (org-agenda-error)))
  8447. (pos (marker-position marker)))
  8448. (with-current-buffer (marker-buffer marker)
  8449. (widen)
  8450. (goto-char pos)
  8451. (org-clock-in))))
  8452. (defun org-agenda-diary-entry ()
  8453. "Make a diary entry, like the `i' command from the calendar.
  8454. All the standard commands work: block, weekly etc."
  8455. (interactive)
  8456. (org-agenda-check-type t 'agenda 'timeline)
  8457. (require 'diary-lib)
  8458. (let* ((char (progn
  8459. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  8460. (read-char-exclusive)))
  8461. (cmd (cdr (assoc char
  8462. '((?d . insert-diary-entry)
  8463. (?w . insert-weekly-diary-entry)
  8464. (?m . insert-monthly-diary-entry)
  8465. (?y . insert-yearly-diary-entry)
  8466. (?a . insert-anniversary-diary-entry)
  8467. (?b . insert-block-diary-entry)
  8468. (?c . insert-cyclic-diary-entry)))))
  8469. (oldf (symbol-function 'calendar-cursor-to-date))
  8470. (point (point))
  8471. (mark (or (mark t) (point))))
  8472. (unless cmd
  8473. (error "No command associated with <%c>" char))
  8474. (unless (and (get-text-property point 'day)
  8475. (or (not (equal ?b char))
  8476. (get-text-property mark 'day)))
  8477. (error "Don't know which date to use for diary entry"))
  8478. ;; We implement this by hacking the `calendar-cursor-to-date' function
  8479. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  8480. (let ((calendar-mark-ring
  8481. (list (calendar-gregorian-from-absolute
  8482. (or (get-text-property mark 'day)
  8483. (get-text-property point 'day))))))
  8484. (unwind-protect
  8485. (progn
  8486. (fset 'calendar-cursor-to-date
  8487. (lambda (&optional error)
  8488. (calendar-gregorian-from-absolute
  8489. (get-text-property point 'day))))
  8490. (call-interactively cmd))
  8491. (fset 'calendar-cursor-to-date oldf)))))
  8492. (defun org-agenda-execute-calendar-command (cmd)
  8493. "Execute a calendar command from the agenda, with the date associated to
  8494. the cursor position."
  8495. (org-agenda-check-type t 'agenda 'timeline)
  8496. (require 'diary-lib)
  8497. (unless (get-text-property (point) 'day)
  8498. (error "Don't know which date to use for calendar command"))
  8499. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  8500. (point (point))
  8501. (date (calendar-gregorian-from-absolute
  8502. (get-text-property point 'day)))
  8503. (displayed-day (extract-calendar-day date))
  8504. (displayed-month (extract-calendar-month date))
  8505. (displayed-year (extract-calendar-year date)))
  8506. (unwind-protect
  8507. (progn
  8508. (fset 'calendar-cursor-to-date
  8509. (lambda (&optional error)
  8510. (calendar-gregorian-from-absolute
  8511. (get-text-property point 'day))))
  8512. (call-interactively cmd))
  8513. (fset 'calendar-cursor-to-date oldf))))
  8514. (defun org-agenda-phases-of-moon ()
  8515. "Display the phases of the moon for the 3 months around the cursor date."
  8516. (interactive)
  8517. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  8518. (defun org-agenda-holidays ()
  8519. "Display the holidays for the 3 months around the cursor date."
  8520. (interactive)
  8521. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  8522. (defun org-agenda-sunrise-sunset (arg)
  8523. "Display sunrise and sunset for the cursor date.
  8524. Latitude and longitude can be specified with the variables
  8525. `calendar-latitude' and `calendar-longitude'. When called with prefix
  8526. argument, latitude and longitude will be prompted for."
  8527. (interactive "P")
  8528. (let ((calendar-longitude (if arg nil calendar-longitude))
  8529. (calendar-latitude (if arg nil calendar-latitude))
  8530. (calendar-location-name
  8531. (if arg "the given coordinates" calendar-location-name)))
  8532. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  8533. (defun org-agenda-goto-calendar ()
  8534. "Open the Emacs calendar with the date at the cursor."
  8535. (interactive)
  8536. (org-agenda-check-type t 'agenda 'timeline)
  8537. (let* ((day (or (get-text-property (point) 'day)
  8538. (error "Don't know which date to open in calendar")))
  8539. (date (calendar-gregorian-from-absolute day))
  8540. (calendar-move-hook nil)
  8541. (view-calendar-holidays-initially nil)
  8542. (view-diary-entries-initially nil))
  8543. (calendar)
  8544. (calendar-goto-date date)))
  8545. (defun org-calendar-goto-agenda ()
  8546. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  8547. This is a command that has to be installed in `calendar-mode-map'."
  8548. (interactive)
  8549. (org-agenda-list nil (calendar-absolute-from-gregorian
  8550. (calendar-cursor-to-date))
  8551. nil))
  8552. (defun org-agenda-convert-date ()
  8553. (interactive)
  8554. (org-agenda-check-type t 'agenda 'timeline)
  8555. (let ((day (get-text-property (point) 'day))
  8556. date s)
  8557. (unless day
  8558. (error "Don't know which date to convert"))
  8559. (setq date (calendar-gregorian-from-absolute day))
  8560. (setq s (concat
  8561. "Gregorian: " (calendar-date-string date) "\n"
  8562. "ISO: " (calendar-iso-date-string date) "\n"
  8563. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  8564. "Julian: " (calendar-julian-date-string date) "\n"
  8565. "Astron. JD: " (calendar-astro-date-string date)
  8566. " (Julian date number at noon UTC)\n"
  8567. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  8568. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  8569. "French: " (calendar-french-date-string date) "\n"
  8570. "Mayan: " (calendar-mayan-date-string date) "\n"
  8571. "Coptic: " (calendar-coptic-date-string date) "\n"
  8572. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  8573. "Persian: " (calendar-persian-date-string date) "\n"
  8574. "Chinese: " (calendar-chinese-date-string date) "\n"))
  8575. (with-output-to-temp-buffer "*Dates*"
  8576. (princ s))
  8577. (if (fboundp 'fit-window-to-buffer)
  8578. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  8579. ;;; Tags
  8580. (defun org-scan-tags (action matcher &optional todo-only)
  8581. "Scan headline tags with inheritance and produce output ACTION.
  8582. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8583. evaluated, testing if a given set of tags qualifies a headline for
  8584. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8585. are included in the output."
  8586. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8587. (mapconcat 'regexp-quote
  8588. (nreverse (cdr (reverse org-todo-keywords)))
  8589. "\\|")
  8590. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*$")) ;;FIXME: was [\n\r] instead of $
  8591. (props (list 'face nil
  8592. 'done-face 'org-done
  8593. 'undone-face nil
  8594. 'mouse-face 'highlight
  8595. 'org-not-done-regexp org-not-done-regexp
  8596. 'keymap org-agenda-keymap
  8597. 'help-echo
  8598. (format "mouse-2 or RET jump to org file %s"
  8599. (abbreviate-file-name buffer-file-name))))
  8600. (case-fold-search nil)
  8601. lspos
  8602. tags tags-list tags-alist (llast 0) rtn level category i txt
  8603. todo marker)
  8604. (save-excursion
  8605. (goto-char (point-min))
  8606. (when (eq action 'sparse-tree) (org-overview))
  8607. (while (re-search-forward re nil t)
  8608. (catch :skip
  8609. (and (eq action 'agenda) (org-agenda-skip))
  8610. (setq todo (if (match-end 1) (match-string 2))
  8611. tags (if (match-end 4) (match-string 4)))
  8612. (goto-char (setq lspos (1+ (match-beginning 0))))
  8613. (setq level (funcall outline-level)
  8614. category (org-get-category))
  8615. (setq i llast llast level)
  8616. ;; remove tag lists from same and sublevels
  8617. (while (>= i level)
  8618. (when (setq entry (assoc i tags-alist))
  8619. (setq tags-alist (delete entry tags-alist)))
  8620. (setq i (1- i)))
  8621. ;; add the nex tags
  8622. (when tags
  8623. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8624. tags-alist
  8625. (cons (cons level tags) tags-alist)))
  8626. ;; compile tags for current headline
  8627. (setq tags-list
  8628. (if org-use-tag-inheritance
  8629. (apply 'append (mapcar 'cdr tags-alist))
  8630. tags))
  8631. (when (and (or (not todo-only) todo)
  8632. (eval matcher)
  8633. (or (not org-agenda-skip-archived-trees)
  8634. (not (member org-archive-tag tags-list))))
  8635. ;; list this headline
  8636. (if (eq action 'sparse-tree)
  8637. (progn
  8638. (org-show-context 'tags-tree))
  8639. (setq txt (org-format-agenda-item
  8640. ""
  8641. (concat
  8642. (if org-tags-match-list-sublevels
  8643. (make-string (1- level) ?.) "")
  8644. (org-get-heading))
  8645. category tags-list))
  8646. (goto-char lspos)
  8647. (setq marker (org-agenda-new-marker))
  8648. (org-add-props txt props
  8649. 'org-marker marker 'org-hd-marker marker 'category category)
  8650. (push txt rtn))
  8651. ;; if we are to skip sublevels, jump to end of subtree
  8652. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8653. (when (and (eq action 'sparse-tree)
  8654. (not org-sparse-tree-open-archived-trees))
  8655. (org-hide-archived-subtrees (point-min) (point-max)))
  8656. (nreverse rtn)))
  8657. (defun org-tags-sparse-tree (&optional arg match)
  8658. "Create a sparse tree according to tags string MATCH.
  8659. MATCH can contain positive and negative selection of tags, like
  8660. \"+WORK+URGENT-WITHBOSS\"."
  8661. (interactive "P")
  8662. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))))
  8663. ;; FIXME: remove this function.
  8664. (defun org-make-tags-matcher-old (match)
  8665. "Create the TAGS matcher form for the tags-selecting string MATCH."
  8666. (unless match
  8667. ;; Get a new match request, with completion
  8668. (setq org-last-tags-completion-table
  8669. (or org-tag-alist
  8670. org-last-tags-completion-table))
  8671. (setq match (completing-read
  8672. "Tags: " 'org-tags-completion-function nil nil nil
  8673. 'org-tags-history)))
  8674. ;; parse the string and create a lisp form
  8675. (let ((match0 match) minus tag mm matcher orterms term orlist)
  8676. (setq orterms (org-split-string match "|"))
  8677. (while (setq term (pop orterms))
  8678. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8679. (setq minus (and (match-end 1)
  8680. (equal (match-string 1 term) "-"))
  8681. tag (match-string 2 term)
  8682. term (substring term (match-end 0))
  8683. mm (list 'member (downcase tag) 'tags-list)
  8684. mm (if minus (list 'not mm) mm))
  8685. (push mm matcher))
  8686. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  8687. orlist)
  8688. (setq matcher nil))
  8689. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8690. ;; Return the string and lisp forms of the matcher
  8691. (cons match0 matcher)))
  8692. (defun org-make-tags-matcher (match)
  8693. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8694. (unless match
  8695. ;; Get a new match request, with completion
  8696. (setq org-last-tags-completion-table
  8697. (or org-tag-alist
  8698. org-last-tags-completion-table))
  8699. (setq match (completing-read
  8700. "Match: " 'org-tags-completion-function nil nil nil
  8701. 'org-tags-history))) ; FIXME: SHould we have a separate history for this?
  8702. ;; Parse the string and create a lisp form
  8703. (let ((match0 match) minus tag mm
  8704. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8705. orterms term orlist)
  8706. (if (string-match "/+" match)
  8707. ;; match contains also a todo-matching request
  8708. (setq tagsmatch (substring match 0 (match-beginning 0))
  8709. todomatch (substring match (match-end 0)))
  8710. ;; only matching tags
  8711. (setq tagsmatch match todomatch nil))
  8712. ;; Make the tags matcher
  8713. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8714. (setq tagsmatcher t)
  8715. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8716. (while (setq term (pop orterms))
  8717. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8718. (setq minus (and (match-end 1)
  8719. (equal (match-string 1 term) "-"))
  8720. tag (match-string 2 term)
  8721. term (substring term (match-end 0))
  8722. mm (list 'member (downcase tag) 'tags-list)
  8723. mm (if minus (list 'not mm) mm))
  8724. (push mm tagsmatcher))
  8725. (push (if (> (length tagsmatcher) 1)
  8726. (cons 'and tagsmatcher)
  8727. (car tagsmatcher))
  8728. orlist)
  8729. (setq tagsmatcher nil))
  8730. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist))))
  8731. ;; Make the todo matcher ;; FIXME: reduce syntax richness?
  8732. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8733. (setq todomatcher t)
  8734. (setq orterms (org-split-string todomatch "|") orlist nil)
  8735. (while (setq term (pop orterms))
  8736. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8737. (setq minus (and (match-end 1)
  8738. (equal (match-string 1 term) "-"))
  8739. kwd (match-string 2 term)
  8740. term (substring term (match-end 0))
  8741. mm (list 'equal 'todo kwd)
  8742. mm (if minus (list 'not mm) mm))
  8743. (push mm todomatcher))
  8744. (push (if (> (length todomatcher) 1)
  8745. (cons 'and todomatcher)
  8746. (car todomatcher))
  8747. orlist)
  8748. (setq todomatcher nil))
  8749. (setq todomatcher (if (> (length orlist) 1)
  8750. (cons 'or orlist) (car orlist))))
  8751. ;; Return the string and lisp forms of the matcher
  8752. (setq matcher (if todomatcher
  8753. (list 'and tagsmatcher todomatcher)
  8754. tagsmatcher))
  8755. (cons match0 matcher)))
  8756. ;;;###autoload
  8757. (defun org-tags-view (&optional todo-only match)
  8758. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  8759. The prefix arg TODO-ONLY limits the search to TODO entries."
  8760. (interactive "P")
  8761. (org-compile-prefix-format 'tags)
  8762. (org-set-sorting-strategy 'tags)
  8763. (let* ((org-tags-match-list-sublevels
  8764. (if todo-only t org-tags-match-list-sublevels))
  8765. (win (selected-window))
  8766. (completion-ignore-case t)
  8767. rtn rtnall files file pos matcher
  8768. buffer)
  8769. (setq matcher (org-make-tags-matcher match)
  8770. match (car matcher) matcher (cdr matcher))
  8771. (org-prepare-agenda)
  8772. (setq org-agenda-redo-command
  8773. (list 'org-tags-view (list 'quote todo-only)
  8774. (list 'if 'current-prefix-arg nil match)))
  8775. (setq files (org-agenda-files)
  8776. rtnall nil)
  8777. (while (setq file (pop files))
  8778. (catch 'nextfile
  8779. (org-check-agenda-file file)
  8780. (setq buffer (if (file-exists-p file)
  8781. (org-get-agenda-file-buffer file)
  8782. (error "No such file %s" file)))
  8783. (if (not buffer)
  8784. ;; If file does not exist, merror message to agenda
  8785. (setq rtn (list
  8786. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  8787. rtnall (append rtnall rtn))
  8788. (with-current-buffer buffer
  8789. (unless (org-mode-p)
  8790. (error "Agenda file %s is not in `org-mode'" file))
  8791. (setq org-category-table (org-get-category-table))
  8792. (save-excursion
  8793. (save-restriction
  8794. (if org-agenda-restrict
  8795. (narrow-to-region org-agenda-restrict-begin
  8796. org-agenda-restrict-end)
  8797. (widen))
  8798. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  8799. (setq rtnall (append rtnall rtn))))))))
  8800. (insert "Headlines with TAGS match: ")
  8801. (add-text-properties (point-min) (1- (point))
  8802. (list 'face 'org-level-3))
  8803. (setq pos (point))
  8804. (insert match "\n")
  8805. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  8806. (setq pos (point))
  8807. (unless org-agenda-multi
  8808. (insert "Press `C-u r' to search again with new search string\n"))
  8809. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  8810. (when rtnall
  8811. (insert (org-finalize-agenda-entries rtnall) "\n"))
  8812. (goto-char (point-min))
  8813. (org-fit-agenda-window)
  8814. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  8815. (org-finalize-agenda)
  8816. (setq buffer-read-only t)
  8817. (if (not org-select-agenda-window) (select-window win))))
  8818. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8819. (defvar org-tags-overlay (org-make-overlay 1 1))
  8820. ;(org-overlay-put org-tags-overlay 'face 'org-warning)
  8821. (org-detatch-overlay org-tags-overlay)
  8822. (defun org-set-tags (&optional arg just-align)
  8823. "Set the tags for the current headline.
  8824. With prefix ARG, realign all tags in headings in the current buffer."
  8825. (interactive "P")
  8826. (let* ((re (concat "^" outline-regexp))
  8827. (col (current-column))
  8828. (current (org-get-tags))
  8829. table current-tags inherited-tags ; computed below when needed
  8830. tags empty invis)
  8831. (if arg
  8832. (save-excursion
  8833. (goto-char (point-min))
  8834. (let (buffer-invisibility-spec) ; Emacs 21 compatibility
  8835. (while (re-search-forward re nil t)
  8836. (org-set-tags nil t)))
  8837. (message "All tags realigned to column %d" org-tags-column))
  8838. (if just-align
  8839. (setq tags current)
  8840. (setq table (or org-tag-alist (org-get-buffer-tags))
  8841. org-last-tags-completion-table table
  8842. current-tags (org-split-string current ":")
  8843. inherited-tags (nreverse
  8844. (nthcdr (length current-tags)
  8845. (nreverse (org-get-tags-at))))
  8846. tags
  8847. (if (or (eq t org-use-fast-tag-selection)
  8848. (and org-use-fast-tag-selection
  8849. (delq nil (mapcar 'cdr table))))
  8850. (org-fast-tag-selection current-tags inherited-tags table)
  8851. (let ((org-add-colon-after-tag-completion t))
  8852. (completing-read "Tags: " 'org-tags-completion-function
  8853. nil nil current 'org-tags-history))))
  8854. (while (string-match "[-+&]+" tags)
  8855. (setq tags (replace-match ":" t t tags))))
  8856. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  8857. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8858. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8859. (if (equal current "")
  8860. (progn
  8861. (end-of-line 1)
  8862. (or empty (insert " ")))
  8863. (beginning-of-line 1)
  8864. (setq invis (org-invisible-p))
  8865. (looking-at (concat ".*?\\([ \t]*" (regexp-quote current) "\\)[ \t]*"))
  8866. (delete-region (match-beginning 1) (match-end 1))
  8867. (goto-char (match-beginning 1))
  8868. (insert (if empty "" " ")))
  8869. (if (equal tags "")
  8870. (save-excursion
  8871. (beginning-of-line 1)
  8872. (skip-chars-forward "*")
  8873. (if (= (char-after) ?\ ) (forward-char 1))
  8874. (and (re-search-forward "[ \t]+$" (point-at-eol) t)
  8875. (replace-match "")))
  8876. (let (buffer-invisibility-spec) ; Emacs 21 compatibility
  8877. (move-to-column (max (current-column)
  8878. (if (> org-tags-column 0)
  8879. org-tags-column
  8880. (- (- org-tags-column) (length tags))))
  8881. t))
  8882. (insert tags)
  8883. (if (and (not invis) (org-invisible-p))
  8884. (outline-flag-region (point) (point-at-bol) nil))) ; show
  8885. (move-to-column col))))
  8886. (defun org-tags-completion-function (string predicate &optional flag)
  8887. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8888. (confirm (lambda (x) (stringp (car x)))))
  8889. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8890. (setq s1 (match-string 1 string)
  8891. s2 (match-string 2 string))
  8892. (setq s1 "" s2 string))
  8893. (cond
  8894. ((eq flag nil)
  8895. ;; try completion
  8896. (setq rtn (try-completion s2 ctable confirm))
  8897. (if (stringp rtn)
  8898. (concat s1 s2 (substring rtn (length s2))
  8899. (if (and org-add-colon-after-tag-completion
  8900. (assoc rtn ctable))
  8901. ":" "")))
  8902. )
  8903. ((eq flag t)
  8904. ;; all-completions
  8905. (all-completions s2 ctable confirm)
  8906. )
  8907. ((eq flag 'lambda)
  8908. ;; exact match?
  8909. (assoc s2 ctable)))
  8910. ))
  8911. (defun org-fast-tag-insert (kwd tags face &optional end)
  8912. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8913. (insert (format "%-12s" (concat kwd ":"))
  8914. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8915. (or end "")))
  8916. (defun org-fast-tag-show-exit (flag)
  8917. (save-excursion
  8918. (goto-line 3)
  8919. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8920. (replace-match ""))
  8921. (when flag
  8922. (end-of-line 1)
  8923. (move-to-column (- (window-width) 19) t)
  8924. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8925. (defun org-set-current-tags-overlay (current prefix)
  8926. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8927. (if (featurep 'xemacs)
  8928. (org-overlay-display org-tags-overlay (concat prefix s)
  8929. 'secondary-selection)
  8930. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8931. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8932. (defun org-fast-tag-selection (current inherited table)
  8933. "Fast tag selection with single keys.
  8934. CURRENT is the current list of tags in the headline, INHERITED is the
  8935. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8936. possibly with grouping information.
  8937. If the keys are nil, a-z are automatically assigned.
  8938. Returns the new tags string, or nil to not change the current settings."
  8939. (let* ((maxlen (apply 'max (mapcar
  8940. (lambda (x)
  8941. (if (stringp (car x)) (string-width (car x)) 0))
  8942. table)))
  8943. (buf (current-buffer))
  8944. (buffer-tags nil)
  8945. (fwidth (+ maxlen 3 1 3))
  8946. (ncol (/ (- (window-width) 4) fwidth))
  8947. (i-face 'org-done)
  8948. (c-face 'org-tag)
  8949. tg cnt e c char c1 c2 ntable tbl rtn
  8950. ov-start ov-end ov-prefix
  8951. (exit-after-next org-fast-tag-selection-single-key)
  8952. groups ingroup)
  8953. (save-excursion
  8954. (beginning-of-line 1)
  8955. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  8956. (setq ov-start (match-beginning 1)
  8957. ov-end (match-end 1)
  8958. ov-prefix "")
  8959. (setq ov-start (1- (point-at-eol))
  8960. ov-end (1+ ov-start))
  8961. (skip-chars-forward "^\n\r")
  8962. (setq ov-prefix
  8963. (concat
  8964. (buffer-substring (1- (point)) (point))
  8965. (if (> (current-column) org-tags-column)
  8966. " "
  8967. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8968. (org-move-overlay org-tags-overlay ov-start ov-end)
  8969. (save-window-excursion
  8970. ;; FIXME: would it be better to keep the other windows?
  8971. (delete-other-windows)
  8972. (split-window-vertically)
  8973. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  8974. (erase-buffer)
  8975. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8976. (org-fast-tag-insert "Current" current c-face "\n\n")
  8977. (org-fast-tag-show-exit exit-after-next)
  8978. (org-set-current-tags-overlay current ov-prefix)
  8979. (setq tbl table char ?a cnt 0)
  8980. (while (setq e (pop tbl))
  8981. (cond
  8982. ((equal e '(:startgroup))
  8983. (push '() groups) (setq ingroup t)
  8984. (when (not (= cnt 0))
  8985. (setq cnt 0)
  8986. (insert "\n"))
  8987. (insert "{ "))
  8988. ((equal e '(:endgroup))
  8989. (setq ingroup nil cnt 0)
  8990. (insert "}\n"))
  8991. (t
  8992. (setq tg (car e) c2 nil)
  8993. (if (cdr e)
  8994. (setq c (cdr e))
  8995. ;; automatically assign a character.
  8996. (setq c1 (string-to-char
  8997. (downcase (substring
  8998. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8999. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9000. (while (or (rassoc char ntable) (rassoc char table))
  9001. (setq char (1+ char)))
  9002. (setq c2 c1))
  9003. (setq c (or c2 char)))
  9004. (if ingroup (push tg (car groups)))
  9005. (setq tg (org-add-props tg nil 'face
  9006. (cond
  9007. ((member tg current) c-face)
  9008. ((member tg inherited) i-face)
  9009. (t nil))))
  9010. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9011. (insert "[" c "] " tg (make-string
  9012. (- fwidth 4 (length tg)) ?\ ))
  9013. (push (cons tg c) ntable)
  9014. (when (= (setq cnt (1+ cnt)) ncol)
  9015. (insert "\n")
  9016. (if ingroup (insert " "))
  9017. (setq cnt 0)))))
  9018. (setq ntable (nreverse ntable))
  9019. (insert "\n")
  9020. (goto-char (point-min))
  9021. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  9022. (setq rtn
  9023. (catch 'exit
  9024. (while t
  9025. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [C-c]: multi%s"
  9026. (if groups " [!] no groups" ""))
  9027. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9028. (cond
  9029. ((= c ?\r) (throw 'exit t))
  9030. ((= c ?!)
  9031. (setq groups nil)
  9032. (goto-char (point-min))
  9033. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9034. ((= c ?\C-c)
  9035. (org-fast-tag-show-exit
  9036. (setq exit-after-next (not exit-after-next))))
  9037. ((or (= c ?\C-g)
  9038. (and (= c ?q) (not (rassoc c ntable))))
  9039. (org-detatch-overlay org-tags-overlay)
  9040. (setq quit-flag t))
  9041. ((= c ?\ )
  9042. (setq current nil)
  9043. (if exit-after-next (setq exit-after-next 'now)))
  9044. ((= c ?\t)
  9045. (condition-case nil
  9046. (setq tg (completing-read
  9047. "Tag: "
  9048. (or buffer-tags
  9049. (with-current-buffer buf
  9050. (org-get-buffer-tags)))))
  9051. (quit (setq tg "")))
  9052. (when (string-match "\\S-" tg)
  9053. (add-to-list 'buffer-tags (list tg))
  9054. (if (member tg current)
  9055. (setq current (delete tg current))
  9056. (push tg current)))
  9057. (if exit-after-next (setq exit-after-next 'now)))
  9058. ((setq e (rassoc c ntable) tg (car e))
  9059. (if (member tg current)
  9060. (setq current (delete tg current))
  9061. (loop for g in groups do
  9062. (if (member tg g)
  9063. (mapcar (lambda (x)
  9064. (setq current (delete x current)))
  9065. g)))
  9066. (push tg current))
  9067. (if exit-after-next (setq exit-after-next 'now))))
  9068. ;; Create a sorted list
  9069. (setq current
  9070. (sort current
  9071. (lambda (a b)
  9072. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9073. (if (eq exit-after-next 'now) (throw 'exit t))
  9074. (goto-char (point-min))
  9075. (beginning-of-line 2)
  9076. (delete-region (point) (point-at-eol))
  9077. (org-fast-tag-insert "Current" current c-face)
  9078. (org-set-current-tags-overlay current ov-prefix)
  9079. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  9080. (setq tg (match-string 1))
  9081. (add-text-properties (match-beginning 1) (match-end 1)
  9082. (list 'face
  9083. (cond
  9084. ((member tg current) c-face)
  9085. ((member tg inherited) i-face)
  9086. (t nil)))))
  9087. (goto-char (point-min)))))
  9088. (org-detatch-overlay org-tags-overlay)
  9089. (if rtn
  9090. (mapconcat 'identity current ":")
  9091. nil))))
  9092. (defun org-get-tags ()
  9093. "Get the TAGS string in the current headline."
  9094. (unless (org-on-heading-p t)
  9095. (error "Not on a heading"))
  9096. (save-excursion
  9097. (beginning-of-line 1)
  9098. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  9099. (org-match-string-no-properties 1)
  9100. "")))
  9101. (defun org-get-buffer-tags ()
  9102. "Get a table of all tags used in the buffer, for completion."
  9103. (let (tags)
  9104. (save-excursion
  9105. (goto-char (point-min))
  9106. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  9107. (mapc (lambda (x) (add-to-list 'tags x))
  9108. (org-split-string (org-match-string-no-properties 1) ":"))))
  9109. (mapcar 'list tags)))
  9110. ;;; Link Stuff
  9111. (defvar org-create-file-search-functions nil
  9112. "List of functions to construct the right search string for a file link.
  9113. These functions are called in turn with point at the location to
  9114. which the link should point.
  9115. A function in the hook should first test if it would like to
  9116. handle this file type, for example by checking the major-mode or
  9117. the file extension. If it decides not to handle this file, it
  9118. should just return nil to give other functions a chance. If it
  9119. does handle the file, it must return the search string to be used
  9120. when following the link. The search string will be part of the
  9121. file link, given after a double colon, and `org-open-at-point'
  9122. will automatically search for it. If special measures must be
  9123. taken to make the search successful, another function should be
  9124. added to the companion hook `org-execute-file-search-functions',
  9125. which see.
  9126. A function in this hook may also use `setq' to set the variable
  9127. `description' to provide a suggestion for the descriptive text to
  9128. be used for this link when it gets inserted into an Org-mode
  9129. buffer with \\[org-insert-link].")
  9130. (defvar org-execute-file-search-functions nil
  9131. "List of functions to execute a file search triggered by a link.
  9132. Functions added to this hook must accept a single argument, the
  9133. search string that was part of the file link, the part after the
  9134. double colon. The function must first check if it would like to
  9135. handle this search, for example by checking the major-mode or the
  9136. file extension. If it decides not to handle this search, it
  9137. should just return nil to give other functions a chance. If it
  9138. does handle the search, it must return a non-nil value to keep
  9139. other functions from trying.
  9140. Each function can access the current prefix argument through the
  9141. variable `current-prefix-argument'. Note that a single prefix is
  9142. used to force opening a link in Emacs, so it may be good to only
  9143. use a numeric or double prefix to guide the search function.
  9144. In case this is needed, a function in this hook can also restore
  9145. the window configuration before `org-open-at-point' was called using:
  9146. (set-window-configuration org-window-config-before-follow-link)")
  9147. (defun org-find-file-at-mouse (ev)
  9148. "Open file link or URL at mouse."
  9149. (interactive "e")
  9150. (mouse-set-point ev)
  9151. (org-open-at-point 'in-emacs))
  9152. (defun org-open-at-mouse (ev)
  9153. "Open file link or URL at mouse."
  9154. (interactive "e")
  9155. (mouse-set-point ev)
  9156. (org-open-at-point))
  9157. (defvar org-window-config-before-follow-link nil
  9158. "The window configuration before following a link.
  9159. This is saved in case the need arises to restore it.")
  9160. ;; FIXME: IN-EMACS is used for many purposes, maybe rename this argument???
  9161. (defun org-open-at-point (&optional in-emacs)
  9162. "Open link at or after point.
  9163. If there is no link at point, this function will search forward up to
  9164. the end of the current subtree.
  9165. Normally, files will be opened by an appropriate application. If the
  9166. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  9167. (interactive "P")
  9168. (setq org-window-config-before-follow-link (current-window-configuration))
  9169. (org-remove-occur-highlights nil nil t)
  9170. (if (org-at-timestamp-p t)
  9171. (org-follow-timestamp-link)
  9172. (let (type path link line search (pos (point)))
  9173. (catch 'match
  9174. (save-excursion
  9175. (skip-chars-forward "^]\n\r")
  9176. (when (and (re-search-backward "\\[\\[" nil t)
  9177. (looking-at org-bracket-link-regexp)
  9178. (<= (match-beginning 0) pos)
  9179. (>= (match-end 0) pos))
  9180. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  9181. (while (string-match " *\n *" link)
  9182. (setq link (replace-match " " t t link)))
  9183. (setq link (org-link-expand-abbrev link))
  9184. (if (string-match org-link-re-with-space2 link)
  9185. (setq type (match-string 1 link)
  9186. path (match-string 2 link))
  9187. (setq type "thisfile"
  9188. path link))
  9189. (throw 'match t)))
  9190. (when (get-text-property (point) 'org-linked-text)
  9191. (setq type "thisfile"
  9192. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9193. (1+ (point)) (point))
  9194. path (buffer-substring
  9195. (previous-single-property-change pos 'org-linked-text)
  9196. (next-single-property-change pos 'org-linked-text)))
  9197. (throw 'match t))
  9198. (save-excursion
  9199. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  9200. (if (equal (char-before) ?<) (backward-char 1))
  9201. (when (or (looking-at org-angle-link-re)
  9202. (looking-at org-plain-link-re)
  9203. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  9204. (re-search-forward org-plain-link-re (point-at-eol) t))
  9205. (<= (match-beginning 0) pos)
  9206. (>= (match-end 0) pos)))
  9207. (setq type (match-string 1)
  9208. path (match-string 2))
  9209. (throw 'match t)))
  9210. (save-excursion
  9211. (skip-chars-backward "^ \t\n\r")
  9212. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  9213. (setq type "tags"
  9214. path (match-string 1))
  9215. (while (string-match ":" path)
  9216. (setq path (replace-match "+" t t path)))
  9217. (throw 'match t)))
  9218. (save-excursion
  9219. (skip-chars-backward "a-zA-Z_")
  9220. (when (and (memq 'camel org-activate-links)
  9221. (looking-at org-camel-regexp))
  9222. (setq type "camel" path (match-string 0))
  9223. (if (equal (char-before) ?*)
  9224. (setq path (concat "*" path))))
  9225. (throw 'match t)))
  9226. (unless path
  9227. (error "No link found"))
  9228. ;; Remove any trailing spaces in path
  9229. (if (string-match " +\\'" path)
  9230. (setq path (replace-match "" t t path)))
  9231. (cond
  9232. ((equal type "mailto")
  9233. (let ((cmd (car org-link-mailto-program))
  9234. (args (cdr org-link-mailto-program)) args1
  9235. (address path) (subject "") a)
  9236. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9237. (setq address (match-string 1 path)
  9238. subject (org-link-escape (match-string 2 path))))
  9239. (while args
  9240. (cond
  9241. ((not (stringp (car args))) (push (pop args) args1))
  9242. (t (setq a (pop args))
  9243. (if (string-match "%a" a)
  9244. (setq a (replace-match address t t a)))
  9245. (if (string-match "%s" a)
  9246. (setq a (replace-match subject t t a)))
  9247. (push a args1))))
  9248. (apply cmd (nreverse args1))))
  9249. ((member type '("http" "https" "ftp" "news"))
  9250. (browse-url (concat type ":" path)))
  9251. ((string= type "tags")
  9252. (org-tags-view in-emacs path))
  9253. ((or (string= type "camel")
  9254. (string= type "thisfile"))
  9255. (if in-emacs
  9256. (switch-to-buffer-other-window
  9257. (org-get-buffer-for-internal-link (current-buffer)))
  9258. (org-mark-ring-push))
  9259. (org-link-search
  9260. path
  9261. (cond ((equal in-emacs '(4)) 'occur)
  9262. ((equal in-emacs '(16)) 'org-occur)
  9263. (t nil))))
  9264. ((string= type "file")
  9265. (if (string-match "::\\([0-9]+\\)\\'" path)
  9266. (setq line (string-to-number (match-string 1 path))
  9267. path (substring path 0 (match-beginning 0)))
  9268. (if (string-match "::\\(.+\\)\\'" path)
  9269. (setq search (match-string 1 path)
  9270. path (substring path 0 (match-beginning 0)))))
  9271. (org-open-file path in-emacs line search))
  9272. ((string= type "news")
  9273. (org-follow-gnus-link path))
  9274. ((string= type "bbdb")
  9275. (org-follow-bbdb-link path))
  9276. ((string= type "info")
  9277. (org-follow-info-link path))
  9278. ((string= type "gnus")
  9279. (let (group article)
  9280. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9281. (error "Error in Gnus link"))
  9282. (setq group (match-string 1 path)
  9283. article (match-string 3 path))
  9284. (org-follow-gnus-link group article)))
  9285. ((string= type "vm")
  9286. (let (folder article)
  9287. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9288. (error "Error in VM link"))
  9289. (setq folder (match-string 1 path)
  9290. article (match-string 3 path))
  9291. ;; in-emacs is the prefix arg, will be interpreted as read-only
  9292. (org-follow-vm-link folder article in-emacs)))
  9293. ((string= type "wl")
  9294. (let (folder article)
  9295. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9296. (error "Error in Wanderlust link"))
  9297. (setq folder (match-string 1 path)
  9298. article (match-string 3 path))
  9299. (org-follow-wl-link folder article)))
  9300. ((string= type "mhe")
  9301. (let (folder article)
  9302. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9303. (error "Error in MHE link"))
  9304. (setq folder (match-string 1 path)
  9305. article (match-string 3 path))
  9306. (org-follow-mhe-link folder article)))
  9307. ((string= type "rmail")
  9308. (let (folder article)
  9309. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  9310. (error "Error in RMAIL link"))
  9311. (setq folder (match-string 1 path)
  9312. article (match-string 3 path))
  9313. (org-follow-rmail-link folder article)))
  9314. ((string= type "shell")
  9315. (let ((cmd path))
  9316. (while (string-match "@{" cmd) ; FIXME: not needed for [[]] links
  9317. (setq cmd (replace-match "<" t t cmd)))
  9318. (while (string-match "@}" cmd) ; FIXME: not needed for [[]] links
  9319. (setq cmd (replace-match ">" t t cmd)))
  9320. (if (or (not org-confirm-shell-link-function)
  9321. (funcall org-confirm-shell-link-function
  9322. (format "Execute \"%s\" in shell? "
  9323. (org-add-props cmd nil
  9324. 'face 'org-warning))))
  9325. (progn
  9326. (message "Executing %s" cmd)
  9327. (shell-command cmd))
  9328. (error "Abort"))))
  9329. ((string= type "elisp")
  9330. (let ((cmd path))
  9331. (if (or (not org-confirm-elisp-link-function)
  9332. (funcall org-confirm-elisp-link-function
  9333. (format "Execute \"%s\" as elisp? "
  9334. (org-add-props cmd nil
  9335. 'face 'org-warning))))
  9336. (message "%s => %s" cmd (eval (read cmd)))
  9337. (error "Abort"))))
  9338. (t
  9339. (browse-url-at-point))))))
  9340. (defun org-link-expand-abbrev (link)
  9341. "Apply replacements as defined in `org-link-abbrev-alist."
  9342. (if (string-match "^\\([a-zA-Z]+\\)\\(::\\(.*\\)\\)?$" link)
  9343. (let* ((key (match-string 1 link))
  9344. (as (or (assoc key org-link-abbrev-alist-local)
  9345. (assoc key org-link-abbrev-alist)))
  9346. (tag (and (match-end 2) (match-string 3 link)))
  9347. rpl)
  9348. (if (not as)
  9349. link
  9350. (setq rpl (cdr as))
  9351. (cond
  9352. ((symbolp rpl) (funcall rpl tag))
  9353. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9354. (t (concat rpl tag)))))
  9355. link))
  9356. (defun org-link-search (s &optional type)
  9357. "Search for a link search option.
  9358. When S is a CamelCaseWord, search for a target, or for a sentence containing
  9359. the words. If S is surrounded by forward slashes, it is interpreted as a
  9360. regular expression. In org-mode files, this will create an `org-occur'
  9361. sparse tree. In ordinary files, `occur' will be used to list matches.
  9362. If the current buffer is in `dired-mode', grep will be used to search
  9363. in all files."
  9364. (let ((case-fold-search t)
  9365. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9366. (pos (point))
  9367. (pre "") (post "")
  9368. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  9369. (cond
  9370. ;; First check if there are any special
  9371. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9372. ;; Now try the builtin stuff
  9373. ((save-excursion
  9374. (goto-char (point-min))
  9375. (and
  9376. (re-search-forward
  9377. (concat "<<" (regexp-quote s0) ">>") nil t)
  9378. (setq pos (match-beginning 0))))
  9379. ;; There is an exact target for this
  9380. (goto-char pos))
  9381. ((string-match "^/\\(.*\\)/$" s)
  9382. ;; A regular expression
  9383. (cond
  9384. ((org-mode-p)
  9385. (org-occur (match-string 1 s)))
  9386. ;;((eq major-mode 'dired-mode)
  9387. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9388. (t (org-do-occur (match-string 1 s)))))
  9389. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  9390. t)
  9391. ;; A camel or a normal search string
  9392. (when (equal (string-to-char s) ?*)
  9393. ;; Anchor on headlines, post may include tags.
  9394. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  9395. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  9396. s (substring s 1)))
  9397. (remove-text-properties
  9398. 0 (length s)
  9399. '(face nil mouse-face nil keymap nil fontified nil) s)
  9400. ;; Make a series of regular expressions to find a match
  9401. (setq words
  9402. (if camel
  9403. (org-camel-to-words s)
  9404. (org-split-string s "[ \n\r\t]+"))
  9405. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9406. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  9407. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9408. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9409. re1 (concat pre re2 post)
  9410. re3 (concat pre re4 post)
  9411. re5 (concat pre ".*" re4)
  9412. re2 (concat pre re2)
  9413. re2a (concat pre re2a)
  9414. re4 (concat pre re4)
  9415. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9416. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9417. re5 "\\)"
  9418. ))
  9419. (cond
  9420. ((eq type 'org-occur) (org-occur reall))
  9421. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9422. (t (goto-char (point-min))
  9423. (if (or (org-search-not-link re0 nil t)
  9424. (org-search-not-link re1 nil t)
  9425. (org-search-not-link re2 nil t)
  9426. (org-search-not-link re2a nil t)
  9427. (org-search-not-link re3 nil t)
  9428. (org-search-not-link re4 nil t)
  9429. (org-search-not-link re5 nil t)
  9430. )
  9431. (goto-char (match-beginning 1))
  9432. (goto-char pos)
  9433. (error "No match")))))
  9434. (t
  9435. ;; Normal string-search
  9436. (goto-char (point-min))
  9437. (if (search-forward s nil t)
  9438. (goto-char (match-beginning 0))
  9439. (error "No match"))))
  9440. (and (org-mode-p) (org-show-context 'link-search))))
  9441. (defun org-search-not-link (&rest args)
  9442. "Execute `re-search-forward', but only accept matches that are not a link."
  9443. (catch 'exit
  9444. (let (p1)
  9445. (while (apply 're-search-forward args)
  9446. (setq p1 (point))
  9447. (if (not (save-match-data
  9448. (and (re-search-backward "\\[\\[" nil t)
  9449. (looking-at org-bracket-link-regexp)
  9450. (<= (match-beginning 0) p1)
  9451. (>= (match-end 0) p1))))
  9452. (progn (goto-char (match-end 0))
  9453. (throw 'exit (point)))
  9454. (goto-char (match-end 0)))))))
  9455. (defun org-get-buffer-for-internal-link (buffer)
  9456. "Return a buffer to be used for displaying the link target of internal links."
  9457. (cond
  9458. ((not org-display-internal-link-with-indirect-buffer)
  9459. buffer)
  9460. ((string-match "(Clone)$" (buffer-name buffer))
  9461. (message "Buffer is already a clone, not making another one")
  9462. ;; we also do not modify visibility in this case
  9463. buffer)
  9464. (t ; make a new indirect buffer for displaying the link
  9465. (let* ((bn (buffer-name buffer))
  9466. (ibn (concat bn "(Clone)"))
  9467. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9468. (with-current-buffer ib (org-overview))
  9469. ib))))
  9470. (defun org-do-occur (regexp &optional cleanup)
  9471. "Call the Emacs command `occur'.
  9472. If CLEANUP is non-nil, remove the printout of the regular expression
  9473. in the *Occur* buffer. This is useful if the regex is long and not useful
  9474. to read."
  9475. (occur regexp)
  9476. (when cleanup
  9477. (let ((cwin (selected-window)) win beg end)
  9478. (when (setq win (get-buffer-window "*Occur*"))
  9479. (select-window win))
  9480. (goto-char (point-min))
  9481. (when (re-search-forward "match[a-z]+" nil t)
  9482. (setq beg (match-end 0))
  9483. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9484. (setq end (1- (match-beginning 0)))))
  9485. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  9486. (goto-char (point-min))
  9487. (select-window cwin))))
  9488. (defvar org-mark-ring nil
  9489. "Mark ring for positions before jumps in Org-mode.")
  9490. (defvar org-mark-ring-last-goto nil
  9491. "Last position in the mark ring used to go back.")
  9492. ;; Fill and close the ring
  9493. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9494. (loop for i from 1 to org-mark-ring-length do
  9495. (push (make-marker) org-mark-ring))
  9496. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9497. org-mark-ring)
  9498. (defun org-mark-ring-push (&optional pos buffer)
  9499. "Put the current position or POS into the mark ring and rotate it."
  9500. (interactive)
  9501. (setq pos (or pos (point)))
  9502. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9503. (move-marker (car org-mark-ring)
  9504. (or pos (point))
  9505. (or buffer (current-buffer)))
  9506. (message
  9507. (substitute-command-keys
  9508. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9509. (defun org-mark-ring-goto (&optional n)
  9510. "Jump to the previous position in the mark ring.
  9511. With prefix arg N, jump back that many stored positions. When
  9512. called several times in succession, walk through the entire ring.
  9513. Org-mode commands jumping to a different position in the current file,
  9514. or to another Org-mode file, automatically push the old position
  9515. onto the ring."
  9516. (interactive "p")
  9517. (let (p m)
  9518. (if (eq last-command this-command)
  9519. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9520. (setq p org-mark-ring))
  9521. (setq org-mark-ring-last-goto p)
  9522. (setq m (car p))
  9523. (switch-to-buffer (marker-buffer m))
  9524. (goto-char m)
  9525. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9526. (defun org-camel-to-words (s)
  9527. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  9528. (let ((case-fold-search nil)
  9529. words)
  9530. (while (string-match "[a-z][A-Z]" s)
  9531. (push (substring s 0 (1+ (match-beginning 0))) words)
  9532. (setq s (substring s (1+ (match-beginning 0)))))
  9533. (nreverse (cons s words))))
  9534. (defun org-remove-angle-brackets (s)
  9535. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9536. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9537. s)
  9538. (defun org-add-angle-brackets (s)
  9539. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9540. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9541. s)
  9542. (defun org-follow-timestamp-link ()
  9543. (cond
  9544. ((org-at-date-range-p t)
  9545. (let ((org-agenda-start-on-weekday)
  9546. (t1 (match-string 1))
  9547. (t2 (match-string 2)))
  9548. (setq t1 (time-to-days (org-time-string-to-time t1))
  9549. t2 (time-to-days (org-time-string-to-time t2)))
  9550. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9551. ((org-at-timestamp-p t)
  9552. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9553. (substring (match-string 1) 0 10)))
  9554. 1))
  9555. (t (error "This should not happen"))))
  9556. (defun org-follow-bbdb-link (name)
  9557. "Follow a BBDB link to NAME."
  9558. (require 'bbdb)
  9559. (let ((inhibit-redisplay t)
  9560. (bbdb-electric-p nil))
  9561. (catch 'exit
  9562. ;; Exact match on name
  9563. (bbdb-name (concat "\\`" name "\\'") nil)
  9564. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9565. ;; Exact match on name
  9566. (bbdb-company (concat "\\`" name "\\'") nil)
  9567. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9568. ;; Partial match on name
  9569. (bbdb-name name nil)
  9570. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9571. ;; Partial match on company
  9572. (bbdb-company name nil)
  9573. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  9574. ;; General match including network address and notes
  9575. (bbdb name nil)
  9576. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  9577. (delete-window (get-buffer-window "*BBDB*"))
  9578. (error "No matching BBDB record")))))
  9579. (defun org-follow-info-link (name)
  9580. "Follow an info file & node link to NAME."
  9581. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  9582. (string-match "\\(.*\\)" name))
  9583. (progn
  9584. (require 'info)
  9585. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  9586. (Info-find-node (match-string 1 name) (match-string 2 name))
  9587. (Info-find-node (match-string 1 name) "Top")))
  9588. (message (concat "Could not open: " name))))
  9589. (defun org-follow-gnus-link (&optional group article)
  9590. "Follow a Gnus link to GROUP and ARTICLE."
  9591. (require 'gnus)
  9592. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  9593. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  9594. (if group (gnus-fetch-group group))
  9595. (if article
  9596. (or (gnus-summary-goto-article article nil 'force)
  9597. (if (fboundp 'gnus-summary-insert-cached-articles)
  9598. (progn
  9599. (gnus-summary-insert-cached-articles)
  9600. (gnus-summary-goto-article article nil 'force))
  9601. (message "Message could not be found.")))))
  9602. (defun org-follow-vm-link (&optional folder article readonly)
  9603. "Follow a VM link to FOLDER and ARTICLE."
  9604. (require 'vm)
  9605. (setq article (org-add-angle-brackets article))
  9606. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  9607. ;; ange-ftp or efs or tramp access
  9608. (let ((user (or (match-string 1 folder) (user-login-name)))
  9609. (host (match-string 2 folder))
  9610. (file (match-string 3 folder)))
  9611. (cond
  9612. ((featurep 'tramp)
  9613. ;; use tramp to access the file
  9614. (if (featurep 'xemacs)
  9615. (setq folder (format "[%s@%s]%s" user host file))
  9616. (setq folder (format "/%s@%s:%s" user host file))))
  9617. (t
  9618. ;; use ange-ftp or efs
  9619. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  9620. (setq folder (format "/%s@%s:%s" user host file))))))
  9621. (when folder
  9622. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  9623. (sit-for 0.1)
  9624. (when article
  9625. (vm-select-folder-buffer)
  9626. (widen)
  9627. (let ((case-fold-search t))
  9628. (goto-char (point-min))
  9629. (if (not (re-search-forward
  9630. (concat "^" "message-id: *" (regexp-quote article))))
  9631. (error "Could not find the specified message in this folder"))
  9632. (vm-isearch-update)
  9633. (vm-isearch-narrow)
  9634. (vm-beginning-of-message)
  9635. (vm-summarize)))))
  9636. (defun org-follow-wl-link (folder article)
  9637. "Follow a Wanderlust link to FOLDER and ARTICLE."
  9638. (setq article (org-add-angle-brackets article))
  9639. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  9640. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  9641. (wl-summary-redisplay))
  9642. (defun org-follow-rmail-link (folder article)
  9643. "Follow an RMAIL link to FOLDER and ARTICLE."
  9644. (setq article (org-add-angle-brackets article))
  9645. (let (message-number)
  9646. (save-excursion
  9647. (save-window-excursion
  9648. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9649. (setq message-number
  9650. (save-restriction
  9651. (widen)
  9652. (goto-char (point-max))
  9653. (if (re-search-backward
  9654. (concat "^Message-ID:\\s-+" (regexp-quote
  9655. (or article "")))
  9656. nil t)
  9657. (rmail-what-message))))))
  9658. (if message-number
  9659. (progn
  9660. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9661. (rmail-show-message message-number)
  9662. message-number)
  9663. (error "Message not found"))))
  9664. ;; mh-e integration based on planner-mode
  9665. (defun org-mhe-get-message-real-folder ()
  9666. "Return the name of the current message real folder, so if you use
  9667. sequences, it will now work."
  9668. (save-excursion
  9669. (let* ((folder
  9670. (if (equal major-mode 'mh-folder-mode)
  9671. mh-current-folder
  9672. ;; Refer to the show buffer
  9673. mh-show-folder-buffer))
  9674. (end-index
  9675. (if (boundp 'mh-index-folder)
  9676. (min (length mh-index-folder) (length folder))))
  9677. )
  9678. ;; a simple test on mh-index-data does not work, because
  9679. ;; mh-index-data is always nil in a show buffer.
  9680. (if (and (boundp 'mh-index-folder)
  9681. (string= mh-index-folder (substring folder 0 end-index)))
  9682. (if (equal major-mode 'mh-show-mode)
  9683. (save-window-excursion
  9684. (when (buffer-live-p (get-buffer folder))
  9685. (progn
  9686. (pop-to-buffer folder)
  9687. (org-mhe-get-message-folder-from-index)
  9688. )
  9689. ))
  9690. (org-mhe-get-message-folder-from-index)
  9691. )
  9692. folder
  9693. )
  9694. )))
  9695. (defun org-mhe-get-message-folder-from-index ()
  9696. "Returns the name of the message folder in a index folder buffer."
  9697. (save-excursion
  9698. (mh-index-previous-folder)
  9699. (re-search-forward "^\\(+.*\\)$" nil t)
  9700. (message (match-string 1))))
  9701. (defun org-mhe-get-message-folder ()
  9702. "Return the name of the current message folder. Be careful if you
  9703. use sequences."
  9704. (save-excursion
  9705. (if (equal major-mode 'mh-folder-mode)
  9706. mh-current-folder
  9707. ;; Refer to the show buffer
  9708. mh-show-folder-buffer)))
  9709. (defun org-mhe-get-message-num ()
  9710. "Return the number of the current message. Be careful if you
  9711. use sequences."
  9712. (save-excursion
  9713. (if (equal major-mode 'mh-folder-mode)
  9714. (mh-get-msg-num nil)
  9715. ;; Refer to the show buffer
  9716. (mh-show-buffer-message-number))))
  9717. (defun org-mhe-get-header (header)
  9718. "Return a header of the message in folder mode. This will create a
  9719. show buffer for the corresponding message. If you have a more clever
  9720. idea..."
  9721. (let* ((folder (org-mhe-get-message-folder))
  9722. (num (org-mhe-get-message-num))
  9723. (buffer (get-buffer-create (concat "show-" folder)))
  9724. (header-field))
  9725. (with-current-buffer buffer
  9726. (mh-display-msg num folder)
  9727. (if (equal major-mode 'mh-folder-mode)
  9728. (mh-header-display)
  9729. (mh-show-header-display))
  9730. (set-buffer buffer)
  9731. (setq header-field (mh-get-header-field header))
  9732. (if (equal major-mode 'mh-folder-mode)
  9733. (mh-show)
  9734. (mh-show-show))
  9735. header-field)))
  9736. (defun org-follow-mhe-link (folder article)
  9737. "Follow an MHE link to FOLDER and ARTICLE.
  9738. If ARTICLE is nil FOLDER is shown. If the configuration variable
  9739. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  9740. ARTICLE is searched in all folders. Indexed searches (swish++,
  9741. namazu, and others supported by MH-E) will always search in all
  9742. folders."
  9743. (require 'mh-e)
  9744. (require 'mh-search)
  9745. (require 'mh-utils)
  9746. (mh-find-path)
  9747. (if (not article)
  9748. (mh-visit-folder (mh-normalize-folder-name folder))
  9749. (setq article (org-add-angle-brackets article))
  9750. (mh-search-choose)
  9751. (if (equal mh-searcher 'pick)
  9752. (progn
  9753. (mh-search folder (list "--message-id" article))
  9754. (when (and org-mhe-search-all-folders
  9755. (not (org-mhe-get-message-real-folder)))
  9756. (kill-this-buffer)
  9757. (mh-search "+" (list "--message-id" article))))
  9758. (mh-search "+" article))
  9759. (if (org-mhe-get-message-real-folder)
  9760. (mh-show-msg 1)
  9761. (kill-this-buffer)
  9762. (error "Message not found"))))
  9763. ;; BibTeX links
  9764. ;; Use the custom search meachnism to construct and use search strings for
  9765. ;; file links to BibTeX database entries.
  9766. (defun org-create-file-search-in-bibtex ()
  9767. "Create the search string and description for a BibTeX database entry."
  9768. (when (eq major-mode 'bibtex-mode)
  9769. ;; yes, we want to construct this search string.
  9770. ;; Make a good description for this entry, using names, year and the title
  9771. ;; Put it into the `description' variable which is dynamically scoped.
  9772. (let ((bibtex-autokey-names 1)
  9773. (bibtex-autokey-names-stretch 1)
  9774. (bibtex-autokey-name-case-convert-function 'identity)
  9775. (bibtex-autokey-name-separator " & ")
  9776. (bibtex-autokey-additional-names " et al.")
  9777. (bibtex-autokey-year-length 4)
  9778. (bibtex-autokey-name-year-separator " ")
  9779. (bibtex-autokey-titlewords 3)
  9780. (bibtex-autokey-titleword-separator " ")
  9781. (bibtex-autokey-titleword-case-convert-function 'identity)
  9782. (bibtex-autokey-titleword-length 'infty)
  9783. (bibtex-autokey-year-title-separator ": "))
  9784. (setq description (bibtex-generate-autokey)))
  9785. ;; Now parse the entry, get the key and return it.
  9786. (save-excursion
  9787. (bibtex-beginning-of-entry)
  9788. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  9789. (defun org-execute-file-search-in-bibtex (s)
  9790. "Find the link search string S as a key for a database entry."
  9791. (when (eq major-mode 'bibtex-mode)
  9792. ;; Yes, we want to do the search in this file.
  9793. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  9794. (goto-char (point-min))
  9795. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  9796. (regexp-quote s) "[ \t\n]*,") nil t)
  9797. (goto-char (match-beginning 0)))
  9798. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  9799. ;; Use double prefix to indicate that any web link should be browsed
  9800. (let ((b (current-buffer)) (p (point)))
  9801. ;; Restore the window configuration because we just use the web link
  9802. (set-window-configuration org-window-config-before-follow-link)
  9803. (save-excursion (set-buffer b) (goto-char p)
  9804. (bibtex-url)))
  9805. (recenter 0)) ; Move entry start to beginning of window
  9806. ;; return t to indicate that the search is done.
  9807. t))
  9808. ;; Finally add the functions to the right hooks.
  9809. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  9810. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  9811. ;; end of Bibtex link setup
  9812. (defun org-upgrade-old-links (&optional query-description)
  9813. "Transfer old <...> style links to new [[...]] style links.
  9814. With arg query-description, ask at each match for a description text to use
  9815. for this link."
  9816. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  9817. (save-excursion
  9818. (goto-char (point-min))
  9819. (let ((re (concat "\\([^[]\\)<\\("
  9820. "\\(" (mapconcat 'identity org-link-types "\\|")
  9821. "\\):"
  9822. "[^" org-non-link-chars "]+\\)>"))
  9823. l1 l2 (cnt 0))
  9824. (while (re-search-forward re nil t)
  9825. (setq cnt (1+ cnt)
  9826. l1 (org-match-string-no-properties 2)
  9827. l2 (save-match-data (org-link-escape l1)))
  9828. (when query-description (setq l1 (read-string "Desc: " l1)))
  9829. (if (equal l1 l2)
  9830. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  9831. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  9832. (message "%d matches have beed treated" cnt))))
  9833. (defun org-open-file (path &optional in-emacs line search)
  9834. "Open the file at PATH.
  9835. First, this expands any special file name abbreviations. Then the
  9836. configuration variable `org-file-apps' is checked if it contains an
  9837. entry for this file type, and if yes, the corresponding command is launched.
  9838. If no application is found, Emacs simply visits the file.
  9839. With optional argument IN-EMACS, Emacs will visit the file.
  9840. Optional LINE specifies a line to go to, optional SEARCH a string to
  9841. search for. If LINE or SEARCH is given, the file will always be
  9842. opened in Emacs.
  9843. If the file does not exist, an error is thrown."
  9844. (setq in-emacs (or in-emacs line search))
  9845. (let* ((file (if (equal path "")
  9846. buffer-file-name
  9847. (substitute-in-file-name (expand-file-name path))))
  9848. (apps (append org-file-apps (org-default-apps)))
  9849. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9850. (dirp (if remp nil (file-directory-p file)))
  9851. (dfile (downcase file))
  9852. (old-buffer (current-buffer))
  9853. (old-pos (point))
  9854. (old-mode major-mode)
  9855. ext cmd)
  9856. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9857. (setq ext (match-string 1 dfile))
  9858. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9859. (setq ext (match-string 1 dfile))))
  9860. (if in-emacs
  9861. (setq cmd 'emacs)
  9862. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9863. (and dirp (cdr (assoc 'directory apps)))
  9864. (cdr (assoc ext apps))
  9865. (cdr (assoc t apps)))))
  9866. (when (eq cmd 'mailcap)
  9867. (require 'mailcap)
  9868. (mailcap-parse-mailcaps)
  9869. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9870. (command (mailcap-mime-info mime-type)))
  9871. (if (stringp command)
  9872. (setq cmd command)
  9873. (setq cmd 'emacs))))
  9874. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  9875. (not (file-exists-p file))
  9876. (not org-open-non-existing-files))
  9877. (error "No such file: %s" file))
  9878. (cond
  9879. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9880. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9881. (if (string-match "['\"]%s['\"]" cmd)
  9882. (setq cmd (replace-match "%s" t t cmd)))
  9883. (setq cmd (format cmd (shell-quote-argument file)))
  9884. (save-window-excursion
  9885. (shell-command (concat cmd " &"))))
  9886. ((or (stringp cmd)
  9887. (eq cmd 'emacs))
  9888. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  9889. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  9890. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9891. (if line (goto-line line)
  9892. (if search (org-link-search search))))
  9893. ((consp cmd)
  9894. (eval cmd))
  9895. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9896. (and (org-mode-p) (eq old-mode 'org-mode)
  9897. (or (not (equal old-buffer (current-buffer)))
  9898. (not (equal old-pos (point))))
  9899. (org-mark-ring-push old-pos old-buffer))))
  9900. (defun org-default-apps ()
  9901. "Return the default applications for this operating system."
  9902. (cond
  9903. ((eq system-type 'darwin)
  9904. org-file-apps-defaults-macosx)
  9905. ((eq system-type 'windows-nt)
  9906. org-file-apps-defaults-windowsnt)
  9907. (t org-file-apps-defaults-gnu)))
  9908. (defun org-expand-file-name (path)
  9909. "Replace special path abbreviations and expand the file name."
  9910. (expand-file-name path))
  9911. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9912. (defun org-file-remote-p (file)
  9913. "Test whether FILE specifies a location on a remote system.
  9914. Return non-nil if the location is indeed remote.
  9915. For example, the filename \"/user@host:/foo\" specifies a location
  9916. on the system \"/user@host:\"."
  9917. (cond ((fboundp 'file-remote-p)
  9918. (file-remote-p file))
  9919. ((fboundp 'tramp-handle-file-remote-p)
  9920. (tramp-handle-file-remote-p file))
  9921. ((and (boundp 'ange-ftp-name-format)
  9922. (string-match (car ange-ftp-name-format) file))
  9923. t)
  9924. (t nil)))
  9925. (defvar org-insert-link-history nil
  9926. "Minibuffer history for links inserted with `org-insert-link'.")
  9927. (defvar org-stored-links nil
  9928. "Contains the links stored with `org-store-link'.")
  9929. ;;;###autoload
  9930. (defun org-store-link (arg)
  9931. "\\<org-mode-map>Store an org-link to the current location.
  9932. This link can later be inserted into an org-buffer with
  9933. \\[org-insert-link].
  9934. For some link types, a prefix arg is interpreted:
  9935. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9936. For file links, arg negates `org-context-in-file-links'."
  9937. (interactive "P")
  9938. (let (link cpltxt desc description search txt (pos (point)))
  9939. (cond
  9940. ((eq major-mode 'bbdb-mode)
  9941. (setq cpltxt (concat
  9942. "bbdb:"
  9943. (or (bbdb-record-name (bbdb-current-record))
  9944. (bbdb-record-company (bbdb-current-record))))
  9945. link (org-make-link cpltxt)))
  9946. ((eq major-mode 'Info-mode)
  9947. (setq link (org-make-link "info:"
  9948. (file-name-nondirectory Info-current-file)
  9949. ":" Info-current-node))
  9950. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9951. ":" Info-current-node)))
  9952. ((eq major-mode 'calendar-mode)
  9953. (let ((cd (calendar-cursor-to-date)))
  9954. (setq link
  9955. (format-time-string
  9956. (car org-time-stamp-formats)
  9957. (apply 'encode-time
  9958. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9959. nil nil nil))))))
  9960. ((or (eq major-mode 'vm-summary-mode)
  9961. (eq major-mode 'vm-presentation-mode))
  9962. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9963. (vm-follow-summary-cursor)
  9964. (save-excursion
  9965. (vm-select-folder-buffer)
  9966. (let* ((message (car vm-message-pointer))
  9967. (folder buffer-file-name)
  9968. (subject (vm-su-subject message))
  9969. (author (vm-su-full-name message))
  9970. (message-id (vm-su-message-id message)))
  9971. (setq message-id (org-remove-angle-brackets message-id))
  9972. (setq folder (abbreviate-file-name folder))
  9973. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9974. folder)
  9975. (setq folder (replace-match "" t t folder)))
  9976. (setq cpltxt (concat author " on: " subject))
  9977. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9978. ((eq major-mode 'wl-summary-mode)
  9979. (let* ((msgnum (wl-summary-message-number))
  9980. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9981. msgnum 'message-id))
  9982. (wl-message-entity (elmo-msgdb-overview-get-entity
  9983. msgnum (wl-summary-buffer-msgdb)))
  9984. (author (wl-summary-line-from)) ; FIXME: correct?
  9985. (subject "???")) ; FIXME:
  9986. (setq message-id (org-remove-angle-brackets message-id))
  9987. (setq cpltxt (concat author " on: " subject))
  9988. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9989. "#" message-id))))
  9990. ((or (equal major-mode 'mh-folder-mode)
  9991. (equal major-mode 'mh-show-mode))
  9992. (let ((from-header (org-mhe-get-header "From:"))
  9993. (to-header (org-mhe-get-header "To:"))
  9994. (subject (org-mhe-get-header "Subject:")))
  9995. (setq cpltxt (concat from-header " on: " subject))
  9996. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9997. (org-remove-angle-brackets
  9998. (org-mhe-get-header "Message-Id:"))))))
  9999. ((eq major-mode 'rmail-mode)
  10000. (save-excursion
  10001. (save-restriction
  10002. (rmail-narrow-to-non-pruned-header)
  10003. (let ((folder buffer-file-name)
  10004. (message-id (mail-fetch-field "message-id"))
  10005. (author (mail-fetch-field "from"))
  10006. (subject (mail-fetch-field "subject")))
  10007. (setq message-id (org-remove-angle-brackets message-id))
  10008. (setq cpltxt (concat author " on: " subject))
  10009. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10010. ((eq major-mode 'gnus-group-mode)
  10011. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10012. (gnus-group-group-name)) ; version
  10013. ((fboundp 'gnus-group-name)
  10014. (gnus-group-name))
  10015. (t "???"))))
  10016. (setq cpltxt (concat
  10017. (if (org-xor arg org-usenet-links-prefer-google)
  10018. "http://groups.google.com/groups?group="
  10019. "gnus:")
  10020. group)
  10021. link (org-make-link cpltxt))))
  10022. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10023. (require 'gnus-sum)
  10024. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10025. (gnus-summary-beginning-of-article)
  10026. (let* ((group (car gnus-article-current))
  10027. (article (cdr gnus-article-current))
  10028. (header (gnus-summary-article-header article))
  10029. (author (mail-header-from header))
  10030. (message-id (mail-header-id header))
  10031. (date (mail-header-date header))
  10032. (subject (gnus-summary-subject-string)))
  10033. (setq cpltxt (concat author " on: " subject))
  10034. (if (org-xor arg org-usenet-links-prefer-google)
  10035. (setq link
  10036. (concat
  10037. cpltxt "\n "
  10038. (format "http://groups.google.com/groups?as_umsgid=%s"
  10039. (org-fixup-message-id-for-http message-id))))
  10040. (setq link (org-make-link "gnus:" group
  10041. "#" (number-to-string article))))))
  10042. ((eq major-mode 'w3-mode)
  10043. (setq cpltxt (url-view-url t)
  10044. link (org-make-link cpltxt)))
  10045. ((eq major-mode 'w3m-mode)
  10046. (setq cpltxt (or w3m-current-title w3m-current-url)
  10047. link (org-make-link w3m-current-url)))
  10048. ((setq search (run-hook-with-args-until-success
  10049. 'org-create-file-search-functions))
  10050. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10051. "::" search))
  10052. (setq cpltxt (or description link)))
  10053. ((eq major-mode 'image-mode)
  10054. (setq cpltxt (concat "file:"
  10055. (abbreviate-file-name buffer-file-name))
  10056. link (org-make-link cpltxt)))
  10057. ((eq major-mode 'dired-mode)
  10058. ;; link to the file in the current line
  10059. (setq cpltxt (concat "file:"
  10060. (abbreviate-file-name
  10061. (expand-file-name
  10062. (dired-get-filename nil t))))
  10063. link (org-make-link cpltxt)))
  10064. ((and buffer-file-name (org-mode-p))
  10065. ;; Just link to current headline
  10066. (setq cpltxt (concat "file:"
  10067. (abbreviate-file-name buffer-file-name)))
  10068. ;; Add a context search string
  10069. (when (org-xor org-context-in-file-links arg)
  10070. ;; Check if we are on a target
  10071. (if (save-excursion
  10072. (skip-chars-forward "^>\n\r")
  10073. (and (re-search-backward "<<" nil t)
  10074. (looking-at "<<\\(.*?\\)>>")
  10075. (<= (match-beginning 0) pos)
  10076. (>= (match-end 0) pos)))
  10077. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10078. (setq txt (cond
  10079. ((org-on-heading-p) nil)
  10080. ((org-region-active-p)
  10081. (buffer-substring (region-beginning) (region-end)))
  10082. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10083. (when (or (null txt) (string-match "\\S-" txt))
  10084. (setq cpltxt
  10085. (concat cpltxt "::"
  10086. (if org-file-link-context-use-camel-case
  10087. (org-make-org-heading-camel txt)
  10088. (org-make-org-heading-search-string txt)))
  10089. desc "NONE"))))
  10090. (if (string-match "::\\'" cpltxt)
  10091. (setq cpltxt (substring cpltxt 0 -2)))
  10092. (setq link (org-make-link cpltxt)))
  10093. (buffer-file-name
  10094. ;; Just link to this file here.
  10095. (setq cpltxt (concat "file:"
  10096. (abbreviate-file-name buffer-file-name)))
  10097. ;; Add a context string
  10098. (when (org-xor org-context-in-file-links arg)
  10099. (setq txt (if (org-region-active-p)
  10100. (buffer-substring (region-beginning) (region-end))
  10101. (buffer-substring (point-at-bol) (point-at-eol))))
  10102. ;; Only use search option if there is some text.
  10103. (when (string-match "\\S-" txt)
  10104. (setq cpltxt
  10105. (concat cpltxt "::"
  10106. (if org-file-link-context-use-camel-case
  10107. (org-make-org-heading-camel txt)
  10108. (org-make-org-heading-search-string txt)))
  10109. desc "NONE")))
  10110. (setq link (org-make-link cpltxt)))
  10111. ((interactive-p)
  10112. (error "Cannot link to a buffer which is not visiting a file"))
  10113. (t (setq link nil)))
  10114. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10115. (setq link (or link cpltxt)
  10116. desc (or desc cpltxt))
  10117. (if (equal desc "NONE") (setq desc nil))
  10118. (if (and (interactive-p) link)
  10119. (progn
  10120. (setq org-stored-links
  10121. (cons (list cpltxt link desc) org-stored-links))
  10122. (message "Stored: %s" (or cpltxt link)))
  10123. (org-make-link-string link desc))))
  10124. (defun org-make-org-heading-search-string (&optional string heading)
  10125. "Make search string for STRING or current headline."
  10126. (interactive)
  10127. (let ((s (or string (org-get-heading))))
  10128. (unless (and string (not heading))
  10129. ;; We are using a headline, clean up garbage in there.
  10130. (if (string-match org-todo-regexp s)
  10131. (setq s (replace-match "" t t s)))
  10132. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  10133. (setq s (replace-match "" t t s)))
  10134. (setq s (org-trim s))
  10135. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10136. org-comment-string "\\)") s)
  10137. (setq s (replace-match "" t t s)))
  10138. (while (string-match org-ts-regexp s)
  10139. (setq s (replace-match "" t t s))))
  10140. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10141. (setq s (replace-match " " t t s)))
  10142. (or string (setq s (concat "*" s))) ; Add * for headlines
  10143. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10144. (defun org-make-org-heading-camel (&optional string heading)
  10145. "Make a CamelCase string for STRING or the current headline."
  10146. (interactive)
  10147. (let ((s (or string (org-get-heading))))
  10148. (unless (and string (not heading))
  10149. ;; We are using a headline, clean up garbage in there.
  10150. (if (string-match org-todo-regexp s)
  10151. (setq s (replace-match "" t t s)))
  10152. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  10153. (setq s (replace-match "" t t s)))
  10154. (setq s (org-trim s))
  10155. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10156. org-comment-string "\\)") s)
  10157. (setq s (replace-match "" t t s)))
  10158. (while (string-match org-ts-regexp s)
  10159. (setq s (replace-match "" t t s))))
  10160. (while (string-match "[^a-zA-Z_ \t]+" s)
  10161. (setq s (replace-match " " t t s)))
  10162. (or string (setq s (concat "*" s))) ; Add * for headlines
  10163. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  10164. (defun org-make-link (&rest strings)
  10165. "Concatenate STRINGS, format resulting string with `org-link-format'."
  10166. (format org-link-format (apply 'concat strings)))
  10167. (defun org-make-link-string (link &optional description)
  10168. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10169. (if (eq org-link-style 'plain)
  10170. (if (equal description link)
  10171. link
  10172. (concat description "\n" link))
  10173. (when (stringp description)
  10174. ;; Remove brackets from the description, they are fatal.
  10175. (while (string-match "\\[\\|\\]" description)
  10176. (setq description (replace-match "" t t description))))
  10177. (when (equal (org-link-escape link) description)
  10178. ;; No description needed, it is identical
  10179. (setq description nil))
  10180. (when (and (not description)
  10181. (not (equal link (org-link-escape link))))
  10182. (setq description link))
  10183. (concat "[[" (org-link-escape link) "]"
  10184. (if description (concat "[" description "]") "")
  10185. "]")))
  10186. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  10187. "Association list of escapes for some characters problematic in links.")
  10188. (defun org-link-escape (text)
  10189. "Escape charaters in TEXT that are problematic for links."
  10190. (when text
  10191. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10192. org-link-escape-chars "\\|")))
  10193. (while (string-match re text)
  10194. (setq text
  10195. (replace-match
  10196. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  10197. t t text)))
  10198. text)))
  10199. (defun org-link-unescape (text)
  10200. "Reverse the action of `org-link-escape'."
  10201. (when text
  10202. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10203. org-link-escape-chars "\\|")))
  10204. (while (string-match re text)
  10205. (setq text
  10206. (replace-match
  10207. (car (rassoc (match-string 0 text) org-link-escape-chars))
  10208. t t text)))
  10209. text)))
  10210. (defun org-xor (a b)
  10211. "Exclusive or."
  10212. (if a (not b) b))
  10213. (defun org-get-header (header)
  10214. "Find a header field in the current buffer."
  10215. (save-excursion
  10216. (goto-char (point-min))
  10217. (let ((case-fold-search t) s)
  10218. (cond
  10219. ((eq header 'from)
  10220. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10221. (setq s (match-string 1)))
  10222. (while (string-match "\"" s)
  10223. (setq s (replace-match "" t t s)))
  10224. (if (string-match "[<(].*" s)
  10225. (setq s (replace-match "" t t s))))
  10226. ((eq header 'message-id)
  10227. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10228. (setq s (match-string 1))))
  10229. ((eq header 'subject)
  10230. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10231. (setq s (match-string 1)))))
  10232. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10233. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10234. s)))
  10235. (defun org-fixup-message-id-for-http (s)
  10236. "Replace special characters in a message id, so it can be used in an http query."
  10237. (while (string-match "<" s)
  10238. (setq s (replace-match "%3C" t t s)))
  10239. (while (string-match ">" s)
  10240. (setq s (replace-match "%3E" t t s)))
  10241. (while (string-match "@" s)
  10242. (setq s (replace-match "%40" t t s)))
  10243. s)
  10244. (defun org-insert-link (&optional complete-file)
  10245. "Insert a link. At the prompt, enter the link.
  10246. Completion can be used to select a link previously stored with
  10247. `org-store-link'. When the empty string is entered (i.e. if you just
  10248. press RET at the prompt), the link defaults to the most recently
  10249. stored link. As SPC triggers completion in the minibuffer, you need to
  10250. use M-SPC or C-q SPC to force the insertion of a space character.
  10251. You will also be prompted for a description, and if one is given, it will
  10252. be displayed in the buffer instead of the link.
  10253. If there is already a link at point, this command will allow you to edit link
  10254. and description parts.
  10255. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10256. selected using completion. The path to the file will be relative to
  10257. the current directory if the file is in the current directory or a
  10258. subdirectory. Otherwise, the link will be the absolute path as
  10259. completed in the minibuffer (i.e. normally ~/path/to/file).
  10260. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10261. is in the current directory or below.
  10262. With three \\[universal-argument] prefixes, negate the meaning of
  10263. `org-keep-stored-link-after-insertion'."
  10264. (interactive "P")
  10265. (let (link desc entry remove file (pos (point)))
  10266. (cond
  10267. ((save-excursion
  10268. (skip-chars-forward "^]\n\r")
  10269. (and (re-search-backward "\\[\\[" nil t)
  10270. (looking-at org-bracket-link-regexp)
  10271. (<= (match-beginning 0) pos)
  10272. (>= (match-end 0) pos)))
  10273. ;; We do have a link at point, and we are going to edit it.
  10274. (setq remove (list (match-beginning 0) (match-end 0)))
  10275. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10276. (setq link (read-string "Link: "
  10277. (org-link-unescape
  10278. (org-match-string-no-properties 1)))))
  10279. ((equal complete-file '(4))
  10280. ;; Completing read for file names.
  10281. (setq file (read-file-name "File: "))
  10282. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10283. (pwd1 (file-name-as-directory (abbreviate-file-name
  10284. (expand-file-name ".")))))
  10285. (cond
  10286. ((equal complete-file '(16))
  10287. (setq link (org-make-link
  10288. "file:"
  10289. (abbreviate-file-name (expand-file-name file)))))
  10290. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10291. (setq link (org-make-link "file:" (match-string 1 file))))
  10292. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10293. (expand-file-name file))
  10294. (setq link (org-make-link
  10295. "file:" (match-string 1 (expand-file-name file)))))
  10296. (t (setq link (org-make-link "file:" file))))))
  10297. (t
  10298. ;; Read link, with completion for stored links.
  10299. (setq link (org-completing-read
  10300. "Link: " org-stored-links nil nil nil
  10301. org-insert-link-history
  10302. (or (car (car org-stored-links)))))
  10303. (setq entry (assoc link org-stored-links))
  10304. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10305. (not org-keep-stored-link-after-insertion))
  10306. (setq org-stored-links (delq (assoc link org-stored-links)
  10307. org-stored-links)))
  10308. (setq link (if entry (nth 1 entry) link)
  10309. desc (or desc (nth 2 entry)))))
  10310. (if (string-match org-plain-link-re link)
  10311. ;; URL-like link, normalize the use of angular brackets.
  10312. (setq link (org-make-link (org-remove-angle-brackets link))))
  10313. ;; Check if we are linking to the current file with a search option
  10314. ;; If yes, simplify the link by using only the search option.
  10315. (when (and buffer-file-name
  10316. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10317. (let* ((path (match-string 1 link))
  10318. (case-fold-search nil)
  10319. (search (match-string 2 link)))
  10320. (save-match-data
  10321. (if (equal (file-truename buffer-file-name) (file-truename path))
  10322. ;; We are linking to this same file, with a search option
  10323. (setq link search)))))
  10324. ;; Check if we can/should use a relative path. If yes, simplify the link
  10325. (when (string-match "\\<file:\\(.*\\)" link)
  10326. (let* ((path (match-string 1 link))
  10327. (case-fold-search nil))
  10328. (cond
  10329. ((eq org-link-file-path-type 'absolute)
  10330. (setq path (abbreviate-file-name (expand-file-name path))))
  10331. ((eq org-link-file-path-type 'noabbrev)
  10332. (setq path (expand-file-name path)))
  10333. ((eq org-link-file-path-type 'relative)
  10334. (setq path (file-relative-name path)))
  10335. (t
  10336. (save-match-data
  10337. (if (string-match (concat "^" (regexp-quote
  10338. (file-name-as-directory
  10339. (expand-file-name "."))))
  10340. (expand-file-name path))
  10341. ;; We are linking a file with relative path name.
  10342. (setq path (substring (expand-file-name path)
  10343. (match-end 0)))))))
  10344. (setq link (concat "file:" path))))
  10345. (setq desc (read-string "Description: " desc))
  10346. (unless (string-match "\\S-" desc) (setq desc nil))
  10347. (if remove (apply 'delete-region remove))
  10348. (insert (org-make-link-string link desc))))
  10349. (defun org-completing-read (&rest args)
  10350. (let ((minibuffer-local-completion-map
  10351. (copy-keymap minibuffer-local-completion-map)))
  10352. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  10353. (apply 'completing-read args)))
  10354. ;;; Hooks for remember.el
  10355. (defvar org-finish-function nil)
  10356. ;;;###autoload
  10357. (defun org-remember-annotation ()
  10358. "Return a link to the current location as an annotation for remember.el.
  10359. If you are using Org-mode files as target for data storage with
  10360. remember.el, then the annotations should include a link compatible with the
  10361. conventions in Org-mode. This function returns such a link."
  10362. (org-store-link nil))
  10363. (defconst org-remember-help
  10364. "Select a destination location for the note.
  10365. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10366. RET at beg-of-buf -> Append to file as level 2 headline
  10367. RET on headline -> Store as sublevel entry to current headline
  10368. <left>/<right> -> before/after current headline, same headings level")
  10369. ;;;###autoload
  10370. (defun org-remember-apply-template ()
  10371. "Initialize *remember* buffer with template, invoke `org-mode'.
  10372. This function should be placed into `remember-mode-hook' and in fact requires
  10373. to be run from that hook to fucntion properly."
  10374. (if org-remember-templates
  10375. (let* ((entry (if (= (length org-remember-templates) 1)
  10376. (cdar org-remember-templates)
  10377. (message "Select template: %s"
  10378. (mapconcat
  10379. (lambda (x) (char-to-string (car x)))
  10380. org-remember-templates " "))
  10381. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  10382. (tpl (car entry))
  10383. (file (if (consp (cdr entry)) (nth 1 entry)))
  10384. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10385. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10386. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10387. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10388. (v-a annotation) ; defined in `remember-mode'
  10389. (v-i initial) ; defined in `remember-mode'
  10390. (v-n user-full-name)
  10391. )
  10392. (unless tpl (setq tpl "") (message "No template") (ding))
  10393. (insert tpl) (goto-char (point-min))
  10394. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  10395. (when (and initial (equal (match-string 0) "%i"))
  10396. (save-match-data
  10397. (let* ((lead (buffer-substring
  10398. (point-at-bol) (match-beginning 0))))
  10399. (setq v-i (mapconcat 'identity
  10400. (org-split-string initial "\n")
  10401. (concat "\n" lead))))))
  10402. (replace-match
  10403. (or (eval (intern (concat "v-" (match-string 1)))) "")
  10404. t t))
  10405. (let ((org-startup-folded nil)
  10406. (org-startup-with-deadline-check nil))
  10407. (org-mode))
  10408. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  10409. (org-set-local 'org-default-notes-file file))
  10410. (goto-char (point-min))
  10411. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  10412. (let ((org-startup-folded nil)
  10413. (org-startup-with-deadline-check nil))
  10414. (org-mode)))
  10415. (org-set-local 'org-finish-function 'remember-buffer))
  10416. ;;;###autoload
  10417. (defun org-remember-handler ()
  10418. "Store stuff from remember.el into an org file.
  10419. First prompts for an org file. If the user just presses return, the value
  10420. of `org-default-notes-file' is used.
  10421. Then the command offers the headings tree of the selected file in order to
  10422. file the text at a specific location.
  10423. You can either immediately press RET to get the note appended to the
  10424. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  10425. find a better place. Then press RET or <left> or <right> in insert the note.
  10426. Key Cursor position Note gets inserted
  10427. -----------------------------------------------------------------------------
  10428. RET buffer-start as level 2 heading at end of file
  10429. RET on headline as sublevel of the heading at cursor
  10430. RET no heading at cursor position, level taken from context.
  10431. Or use prefix arg to specify level manually.
  10432. <left> on headline as same level, before current heading
  10433. <right> on headline as same level, after current heading
  10434. So the fastest way to store the note is to press RET RET to append it to
  10435. the default file. This way your current train of thought is not
  10436. interrupted, in accordance with the principles of remember.el. But with
  10437. little extra effort, you can push it directly to the correct location.
  10438. Before being stored away, the function ensures that the text has a
  10439. headline, i.e. a first line that starts with a \"*\". If not, a headline
  10440. is constructed from the current date and some additional data.
  10441. If the variable `org-adapt-indentation' is non-nil, the entire text is
  10442. also indented so that it starts in the same column as the headline
  10443. \(i.e. after the stars).
  10444. See also the variable `org-reverse-note-order'."
  10445. (catch 'quit
  10446. (let* ((txt (buffer-substring (point-min) (point-max)))
  10447. (fastp current-prefix-arg)
  10448. (file (if fastp org-default-notes-file (org-get-org-file)))
  10449. (visiting (find-buffer-visiting file))
  10450. (org-startup-with-deadline-check nil)
  10451. (org-startup-folded nil)
  10452. (org-startup-align-all-tables nil)
  10453. spos level indent reversed)
  10454. ;; Modify text so that it becomes a nice subtree which can be inserted
  10455. ;; into an org tree.
  10456. (let* ((lines (split-string txt "\n"))
  10457. first)
  10458. ;; remove empty lines at the beginning
  10459. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  10460. (setq lines (cdr lines)))
  10461. (setq first (car lines) lines (cdr lines))
  10462. (if (string-match "^\\*+" first)
  10463. ;; Is already a headline
  10464. (setq indent nil)
  10465. ;; We need to add a headline: Use time and first buffer line
  10466. (setq lines (cons first lines)
  10467. first (concat "* " (current-time-string)
  10468. " (" (remember-buffer-desc) ")")
  10469. indent " "))
  10470. (if (and org-adapt-indentation indent)
  10471. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  10472. (setq txt (concat first "\n"
  10473. (mapconcat 'identity lines "\n"))))
  10474. ;; Find the file
  10475. (if (not visiting)
  10476. (find-file-noselect file))
  10477. (with-current-buffer (get-file-buffer file)
  10478. (save-excursion (and (goto-char (point-min))
  10479. (not (re-search-forward "^\\* " nil t))
  10480. (insert "\n* Notes\n")))
  10481. (setq reversed (org-notes-order-reversed-p))
  10482. (save-excursion
  10483. (save-restriction
  10484. (widen)
  10485. ;; Ask the User for a location
  10486. (setq spos (if fastp 1 (org-get-location
  10487. (current-buffer)
  10488. org-remember-help)))
  10489. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  10490. ; not handle this note
  10491. (goto-char spos)
  10492. (cond ((bobp)
  10493. ;; Put it at the start or end, as level 2
  10494. (save-restriction
  10495. (widen)
  10496. (goto-char (if reversed (point-min) (point-max)))
  10497. (if (not (bolp)) (newline))
  10498. (org-paste-subtree 2 txt)))
  10499. ((and (org-on-heading-p nil) (not current-prefix-arg))
  10500. ;; Put it below this entry, at the beg/end of the subtree
  10501. (org-back-to-heading)
  10502. (setq level (funcall outline-level))
  10503. (if reversed
  10504. (outline-end-of-heading)
  10505. (outline-end-of-subtree))
  10506. (if (not (bolp)) (newline))
  10507. (beginning-of-line 1)
  10508. (org-paste-subtree (org-get-legal-level level 1) txt))
  10509. (t
  10510. ;; Put it right there, with automatic level determined by
  10511. ;; org-paste-subtree or from prefix arg
  10512. (org-paste-subtree current-prefix-arg txt)))
  10513. (when remember-save-after-remembering
  10514. (save-buffer)
  10515. (if (not visiting) (kill-buffer (current-buffer)))))))))
  10516. t) ;; return t to indicate that we took care of this note.
  10517. (defun org-get-org-file ()
  10518. "Read a filename, with default directory `org-directory'."
  10519. (let ((default (or org-default-notes-file remember-data-file)))
  10520. (read-file-name (format "File name [%s]: " default)
  10521. (file-name-as-directory org-directory)
  10522. default)))
  10523. (defun org-notes-order-reversed-p ()
  10524. "Check if the current file should receive notes in reversed order."
  10525. (cond
  10526. ((not org-reverse-note-order) nil)
  10527. ((eq t org-reverse-note-order) t)
  10528. ((not (listp org-reverse-note-order)) nil)
  10529. (t (catch 'exit
  10530. (let ((all org-reverse-note-order)
  10531. entry)
  10532. (while (setq entry (pop all))
  10533. (if (string-match (car entry) buffer-file-name)
  10534. (throw 'exit (cdr entry))))
  10535. nil)))))
  10536. ;;; Tables
  10537. ;; Watch out: Here we are talking about two different kind of tables.
  10538. ;; Most of the code is for the tables created with the Org-mode table editor.
  10539. ;; Sometimes, we talk about tables created and edited with the table.el
  10540. ;; Emacs package. We call the former org-type tables, and the latter
  10541. ;; table.el-type tables.
  10542. (defun org-before-change-function (beg end)
  10543. "Every change indicates that a table might need an update."
  10544. (setq org-table-may-need-update t))
  10545. (defconst org-table-line-regexp "^[ \t]*|"
  10546. "Detects an org-type table line.")
  10547. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  10548. "Detects an org-type table line.")
  10549. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  10550. "Detects a table line marked for automatic recalculation.")
  10551. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  10552. "Detects a table line marked for automatic recalculation.")
  10553. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  10554. "Detects a table line marked for automatic recalculation.")
  10555. (defconst org-table-hline-regexp "^[ \t]*|-"
  10556. "Detects an org-type table hline.")
  10557. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  10558. "Detects a table-type table hline.")
  10559. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  10560. "Detects an org-type or table-type table.")
  10561. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  10562. "Searching from within a table (any type) this finds the first line
  10563. outside the table.")
  10564. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  10565. "Searching from within a table (any type) this finds the first line
  10566. outside the table.")
  10567. (defun org-table-create-with-table.el ()
  10568. "Use the table.el package to insert a new table.
  10569. If there is already a table at point, convert between Org-mode tables
  10570. and table.el tables."
  10571. (interactive)
  10572. (require 'table)
  10573. (cond
  10574. ((org-at-table.el-p)
  10575. (if (y-or-n-p "Convert table to Org-mode table? ")
  10576. (org-table-convert)))
  10577. ((org-at-table-p)
  10578. (if (y-or-n-p "Convert table to table.el table? ")
  10579. (org-table-convert)))
  10580. (t (call-interactively 'table-insert))))
  10581. (defun org-table-create-or-convert-from-region (arg)
  10582. "Convert region to table, or create an empty table.
  10583. If there is an active region, convert it to a table. If there is no such
  10584. region, create an empty table."
  10585. (interactive "P")
  10586. (if (org-region-active-p)
  10587. (org-table-convert-region (region-beginning) (region-end) arg)
  10588. (org-table-create arg)))
  10589. (defun org-table-create (&optional size)
  10590. "Query for a size and insert a table skeleton.
  10591. SIZE is a string Columns x Rows like for example \"3x2\"."
  10592. (interactive "P")
  10593. (unless size
  10594. (setq size (read-string
  10595. (concat "Table size Columns x Rows [e.g. "
  10596. org-table-default-size "]: ")
  10597. "" nil org-table-default-size)))
  10598. (let* ((pos (point))
  10599. (indent (make-string (current-column) ?\ ))
  10600. (split (org-split-string size " *x *"))
  10601. (rows (string-to-number (nth 1 split)))
  10602. (columns (string-to-number (car split)))
  10603. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  10604. "\n")))
  10605. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  10606. (point-at-bol) (point)))
  10607. (beginning-of-line 1)
  10608. (newline))
  10609. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  10610. (dotimes (i rows) (insert line))
  10611. (goto-char pos)
  10612. (if (> rows 1)
  10613. ;; Insert a hline after the first row.
  10614. (progn
  10615. (end-of-line 1)
  10616. (insert "\n|-")
  10617. (goto-char pos)))
  10618. (org-table-align)))
  10619. (defun org-table-convert-region (beg0 end0 &optional nspace)
  10620. "Convert region to a table.
  10621. The region goes from BEG0 to END0, but these borders will be moved
  10622. slightly, to make sure a beginning of line in the first line is included.
  10623. When NSPACE is non-nil, it indicates the minimum number of spaces that
  10624. separate columns (default: just one space)."
  10625. (interactive "rP")
  10626. (let* ((beg (min beg0 end0))
  10627. (end (max beg0 end0))
  10628. (tabsep t)
  10629. re)
  10630. (goto-char beg)
  10631. (beginning-of-line 1)
  10632. (setq beg (move-marker (make-marker) (point)))
  10633. (goto-char end)
  10634. (if (bolp) (backward-char 1) (end-of-line 1))
  10635. (setq end (move-marker (make-marker) (point)))
  10636. ;; Lets see if this is tab-separated material. If every nonempty line
  10637. ;; contains a tab, we will assume that it is tab-separated material
  10638. (if nspace
  10639. (setq tabsep nil)
  10640. (goto-char beg)
  10641. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  10642. (if nspace (setq tabsep nil))
  10643. (if tabsep
  10644. (setq re "^\\|\t")
  10645. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  10646. (max 1 (prefix-numeric-value nspace)))))
  10647. (goto-char beg)
  10648. (while (re-search-forward re end t)
  10649. (replace-match "|" t t))
  10650. (goto-char beg)
  10651. (insert " ")
  10652. (org-table-align)))
  10653. (defun org-table-import (file arg)
  10654. "Import FILE as a table.
  10655. The file is assumed to be tab-separated. Such files can be produced by most
  10656. spreadsheet and database applications. If no tabs (at least one per line)
  10657. are found, lines will be split on whitespace into fields."
  10658. (interactive "f\nP")
  10659. (or (bolp) (newline))
  10660. (let ((beg (point))
  10661. (pm (point-max)))
  10662. (insert-file-contents file)
  10663. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  10664. (defun org-table-export ()
  10665. "Export table as a tab-separated file.
  10666. Such a file can be imported into a spreadsheet program like Excel."
  10667. (interactive)
  10668. (let* ((beg (org-table-begin))
  10669. (end (org-table-end))
  10670. (table (buffer-substring beg end))
  10671. (file (read-file-name "Export table to: "))
  10672. buf)
  10673. (unless (or (not (file-exists-p file))
  10674. (y-or-n-p (format "Overwrite file %s? " file)))
  10675. (error "Abort"))
  10676. (with-current-buffer (find-file-noselect file)
  10677. (setq buf (current-buffer))
  10678. (erase-buffer)
  10679. (fundamental-mode)
  10680. (insert table)
  10681. (goto-char (point-min))
  10682. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  10683. (replace-match "" t t)
  10684. (end-of-line 1))
  10685. (goto-char (point-min))
  10686. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  10687. (replace-match "" t t)
  10688. (goto-char (min (1+ (point)) (point-max))))
  10689. (goto-char (point-min))
  10690. (while (re-search-forward "^-[-+]*$" nil t)
  10691. (replace-match "")
  10692. (if (looking-at "\n")
  10693. (delete-char 1)))
  10694. (goto-char (point-min))
  10695. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  10696. (replace-match "\t" t t))
  10697. (save-buffer))
  10698. (kill-buffer buf)))
  10699. (defvar org-table-aligned-begin-marker (make-marker)
  10700. "Marker at the beginning of the table last aligned.
  10701. Used to check if cursor still is in that table, to minimize realignment.")
  10702. (defvar org-table-aligned-end-marker (make-marker)
  10703. "Marker at the end of the table last aligned.
  10704. Used to check if cursor still is in that table, to minimize realignment.")
  10705. (defvar org-table-last-alignment nil
  10706. "List of flags for flushright alignment, from the last re-alignment.
  10707. This is being used to correctly align a single field after TAB or RET.")
  10708. (defvar org-table-last-column-widths nil
  10709. "List of max width of fields in each column.
  10710. This is being used to correctly align a single field after TAB or RET.")
  10711. (defvar org-last-recalc-line nil)
  10712. (defconst org-narrow-column-arrow "=>"
  10713. "Used as display property in narrowed table columns.")
  10714. (defun org-table-align ()
  10715. "Align the table at point by aligning all vertical bars."
  10716. (interactive)
  10717. (let* (
  10718. ;; Limits of table
  10719. (beg (org-table-begin))
  10720. (end (org-table-end))
  10721. ;; Current cursor position
  10722. (linepos (org-current-line))
  10723. (colpos (org-table-current-column))
  10724. (winstart (window-start))
  10725. (winstartline (org-current-line (min winstart (1- (point-max)))))
  10726. lines (new "") lengths l typenums ty fields maxfields i
  10727. column
  10728. (indent "") cnt frac
  10729. rfmt hfmt
  10730. (spaces '(1 . 1))
  10731. (sp1 (car spaces))
  10732. (sp2 (cdr spaces))
  10733. (rfmt1 (concat
  10734. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  10735. (hfmt1 (concat
  10736. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  10737. emptystrings links dates narrow fmax f1 len c e)
  10738. (untabify beg end)
  10739. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  10740. ;; Check if we have links or dates
  10741. (goto-char beg)
  10742. (setq links (re-search-forward org-bracket-link-regexp end t))
  10743. (goto-char beg)
  10744. (setq dates (and org-display-custom-times
  10745. (re-search-forward org-ts-regexp-both end t)))
  10746. ;; Make sure the link properties are right
  10747. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  10748. ;; Make sure the date properties are right
  10749. (when dates (goto-char beg) (while (org-activate-dates end)))
  10750. ;; Check if we are narrowing any columns
  10751. (goto-char beg)
  10752. (setq narrow (and org-format-transports-properties-p
  10753. (re-search-forward "<[0-9]+>" end t)))
  10754. ;; Get the rows
  10755. (setq lines (org-split-string
  10756. (buffer-substring beg end) "\n"))
  10757. ;; Store the indentation of the first line
  10758. (if (string-match "^ *" (car lines))
  10759. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  10760. ;; Mark the hlines by setting the corresponding element to nil
  10761. ;; At the same time, we remove trailing space.
  10762. (setq lines (mapcar (lambda (l)
  10763. (if (string-match "^ *|-" l)
  10764. nil
  10765. (if (string-match "[ \t]+$" l)
  10766. (substring l 0 (match-beginning 0))
  10767. l)))
  10768. lines))
  10769. ;; Get the data fields by splitting the lines.
  10770. (setq fields (mapcar
  10771. (lambda (l)
  10772. (org-split-string l " *| *"))
  10773. (delq nil (copy-sequence lines))))
  10774. ;; How many fields in the longest line?
  10775. (condition-case nil
  10776. (setq maxfields (apply 'max (mapcar 'length fields)))
  10777. (error
  10778. (kill-region beg end)
  10779. (org-table-create org-table-default-size)
  10780. (error "Empty table - created default table")))
  10781. ;; A list of empty strings to fill any short rows on output
  10782. (setq emptystrings (make-list maxfields ""))
  10783. ;; Check for special formatting.
  10784. (setq i -1)
  10785. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  10786. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  10787. ;; Check if there is an explicit width specified
  10788. (when (and org-table-limit-column-width narrow)
  10789. (setq c column fmax nil)
  10790. (while c
  10791. (setq e (pop c))
  10792. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  10793. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  10794. ;; Find fields that are wider than fmax, and shorten them
  10795. (when fmax
  10796. (loop for xx in column do
  10797. (when (and (stringp xx)
  10798. (> (org-string-width xx) fmax))
  10799. (org-add-props xx nil
  10800. 'help-echo
  10801. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  10802. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  10803. (unless (> f1 1)
  10804. (error "Cannot narrow field starting with wide link \"%s\""
  10805. (match-string 0 xx)))
  10806. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  10807. (add-text-properties (- f1 2) f1
  10808. (list 'display org-narrow-column-arrow)
  10809. xx)))))
  10810. ;; Get the maximum width for each column
  10811. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  10812. ;; Get the fraction of numbers, to decide about alignment of the column
  10813. (setq cnt 0 frac 0.0)
  10814. (loop for x in column do
  10815. (if (equal x "")
  10816. nil
  10817. (setq frac ( / (+ (* frac cnt)
  10818. (if (string-match org-table-number-regexp x) 1 0))
  10819. (setq cnt (1+ cnt))))))
  10820. (push (>= frac org-table-number-fraction) typenums))
  10821. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  10822. ;; Store the alignment of this table, for later editing of single fields
  10823. (setq org-table-last-alignment typenums
  10824. org-table-last-column-widths lengths)
  10825. ;; With invisible characters, `format' does not get the field width right
  10826. ;; So we need to make these fields wide by hand.
  10827. (when links
  10828. (loop for i from 0 upto (1- maxfields) do
  10829. (setq len (nth i lengths))
  10830. (loop for j from 0 upto (1- (length fields)) do
  10831. (setq c (nthcdr i (car (nthcdr j fields))))
  10832. (if (and (stringp (car c))
  10833. (string-match org-bracket-link-regexp (car c))
  10834. (< (org-string-width (car c)) len))
  10835. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  10836. ;; Compute the formats needed for output of the table
  10837. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  10838. (while (setq l (pop lengths))
  10839. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  10840. (setq rfmt (concat rfmt (format rfmt1 ty l))
  10841. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  10842. (setq rfmt (concat rfmt "\n")
  10843. hfmt (concat (substring hfmt 0 -1) "|\n"))
  10844. (setq new (mapconcat
  10845. (lambda (l)
  10846. (if l (apply 'format rfmt
  10847. (append (pop fields) emptystrings))
  10848. hfmt))
  10849. lines ""))
  10850. ;; Replace the old one
  10851. (delete-region beg end)
  10852. (move-marker end nil)
  10853. (move-marker org-table-aligned-begin-marker (point))
  10854. (insert new)
  10855. (move-marker org-table-aligned-end-marker (point))
  10856. (when (and orgtbl-mode (not (org-mode-p)))
  10857. (goto-char org-table-aligned-begin-marker)
  10858. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  10859. ;; Try to move to the old location
  10860. (goto-line winstartline)
  10861. (setq winstart (point-at-bol))
  10862. (goto-line linepos)
  10863. (set-window-start (selected-window) winstart 'noforce)
  10864. (org-table-goto-column colpos)
  10865. (setq org-table-may-need-update nil)
  10866. ))
  10867. (defun org-string-width (s)
  10868. "Compute width of string, ignoring invisible characters.
  10869. This ignores character with invisibility property `org-link', and also
  10870. characters with property `org-cwidth', because these will become invisible
  10871. upon the next fontification round."
  10872. (let (b l)
  10873. (when (or (eq t buffer-invisibility-spec)
  10874. (assq 'org-link buffer-invisibility-spec))
  10875. (while (setq b (text-property-any 0 (length s)
  10876. 'invisible 'org-link s))
  10877. (setq s (concat (substring s 0 b)
  10878. (substring s (or (next-single-property-change
  10879. b 'invisible s) (length s)))))))
  10880. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  10881. (setq s (concat (substring s 0 b)
  10882. (substring s (or (next-single-property-change
  10883. b 'org-cwidth s) (length s))))))
  10884. (setq l (string-width s) b -1)
  10885. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  10886. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  10887. l))
  10888. (defun org-table-begin (&optional table-type)
  10889. "Find the beginning of the table and return its position.
  10890. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  10891. (save-excursion
  10892. (if (not (re-search-backward
  10893. (if table-type org-table-any-border-regexp
  10894. org-table-border-regexp)
  10895. nil t))
  10896. (progn (goto-char (point-min)) (point))
  10897. (goto-char (match-beginning 0))
  10898. (beginning-of-line 2)
  10899. (point))))
  10900. (defun org-table-end (&optional table-type)
  10901. "Find the end of the table and return its position.
  10902. With argument TABLE-TYPE, go to the end of a table.el-type table."
  10903. (save-excursion
  10904. (if (not (re-search-forward
  10905. (if table-type org-table-any-border-regexp
  10906. org-table-border-regexp)
  10907. nil t))
  10908. (goto-char (point-max))
  10909. (goto-char (match-beginning 0)))
  10910. (point-marker)))
  10911. (defun org-table-justify-field-maybe (&optional new)
  10912. "Justify the current field, text to left, number to right.
  10913. Optional argument NEW may specify text to replace the current field content."
  10914. (cond
  10915. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  10916. ((org-at-table-hline-p))
  10917. ((and (not new)
  10918. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  10919. (current-buffer)))
  10920. (< (point) org-table-aligned-begin-marker)
  10921. (>= (point) org-table-aligned-end-marker)))
  10922. ;; This is not the same table, force a full re-align
  10923. (setq org-table-may-need-update t))
  10924. (t ;; realign the current field, based on previous full realign
  10925. (let* ((pos (point)) s
  10926. (col (org-table-current-column))
  10927. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  10928. l f n o e)
  10929. (when (> col 0)
  10930. (skip-chars-backward "^|\n")
  10931. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  10932. (progn
  10933. (setq s (match-string 1)
  10934. o (match-string 0)
  10935. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  10936. e (not (= (match-beginning 2) (match-end 2))))
  10937. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  10938. l (if e "|" (setq org-table-may-need-update t) ""))
  10939. n (format f s))
  10940. (if new
  10941. (if (<= (length new) l) ;; FIXME: length -> str-width?
  10942. (setq n (format f new))
  10943. (setq n (concat new "|") org-table-may-need-update t)))
  10944. (or (equal n o)
  10945. (let (org-table-may-need-update)
  10946. (replace-match n))))
  10947. (setq org-table-may-need-update t))
  10948. (goto-char pos))))))
  10949. (defun org-table-next-field ()
  10950. "Go to the next field in the current table, creating new lines as needed.
  10951. Before doing so, re-align the table if necessary."
  10952. (interactive)
  10953. (org-table-maybe-eval-formula)
  10954. (org-table-maybe-recalculate-line)
  10955. (if (and org-table-automatic-realign
  10956. org-table-may-need-update)
  10957. (org-table-align))
  10958. (let ((end (org-table-end)))
  10959. (if (org-at-table-hline-p)
  10960. (end-of-line 1))
  10961. (condition-case nil
  10962. (progn
  10963. (re-search-forward "|" end)
  10964. (if (looking-at "[ \t]*$")
  10965. (re-search-forward "|" end))
  10966. (if (and (looking-at "-")
  10967. org-table-tab-jumps-over-hlines
  10968. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  10969. (goto-char (match-beginning 1)))
  10970. (if (looking-at "-")
  10971. (progn
  10972. (beginning-of-line 0)
  10973. (org-table-insert-row 'below))
  10974. (if (looking-at " ") (forward-char 1))))
  10975. (error
  10976. (org-table-insert-row 'below)))))
  10977. (defun org-table-previous-field ()
  10978. "Go to the previous field in the table.
  10979. Before doing so, re-align the table if necessary."
  10980. (interactive)
  10981. (org-table-justify-field-maybe)
  10982. (org-table-maybe-recalculate-line)
  10983. (if (and org-table-automatic-realign
  10984. org-table-may-need-update)
  10985. (org-table-align))
  10986. (if (org-at-table-hline-p)
  10987. (end-of-line 1))
  10988. (re-search-backward "|" (org-table-begin))
  10989. (re-search-backward "|" (org-table-begin))
  10990. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  10991. (re-search-backward "|" (org-table-begin)))
  10992. (if (looking-at "| ?")
  10993. (goto-char (match-end 0))))
  10994. (defun org-table-next-row ()
  10995. "Go to the next row (same column) in the current table.
  10996. Before doing so, re-align the table if necessary."
  10997. (interactive)
  10998. (org-table-maybe-eval-formula)
  10999. (org-table-maybe-recalculate-line)
  11000. (if (or (looking-at "[ \t]*$")
  11001. (save-excursion (skip-chars-backward " \t") (bolp)))
  11002. (newline)
  11003. (if (and org-table-automatic-realign
  11004. org-table-may-need-update)
  11005. (org-table-align))
  11006. (let ((col (org-table-current-column)))
  11007. (beginning-of-line 2)
  11008. (if (or (not (org-at-table-p))
  11009. (org-at-table-hline-p))
  11010. (progn
  11011. (beginning-of-line 0)
  11012. (org-table-insert-row 'below)))
  11013. (org-table-goto-column col)
  11014. (skip-chars-backward "^|\n\r")
  11015. (if (looking-at " ") (forward-char 1)))))
  11016. (defun org-table-copy-down (n)
  11017. "Copy a field down in the current column.
  11018. If the field at the cursor is empty, copy into it the content of the nearest
  11019. non-empty field above. With argument N, use the Nth non-empty field.
  11020. If the current field is not empty, it is copied down to the next row, and
  11021. the cursor is moved with it. Therefore, repeating this command causes the
  11022. column to be filled row-by-row.
  11023. If the variable `org-table-copy-increment' is non-nil and the field is an
  11024. integer, it will be incremented while copying."
  11025. (interactive "p")
  11026. (let* ((colpos (org-table-current-column))
  11027. (field (org-table-get-field))
  11028. (non-empty (string-match "[^ \t]" field))
  11029. (beg (org-table-begin))
  11030. txt)
  11031. (org-table-check-inside-data-field)
  11032. (if non-empty
  11033. (progn
  11034. (setq txt (org-trim field))
  11035. (org-table-next-row)
  11036. (org-table-blank-field))
  11037. (save-excursion
  11038. (setq txt
  11039. (catch 'exit
  11040. (while (progn (beginning-of-line 1)
  11041. (re-search-backward org-table-dataline-regexp
  11042. beg t))
  11043. (org-table-goto-column colpos t)
  11044. (if (and (looking-at
  11045. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  11046. (= (setq n (1- n)) 0))
  11047. (throw 'exit (match-string 1))))))))
  11048. (if txt
  11049. (progn
  11050. (if (and org-table-copy-increment
  11051. (string-match "^[0-9]+$" txt))
  11052. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  11053. (insert txt)
  11054. (org-table-maybe-recalculate-line)
  11055. (org-table-align))
  11056. (error "No non-empty field found"))))
  11057. (defun org-table-check-inside-data-field ()
  11058. "Is point inside a table data field?
  11059. I.e. not on a hline or before the first or after the last column?
  11060. This actually throws an error, so it aborts the current command."
  11061. (if (or (not (org-at-table-p))
  11062. (= (org-table-current-column) 0)
  11063. (org-at-table-hline-p)
  11064. (looking-at "[ \t]*$"))
  11065. (error "Not in table data field")))
  11066. (defvar org-table-clip nil
  11067. "Clipboard for table regions.")
  11068. (defun org-table-blank-field ()
  11069. "Blank the current table field or active region."
  11070. (interactive)
  11071. (org-table-check-inside-data-field)
  11072. (if (and (interactive-p) (org-region-active-p))
  11073. (let (org-table-clip)
  11074. (org-table-cut-region (region-beginning) (region-end)))
  11075. (skip-chars-backward "^|")
  11076. (backward-char 1)
  11077. (if (looking-at "|[^|\n]+")
  11078. (let* ((pos (match-beginning 0))
  11079. (match (match-string 0))
  11080. (len (org-string-width match)))
  11081. (replace-match (concat "|" (make-string (1- len) ?\ )))
  11082. (goto-char (+ 2 pos))
  11083. (substring match 1)))))
  11084. (defun org-table-get-field (&optional n replace)
  11085. "Return the value of the field in column N of current row.
  11086. N defaults to current field.
  11087. If REPLACE is a string, replace field with this value. The return value
  11088. is always the old value."
  11089. (and n (org-table-goto-column n))
  11090. (skip-chars-backward "^|\n")
  11091. (backward-char 1)
  11092. (if (looking-at "|[^|\r\n]*")
  11093. (let* ((pos (match-beginning 0))
  11094. (val (buffer-substring (1+ pos) (match-end 0))))
  11095. (if replace
  11096. (replace-match (concat "|" replace)))
  11097. (goto-char (min (point-at-eol) (+ 2 pos)))
  11098. val)
  11099. (forward-char 1) ""))
  11100. (defun org-table-current-column ()
  11101. "Find out which column we are in.
  11102. When called interactively, column is also displayed in echo area."
  11103. (interactive)
  11104. (if (interactive-p) (org-table-check-inside-data-field))
  11105. (save-excursion
  11106. (let ((cnt 0) (pos (point)))
  11107. (beginning-of-line 1)
  11108. (while (search-forward "|" pos t)
  11109. (setq cnt (1+ cnt)))
  11110. (if (interactive-p) (message "This is table column %d" cnt))
  11111. cnt)))
  11112. (defun org-table-goto-column (n &optional on-delim force)
  11113. "Move the cursor to the Nth column in the current table line.
  11114. With optional argument ON-DELIM, stop with point before the left delimiter
  11115. of the field.
  11116. If there are less than N fields, just go to after the last delimiter.
  11117. However, when FORCE is non-nil, create new columns if necessary."
  11118. (interactive "p")
  11119. (let ((pos (point-at-eol)))
  11120. (beginning-of-line 1)
  11121. (when (> n 0)
  11122. (while (and (> (setq n (1- n)) -1)
  11123. (or (search-forward "|" pos t)
  11124. (and force
  11125. (progn (end-of-line 1)
  11126. (skip-chars-backward "^|")
  11127. (insert " | "))))))
  11128. ; (backward-char 2) t)))))
  11129. (when (and force (not (looking-at ".*|")))
  11130. (save-excursion (end-of-line 1) (insert " | ")))
  11131. (if on-delim
  11132. (backward-char 1)
  11133. (if (looking-at " ") (forward-char 1))))))
  11134. (defun org-at-table-p (&optional table-type)
  11135. "Return t if the cursor is inside an org-type table.
  11136. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  11137. (if org-enable-table-editor
  11138. (save-excursion
  11139. (beginning-of-line 1)
  11140. (looking-at (if table-type org-table-any-line-regexp
  11141. org-table-line-regexp)))
  11142. nil))
  11143. (defun org-at-table.el-p ()
  11144. "Return t if and only if we are at a table.el table."
  11145. (and (org-at-table-p 'any)
  11146. (save-excursion
  11147. (goto-char (org-table-begin 'any))
  11148. (looking-at org-table1-hline-regexp))))
  11149. (defun org-table-recognize-table.el ()
  11150. "If there is a table.el table nearby, recognize it and move into it."
  11151. (if org-table-tab-recognizes-table.el
  11152. (if (org-at-table.el-p)
  11153. (progn
  11154. (beginning-of-line 1)
  11155. (if (looking-at org-table-dataline-regexp)
  11156. nil
  11157. (if (looking-at org-table1-hline-regexp)
  11158. (progn
  11159. (beginning-of-line 2)
  11160. (if (looking-at org-table-any-border-regexp)
  11161. (beginning-of-line -1)))))
  11162. (if (re-search-forward "|" (org-table-end t) t)
  11163. (progn
  11164. (require 'table)
  11165. (if (table--at-cell-p (point))
  11166. t
  11167. (message "recognizing table.el table...")
  11168. (table-recognize-table)
  11169. (message "recognizing table.el table...done")))
  11170. (error "This should not happen..."))
  11171. t)
  11172. nil)
  11173. nil))
  11174. (defun org-at-table-hline-p ()
  11175. "Return t if the cursor is inside a hline in a table."
  11176. (if org-enable-table-editor
  11177. (save-excursion
  11178. (beginning-of-line 1)
  11179. (looking-at org-table-hline-regexp))
  11180. nil))
  11181. (defun org-table-insert-column ()
  11182. "Insert a new column into the table."
  11183. (interactive)
  11184. (if (not (org-at-table-p))
  11185. (error "Not at a table"))
  11186. (org-table-find-dataline)
  11187. (let* ((col (max 1 (org-table-current-column)))
  11188. (beg (org-table-begin))
  11189. (end (org-table-end))
  11190. ;; Current cursor position
  11191. (linepos (org-current-line))
  11192. (colpos col))
  11193. (goto-char beg)
  11194. (while (< (point) end)
  11195. (if (org-at-table-hline-p)
  11196. nil
  11197. (org-table-goto-column col t)
  11198. (insert "| "))
  11199. (beginning-of-line 2))
  11200. (move-marker end nil)
  11201. (goto-line linepos)
  11202. (org-table-goto-column colpos)
  11203. (org-table-align)
  11204. (org-table-modify-formulas 'insert col)))
  11205. (defun org-table-find-dataline ()
  11206. "Find a dataline in the current table, which is needed for column commands."
  11207. (if (and (org-at-table-p)
  11208. (not (org-at-table-hline-p)))
  11209. t
  11210. (let ((col (current-column))
  11211. (end (org-table-end)))
  11212. (move-to-column col)
  11213. (while (and (< (point) end)
  11214. (or (not (= (current-column) col))
  11215. (org-at-table-hline-p)))
  11216. (beginning-of-line 2)
  11217. (move-to-column col))
  11218. (if (and (org-at-table-p)
  11219. (not (org-at-table-hline-p)))
  11220. t
  11221. (error
  11222. "Please position cursor in a data line for column operations")))))
  11223. (defun org-table-delete-column ()
  11224. "Delete a column from the table."
  11225. (interactive)
  11226. (if (not (org-at-table-p))
  11227. (error "Not at a table"))
  11228. (org-table-find-dataline)
  11229. (org-table-check-inside-data-field)
  11230. (let* ((col (org-table-current-column))
  11231. (beg (org-table-begin))
  11232. (end (org-table-end))
  11233. ;; Current cursor position
  11234. (linepos (org-current-line))
  11235. (colpos col))
  11236. (goto-char beg)
  11237. (while (< (point) end)
  11238. (if (org-at-table-hline-p)
  11239. nil
  11240. (org-table-goto-column col t)
  11241. (and (looking-at "|[^|\n]+|")
  11242. (replace-match "|")))
  11243. (beginning-of-line 2))
  11244. (move-marker end nil)
  11245. (goto-line linepos)
  11246. (org-table-goto-column colpos)
  11247. (org-table-align)
  11248. (org-table-modify-formulas 'remove col)))
  11249. (defun org-table-move-column-right ()
  11250. "Move column to the right."
  11251. (interactive)
  11252. (org-table-move-column nil))
  11253. (defun org-table-move-column-left ()
  11254. "Move column to the left."
  11255. (interactive)
  11256. (org-table-move-column 'left))
  11257. (defun org-table-move-column (&optional left)
  11258. "Move the current column to the right. With arg LEFT, move to the left."
  11259. (interactive "P")
  11260. (if (not (org-at-table-p))
  11261. (error "Not at a table"))
  11262. (org-table-find-dataline)
  11263. (org-table-check-inside-data-field)
  11264. (let* ((col (org-table-current-column))
  11265. (col1 (if left (1- col) col))
  11266. (beg (org-table-begin))
  11267. (end (org-table-end))
  11268. ;; Current cursor position
  11269. (linepos (org-current-line))
  11270. (colpos (if left (1- col) (1+ col))))
  11271. (if (and left (= col 1))
  11272. (error "Cannot move column further left"))
  11273. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  11274. (error "Cannot move column further right"))
  11275. (goto-char beg)
  11276. (while (< (point) end)
  11277. (if (org-at-table-hline-p)
  11278. nil
  11279. (org-table-goto-column col1 t)
  11280. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  11281. (replace-match "|\\2|\\1|")))
  11282. (beginning-of-line 2))
  11283. (move-marker end nil)
  11284. (goto-line linepos)
  11285. (org-table-goto-column colpos)
  11286. (org-table-align)
  11287. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  11288. (defun org-table-move-row-down ()
  11289. "Move table row down."
  11290. (interactive)
  11291. (org-table-move-row nil))
  11292. (defun org-table-move-row-up ()
  11293. "Move table row up."
  11294. (interactive)
  11295. (org-table-move-row 'up))
  11296. (defun org-table-move-row (&optional up)
  11297. "Move the current table line down. With arg UP, move it up."
  11298. (interactive "P")
  11299. (let ((col (current-column))
  11300. (pos (point))
  11301. (tonew (if up 0 2))
  11302. txt)
  11303. (beginning-of-line tonew)
  11304. (if (not (org-at-table-p))
  11305. (progn
  11306. (goto-char pos)
  11307. (error "Cannot move row further")))
  11308. (goto-char pos)
  11309. (beginning-of-line 1)
  11310. (setq pos (point))
  11311. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  11312. (delete-region (point) (1+ (point-at-eol)))
  11313. (beginning-of-line tonew)
  11314. (insert txt)
  11315. (beginning-of-line 0)
  11316. (move-to-column col)))
  11317. (defun org-table-insert-row (&optional arg)
  11318. "Insert a new row above the current line into the table.
  11319. With prefix ARG, insert below the current line."
  11320. (interactive "P")
  11321. (if (not (org-at-table-p))
  11322. (error "Not at a table"))
  11323. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  11324. (new (org-table-clean-line line)))
  11325. ;; Fix the first field if necessary
  11326. (if (string-match "^[ \t]*| *[#$] *|" line)
  11327. (setq new (replace-match (match-string 0 line) t t new)))
  11328. (beginning-of-line (if arg 2 1))
  11329. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  11330. (beginning-of-line 0)
  11331. (re-search-forward "| ?" (point-at-eol) t)
  11332. (and org-table-may-need-update (org-table-align))))
  11333. (defun org-table-insert-hline (&optional arg)
  11334. "Insert a horizontal-line below the current line into the table.
  11335. With prefix ARG, insert above the current line."
  11336. (interactive "P")
  11337. (if (not (org-at-table-p))
  11338. (error "Not at a table"))
  11339. (let ((line (org-table-clean-line
  11340. (buffer-substring (point-at-bol) (point-at-eol))))
  11341. (col (current-column)))
  11342. (while (string-match "|\\( +\\)|" line)
  11343. (setq line (replace-match
  11344. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  11345. ?-) "|") t t line)))
  11346. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  11347. (beginning-of-line (if arg 1 2))
  11348. (insert line "\n")
  11349. (beginning-of-line (if arg 1 -1))
  11350. (move-to-column col)))
  11351. (defun org-table-clean-line (s)
  11352. "Convert a table line S into a string with only \"|\" and space.
  11353. In particular, this does handle wide and invisible characters."
  11354. (if (string-match "^[ \t]*|-" s)
  11355. ;; It's a hline, just map the characters
  11356. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  11357. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  11358. (setq s (replace-match
  11359. (concat "|" (make-string (org-string-width (match-string 1 s))
  11360. ?\ ) "|")
  11361. t t s)))
  11362. s))
  11363. (defun org-table-kill-row ()
  11364. "Delete the current row or horizontal line from the table."
  11365. (interactive)
  11366. (if (not (org-at-table-p))
  11367. (error "Not at a table"))
  11368. (let ((col (current-column)))
  11369. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  11370. (if (not (org-at-table-p)) (beginning-of-line 0))
  11371. (move-to-column col)))
  11372. (defun org-table-sort-lines (beg end numericp)
  11373. "Sort table lines in region.
  11374. Point and mark define the first and last line to include. Both point and
  11375. mark should be in the column that is used for sorting. For example, to
  11376. sort according to column 3, put the mark in the first line to sort, in
  11377. table column 3. Put point into the last line to be included in the sorting,
  11378. also in table column 3. The command will prompt for the sorting method
  11379. \(n for numerical, a for alphanumeric)."
  11380. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  11381. (setq numericp (string-match "[nN]" numericp))
  11382. (org-table-align) ;; Just to be safe
  11383. (let* (bcol ecol cmp column lns)
  11384. (goto-char beg)
  11385. (org-table-check-inside-data-field)
  11386. (setq column (org-table-current-column)
  11387. beg (move-marker (make-marker) (point-at-bol)))
  11388. (goto-char end)
  11389. (org-table-check-inside-data-field)
  11390. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  11391. (untabify beg end)
  11392. (goto-char beg)
  11393. (org-table-goto-column column)
  11394. (skip-chars-backward "^|")
  11395. (setq bcol (current-column))
  11396. (org-table-goto-column (1+ column))
  11397. (skip-chars-backward "^|")
  11398. (setq ecol (1- (current-column)))
  11399. (setq cmp (if numericp
  11400. (lambda (a b) (< (car a) (car b)))
  11401. (lambda (a b) (string< (car a) (car b)))))
  11402. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  11403. (org-split-string (buffer-substring beg end) "\n")))
  11404. (if numericp
  11405. (setq lns (mapcar (lambda(x)
  11406. (cons (string-to-number (car x)) (cdr x)))
  11407. lns)))
  11408. (delete-region beg end)
  11409. (move-marker beg nil)
  11410. (move-marker end nil)
  11411. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  11412. (message "%d lines sorted %s based on column %d"
  11413. (length lns)
  11414. (if numericp "numerically" "alphabetically") column)))
  11415. (defun org-table-cut-region (beg end)
  11416. "Copy region in table to the clipboard and blank all relevant fields."
  11417. (interactive "r")
  11418. (org-table-copy-region beg end 'cut))
  11419. (defun org-table-copy-region (beg end &optional cut)
  11420. "Copy rectangular region in table to clipboard.
  11421. A special clipboard is used which can only be accessed
  11422. with `org-table-paste-rectangle'."
  11423. (interactive "rP")
  11424. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  11425. region cols
  11426. (rpl (if cut " " nil)))
  11427. (goto-char beg)
  11428. (org-table-check-inside-data-field)
  11429. (setq l01 (count-lines (point-min) (point))
  11430. c01 (org-table-current-column))
  11431. (goto-char end)
  11432. (org-table-check-inside-data-field)
  11433. (setq l02 (count-lines (point-min) (point))
  11434. c02 (org-table-current-column))
  11435. (setq l1 (min l01 l02) l2 (max l01 l02)
  11436. c1 (min c01 c02) c2 (max c01 c02))
  11437. (catch 'exit
  11438. (while t
  11439. (catch 'nextline
  11440. (if (> l1 l2) (throw 'exit t))
  11441. (goto-line l1)
  11442. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  11443. (setq cols nil ic1 c1 ic2 c2)
  11444. (while (< ic1 (1+ ic2))
  11445. (push (org-table-get-field ic1 rpl) cols)
  11446. (setq ic1 (1+ ic1)))
  11447. (push (nreverse cols) region)
  11448. (setq l1 (1+ l1)))))
  11449. (setq org-table-clip (nreverse region))
  11450. (if cut (org-table-align))
  11451. org-table-clip))
  11452. (defun org-table-paste-rectangle ()
  11453. "Paste a rectangular region into a table.
  11454. The upper right corner ends up in the current field. All involved fields
  11455. will be overwritten. If the rectangle does not fit into the present table,
  11456. the table is enlarged as needed. The process ignores horizontal separator
  11457. lines."
  11458. (interactive)
  11459. (unless (and org-table-clip (listp org-table-clip))
  11460. (error "First cut/copy a region to paste!"))
  11461. (org-table-check-inside-data-field)
  11462. (let* ((clip org-table-clip)
  11463. (line (count-lines (point-min) (point)))
  11464. (col (org-table-current-column))
  11465. (org-enable-table-editor t)
  11466. (org-table-automatic-realign nil)
  11467. c cols field)
  11468. (while (setq cols (pop clip))
  11469. (while (org-at-table-hline-p) (beginning-of-line 2))
  11470. (if (not (org-at-table-p))
  11471. (progn (end-of-line 0) (org-table-next-field)))
  11472. (setq c col)
  11473. (while (setq field (pop cols))
  11474. (org-table-goto-column c nil 'force)
  11475. (org-table-get-field nil field)
  11476. (setq c (1+ c)))
  11477. (beginning-of-line 2))
  11478. (goto-line line)
  11479. (org-table-goto-column col)
  11480. (org-table-align)))
  11481. (defun org-table-convert ()
  11482. "Convert from `org-mode' table to table.el and back.
  11483. Obviously, this only works within limits. When an Org-mode table is
  11484. converted to table.el, all horizontal separator lines get lost, because
  11485. table.el uses these as cell boundaries and has no notion of horizontal lines.
  11486. A table.el table can be converted to an Org-mode table only if it does not
  11487. do row or column spanning. Multiline cells will become multiple cells.
  11488. Beware, Org-mode does not test if the table can be successfully converted - it
  11489. blindly applies a recipe that works for simple tables."
  11490. (interactive)
  11491. (require 'table)
  11492. (if (org-at-table.el-p)
  11493. ;; convert to Org-mode table
  11494. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  11495. (end (move-marker (make-marker) (org-table-end t))))
  11496. (table-unrecognize-region beg end)
  11497. (goto-char beg)
  11498. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  11499. (replace-match ""))
  11500. (goto-char beg))
  11501. (if (org-at-table-p)
  11502. ;; convert to table.el table
  11503. (let ((beg (move-marker (make-marker) (org-table-begin)))
  11504. (end (move-marker (make-marker) (org-table-end))))
  11505. ;; first, get rid of all horizontal lines
  11506. (goto-char beg)
  11507. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  11508. (replace-match ""))
  11509. ;; insert a hline before first
  11510. (goto-char beg)
  11511. (org-table-insert-hline 'above)
  11512. (beginning-of-line -1)
  11513. ;; insert a hline after each line
  11514. (while (progn (beginning-of-line 3) (< (point) end))
  11515. (org-table-insert-hline))
  11516. (goto-char beg)
  11517. (setq end (move-marker end (org-table-end)))
  11518. ;; replace "+" at beginning and ending of hlines
  11519. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  11520. (replace-match "\\1+-"))
  11521. (goto-char beg)
  11522. (while (re-search-forward "-|[ \t]*$" end t)
  11523. (replace-match "-+"))
  11524. (goto-char beg)))))
  11525. (defun org-table-wrap-region (arg)
  11526. "Wrap several fields in a column like a paragraph.
  11527. This is useful if you'd like to spread the contents of a field over several
  11528. lines, in order to keep the table compact.
  11529. If there is an active region, and both point and mark are in the same column,
  11530. the text in the column is wrapped to minimum width for the given number of
  11531. lines. Generally, this makes the table more compact. A prefix ARG may be
  11532. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  11533. formats the selected text to two lines. If the region was longer than two
  11534. lines, the remaining lines remain empty. A negative prefix argument reduces
  11535. the current number of lines by that amount. The wrapped text is pasted back
  11536. into the table. If you formatted it to more lines than it was before, fields
  11537. further down in the table get overwritten - so you might need to make space in
  11538. the table first.
  11539. If there is no region, the current field is split at the cursor position and
  11540. the text fragment to the right of the cursor is prepended to the field one
  11541. line down.
  11542. If there is no region, but you specify a prefix ARG, the current field gets
  11543. blank, and the content is appended to the field above."
  11544. (interactive "P")
  11545. (org-table-check-inside-data-field)
  11546. (if (org-region-active-p)
  11547. ;; There is a region: fill as a paragraph
  11548. (let ((beg (region-beginning))
  11549. nlines)
  11550. (org-table-cut-region (region-beginning) (region-end))
  11551. (if (> (length (car org-table-clip)) 1)
  11552. (error "Region must be limited to single column"))
  11553. (setq nlines (if arg
  11554. (if (< arg 1)
  11555. (+ (length org-table-clip) arg)
  11556. arg)
  11557. (length org-table-clip)))
  11558. (setq org-table-clip
  11559. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  11560. nil nlines)))
  11561. (goto-char beg)
  11562. (org-table-paste-rectangle))
  11563. ;; No region, split the current field at point
  11564. (if arg
  11565. ;; combine with field above
  11566. (let ((s (org-table-blank-field))
  11567. (col (org-table-current-column)))
  11568. (beginning-of-line 0)
  11569. (while (org-at-table-hline-p) (beginning-of-line 0))
  11570. (org-table-goto-column col)
  11571. (skip-chars-forward "^|")
  11572. (skip-chars-backward " ")
  11573. (insert " " (org-trim s))
  11574. (org-table-align))
  11575. ;; split field
  11576. (when (looking-at "\\([^|]+\\)+|")
  11577. (let ((s (match-string 1)))
  11578. (replace-match " |")
  11579. (goto-char (match-beginning 0))
  11580. (org-table-next-row)
  11581. (insert (org-trim s) " ")
  11582. (org-table-align))))))
  11583. (defvar org-field-marker nil)
  11584. (defun org-table-edit-field (arg)
  11585. "Edit table field in a different window.
  11586. This is mainly useful for fields that contain hidden parts.
  11587. When called with a \\[universal-argument] prefix, just make the full field visible so that
  11588. it can be edited in place."
  11589. (interactive "P")
  11590. (if arg
  11591. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  11592. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  11593. (remove-text-properties b e '(org-cwidth t invisible t
  11594. display t intangible t))
  11595. (if (and (boundp 'font-lock-mode) font-lock-mode)
  11596. (font-lock-fontify-block)))
  11597. (let ((pos (move-marker (make-marker) (point)))
  11598. (field (org-table-get-field))
  11599. (cw (current-window-configuration))
  11600. p)
  11601. (switch-to-buffer-other-window "*Org tmp*")
  11602. (erase-buffer)
  11603. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  11604. (org-mode)
  11605. (goto-char (setq p (point-max)))
  11606. (insert (org-trim field))
  11607. (remove-text-properties p (point-max)
  11608. '(invisible t org-cwidth t display t
  11609. intangible t))
  11610. (goto-char p)
  11611. (org-set-local 'org-finish-function
  11612. 'org-table-finish-edit-field)
  11613. (org-set-local 'org-window-configuration cw)
  11614. (org-set-local 'org-field-marker pos)
  11615. (message "Edit and finish with C-c C-c"))))
  11616. (defun org-table-finish-edit-field ()
  11617. "Finish editing a table data field.
  11618. Remove all newline characters, insert the result into the table, realign
  11619. the table and kill the editing buffer."
  11620. (let ((pos org-field-marker)
  11621. (cw org-window-configuration)
  11622. (cb (current-buffer))
  11623. text)
  11624. (goto-char (point-min))
  11625. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  11626. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  11627. (replace-match " "))
  11628. (setq text (org-trim (buffer-string)))
  11629. (set-window-configuration cw)
  11630. (kill-buffer cb)
  11631. (select-window (get-buffer-window (marker-buffer pos)))
  11632. (goto-char pos)
  11633. (move-marker pos nil)
  11634. (org-table-check-inside-data-field)
  11635. (org-table-get-field nil text)
  11636. (org-table-align)
  11637. (message "New field value inserted")))
  11638. (defun org-trim (s)
  11639. "Remove whitespace at beginning and end of string."
  11640. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  11641. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  11642. s)
  11643. (defun org-wrap (string &optional width lines)
  11644. "Wrap string to either a number of lines, or a width in characters.
  11645. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11646. that costs. If there is a word longer than WIDTH, the text is actually
  11647. wrapped to the length of that word.
  11648. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11649. many lines, whatever width that takes.
  11650. The return value is a list of lines, without newlines at the end."
  11651. (let* ((words (org-split-string string "[ \t\n]+"))
  11652. (maxword (apply 'max (mapcar 'org-string-width words)))
  11653. w ll)
  11654. (cond (width
  11655. (org-do-wrap words (max maxword width)))
  11656. (lines
  11657. (setq w maxword)
  11658. (setq ll (org-do-wrap words maxword))
  11659. (if (<= (length ll) lines)
  11660. ll
  11661. (setq ll words)
  11662. (while (> (length ll) lines)
  11663. (setq w (1+ w))
  11664. (setq ll (org-do-wrap words w)))
  11665. ll))
  11666. (t (error "Cannot wrap this")))))
  11667. (defun org-do-wrap (words width)
  11668. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11669. (let (lines line)
  11670. (while words
  11671. (setq line (pop words))
  11672. (while (and words (< (+ (length line) (length (car words))) width))
  11673. (setq line (concat line " " (pop words))))
  11674. (setq lines (push line lines)))
  11675. (nreverse lines)))
  11676. (defun org-split-string (string &optional separators)
  11677. "Splits STRING into substrings at SEPARATORS.
  11678. No empty strings are returned if there are matches at the beginning
  11679. and end of string."
  11680. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11681. (start 0)
  11682. notfirst
  11683. (list nil))
  11684. (while (and (string-match rexp string
  11685. (if (and notfirst
  11686. (= start (match-beginning 0))
  11687. (< start (length string)))
  11688. (1+ start) start))
  11689. (< (match-beginning 0) (length string)))
  11690. (setq notfirst t)
  11691. (or (eq (match-beginning 0) 0)
  11692. (and (eq (match-beginning 0) (match-end 0))
  11693. (eq (match-beginning 0) start))
  11694. (setq list
  11695. (cons (substring string start (match-beginning 0))
  11696. list)))
  11697. (setq start (match-end 0)))
  11698. (or (eq start (length string))
  11699. (setq list
  11700. (cons (substring string start)
  11701. list)))
  11702. (nreverse list)))
  11703. (defun org-table-map-tables (function)
  11704. "Apply FUNCTION to the start of all tables in the buffer."
  11705. (save-excursion
  11706. (save-restriction
  11707. (widen)
  11708. (goto-char (point-min))
  11709. (while (re-search-forward org-table-any-line-regexp nil t)
  11710. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  11711. (beginning-of-line 1)
  11712. (if (looking-at org-table-line-regexp)
  11713. (save-excursion (funcall function)))
  11714. (re-search-forward org-table-any-border-regexp nil 1))))
  11715. (message "Mapping tables: done"))
  11716. (defun org-table-sum (&optional beg end nlast)
  11717. "Sum numbers in region of current table column.
  11718. The result will be displayed in the echo area, and will be available
  11719. as kill to be inserted with \\[yank].
  11720. If there is an active region, it is interpreted as a rectangle and all
  11721. numbers in that rectangle will be summed. If there is no active
  11722. region and point is located in a table column, sum all numbers in that
  11723. column.
  11724. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  11725. numbers are assumed to be times as well (in decimal hours) and the
  11726. numbers are added as such.
  11727. If NLAST is a number, only the NLAST fields will actually be summed."
  11728. (interactive)
  11729. (save-excursion
  11730. (let (col (timecnt 0) diff h m s org-table-clip)
  11731. (cond
  11732. ((and beg end)) ; beg and end given explicitly
  11733. ((org-region-active-p)
  11734. (setq beg (region-beginning) end (region-end)))
  11735. (t
  11736. (setq col (org-table-current-column))
  11737. (goto-char (org-table-begin))
  11738. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  11739. (error "No table data"))
  11740. (org-table-goto-column col)
  11741. ;not needed? (skip-chars-backward "^|")
  11742. (setq beg (point))
  11743. (goto-char (org-table-end))
  11744. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  11745. (error "No table data"))
  11746. (org-table-goto-column col)
  11747. ;not needed? (skip-chars-forward "^|")
  11748. (setq end (point))))
  11749. (let* ((items (apply 'append (org-table-copy-region beg end)))
  11750. (items1 (cond ((not nlast) items)
  11751. ((>= nlast (length items)) items)
  11752. (t (setq items (reverse items))
  11753. (setcdr (nthcdr (1- nlast) items) nil)
  11754. (nreverse items))))
  11755. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  11756. items1)))
  11757. (res (apply '+ numbers))
  11758. (sres (if (= timecnt 0)
  11759. (format "%g" res)
  11760. (setq diff (* 3600 res)
  11761. h (floor (/ diff 3600)) diff (mod diff 3600)
  11762. m (floor (/ diff 60)) diff (mod diff 60)
  11763. s diff)
  11764. (format "%d:%02d:%02d" h m s))))
  11765. (kill-new sres)
  11766. (if (interactive-p)
  11767. (message "%s"
  11768. (substitute-command-keys
  11769. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  11770. (length numbers) sres))))
  11771. sres))))
  11772. (defun org-table-get-number-for-summing (s)
  11773. (let (n)
  11774. (if (string-match "^ *|? *" s)
  11775. (setq s (replace-match "" nil nil s)))
  11776. (if (string-match " *|? *$" s)
  11777. (setq s (replace-match "" nil nil s)))
  11778. (setq n (string-to-number s))
  11779. (cond
  11780. ((and (string-match "0" s)
  11781. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  11782. ((string-match "\\`[ \t]+\\'" s) nil)
  11783. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  11784. (let ((h (string-to-number (or (match-string 1 s) "0")))
  11785. (m (string-to-number (or (match-string 2 s) "0")))
  11786. (s (string-to-number (or (match-string 4 s) "0"))))
  11787. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  11788. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  11789. ((equal n 0) nil)
  11790. (t n))))
  11791. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  11792. "Get a calc vector from a column, accorting to descriptor DESC.
  11793. Optional arguments TBEG and COL can give the beginning of the table and
  11794. the current column, to avoid unnecessary parsing."
  11795. (save-excursion
  11796. (or tbeg (setq tbeg (org-table-begin)))
  11797. (or col (setq col (org-table-current-column)))
  11798. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  11799. (cond
  11800. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  11801. (setq n1 (- (match-end 1) (match-beginning 1)))
  11802. (if (match-beginning 3)
  11803. (setq n2 (- (match-end 2) (match-beginning 3))))
  11804. (setq n (if n2 (max n1 n2) n1))
  11805. (setq n1 (if n2 (min n1 n2)))
  11806. (setq nn n)
  11807. (while (and (> nn 0)
  11808. (re-search-backward org-table-hline-regexp tbeg t))
  11809. (push (org-current-line) hline-list)
  11810. (setq nn (1- nn)))
  11811. (setq hline-list (nreverse hline-list))
  11812. (goto-line (nth (1- n) hline-list))
  11813. (when (re-search-forward org-table-dataline-regexp)
  11814. (org-table-goto-column col)
  11815. (setq beg (point)))
  11816. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  11817. (when (re-search-backward org-table-dataline-regexp)
  11818. (org-table-goto-column col)
  11819. (setq end (point)))
  11820. (setq l (apply 'append (org-table-copy-region beg end)))
  11821. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  11822. (if (equal x "") "0" x))
  11823. l ",") "]"))
  11824. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  11825. (setq n1 (string-to-number (match-string 1 desc))
  11826. n2 (string-to-number (match-string 2 desc)))
  11827. (beginning-of-line 1)
  11828. (save-excursion
  11829. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  11830. (org-table-goto-column col)
  11831. (setq beg (point))))
  11832. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  11833. (org-table-goto-column col)
  11834. (setq end (point)))
  11835. (setq l (apply 'append (org-table-copy-region beg end)))
  11836. (concat "[" (mapconcat
  11837. (lambda (x) (setq x (org-trim x))
  11838. (if (equal x "") "0" x))
  11839. l ",") "]"))
  11840. ((string-match "\\([0-9]+\\)" desc)
  11841. (beginning-of-line 1)
  11842. (when (re-search-backward org-table-dataline-regexp tbeg t
  11843. (string-to-number (match-string 0 desc)))
  11844. (org-table-goto-column col)
  11845. (org-trim (org-table-get-field))))))))
  11846. (defvar org-table-formula-history nil)
  11847. (defvar org-table-column-names nil
  11848. "Alist with column names, derived from the `!' line.")
  11849. (defvar org-table-column-name-regexp nil
  11850. "Regular expression matching the current column names.")
  11851. (defvar org-table-local-parameters nil
  11852. "Alist with parameter names, derived from the `$' line.")
  11853. (defvar org-table-named-field-locations nil
  11854. "Alist with locations of named fields.")
  11855. (defun org-table-get-formula (&optional equation named)
  11856. "Read a formula from the minibuffer, offer stored formula as default."
  11857. (let* ((name (car (rassoc (list (org-current-line)
  11858. (org-table-current-column))
  11859. org-table-named-field-locations)))
  11860. (scol (if named
  11861. (if name name
  11862. (error "Not in a named field"))
  11863. (int-to-string (org-table-current-column))))
  11864. (dummy (and name (not named)
  11865. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  11866. (error "Abort")))
  11867. (org-table-may-need-update nil)
  11868. (stored-list (org-table-get-stored-formulas))
  11869. (stored (cdr (assoc scol stored-list)))
  11870. (eq (cond
  11871. ((and stored equation (string-match "^ *=? *$" equation))
  11872. stored)
  11873. ((stringp equation)
  11874. equation)
  11875. (t (read-string
  11876. (format "%s formula $%s=" (if named "Field" "Column") scol)
  11877. (or stored "") 'org-table-formula-history
  11878. ;stored
  11879. ))))
  11880. mustsave)
  11881. (when (not (string-match "\\S-" eq))
  11882. ;; remove formula
  11883. (setq stored-list (delq (assoc scol stored-list) stored-list))
  11884. (org-table-store-formulas stored-list)
  11885. (error "Formula removed"))
  11886. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  11887. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  11888. (if (and name (not named))
  11889. ;; We set the column equation, delete the named one.
  11890. (setq stored-list (delq (assoc name stored-list) stored-list)
  11891. mustsave t))
  11892. (if stored
  11893. (setcdr (assoc scol stored-list) eq)
  11894. (setq stored-list (cons (cons scol eq) stored-list)))
  11895. (if (or mustsave (not (equal stored eq)))
  11896. (org-table-store-formulas stored-list))
  11897. eq))
  11898. (defun org-table-store-formulas (alist)
  11899. "Store the list of formulas below the current table."
  11900. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  11901. (save-excursion
  11902. (goto-char (org-table-end))
  11903. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  11904. (delete-region (point) (match-end 0)))
  11905. (insert "#+TBLFM: "
  11906. (mapconcat (lambda (x)
  11907. (concat "$" (car x) "=" (cdr x)))
  11908. alist "::")
  11909. "\n")))
  11910. (defun org-table-get-stored-formulas ()
  11911. "Return an alist with the stored formulas directly after current table."
  11912. (interactive)
  11913. (let (scol eq eq-alist strings string seen)
  11914. (save-excursion
  11915. (goto-char (org-table-end))
  11916. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  11917. (setq strings (org-split-string (match-string 2) " *:: *"))
  11918. (while (setq string (pop strings))
  11919. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  11920. (setq scol (match-string 1 string)
  11921. eq (match-string 2 string)
  11922. eq-alist (cons (cons scol eq) eq-alist))
  11923. (if (member scol seen)
  11924. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  11925. (push scol seen))))))
  11926. (nreverse eq-alist)))
  11927. (defun org-table-modify-formulas (action &rest columns)
  11928. "Modify the formulas stored below the current table.
  11929. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  11930. expected, for the other actions only a single column number is needed."
  11931. (let ((list (org-table-get-stored-formulas))
  11932. (nmax (length (org-split-string
  11933. (buffer-substring (point-at-bol) (point-at-eol))
  11934. "|")))
  11935. col col1 col2 scol si sc1 sc2)
  11936. (cond
  11937. ((null list)) ; No action needed if there are no stored formulas
  11938. ((eq action 'remove)
  11939. (setq col (car columns)
  11940. scol (int-to-string col))
  11941. (org-table-replace-in-formulas list scol "INVALID")
  11942. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  11943. (loop for i from (1+ col) upto nmax by 1 do
  11944. (setq si (int-to-string i))
  11945. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  11946. (if (assoc si list) (setcar (assoc si list)
  11947. (int-to-string (1- i))))))
  11948. ((eq action 'insert)
  11949. (setq col (car columns))
  11950. (loop for i from nmax downto col by 1 do
  11951. (setq si (int-to-string i))
  11952. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  11953. (if (assoc si list) (setcar (assoc si list)
  11954. (int-to-string (1+ i))))))
  11955. ((eq action 'swap)
  11956. (setq col1 (car columns) col2 (nth 1 columns)
  11957. sc1 (int-to-string col1) sc2 (int-to-string col2))
  11958. ;; Hopefully, ZqZtZ will never be a name in a table
  11959. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  11960. (org-table-replace-in-formulas list sc2 sc1)
  11961. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  11962. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  11963. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  11964. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  11965. (t (error "Invalid action in `org-table-modify-formulas'")))
  11966. (if list (org-table-store-formulas list))))
  11967. (defun org-table-replace-in-formulas (list s1 s2)
  11968. (let (elt re s)
  11969. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  11970. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  11971. re (concat (regexp-quote s1) "\\>"))
  11972. (while (setq elt (pop list))
  11973. (setq s (cdr elt))
  11974. (while (string-match re s)
  11975. (setq s (replace-match s2 t t s)))
  11976. (setcdr elt s))))
  11977. (defun org-table-get-specials ()
  11978. "Get the column names and local parameters for this table."
  11979. (save-excursion
  11980. (let ((beg (org-table-begin)) (end (org-table-end))
  11981. names name fields fields1 field cnt c v line col)
  11982. (setq org-table-column-names nil
  11983. org-table-local-parameters nil
  11984. org-table-named-field-locations nil)
  11985. (goto-char beg)
  11986. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  11987. (setq names (org-split-string (match-string 1) " *| *")
  11988. cnt 1)
  11989. (while (setq name (pop names))
  11990. (setq cnt (1+ cnt))
  11991. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  11992. (push (cons name (int-to-string cnt)) org-table-column-names))))
  11993. (setq org-table-column-names (nreverse org-table-column-names))
  11994. (setq org-table-column-name-regexp
  11995. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  11996. (goto-char beg)
  11997. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  11998. (setq fields (org-split-string (match-string 1) " *| *"))
  11999. (while (setq field (pop fields))
  12000. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  12001. (push (cons (match-string 1 field) (match-string 2 field))
  12002. org-table-local-parameters))))
  12003. (goto-char beg)
  12004. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  12005. (setq c (match-string 1)
  12006. fields (org-split-string (match-string 2) " *| *"))
  12007. (save-excursion
  12008. (beginning-of-line (if (equal c "_") 2 0))
  12009. (setq line (org-current-line) col 1)
  12010. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  12011. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  12012. (while (and fields1 (setq field (pop fields)))
  12013. (setq v (pop fields1) col (1+ col))
  12014. (when (and (stringp field) (stringp v)
  12015. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  12016. (push (cons field v) org-table-local-parameters)
  12017. (push (list field line col) org-table-named-field-locations)))))))
  12018. (defun org-this-word ()
  12019. ;; Get the current word
  12020. (save-excursion
  12021. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  12022. (end (progn (skip-chars-forward "^ \t\n") (point))))
  12023. (buffer-substring-no-properties beg end))))
  12024. (defun org-table-maybe-eval-formula ()
  12025. "Check if the current field starts with \"=\" or \":=\".
  12026. If yes, store the formula and apply it."
  12027. ;; We already know we are in a table. Get field will only return a formula
  12028. ;; when appropriate. It might return a separator line, but no problem.
  12029. (when org-table-formula-evaluate-inline
  12030. (let* ((field (org-trim (or (org-table-get-field) "")))
  12031. named eq)
  12032. (when (string-match "^:?=\\(.*\\)" field)
  12033. (setq named (equal (string-to-char field) ?:)
  12034. eq (match-string 1 field))
  12035. (if (fboundp 'calc-eval)
  12036. (org-table-eval-formula (if named '(4) nil) eq))))))
  12037. (defvar org-recalc-commands nil
  12038. "List of commands triggering the recalculation of a line.
  12039. Will be filled automatically during use.")
  12040. (defvar org-recalc-marks
  12041. '((" " . "Unmarked: no special line, no automatic recalculation")
  12042. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  12043. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  12044. ("!" . "Column name definition line. Reference in formula as $name.")
  12045. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  12046. ("_" . "Names for values in row below this one.")
  12047. ("^" . "Names for values in row above this one.")))
  12048. (defun org-table-rotate-recalc-marks (&optional newchar)
  12049. "Rotate the recalculation mark in the first column.
  12050. If in any row, the first field is not consistent with a mark,
  12051. insert a new column for the markers.
  12052. When there is an active region, change all the lines in the region,
  12053. after prompting for the marking character.
  12054. After each change, a message will be displayed indicating the meaning
  12055. of the new mark."
  12056. (interactive)
  12057. (unless (org-at-table-p) (error "Not at a table"))
  12058. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  12059. (beg (org-table-begin))
  12060. (end (org-table-end))
  12061. (l (org-current-line))
  12062. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  12063. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  12064. (have-col
  12065. (save-excursion
  12066. (goto-char beg)
  12067. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  12068. (col (org-table-current-column))
  12069. (forcenew (car (assoc newchar org-recalc-marks)))
  12070. epos new)
  12071. (when l1
  12072. (message "Change region to what mark? Type # * ! $ or SPC: ")
  12073. (setq newchar (char-to-string (read-char-exclusive))
  12074. forcenew (car (assoc newchar org-recalc-marks))))
  12075. (if (and newchar (not forcenew))
  12076. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  12077. newchar))
  12078. (if l1 (goto-line l1))
  12079. (save-excursion
  12080. (beginning-of-line 1)
  12081. (unless (looking-at org-table-dataline-regexp)
  12082. (error "Not at a table data line")))
  12083. (unless have-col
  12084. (org-table-goto-column 1)
  12085. (org-table-insert-column)
  12086. (org-table-goto-column (1+ col)))
  12087. (setq epos (point-at-eol))
  12088. (save-excursion
  12089. (beginning-of-line 1)
  12090. (org-table-get-field
  12091. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  12092. (concat " "
  12093. (setq new (or forcenew
  12094. (cadr (member (match-string 1) marks))))
  12095. " ")
  12096. " # ")))
  12097. (if (and l1 l2)
  12098. (progn
  12099. (goto-line l1)
  12100. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  12101. (and (looking-at org-table-dataline-regexp)
  12102. (org-table-get-field 1 (concat " " new " "))))
  12103. (goto-line l1)))
  12104. (if (not (= epos (point-at-eol))) (org-table-align))
  12105. (goto-line l)
  12106. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  12107. (defun org-table-maybe-recalculate-line ()
  12108. "Recompute the current line if marked for it, and if we haven't just done it."
  12109. (interactive)
  12110. (and org-table-allow-automatic-line-recalculation
  12111. (not (and (memq last-command org-recalc-commands)
  12112. (equal org-last-recalc-line (org-current-line))))
  12113. (save-excursion (beginning-of-line 1)
  12114. (looking-at org-table-auto-recalculate-regexp))
  12115. (fboundp 'calc-eval)
  12116. (org-table-recalculate) t))
  12117. (defvar org-table-formula-debug nil
  12118. "Non-nil means, debug table formulas.
  12119. When nil, simply write \"#ERROR\" in corrupted fields.")
  12120. (defvar modes)
  12121. (defsubst org-set-calc-mode (var &optional value)
  12122. (if (stringp var)
  12123. (setq var (assoc var '(("D" calc-angle-mode deg)
  12124. ("R" calc-angle-mode rad)
  12125. ("F" calc-prefer-frac t)
  12126. ("S" calc-symbolic-mode t)))
  12127. value (nth 2 var) var (nth 1 var)))
  12128. (if (memq var modes)
  12129. (setcar (cdr (memq var modes)) value)
  12130. (cons var (cons value modes)))
  12131. modes)
  12132. (defun org-table-eval-formula (&optional arg equation
  12133. suppress-align suppress-const
  12134. suppress-store)
  12135. "Replace the table field value at the cursor by the result of a calculation.
  12136. This function makes use of Dave Gillespie's Calc package, in my view the
  12137. most exciting program ever written for GNU Emacs. So you need to have Calc
  12138. installed in order to use this function.
  12139. In a table, this command replaces the value in the current field with the
  12140. result of a formula. It also installs the formula as the \"current\" column
  12141. formula, by storing it in a special line below the table. When called
  12142. with a `C-u' prefix, the current field must ba a named field, and the
  12143. formula is installed as valid in only this specific field.
  12144. When called, the command first prompts for a formula, which is read in
  12145. the minibuffer. Previously entered formulas are available through the
  12146. history list, and the last used formula is offered as a default.
  12147. These stored formulas are adapted correctly when moving, inserting, or
  12148. deleting columns with the corresponding commands.
  12149. The formula can be any algebraic expression understood by the Calc package.
  12150. For details, see the Org-mode manual.
  12151. This function can also be called from Lisp programs and offers
  12152. additional arguments: EQUATION can be the formula to apply. If this
  12153. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  12154. used to speed-up recursive calls by by-passing unnecessary aligns.
  12155. SUPPRESS-CONST suppresses the interpretation of constants in the
  12156. formula, assuming that this has been done already outside the function.
  12157. SUPPRESS-STORE means the formula should not be stored, either because
  12158. it is already stored, or because it is a modified equation that should
  12159. not overwrite the stored one."
  12160. (interactive "P")
  12161. (require 'calc)
  12162. (org-table-check-inside-data-field)
  12163. (org-table-get-specials)
  12164. (let* (fields
  12165. (ndown (if (integerp arg) arg 1))
  12166. (org-table-automatic-realign nil)
  12167. (case-fold-search nil)
  12168. (down (> ndown 1))
  12169. (formula (if (and equation suppress-store)
  12170. equation
  12171. (org-table-get-formula equation (equal arg '(4)))))
  12172. (n0 (org-table-current-column))
  12173. (modes (copy-sequence org-calc-default-modes))
  12174. n form fmt x ev orig c lispp)
  12175. ;; Parse the format string. Since we have a lot of modes, this is
  12176. ;; a lot of work. However, I think calc still uses most of the time.
  12177. (if (string-match ";" formula)
  12178. (let ((tmp (org-split-string formula ";")))
  12179. (setq formula (car tmp)
  12180. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  12181. (nth 1 tmp)))
  12182. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  12183. (setq c (string-to-char (match-string 1 fmt))
  12184. n (string-to-number (match-string 2 fmt)))
  12185. (if (= c ?p)
  12186. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  12187. (setq modes (org-set-calc-mode
  12188. 'calc-float-format
  12189. (list (cdr (assoc c '((?n . float) (?f . fix)
  12190. (?s . sci) (?e . eng))))
  12191. n))))
  12192. (setq fmt (replace-match "" t t fmt)))
  12193. (while (string-match "[DRFS]" fmt)
  12194. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  12195. (setq fmt (replace-match "" t t fmt)))
  12196. (unless (string-match "\\S-" fmt)
  12197. (setq fmt nil))))
  12198. (if (and (not suppress-const) org-table-formula-use-constants)
  12199. (setq formula (org-table-formula-substitute-names formula)))
  12200. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  12201. (while (> ndown 0)
  12202. (setq fields (org-split-string
  12203. (buffer-substring
  12204. (point-at-bol) (point-at-eol)) " *| *"))
  12205. (if org-table-formula-numbers-only
  12206. (setq fields (mapcar
  12207. (lambda (x) (number-to-string (string-to-number x)))
  12208. fields)))
  12209. (setq ndown (1- ndown))
  12210. (setq form (copy-sequence formula)
  12211. lispp (equal (substring form 0 2) "'("))
  12212. ;; Insert the references to fields in same row
  12213. (while (string-match "\\$\\([0-9]+\\)?" form)
  12214. (setq n (if (match-beginning 1)
  12215. (string-to-number (match-string 1 form))
  12216. n0)
  12217. x (nth (1- n) fields))
  12218. (unless x (error "Invalid field specifier \"%s\""
  12219. (match-string 0 form)))
  12220. (if (equal x "") (setq x "0"))
  12221. (setq form (replace-match
  12222. (if lispp x (concat "(" x ")"))
  12223. t t form)))
  12224. ;; Insert ranges in current column
  12225. (while (string-match "\\&[-I0-9]+" form)
  12226. (setq form (replace-match
  12227. (save-match-data
  12228. (org-table-get-vertical-vector (match-string 0 form)
  12229. nil n0))
  12230. t t form)))
  12231. (if lispp
  12232. (setq ev (eval (eval (read form)))
  12233. ev (if (numberp ev) (number-to-string ev) ev))
  12234. (setq ev (calc-eval (cons form modes)
  12235. (if org-table-formula-numbers-only 'num))))
  12236. (when org-table-formula-debug
  12237. (with-output-to-temp-buffer "*Help*"
  12238. (princ (format "Substitution history of formula
  12239. Orig: %s
  12240. $xyz-> %s
  12241. $1-> %s\n" orig formula form))
  12242. (if (listp ev)
  12243. (princ (format " %s^\nError: %s"
  12244. (make-string (car ev) ?\-) (nth 1 ev)))
  12245. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  12246. ev (or fmt "NONE")
  12247. (if fmt (format fmt (string-to-number ev)) ev)))))
  12248. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  12249. (unless (and (interactive-p) (not ndown))
  12250. (unless (let (inhibit-redisplay)
  12251. (y-or-n-p "Debugging Formula. Continue to next? "))
  12252. (org-table-align)
  12253. (error "Abort"))
  12254. (delete-window (get-buffer-window "*Help*"))
  12255. (message "")))
  12256. (if (listp ev) (setq fmt nil ev "#ERROR"))
  12257. (org-table-justify-field-maybe
  12258. (if fmt (format fmt (string-to-number ev)) ev))
  12259. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  12260. (call-interactively 'org-return)
  12261. (setq ndown 0)))
  12262. (and down (org-table-maybe-recalculate-line))
  12263. (or suppress-align (and org-table-may-need-update
  12264. (org-table-align)))))
  12265. (defun org-table-recalculate (&optional all noalign)
  12266. "Recalculate the current table line by applying all stored formulas.
  12267. With prefix arg ALL, do this for all lines in the table."
  12268. (interactive "P")
  12269. (or (memq this-command org-recalc-commands)
  12270. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  12271. (unless (org-at-table-p) (error "Not at a table"))
  12272. (org-table-get-specials)
  12273. (let* ((eqlist (sort (org-table-get-stored-formulas)
  12274. (lambda (a b) (string< (car a) (car b)))))
  12275. (inhibit-redisplay t)
  12276. (line-re org-table-dataline-regexp)
  12277. (thisline (org-current-line))
  12278. (thiscol (org-table-current-column))
  12279. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  12280. ;; Insert constants in all formulas
  12281. (setq eqlist
  12282. (mapcar (lambda (x)
  12283. (setcdr x (org-table-formula-substitute-names (cdr x)))
  12284. x)
  12285. eqlist))
  12286. ;; Split the equation list
  12287. (while (setq eq (pop eqlist))
  12288. (if (<= (string-to-char (car eq)) ?9)
  12289. (push eq eqlnum)
  12290. (push eq eqlname)))
  12291. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  12292. (if all
  12293. (progn
  12294. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  12295. (goto-char (setq beg (org-table-begin)))
  12296. (if (re-search-forward org-table-calculate-mark-regexp end t)
  12297. ;; This is a table with marked lines, only compute selected lines
  12298. (setq line-re org-table-recalculate-regexp)
  12299. ;; Move forward to the first non-header line
  12300. (if (and (re-search-forward org-table-dataline-regexp end t)
  12301. (re-search-forward org-table-hline-regexp end t)
  12302. (re-search-forward org-table-dataline-regexp end t))
  12303. (setq beg (match-beginning 0))
  12304. nil))) ;; just leave beg where it is
  12305. (setq beg (point-at-bol)
  12306. end (move-marker (make-marker) (1+ (point-at-eol)))))
  12307. (goto-char beg)
  12308. (and all (message "Re-applying formulas to full table..."))
  12309. (while (re-search-forward line-re end t)
  12310. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  12311. ;; Unprotected line, recalculate
  12312. (and all (message "Re-applying formulas to full table...(line %d)"
  12313. (setq cnt (1+ cnt))))
  12314. (setq org-last-recalc-line (org-current-line))
  12315. (setq eql eqlnum)
  12316. (while (setq entry (pop eql))
  12317. (goto-line org-last-recalc-line)
  12318. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  12319. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  12320. (goto-line thisline)
  12321. (org-table-goto-column thiscol)
  12322. (or noalign (and org-table-may-need-update (org-table-align))
  12323. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  12324. ;; Now do the names fields
  12325. (while (setq eq (pop eqlname))
  12326. (setq name (car eq)
  12327. a (assoc name org-table-named-field-locations))
  12328. (when a
  12329. (message "Re-applying formula to named field: %s" name)
  12330. (goto-line (nth 1 a))
  12331. (org-table-goto-column (nth 2 a))
  12332. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  12333. ;; back to initial position
  12334. (goto-line thisline)
  12335. (org-table-goto-column thiscol)
  12336. (or noalign (and org-table-may-need-update (org-table-align))
  12337. (and all (message "Re-applying formulas...done")))))
  12338. (defun org-table-formula-substitute-names (f)
  12339. "Replace $const with values in string F."
  12340. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  12341. ;; First, check for column names
  12342. (while (setq start (string-match org-table-column-name-regexp f start))
  12343. (setq start (1+ start))
  12344. (setq a (assoc (match-string 1 f) org-table-column-names))
  12345. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  12346. ;; Expand ranges to vectors
  12347. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  12348. (setq n1 (string-to-number (match-string 1 f))
  12349. n2 (string-to-number (match-string 2 f))
  12350. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  12351. s (concat "[($" (number-to-string (1- nn1)) ")"))
  12352. (loop for i from nn1 upto nn2 do
  12353. (setq s (concat s ",($" (int-to-string i) ")")))
  12354. (setq s (concat s "]"))
  12355. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  12356. (setq f (replace-match s t t f)))
  12357. ;; Parameters and constants
  12358. (setq start 0)
  12359. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  12360. (setq start (1+ start))
  12361. (if (setq a (save-match-data
  12362. (org-table-get-constant (match-string 1 f))))
  12363. (setq f (replace-match (concat "(" a ")") t t f))))
  12364. (if org-table-formula-debug
  12365. (put-text-property 0 (length f) :orig-formula f1 f))
  12366. f))
  12367. (defun org-table-get-constant (const)
  12368. "Find the value for a parameter or constant in a formula.
  12369. Parameters get priority."
  12370. (or (cdr (assoc const org-table-local-parameters))
  12371. (cdr (assoc const org-table-formula-constants))
  12372. (and (fboundp 'constants-get) (constants-get const))
  12373. "#UNDEFINED_NAME"))
  12374. (defvar org-edit-formulas-map (make-sparse-keymap))
  12375. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  12376. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  12377. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  12378. (defvar org-pos)
  12379. (defun org-table-edit-formulas ()
  12380. "Edit the formulas of the current table in a separate buffer."
  12381. (interactive)
  12382. (unless (org-at-table-p)
  12383. (error "Not at a table"))
  12384. (org-table-get-specials)
  12385. (let ((eql (org-table-get-stored-formulas))
  12386. (pos (move-marker (make-marker) (point)))
  12387. (wc (current-window-configuration))
  12388. entry loc s)
  12389. (switch-to-buffer-other-window "*Edit Formulas*")
  12390. (erase-buffer)
  12391. (fundamental-mode)
  12392. (org-set-local 'org-pos pos)
  12393. (org-set-local 'org-window-configuration wc)
  12394. (use-local-map org-edit-formulas-map)
  12395. (setq s "# Edit formulas and finish with `C-c C-c'.
  12396. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  12397. # Use `C-c ?' to get information about $name at point.
  12398. # To cancel editing, press `C-c C-q'.\n")
  12399. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  12400. (insert s)
  12401. (while (setq entry (pop eql))
  12402. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  12403. (setq s (format "# Named formula, referring to column %d in line %d\n"
  12404. (nth 2 loc) (nth 1 loc)))
  12405. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  12406. (insert s))
  12407. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  12408. (remove-text-properties 0 (length s) '(face nil) s)
  12409. (insert s))
  12410. (goto-char (point-min))
  12411. (message "Edit formulas and finish with `C-c C-c'.")))
  12412. (defun org-show-variable ()
  12413. "Show the location/value of the $ expression at point."
  12414. (interactive)
  12415. (let (var (pos org-pos) (win (selected-window)) e)
  12416. (save-excursion
  12417. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  12418. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  12419. (setq var (match-string 1))
  12420. (error "No variable at point")))
  12421. (cond
  12422. ((setq e (assoc var org-table-named-field-locations))
  12423. (switch-to-buffer-other-window (marker-buffer pos))
  12424. (goto-line (nth 1 e))
  12425. (org-table-goto-column (nth 2 e))
  12426. (select-window win)
  12427. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  12428. ((setq e (assoc var org-table-column-names))
  12429. (switch-to-buffer-other-window (marker-buffer pos))
  12430. (goto-char pos)
  12431. (goto-char (org-table-begin))
  12432. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  12433. (org-table-end) t)
  12434. (progn
  12435. (goto-char (match-beginning 1))
  12436. (message "Named column (column %s)" (cdr e)))
  12437. (error "Column name not found"))
  12438. (select-window win))
  12439. ((string-match "^[0-9]$" var)
  12440. ;; column number
  12441. (switch-to-buffer-other-window (marker-buffer pos))
  12442. (goto-char pos)
  12443. (goto-char (org-table-begin))
  12444. (recenter 1)
  12445. (if (re-search-forward org-table-dataline-regexp
  12446. (org-table-end) t)
  12447. (progn
  12448. (goto-char (match-beginning 0))
  12449. (org-table-goto-column (string-to-number var))
  12450. (message "Column %s" var))
  12451. (error "Column name not found"))
  12452. (select-window win))
  12453. ((setq e (assoc var org-table-local-parameters))
  12454. (switch-to-buffer-other-window (marker-buffer pos))
  12455. (goto-char pos)
  12456. (goto-char (org-table-begin))
  12457. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  12458. (progn
  12459. (goto-char (match-beginning 1))
  12460. (message "Local parameter."))
  12461. (error "Parameter not found"))
  12462. (select-window win))
  12463. (t
  12464. (cond
  12465. ((setq e (assoc var org-table-formula-constants))
  12466. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  12467. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  12468. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  12469. (t (error "Undefined name $%s" var)))))))
  12470. (defun org-finish-edit-formulas (&optional arg)
  12471. "Parse the buffer for formula definitions and install them.
  12472. With prefix ARG, apply the new formulas to the table."
  12473. (interactive "P")
  12474. (let ((pos org-pos) eql)
  12475. (goto-char (point-min))
  12476. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  12477. (push (cons (match-string 1) (match-string 2)) eql))
  12478. (set-window-configuration org-window-configuration)
  12479. (select-window (get-buffer-window (marker-buffer pos)))
  12480. (goto-char pos)
  12481. (unless (org-at-table-p)
  12482. (error "Lost table position - cannot install formulae"))
  12483. (org-table-store-formulas eql)
  12484. (move-marker pos nil)
  12485. (kill-buffer "*Edit Formulas*")
  12486. (if arg
  12487. (org-table-recalculate 'all)
  12488. (message "New formulas installed - press C-u C-c C-c to apply."))))
  12489. (defun org-abort-edit-formulas ()
  12490. "Abort editing formulas, without installing the changes."
  12491. (interactive)
  12492. (let ((pos org-pos))
  12493. (set-window-configuration org-window-configuration)
  12494. (select-window (get-buffer-window (marker-buffer pos)))
  12495. (goto-char pos)
  12496. (message "Formula editing aborted without installing changes")))
  12497. ;;; The orgtbl minor mode
  12498. ;; Define a minor mode which can be used in other modes in order to
  12499. ;; integrate the org-mode table editor.
  12500. ;; This is really a hack, because the org-mode table editor uses several
  12501. ;; keys which normally belong to the major mode, for example the TAB and
  12502. ;; RET keys. Here is how it works: The minor mode defines all the keys
  12503. ;; necessary to operate the table editor, but wraps the commands into a
  12504. ;; function which tests if the cursor is currently inside a table. If that
  12505. ;; is the case, the table editor command is executed. However, when any of
  12506. ;; those keys is used outside a table, the function uses `key-binding' to
  12507. ;; look up if the key has an associated command in another currently active
  12508. ;; keymap (minor modes, major mode, global), and executes that command.
  12509. ;; There might be problems if any of the keys used by the table editor is
  12510. ;; otherwise used as a prefix key.
  12511. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  12512. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  12513. ;; addresses this by checking explicitly for both bindings.
  12514. ;; The optimized version (see variable `orgtbl-optimized') takes over
  12515. ;; all keys which are bound to `self-insert-command' in the *global map*.
  12516. ;; Some modes bind other commands to simple characters, for example
  12517. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  12518. ;; active, this binding is ignored inside tables and replaced with a
  12519. ;; modified self-insert.
  12520. (defvar orgtbl-mode nil
  12521. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  12522. table editor in arbitrary modes.")
  12523. (make-variable-buffer-local 'orgtbl-mode)
  12524. (defvar orgtbl-mode-map (make-keymap)
  12525. "Keymap for `orgtbl-mode'.")
  12526. ;;;###autoload
  12527. (defun turn-on-orgtbl ()
  12528. "Unconditionally turn on `orgtbl-mode'."
  12529. (orgtbl-mode 1))
  12530. ;;;###autoload
  12531. (defun orgtbl-mode (&optional arg)
  12532. "The `org-mode' table editor as a minor mode for use in other modes."
  12533. (interactive)
  12534. (if (org-mode-p)
  12535. ;; Exit without error, in case some hook functions calls this
  12536. ;; by accident in org-mode.
  12537. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  12538. (setq orgtbl-mode
  12539. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  12540. (if orgtbl-mode
  12541. (progn
  12542. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  12543. ;; Make sure we are first in minor-mode-map-alist
  12544. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  12545. (and c (setq minor-mode-map-alist
  12546. (cons c (delq c minor-mode-map-alist)))))
  12547. (org-set-local (quote org-table-may-need-update) t)
  12548. (org-add-hook 'before-change-functions 'org-before-change-function
  12549. nil 'local)
  12550. (org-set-local 'org-old-auto-fill-inhibit-regexp
  12551. auto-fill-inhibit-regexp)
  12552. (org-set-local 'auto-fill-inhibit-regexp
  12553. (if auto-fill-inhibit-regexp
  12554. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  12555. "[ \t]*|"))
  12556. (org-add-to-invisibility-spec '(org-cwidth))
  12557. (easy-menu-add orgtbl-mode-menu)
  12558. (run-hooks 'orgtbl-mode-hook))
  12559. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  12560. (org-cleanup-narrow-column-properties)
  12561. (org-remove-from-invisibility-spec '(org-cwidth))
  12562. (remove-hook 'before-change-functions 'org-before-change-function t)
  12563. (easy-menu-remove orgtbl-mode-menu)
  12564. (force-mode-line-update 'all))))
  12565. (defun org-cleanup-narrow-column-properties ()
  12566. "Remove all properties related to narrow-column invisibility."
  12567. (let ((s 1))
  12568. (while (setq s (text-property-any s (point-max)
  12569. 'display org-narrow-column-arrow))
  12570. (remove-text-properties s (1+ s) '(display t)))
  12571. (setq s 1)
  12572. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  12573. (remove-text-properties s (1+ s) '(org-cwidth t)))
  12574. (setq s 1)
  12575. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  12576. (remove-text-properties s (1+ s) '(invisible t)))))
  12577. ;; Install it as a minor mode.
  12578. (put 'orgtbl-mode :included t)
  12579. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  12580. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  12581. (defun orgtbl-make-binding (fun n &rest keys)
  12582. "Create a function for binding in the table minor mode.
  12583. FUN is the command to call inside a table. N is used to create a unique
  12584. command name. KEYS are keys that should be checked in for a command
  12585. to execute outside of tables."
  12586. (eval
  12587. (list 'defun
  12588. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  12589. '(arg)
  12590. (concat "In tables, run `" (symbol-name fun) "'.\n"
  12591. "Outside of tables, run the binding of `"
  12592. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  12593. "'.")
  12594. '(interactive "p")
  12595. (list 'if
  12596. '(org-at-table-p)
  12597. (list 'call-interactively (list 'quote fun))
  12598. (list 'let '(orgtbl-mode)
  12599. (list 'call-interactively
  12600. (append '(or)
  12601. (mapcar (lambda (k)
  12602. (list 'key-binding k))
  12603. keys)
  12604. '('orgtbl-error))))))))
  12605. (defun orgtbl-error ()
  12606. "Error when there is no default binding for a table key."
  12607. (interactive)
  12608. (error "This key is has no function outside tables"))
  12609. (defun orgtbl-setup ()
  12610. "Setup orgtbl keymaps."
  12611. (let ((nfunc 0)
  12612. (bindings
  12613. (list
  12614. '([(meta shift left)] org-table-delete-column)
  12615. '([(meta left)] org-table-move-column-left)
  12616. '([(meta right)] org-table-move-column-right)
  12617. '([(meta shift right)] org-table-insert-column)
  12618. '([(meta shift up)] org-table-kill-row)
  12619. '([(meta shift down)] org-table-insert-row)
  12620. '([(meta up)] org-table-move-row-up)
  12621. '([(meta down)] org-table-move-row-down)
  12622. '("\C-c\C-w" org-table-cut-region)
  12623. '("\C-c\M-w" org-table-copy-region)
  12624. '("\C-c\C-y" org-table-paste-rectangle)
  12625. '("\C-c-" org-table-insert-hline)
  12626. ; '([(shift tab)] org-table-previous-field)
  12627. '("\C-m" org-table-next-row)
  12628. (list (org-key 'S-return) 'org-table-copy-down)
  12629. '([(meta return)] org-table-wrap-region)
  12630. '("\C-c\C-q" org-table-wrap-region)
  12631. '("\C-c?" org-table-current-column)
  12632. '("\C-c " org-table-blank-field)
  12633. '("\C-c+" org-table-sum)
  12634. '("\C-c=" org-table-eval-formula)
  12635. '("\C-c'" org-table-edit-formulas)
  12636. '("\C-c`" org-table-edit-field)
  12637. '("\C-c*" org-table-recalculate)
  12638. '("\C-c|" org-table-create-or-convert-from-region)
  12639. '("\C-c^" org-table-sort-lines)
  12640. '([(control ?#)] org-table-rotate-recalc-marks)))
  12641. elt key fun cmd)
  12642. (while (setq elt (pop bindings))
  12643. (setq nfunc (1+ nfunc))
  12644. (setq key (car elt)
  12645. fun (nth 1 elt)
  12646. cmd (orgtbl-make-binding fun nfunc key))
  12647. (define-key orgtbl-mode-map key cmd))
  12648. ;; Special treatment needed for TAB and RET
  12649. (define-key orgtbl-mode-map [(return)]
  12650. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  12651. (define-key orgtbl-mode-map "\C-m"
  12652. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  12653. (define-key orgtbl-mode-map [(tab)]
  12654. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  12655. (define-key orgtbl-mode-map "\C-i"
  12656. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  12657. (define-key orgtbl-mode-map "\C-i"
  12658. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  12659. (define-key orgtbl-mode-map "\C-c\C-c"
  12660. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  12661. (when orgtbl-optimized
  12662. ;; If the user wants maximum table support, we need to hijack
  12663. ;; some standard editing functions
  12664. (org-remap orgtbl-mode-map
  12665. 'self-insert-command 'orgtbl-self-insert-command
  12666. 'delete-char 'org-delete-char
  12667. 'delete-backward-char 'org-delete-backward-char)
  12668. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  12669. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  12670. '("OrgTbl"
  12671. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  12672. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  12673. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  12674. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  12675. "--"
  12676. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  12677. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  12678. ["Copy Field from Above"
  12679. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  12680. "--"
  12681. ("Column"
  12682. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  12683. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  12684. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  12685. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  12686. "--"
  12687. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  12688. ("Row"
  12689. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  12690. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  12691. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  12692. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  12693. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  12694. "--"
  12695. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  12696. ("Rectangle"
  12697. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  12698. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  12699. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  12700. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  12701. "--"
  12702. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  12703. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12704. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  12705. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  12706. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  12707. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  12708. ["Sum Column/Rectangle" org-table-sum
  12709. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  12710. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  12711. ["Debug Formulas"
  12712. (setq org-table-formula-debug (not org-table-formula-debug))
  12713. :style toggle :selected org-table-formula-debug]
  12714. ))
  12715. t)
  12716. (defun orgtbl-tab (arg)
  12717. "Justification and field motion for `orgtbl-mode'."
  12718. (interactive "P")
  12719. (if arg (org-table-edit-field t)
  12720. (org-table-justify-field-maybe)
  12721. (org-table-next-field)))
  12722. (defun orgtbl-ret ()
  12723. "Justification and field motion for `orgtbl-mode'."
  12724. (interactive)
  12725. (org-table-justify-field-maybe)
  12726. (org-table-next-row))
  12727. (defun orgtbl-self-insert-command (N)
  12728. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12729. If the cursor is in a table looking at whitespace, the whitespace is
  12730. overwritten, and the table is not marked as requiring realignment."
  12731. (interactive "p")
  12732. (if (and (org-at-table-p)
  12733. (or
  12734. (and org-table-auto-blank-field
  12735. (member last-command
  12736. '(orgtbl-hijacker-command-100
  12737. orgtbl-hijacker-command-101
  12738. orgtbl-hijacker-command-102
  12739. orgtbl-hijacker-command-103
  12740. orgtbl-hijacker-command-104
  12741. orgtbl-hijacker-command-105))
  12742. (org-table-blank-field))
  12743. t)
  12744. (eq N 1)
  12745. (looking-at "[^|\n]* +|"))
  12746. (let (org-table-may-need-update)
  12747. (goto-char (1- (match-end 0)))
  12748. (delete-backward-char 1)
  12749. (goto-char (match-beginning 0))
  12750. (self-insert-command N))
  12751. (setq org-table-may-need-update t)
  12752. (let (orgtbl-mode)
  12753. (call-interactively (key-binding (vector last-input-event))))))
  12754. (defun org-force-self-insert (N)
  12755. "Needed to enforce self-insert under remapping."
  12756. (interactive "p")
  12757. (self-insert-command N))
  12758. ;;; Exporting
  12759. (defconst org-level-max 20)
  12760. (defvar org-export-html-preamble nil
  12761. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  12762. (defvar org-export-html-postamble nil
  12763. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  12764. (defvar org-export-html-auto-preamble t
  12765. "Should default preamble be inserted? Set by publishing functions.")
  12766. (defvar org-export-html-auto-postamble t
  12767. "Should default postamble be inserted? Set by publishing functions.")
  12768. (defconst org-export-plist-vars
  12769. '((:language . org-export-default-language)
  12770. (:headline-levels . org-export-headline-levels)
  12771. (:section-numbers . org-export-with-section-numbers)
  12772. (:table-of-contents . org-export-with-toc)
  12773. (:archived-trees . org-export-with-archived-trees)
  12774. (:emphasize . org-export-with-emphasize)
  12775. (:sub-superscript . org-export-with-sub-superscripts)
  12776. (:TeX-macros . org-export-with-TeX-macros)
  12777. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  12778. (:fixed-width . org-export-with-fixed-width)
  12779. (:timestamps . org-export-with-timestamps)
  12780. (:tables . org-export-with-tables)
  12781. (:table-auto-headline . org-export-highlight-first-table-line)
  12782. (:style . org-export-html-style)
  12783. (:convert-org-links . org-export-html-link-org-files-as-html)
  12784. (:inline-images . org-export-html-inline-images)
  12785. (:expand-quoted-html . org-export-html-expand)
  12786. (:timestamp . org-export-html-with-timestamp)
  12787. (:publishing-directory . org-export-publishing-directory)
  12788. (:preamble . org-export-html-preamble)
  12789. (:postamble . org-export-html-postamble)
  12790. (:auto-preamble . org-export-html-auto-preamble)
  12791. (:auto-postamble . org-export-html-auto-postamble)
  12792. (:author . user-full-name)
  12793. (:email . user-mail-address)))
  12794. (defun org-default-export-plist ()
  12795. "Return the property list with default settings for the export variables."
  12796. (let ((l org-export-plist-vars) rtn e)
  12797. (while (setq e (pop l))
  12798. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  12799. rtn))
  12800. (defun org-infile-export-plist ()
  12801. "Return the property list with file-local settings for export."
  12802. (save-excursion
  12803. (goto-char 0)
  12804. (let ((re (org-make-options-regexp
  12805. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  12806. p key val text options)
  12807. (while (re-search-forward re nil t)
  12808. (setq key (org-match-string-no-properties 1)
  12809. val (org-match-string-no-properties 2))
  12810. (cond
  12811. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  12812. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  12813. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  12814. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  12815. ((string-equal key "TEXT")
  12816. (setq text (if text (concat text "\n" val) val)))
  12817. ((string-equal key "OPTIONS") (setq options val))))
  12818. (setq p (plist-put p :text text))
  12819. (when options
  12820. (let ((op '(("H" . :headline-levels)
  12821. ("num" . :section-numbers)
  12822. ("toc" . :table-of-contents)
  12823. ("\\n" . :preserve-breaks)
  12824. ("@" . :expand-quoted-html)
  12825. (":" . :fixed-width)
  12826. ("|" . :tables)
  12827. ("^" . :sub-superscript)
  12828. ("*" . :emphasize)
  12829. ("TeX" . :TeX-macros)
  12830. ("LaTeX" . :LaTeX-fragments)))
  12831. o)
  12832. (while (setq o (pop op))
  12833. (if (string-match (concat (regexp-quote (car o))
  12834. ":\\([^ \t\n\r;,.]*\\)")
  12835. options)
  12836. (setq p (plist-put p (cdr o)
  12837. (car (read-from-string
  12838. (match-string 1 options)))))))))
  12839. p)))
  12840. (defun org-combine-plists (&rest plists)
  12841. "Create a single property list from all plists in PLISTS.
  12842. The process starts by copying the last list, and then setting properties
  12843. from the other lists. Settings in the first list are the most significant
  12844. ones and overrule settings in the other lists."
  12845. (let ((rtn (copy-sequence (pop plists)))
  12846. p v ls)
  12847. (while plists
  12848. (setq ls (pop plists))
  12849. (while ls
  12850. (setq p (pop ls) v (pop ls))
  12851. (setq rtn (plist-put rtn p v))))
  12852. rtn))
  12853. (defun org-export-directory (type plist)
  12854. (let* ((val (plist-get plist :publishing-directory))
  12855. (dir (if (listp val)
  12856. (or (cdr (assoc type val)) ".")
  12857. val)))
  12858. dir))
  12859. (defun org-export-find-first-heading-line (list)
  12860. "Remove all lines from LIST which are before the first headline."
  12861. (let ((orig-list list)
  12862. (re (concat "^" outline-regexp)))
  12863. (while (and list
  12864. (not (string-match re (car list))))
  12865. (pop list))
  12866. (or list orig-list)))
  12867. (defun org-skip-comments (lines)
  12868. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  12869. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  12870. (re2 "^\\(\\*+\\)[ \t\n\r]")
  12871. rtn line level)
  12872. (while (setq line (pop lines))
  12873. (cond
  12874. ((and (string-match re1 line)
  12875. (setq level (- (match-end 1) (match-beginning 1))))
  12876. ;; Beginning of a COMMENT subtree. Skip it.
  12877. (while (and (setq line (pop lines))
  12878. (or (not (string-match re2 line))
  12879. (> (- (match-end 1) (match-beginning 1)) level))))
  12880. (setq lines (cons line lines)))
  12881. ((string-match "^#" line)
  12882. ;; an ordinary comment line
  12883. )
  12884. ((and org-export-table-remove-special-lines
  12885. (string-match "^[ \t]*|" line)
  12886. (or (string-match "^[ \t]*| *[!_^] *|" line)
  12887. (and (string-match "| *<[0-9]+> *|" line)
  12888. (not (string-match "| *[^ <|]" line)))))
  12889. ;; a special table line that should be removed
  12890. )
  12891. (t (setq rtn (cons line rtn)))))
  12892. (nreverse rtn)))
  12893. (defun org-export (&optional arg)
  12894. (interactive)
  12895. (let ((help "[t] insert the export option template
  12896. \[v] limit export to visible part of outline tree
  12897. \[a] export as ASCII
  12898. \[h] export as HTML
  12899. \[b] export as HTML and browse immediately
  12900. \[x] export as XOXO
  12901. \[i] export current file as iCalendar file
  12902. \[I] export all agenda files as iCalendar files
  12903. \[c] export agenda files into combined iCalendar file
  12904. \[F] publish current file
  12905. \[P] publish current project
  12906. \[X] publish... (project will be prompted for)
  12907. \[A] publish all projects")
  12908. (cmds
  12909. '((?t . org-insert-export-options-template)
  12910. (?v . org-export-visible)
  12911. (?a . org-export-as-ascii)
  12912. (?h . org-export-as-html)
  12913. (?b . org-export-as-html-and-open)
  12914. (?x . org-export-as-xoxo)
  12915. (?i . org-export-icalendar-this-file)
  12916. (?I . org-export-icalendar-all-agenda-files)
  12917. (?c . org-export-icalendar-combine-agenda-files)
  12918. (?F . org-publish-current-file)
  12919. (?P . org-publish-current-project)
  12920. (?X . org-publish)
  12921. (?A . org-publish-all)))
  12922. r1 r2 ass)
  12923. (save-window-excursion
  12924. (delete-other-windows)
  12925. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  12926. (princ help))
  12927. (message "Select command: ")
  12928. (setq r1 (read-char-exclusive)))
  12929. (setq r2 (if (< r1 27) (+ r1 96) r1))
  12930. (if (setq ass (assq r2 cmds))
  12931. (call-interactively (cdr ass))
  12932. (error "No command associated with key %c" r1))))
  12933. ;; ASCII
  12934. (defconst org-html-entities
  12935. '(("nbsp")
  12936. ("iexcl")
  12937. ("cent")
  12938. ("pound")
  12939. ("curren")
  12940. ("yen")
  12941. ("brvbar")
  12942. ("sect")
  12943. ("uml")
  12944. ("copy")
  12945. ("ordf")
  12946. ("laquo")
  12947. ("not")
  12948. ("shy")
  12949. ("reg")
  12950. ("macr")
  12951. ("deg")
  12952. ("plusmn")
  12953. ("sup2")
  12954. ("sup3")
  12955. ("acute")
  12956. ("micro")
  12957. ("para")
  12958. ("middot")
  12959. ("odot"."o")
  12960. ("star"."*")
  12961. ("cedil")
  12962. ("sup1")
  12963. ("ordm")
  12964. ("raquo")
  12965. ("frac14")
  12966. ("frac12")
  12967. ("frac34")
  12968. ("iquest")
  12969. ("Agrave")
  12970. ("Aacute")
  12971. ("Acirc")
  12972. ("Atilde")
  12973. ("Auml")
  12974. ("Aring") ("AA"."&Aring;")
  12975. ("AElig")
  12976. ("Ccedil")
  12977. ("Egrave")
  12978. ("Eacute")
  12979. ("Ecirc")
  12980. ("Euml")
  12981. ("Igrave")
  12982. ("Iacute")
  12983. ("Icirc")
  12984. ("Iuml")
  12985. ("ETH")
  12986. ("Ntilde")
  12987. ("Ograve")
  12988. ("Oacute")
  12989. ("Ocirc")
  12990. ("Otilde")
  12991. ("Ouml")
  12992. ("times")
  12993. ("Oslash")
  12994. ("Ugrave")
  12995. ("Uacute")
  12996. ("Ucirc")
  12997. ("Uuml")
  12998. ("Yacute")
  12999. ("THORN")
  13000. ("szlig")
  13001. ("agrave")
  13002. ("aacute")
  13003. ("acirc")
  13004. ("atilde")
  13005. ("auml")
  13006. ("aring")
  13007. ("aelig")
  13008. ("ccedil")
  13009. ("egrave")
  13010. ("eacute")
  13011. ("ecirc")
  13012. ("euml")
  13013. ("igrave")
  13014. ("iacute")
  13015. ("icirc")
  13016. ("iuml")
  13017. ("eth")
  13018. ("ntilde")
  13019. ("ograve")
  13020. ("oacute")
  13021. ("ocirc")
  13022. ("otilde")
  13023. ("ouml")
  13024. ("divide")
  13025. ("oslash")
  13026. ("ugrave")
  13027. ("uacute")
  13028. ("ucirc")
  13029. ("uuml")
  13030. ("yacute")
  13031. ("thorn")
  13032. ("yuml")
  13033. ("fnof")
  13034. ("Alpha")
  13035. ("Beta")
  13036. ("Gamma")
  13037. ("Delta")
  13038. ("Epsilon")
  13039. ("Zeta")
  13040. ("Eta")
  13041. ("Theta")
  13042. ("Iota")
  13043. ("Kappa")
  13044. ("Lambda")
  13045. ("Mu")
  13046. ("Nu")
  13047. ("Xi")
  13048. ("Omicron")
  13049. ("Pi")
  13050. ("Rho")
  13051. ("Sigma")
  13052. ("Tau")
  13053. ("Upsilon")
  13054. ("Phi")
  13055. ("Chi")
  13056. ("Psi")
  13057. ("Omega")
  13058. ("alpha")
  13059. ("beta")
  13060. ("gamma")
  13061. ("delta")
  13062. ("epsilon")
  13063. ("varepsilon"."&epsilon;")
  13064. ("zeta")
  13065. ("eta")
  13066. ("theta")
  13067. ("iota")
  13068. ("kappa")
  13069. ("lambda")
  13070. ("mu")
  13071. ("nu")
  13072. ("xi")
  13073. ("omicron")
  13074. ("pi")
  13075. ("rho")
  13076. ("sigmaf") ("varsigma"."&sigmaf;")
  13077. ("sigma")
  13078. ("tau")
  13079. ("upsilon")
  13080. ("phi")
  13081. ("chi")
  13082. ("psi")
  13083. ("omega")
  13084. ("thetasym") ("vartheta"."&thetasym;")
  13085. ("upsih")
  13086. ("piv")
  13087. ("bull") ("bullet"."&bull;")
  13088. ("hellip") ("dots"."&hellip;")
  13089. ("prime")
  13090. ("Prime")
  13091. ("oline")
  13092. ("frasl")
  13093. ("weierp")
  13094. ("image")
  13095. ("real")
  13096. ("trade")
  13097. ("alefsym")
  13098. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  13099. ("uarr") ("uparrow"."&uarr;")
  13100. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  13101. ("darr")("downarrow"."&darr;")
  13102. ("harr") ("leftrightarrow"."&harr;")
  13103. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  13104. ("lArr") ("Leftarrow"."&lArr;")
  13105. ("uArr") ("Uparrow"."&uArr;")
  13106. ("rArr") ("Rightarrow"."&rArr;")
  13107. ("dArr") ("Downarrow"."&dArr;")
  13108. ("hArr") ("Leftrightarrow"."&hArr;")
  13109. ("forall")
  13110. ("part") ("partial"."&part;")
  13111. ("exist") ("exists"."&exist;")
  13112. ("empty") ("emptyset"."&empty;")
  13113. ("nabla")
  13114. ("isin") ("in"."&isin;")
  13115. ("notin")
  13116. ("ni")
  13117. ("prod")
  13118. ("sum")
  13119. ("minus")
  13120. ("lowast") ("ast"."&lowast;")
  13121. ("radic")
  13122. ("prop") ("proptp"."&prop;")
  13123. ("infin") ("infty"."&infin;")
  13124. ("ang") ("angle"."&ang;")
  13125. ("and") ("vee"."&and;")
  13126. ("or") ("wedge"."&or;")
  13127. ("cap")
  13128. ("cup")
  13129. ("int")
  13130. ("there4")
  13131. ("sim")
  13132. ("cong") ("simeq"."&cong;")
  13133. ("asymp")("approx"."&asymp;")
  13134. ("ne") ("neq"."&ne;")
  13135. ("equiv")
  13136. ("le")
  13137. ("ge")
  13138. ("sub") ("subset"."&sub;")
  13139. ("sup") ("supset"."&sup;")
  13140. ("nsub")
  13141. ("sube")
  13142. ("supe")
  13143. ("oplus")
  13144. ("otimes")
  13145. ("perp")
  13146. ("sdot") ("cdot"."&sdot;")
  13147. ("lceil")
  13148. ("rceil")
  13149. ("lfloor")
  13150. ("rfloor")
  13151. ("lang")
  13152. ("rang")
  13153. ("loz") ("Diamond"."&loz;")
  13154. ("spades") ("spadesuit"."&spades;")
  13155. ("clubs") ("clubsuit"."&clubs;")
  13156. ("hearts") ("diamondsuit"."&hearts;")
  13157. ("diams") ("diamondsuit"."&diams;")
  13158. ("quot")
  13159. ("amp")
  13160. ("lt")
  13161. ("gt")
  13162. ("OElig")
  13163. ("oelig")
  13164. ("Scaron")
  13165. ("scaron")
  13166. ("Yuml")
  13167. ("circ")
  13168. ("tilde")
  13169. ("ensp")
  13170. ("emsp")
  13171. ("thinsp")
  13172. ("zwnj")
  13173. ("zwj")
  13174. ("lrm")
  13175. ("rlm")
  13176. ("ndash")
  13177. ("mdash")
  13178. ("lsquo")
  13179. ("rsquo")
  13180. ("sbquo")
  13181. ("ldquo")
  13182. ("rdquo")
  13183. ("bdquo")
  13184. ("dagger")
  13185. ("Dagger")
  13186. ("permil")
  13187. ("lsaquo")
  13188. ("rsaquo")
  13189. ("euro")
  13190. ("arccos"."arccos")
  13191. ("arcsin"."arcsin")
  13192. ("arctan"."arctan")
  13193. ("arg"."arg")
  13194. ("cos"."cos")
  13195. ("cosh"."cosh")
  13196. ("cot"."cot")
  13197. ("coth"."coth")
  13198. ("csc"."csc")
  13199. ("deg"."deg")
  13200. ("det"."det")
  13201. ("dim"."dim")
  13202. ("exp"."exp")
  13203. ("gcd"."gcd")
  13204. ("hom"."hom")
  13205. ("inf"."inf")
  13206. ("ker"."ker")
  13207. ("lg"."lg")
  13208. ("lim"."lim")
  13209. ("liminf"."liminf")
  13210. ("limsup"."limsup")
  13211. ("ln"."ln")
  13212. ("log"."log")
  13213. ("max"."max")
  13214. ("min"."min")
  13215. ("Pr"."Pr")
  13216. ("sec"."sec")
  13217. ("sin"."sin")
  13218. ("sinh"."sinh")
  13219. ("sup"."sup")
  13220. ("tan"."tan")
  13221. ("tanh"."tanh")
  13222. )
  13223. "Entities for TeX->HTML translation.
  13224. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  13225. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  13226. In that case, \"\\ent\" will be translated to \"&other;\".
  13227. The list contains HTML entities for Latin-1, Greek and other symbols.
  13228. It is supplemented by a number of commonly used TeX macros with appropriate
  13229. translations. There is currently no way for users to extend this.")
  13230. (defun org-cleaned-string-for-export (string &rest parameters)
  13231. "Cleanup a buffer substring so that links can be created safely."
  13232. (interactive)
  13233. (let* ((re-radio (and org-target-link-regexp
  13234. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  13235. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  13236. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  13237. (re-archive (concat ":" org-archive-tag ":"))
  13238. rtn)
  13239. (save-excursion
  13240. (set-buffer (get-buffer-create " org-mode-tmp"))
  13241. (erase-buffer)
  13242. (insert string)
  13243. (let ((org-inhibit-startup t)) (org-mode))
  13244. ;; Get rid of archived trees
  13245. (when (not (eq org-export-with-archived-trees t))
  13246. (goto-char (point-min))
  13247. (while (re-search-forward re-archive nil t)
  13248. (if (not (org-on-heading-p))
  13249. (org-end-of-subtree t)
  13250. (beginning-of-line 1)
  13251. (delete-region
  13252. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  13253. (org-end-of-subtree t)))))
  13254. ;; Find targets in comments and move them out of comments,
  13255. ;; but mark them as targets that should be invisible
  13256. (goto-char (point-min))
  13257. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  13258. (replace-match "\\1(INVISIBLE)"))
  13259. ;; Remove comments
  13260. (goto-char (point-min))
  13261. (while (re-search-forward "^#.*\n?" nil t)
  13262. (replace-match ""))
  13263. ;; Find matches for radio targets and turn them into internal links
  13264. (goto-char (point-min))
  13265. (when re-radio
  13266. (while (re-search-forward re-radio nil t)
  13267. (replace-match "\\1[[\\2]]")))
  13268. ;; Find all links that contain a newline and put them into a single line
  13269. (goto-char (point-min))
  13270. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  13271. (replace-match "\\1 \\3")
  13272. (goto-char (match-beginning 0)))
  13273. ;; Convert LaTeX fragments to images
  13274. (when (memq :LaTeX-fragments parameters)
  13275. (org-format-latex
  13276. (concat "ltxpng/" (file-name-sans-extension
  13277. (file-name-nondirectory
  13278. org-current-export-file)))
  13279. org-current-export-dir nil "Creating LaTeX image %s"))
  13280. (message "Exporting...")
  13281. ;; Normalize links: Convert angle and plain links into bracket links
  13282. ;; Expand link abbreviations
  13283. (goto-char (point-min))
  13284. (while (re-search-forward re-plain-link nil t)
  13285. (replace-match
  13286. (concat
  13287. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  13288. t t))
  13289. (goto-char (point-min))
  13290. (while (re-search-forward re-angle-link nil t)
  13291. (replace-match
  13292. (concat
  13293. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  13294. t t))
  13295. (goto-char (point-min))
  13296. (while (re-search-forward org-bracket-link-regexp nil t)
  13297. (replace-match
  13298. (concat "[[" (save-match-data
  13299. (org-link-expand-abbrev (match-string 1)))
  13300. "]"
  13301. (if (match-end 3)
  13302. (match-string 2)
  13303. (concat "[" (match-string 1) "]"))
  13304. "]")
  13305. t t))
  13306. ;; Find multiline emphasis and put them into single line
  13307. (when (memq :emph-multiline parameters)
  13308. (goto-char (point-min))
  13309. (while (re-search-forward org-emph-re nil t)
  13310. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  13311. (goto-char (1- (match-end 0)))))
  13312. (setq rtn (buffer-string)))
  13313. (kill-buffer " org-mode-tmp")
  13314. rtn))
  13315. (defun org-solidify-link-text (s &optional alist)
  13316. "Take link text and make a safe target out of it."
  13317. (save-match-data
  13318. (let* ((rtn
  13319. (mapconcat
  13320. 'identity
  13321. (org-split-string s "[ \t\r\n]+") "--"))
  13322. (a (assoc rtn alist)))
  13323. (or (cdr a) rtn))))
  13324. (defun org-convert-to-odd-levels ()
  13325. "Convert an org-mode file with all levels allowed to one with odd levels.
  13326. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  13327. level 5 etc."
  13328. (interactive)
  13329. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  13330. (let ((org-odd-levels-only nil) n)
  13331. (save-excursion
  13332. (goto-char (point-min))
  13333. (while (re-search-forward "^\\*\\*+" nil t)
  13334. (setq n (1- (length (match-string 0))))
  13335. (while (>= (setq n (1- n)) 0)
  13336. (org-demote))
  13337. (end-of-line 1))))))
  13338. (defun org-convert-to-oddeven-levels ()
  13339. "Convert an org-mode file with only odd levels to one with odd and even levels.
  13340. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  13341. section with an even level, conversion would destroy the structure of the file. An error
  13342. is signaled in this case."
  13343. (interactive)
  13344. (goto-char (point-min))
  13345. ;; First check if there are no even levels
  13346. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  13347. (org-show-context t)
  13348. (error "Not all levels are odd in this file. Conversion not possible."))
  13349. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  13350. (let ((org-odd-levels-only nil) n)
  13351. (save-excursion
  13352. (goto-char (point-min))
  13353. (while (re-search-forward "^\\*\\*+" nil t)
  13354. (setq n (/ (length (match-string 0)) 2))
  13355. (while (>= (setq n (1- n)) 0)
  13356. (org-promote))
  13357. (end-of-line 1))))))
  13358. (defun org-tr-level (n)
  13359. "Make N odd if required."
  13360. (if org-odd-levels-only (1+ (/ n 2)) n))
  13361. (defvar org-last-level nil) ; dynamically scoped variable
  13362. (defvar org-ascii-current-indentation nil) ; For communication
  13363. (defun org-export-as-ascii (arg)
  13364. "Export the outline as a pretty ASCII file.
  13365. If there is an active region, export only the region.
  13366. The prefix ARG specifies how many levels of the outline should become
  13367. underlined headlines. The default is 3."
  13368. (interactive "P")
  13369. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13370. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13371. (org-infile-export-plist)))
  13372. (region
  13373. (buffer-substring
  13374. (if (org-region-active-p) (region-beginning) (point-min))
  13375. (if (org-region-active-p) (region-end) (point-max))))
  13376. (lines (org-export-find-first-heading-line
  13377. (org-skip-comments
  13378. (org-split-string
  13379. (org-cleaned-string-for-export region)
  13380. "[\r\n]"))))
  13381. (org-ascii-current-indentation '(0 . 0))
  13382. (org-startup-with-deadline-check nil)
  13383. (level 0) line txt
  13384. (umax nil)
  13385. (case-fold-search nil)
  13386. (filename (concat (file-name-as-directory
  13387. (org-export-directory :ascii opt-plist))
  13388. (file-name-sans-extension
  13389. (file-name-nondirectory buffer-file-name))
  13390. ".txt"))
  13391. (buffer (find-file-noselect filename))
  13392. (levels-open (make-vector org-level-max nil))
  13393. (odd org-odd-levels-only)
  13394. (date (format-time-string "%Y/%m/%d" (current-time)))
  13395. (time (format-time-string "%X" (org-current-time)))
  13396. (author (plist-get opt-plist :author))
  13397. (title (or (plist-get opt-plist :title)
  13398. (file-name-sans-extension
  13399. (file-name-nondirectory buffer-file-name))))
  13400. (email (plist-get opt-plist :email))
  13401. (language (plist-get opt-plist :language))
  13402. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13403. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13404. (text nil)
  13405. (todo nil)
  13406. (lang-words nil))
  13407. (setq org-last-level 1)
  13408. (org-init-section-numbers)
  13409. (find-file-noselect filename)
  13410. (setq lang-words (or (assoc language org-export-language-setup)
  13411. (assoc "en" org-export-language-setup)))
  13412. (if org-export-ascii-show-new-buffer
  13413. (switch-to-buffer-other-window buffer)
  13414. (set-buffer buffer))
  13415. (erase-buffer)
  13416. (fundamental-mode)
  13417. ;; create local variables for all options, to make sure all called
  13418. ;; functions get the correct information
  13419. (mapcar (lambda (x)
  13420. (set (make-local-variable (cdr x))
  13421. (plist-get opt-plist (car x))))
  13422. org-export-plist-vars)
  13423. (org-set-local 'org-odd-levels-only odd)
  13424. (setq umax (if arg (prefix-numeric-value arg)
  13425. org-export-headline-levels))
  13426. ;; File header
  13427. (if title (org-insert-centered title ?=))
  13428. (insert "\n")
  13429. (if (or author email)
  13430. (insert (concat (nth 1 lang-words) ": " (or author "")
  13431. (if email (concat " <" email ">") "")
  13432. "\n")))
  13433. (if (and date time)
  13434. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  13435. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  13436. (insert "\n\n")
  13437. (if org-export-with-toc
  13438. (progn
  13439. (insert (nth 3 lang-words) "\n"
  13440. (make-string (length (nth 3 lang-words)) ?=) "\n")
  13441. (mapcar '(lambda (line)
  13442. (if (string-match org-todo-line-regexp
  13443. line)
  13444. ;; This is a headline
  13445. (progn
  13446. (setq level (- (match-end 1) (match-beginning 1))
  13447. level (org-tr-level level)
  13448. txt (match-string 3 line)
  13449. todo
  13450. (or (and org-export-mark-todo-in-toc
  13451. (match-beginning 2)
  13452. (not (equal (match-string 2 line)
  13453. org-done-string)))
  13454. ; TODO, not DONE
  13455. (and org-export-mark-todo-in-toc
  13456. (= level umax)
  13457. (org-search-todo-below
  13458. line lines level))))
  13459. (setq txt (org-html-expand-for-ascii txt))
  13460. (if (and (memq org-export-with-tags '(not-in-toc nil))
  13461. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  13462. (setq txt (replace-match "" t t txt)))
  13463. (if (string-match quote-re0 txt)
  13464. (setq txt (replace-match "" t t txt)))
  13465. (if org-export-with-section-numbers
  13466. (setq txt (concat (org-section-number level)
  13467. " " txt)))
  13468. (if (<= level umax)
  13469. (progn
  13470. (insert
  13471. (make-string (* (1- level) 4) ?\ )
  13472. (format (if todo "%s (*)\n" "%s\n") txt))
  13473. (setq org-last-level level))
  13474. ))))
  13475. lines)))
  13476. (org-init-section-numbers)
  13477. (while (setq line (pop lines))
  13478. ;; Remove the quoted HTML tags.
  13479. (setq line (org-html-expand-for-ascii line))
  13480. ;; Remove targets
  13481. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  13482. (setq line (replace-match "" t t line)))
  13483. ;; Replace internal links
  13484. (while (string-match org-bracket-link-regexp line)
  13485. (setq line (replace-match
  13486. (if (match-end 3) "[\\3]" "[\\1]")
  13487. t nil line)))
  13488. (cond
  13489. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13490. ;; a Headline
  13491. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13492. txt (match-string 2 line))
  13493. (org-ascii-level-start level txt umax lines))
  13494. (t
  13495. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  13496. (normal-mode)
  13497. (save-buffer)
  13498. ;; remove display and invisible chars
  13499. (let (beg end)
  13500. (goto-char (point-min))
  13501. (while (setq beg (next-single-property-change (point) 'display))
  13502. (setq end (next-single-property-change beg 'display))
  13503. (delete-region beg end)
  13504. (goto-char beg)
  13505. (insert "=>"))
  13506. (goto-char (point-min))
  13507. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  13508. (setq end (next-single-property-change beg 'org-cwidth))
  13509. (delete-region beg end)
  13510. (goto-char beg)))
  13511. (goto-char (point-min))))
  13512. (defun org-search-todo-below (line lines level)
  13513. "Search the subtree below LINE for any TODO entries."
  13514. (let ((rest (cdr (memq line lines)))
  13515. (re org-todo-line-regexp)
  13516. line lv todo)
  13517. (catch 'exit
  13518. (while (setq line (pop rest))
  13519. (if (string-match re line)
  13520. (progn
  13521. (setq lv (- (match-end 1) (match-beginning 1))
  13522. todo (and (match-beginning 2)
  13523. (not (equal (match-string 2 line)
  13524. org-done-string))))
  13525. ; TODO, not DONE
  13526. (if (<= lv level) (throw 'exit nil))
  13527. (if todo (throw 'exit t))))))))
  13528. (defun org-html-expand-for-ascii (line)
  13529. "Handle quoted HTML for ASCII export."
  13530. (if org-export-html-expand
  13531. (while (string-match "@<[^<>\n]*>" line)
  13532. ;; We just remove the tags for now.
  13533. (setq line (replace-match "" nil nil line))))
  13534. line)
  13535. (defun org-insert-centered (s &optional underline)
  13536. "Insert the string S centered and underline it with character UNDERLINE."
  13537. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  13538. (insert (make-string ind ?\ ) s "\n")
  13539. (if underline
  13540. (insert (make-string ind ?\ )
  13541. (make-string (string-width s) underline)
  13542. "\n"))))
  13543. (defun org-ascii-level-start (level title umax &optional lines)
  13544. "Insert a new level in ASCII export."
  13545. (let (char (n (- level umax 1)) (ind 0))
  13546. (if (> level umax)
  13547. (progn
  13548. (insert (make-string (* 2 n) ?\ )
  13549. (char-to-string (nth (% n (length org-export-ascii-bullets))
  13550. org-export-ascii-bullets))
  13551. " " title "\n")
  13552. ;; find the indentation of the next non-empty line
  13553. (catch 'stop
  13554. (while lines
  13555. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  13556. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  13557. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  13558. (pop lines)))
  13559. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  13560. (if (or (not (equal (char-before) ?\n))
  13561. (not (equal (char-before (1- (point))) ?\n)))
  13562. (insert "\n"))
  13563. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  13564. (unless org-export-with-tags
  13565. (if (string-match "[ \t]+\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13566. (setq title (replace-match "" t t title))))
  13567. (if org-export-with-section-numbers
  13568. (setq title (concat (org-section-number level) " " title)))
  13569. (insert title "\n" (make-string (string-width title) char) "\n")
  13570. (setq org-ascii-current-indentation '(0 . 0)))))
  13571. (defun org-export-visible (type arg)
  13572. "Create a copy of the visible part of the current buffer, and export it.
  13573. The copy is created in a temporary buffer and removed after use.
  13574. TYPE is the final key (as a string) that also select the export command in
  13575. the `C-c C-e' export dispatcher.
  13576. As a special case, if the you type SPC at the prompt, the temporary
  13577. org-mode file will not be removed but presented to you so that you can
  13578. continue to use it. The prefix arg ARG is passed through to the exporting
  13579. command."
  13580. (interactive
  13581. (list (progn
  13582. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  13583. (read-char-exclusive))
  13584. current-prefix-arg))
  13585. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  13586. (error "Invalid export key"))
  13587. (let* ((binding (cdr (assoc type
  13588. '((?a . org-export-as-ascii)
  13589. (?\C-a . org-export-as-ascii)
  13590. (?b . org-export-as-html-and-open)
  13591. (?\C-b . org-export-as-html-and-open)
  13592. (?h . org-export-as-html)
  13593. (?x . org-export-as-xoxo)))))
  13594. (keepp (equal type ?\ ))
  13595. (file buffer-file-name)
  13596. (buffer (get-buffer-create "*Org Export Visible*"))
  13597. s e)
  13598. (with-current-buffer buffer (erase-buffer))
  13599. (save-excursion
  13600. (setq s (goto-char (point-min)))
  13601. (while (not (= (point) (point-max)))
  13602. (goto-char (org-find-invisible))
  13603. (append-to-buffer buffer s (point))
  13604. (setq s (goto-char (org-find-visible))))
  13605. (goto-char (point-min))
  13606. (unless keepp
  13607. ;; Copy all comment lines to the end, to make sure #+ settings are
  13608. ;; still available for the second export step. Kind of a hack, but
  13609. ;; does do the trick.
  13610. (if (looking-at "#[^\r\n]*")
  13611. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  13612. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  13613. (append-to-buffer buffer (1+ (match-beginning 0))
  13614. (min (point-max) (1+ (match-end 0))))))
  13615. (set-buffer buffer)
  13616. (let ((buffer-file-name file)
  13617. (org-inhibit-startup t))
  13618. (org-mode)
  13619. (show-all)
  13620. (unless keepp (funcall binding arg))))
  13621. (if (not keepp)
  13622. (kill-buffer buffer)
  13623. (switch-to-buffer-other-window buffer)
  13624. (goto-char (point-min)))))
  13625. (defun org-find-visible ()
  13626. (let ((s (point)))
  13627. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13628. (get-char-property s 'invisible)))
  13629. s))
  13630. (defun org-find-invisible ()
  13631. (let ((s (point)))
  13632. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  13633. (not (get-char-property s 'invisible))))
  13634. s))
  13635. ;; HTML
  13636. (defun org-get-current-options ()
  13637. "Return a string with current options as keyword options.
  13638. Does include HTML export options as well as TODO and CATEGORY stuff."
  13639. (format
  13640. "#+TITLE: %s
  13641. #+AUTHOR: %s
  13642. #+EMAIL: %s
  13643. #+LANGUAGE: %s
  13644. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  13645. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  13646. #+CATEGORY: %s
  13647. #+SEQ_TODO: %s
  13648. #+TYP_TODO: %s
  13649. #+STARTUP: %s %s %s %s %s %s
  13650. #+TAGS: %s
  13651. #+ARCHIVE: %s
  13652. #+LINK: %s
  13653. "
  13654. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  13655. org-export-headline-levels
  13656. org-export-with-section-numbers
  13657. org-export-with-toc
  13658. org-export-preserve-breaks
  13659. org-export-html-expand
  13660. org-export-with-fixed-width
  13661. org-export-with-tables
  13662. org-export-with-sub-superscripts
  13663. org-export-with-emphasize
  13664. org-export-with-TeX-macros
  13665. org-export-with-LaTeX-fragments
  13666. (file-name-nondirectory buffer-file-name)
  13667. (if (equal org-todo-interpretation 'sequence)
  13668. (mapconcat 'identity org-todo-keywords " ")
  13669. "TODO FEEDBACK VERIFY DONE")
  13670. (if (equal org-todo-interpretation 'type)
  13671. (mapconcat 'identity org-todo-keywords " ")
  13672. "Me Jason Marie DONE")
  13673. (cdr (assoc org-startup-folded
  13674. '((nil . "showall") (t . "overview") (content . "content"))))
  13675. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  13676. (if org-odd-levels-only "odd" "oddeven")
  13677. (if org-hide-leading-stars "hidestars" "showstars")
  13678. (if org-startup-align-all-tables "align" "noalign")
  13679. (if org-log-done "logging" "nologging")
  13680. (or (mapconcat (lambda (x)
  13681. (cond
  13682. ((equal '(:startgroup) x) "{")
  13683. ((equal '(:endgroup) x) "}")
  13684. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  13685. (t (car x))))
  13686. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  13687. org-archive-location
  13688. "org file:~/org/%s.org"
  13689. ))
  13690. (defun org-insert-export-options-template ()
  13691. "Insert into the buffer a template with information for exporting."
  13692. (interactive)
  13693. (if (not (bolp)) (newline))
  13694. (let ((s (org-get-current-options)))
  13695. (and (string-match "#\\+CATEGORY" s)
  13696. (setq s (substring s 0 (match-beginning 0))))
  13697. (insert s)))
  13698. (defun org-toggle-fixed-width-section (arg)
  13699. "Toggle the fixed-width export.
  13700. If there is no active region, the QUOTE keyword at the current headline is
  13701. inserted or removed. When present, it causes the text between this headline
  13702. and the next to be exported as fixed-width text, and unmodified.
  13703. If there is an active region, this command adds or removes a colon as the
  13704. first character of this line. If the first character of a line is a colon,
  13705. this line is also exported in fixed-width font."
  13706. (interactive "P")
  13707. (let* ((cc 0)
  13708. (regionp (org-region-active-p))
  13709. (beg (if regionp (region-beginning) (point)))
  13710. (end (if regionp (region-end)))
  13711. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13712. (re "[ \t]*\\(:\\)")
  13713. off)
  13714. (if regionp
  13715. (save-excursion
  13716. (goto-char beg)
  13717. (setq cc (current-column))
  13718. (beginning-of-line 1)
  13719. (setq off (looking-at re))
  13720. (while (> nlines 0)
  13721. (setq nlines (1- nlines))
  13722. (beginning-of-line 1)
  13723. (cond
  13724. (arg
  13725. (move-to-column cc t)
  13726. (insert ":\n")
  13727. (forward-line -1))
  13728. ((and off (looking-at re))
  13729. (replace-match "" t t nil 1))
  13730. ((not off) (move-to-column cc t) (insert ":")))
  13731. (forward-line 1)))
  13732. (save-excursion
  13733. (org-back-to-heading)
  13734. (if (looking-at (concat outline-regexp
  13735. "\\( +\\<" org-quote-string "\\>\\)"))
  13736. (replace-match "" t t nil 1)
  13737. (if (looking-at outline-regexp)
  13738. (progn
  13739. (goto-char (match-end 0))
  13740. (insert " " org-quote-string))))))))
  13741. (defun org-export-as-html-and-open (arg)
  13742. "Export the outline as HTML and immediately open it with a browser.
  13743. If there is an active region, export only the region.
  13744. The prefix ARG specifies how many levels of the outline should become
  13745. headlines. The default is 3. Lower levels will become bulleted lists."
  13746. (interactive "P")
  13747. (org-export-as-html arg 'hidden)
  13748. (org-open-file buffer-file-name))
  13749. (defun org-export-as-html-batch ()
  13750. "Call `org-export-as-html', may be used in batch processing as
  13751. emacs --batch
  13752. --load=$HOME/lib/emacs/org.el
  13753. --eval \"(setq org-export-headline-levels 2)\"
  13754. --visit=MyFile --funcall org-export-as-html-batch"
  13755. (org-export-as-html org-export-headline-levels 'hidden))
  13756. (defun org-export-as-html (arg &optional hidden ext-plist)
  13757. "Export the outline as a pretty HTML file.
  13758. If there is an active region, export only the region.
  13759. The prefix ARG specifies how many levels of the outline should become
  13760. headlines. The default is 3. Lower levels will become bulleted lists.
  13761. When HIDDEN is non-nil, don't display the HTML buffer.
  13762. EXT-PLIST is a property list with external parameters overriding
  13763. org-mode's default settings, but still inferior to file-local settings."
  13764. (interactive "P")
  13765. (message "Exporting...")
  13766. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13767. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  13768. (setq-default org-done-string org-done-string)
  13769. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13770. ext-plist
  13771. (org-infile-export-plist)))
  13772. (style (plist-get opt-plist :style))
  13773. (link-validate (plist-get opt-plist :link-validation-function))
  13774. valid
  13775. (odd org-odd-levels-only)
  13776. (region-p (org-region-active-p))
  13777. (region
  13778. (buffer-substring
  13779. (if region-p (region-beginning) (point-min))
  13780. (if region-p (region-end) (point-max))))
  13781. ;; The following two are dynamically scoped into other
  13782. ;; routines below.
  13783. (org-current-export-dir (org-export-directory :html opt-plist))
  13784. (org-current-export-file buffer-file-name)
  13785. (all_lines
  13786. (org-skip-comments (org-split-string
  13787. (org-cleaned-string-for-export
  13788. region :emph-multiline
  13789. (if (plist-get opt-plist :LaTeX-fragments)
  13790. :LaTeX-fragments))
  13791. "[\r\n]")))
  13792. (lines (org-export-find-first-heading-line all_lines))
  13793. (level 0) (line "") (origline "") txt todo
  13794. (umax nil)
  13795. (filename (concat (file-name-as-directory
  13796. (org-export-directory :html opt-plist))
  13797. (file-name-sans-extension
  13798. (file-name-nondirectory buffer-file-name))
  13799. ".html"))
  13800. (current-dir (file-name-directory buffer-file-name))
  13801. (buffer (find-file-noselect filename))
  13802. (levels-open (make-vector org-level-max nil))
  13803. (date (format-time-string "%Y/%m/%d" (current-time)))
  13804. (time (format-time-string "%X" (org-current-time)))
  13805. (author (plist-get opt-plist :author))
  13806. (title (or (plist-get opt-plist :title)
  13807. (file-name-sans-extension
  13808. (file-name-nondirectory buffer-file-name))))
  13809. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13810. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13811. (inquote nil)
  13812. (infixed nil)
  13813. (in-local-list nil)
  13814. (local-list-num nil)
  13815. (local-list-indent nil)
  13816. (llt org-plain-list-ordered-item-terminator)
  13817. (email (plist-get opt-plist :email))
  13818. (language (plist-get opt-plist :language))
  13819. (text (plist-get opt-plist :text))
  13820. (lang-words nil)
  13821. (target-alist nil) tg
  13822. (head-count 0) cnt
  13823. (start 0)
  13824. (coding-system (and (boundp 'buffer-file-coding-system)
  13825. buffer-file-coding-system))
  13826. (coding-system-for-write coding-system)
  13827. (save-buffer-coding-system coding-system)
  13828. (charset (and coding-system
  13829. (fboundp 'coding-system-get)
  13830. (coding-system-get coding-system 'mime-charset)))
  13831. table-open type
  13832. table-buffer table-orig-buffer
  13833. ind start-is-num starter
  13834. rpl path desc descp desc1 desc2 link
  13835. )
  13836. (message "Exporting...")
  13837. (setq org-last-level 1)
  13838. (org-init-section-numbers)
  13839. ;; Get the language-dependent settings
  13840. (setq lang-words (or (assoc language org-export-language-setup)
  13841. (assoc "en" org-export-language-setup)))
  13842. ;; Switch to the output buffer
  13843. (if (or hidden (not org-export-html-show-new-buffer))
  13844. (set-buffer buffer)
  13845. (switch-to-buffer-other-window buffer))
  13846. (erase-buffer)
  13847. (fundamental-mode)
  13848. (let ((case-fold-search nil)
  13849. (org-odd-levels-only odd))
  13850. ;; create local variables for all options, to make sure all called
  13851. ;; functions get the correct information
  13852. (mapcar (lambda (x)
  13853. (set (make-local-variable (cdr x))
  13854. (plist-get opt-plist (car x))))
  13855. org-export-plist-vars)
  13856. (setq umax (if arg (prefix-numeric-value arg)
  13857. org-export-headline-levels))
  13858. ;; File header
  13859. (insert (format
  13860. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  13861. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  13862. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  13863. lang=\"%s\" xml:lang=\"%s\">
  13864. <head>
  13865. <title>%s</title>
  13866. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  13867. <meta name=\"generator\" content=\"Org-mode\"/>
  13868. <meta name=\"generated\" content=\"%s %s\"/>
  13869. <meta name=\"author\" content=\"%s\"/>
  13870. %s
  13871. </head><body>
  13872. "
  13873. language language (org-html-expand title) (or charset "iso-8859-1")
  13874. date time author style))
  13875. (insert (or (plist-get opt-plist :preamble) ""))
  13876. (when (plist-get opt-plist :auto-preamble)
  13877. (if title (insert (format org-export-html-title-format
  13878. (org-html-expand title))))
  13879. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  13880. (if org-export-with-toc
  13881. (progn
  13882. (insert (format "<h%d>%s</h%d>\n"
  13883. org-export-html-toplevel-hlevel
  13884. (nth 3 lang-words)
  13885. org-export-html-toplevel-hlevel))
  13886. (insert "<ul>\n<li>")
  13887. (setq lines
  13888. (mapcar '(lambda (line)
  13889. (if (string-match org-todo-line-regexp line)
  13890. ;; This is a headline
  13891. (progn
  13892. (setq level (- (match-end 1) (match-beginning 1))
  13893. level (org-tr-level level)
  13894. txt (save-match-data
  13895. (org-html-expand
  13896. (org-export-cleanup-toc-line
  13897. (match-string 3 line))))
  13898. todo
  13899. (or (and org-export-mark-todo-in-toc
  13900. (match-beginning 2)
  13901. (not (equal (match-string 2 line)
  13902. org-done-string)))
  13903. ; TODO, not DONE
  13904. (and org-export-mark-todo-in-toc
  13905. (= level umax)
  13906. (org-search-todo-below
  13907. line lines level))))
  13908. (if (and (memq org-export-with-tags '(not-in-toc nil))
  13909. (string-match "[ \t]+:[a-zA-Z0-9_@:]+:[ \t]*$" txt))
  13910. (setq txt (replace-match "" t t txt)))
  13911. (if (string-match quote-re0 txt)
  13912. (setq txt (replace-match "" t t txt)))
  13913. (if org-export-with-section-numbers
  13914. (setq txt (concat (org-section-number level)
  13915. " " txt)))
  13916. (if (<= level umax)
  13917. (progn
  13918. (setq head-count (+ head-count 1))
  13919. (if (> level org-last-level)
  13920. (progn
  13921. (setq cnt (- level org-last-level))
  13922. (while (>= (setq cnt (1- cnt)) 0)
  13923. (insert "\n<ul>\n<li>"))
  13924. (insert "\n")))
  13925. (if (< level org-last-level)
  13926. (progn
  13927. (setq cnt (- org-last-level level))
  13928. (while (>= (setq cnt (1- cnt)) 0)
  13929. (insert "</li>\n</ul>"))
  13930. (insert "\n")))
  13931. ;; Check for targets
  13932. (while (string-match org-target-regexp line)
  13933. (setq tg (match-string 1 line)
  13934. line (replace-match
  13935. (concat "@<span class=\"target\">" tg "@</span> ")
  13936. t t line))
  13937. (push (cons (org-solidify-link-text tg)
  13938. (format "sec-%d" head-count))
  13939. target-alist))
  13940. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  13941. (setq txt (replace-match "" t t txt)))
  13942. (insert
  13943. (format
  13944. (if todo
  13945. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  13946. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  13947. head-count txt))
  13948. (setq org-last-level level))
  13949. )))
  13950. line)
  13951. lines))
  13952. (while (> org-last-level 0)
  13953. (setq org-last-level (1- org-last-level))
  13954. (insert "</li>\n</ul>\n"))
  13955. ))
  13956. (setq head-count 0)
  13957. (org-init-section-numbers)
  13958. (while (setq line (pop lines) origline line)
  13959. (catch 'nextline
  13960. ;; end of quote section?
  13961. (when (and inquote (string-match "^\\*+" line))
  13962. (insert "</pre>\n")
  13963. (setq inquote nil))
  13964. ;; inside a quote section?
  13965. (when inquote
  13966. (insert (org-html-protect line) "\n")
  13967. (throw 'nextline nil))
  13968. ;; verbatim lines
  13969. (when (and org-export-with-fixed-width
  13970. (string-match "^[ \t]*:\\(.*\\)" line))
  13971. (when (not infixed)
  13972. (setq infixed t)
  13973. (insert "<pre>\n"))
  13974. (insert (org-html-protect (match-string 1 line)) "\n")
  13975. (when (and lines
  13976. (not (string-match "^[ \t]*\\(:.*\\)"
  13977. (car lines))))
  13978. (setq infixed nil)
  13979. (insert "</pre>\n"))
  13980. (throw 'nextline nil))
  13981. ;; make targets to anchors
  13982. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  13983. (cond
  13984. ((match-end 2)
  13985. (setq line (replace-match
  13986. (concat "@<a name=\""
  13987. (org-solidify-link-text (match-string 1 line))
  13988. "\">\\nbsp@</a>")
  13989. t t line)))
  13990. ((and org-export-with-toc (equal (string-to-char line) ?*))
  13991. (setq line (replace-match
  13992. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  13993. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  13994. t t line)))
  13995. (t
  13996. (setq line (replace-match
  13997. (concat "@<a name=\""
  13998. (org-solidify-link-text (match-string 1 line))
  13999. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  14000. t t line)))))
  14001. (setq line (org-html-handle-time-stamps line))
  14002. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  14003. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  14004. ;; Also handle sub_superscripts and checkboxes
  14005. (setq line (org-html-expand line))
  14006. ;; Format the links
  14007. (setq start 0)
  14008. (while (string-match org-bracket-link-analytic-regexp line start)
  14009. (setq start (match-beginning 0))
  14010. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  14011. (setq path (match-string 3 line))
  14012. (setq desc1 (if (match-end 5) (match-string 5 line))
  14013. desc2 (if (match-end 2) (concat type ":" path) path)
  14014. descp (and desc1 (not (equal desc1 desc2)))
  14015. desc (or desc1 desc2))
  14016. ;; FIXME: do we need to unescape here somewhere?
  14017. (cond
  14018. ((equal type "internal")
  14019. (setq rpl
  14020. (concat
  14021. "<a href=\"#"
  14022. (org-solidify-link-text path target-alist)
  14023. "\">" desc "</a>")))
  14024. ((member type '("http" "https" "ftp" "mailto" "news"))
  14025. ;; standard URL
  14026. (setq link (concat type ":" path))
  14027. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  14028. ((string= type "file")
  14029. ;; FILE link
  14030. (let* ((filename path)
  14031. (abs-p (file-name-absolute-p filename))
  14032. thefile file-is-image-p search)
  14033. (save-match-data
  14034. (if (string-match "::\\(.*\\)" filename)
  14035. (setq search (match-string 1 filename)
  14036. filename (replace-match "" t nil filename)))
  14037. (setq valid
  14038. (if (functionp link-validate)
  14039. (funcall link-validate filename current-dir)
  14040. t))
  14041. (setq file-is-image-p
  14042. (string-match (org-image-file-name-regexp) filename))
  14043. (setq thefile (if abs-p (expand-file-name filename) filename))
  14044. (when (and org-export-html-link-org-files-as-html
  14045. (string-match "\\.org$" thefile))
  14046. (setq thefile (concat (substring thefile 0
  14047. (match-beginning 0))
  14048. ".html"))
  14049. (if (and search
  14050. ;; make sure this is can be used as target search
  14051. (not (string-match "^[0-9]*$" search))
  14052. (not (string-match "^\\*" search))
  14053. (not (string-match "^/.*/$" search)))
  14054. (setq thefile (concat thefile "#"
  14055. (org-solidify-link-text
  14056. (org-link-unescape search)))))
  14057. (when (string-match "^file:" desc)
  14058. (setq desc (replace-match "" t t desc))
  14059. (if (string-match "\\.org$" desc)
  14060. (setq desc (replace-match "" t t desc))))))
  14061. (setq rpl (if (and file-is-image-p
  14062. (or (eq t org-export-html-inline-images)
  14063. (and org-export-html-inline-images
  14064. (not descp))))
  14065. (concat "<img src=\"" thefile "\"/>")
  14066. (concat "<a href=\"" thefile "\">" desc "</a>")))
  14067. (if (not valid) (setq rpl desc))))
  14068. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  14069. (setq rpl (concat "<i>&lt;" type ":"
  14070. (save-match-data (org-link-unescape path))
  14071. "&gt;</i>"))))
  14072. (setq line (replace-match rpl t t line)
  14073. start (+ start (length rpl))))
  14074. ;; TODO items
  14075. (if (and (string-match org-todo-line-regexp line)
  14076. (match-beginning 2))
  14077. (if (equal (match-string 2 line) org-done-string)
  14078. (setq line (replace-match
  14079. "<span class=\"done\">\\2</span>"
  14080. t nil line 2))
  14081. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  14082. t nil line 2))))
  14083. (cond
  14084. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  14085. ;; This is a headline
  14086. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  14087. txt (match-string 2 line))
  14088. (if (string-match quote-re0 txt)
  14089. (setq txt (replace-match "" t t txt)))
  14090. (if (<= level umax) (setq head-count (+ head-count 1)))
  14091. (when in-local-list
  14092. ;; Close any local lists before inserting a new header line
  14093. (while local-list-num
  14094. (org-close-li)
  14095. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  14096. (pop local-list-num))
  14097. (setq local-list-indent nil
  14098. in-local-list nil))
  14099. (org-html-level-start level txt umax
  14100. (and org-export-with-toc (<= level umax))
  14101. head-count)
  14102. ;; QUOTES
  14103. (when (string-match quote-re line)
  14104. (insert "<pre>")
  14105. (setq inquote t)))
  14106. ((and org-export-with-tables
  14107. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  14108. (if (not table-open)
  14109. ;; New table starts
  14110. (setq table-open t table-buffer nil table-orig-buffer nil))
  14111. ;; Accumulate lines
  14112. (setq table-buffer (cons line table-buffer)
  14113. table-orig-buffer (cons origline table-orig-buffer))
  14114. (when (or (not lines)
  14115. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  14116. (car lines))))
  14117. (setq table-open nil
  14118. table-buffer (nreverse table-buffer)
  14119. table-orig-buffer (nreverse table-orig-buffer))
  14120. (org-close-par-maybe)
  14121. (insert (org-format-table-html table-buffer table-orig-buffer))))
  14122. (t
  14123. ;; Normal lines
  14124. (when (string-match
  14125. (cond
  14126. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14127. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14128. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  14129. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  14130. line)
  14131. (setq ind (org-get-string-indentation line)
  14132. start-is-num (match-beginning 4)
  14133. starter (if (match-beginning 2)
  14134. (substring (match-string 2 line) 0 -1))
  14135. line (substring line (match-beginning 5)))
  14136. (unless (string-match "[^ \t]" line)
  14137. ;; empty line. Pretend indentation is large.
  14138. (setq ind (1+ (or (car local-list-indent) 1))))
  14139. (while (and in-local-list
  14140. (or (and (= ind (car local-list-indent))
  14141. (not starter))
  14142. (< ind (car local-list-indent))))
  14143. (org-close-li)
  14144. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  14145. (pop local-list-num) (pop local-list-indent)
  14146. (setq in-local-list local-list-indent))
  14147. (cond
  14148. ((and starter
  14149. (or (not in-local-list)
  14150. (> ind (car local-list-indent))))
  14151. ;; Start new (level of ) list
  14152. (org-close-par-maybe)
  14153. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  14154. (push start-is-num local-list-num)
  14155. (push ind local-list-indent)
  14156. (setq in-local-list t))
  14157. (starter
  14158. ;; continue current list
  14159. (org-close-li)
  14160. (insert "<li>\n")))
  14161. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  14162. (setq line
  14163. (replace-match
  14164. (if (equal (match-string 1 line) "X")
  14165. "<b>[X]</b>"
  14166. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  14167. t t line))))
  14168. ;; Empty lines start a new paragraph. If hand-formatted lists
  14169. ;; are not fully interpreted, lines starting with "-", "+", "*"
  14170. ;; also start a new paragraph.
  14171. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  14172. ;; Check if the line break needs to be conserved
  14173. (cond
  14174. ((string-match "\\\\\\\\[ \t]*$" line)
  14175. (setq line (replace-match "<br/>" t t line)))
  14176. (org-export-preserve-breaks
  14177. (setq line (concat line "<br/>"))))
  14178. (insert line "\n")))))
  14179. ;; Properly close all local lists and other lists
  14180. (when inquote (insert "</pre>\n"))
  14181. (when in-local-list
  14182. ;; Close any local lists before inserting a new header line
  14183. (while local-list-num
  14184. (org-close-li)
  14185. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  14186. (pop local-list-num))
  14187. (setq local-list-indent nil
  14188. in-local-list nil))
  14189. (org-html-level-start 1 nil umax
  14190. (and org-export-with-toc (<= level umax))
  14191. head-count)
  14192. (when (plist-get opt-plist :auto-postamble)
  14193. (when author
  14194. (insert "<p class=\"author\"> "
  14195. (nth 1 lang-words) ": " author "\n")
  14196. (when email
  14197. (insert "<a href=\"mailto:" email "\">&lt;"
  14198. email "&gt;</a>\n"))
  14199. (insert "</p>\n"))
  14200. (when (and date time)
  14201. (insert "<p class=\"date\"> "
  14202. (nth 2 lang-words) ": "
  14203. date " " time "</p>\n")))
  14204. (if org-export-html-with-timestamp
  14205. (insert org-export-html-html-helper-timestamp))
  14206. (insert (or (plist-get opt-plist :postamble) ""))
  14207. (insert "</body>\n</html>\n")
  14208. (normal-mode)
  14209. ;; remove empty paragraphs and lists
  14210. (goto-char (point-min))
  14211. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  14212. (replace-match ""))
  14213. (goto-char (point-min))
  14214. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  14215. (replace-match ""))
  14216. (save-buffer)
  14217. (goto-char (point-min))
  14218. (message "Exporting... done"))))
  14219. (defun org-format-table-html (lines olines)
  14220. "Find out which HTML converter to use and return the HTML code."
  14221. (if (string-match "^[ \t]*|" (car lines))
  14222. ;; A normal org table
  14223. (org-format-org-table-html lines)
  14224. ;; Table made by table.el - test for spanning
  14225. (let* ((hlines (delq nil (mapcar
  14226. (lambda (x)
  14227. (if (string-match "^[ \t]*\\+-" x) x
  14228. nil))
  14229. lines)))
  14230. (first (car hlines))
  14231. (ll (and (string-match "\\S-+" first)
  14232. (match-string 0 first)))
  14233. (re (concat "^[ \t]*" (regexp-quote ll)))
  14234. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  14235. hlines))))
  14236. (if (and (not spanning)
  14237. (not org-export-prefer-native-exporter-for-tables))
  14238. ;; We can use my own converter with HTML conversions
  14239. (org-format-table-table-html lines)
  14240. ;; Need to use the code generator in table.el, with the original text.
  14241. (org-format-table-table-html-using-table-generate-source olines)))))
  14242. (defun org-format-org-table-html (lines)
  14243. "Format a table into HTML."
  14244. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  14245. (setq lines (nreverse lines))
  14246. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  14247. (setq lines (nreverse lines))
  14248. (when org-export-table-remove-special-lines
  14249. ;; Check if the table has a marking column. If yes remove the
  14250. ;; column and the special lines
  14251. (let* ((special
  14252. (not
  14253. (memq nil
  14254. (mapcar
  14255. (lambda (x)
  14256. (or (string-match "^[ \t]*|-" x)
  14257. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  14258. lines)))))
  14259. (if special
  14260. (setq lines
  14261. (delq nil
  14262. (mapcar
  14263. (lambda (x)
  14264. (if (string-match "^[ \t]*| *[!_^] *|" x)
  14265. nil ; ignore this line
  14266. (and (or (string-match "^[ \t]*|-+\\+" x)
  14267. (string-match "^[ \t]*|[^|]*|" x))
  14268. (replace-match "|" t t x))))
  14269. lines))))))
  14270. (let ((head (and org-export-highlight-first-table-line
  14271. (delq nil (mapcar
  14272. (lambda (x) (string-match "^[ \t]*|-" x))
  14273. (cdr lines)))))
  14274. line fields html)
  14275. (setq html (concat org-export-html-table-tag "\n"))
  14276. (while (setq line (pop lines))
  14277. (catch 'next-line
  14278. (if (string-match "^[ \t]*|-" line)
  14279. (progn
  14280. (setq head nil) ;; head ends here, first time around
  14281. ;; ignore this line
  14282. (throw 'next-line t)))
  14283. ;; Break the line into fields
  14284. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  14285. (setq html (concat
  14286. html
  14287. "<tr>"
  14288. (mapconcat (lambda (x)
  14289. (if head
  14290. (concat "<th>" x "</th>")
  14291. (concat "<td>" x "</td>")))
  14292. fields "")
  14293. "</tr>\n"))))
  14294. (setq html (concat html "</table>\n"))
  14295. html))
  14296. (defun org-fake-empty-table-line (line)
  14297. "Replace everything except \"|\" with spaces."
  14298. (let ((i (length line))
  14299. (newstr (copy-sequence line)))
  14300. (while (> i 0)
  14301. (setq i (1- i))
  14302. (if (not (eq (aref newstr i) ?|))
  14303. (aset newstr i ?\ )))
  14304. newstr))
  14305. (defun org-format-table-table-html (lines)
  14306. "Format a table generated by table.el into HTML.
  14307. This conversion does *not* use `table-generate-source' from table.el.
  14308. This has the advantage that Org-mode's HTML conversions can be used.
  14309. But it has the disadvantage, that no cell- or row-spanning is allowed."
  14310. (let (line field-buffer
  14311. (head org-export-highlight-first-table-line)
  14312. fields html empty)
  14313. (setq html (concat org-export-html-table-tag "\n"))
  14314. (while (setq line (pop lines))
  14315. (setq empty "&nbsp;")
  14316. (catch 'next-line
  14317. (if (string-match "^[ \t]*\\+-" line)
  14318. (progn
  14319. (if field-buffer
  14320. (progn
  14321. (setq html (concat
  14322. html
  14323. "<tr>"
  14324. (mapconcat
  14325. (lambda (x)
  14326. (if (equal x "") (setq x empty))
  14327. (if head
  14328. (concat "<th>" x "</th>\n")
  14329. (concat "<td>" x "</td>\n")))
  14330. field-buffer "\n")
  14331. "</tr>\n"))
  14332. (setq head nil)
  14333. (setq field-buffer nil)))
  14334. ;; Ignore this line
  14335. (throw 'next-line t)))
  14336. ;; Break the line into fields and store the fields
  14337. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  14338. (if field-buffer
  14339. (setq field-buffer (mapcar
  14340. (lambda (x)
  14341. (concat x "<br/>" (pop fields)))
  14342. field-buffer))
  14343. (setq field-buffer fields))))
  14344. (setq html (concat html "</table>\n"))
  14345. html))
  14346. (defun org-format-table-table-html-using-table-generate-source (lines)
  14347. "Format a table into html, using `table-generate-source' from table.el.
  14348. This has the advantage that cell- or row-spanning is allowed.
  14349. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  14350. (require 'table)
  14351. (with-current-buffer (get-buffer-create " org-tmp1 ")
  14352. (erase-buffer)
  14353. (insert (mapconcat 'identity lines "\n"))
  14354. (goto-char (point-min))
  14355. (if (not (re-search-forward "|[^+]" nil t))
  14356. (error "Error processing table"))
  14357. (table-recognize-table)
  14358. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  14359. (table-generate-source 'html " org-tmp2 ")
  14360. (set-buffer " org-tmp2 ")
  14361. (buffer-substring (point-min) (point-max))))
  14362. (defun org-html-handle-time-stamps (s)
  14363. "Format time stamps in string S, or remove them."
  14364. (catch 'exit
  14365. (let (r b)
  14366. (while (string-match org-maybe-keyword-time-regexp s)
  14367. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  14368. ;; never export CLOCK
  14369. (throw 'exit ""))
  14370. (or b (setq b (substring s 0 (match-beginning 0))))
  14371. (if (not org-export-with-timestamps)
  14372. (setq r (concat r (substring s 0 (match-beginning 0)))
  14373. s (substring s (match-end 0)))
  14374. (setq r (concat
  14375. r (substring s 0 (match-beginning 0))
  14376. (if (match-end 1)
  14377. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  14378. (match-string 1 s)))
  14379. (format " @<span class=\"timestamp\">%s@</span>"
  14380. (substring (match-string 3 s) 1 -1)))
  14381. s (substring s (match-end 0)))))
  14382. ;; Line break if line started and ended with time stamp stuff
  14383. (if (not r)
  14384. s
  14385. (setq r (concat r s))
  14386. (unless (string-match "\\S-" (concat b s))
  14387. (setq r (concat r "@<br/>")))
  14388. r))))
  14389. (defun org-html-protect (s)
  14390. ;; convert & to &amp;, < to &lt; and > to &gt;
  14391. (let ((start 0))
  14392. (while (string-match "&" s start)
  14393. (setq s (replace-match "&amp;" t t s)
  14394. start (1+ (match-beginning 0))))
  14395. (while (string-match "<" s)
  14396. (setq s (replace-match "&lt;" t t s)))
  14397. (while (string-match ">" s)
  14398. (setq s (replace-match "&gt;" t t s))))
  14399. s)
  14400. (defun org-export-cleanup-toc-line (s)
  14401. "Remove tags and time staps from lines going into the toc."
  14402. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  14403. (setq s (replace-match "" t t s)))
  14404. (when org-export-remove-timestamps-from-toc
  14405. (while (string-match org-maybe-keyword-time-regexp s)
  14406. (setq s (replace-match "" t t s))))
  14407. s)
  14408. (defun org-html-expand (string)
  14409. "Prepare STRING for HTML export. Applies all active conversions.
  14410. If there are links in the string, don't modify these."
  14411. (let* (m s l res)
  14412. (while (setq m (string-match org-bracket-link-regexp string))
  14413. (setq s (substring string 0 m)
  14414. l (match-string 0 string)
  14415. string (substring string (match-end 0)))
  14416. (push (org-html-do-expand s) res)
  14417. (push l res))
  14418. (push (org-html-do-expand string) res)
  14419. (apply 'concat (nreverse res))))
  14420. (defun org-html-do-expand (s)
  14421. "Apply all active conversions to translate special ASCII to HTML."
  14422. (setq s (org-html-protect s))
  14423. (if org-export-html-expand
  14424. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  14425. (setq s (replace-match "<\\1>" t nil s))))
  14426. (if org-export-with-emphasize
  14427. (setq s (org-export-html-convert-emphasize s)))
  14428. (if org-export-with-sub-superscripts
  14429. (setq s (org-export-html-convert-sub-super s)))
  14430. (if org-export-with-TeX-macros
  14431. (let ((start 0) wd ass)
  14432. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  14433. (setq wd (match-string 1 s))
  14434. (if (setq ass (assoc wd org-html-entities))
  14435. (setq s (replace-match (or (cdr ass)
  14436. (concat "&" (car ass) ";"))
  14437. t t s))
  14438. (setq start (+ start (length wd)))))))
  14439. s)
  14440. (defun org-create-multibrace-regexp (left right n)
  14441. "Create a regular expression which will match a balanced sexp.
  14442. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  14443. as single character strings.
  14444. The regexp returned will match the entire expression including the
  14445. delimiters. It will also define a single group which contains the
  14446. match except for the outermost delimiters. The maximum depth of
  14447. stacked delimiters is N. Escaping delimiters is not possible."
  14448. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  14449. (or "\\|")
  14450. (re nothing)
  14451. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  14452. (while (> n 1)
  14453. (setq n (1- n)
  14454. re (concat re or next)
  14455. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  14456. (concat left "\\(" re "\\)" right)))
  14457. (defvar org-match-substring-regexp
  14458. (concat
  14459. "\\([^\\]\\)\\([_^]\\)\\("
  14460. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  14461. "\\|"
  14462. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  14463. "\\|"
  14464. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  14465. "The regular expression matching a sub- or superscript.")
  14466. (defun org-export-html-convert-sub-super (string)
  14467. "Convert sub- and superscripts in STRING to HTML."
  14468. (let (key c)
  14469. (while (string-match org-match-substring-regexp string)
  14470. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  14471. (setq c (or (match-string 8 string)
  14472. (match-string 6 string)
  14473. (match-string 5 string)))
  14474. (setq string (replace-match
  14475. (concat (match-string 1 string)
  14476. "<" key ">" c "</" key ">")
  14477. t t string)))
  14478. (while (string-match "\\\\\\([_^]\\)" string)
  14479. (setq string (replace-match (match-string 1 string) t t string))))
  14480. string)
  14481. (defun org-export-html-convert-emphasize (string)
  14482. "Apply emphasis."
  14483. (while (string-match org-emph-re string)
  14484. (setq string (replace-match (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist)) "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist)) "\\5") t nil string)))
  14485. string)
  14486. (defvar org-par-open nil)
  14487. (defun org-open-par ()
  14488. "Insert <p>, but first close previous paragraph if any."
  14489. (org-close-par-maybe)
  14490. (insert "\n<p>")
  14491. (setq org-par-open t))
  14492. (defun org-close-par-maybe ()
  14493. "Close paragraph if there is one open."
  14494. (when org-par-open
  14495. (insert "</p>")
  14496. (setq org-par-open nil)))
  14497. (defun org-close-li ()
  14498. "Close <li> if necessary."
  14499. (org-close-par-maybe)
  14500. (insert "</li>\n"))
  14501. ; (when (save-excursion
  14502. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  14503. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  14504. ; (insert "</li>"))))
  14505. (defun org-html-level-start (level title umax with-toc head-count)
  14506. "Insert a new level in HTML export.
  14507. When TITLE is nil, just close all open levels."
  14508. (org-close-par-maybe)
  14509. (let ((l (1+ (max level umax))))
  14510. (while (<= l org-level-max)
  14511. (if (aref levels-open (1- l))
  14512. (progn
  14513. (org-html-level-close l)
  14514. (aset levels-open (1- l) nil)))
  14515. (setq l (1+ l)))
  14516. (when title
  14517. ;; If title is nil, this means this function is called to close
  14518. ;; all levels, so the rest is done only if title is given
  14519. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  14520. (setq title (replace-match
  14521. (if org-export-with-tags
  14522. (save-match-data
  14523. (concat
  14524. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  14525. (mapconcat 'identity (org-split-string
  14526. (match-string 1 title) ":")
  14527. "&nbsp;")
  14528. "</span>"))
  14529. "")
  14530. t t title)))
  14531. (if (> level umax)
  14532. (progn
  14533. (if (aref levels-open (1- level))
  14534. (progn
  14535. (org-close-li)
  14536. (insert "<li>" title "<br/>\n"))
  14537. (aset levels-open (1- level) t)
  14538. (org-close-par-maybe)
  14539. (insert "<ul>\n<li>" title "<br/>\n")))
  14540. (if org-export-with-section-numbers
  14541. (setq title (concat (org-section-number level) " " title)))
  14542. (setq level (+ level org-export-html-toplevel-hlevel -1))
  14543. (if with-toc
  14544. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  14545. level head-count title level))
  14546. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  14547. (org-open-par)))))
  14548. (defun org-html-level-close (&rest args)
  14549. "Terminate one level in HTML export."
  14550. (org-close-li)
  14551. (insert "</ul>"))
  14552. ;; Variable holding the vector with section numbers
  14553. (defvar org-section-numbers (make-vector org-level-max 0))
  14554. (defun org-init-section-numbers ()
  14555. "Initialize the vector for the section numbers."
  14556. (let* ((level -1)
  14557. (numbers (nreverse (org-split-string "" "\\.")))
  14558. (depth (1- (length org-section-numbers)))
  14559. (i depth) number-string)
  14560. (while (>= i 0)
  14561. (if (> i level)
  14562. (aset org-section-numbers i 0)
  14563. (setq number-string (or (car numbers) "0"))
  14564. (if (string-match "\\`[A-Z]\\'" number-string)
  14565. (aset org-section-numbers i
  14566. (- (string-to-char number-string) ?A -1))
  14567. (aset org-section-numbers i (string-to-number number-string)))
  14568. (pop numbers))
  14569. (setq i (1- i)))))
  14570. (defun org-section-number (&optional level)
  14571. "Return a string with the current section number.
  14572. When LEVEL is non-nil, increase section numbers on that level."
  14573. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  14574. (when level
  14575. (when (> level -1)
  14576. (aset org-section-numbers
  14577. level (1+ (aref org-section-numbers level))))
  14578. (setq idx (1+ level))
  14579. (while (<= idx depth)
  14580. (if (not (= idx 1))
  14581. (aset org-section-numbers idx 0))
  14582. (setq idx (1+ idx))))
  14583. (setq idx 0)
  14584. (while (<= idx depth)
  14585. (setq n (aref org-section-numbers idx))
  14586. (setq string (concat string (if (not (string= string "")) "." "")
  14587. (int-to-string n)))
  14588. (setq idx (1+ idx)))
  14589. (save-match-data
  14590. (if (string-match "\\`\\([@0]\\.\\)+" string)
  14591. (setq string (replace-match "" t nil string)))
  14592. (if (string-match "\\(\\.0\\)+\\'" string)
  14593. (setq string (replace-match "" t nil string))))
  14594. string))
  14595. ;;;###autoload
  14596. (defun org-export-icalendar-this-file ()
  14597. "Export current file as an iCalendar file.
  14598. The iCalendar file will be located in the same directory as the Org-mode
  14599. file, but with extension `.ics'."
  14600. (interactive)
  14601. (org-export-icalendar nil buffer-file-name))
  14602. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  14603. (with-current-buffer buffer
  14604. (apply 'insert output)))
  14605. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  14606. (defun org-export-as-xoxo (&optional buffer)
  14607. "Export the org buffer as XOXO.
  14608. The XOXO buffer is named *xoxo-<source buffer name>*"
  14609. (interactive (list (current-buffer)))
  14610. ;; A quickie abstraction
  14611. ;; Output everything as XOXO
  14612. (with-current-buffer (get-buffer buffer)
  14613. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  14614. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  14615. (org-infile-export-plist)))
  14616. (filename (concat (file-name-as-directory
  14617. (org-export-directory :xoxo opt-plist))
  14618. (file-name-sans-extension
  14619. (file-name-nondirectory buffer-file-name))
  14620. ".html"))
  14621. (out (find-file-noselect filename))
  14622. (last-level 1)
  14623. (hanging-li nil))
  14624. ;; Check the output buffer is empty.
  14625. (with-current-buffer out (erase-buffer))
  14626. ;; Kick off the output
  14627. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  14628. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  14629. (let* ((hd (match-string-no-properties 1))
  14630. (level (length hd))
  14631. (text (concat
  14632. (match-string-no-properties 2)
  14633. (save-excursion
  14634. (goto-char (match-end 0))
  14635. (let ((str ""))
  14636. (catch 'loop
  14637. (while 't
  14638. (forward-line)
  14639. (if (looking-at "^[ \t]\\(.*\\)")
  14640. (setq str (concat str (match-string-no-properties 1)))
  14641. (throw 'loop str)))))))))
  14642. ;; Handle level rendering
  14643. (cond
  14644. ((> level last-level)
  14645. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  14646. ((< level last-level)
  14647. (dotimes (- (- last-level level) 1)
  14648. (if hanging-li
  14649. (org-export-as-xoxo-insert-into out "</li>\n"))
  14650. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14651. (when hanging-li
  14652. (org-export-as-xoxo-insert-into out "</li>\n")
  14653. (setq hanging-li nil)))
  14654. ((equal level last-level)
  14655. (if hanging-li
  14656. (org-export-as-xoxo-insert-into out "</li>\n")))
  14657. )
  14658. (setq last-level level)
  14659. ;; And output the new li
  14660. (setq hanging-li 't)
  14661. (if (equal ?+ (elt text 0))
  14662. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  14663. (org-export-as-xoxo-insert-into out "<li>" text))))
  14664. ;; Finally finish off the ol
  14665. (dotimes (- last-level 1)
  14666. (if hanging-li
  14667. (org-export-as-xoxo-insert-into out "</li>\n"))
  14668. (org-export-as-xoxo-insert-into out "</ol>\n"))
  14669. ;; Finish the buffer off and clean it up.
  14670. (switch-to-buffer-other-window out)
  14671. (indent-region (point-min) (point-max) nil)
  14672. (save-buffer)
  14673. (goto-char (point-min))
  14674. )))
  14675. ;;;###autoload
  14676. (defun org-export-icalendar-all-agenda-files ()
  14677. "Export all files in `org-agenda-files' to iCalendar .ics files.
  14678. Each iCalendar file will be located in the same directory as the Org-mode
  14679. file, but with extension `.ics'."
  14680. (interactive)
  14681. (apply 'org-export-icalendar nil (org-agenda-files t)))
  14682. ;;;###autoload
  14683. (defun org-export-icalendar-combine-agenda-files ()
  14684. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  14685. The file is stored under the name `org-combined-agenda-icalendar-file'."
  14686. (interactive)
  14687. (apply 'org-export-icalendar t (org-agenda-files t)))
  14688. (defun org-export-icalendar (combine &rest files)
  14689. "Create iCalendar files for all elements of FILES.
  14690. If COMBINE is non-nil, combine all calendar entries into a single large
  14691. file and store it under the name `org-combined-agenda-icalendar-file'."
  14692. (save-excursion
  14693. (let* ((dir (org-export-directory
  14694. :ical (list :publishing-directory
  14695. org-export-publishing-directory)))
  14696. file ical-file ical-buffer category started org-agenda-new-buffers)
  14697. (when combine
  14698. (setq ical-file
  14699. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  14700. org-combined-agenda-icalendar-file
  14701. (expand-file-name org-combined-agenda-icalendar-file dir))
  14702. ical-buffer (org-get-agenda-file-buffer ical-file))
  14703. (set-buffer ical-buffer) (erase-buffer))
  14704. (while (setq file (pop files))
  14705. (catch 'nextfile
  14706. (org-check-agenda-file file)
  14707. (set-buffer (org-get-agenda-file-buffer file))
  14708. (unless combine
  14709. (setq ical-file (concat (file-name-as-directory dir)
  14710. (file-name-sans-extension
  14711. (file-name-nondirectory buffer-file-name))
  14712. ".ics"))
  14713. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  14714. (with-current-buffer ical-buffer (erase-buffer)))
  14715. (setq category (or org-category
  14716. (file-name-sans-extension
  14717. (file-name-nondirectory buffer-file-name))))
  14718. (if (symbolp category) (setq category (symbol-name category)))
  14719. (let ((standard-output ical-buffer))
  14720. (if combine
  14721. (and (not started) (setq started t)
  14722. (org-start-icalendar-file org-icalendar-combined-name))
  14723. (org-start-icalendar-file category))
  14724. (org-print-icalendar-entries combine category)
  14725. (when (or (and combine (not files)) (not combine))
  14726. (org-finish-icalendar-file)
  14727. (set-buffer ical-buffer)
  14728. (save-buffer)
  14729. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  14730. (org-release-buffers org-agenda-new-buffers))))
  14731. (defvar org-after-save-iCalendar-file-hook nil
  14732. "Hook run after an iCalendar file has been saved.
  14733. The iCalendar buffer is still current when this hook is run.
  14734. A good way to use this is to tell a desktop calenndar application to re-read
  14735. the iCalendar file.")
  14736. (defun org-print-icalendar-entries (&optional combine category)
  14737. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  14738. When COMBINE is non nil, add the category to each line."
  14739. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  14740. (dts (org-ical-ts-to-string
  14741. (format-time-string (cdr org-time-stamp-formats) (current-time))
  14742. "DTSTART"))
  14743. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  14744. (save-excursion
  14745. (goto-char (point-min))
  14746. (while (re-search-forward org-ts-regexp nil t)
  14747. (setq pos (match-beginning 0)
  14748. ts (match-string 0)
  14749. inc t
  14750. hd (org-get-heading))
  14751. (if (looking-at re2)
  14752. (progn
  14753. (goto-char (match-end 0))
  14754. (setq ts2 (match-string 1) inc nil))
  14755. (setq ts2 ts
  14756. tmp (buffer-substring (max (point-min)
  14757. (- pos org-ds-keyword-length))
  14758. pos)
  14759. deadlinep (string-match org-deadline-regexp tmp)
  14760. scheduledp (string-match org-scheduled-regexp tmp)
  14761. ;; donep (org-entry-is-done-p)
  14762. ))
  14763. (if (or (string-match org-tr-regexp hd)
  14764. (string-match org-ts-regexp hd))
  14765. (setq hd (replace-match "" t t hd)))
  14766. (if combine
  14767. (setq hd (concat hd " (category " category ")")))
  14768. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  14769. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  14770. (princ (format "BEGIN:VEVENT
  14771. %s
  14772. %s
  14773. SUMMARY:%s
  14774. END:VEVENT\n"
  14775. (org-ical-ts-to-string ts "DTSTART")
  14776. (org-ical-ts-to-string ts2 "DTEND" inc)
  14777. hd)))
  14778. (when org-icalendar-include-todo
  14779. (goto-char (point-min))
  14780. (while (re-search-forward org-todo-line-regexp nil t)
  14781. (setq state (match-string 1))
  14782. (unless (equal state org-done-string)
  14783. (setq hd (match-string 3))
  14784. (if (string-match org-priority-regexp hd)
  14785. (setq pri (string-to-char (match-string 2 hd))
  14786. hd (concat (substring hd 0 (match-beginning 1))
  14787. (substring hd (- (match-end 1)))))
  14788. (setq pri org-default-priority))
  14789. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  14790. (- org-lowest-priority ?A))))))
  14791. (princ (format "BEGIN:VTODO
  14792. %s
  14793. SUMMARY:%s
  14794. SEQUENCE:1
  14795. PRIORITY:%d
  14796. END:VTODO\n"
  14797. dts hd pri))))))))
  14798. (defun org-start-icalendar-file (name)
  14799. "Start an iCalendar file by inserting the header."
  14800. (let ((user user-full-name)
  14801. (name (or name "unknown"))
  14802. (timezone (cadr (current-time-zone))))
  14803. (princ
  14804. (format "BEGIN:VCALENDAR
  14805. VERSION:2.0
  14806. X-WR-CALNAME:%s
  14807. PRODID:-//%s//Emacs with Org-mode//EN
  14808. X-WR-TIMEZONE:%s
  14809. CALSCALE:GREGORIAN\n" name user timezone))))
  14810. (defun org-finish-icalendar-file ()
  14811. "Finish an iCalendar file by inserting the END statement."
  14812. (princ "END:VCALENDAR\n"))
  14813. (defun org-ical-ts-to-string (s keyword &optional inc)
  14814. "Take a time string S and convert it to iCalendar format.
  14815. KEYWORD is added in front, to make a complete line like DTSTART....
  14816. When INC is non-nil, increase the hour by two (if time string contains
  14817. a time), or the day by one (if it does not contain a time)."
  14818. (let ((t1 (org-parse-time-string s 'nodefault))
  14819. t2 fmt have-time time)
  14820. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  14821. (setq t2 t1 have-time t)
  14822. (setq t2 (org-parse-time-string s)))
  14823. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  14824. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  14825. (when inc
  14826. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  14827. (setq time (encode-time s mi h d m y)))
  14828. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  14829. (concat keyword (format-time-string fmt time))))
  14830. ;;; LaTeX stuff
  14831. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14832. "Keymap for the minor `org-cdlatex-mode'.")
  14833. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14834. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14835. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14836. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14837. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14838. (defvar org-cdlatex-texmathp-advice-is-done nil
  14839. "Flag remembering if we have applied the advice to texmathp already.")
  14840. (define-minor-mode org-cdlatex-mode
  14841. "Toggle the minor `org-cdlatex-mode'.
  14842. This mode supports entering LaTeX environment and math in LaTeX fragments
  14843. in Org-mode.
  14844. \\{org-cdlatex-mode-map}"
  14845. nil " OCDL" nil
  14846. (when org-cdlatex-mode (require 'cdlatex))
  14847. (unless org-cdlatex-texmathp-advice-is-done
  14848. (setq org-cdlatex-texmathp-advice-is-done t)
  14849. (defadvice texmathp (around org-math-always-on activate)
  14850. "Always return t in org-mode buffers.
  14851. This is because we want to insert math symbols without dollars even outside
  14852. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14853. en embedded LaTeX fragement, let texmathp do its job.
  14854. \\[org-cdlatex-mode-map]"
  14855. (interactive)
  14856. (let (p)
  14857. (cond
  14858. ((not (org-mode-p)) ad-do-it)
  14859. ((eq this-command 'cdlatex-math-symbol)
  14860. (setq ad-return-value t
  14861. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14862. (t
  14863. (let ((p (org-inside-LaTeX-fragment-p)))
  14864. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14865. (setq ad-return-value t
  14866. texmathp-why '("Org-mode embedded math" . 0))
  14867. (if p ad-do-it)))))))))
  14868. (defun turn-on-org-cdlatex ()
  14869. "Unconditionally turn on `org-cdlatex-mode'."
  14870. (org-cdlatex-mode 1))
  14871. (defun org-inside-LaTeX-fragment-p ()
  14872. "Test if point is inside a LaTeX fragment.
  14873. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14874. sequence appearing also before point.
  14875. Even though the matchers for math are configurable, this function assumes
  14876. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14877. delimiters are skipped when they have been removed by customization.
  14878. The return value is nil, or a cons cell with the delimiter and
  14879. and the position of this delimiter.
  14880. This function does a reasonably good job, but can locally be fooled by
  14881. for example currency specifications. For example it will assume being in
  14882. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14883. fragments that are properly closed, but during editing, we have to live
  14884. with the uncertainty caused by missing closing delimiters. This function
  14885. looks only before point, not after."
  14886. (catch 'exit
  14887. (let ((pos (point))
  14888. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14889. (lim (progn
  14890. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14891. (point)))
  14892. dd-on str (start 0) m re)
  14893. (goto-char pos)
  14894. (when dodollar
  14895. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14896. re (nth 1 (assoc "$" org-latex-regexps)))
  14897. (while (string-match re str start)
  14898. (cond
  14899. ((= (match-end 0) (length str))
  14900. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  14901. ((= (match-end 0) (- (length str) 5))
  14902. (throw 'exit nil))
  14903. (t (setq start (match-end 0))))))
  14904. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14905. (goto-char pos)
  14906. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14907. (and (match-beginning 2) (throw 'exit nil))
  14908. ;; count $$
  14909. (while (re-search-backward "\\$\\$" lim t)
  14910. (setq dd-on (not dd-on)))
  14911. (goto-char pos)
  14912. (if dd-on (cons "$$" m))))))
  14913. (defun org-try-cdlatex-tab ()
  14914. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14915. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14916. - inside a LaTeX fragment, or
  14917. - after the first word in a line, where an abbreviation expansion could
  14918. insert a LaTeX environment."
  14919. (when org-cdlatex-mode
  14920. (cond
  14921. ((save-excursion
  14922. (skip-chars-backward "a-zA-Z0-9*")
  14923. (skip-chars-backward " \t")
  14924. (bolp))
  14925. (cdlatex-tab) t)
  14926. ((org-inside-LaTeX-fragment-p)
  14927. (cdlatex-tab) t)
  14928. (t nil))))
  14929. (defun org-cdlatex-underscore-caret (&optional arg)
  14930. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14931. Revert to the normal definition outside of these fragments."
  14932. (interactive "P")
  14933. (if (org-inside-LaTeX-fragment-p)
  14934. (call-interactively 'cdlatex-sub-superscript)
  14935. (let (org-cdlatex-mode)
  14936. (call-interactively (key-binding (vector last-input-event))))))
  14937. (defun org-cdlatex-math-modify (&optional arg)
  14938. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14939. Revert to the normal definition outside of these fragments."
  14940. (interactive "P")
  14941. (if (org-inside-LaTeX-fragment-p)
  14942. (call-interactively 'cdlatex-math-modify)
  14943. (let (org-cdlatex-mode)
  14944. (call-interactively (key-binding (vector last-input-event))))))
  14945. (defvar org-latex-fragment-image-overlays nil
  14946. "List of overlays carrying the images of latex fragments.")
  14947. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14948. (defun org-remove-latex-fragment-image-overlays ()
  14949. "Remove all overlays with LaTeX fragment images in current buffer."
  14950. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14951. (setq org-latex-fragment-image-overlays nil))
  14952. (defun org-preview-latex-fragment (&optional subtree)
  14953. "Preview the LaTeX fragment at point, or all locally or globally.
  14954. If the cursor is in a LaTeX fragment, create the image and overlay
  14955. it over the source code. If there is no fragment at point, display
  14956. all fragments in the current text, from one headline to the next. With
  14957. prefix SUBTREE, display all fragments in the current subtree. With a
  14958. double prefix `C-u C-u', or when the cursor is before the first headline,
  14959. display all fragments in the buffer.
  14960. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14961. (interactive "P")
  14962. (org-remove-latex-fragment-image-overlays)
  14963. (save-excursion
  14964. (save-restriction
  14965. (let (beg end at msg)
  14966. (cond
  14967. ((or (equal subtree '(16))
  14968. (not (save-excursion
  14969. (re-search-backward (concat "^" outline-regexp) nil t))))
  14970. (setq beg (point-min) end (point-max)
  14971. msg "Creating images for buffer...%s"))
  14972. ((equal subtree '(4))
  14973. (org-back-to-heading)
  14974. (setq beg (point) end (org-end-of-subtree t)
  14975. msg "Creating images for subtree...%s"))
  14976. (t
  14977. (if (setq at (org-inside-LaTeX-fragment-p))
  14978. (goto-char (max (point-min) (- (cdr at) 2)))
  14979. (org-back-to-heading))
  14980. (setq beg (point) end (progn (outline-next-heading) (point))
  14981. msg (if at "Creating image...%s"
  14982. "Creating images for entry...%s"))))
  14983. (message msg "")
  14984. (narrow-to-region beg end)
  14985. (org-format-latex
  14986. (concat "ltxpng/" (file-name-sans-extension
  14987. (file-name-nondirectory
  14988. buffer-file-name)))
  14989. default-directory 'overlays msg at)
  14990. (message msg "done. Use `C-c C-c' to remove images.")))))
  14991. (defvar org-latex-regexps
  14992. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14993. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14994. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14995. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14996. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14997. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14998. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14999. "Regular expressions for matching embedded LaTeX.")
  15000. (defun org-format-latex (prefix &optional dir overlays msg at)
  15001. "Replace LaTeX fragments with links to an image, and produce images."
  15002. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15003. (let* ((prefixnodir (file-name-nondirectory prefix))
  15004. (absprefix (expand-file-name prefix dir))
  15005. (todir (file-name-directory absprefix))
  15006. (opt org-format-latex-options)
  15007. (matchers (plist-get opt :matchers))
  15008. (re-list org-latex-regexps)
  15009. (cnt 0) txt link beg end re e oldfiles
  15010. m n block linkfile movefile ov)
  15011. ;; Make sure the directory exists
  15012. (or (file-directory-p todir) (make-directory todir))
  15013. ;; Check if there are old images files with this prefix, and remove them
  15014. (setq oldfiles (directory-files
  15015. todir 'full
  15016. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  15017. (while oldfiles (delete-file (pop oldfiles)))
  15018. ;; Check the different regular expressions
  15019. (while (setq e (pop re-list))
  15020. (setq m (car e) re (nth 1 e) n (nth 2 e)
  15021. block (if (nth 3 e) "\n\n" ""))
  15022. (when (member m matchers)
  15023. (goto-char (point-min))
  15024. (while (re-search-forward re nil t)
  15025. (when (or (not at) (equal (cdr at) (match-beginning n)))
  15026. (setq txt (match-string n)
  15027. beg (match-beginning n) end (match-end n)
  15028. cnt (1+ cnt)
  15029. linkfile (format "%s_%04d.png" prefix cnt)
  15030. movefile (format "%s_%04d.png" absprefix cnt)
  15031. link (concat block "[[file:" linkfile "]]" block))
  15032. (if msg (message msg cnt))
  15033. (goto-char beg)
  15034. (org-create-formula-image
  15035. txt movefile opt)
  15036. (if overlays
  15037. (progn
  15038. (setq ov (org-make-overlay beg end))
  15039. (if (featurep 'xemacs)
  15040. (progn
  15041. (org-overlay-put ov 'invisible t)
  15042. (org-overlay-put
  15043. ov 'end-glyph
  15044. (make-glyph (vector 'png :file movefile))))
  15045. (org-overlay-put
  15046. ov 'display
  15047. (list 'image :type 'png :file movefile :ascent 'center)))
  15048. (push ov org-latex-fragment-image-overlays)
  15049. (goto-char end))
  15050. (delete-region beg end)
  15051. (insert link))))))))
  15052. ;; This function borrows from Ganesh Swami's latex2png.el
  15053. (defun org-create-formula-image (string tofile options)
  15054. (let* ((tmpdir (if (featurep 'xemacs)
  15055. (temp-directory)
  15056. temporary-file-directory))
  15057. (texfilebase (make-temp-name
  15058. (expand-file-name "orgtex" tmpdir)))
  15059. ;(texfilebase (make-temp-file "orgtex"))
  15060. ; (dummy (delete-file texfilebase))
  15061. (texfile (concat texfilebase ".tex"))
  15062. (dvifile (concat texfilebase ".dvi"))
  15063. (pngfile (concat texfilebase ".png"))
  15064. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  15065. (fg (or (plist-get options :foreground) "Black"))
  15066. (bg (or (plist-get options :background) "Transparent")))
  15067. (with-temp-file texfile
  15068. (insert "\\documentclass{article}
  15069. \\usepackage{fullpage}
  15070. \\usepackage{amssymb}
  15071. \\usepackage[usenames]{color}
  15072. \\usepackage{amsmath}
  15073. \\usepackage{latexsym}
  15074. \\usepackage[mathscr]{eucal}
  15075. \\pagestyle{empty}
  15076. \\begin{document}\n" string "\n\\end{document}\n"))
  15077. (let ((dir default-directory))
  15078. (condition-case nil
  15079. (progn
  15080. (cd tmpdir)
  15081. (call-process "latex" nil nil nil texfile))
  15082. (error nil))
  15083. (cd dir))
  15084. (if (not (file-exists-p dvifile))
  15085. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15086. (call-process "dvipng" nil nil nil
  15087. "-E" "-fg" fg "-bg" bg
  15088. "-x" scale "-y" scale "-T" "tight"
  15089. "-o" pngfile
  15090. dvifile)
  15091. (if (not (file-exists-p pngfile))
  15092. (progn (message "Failed to create png file from %s" texfile) nil)
  15093. ;; Use the requested file name and clean up
  15094. (copy-file pngfile tofile 'replace)
  15095. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15096. (delete-file (concat texfilebase e)))
  15097. pngfile))))
  15098. ;;; Key bindings
  15099. ;; - Bindings in Org-mode map are currently
  15100. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  15101. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  15102. ;; e (?) useful from outline-mode
  15103. ;; i k @ expendable from outline-mode
  15104. ;; 0123456789 % & ()_{} " ` free
  15105. ;; Make `C-c C-x' a prefix key
  15106. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15107. ;; TAB key with modifiers
  15108. (define-key org-mode-map "\C-i" 'org-cycle)
  15109. (define-key org-mode-map [(tab)] 'org-cycle)
  15110. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  15111. (define-key org-mode-map [(meta tab)] 'org-complete)
  15112. ;; The following line is necessary under Suse GNU/Linux
  15113. (unless (featurep 'xemacs)
  15114. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  15115. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  15116. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  15117. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15118. (define-key org-mode-map [(meta return)] 'org-meta-return)
  15119. ;; Cursor keys with modifiers
  15120. (define-key org-mode-map [(meta left)] 'org-metaleft)
  15121. (define-key org-mode-map [(meta right)] 'org-metaright)
  15122. (define-key org-mode-map [(meta up)] 'org-metaup)
  15123. (define-key org-mode-map [(meta down)] 'org-metadown)
  15124. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15125. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15126. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15127. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15128. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  15129. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  15130. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  15131. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  15132. ;; Extra keys for tty access. We only set them when really needed
  15133. ;; because otherwise the menus don't show the simple keys
  15134. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15135. (not window-system))
  15136. (define-key org-mode-map "\M-\C-i" 'org-complete)
  15137. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15138. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15139. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return)
  15140. (define-key org-mode-map [?\e (return)] 'org-meta-return)
  15141. (define-key org-mode-map [?\e (left)] 'org-metaleft)
  15142. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft)
  15143. (define-key org-mode-map [?\e (right)] 'org-metaright)
  15144. (define-key org-mode-map "\C-c\C-xr" 'org-metaright)
  15145. (define-key org-mode-map [?\e (up)] 'org-metaup)
  15146. (define-key org-mode-map "\C-c\C-xu" 'org-metaup)
  15147. (define-key org-mode-map [?\e (down)] 'org-metadown)
  15148. (define-key org-mode-map "\C-c\C-xd" 'org-metadown)
  15149. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15150. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15151. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15152. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15153. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  15154. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  15155. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  15156. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright))
  15157. ;; All the other keys
  15158. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15159. (define-key org-mode-map "\C-c\C-r" 'org-reveal)
  15160. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  15161. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  15162. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  15163. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  15164. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  15165. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  15166. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  15167. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  15168. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  15169. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  15170. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  15171. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  15172. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  15173. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  15174. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  15175. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  15176. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  15177. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  15178. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  15179. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15180. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15181. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15182. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15183. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  15184. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  15185. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15186. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15187. (define-key org-mode-map "\C-c]" 'org-remove-file)
  15188. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  15189. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  15190. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15191. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  15192. (define-key org-mode-map "\C-m" 'org-return)
  15193. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  15194. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  15195. (define-key org-mode-map "\C-c+" 'org-table-sum)
  15196. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  15197. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  15198. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  15199. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15200. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  15201. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15202. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15203. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  15204. (define-key org-mode-map "\C-c\C-e" 'org-export)
  15205. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15206. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  15207. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15208. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15209. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15210. (define-key org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15211. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15212. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15213. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15214. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15215. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15216. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15217. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15218. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15219. (when (featurep 'xemacs)
  15220. (define-key org-mode-map 'button3 'popup-mode-menu))
  15221. (defsubst org-table-p () (org-at-table-p))
  15222. (defun org-self-insert-command (N)
  15223. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15224. If the cursor is in a table looking at whitespace, the whitespace is
  15225. overwritten, and the table is not marked as requiring realignment."
  15226. (interactive "p")
  15227. (if (and (org-table-p)
  15228. (progn
  15229. ;; check if we blank the field, and if that triggers align
  15230. (and org-table-auto-blank-field
  15231. (member last-command
  15232. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  15233. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15234. ;; got extra space, this field does not determine column width
  15235. (let (org-table-may-need-update) (org-table-blank-field))
  15236. ;; no extra space, this field may determine column width
  15237. (org-table-blank-field)))
  15238. t)
  15239. (eq N 1)
  15240. (looking-at "[^|\n]* |"))
  15241. (let (org-table-may-need-update)
  15242. (goto-char (1- (match-end 0)))
  15243. (delete-backward-char 1)
  15244. (goto-char (match-beginning 0))
  15245. (self-insert-command N))
  15246. (setq org-table-may-need-update t)
  15247. (self-insert-command N)))
  15248. (defun org-delete-backward-char (N)
  15249. "Like `delete-backward-char', insert whitespace at field end in tables.
  15250. When deleting backwards, in tables this function will insert whitespace in
  15251. front of the next \"|\" separator, to keep the table aligned. The table will
  15252. still be marked for re-alignment if the field did fill the entire column,
  15253. because, in this case the deletion might narrow the column."
  15254. (interactive "p")
  15255. (if (and (org-table-p)
  15256. (eq N 1)
  15257. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15258. (looking-at ".*?|"))
  15259. (let ((pos (point))
  15260. (noalign (looking-at "[^|\n\r]* |"))
  15261. (c org-table-may-need-update))
  15262. (backward-delete-char N)
  15263. (skip-chars-forward "^|")
  15264. (insert " ")
  15265. (goto-char (1- pos))
  15266. ;; noalign: if there were two spaces at the end, this field
  15267. ;; does not determine the width of the column.
  15268. (if noalign (setq org-table-may-need-update c)))
  15269. (backward-delete-char N)))
  15270. (defun org-delete-char (N)
  15271. "Like `delete-char', but insert whitespace at field end in tables.
  15272. When deleting characters, in tables this function will insert whitespace in
  15273. front of the next \"|\" separator, to keep the table aligned. The table will
  15274. still be marked for re-alignment if the field did fill the entire column,
  15275. because, in this case the deletion might narrow the column."
  15276. (interactive "p")
  15277. (if (and (org-table-p)
  15278. (not (bolp))
  15279. (not (= (char-after) ?|))
  15280. (eq N 1))
  15281. (if (looking-at ".*?|")
  15282. (let ((pos (point))
  15283. (noalign (looking-at "[^|\n\r]* |"))
  15284. (c org-table-may-need-update))
  15285. (replace-match (concat
  15286. (substring (match-string 0) 1 -1)
  15287. " |"))
  15288. (goto-char pos)
  15289. ;; noalign: if there were two spaces at the end, this field
  15290. ;; does not determine the width of the column.
  15291. (if noalign (setq org-table-may-need-update c)))
  15292. (delete-char N))
  15293. (delete-char N)))
  15294. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15295. (put 'org-self-insert-command 'delete-selection t)
  15296. (put 'orgtbl-self-insert-command 'delete-selection t)
  15297. (put 'org-delete-char 'delete-selection 'supersede)
  15298. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15299. ;; How to do this: Measure non-white length of current string
  15300. ;; If equal to column width, we should realign.
  15301. (defun org-remap (map &rest commands)
  15302. "In MAP, remap the functions given in COMMANDS.
  15303. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15304. (let (new old)
  15305. (while commands
  15306. (setq old (pop commands) new (pop commands))
  15307. (if (fboundp 'command-remapping)
  15308. (define-key map (vector 'remap old) new)
  15309. (substitute-key-definition old new map global-map)))))
  15310. (when (eq org-enable-table-editor 'optimized)
  15311. ;; If the user wants maximum table support, we need to hijack
  15312. ;; some standard editing functions
  15313. (org-remap org-mode-map
  15314. 'self-insert-command 'org-self-insert-command
  15315. 'delete-char 'org-delete-char
  15316. 'delete-backward-char 'org-delete-backward-char)
  15317. (define-key org-mode-map "|" 'org-force-self-insert))
  15318. (defun org-shiftcursor-error ()
  15319. "Throw an error because Shift-Cursor command was applied in wrong context."
  15320. (error "This command is active in special context like tables, headlines or timestamps"))
  15321. (defun org-shifttab (&optional arg)
  15322. "Global visibility cycling or move to previous table field.
  15323. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15324. on context.
  15325. See the individual commands for more information."
  15326. (interactive "P")
  15327. (cond
  15328. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15329. (t (call-interactively 'org-global-cycle))))
  15330. (defun org-shiftmetaleft ()
  15331. "Promote subtree or delete table column.
  15332. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  15333. See the individual commands for more information."
  15334. (interactive)
  15335. (cond
  15336. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15337. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15338. ((org-at-item-p) (call-interactively 'org-outdent-item))
  15339. (t (org-shiftcursor-error))))
  15340. (defun org-shiftmetaright ()
  15341. "Demote subtree or insert table column.
  15342. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  15343. See the individual commands for more information."
  15344. (interactive)
  15345. (cond
  15346. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15347. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15348. ((org-at-item-p) (call-interactively 'org-indent-item))
  15349. (t (org-shiftcursor-error))))
  15350. (defun org-shiftmetaup (&optional arg)
  15351. "Move subtree up or kill table row.
  15352. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15353. `org-move-item-up' depending on context. See the individual commands
  15354. for more information."
  15355. (interactive "P")
  15356. (cond
  15357. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15358. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15359. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15360. (t (org-shiftcursor-error))))
  15361. (defun org-shiftmetadown (&optional arg)
  15362. "Move subtree down or insert table row.
  15363. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15364. `org-move-item-down', depending on context. See the individual
  15365. commands for more information."
  15366. (interactive "P")
  15367. (cond
  15368. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15369. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15370. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15371. (t (org-shiftcursor-error))))
  15372. (defun org-metaleft (&optional arg)
  15373. "Promote heading or move table column to left.
  15374. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15375. With no specific context, calls the Emacs default `backward-word'.
  15376. See the individual commands for more information."
  15377. (interactive "P")
  15378. (cond
  15379. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15380. ((or (org-on-heading-p) (org-region-active-p))
  15381. (call-interactively 'org-do-promote))
  15382. (t (call-interactively 'backward-word))))
  15383. (defun org-metaright (&optional arg)
  15384. "Demote subtree or move table column to right.
  15385. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15386. With no specific context, calls the Emacs default `forward-word'.
  15387. See the individual commands for more information."
  15388. (interactive "P")
  15389. (cond
  15390. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15391. ((or (org-on-heading-p) (org-region-active-p))
  15392. (call-interactively 'org-do-demote))
  15393. (t (call-interactively 'forward-word))))
  15394. (defun org-metaup (&optional arg)
  15395. "Move subtree up or move table row up.
  15396. Calls `org-move-subtree-up' or `org-table-move-row' or
  15397. `org-move-item-up', depending on context. See the individual commands
  15398. for more information."
  15399. (interactive "P")
  15400. (cond
  15401. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15402. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15403. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15404. (t (org-shiftcursor-error))))
  15405. (defun org-metadown (&optional arg)
  15406. "Move subtree down or move table row down.
  15407. Calls `org-move-subtree-down' or `org-table-move-row' or
  15408. `org-move-item-down', depending on context. See the individual
  15409. commands for more information."
  15410. (interactive "P")
  15411. (cond
  15412. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15413. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15414. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15415. (t (org-shiftcursor-error))))
  15416. (defun org-shiftup (&optional arg)
  15417. "Increase item in timestamp or increase priority of current headline.
  15418. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  15419. See the individual commands for more information."
  15420. (interactive "P")
  15421. (cond
  15422. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  15423. ((org-on-heading-p) (call-interactively 'org-priority-up))
  15424. ((org-at-item-p) (call-interactively 'org-previous-item))
  15425. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  15426. (defun org-shiftdown (&optional arg)
  15427. "Decrease item in timestamp or decrease priority of current headline.
  15428. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  15429. See the individual commands for more information."
  15430. (interactive "P")
  15431. (cond
  15432. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  15433. ((org-on-heading-p) (call-interactively 'org-priority-down))
  15434. (t (call-interactively 'org-next-item))))
  15435. (defun org-shiftright ()
  15436. "Next TODO keyword or timestamp one day later, depending on context."
  15437. (interactive)
  15438. (cond
  15439. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15440. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  15441. (t (org-shiftcursor-error))))
  15442. (defun org-shiftleft ()
  15443. "Previous TODO keyword or timestamp one day earlier, depending on context."
  15444. (interactive)
  15445. (cond
  15446. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15447. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  15448. (t (org-shiftcursor-error))))
  15449. (defun org-copy-special ()
  15450. "Copy region in table or copy current subtree.
  15451. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15452. See the individual commands for more information."
  15453. (interactive)
  15454. (call-interactively
  15455. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15456. (defun org-cut-special ()
  15457. "Cut region in table or cut current subtree.
  15458. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15459. See the individual commands for more information."
  15460. (interactive)
  15461. (call-interactively
  15462. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15463. (defun org-paste-special (arg)
  15464. "Paste rectangular region into table, or past subtree relative to level.
  15465. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15466. See the individual commands for more information."
  15467. (interactive "P")
  15468. (if (org-at-table-p)
  15469. (org-table-paste-rectangle)
  15470. (org-paste-subtree arg)))
  15471. (defun org-ctrl-c-ctrl-c (&optional arg)
  15472. "Set tags in headline, or update according to changed information at point.
  15473. This command does many different things, depending on context:
  15474. - If the cursor is in a headline, prompt for tags and insert them
  15475. into the current line, aligned to `org-tags-column'. When called
  15476. with prefix arg, realign all tags in the current buffer.
  15477. - If the cursor is in one of the special #+KEYWORD lines, this
  15478. triggers scanning the buffer for these lines and updating the
  15479. information.
  15480. - If the cursor is inside a table, realign the table. This command
  15481. works even if the automatic table editor has been turned off.
  15482. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15483. the entire table.
  15484. - If the cursor is inside a table created by the table.el package,
  15485. activate that table.
  15486. - If the current buffer is a remember buffer, close note and file it.
  15487. with a prefix argument, file it without further interaction to the default
  15488. location.
  15489. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15490. links in this buffer.
  15491. - If the cursor is on a numbered item in a plain list, renumber the
  15492. ordered list."
  15493. (interactive "P")
  15494. (let ((org-enable-table-editor t))
  15495. (cond
  15496. ((or org-clock-overlays
  15497. org-occur-highlights
  15498. org-latex-fragment-image-overlays)
  15499. (org-remove-clock-overlays)
  15500. (org-remove-occur-highlights)
  15501. (org-remove-latex-fragment-image-overlays)
  15502. (message "Temporary highlights/overlays removed from current buffer"))
  15503. ((and (local-variable-p 'org-finish-function (current-buffer))
  15504. (fboundp org-finish-function))
  15505. (funcall org-finish-function))
  15506. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15507. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15508. ((org-at-table.el-p)
  15509. (require 'table)
  15510. (beginning-of-line 1)
  15511. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  15512. (call-interactively 'table-recognize-table))
  15513. ((org-at-table-p)
  15514. (org-table-maybe-eval-formula)
  15515. (if arg
  15516. (call-interactively 'org-table-recalculate)
  15517. (org-table-maybe-recalculate-line))
  15518. (call-interactively 'org-table-align))
  15519. ((org-at-item-checkbox-p)
  15520. (call-interactively 'org-toggle-checkbox))
  15521. ((org-at-item-p)
  15522. (call-interactively 'org-renumber-ordered-list))
  15523. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  15524. (cond
  15525. ((equal (match-string 1) "TBLFM")
  15526. ;; Recalculate the table before this line
  15527. (save-excursion
  15528. (beginning-of-line 1)
  15529. (skip-chars-backward " \r\n\t")
  15530. (if (org-at-table-p)
  15531. (org-call-with-arg 'org-table-recalculate t))))
  15532. (t
  15533. (call-interactively 'org-mode-restart))))
  15534. (t (error "C-c C-c can do nothing useful at this location.")))))
  15535. (defun org-mode-restart ()
  15536. "Restart Org-mode, to scan again for special lines.
  15537. Also updates the keyword regular expressions."
  15538. (interactive)
  15539. (let ((org-inhibit-startup t)) (org-mode))
  15540. (message "Org-mode restarted to refresh keyword and special line setup"))
  15541. (defun org-return ()
  15542. "Goto next table row or insert a newline.
  15543. Calls `org-table-next-row' or `newline', depending on context.
  15544. See the individual commands for more information."
  15545. (interactive)
  15546. (cond
  15547. ((org-at-table-p)
  15548. (org-table-justify-field-maybe)
  15549. (call-interactively 'org-table-next-row))
  15550. (t (newline))))
  15551. (defun org-meta-return (&optional arg)
  15552. "Insert a new heading or wrap a region in a table.
  15553. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15554. See the individual commands for more information."
  15555. (interactive "P")
  15556. (cond
  15557. ((org-at-table-p)
  15558. (call-interactively 'org-table-wrap-region))
  15559. (t (call-interactively 'org-insert-heading))))
  15560. ;;; Menu entries
  15561. ;; Define the Org-mode menus
  15562. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15563. '("Tbl"
  15564. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  15565. ["Next Field" org-cycle (org-at-table-p)]
  15566. ["Previous Field" org-shifttab (org-at-table-p)]
  15567. ["Next Row" org-return (org-at-table-p)]
  15568. "--"
  15569. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15570. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15571. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15572. "--"
  15573. ("Column"
  15574. ["Move Column Left" org-metaleft (org-at-table-p)]
  15575. ["Move Column Right" org-metaright (org-at-table-p)]
  15576. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15577. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  15578. "--"
  15579. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  15580. ("Row"
  15581. ["Move Row Up" org-metaup (org-at-table-p)]
  15582. ["Move Row Down" org-metadown (org-at-table-p)]
  15583. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15584. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15585. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15586. "--"
  15587. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  15588. ("Rectangle"
  15589. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15590. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15591. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15592. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15593. "--"
  15594. ("Calculate"
  15595. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15596. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15597. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  15598. "--"
  15599. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15600. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15601. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15602. "--"
  15603. ["Sum Column/Rectangle" org-table-sum
  15604. (or (org-at-table-p) (org-region-active-p))]
  15605. ["Which Column?" org-table-current-column (org-at-table-p)])
  15606. ["Debug Formulas"
  15607. (setq org-table-formula-debug (not org-table-formula-debug))
  15608. :style toggle :selected org-table-formula-debug]
  15609. "--"
  15610. ["Create" org-table-create (and (not (org-at-table-p))
  15611. org-enable-table-editor)]
  15612. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15613. ["Import from File" org-table-import (not (org-at-table-p))]
  15614. ["Export to File" org-table-export (org-at-table-p)]
  15615. "--"
  15616. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15617. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15618. '("Org"
  15619. ("Show/Hide"
  15620. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  15621. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  15622. ["Sparse Tree" org-occur t]
  15623. ["Reveal Context" org-reveal t]
  15624. ["Show All" show-all t])
  15625. "--"
  15626. ["New Heading" org-insert-heading t]
  15627. ("Navigate Headings"
  15628. ["Up" outline-up-heading t]
  15629. ["Next" outline-next-visible-heading t]
  15630. ["Previous" outline-previous-visible-heading t]
  15631. ["Next Same Level" outline-forward-same-level t]
  15632. ["Previous Same Level" outline-backward-same-level t]
  15633. "--"
  15634. ["Jump" org-goto t])
  15635. ("Edit Structure"
  15636. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15637. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15638. "--"
  15639. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15640. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15641. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15642. "--"
  15643. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15644. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15645. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15646. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15647. "--"
  15648. ["Convert to odd levels" org-convert-to-odd-levels t]
  15649. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15650. ("Archive"
  15651. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15652. ["Check and Tag Children" (org-toggle-archive-tag (4))
  15653. :active t :keys "C-u C-c C-x C-a"]
  15654. ["Sparse trees open ARCHIVE trees"
  15655. (setq org-sparse-tree-open-archived-trees
  15656. (not org-sparse-tree-open-archived-trees))
  15657. :style toggle :selected org-sparse-tree-open-archived-trees]
  15658. ["Cycling opens ARCHIVE trees"
  15659. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  15660. :style toggle :selected org-cycle-open-archived-trees]
  15661. ["Agenda includes ARCHIVE trees"
  15662. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  15663. :style toggle :selected (not org-agenda-skip-archived-trees)]
  15664. "--"
  15665. ["Move Subtree to Archive" org-archive-subtree t]
  15666. ["Check and Move Children" (org-archive-subtree '(4))
  15667. :active t :keys "C-u C-c $"])
  15668. "--"
  15669. ("TODO Lists"
  15670. ["TODO/DONE/-" org-todo t]
  15671. ("Select keyword"
  15672. ["Next keyword" org-shiftright (org-on-heading-p)]
  15673. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15674. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  15675. ["Show TODO Tree" org-show-todo-tree t]
  15676. ["Global TODO list" org-todo-list t]
  15677. "--"
  15678. ["Set Priority" org-priority t]
  15679. ["Priority Up" org-shiftup t]
  15680. ["Priority Down" org-shiftdown t]
  15681. "--"
  15682. ; ["Insert Checkbox" org-insert-todo-heading (org-in-item-p)]
  15683. ; ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  15684. ; ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  15685. ; (or (org-on-heading-p) (org-at-item-p))]
  15686. ; ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  15687. ; (or (org-on-heading-p) (org-at-item-p))]
  15688. ; ["Update Statistics" org-update-checkbox-count t]
  15689. )
  15690. ("Dates and Scheduling"
  15691. ["Timestamp" org-time-stamp t]
  15692. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15693. ("Change Date"
  15694. ["1 Day Later" org-shiftright t]
  15695. ["1 Day Earlier" org-shiftleft t]
  15696. ["1 ... Later" org-shiftup t]
  15697. ["1 ... Earlier" org-shiftdown t])
  15698. ["Compute Time Range" org-evaluate-time-range t]
  15699. ["Schedule Item" org-schedule t]
  15700. ["Deadline" org-deadline t]
  15701. "--"
  15702. ["Custom time format" org-toggle-time-stamp-overlays
  15703. :style radio :selected org-display-custom-times]
  15704. "--"
  15705. ["Goto Calendar" org-goto-calendar t]
  15706. ["Date from Calendar" org-date-from-calendar t])
  15707. ("Logging work"
  15708. ["Clock in" org-clock-in t]
  15709. ["Clock out" org-clock-out t]
  15710. ["Clock cancel" org-clock-cancel t]
  15711. ["Display times" org-clock-display t]
  15712. ["Create clock table" org-clock-report t]
  15713. "--"
  15714. ["Record DONE time"
  15715. (progn (setq org-log-done (not org-log-done))
  15716. (message "Switching to %s will %s record a timestamp"
  15717. org-done-string
  15718. (if org-log-done "automatically" "not")))
  15719. :style toggle :selected org-log-done])
  15720. "--"
  15721. ["Agenda Command..." org-agenda t]
  15722. ("File List for Agenda")
  15723. ("Special views current file"
  15724. ["TODO Tree" org-show-todo-tree t]
  15725. ["Check Deadlines" org-check-deadlines t]
  15726. ["Timeline" org-timeline t]
  15727. ["Tags Tree" org-tags-sparse-tree t])
  15728. "--"
  15729. ("Hyperlinks"
  15730. ["Store Link (Global)" org-store-link t]
  15731. ["Insert Link" org-insert-link t]
  15732. ["Follow Link" org-open-at-point t]
  15733. "--"
  15734. ["Descriptive Links"
  15735. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15736. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  15737. ["Literal Links"
  15738. (progn
  15739. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15740. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  15741. "--"
  15742. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  15743. (save-excursion (goto-char (point-min))
  15744. (re-search-forward "<[a-z]+:" nil t))])
  15745. "--"
  15746. ["Export/Publish..." org-export t]
  15747. ("LaTeX"
  15748. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15749. :selected org-cdlatex-mode]
  15750. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15751. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15752. ["Modify math symbol" org-cdlatex-math-modify
  15753. (org-inside-LaTeX-fragment-p)]
  15754. ["Export LaTeX fragments as images"
  15755. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  15756. :style toggle :selected org-export-with-LaTeX-fragments])
  15757. "--"
  15758. ("Documentation"
  15759. ["Show Version" org-version t]
  15760. ["Info Documentation" org-info t])
  15761. ("Customize"
  15762. ["Browse Org Group" org-customize t]
  15763. "--"
  15764. ["Expand This Menu" org-create-customize-menu
  15765. (fboundp 'customize-menu-create)])
  15766. "--"
  15767. ["Refresh setup" org-mode-restart t]
  15768. ))
  15769. (defun org-info (&optional node)
  15770. "Read documentation for Org-mode in the info system.
  15771. With optional NODE, go directly to that node."
  15772. (interactive)
  15773. (require 'info)
  15774. (Info-goto-node (format "(org)%s" (or node ""))))
  15775. (defun org-install-agenda-files-menu ()
  15776. (let ((bl (buffer-list)))
  15777. (save-excursion
  15778. (while bl
  15779. (set-buffer (pop bl))
  15780. (if (org-mode-p) (setq bl nil)))
  15781. (when (org-mode-p)
  15782. (easy-menu-change
  15783. '("Org") "File List for Agenda"
  15784. (append
  15785. (list
  15786. ["Edit File List" (org-edit-agenda-file-list) t]
  15787. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15788. ["Remove Current File from List" org-remove-file t]
  15789. ["Cycle through agenda files" org-cycle-agenda-files t]
  15790. "--")
  15791. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15792. ;;; Documentation
  15793. (defun org-customize ()
  15794. "Call the customize function with org as argument."
  15795. (interactive)
  15796. (customize-browse 'org))
  15797. (defun org-create-customize-menu ()
  15798. "Create a full customization menu for Org-mode, insert it into the menu."
  15799. (interactive)
  15800. (if (fboundp 'customize-menu-create)
  15801. (progn
  15802. (easy-menu-change
  15803. '("Org") "Customize"
  15804. `(["Browse Org group" org-customize t]
  15805. "--"
  15806. ,(customize-menu-create 'org)
  15807. ["Set" Custom-set t]
  15808. ["Save" Custom-save t]
  15809. ["Reset to Current" Custom-reset-current t]
  15810. ["Reset to Saved" Custom-reset-saved t]
  15811. ["Reset to Standard Settings" Custom-reset-standard t]))
  15812. (message "\"Org\"-menu now contains full customization menu"))
  15813. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15814. ;;; Miscellaneous stuff
  15815. (defun org-context ()
  15816. "Return a list of contexts of the current cursor position.
  15817. If several contexts apply, all are returned.
  15818. Each context entry is a list with a symbol naming the context, and
  15819. two positions indicating start and end of the context. Possible
  15820. contexts are:
  15821. :headline anywhere in a headline
  15822. :headline-stars on the leading stars in a headline
  15823. :todo-keyword on a TODO keyword (including DONE) in a headline
  15824. :tags on the TAGS in a headline
  15825. :priority on the priority cookie in a headline
  15826. :item on the first line of a plain list item
  15827. :item-bullet on the bullet/number of a plain list item
  15828. :checkbox on the checkbox in a plain list item
  15829. :table in an org-mode table
  15830. :table-special on a special filed in a table
  15831. :table-table in a table.el table
  15832. :link on a hyperline
  15833. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15834. :target on a <<target>>
  15835. :radio-target on a <<<radio-target>>>
  15836. :latex-fragment on a LaTeX fragment
  15837. :latex-preview on a LaTeX fragment with overlayed preview image
  15838. This function expects the position to be visible because it uses font-lock
  15839. faces as a help to recognize the following contexts: :table-special, :link,
  15840. and :keyword."
  15841. (let* ((f (get-text-property (point) 'face))
  15842. (faces (if (listp f) f (list f)))
  15843. (p (point)) clist o)
  15844. ;; First the large context
  15845. (cond
  15846. ((org-on-heading-p)
  15847. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15848. (when (progn
  15849. (beginning-of-line 1)
  15850. (looking-at org-todo-line-tags-regexp))
  15851. (push (org-point-in-group p 1 :headline-stars) clist)
  15852. (push (org-point-in-group p 2 :todo-keyword) clist)
  15853. (push (org-point-in-group p 4 :tags) clist))
  15854. (goto-char p)
  15855. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  15856. (if (looking-at "\\[#[A-Z]\\]")
  15857. (push (org-point-in-group p 0 :priority) clist)))
  15858. ((org-at-item-p)
  15859. (push (org-point-in-group p 2 :item-bullet) clist)
  15860. (push (list :item (point-at-bol)
  15861. (save-excursion (org-end-of-item) (point)))
  15862. clist)
  15863. (and (org-at-item-checkbox-p)
  15864. (push (org-point-in-group p 0 :checkbox) clist)))
  15865. ((org-at-table-p)
  15866. (push (list :table (org-table-begin) (org-table-end)) clist)
  15867. (if (memq 'org-formula faces)
  15868. (push (list :table-special
  15869. (previous-single-property-change p 'face)
  15870. (next-single-property-change p 'face)) clist)))
  15871. ((org-at-table-p 'any)
  15872. (push (list :table-table) clist)))
  15873. (goto-char p)
  15874. ;; Now the small context
  15875. (cond
  15876. ((org-at-timestamp-p)
  15877. (push (org-point-in-group p 0 :timestamp) clist))
  15878. ((memq 'org-link faces)
  15879. (push (list :link
  15880. (previous-single-property-change p 'face)
  15881. (next-single-property-change p 'face)) clist))
  15882. ((memq 'org-special-keyword faces)
  15883. (push (list :keyword
  15884. (previous-single-property-change p 'face)
  15885. (next-single-property-change p 'face)) clist))
  15886. ((org-on-target-p)
  15887. (push (org-point-in-group p 0 :target) clist)
  15888. (goto-char (1- (match-beginning 0)))
  15889. (if (looking-at org-radio-target-regexp)
  15890. (push (org-point-in-group p 0 :radio-target) clist))
  15891. (goto-char p))
  15892. ((setq o (car (delq nil
  15893. (mapcar
  15894. (lambda (x)
  15895. (if (memq x org-latex-fragment-image-overlays) x))
  15896. (org-overlays-at (point))))))
  15897. (push (list :latex-fragment
  15898. (org-overlay-start o) (org-overlay-end o)) clist)
  15899. (push (list :latex-preview
  15900. (org-overlay-start o) (org-overlay-end o)) clist))
  15901. ((org-inside-LaTeX-fragment-p)
  15902. ;; FIXME: positions wrong.
  15903. (push (list :latex-fragment (point) (point)) clist)))
  15904. (setq clist (nreverse (delq nil clist)))
  15905. clist))
  15906. (defun org-point-in-group (point group &optional context)
  15907. "Check if POINT is in match-group GROUP.
  15908. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15909. match. If the match group does ot exist or point is not inside it,
  15910. return nil."
  15911. (and (match-beginning group)
  15912. (>= point (match-beginning group))
  15913. (<= point (match-end group))
  15914. (if context
  15915. (list context (match-beginning group) (match-end group))
  15916. t)))
  15917. (defun org-move-line-down (arg)
  15918. "Move the current line down. With prefix argument, move it past ARG lines."
  15919. (interactive "p")
  15920. (let ((col (current-column))
  15921. beg end pos)
  15922. (beginning-of-line 1) (setq beg (point))
  15923. (beginning-of-line 2) (setq end (point))
  15924. (beginning-of-line (+ 1 arg))
  15925. (setq pos (move-marker (make-marker) (point)))
  15926. (insert (delete-and-extract-region beg end))
  15927. (goto-char pos)
  15928. (move-to-column col)))
  15929. (defun org-move-line-up (arg)
  15930. "Move the current line up. With prefix argument, move it past ARG lines."
  15931. (interactive "p")
  15932. (let ((col (current-column))
  15933. beg end pos)
  15934. (beginning-of-line 1) (setq beg (point))
  15935. (beginning-of-line 2) (setq end (point))
  15936. (beginning-of-line (- arg))
  15937. (setq pos (move-marker (make-marker) (point)))
  15938. (insert (delete-and-extract-region beg end))
  15939. (goto-char pos)
  15940. (move-to-column col)))
  15941. ;; Paragraph filling stuff.
  15942. ;; We want this to be just right, so use the full arsenal.
  15943. (defun org-set-autofill-regexps ()
  15944. (interactive)
  15945. ;; In the paragraph separator we include headlines, because filling
  15946. ;; text in a line directly attached to a headline would otherwise
  15947. ;; fill the headline as well.
  15948. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15949. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  15950. ;; The paragraph starter includes hand-formatted lists.
  15951. (org-set-local 'paragraph-start
  15952. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  15953. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15954. ;; But only if the user has not turned off tables or fixed-width regions
  15955. (org-set-local
  15956. 'auto-fill-inhibit-regexp
  15957. (concat "\\*\\|#"
  15958. "\\|[ \t]*" org-keyword-time-regexp
  15959. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15960. (concat
  15961. "\\|[ \t]*["
  15962. (if org-enable-table-editor "|" "")
  15963. (if org-enable-fixed-width-editor ":" "")
  15964. "]"))))
  15965. ;; We use our own fill-paragraph function, to make sure that tables
  15966. ;; and fixed-width regions are not wrapped. That function will pass
  15967. ;; through to `fill-paragraph' when appropriate.
  15968. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15969. ; Adaptive filling: To get full control, first make sure that
  15970. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15971. (org-set-local 'adaptive-fill-regexp "\000")
  15972. (org-set-local 'adaptive-fill-function
  15973. 'org-adaptive-fill-function))
  15974. (defun org-fill-paragraph (&optional justify)
  15975. "Re-align a table, pass through to fill-paragraph if no table."
  15976. (let ((table-p (org-at-table-p))
  15977. (table.el-p (org-at-table.el-p)))
  15978. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  15979. (table.el-p t) ; skip table.el tables
  15980. (table-p (org-table-align) t) ; align org-mode tables
  15981. (t nil)))) ; call paragraph-fill
  15982. ;; For reference, this is the default value of adaptive-fill-regexp
  15983. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15984. (defun org-adaptive-fill-function ()
  15985. "Return a fill prefix for org-mode files.
  15986. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15987. work correctly."
  15988. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  15989. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  15990. ;; Functions needed for Emacs/XEmacs region compatibility
  15991. (defun org-add-hook (hook function &optional append local)
  15992. "Add-hook, compatible with both Emacsen."
  15993. (if (and local (featurep 'xemacs))
  15994. (add-local-hook hook function append)
  15995. (add-hook hook function append local)))
  15996. (defun org-region-active-p ()
  15997. "Is `transient-mark-mode' on and the region active?
  15998. Works on both Emacs and XEmacs."
  15999. (if org-ignore-region
  16000. nil
  16001. (if (featurep 'xemacs)
  16002. (and zmacs-regions (region-active-p))
  16003. (and transient-mark-mode mark-active))))
  16004. (defun org-add-to-invisibility-spec (arg)
  16005. "Add elements to `buffer-invisibility-spec'.
  16006. See documentation for `buffer-invisibility-spec' for the kind of elements
  16007. that can be added."
  16008. (cond
  16009. ((fboundp 'add-to-invisibility-spec)
  16010. (add-to-invisibility-spec arg))
  16011. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  16012. (setq buffer-invisibility-spec (list arg)))
  16013. (t
  16014. (setq buffer-invisibility-spec
  16015. (cons arg buffer-invisibility-spec)))))
  16016. (defun org-remove-from-invisibility-spec (arg)
  16017. "Remove elements from `buffer-invisibility-spec'."
  16018. (if (fboundp 'remove-from-invisibility-spec)
  16019. (remove-from-invisibility-spec arg)
  16020. (if (consp buffer-invisibility-spec)
  16021. (setq buffer-invisibility-spec
  16022. (delete arg buffer-invisibility-spec)))))
  16023. (defun org-in-invisibility-spec-p (arg)
  16024. "Is ARG a member of `buffer-invisibility-spec'?"
  16025. (if (consp buffer-invisibility-spec)
  16026. (member arg buffer-invisibility-spec)
  16027. nil))
  16028. (defun org-image-file-name-regexp ()
  16029. "Return regexp matching the file names of images."
  16030. (if (fboundp 'image-file-name-regexp)
  16031. (image-file-name-regexp)
  16032. (let ((image-file-name-extensions
  16033. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16034. "xbm" "xpm" "pbm" "pgm" "ppm")))
  16035. (concat "\\."
  16036. (regexp-opt (nconc (mapcar 'upcase
  16037. image-file-name-extensions)
  16038. image-file-name-extensions)
  16039. t)
  16040. "\\'"))))
  16041. ;; Functions extending outline functionality
  16042. ;; C-a should go to the beginning of a *visible* line, also in the
  16043. ;; new outline.el. I guess this should be patched into Emacs?
  16044. (defun org-beginning-of-line ()
  16045. "Go to the beginning of the current line. If that is invisible, continue
  16046. to a visible line beginning. This makes the function of C-a more intuitive."
  16047. (interactive)
  16048. (beginning-of-line 1)
  16049. (if (bobp)
  16050. nil
  16051. (backward-char 1)
  16052. (if (org-invisible-p)
  16053. (while (and (not (bobp)) (org-invisible-p))
  16054. (backward-char 1)
  16055. (beginning-of-line 1))
  16056. (forward-char 1))))
  16057. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16058. (defun org-invisible-p ()
  16059. "Check if point is at a character currently not visible."
  16060. ;; Early versions of noutline don't have `outline-invisible-p'.
  16061. (if (fboundp 'outline-invisible-p)
  16062. (outline-invisible-p)
  16063. (get-char-property (point) 'invisible)))
  16064. (defun org-invisible-p2 ()
  16065. "Check if point is at a character currently not visible."
  16066. (save-excursion
  16067. (if (and (eolp) (not (bobp))) (backward-char 1))
  16068. ;; Early versions of noutline don't have `outline-invisible-p'.
  16069. (if (fboundp 'outline-invisible-p)
  16070. (outline-invisible-p)
  16071. (get-char-property (point) 'invisible))))
  16072. (defalias 'org-back-to-heading 'outline-back-to-heading)
  16073. (defalias 'org-on-heading-p 'outline-on-heading-p)
  16074. (defun org-on-target-p ()
  16075. (let ((pos (point)))
  16076. (save-excursion
  16077. (skip-chars-forward "<")
  16078. (and (re-search-backward "<<" nil t)
  16079. (or (looking-at org-radio-target-regexp)
  16080. (looking-at org-target-regexp))
  16081. (<= (match-beginning 0) pos)
  16082. (>= (1+ (match-end 0)) pos)))))
  16083. (defun org-up-heading-all (arg)
  16084. "Move to the heading line of which the present line is a subheading.
  16085. This function considers both visible and invisible heading lines.
  16086. With argument, move up ARG levels."
  16087. (if (fboundp 'outline-up-heading-all)
  16088. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16089. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16090. (defun org-goto-sibling (&optional previous)
  16091. "Goto the next sibling, even if it is invisible.
  16092. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16093. when a sibling was found. When none is found, return nil and don't
  16094. move point."
  16095. (let ((fun (if previous 're-search-backward 're-search-forward))
  16096. (pos (point))
  16097. (re (concat "^" outline-regexp))
  16098. level l)
  16099. (org-back-to-heading t)
  16100. (setq level (funcall outline-level))
  16101. (catch 'exit
  16102. (or previous (forward-char 1))
  16103. (while (funcall fun re nil t)
  16104. (setq l (funcall outline-level))
  16105. (when (< l level) (goto-char pos) (throw 'exit nil))
  16106. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16107. (goto-char pos)
  16108. nil)))
  16109. (defun org-show-hidden-entry ()
  16110. "Show an entry where even the heading is hidden."
  16111. (save-excursion
  16112. (org-show-entry)))
  16113. (defun org-flag-heading (flag &optional entry)
  16114. "Flag the current heading. FLAG non-nil means make invisible.
  16115. When ENTRY is non-nil, show the entire entry."
  16116. (save-excursion
  16117. (org-back-to-heading t)
  16118. ;; Check if we should show the entire entry
  16119. (if entry
  16120. (progn
  16121. (org-show-entry)
  16122. (save-excursion
  16123. (and (outline-next-heading)
  16124. (org-flag-heading nil))))
  16125. (outline-flag-region (max 1 (1- (point)))
  16126. (save-excursion (outline-end-of-heading) (point))
  16127. flag))))
  16128. (defun org-end-of-subtree (&optional invisible-OK)
  16129. ;; This is an exact copy of the original function, but it uses
  16130. ;; `org-back-to-heading', to make it work also in invisible
  16131. ;; trees. And is uses an invisible-OK argument.
  16132. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16133. (org-back-to-heading invisible-OK)
  16134. (let ((first t)
  16135. (level (funcall outline-level)))
  16136. (while (and (not (eobp))
  16137. (or first (> (funcall outline-level) level)))
  16138. (setq first nil)
  16139. (outline-next-heading))
  16140. (if (memq (preceding-char) '(?\n ?\^M))
  16141. (progn
  16142. ;; Go to end of line before heading
  16143. (forward-char -1)
  16144. (if (memq (preceding-char) '(?\n ?\^M))
  16145. ;; leave blank line before heading
  16146. (forward-char -1)))))
  16147. (point))
  16148. (defun org-show-subtree ()
  16149. "Show everything after this heading at deeper levels."
  16150. (outline-flag-region
  16151. (point)
  16152. (save-excursion
  16153. (outline-end-of-subtree) (outline-next-heading) (point))
  16154. nil))
  16155. (defun org-show-entry ()
  16156. "Show the body directly following this heading.
  16157. Show the heading too, if it is currently invisible."
  16158. (interactive)
  16159. (save-excursion
  16160. (org-back-to-heading t)
  16161. (outline-flag-region
  16162. (max 1 (1- (point)))
  16163. (save-excursion
  16164. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  16165. (or (match-beginning 1) (point-max)))
  16166. nil)))
  16167. (defun org-make-options-regexp (kwds)
  16168. "Make a regular expression for keyword lines."
  16169. (concat
  16170. "^"
  16171. "#?[ \t]*\\+\\("
  16172. (mapconcat 'regexp-quote kwds "\\|")
  16173. "\\):[ \t]*"
  16174. "\\(.+\\)"))
  16175. ;; Make `bookmark-jump' show the jump location if it was hidden.
  16176. (eval-after-load "bookmark"
  16177. '(if (boundp 'bookmark-after-jump-hook)
  16178. ;; We can use the hook
  16179. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16180. ;; Hook not available, use advice
  16181. (defadvice bookmark-jump (after org-make-visible activate)
  16182. "Make the position visible."
  16183. (org-bookmark-jump-unhide))))
  16184. (defun org-bookmark-jump-unhide ()
  16185. "Unhide the current position, to show the bookmark location."
  16186. (and (org-mode-p)
  16187. (or (org-invisible-p)
  16188. (save-excursion (goto-char (max (point-min) (1- (point))))
  16189. (org-invisible-p)))
  16190. (org-show-context 'bookmark-jump)))
  16191. ;; Make session.el ignore our circular variable
  16192. (eval-after-load "session"
  16193. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16194. ;;; Experimental code
  16195. ;;; Finish up
  16196. (provide 'org)
  16197. (run-hooks 'org-load-hook)
  16198. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16199. ;;; org.el ends here